-
Notifications
You must be signed in to change notification settings - Fork 0
/
SilabsBGAPIProtocol.py
393 lines (370 loc) · 17.5 KB
/
SilabsBGAPIProtocol.py
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import perilib
import struct
from .SilabsBGAPIPacket import *
class SilabsBGAPIProtocol(perilib.StreamProtocol):
"""Silicon Labs BGAPI protocol definition.
This class describes the binary BGAPI protocol used to communicate with
some of the modules from Bluegiga (now Silicon Labs), specifically BLE112,
BLE113, BLE121LR, WF121, WGM110, and BT121. This protocol follows a basic
command-response pattern, and also includes events sent from the module to
the host at unpredictable times when certain activities occur, such as a
new connection from a remote device, or incoming data.
Command/response pairs are defined together, each with their own set of
arguments. Events are defined in a separate dictionary and have only one
argument set. The protocol is divided by technology type (`ble`, `wifi`, and
`dumo` to match the definitions in the original BGAPI host implementations),
then subdivided into group and method names. Packet names are generated
using these categories along with the packet type, yielding names such as
`ble_cmd_system_hello` and `wifi_evt_endpoint_data`."""
header_args = [
{ "name": "type", "type": "uint8" },
{ "name": "length", "type": "uint8" },
{ "name": "group", "type": "uint8" },
{ "name": "method", "type": "uint8" }
]
response_packet_timeout = 0.5
incoming_packet_timeout = 1.2
commands = {
0x0: { # technology_type = 0x0 (BLE)
"name": "ble",
0x00: { # group_id = 0x00 (system)
"name": "system",
0x01: { # method_id = 0x01 (system_hello)
"name": "hello",
"response_required": "ble_rsp_system_hello",
"command_args": [],
"response_args": [],
},
},
},
0x1: { # technology_type = 0x1 (Wifi)
"name": "wifi",
},
0x4: { # technology_type = 0x4 (Dumo)
"name": "dumo",
0x01: { # group_id = 0x01 (system)
"name": "system",
0x00: { # method_id = 0x00 (system_hello)
"name": "hello",
"response_required": "dumo_rsp_system_hello",
"command_args": [],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
0x08: { # method_id = 0x08 (system_set_local_name)
"name": "set_local_name",
"response_required": "dumo_rsp_system_set_local_name",
"command_args": [
{ "name": "name", "type": "uint8a-l8v" },
],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
},
0x02: { # group_id = 0x02 (bt_gap)
"name": "bt_gap",
0x03: { # method_id = 0x03 (bt_gap_set_mode)
"name": "set_mode",
"response_required": "dumo_rsp_bt_gap_set_mode",
"command_args": [
{ "name": "connectable", "type": "uint8" },
{ "name": "discoverable", "type": "uint8" },
{ "name": "limited", "type": "uint8" },
],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
},
0x04: { # group_id = 0x04 (bt_rfcomm)
"name": "bt_rfcomm",
0x01: { # method_id = 0x01 (bt_rfcomm_start_server)
"name": "start_server",
"response_required": "dumo_rsp_bt_rfcomm_start_server",
"command_args": [
{ "name": "sdp_id", "type": "uint8" },
{ "name": "streaming_destination", "type": "uint8" },
],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
},
0x0B: { # group_id = 0x0B (endpoint)
"name": "endpoint",
0x00: { # method_id = 0x00 (endpoint_send)
"name": "send",
"response_required": "dumo_rsp_endpoint_send",
"command_args": [
{ "name": "endpoint", "type": "uint8" },
{ "name": "data", "type": "uint8a-l8v" },
],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
},
0x0C: { # group_id = 0x0C (hardware)
"name": "hardware",
0x08: { # method_id = 0x08 (hardware_set_uart_configuration)
"name": "set_uart_configuration",
"response_required": "dumo_rsp_hardware_set_uart_configuration",
"command_args": [
{ "name": "endpoint", "type": "uint8" },
{ "name": "rate", "type": "uint32" },
{ "name": "data_bits", "type": "uint8" },
{ "name": "stop_bits", "type": "uint8" },
{ "name": "parity", "type": "uint8" },
{ "name": "flow_ctrl", "type": "uint8" },
],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
},
0x0F: { # group_id = 0x0F (sm)
"name": "sm",
0x00: { # method_id = 0x00 (sm_set_bondable_mode)
"name": "set_bondable_mode",
"response_required": "dumo_rsp_sm_set_bondable_mode",
"command_args": [
{ "name": "bondable", "type": "uint8" },
],
"response_args": [
{ "name": "result", "type": "uint16" },
],
},
},
}
}
events = {
0x0: { # technology_type = 0x0 (BLE)
"name": "ble",
},
0x1: { # technology_type = 0x1 (Wifi)
"name": "wifi",
},
0x4: { # technology_type = 0x4 (Dumo)
"name": "dumo",
0x01: { # group_id = 0x01 (system)
"name": "system",
0x00: { # method_id = 0x00 (system_boot)
"name": "boot",
"event_args": [
{ "name": "major", "type": "uint16" },
{ "name": "minor", "type": "uint16" },
{ "name": "patch", "type": "uint16" },
{ "name": "build", "type": "uint16" },
{ "name": "bootloader", "type": "uint16" },
{ "name": "hw", "type": "uint16" },
],
},
0x01: { # method_id = 0x01 (system_initialized)
"name": "initialized",
"event_args": [
{ "name": "address", "type": "macaddr" },
],
},
0x02: { # method_id = 0x01 (system_recovery)
"name": "recovery",
"event_args": [
{ "name": "id1", "type": "uint32" },
{ "name": "id2", "type": "uint32" },
{ "name": "data", "type": "uint8a-l8v" },
],
},
},
0x04: { # group_id = 0x04 (bt_rfcomm)
"name": "bt_rfcomm",
0x01: { # method_id = 0x01 (bt_rfcomm_modem_status)
"name": "modem_status",
"event_args": [
{ "name": "endpoint", "type": "uint8" },
{ "name": "modem", "type": "uint8" },
],
},
},
0x07: { # group_id = 0x07 (bt_connection)
"name": "bt_connection",
0x00: { # method_id = 0x00 (bt_connection_opened)
"name": "opened",
"event_args": [
{ "name": "address", "type": "macaddr" },
{ "name": "master", "type": "uint8" },
{ "name": "connection", "type": "uint8" },
{ "name": "bonding", "type": "uint8" },
],
},
0x01: { # method_id = 0x01 (bt_connection_closed)
"name": "closed",
"event_args": [
{ "name": "reason", "type": "uint16" },
{ "name": "endpoint", "type": "uint8" },
],
},
0x02: { # method_id = 0x02 (bt_connection_parameters)
"name": "parameters",
"event_args": [
{ "name": "endpoint", "type": "uint8" },
{ "name": "block_size", "type": "uint32" },
{ "name": "msc", "type": "uint8" },
{ "name": "address", "type": "macaddr" },
{ "name": "direction", "type": "uint8" },
{ "name": "powermode", "type": "uint8" },
{ "name": "role", "type": "uint8" },
{ "name": "encryption", "type": "uint8" },
{ "name": "input_buffer", "type": "uint32" },
{ "name": "port", "type": "uint8" },
],
},
},
0x0B: { # group_id = 0x0B (endpoint)
"name": "endpoint",
0x00: { # method_id = 0x00 (endpoint_syntax_error)
"name": "syntax_error",
"event_args": [
{ "name": "result", "type": "uint16" },
{ "name": "endpoint", "type": "uint8" },
],
},
0x01: { # method_id = 0x01 (endpoint_data)
"name": "data",
"event_args": [
{ "name": "endpoint", "type": "uint8" },
{ "name": "data", "type": "uint8a-l8v" },
],
},
0x02: { # method_id = 0x02 (endpoint_status)
"name": "status",
"event_args": [
{ "name": "endpoint", "type": "uint8" },
{ "name": "type", "type": "uint32" },
{ "name": "destination_endpoint", "type": "int8" },
{ "name": "flags", "type": "uint8" },
],
},
0x03: { # method_id = 0x03 (endpoint_closing)
"name": "closing",
"event_args": [
{ "name": "reason", "type": "uint16" },
{ "name": "endpoint", "type": "uint8" },
],
},
0x04: { # method_id = 0x04 (endpoint_closed)
"name": "closed",
"event_args": [
{ "name": "endpoint", "type": "uint8" },
],
},
},
0x0F: { # group_id = 0x0F (sm)
"name": "sm",
0x03: { # method_id = 0x03 (sm_bonded)
"name": "bonded",
"event_args": [
{ "name": "connection", "type": "uint8" },
{ "name": "bonding", "type": "uint8" },
],
},
},
}
}
@classmethod
def test_packet_complete(cls, buffer, is_tx=False):
# make sure we have at least the header (4 bytes)
if len(buffer) > 3:
# check 11-bit "length" field in 4-byte header
(payload_length,) = struct.unpack(">H", buffer[0:2])
payload_length = payload_length & 0x3FF
if len(buffer) == payload_length + 4:
return perilib.ParseStatus.COMPLETE if buffer != "\x00\x00\x00\x00" else perilib.ParseStatus.IDLE
# not finished if we made it here
return perilib.ParseStatus.IN_PROGRESS
@classmethod
def get_packet_from_buffer(cls, buffer, parser_generator=None, is_tx=False):
(type_data, payload_length, group_id, method_id) = struct.unpack("4B", buffer[0:4])
message_type = (type_data & 0x80) >> 7
technology_type = (type_data & 0x78) >> 3
payload_length += (type_data & 0x07)
try:
if message_type == 0:
# command (TX) or response (RX) packet (or command, but for RX this is only response)
if is_tx:
packet_type = SilabsBGAPIPacket.TYPE_COMMAND
token = "cmd"
else:
packet_type = SilabsBGAPIPacket.TYPE_RESPONSE
token = "rsp"
packet_definition = SilabsBGAPIProtocol.commands[technology_type][group_id][method_id]
packet_name = "%s_%s_%s_%s" % ( \
SilabsBGAPIProtocol.commands[technology_type]["name"], \
token, \
SilabsBGAPIProtocol.commands[technology_type][group_id]["name"], \
SilabsBGAPIProtocol.commands[technology_type][group_id][method_id]["name"])
else:
# event packet (message_type == 1)
packet_type = SilabsBGAPIPacket.TYPE_EVENT
packet_definition = SilabsBGAPIProtocol.events[technology_type][group_id][method_id]
packet_name = "%s_evt_%s_%s" % ( \
SilabsBGAPIProtocol.events[technology_type]["name"], \
SilabsBGAPIProtocol.events[technology_type][group_id]["name"], \
SilabsBGAPIProtocol.events[technology_type][group_id][method_id]["name"])
except KeyError as e:
raise perilib.PerilibProtocolException(
"Could not find packet definition for %s with "
"technology type %d, group %d, and method %d" \
% (SilabsBGAPIPacket.TYPE_STR[packet_type], technology_type, group_id, method_id))
packet_definition["header_args"] = SilabsBGAPIProtocol.header_args
packet_metadata = {
"message_type": message_type,
"technology_type": technology_type,
"group_id": group_id,
"method_id": method_id,
}
# create packet instance
return SilabsBGAPIPacket(type=packet_type, name=packet_name, definition=packet_definition, buffer=buffer, metadata=packet_metadata, parser_generator=parser_generator)
@classmethod
def get_packet_from_name_and_args(cls, _packet_name, _parser_generator=None, **kwargs):
# split "ble_cmd_system_hello" into relevant parts
parts = _packet_name.split('_', maxsplit=2)
if len(parts) != 3:
raise perilib.PerilibProtocolException("Invalid packet name '%s' specified" % _packet_name)
# find the entry in the protocol definition table
(technology_type_str, message_type_str, short_name) = parts
packet_definition = None
if message_type_str == "evt":
search = SilabsBGAPIProtocol.events
message_type = 1
packet_type = SilabsBGAPIPacket.TYPE_EVENT
else:
search = SilabsBGAPIProtocol.commands
message_type = 0
if message_type_str == "rsp":
packet_type = SilabsBGAPIPacket.TYPE_RESPONSE
else:
packet_type = SilabsBGAPIPacket.TYPE_COMMAND
for technology_type in search:
if search[technology_type]["name"] == technology_type_str:
for group_id in search[technology_type]:
if type(group_id) == str:
continue
if search[technology_type][group_id]["name"] == short_name[:len(search[technology_type][group_id]["name"])]:
method_name = short_name[len(search[technology_type][group_id]["name"]) + 1:]
for method_id in search[technology_type][group_id]:
if type(method_id) == str:
continue
if search[technology_type][group_id][method_id]["name"] == method_name:
packet_definition = search[technology_type][group_id][method_id]
packet_definition["header_args"] = SilabsBGAPIProtocol.header_args
packet_metadata = {
"message_type": message_type,
"technology_type": technology_type,
"group_id": group_id,
"method_id": method_id,
}
# create packet instance
return SilabsBGAPIPacket(type=packet_type, name=_packet_name, definition=packet_definition, payload=kwargs, metadata=packet_metadata, parser_generator=_parser_generator)
# unable to find correct packet
raise perilib.PerilibProtocolException("Unable to locate packet definition for '%s'" % _packet_name)