/
slots.js
67 lines (56 loc) · 1.54 KB
/
slots.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var fs = require('fs');
var SLOTS_PATH = '/sys/devices/platform/bone_capemgr/slots',
FS_OPTIONS = {encoding: 'utf8'};
/**
* Returns the number of the slot with the specified name. Returns -1 if there
* is no such slot.
*
* name: string // Slot name
*
* Returns - number // Slot number or -1
*/
function number(name) {
var slots = fs.readFileSync(SLOTS_PATH, FS_OPTIONS).split('\n'),
i;
name = ',' + name;
// Search backwards. Added slots are more likely to be at end of slots file.
for (i = slots.length - 1; i >= 0; i -= 1) {
if (slots[i].indexOf(name, slots[i].length - name.length) !== -1) {
return parseInt(slots[i], 10);
}
}
return -1;
}
module.exports.number = number;
/**
* Adds a slot with the specified name if there is not already a slot with
* that name. Does nothing if there is already a slot with the specified name.
*
* name: string // Slot name
*
* Returns - undefined
*
* Throws ENOENT Errors if the required device-tree file does not exist in
* /lib/firmware
*/
module.exports.add = function (name) {
var slotNumber = number(name);
if (slotNumber === -1) {
fs.writeFileSync(SLOTS_PATH, name, FS_OPTIONS);
}
};
/**
* Removes the slot with the specified name if there is a slot with that name.
* Does nothing if there is no such slot.
*
* name: string // Slot name
*
* Returns - undefined
*/
module.exports.remove = function (name) {
var slotNumber = number(name);
if (slotNumber !== -1) {
fs.writeFileSync(SLOTS_PATH, '' + -slotNumber, FS_OPTIONS);
}
};