This repository was archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSBrickAdvertisementData.js
More file actions
105 lines (92 loc) · 2.75 KB
/
SBrickAdvertisementData.js
File metadata and controls
105 lines (92 loc) · 2.75 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const SBrickAdvertisementData = function () {
this.uuid = null;
this.hwVersion = null;
this.swVersion = null;
this.secured = false;
this.identifier = null;
};
SBrickAdvertisementData.parse = function (data) {
data = data instanceof Buffer ? data : new Buffer(data, 'hex');
if (SBrickAdvertisementData.winston) {
SBrickAdvertisementData.winston.debug(data.toString('hex'));
}
let i = 0;
let byteLength = 0;
let nextByteLength = 2;
let section = [];
let curSection = 0;
let advertisementData = new SBrickAdvertisementData();
for (let byte of data) {
if (i === nextByteLength) {
if (handleSection['s' + curSection]) {
handleSection['s' + curSection](advertisementData, section);
}
byteLength = byte;
nextByteLength = i + byteLength + 1;
section = [];
curSection++;
} else {
section.push(byte);
}
i++;
}
if (handleSection['s' + curSection]) {
handleSection['s' + curSection](advertisementData, section);
}
return advertisementData;
};
const handleSection = {
//header
s0: function (data, bytes) {
if (bytes[0] !== 152 || bytes[1] !== 1) {
throw new Error('not SBrick');
}
},
/*
00 Product type
00 <1: Product ID> <2: HW major/minor version> <2: FW major/minor version>
00 - SBrick
Example 1: 02 00 00 - Product SBrick
Example 2: 06 00 00 04 00 04 01 - Product SBrick, HW 4.0, FW 4.1
*/
s1: function (data, bytes) {
if (bytes[0] !== 0 || bytes[1] !== 0) {
throw new Error('not SBrick');
}
if (bytes.length > 2) {
data.hwVersion = parseFloat(bytes[2] + '.' + bytes[3]);
}
if (bytes.length > 4) {
data.swVersion = parseFloat(bytes[4] + '.' + bytes[5]);
}
},
/*
01 BlueGiga ADC sensor raw reading
01 <1: channel> <2: raw sensor reading>
Example, battery reading '12f0' on SBrick: 04 01 00 12 F0
Example, temperature reading '12f0': 04 01 0e 12 F0
*/
s2: function (data, bytes) {
},
/*
02 Device Identifier
02 < Device identifier string >
Example, SBrick device ID: 07 02 0D 23 FC 19 87 63
*/
s3: function (data, bytes) {
bytes.shift();
data.identifier = bytes.map(function (byte) {
return ('00' + byte.toString(16)).substr(-2);
}).join(':');
},
/*
03 Simple Security status
05 < status code >
00: Freely accessible
01: Authentication needed for some functions
*/
s4: function (data, bytes) {
data.secured = bytes[1] === 1;
}
};
module.exports = SBrickAdvertisementData;