-
Notifications
You must be signed in to change notification settings - Fork 15
/
command-definitions.js
305 lines (250 loc) · 7.13 KB
/
command-definitions.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
'use strict'
const binary = require('./util/binary')
const util = require('./util/util')
const buffer = require('buffer')
const yaml = require('js-yaml')
const fs = require('fs')
const path = require('path')
const logger = require('./util/logs').logger
const DEFINITIONS_FILE = path.join(__dirname, 'resources', 'command-definitions.yaml')
const MESSAGE_RESPONSE_BIT = 0x80
let _defns = null
function _loadDefinitions() {
try {
let commandDefs = yaml.safeLoad(fs.readFileSync(DEFINITIONS_FILE, 'utf8'))
_defns = commandDefs
} catch (e) {
logger.info(`Failed to load definitions: ${e}`)
}
}
function _binaryField(type) {
let binaryField = null
switch (type) {
case 'uint8':
binaryField = binary.UInt8
break
case 'uint16':
binaryField = binary.UInt16
break
case 'uint32':
binaryField = binary.UInt32
break
case 'int16':
binaryField = binary.Int16
break
case 'padded_string':
binaryField = binary.PaddedString
break
case 'fixed_length_bytes':
binaryField = binary.FixedLengthBytes
break
case 'variable_length_bytes':
binaryField = binary.VariableLengthBytes
break
default:
logger.info(`No binary type found: ${type}`)
}
return binaryField
}
class Message {
/**
* Represents a LightBlue Serial Transport Message
*
* Defined as:
*
* [1 byte] - Length (Message ID + Payload)
* [1 byte] - Reserved
* [2 byte] BE - Message ID
* [0-64 bytes] LE - Payload
* [2 bytes] LE - CRC (Everything before CRC)
*
* @param messageId
* @param definition
*/
constructor(messageId, args, definition) {
this._msgId = messageId
this._args = args
this._definition = definition
}
static _unpackCRC(buf) {
let crcValue = buf.readUInt16LE(buf.length - 2)
let crcBuf = buf.slice(0, buf.length - 2)
let calcedCrc = util.crc16(crcBuf)
if (crcValue != calcedCrc) {
throw new Error(`CRC Mismatch: ${crcValue} != ${calcedCrc}`)
}
}
static _unpackLength(buf) {
return buf.readUInt8(0)
}
static _unpackReserved(buf) {
return buf.readUInt8(1)
}
static _unpackMessageId(buf) {
let messageId = buf.readUInt16BE(2)
return messageId & ~MESSAGE_RESPONSE_BIT
}
static _unpackPayload(buf, payloadLength, argDefn) {
let argsLength = payloadLength - 2 // Subtract message ID from length
let payloadBuf = buf.slice(4, 4 + argsLength)
let offset = 0
let argValues = []
for (let fieldDefinition of argDefn) {
let Field = _binaryField(fieldDefinition.type)
let binaryField = Field.fromBuffer(payloadBuf, offset, fieldDefinition)
offset += binaryField.size()
argValues.push(binaryField.getValue())
}
return argValues
}
_packLength(payloadLength) {
let buf = new buffer.Buffer(1)
buf.writeUInt8(payloadLength + 2, 0) // Length = payload + msg ID
return buf
}
_packReserved() {
let buf = new buffer.Buffer(1)
buf.writeUInt8(0, 0)
return buf
}
_packMessageId() {
let buf = new buffer.Buffer(2)
buf.writeUInt16BE(this._msgId)
return buf
}
_packPayload(args) {
let packedArgs = []
let totalLength = 0
for (let index in args) {
let fieldDefinition = this._definition.arguments[index]
let value = args[index]
let Field = _binaryField(fieldDefinition.type)
let binaryField = new Field(value, fieldDefinition)
packedArgs.push(binaryField.pack())
totalLength += binaryField.size()
}
return buffer.Buffer.concat(packedArgs, totalLength)
}
_packCRC(crcBuf) {
let crcValue = util.crc16(crcBuf)
let buf = new buffer.Buffer(2)
buf.writeUInt16LE(crcValue)
return buf
}
getMessageId() {
return this._msgId
}
getArguments() {
return this._args
}
getDefinition() {
return this._definition
}
static fromBuffer(buf) {
throw new Error("Subclasses must implement static method .fromBuffer(buf)")
}
pack() {
throw new Error("Subclasses must implement method .pack()")
}
asObject(defn) {
let obj = {}
for (let idx in defn) {
let fieldDefn = defn[idx]
let fieldVal = this._args[idx]
obj[fieldDefn.name] = fieldVal
}
return obj
}
}
class Response extends Message {
static fromBuffer(buf, defn) {
let crc = this._unpackCRC(buf)
let length = this._unpackLength(buf)
let reserved = this._unpackReserved(buf)
let messageId = this._unpackMessageId(buf)
let payloadArgs = this._unpackPayload(buf, length, defn.response)
return new Response(messageId, payloadArgs, defn)
}
pack() {
throw new Error("Not Implemented!")
}
}
class Command extends Message {
static fromBuffer(buf, defn) {
let crc = this._unpackCRC(buf)
let length = this._unpackLength(buf)
let reserved = this._unpackReserved(buf)
let messageId = this._unpackMessageId(buf)
let payloadArgs = this._unpackPayload(buf, length, defn.arguments)
return new Command(messageId, payloadArgs, defn)
}
pack() {
let messageId = this._packMessageId()
let payload = this._packPayload(this._args)
let length = this._packLength(payload.length)
let reserved = this._packReserved()
let crc = this._packCRC(util.concatBuffers([length, reserved, messageId, payload]))
return util.concatBuffers([length, reserved, messageId, payload, crc])
}
}
function definitionForCommand(cmdId) {
if (!_defns) {
_loadDefinitions()
}
if (_defns.incoming[cmdId])
return _defns.incoming[cmdId]
if (_defns.outgoing[cmdId])
return _defns.outgoing[cmdId]
throw new Error(`No definition for command ID: ${cmdId}`)
}
function definitions() {
if (!_defns) {
_loadDefinitions()
}
return _defns
}
const commandIds = {
SERIAL_DATA: 0x0000,
LB_PROTOCOL_ERROR: 0x0001,
BT_SET_ADV: 0x0500,
BT_SET_CONN: 0x0502,
BT_SET_LOCAL_NAME: 0x0504,
BT_SET_PIN: 0x0506,
BT_SET_TX_PWR: 0x0508,
BT_GET_CONFIG: 0x0510,
BT_SET_CONFIG: 0x0511,
BT_SET_CONFIG_NOSAVE: 0x0540,
BT_END_GATE: 0x0550,
BT_ADV_ONOFF: 0x0512,
BT_SET_SCRATCH: 0x0514,
BT_GET_SCRATCH: 0x0515,
BT_RESTART: 0x0520,
BL_CMD_START: 0x1000,
BL_FW_BLOCK: 0x1001,
BL_STATUS: 0x1002,
BL_GET_META: 0x1003,
CC_LED_WRITE: 0x2000,
CC_LED_WRITE_ALL: 0x2001,
CC_LED_READ_ALL: 0x2002,
CC_ACCEL_READ: 0x2010,
CC_TEMP_READ: 0x2011,
CC_BATT_READ: 0x2015,
CC_POWER_ARDUINO: 0x2020,
CC_GET_AR_POWER: 0x2021,
CC_ACCEL_GET_RANGE: 0x2030,
CC_ACCEL_SET_RANGE: 0x2035,
AR_SLEEP: 0x3000,
ERROR_CC: 0x4000,
DB_LOOPBACK: 0xFE00,
DB_COUNTER: 0xFE01,
DB_E2E_LOOPBACK: 0xFE02,
DB_PTM: 0xFE03
}
module.exports = {
commandIds: commandIds,
definitionForCommand: definitionForCommand,
definitions: definitions,
Command: Command,
Response: Response,
MESSAGE_RESPONSE_BIT: MESSAGE_RESPONSE_BIT
}