-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathbglib.device.nut
2003 lines (1679 loc) · 94.2 KB
/
bglib.device.nut
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2014 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
/*
BGLib for Squirrel
==================
This implements the BGLib library for Bluegiga's BLE112 Bluetooth Smart module.
It assumes you have connected from the Imp to the BLE112:
- UART (uart1289 is recommended as flow control is important)
- Wake pin (optional depending on BLE112 configuration)
- Reset pin (optional but really helpful as software reset is not always reliable)
The wake control is made available but not fully automated. If you want to control
the sleep cycles from BGLib then you will need to pull the wake pin high before
sending a command and wait for "hardware_io_port_status" events to indicate the
BLE112 is awake. After completing the command, sent the pin low again to let the
device go back to sleep when it is ready.
Packet mode should be configured to match the device configuration. If there is
no flow control you should turn packet mode on.
Some useful references:
- [Bluetooth crash course] (http://flyingcarsandstuff.com/projects/bluetooth-low-energy/bluetooth-smartble-crash-course/)
- [Bluetooth specs] (https://developer.bluetooth.org/gatt/Pages/Definition-Browser.aspx)
- [BLE112 v.1.2.2 API Reference] (https://www.bluegiga.com/en-US/download/?file=P84Ulj3ZRiyiFv4TU51uVA)
- [BLE112 Datasheets and App notes] (https://www.bluegiga.com/en-US/products/bluetooth-4.0-modules/ble112-bluetooth--smart-module/documentation/)
- [BGLib/BGAPI explanation] (https://bluegiga.zendesk.com/entries/22412436--REFERENCE-What-is-the-difference-between-BGScript-BGAPI-and-BGLib)
- [BGAPI protocol breakdown] (https://bluegiga.zendesk.com/entries/23791201-BGAPI-how-to-composite-BGAPI-binary-commands)
- [Bluegiga forum] (https://bluegiga.zendesk.com/forums/21316731-Bluetooth-Smarth)
- [Arduino BGLib] (https://github.com/jrowberg/bglib/blob/master/Arduino/BGLib.cpp)
To do list:
- Time-out requests as sometimes things go wrong.
*/
//------------------------------------------------------------------------------
class BGLib {
_uart = null;
_wake = null;
_reset_l = null;
_packet_m = false;
_baud = null;
_response_callbacks = null;
_event_callbacks = null;
_uart_buffer = null;
// -------------------------------------------------------------------------
constructor(uart, wake, reset_l, packet_m = false, baud = 57600) {
init();
_uart = uart;
_wake = wake;
_reset_l = reset_l;
_packet_m = packet_m;
_baud = baud;
_response_callbacks = [];
_event_callbacks = {};
_uart_buffer = "";
_uart.configure(_baud, 8, PARITY_NONE, 1, 0, read_uart.bindenv(this));
if (_wake) {
_wake.configure(DIGITAL_OUT);
_wake.write(1); // Pull high to keep awake
}
if (_reset_l) {
// Tristate the pin for now
_reset_l.configure(DIGITAL_IN);
}
}
// -------------------------------------------------------------------------
function init() {
const BLE_TIMEOUT = 20;
const BLE_MAX_PAYLOAD = 0x7FF;
const BLE_HEADER_SIZE = 4;
const BLE_CONN_BACKOFF = 60;
const BLE_DUMP_MAX = 200;
enum BLE_CLASS_ID {
SYSTEM = 0x00, // Provides access to system functions
PERSISTENT = 0x01, // Provides access the persistence store (parameters)
ATT_DB = 0x02, // Provides access to local GATT database
CONNECTION = 0x03, // Provides access to connection management functions
ATT_CLIENT = 0x04, // Functions to access remote devices GATT database
SECURITY = 0x05, // Bluetooth low energy security functions
GAP = 0x06, // GAP functions
HARDWARE = 0x07, // Provides access to hardware such as timers and ADC
TEST = 0x08, // Not implemented
DFU = 0x09 // Provides tools for uploading new programming over USART
}
enum BLE_MESSAGE_TYPE {
COMMAND = 0x00, // Commands and Command Responses
EVENT = 0x80 // Event notifications
}
enum BLE_ERRORS {
INVALID_PARAMETER = 0x0180,
DEVICE_IN_WRONG_STATE = 0x0181,
OUT_OF_MEMORY = 0x0182,
FEATURE_NOT_IMPLEMENTED = 0x0183,
COMMAND_NOT_RECOGNIZED = 0x0184,
TIMEOUT = 0x0185,
NOT_CONNECTED = 0x0186,
FLOW = 0x0187,
USER_ATTRIBUTE = 0x0188,
INVALID_LICENSE_KEY = 0x0189,
COMMAND_TOO_LONG = 0x018A,
OUT_OF_BONDS = 0x018B,
AUTHENTICATION_FAILURE = 0x0205,
PIN_OR_KEY_MISSING = 0x0206,
MEMORY_CAPACITY_EXCEEDED = 0x0207,
CONNECTION_TIMEOUT = 0x0208,
CONNECTION_LIMIT_EXCEEDED = 0x0209,
COMMAND_DISALLOWED = 0x020C,
INVALID_COMMAND_PARAMETERS = 0x0212,
REMOTE_USER_TERMINATED_CONNECTION = 0x0213,
CONNECTION_TERMINATED_BY_LOCAL_HOST = 0x0216,
LL_RESPONSE_TIMEOUT = 0x0222,
LL_INSTANT_PASSED = 0x0228,
CONTROLLER_BUSY = 0x023A,
UNACCEPTABLE_CONNECTION_INTERVAL = 0x023B,
DIRECTED_ADVERTISING_TIMEOUT = 0x023C,
MIC_FAILURE = 0x023D,
CONNECTION_FAILED_TO_BE_ESTABLISHED = 0x023E,
PASSKEY_ENTRY_FAILED = 0x0301,
OOB_DATA_IS_NOT_AVAILABLE = 0x0302,
AUTHENTICATION_REQUIREMENTS = 0x0303,
CONFIRM_VALUE_FAILED = 0x0304,
PAIRING_NOT_SUPPORTED = 0x0305,
ENCRYPTION_KEY_SIZE = 0x0306,
COMMAND_NOT_SUPPORTED = 0x0307,
UNSPECIFIED_REASON = 0x0308,
REPEATED_ATTEMPTS = 0x0309,
INVALID_PARAMETERS = 0x030A,
INVALID_HANDLE = 0x0401,
READ_NOT_PERMITTED = 0x0402,
WRITE_NOT_PERMITTED = 0x0403,
INVALID_PDU = 0x0404,
INSUFFICIENT_AUTHENTICATION = 0x0405,
REQUEST_NOT_SUPPORTED = 0x0406,
INVALID_OFFSET = 0x0407,
INSUFFICIENT_AUTHORIZATION = 0x0408,
PREPARE_QUEUE_FULL = 0x0409,
ATTRIBUTE_NOT_FOUND = 0x040A,
ATTRIBUTE_NOT_LONG = 0x040B,
INSUFFICIENT_ENCRYPTION_KEY_SIZE = 0x040C,
INVALID_ATTRIBUTE_VALUE_LENGTH = 0x040D,
UNLIKELY_ERROR = 0x040E,
INSUFFICIENT_ENCRYPTION = 0x040F,
UNSUPPORTED_GROUP_TYPE = 0x0410,
INSUFFICIENT_RESOURCES = 0x0411,
APPLICATION_ERROR_CODES = 0x0480
}
enum BLE_SYSTEM_ENDPOINTS
{
SYSTEM_ENDPOINT_API = 0,
SYSTEM_ENDPOINT_TEST = 1,
SYSTEM_ENDPOINT_SCRIPT = 2,
SYSTEM_ENDPOINT_USB = 3,
SYSTEM_ENDPOINT_UART0 = 4,
SYSTEM_ENDPOINT_UART1 = 5
};
enum BLE_ATTRIBUTES_ATTRIBUTE_CHANGE_REASON
{
ATTRIBUTES_ATTRIBUTE_CHANGE_REASON_WRITE_REQUEST = 0,
ATTRIBUTES_ATTRIBUTE_CHANGE_REASON_WRITE_COMMAND = 1,
ATTRIBUTES_ATTRIBUTE_CHANGE_REASON_WRITE_REQUEST_USER = 2
};
enum BLE_ATTRIBUTES_ATTRIBUTE_STATUS_FLAG
{
ATTRIBUTES_ATTRIBUTE_STATUS_FLAG_NOTIFY = 1,
ATTRIBUTES_ATTRIBUTE_STATUS_FLAG_INDICATE = 2
};
enum BLE_CONNECTION_CONNSTATUS
{
CONNECTION_CONNECTED = 1,
CONNECTION_ENCRYPTED = 2,
CONNECTION_COMPLETED = 4,
CONNECTION_PARAMETERS_CHANGE = 8
};
enum BLE_ATTCLIENT_ATTRIBUTE_VALUE_TYPES
{
ATTCLIENT_ATTRIBUTE_VALUE_TYPE_READ = 0,
ATTCLIENT_ATTRIBUTE_VALUE_TYPE_NOTIFY = 1,
ATTCLIENT_ATTRIBUTE_VALUE_TYPE_INDICATE = 2,
ATTCLIENT_ATTRIBUTE_VALUE_TYPE_READ_BY_TYPE = 3,
ATTCLIENT_ATTRIBUTE_VALUE_TYPE_READ_BLOB = 4,
ATTCLIENT_ATTRIBUTE_VALUE_TYPE_INDICATE_RSP_REQ = 5
};
enum BLE_SM_BONDING_KEY
{
SM_BONDING_KEY_LTK = 0X01,
SM_BONDING_KEY_ADDR_PUBLIC = 0X02,
SM_BONDING_KEY_ADDR_STATIC = 0X04,
SM_BONDING_KEY_IRK = 0X08,
SM_BONDING_KEY_EDIVRAND = 0X10,
SM_BONDING_KEY_CSRK = 0X20,
SM_BONDING_KEY_MASTERID = 0X40
};
enum BLE_SM_IO_CAPABILITY
{
SM_IO_CAPABILITY_DISPLAYONLY = 0,
SM_IO_CAPABILITY_DISPLAYYESNO = 1,
SM_IO_CAPABILITY_KEYBOARDONLY = 2,
SM_IO_CAPABILITY_NOINPUTNOOUTPUT = 3,
SM_IO_CAPABILITY_KEYBOARDDISPLAY = 4
};
enum BLE_GAP_ADDRESS_TYPE
{
GAP_ADDRESS_TYPE_PUBLIC = 0,
GAP_ADDRESS_TYPE_RANDOM = 1
};
enum BLE_GAP_DISCOVERABLE_MODE
{
GAP_NON_DISCOVERABLE = 0,
GAP_LIMITED_DISCOVERABLE = 1,
GAP_GENERAL_DISCOVERABLE = 2,
GAP_BROADCAST = 3,
GAP_USER_DATA = 4
};
enum BLE_GAP_CONNECTABLE_MODE
{
GAP_NON_CONNECTABLE = 0,
GAP_DIRECTED_CONNECTABLE = 1,
GAP_UNDIRECTED_CONNECTABLE = 2,
GAP_SCANNABLE_CONNECTABLE = 3
};
enum BLE_GAP_DISCOVER_MODE
{
GAP_DISCOVER_LIMITED = 0,
GAP_DISCOVER_GENERIC = 1,
GAP_DISCOVER_OBSERVATION = 2
};
enum BLE_GAP_AD_TYPES
{
GAP_AD_TYPE_NONE = 0,
GAP_AD_TYPE_FLAGS = 1,
GAP_AD_TYPE_SERVICES_16BIT_MORE = 2,
GAP_AD_TYPE_SERVICES_16BIT_ALL = 3,
GAP_AD_TYPE_SERVICES_32BIT_MORE = 4,
GAP_AD_TYPE_SERVICES_32BIT_ALL = 5,
GAP_AD_TYPE_SERVICES_128BIT_MORE = 6,
GAP_AD_TYPE_SERVICES_128BIT_ALL = 7,
GAP_AD_TYPE_LOCALNAME_SHORT = 8,
GAP_AD_TYPE_LOCALNAME_COMPLETE = 9,
GAP_AD_TYPE_TXPOWER = 10,
GAP_AD_TYPE_MANUFACTURER_DATA = 255
};
enum BLE_GAP_ADVERTISING_POLICY
{
GAP_ADV_POLICY_ALL = 0,
GAP_ADV_POLICY_WHITELIST_SCAN = 1,
GAP_ADV_POLICY_WHITELIST_CONNECT = 2,
GAP_ADV_POLICY_WHITELIST_ALL = 3
};
enum BLE_GAP_SCAN_POLICY
{
GAP_SCAN_POLICY_ALL = 0,
GAP_SCAN_POLICY_WHITELIST = 1
};
enum BLE_PARAMETER_TYPES
{
BLE_MSG_PARAMETER_UINT8 = 2,
BLE_MSG_PARAMETER_INT8 = 3,
BLE_MSG_PARAMETER_UINT16 = 4,
BLE_MSG_PARAMETER_INT16 = 5,
BLE_MSG_PARAMETER_UINT32 = 6,
BLE_MSG_PARAMETER_INT32 = 7,
BLE_MSG_PARAMETER_UINT8ARRAY = 8,
BLE_MSG_PARAMETER_STRING = 9,
BLE_MSG_PARAMETER_HWADDR = 10
};
}
// -------------------------------------------------------------------------
function log(type, message) {
if ("log" in _event_callbacks) {
_event_callbacks.log(type, message);
} else if (type == "ERR") {
server.error(format("%s: %s", type, message));
} else if (type == "SEND" || type == "RECV") {
server.log(format("%s: %s", type, hexdump(message)));
} else {
server.log(format("%s: %s", type, message));
}
}
// -------------------------------------------------------------------------
function hexdump(dump, ascii = true) {
local dbg = "";
foreach (ch in dump) {
dbg += format("%02x ", ch)
if (ch >= 32 && ch <= 126 && ascii) dbg += format("[%c] ", ch);
if (dbg.len() > BLE_DUMP_MAX) {
dbg += "... ";
break;
}
}
return (dbg.len() > 0) ? dbg.slice(0, -1) : "";
}
//------------------------------------------------------------------------------------------------------------------------------
function hex_to_int(str) {
// Parses a hex string and turns it into an integer
local hex = 0x0000;
foreach (ch in str.toupper()) {
local nibble;
if (ch >= '0' && ch <= '9') {
nibble = (ch - '0');
} else {
nibble = (ch - 'A' + 10);
}
hex = (hex << 4) + nibble;
}
return hex;
}
//------------------------------------------------------------------------------------------------------------------------------
function string_to_addr(address) {
assert(address.len() == 17);
return format("%c%c%c%c%c%c",
hex_to_int(address.slice(15,17)),
hex_to_int(address.slice(12,14)),
hex_to_int(address.slice( 9,11)),
hex_to_int(address.slice( 6, 8)),
hex_to_int(address.slice( 3, 5)),
hex_to_int(address.slice( 0, 2))
);
}
//------------------------------------------------------------------------------------------------------------------------------
function addr_to_string(payload) {
assert(payload.len() == 6);
return format("%02x:%02x:%02x:%02x:%02x:%02x",
payload[5],
payload[4],
payload[3],
payload[2],
payload[1],
payload[0]);
}
//------------------------------------------------------------------------------------------------------------------------------
function addr_type_to_string(addr_type) {
return (addr_type == 0) ? "public" : "random";
}
//------------------------------------------------------------------------------------------------------------------------------
function string_to_addr_type(addr_type) {
return (addr_type == "public") ? 0 : 1;
}
// -------------------------------------------------------------------------
function halt() {
if (_reset_l) {
_reset_l.configure(DIGITAL_OUT);
_reset_l.write(0);
}
}
// -------------------------------------------------------------------------
function reboot() {
if (_reset_l) {
_reset_l.configure(DIGITAL_OUT);
_reset_l.write(0);
imp.wakeup(0.1, function() {
_reset_l.write(1);
_reset_l.configure(DIGITAL_IN);
_uart_buffer = "";
}.bindenv(this))
}
}
// -------------------------------------------------------------------------
function wake() {
if (_wake) _wake.write(1);
}
// -------------------------------------------------------------------------
function sleep() {
if (_wake) _wake.write(0);
}
// -------------------------------------------------------------------------
function fire_response(event) {
// Parse out the result
local result = "unknown";
if ("result" in event) {
switch (event.result) {
case 0x00:
result = "OK";
break;
case "timeout":
result = "timeout";
break;
default:
if (typeof event.result == "integer") {
result = format("Error 0x%04x", event.result);
}
break;
}
}
// Find the original callback in the queue and fire it
for (local i = 0; i < _response_callbacks.len(); i++) {
local cb = _response_callbacks[i];
if (cb.cid == event.cid && cb.cmd == event.cmd) {
imp.cancelwakeup(cb.timer); cb.timer = null;
_response_callbacks.remove(i);
if (cb.callback != null) {
log("LOG", format("resp %s: %s", event.name, result));
result = null;
cb.callback(event);
}
break;
}
}
if (result != null) {
log("LOG", format("resp %s: %s (unhandled)", event.name, result))
}
}
// -------------------------------------------------------------------------
function fire_event(event) {
if (event.cid == BLE_CLASS_ID.SYSTEM && event.cmd == 0) {
// After the system_boot event the device has just booted so we
// have no use for old callbacks. Clear them.
_response_callbacks.clear();
}
// Find the event handler registered and fire it
if (event.name in _event_callbacks) {
log("LOG", "event " + event.name);
_event_callbacks[event.name](event);
} else {
log("LOG", "event " + event.name + " (unhandled)");
}
}
// -------------------------------------------------------------------------
function send_command(name, cid, cmd, payload, callback = null) {
log("LOG", format("call %s", name));
// Queue the callback, build the packet and send it off
local command = {name=name, cid=cid, cmd=cmd, callback=callback};
local timer = imp.wakeup(BLE_TIMEOUT, function() {
// The timeout has expired. Send an event.
command.result <- "timeout";
fire_response(command);
}.bindenv(this));
command.timer <- timer;
_response_callbacks.push(command)
local len = payload == null ? 0 : payload.len();
local header = format("%c%c%c%c", (len >> 8) & 0x07, len & 0xFF, cid, cmd);
uart_write(header, payload);
}
// -------------------------------------------------------------------------
function on(event, callback) {
if (callback == null) {
if (event in _event_callbacks) {
delete _event_callbacks[event];
}
} else {
_event_callbacks[event] <- callback;
}
}
// -------------------------------------------------------------------------
function uart_write(header, payload) {
log("SEND", payload == null ? header : header + payload);
local packet_size = null;
if (_packet_m) {
if (payload == null) {
packet_size = format("%c", header.len(), 0x20);
} else {
packet_size = format("%c", header.len() + payload.len(), 0x20);
}
}
if (packet_size != null) _uart.write(packet_size);
_uart.write(header);
if (payload != null) _uart.write(payload);
}
// -------------------------------------------------------------------------
function read_uart() {
// Read the complete UART buffer
local ch = null;
while ((ch = _uart.read()) != -1) {
_uart_buffer += format("%c", ch);
// We can back off reading more than this many bytes into a buffer as
// flow control will stop the other side.
if (_uart_buffer.len() >= 0x7FF + 4) {
break;
}
}
if (_uart_buffer.len() == 0) return;
while (_uart_buffer.len() >= 4) {
// If we have at least enough for the header, then try parsing the buffer
local event = null;
try {
event = parse_packet(_uart_buffer);
} catch (e) {
log("ERR", "Caught exception while parsing the UART buffer: " + e);
throw "Caught exception while parsing the UART buffer: " + e;
}
if (event != null) {
log("RECV", _uart_buffer.slice(0, event.length + 4));
_uart_buffer = _uart_buffer.slice(event.length + 4)
// We have a workable buffer, send it down the right path
if (event.msg_type == BLE_MESSAGE_TYPE.COMMAND) {
fire_response(event);
} else {
fire_event(event);
}
} else {
// Skipped an incomplete packet. Wait for it to fill up properly.
// log("RECV", hexdump(_uart_buffer) + " (skipped)");
break;
}
}
}
// -------------------------------------------------------------------------
function parse_packet(buffer) {
// Parse the header
local event = {};
event.msg_type <- (buffer[0] & 0x80);
event.tech_type <- (buffer[0] & 0x78) >> 3;
event.length <- ((buffer[0] & 0x07) << 8) + buffer[1];
event.cid <- buffer[2];
event.cmd <- buffer[3];
event.name <- "unknown";
event.result <- 0;
event.payload <- {};
local payload = null;
if (event.length > 0) {
if (buffer.len() >= 4 + event.length) {
payload = buffer.slice(4, 4 + event.length);
} else {
// The packet is incomplete
return null;
}
}
// Command responses
switch (event.msg_type) {
case BLE_MESSAGE_TYPE.COMMAND:
switch (event.cid) {
case BLE_CLASS_ID.SYSTEM:
switch (event.cmd) {
case 1: // system_hello response
event.name <- "system_hello";
break;
case 2: // system_address_get response
event.payload.address <- addr_to_string(payload.slice(0, 6));
event.name <- "system_address_get";
break;
case 3: // system_reg_write response
event.name <- "system_reg_write";
break;
case 4: // system_reg_read response
event.name <- "system_reg_read";
break;
case 5: // system_get_counters response
event.payload.txok <- payload[0];
event.payload.txretry <- payload[1];
event.payload.rxok <- payload[2];
event.payload.rxfail <- payload[3];
event.payload.mbuf <- payload[4];
event.name <- "system_get_connections";
break;
case 6: // system_get_connections response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "system_get_connections";
break;
case 7: // system_read_memory
event.name <- "system_read_memory";
break;
case 8: // system_get_info response
event.payload.major <- payload[0] + (payload[1] << 8);
event.payload.minor <- payload[2] + (payload[3] << 8);
event.payload.patch <- payload[4] + (payload[5] << 8);
event.payload.build <- payload[6] + (payload[7] << 8);
event.payload.ll_version <- payload[8] + (payload[9] << 8);
event.payload.protocol_version <- payload[10];
event.payload.hw <- payload[11];
event.name <- "system_get_info";
break;
case 9: // system_endpoint_tx response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "system_endpoint_tx";
break;
case 10: // system_whitelist_append response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "system_whitelist_append";
break;
case 11: // system_whitelist_remove response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "system_whitelist_remove";
break;
case 12: // system_whitelist_clear response
event.name <- "system_whitelist_clear";
break;
case 13: // system_endpoint_rx response
event.result <- payload[0] + (payload[1] << 8);
event.payload.data <- payload.slice(3)
event.name <- "system_endpoint_rx";
break;
case 14: // system_endpoint_set_watermarks response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "system_endpoint_set_watermarks";
break;
}
break;
/*
case BLE_CLASS_ID.PERSISTENT:
switch (event.cmd) {
case 0: // flash_ps_defrag response
event.name <- "flash_ps_defrag";
break;
case 1: // flash_ps_dump response
event.name <- "flash_ps_dump";
break;
case 2: // flash_ps_erase_all response
event.name <- "flash_ps_erase_all";
break;
case 3: // flash_ps_save response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "flash_ps_save";
break;
case 4: // flash_ps_load response
event.result <- payload[0] + (payload[1] << 8);
event.payload.value <- payload.slice(3);
event.name <- "flash_ps_load";
break;
case 5: // flash_ps_erase response
event.name <- "flash_ps_erase";
break;
case 6: // flash_erase_page response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "flash_erase_page";
break;
case 7: // flash_write_data response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "flash_write_data";
break;
case 8: // flash_read_data response
event.payload.value <- payload.slice(1);
event.name <- "flash_read_data";
break;
}
break;
*/
case BLE_CLASS_ID.ATT_DB:
switch(event.cmd) {
case 0: // attributes_write response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "attributes_write";
break;
case 1: // attributes_read response
event.payload.handle <- payload[0] + (payload[1] << 8);
event.payload.offset <- payload[2] + (payload[3] << 8);
event.result <- payload[4] + (payload[5] << 8);
event.payload.value <- payload.slice(7);
event.name <- "attributes_read";
break;
case 2: // attributes_read_type response
event.payload.handle <- payload[0] + (payload[1] << 8);
event.result <- payload[2] + (payload[3] << 8);
event.payload.value <- payload.slice(5);
event.name <- "attributes_read_type";
break;
case 3: // attributes_user_read_response response
event.name <- "attributes_user_read_response";
break;
case 4: // attributes_user_write_response response
event.name <- "attributes_user_write_response";
break;
}
break;
case BLE_CLASS_ID.CONNECTION:
switch(event.cmd) {
case 0: // connection_disconnect response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "connection_disconnect";
break;
case 1: // connection_get_rssi response
event.payload.connection <- payload[0];
event.payload.rssi <- payload[1] - 256;
event.name <- "connection_get_rssi";
break;
case 2: // connection_update response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "connection_update";
break;
case 3: // connection_version_update response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "connection_version_update";
break;
case 4: // connection_channel_map_get response
event.name <- "connection_channel_map_get";
break;
case 5: // connection_channel_map_set response
event.name <- "connection_channel_map_set";
break;
case 6: // connection_features_get response
event.name <- "connection_features_get";
break;
case 7: // connection_get_status response
event.payload.connection <- payload[0];
event.name <- "connection_get_status";
break;
case 8: // connection_raw_tx response
event.name <- "connection_raw_tx";
break;
}
break;
case BLE_CLASS_ID.ATT_CLIENT:
switch(event.cmd) {
case 0: // attclient_find_by_type_value response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_find_by_type_value";
break;
case 1: // attclient_read_by_group_type response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_read_by_group_type";
break;
case 2: // attclient_read_by_type response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_read_by_type";
break;
case 3: // attclient_find_information response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_find_information";
break;
case 4: // attclient_read_by_handle response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_read_by_handle";
break;
case 5: // attclient_attribute_write response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_attribute_write";
break;
case 6: // attclient_write_command response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "attclient_write_command";
break;
case 7: // attclient_indicate_confirm response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "attclient_indicate_confirm";
break;
case 8: // attclient_read_long response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_read_long";
break;
case 9: // attclient_prepare_write response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_prepare_write";
break;
case 10: // attclient_execute_write response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_execute_write";
break;
case 11: // attclient_read_multiple response
event.payload.connection <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "attclient_read_multiple";
break;
}
break;
/*
case BLE_CLASS_ID.SECURITY:
switch(event.cmd) {
case 0: // sm_encrypt_start response
event.payload.handle <- payload[0];
event.result <- payload[1] + (payload[2] << 8);
event.name <- "sm_encrypt_start";
break;
case 1: // sm_set_bondable_mode response
event.name <- "sm_set_bondable_mode";
break;
case 2: // sm_delete_bonding response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "sm_delete_bonding";
break;
case 3: // sm_set_parameters response
event.name <- "sm_set_parameters";
break;
case 4: // sm_passkey_entry response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "sm_passkey_entry";
break;
case 5: // sm_get_bonds response
event.payload.bonds <- payload[0];
event.name <- "sm_get_bonds";
break;
case 6: // sm_set_oob_data response
event.name <- "sm_set_oob_data";
break;
}
break;
*/
case BLE_CLASS_ID.GAP:
switch(event.cmd) {
case 0: // gap_set_privacy_flags response
event.name <- "gap_set_privacy_flags";
break;
case 1: // gap_set_mode response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_set_mode";
break;
case 2: // gap_discover response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_discover";
break;
case 3: // gap_connect_direct response
event.result <- payload[0] + (payload[1] << 8);
event.connection_handle <- payload[2];
event.name <- "gap_connect_direct";
break;
case 4: // gap_end_procedure response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_end_procedure";
break;
case 5: // gap_connect_selective response
event.result <- payload[0] + (payload[1] << 8);
event.connection_handle <- payload[2];
event.name <- "gap_connect_selective";
break;
case 6: // gap_set_filtering response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_set_filtering";
break;
case 7: // gap_set_scan_parameters response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_set_scan_parameters";
break;
case 8: // gap_set_adv_parameters response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_set_adv_parameters";
break;
case 9: // gap_set_adv_data response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_set_adv_data";
break;
case 10: // gap_set_directed_connectable_mode response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "gap_set_directed_connectable_mode";
break;
}
break;
case BLE_CLASS_ID.HARDWARE:
switch(event.cmd) {
case 0: // hardware_io_port_config_irq response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "hardware_io_port_config_irq";
break;
case 1: // hardware_set_soft_timer response
event.result <- payload[0] + (payload[1] << 8);
event.name <- "hardware_set_soft_timer";
break;