-
Notifications
You must be signed in to change notification settings - Fork 0
/
bglib.py
1380 lines (1327 loc) · 91.5 KB
/
bglib.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
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
#!/usr/bin/env python
""" Bluegiga BGAPI/BGLib implementation
Changelog:
2013-05-04 - Fixed single-item struct.unpack returns (@zwasson on Github)
2013-04-28 - Fixed numerous uint8array/bd_addr command arg errors
- Added 'debug' support
2013-04-16 - Fixed 'bglib_on_idle' to be 'on_idle'
2013-04-15 - Added wifi BGAPI support in addition to BLE BGAPI
- Fixed references to 'this' instead of 'self'
2013-04-11 - Initial release
============================================
Bluegiga BGLib Python interface library
2013-05-04 by Jeff Rowberg <[email protected]>
Updates should (hopefully) always be available at https://github.com/jrowberg/bglib
============================================
BGLib Python interface library code is placed under the MIT license
Copyright (c) 2013 Jeff Rowberg
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
"""
__author__ = "Jeff Rowberg"
__license__ = "MIT"
__version__ = "2013-05-04"
__email__ = "[email protected]"
import struct
# thanks to Masaaki Shibata for Python event handler code
# http://www.emptypage.jp/notes/pyevent.en.html
class BGAPIEvent(object):
def __init__(self, doc=None):
self.__doc__ = doc
def __get__(self, obj, objtype=None):
if obj is None:
return self
return BGAPIEventHandler(self, obj)
def __set__(self, obj, value):
pass
class BGAPIEventHandler(object):
def __init__(self, event, obj):
self.event = event
self.obj = obj
def _getfunctionlist(self):
"""(internal use) """
try:
eventhandler = self.obj.__eventhandler__
except AttributeError:
eventhandler = self.obj.__eventhandler__ = {}
return eventhandler.setdefault(self.event, [])
def add(self, func):
"""Add new event handler function.
Event handler function must be defined like func(sender, earg).
You can add handler also by using '+=' operator.
"""
self._getfunctionlist().append(func)
return self
def remove(self, func):
"""Remove existing event handler function.
You can remove handler also by using '-=' operator.
"""
self._getfunctionlist().remove(func)
return self
def fire(self, earg=None):
"""Fire event and call all handler functions
You can call EventHandler object itself like e(earg) instead of
e.fire(earg).
"""
for func in self._getfunctionlist():
func(self.obj, earg)
__iadd__ = add
__isub__ = remove
__call__ = fire
class BGLib(object):
def ble_cmd_system_reset(self, boot_in_dfu):
return struct.pack('<4BB', 0, 1, 0, 0, boot_in_dfu)
def ble_cmd_system_hello(self):
return struct.pack('<4B', 0, 0, 0, 1)
def ble_cmd_system_address_get(self):
return struct.pack('<4B', 0, 0, 0, 2)
def ble_cmd_system_reg_write(self, address, value):
return struct.pack('<4BHB', 0, 3, 0, 3, address, value)
def ble_cmd_system_reg_read(self, address):
return struct.pack('<4BH', 0, 2, 0, 4, address)
def ble_cmd_system_get_counters(self):
return struct.pack('<4B', 0, 0, 0, 5)
def ble_cmd_system_get_connections(self):
return struct.pack('<4B', 0, 0, 0, 6)
def ble_cmd_system_read_memory(self, address, length):
return struct.pack('<4BIB', 0, 5, 0, 7, address, length)
def ble_cmd_system_get_info(self):
return struct.pack('<4B', 0, 0, 0, 8)
def ble_cmd_system_endpoint_tx(self, endpoint, data):
return struct.pack('<4BBB' + str(len(data)) + 's', 0, 2 + len(data), 0, 9, endpoint, len(data), b''.join(chr(i) for i in data))
def ble_cmd_system_whitelist_append(self, address, address_type):
return struct.pack('<4B6sB', 0, 7, 0, 10, b''.join(chr(i) for i in address), address_type)
def ble_cmd_system_whitelist_remove(self, address, address_type):
return struct.pack('<4B6sB', 0, 7, 0, 11, b''.join(chr(i) for i in address), address_type)
def ble_cmd_system_whitelist_clear(self):
return struct.pack('<4B', 0, 0, 0, 12)
def ble_cmd_system_endpoint_rx(self, endpoint, size):
return struct.pack('<4BBB', 0, 2, 0, 13, endpoint, size)
def ble_cmd_system_endpoint_set_watermarks(self, endpoint, rx, tx):
return struct.pack('<4BBBB', 0, 3, 0, 14, endpoint, rx, tx)
def ble_cmd_flash_ps_defrag(self):
return struct.pack('<4B', 0, 0, 1, 0)
def ble_cmd_flash_ps_dump(self):
return struct.pack('<4B', 0, 0, 1, 1)
def ble_cmd_flash_ps_erase_all(self):
return struct.pack('<4B', 0, 0, 1, 2)
def ble_cmd_flash_ps_save(self, key, value):
return struct.pack('<4BHB' + str(len(value)) + 's', 0, 3 + len(value), 1, 3, key, len(value), b''.join(chr(i) for i in value))
def ble_cmd_flash_ps_load(self, key):
return struct.pack('<4BH', 0, 2, 1, 4, key)
def ble_cmd_flash_ps_erase(self, key):
return struct.pack('<4BH', 0, 2, 1, 5, key)
def ble_cmd_flash_erase_page(self, page):
return struct.pack('<4BB', 0, 1, 1, 6, page)
def ble_cmd_flash_write_words(self, address, words):
return struct.pack('<4BHB' + str(len(words)) + 's', 0, 3 + len(words), 1, 7, address, len(words), b''.join(chr(i) for i in words))
def ble_cmd_attributes_write(self, handle, offset, value):
return struct.pack('<4BHBB' + str(len(value)) + 's', 0, 4 + len(value), 2, 0, handle, offset, len(value), b''.join(chr(i) for i in value))
def ble_cmd_attributes_read(self, handle, offset):
return struct.pack('<4BHH', 0, 4, 2, 1, handle, offset)
def ble_cmd_attributes_read_type(self, handle):
return struct.pack('<4BH', 0, 2, 2, 2, handle)
def ble_cmd_attributes_user_read_response(self, connection, att_error, value):
return struct.pack('<4BBBB' + str(len(value)) + 's', 0, 3 + len(value), 2, 3, connection, att_error, len(value), b''.join(chr(i) for i in value))
def ble_cmd_attributes_user_write_response(self, connection, att_error):
return struct.pack('<4BBB', 0, 2, 2, 4, connection, att_error)
def ble_cmd_connection_disconnect(self, connection):
return struct.pack('<4BB', 0, 1, 3, 0, connection)
def ble_cmd_connection_get_rssi(self, connection):
return struct.pack('<4BB', 0, 1, 3, 1, connection)
def ble_cmd_connection_update(self, connection, interval_min, interval_max, latency, timeout):
return struct.pack('<4BBHHHH', 0, 9, 3, 2, connection, interval_min, interval_max, latency, timeout)
def ble_cmd_connection_version_update(self, connection):
return struct.pack('<4BB', 0, 1, 3, 3, connection)
def ble_cmd_connection_channel_map_get(self, connection):
return struct.pack('<4BB', 0, 1, 3, 4, connection)
def ble_cmd_connection_channel_map_set(self, connection, map):
return struct.pack('<4BBB' + str(len(map)) + 's', 0, 2 + len(map), 3, 5, connection, len(map), b''.join(chr(i) for i in map))
def ble_cmd_connection_features_get(self, connection):
return struct.pack('<4BB', 0, 1, 3, 6, connection)
def ble_cmd_connection_get_status(self, connection):
return struct.pack('<4BB', 0, 1, 3, 7, connection)
def ble_cmd_connection_raw_tx(self, connection, data):
return struct.pack('<4BBB' + str(len(data)) + 's', 0, 2 + len(data), 3, 8, connection, len(data), b''.join(chr(i) for i in data))
def ble_cmd_attclient_find_by_type_value(self, connection, start, end, uuid, value):
return struct.pack('<4BBHHHB' + str(len(value)) + 's', 0, 8 + len(value), 4, 0, connection, start, end, uuid, len(value), b''.join(chr(i) for i in value))
def ble_cmd_attclient_read_by_group_type(self, connection, start, end, uuid):
return struct.pack('<4BBHHB' + str(len(uuid)) + 's', 0, 6 + len(uuid), 4, 1, connection, start, end, len(uuid), b''.join(chr(i) for i in uuid))
def ble_cmd_attclient_read_by_type(self, connection, start, end, uuid):
return struct.pack('<4BBHHB' + str(len(uuid)) + 's', 0, 6 + len(uuid), 4, 2, connection, start, end, len(uuid), b''.join(chr(i) for i in uuid))
def ble_cmd_attclient_find_information(self, connection, start, end):
return struct.pack('<4BBHH', 0, 5, 4, 3, connection, start, end)
def ble_cmd_attclient_read_by_handle(self, connection, chrhandle):
return struct.pack('<4BBH', 0, 3, 4, 4, connection, chrhandle)
def ble_cmd_attclient_attribute_write(self, connection, atthandle, data):
return struct.pack('<4BBHB' + str(len(data)) + 's', 0, 4 + len(data), 4, 5, connection, atthandle, len(data), b''.join(chr(i) for i in data))
def ble_cmd_attclient_write_command(self, connection, atthandle, data):
return struct.pack('<4BBHB' + str(len(data)) + 's', 0, 4 + len(data), 4, 6, connection, atthandle, len(data), b''.join(chr(i) for i in data))
def ble_cmd_attclient_indicate_confirm(self, connection):
return struct.pack('<4BB', 0, 1, 4, 7, connection)
def ble_cmd_attclient_read_long(self, connection, chrhandle):
return struct.pack('<4BBH', 0, 3, 4, 8, connection, chrhandle)
def ble_cmd_attclient_prepare_write(self, connection, atthandle, offset, data):
return struct.pack('<4BBHHB' + str(len(data)) + 's', 0, 6 + len(data), 4, 9, connection, atthandle, offset, len(data), b''.join(chr(i) for i in data))
def ble_cmd_attclient_execute_write(self, connection, commit):
return struct.pack('<4BBB', 0, 2, 4, 10, connection, commit)
def ble_cmd_attclient_read_multiple(self, connection, handles):
return struct.pack('<4BBB' + str(len(handles)) + 's', 0, 2 + len(handles), 4, 11, connection, len(handles), b''.join(chr(i) for i in handles))
def ble_cmd_sm_encrypt_start(self, handle, bonding):
return struct.pack('<4BBB', 0, 2, 5, 0, handle, bonding)
def ble_cmd_sm_set_bondable_mode(self, bondable):
return struct.pack('<4BB', 0, 1, 5, 1, bondable)
def ble_cmd_sm_delete_bonding(self, handle):
return struct.pack('<4BB', 0, 1, 5, 2, handle)
def ble_cmd_sm_set_parameters(self, mitm, min_key_size, io_capabilities):
return struct.pack('<4BBBB', 0, 3, 5, 3, mitm, min_key_size, io_capabilities)
def ble_cmd_sm_passkey_entry(self, handle, passkey):
return struct.pack('<4BBI', 0, 5, 5, 4, handle, passkey)
def ble_cmd_sm_get_bonds(self):
return struct.pack('<4B', 0, 0, 5, 5)
def ble_cmd_sm_set_oob_data(self, oob):
return struct.pack('<4BB' + str(len(oob)) + 's', 0, 1 + len(oob), 5, 6, len(oob), b''.join(chr(i) for i in oob))
def ble_cmd_gap_set_privacy_flags(self, peripheral_privacy, central_privacy):
return struct.pack('<4BBB', 0, 2, 6, 0, peripheral_privacy, central_privacy)
def ble_cmd_gap_set_mode(self, discover, connect):
return struct.pack('<4BBB', 0, 2, 6, 1, discover, connect)
def ble_cmd_gap_discover(self, mode):
return struct.pack('<4BB', 0, 1, 6, 2, mode)
def ble_cmd_gap_connect_direct(self, address, addr_type, conn_interval_min, conn_interval_max, timeout, latency):
return struct.pack('<4B6sBHHHH', 0, 15, 6, 3, b''.join(chr(i) for i in address), addr_type, conn_interval_min, conn_interval_max, timeout, latency)
def ble_cmd_gap_end_procedure(self):
return struct.pack('<4B', 0, 0, 6, 4)
def ble_cmd_gap_connect_selective(self, conn_interval_min, conn_interval_max, timeout, latency):
return struct.pack('<4BHHHH', 0, 8, 6, 5, conn_interval_min, conn_interval_max, timeout, latency)
def ble_cmd_gap_set_filtering(self, scan_policy, adv_policy, scan_duplicate_filtering):
return struct.pack('<4BBBB', 0, 3, 6, 6, scan_policy, adv_policy, scan_duplicate_filtering)
def ble_cmd_gap_set_scan_parameters(self, scan_interval, scan_window, active):
return struct.pack('<4BHHB', 0, 5, 6, 7, scan_interval, scan_window, active)
def ble_cmd_gap_set_adv_parameters(self, adv_interval_min, adv_interval_max, adv_channels):
return struct.pack('<4BHHB', 0, 5, 6, 8, adv_interval_min, adv_interval_max, adv_channels)
def ble_cmd_gap_set_adv_data(self, set_scanrsp, adv_data):
return struct.pack('<4BBB' + str(len(adv_data)) + 's', 0, 2 + len(adv_data), 6, 9, set_scanrsp, len(adv_data), b''.join(chr(i) for i in adv_data))
def ble_cmd_gap_set_directed_connectable_mode(self, address, addr_type):
return struct.pack('<4B6sB', 0, 7, 6, 10, b''.join(chr(i) for i in address), addr_type)
def ble_cmd_hardware_io_port_config_irq(self, port, enable_bits, falling_edge):
return struct.pack('<4BBBB', 0, 3, 7, 0, port, enable_bits, falling_edge)
def ble_cmd_hardware_set_soft_timer(self, time, handle, single_shot):
return struct.pack('<4BIBB', 0, 6, 7, 1, time, handle, single_shot)
def ble_cmd_hardware_adc_read(self, input, decimation, reference_selection):
return struct.pack('<4BBBB', 0, 3, 7, 2, input, decimation, reference_selection)
def ble_cmd_hardware_io_port_config_direction(self, port, direction):
return struct.pack('<4BBB', 0, 2, 7, 3, port, direction)
def ble_cmd_hardware_io_port_config_function(self, port, function):
return struct.pack('<4BBB', 0, 2, 7, 4, port, function)
def ble_cmd_hardware_io_port_config_pull(self, port, tristate_mask, pull_up):
return struct.pack('<4BBBB', 0, 3, 7, 5, port, tristate_mask, pull_up)
def ble_cmd_hardware_io_port_write(self, port, mask, data):
return struct.pack('<4BBBB', 0, 3, 7, 6, port, mask, data)
def ble_cmd_hardware_io_port_read(self, port, mask):
return struct.pack('<4BBB', 0, 2, 7, 7, port, mask)
def ble_cmd_hardware_spi_config(self, channel, polarity, phase, bit_order, baud_e, baud_m):
return struct.pack('<4BBBBBBB', 0, 6, 7, 8, channel, polarity, phase, bit_order, baud_e, baud_m)
def ble_cmd_hardware_spi_transfer(self, channel, data):
return struct.pack('<4BBB' + str(len(data)) + 's', 0, 2 + len(data), 7, 9, channel, len(data), b''.join(chr(i) for i in data))
def ble_cmd_hardware_i2c_read(self, address, stop, length):
return struct.pack('<4BBBB', 0, 3, 7, 10, address, stop, length)
def ble_cmd_hardware_i2c_write(self, address, stop, data):
return struct.pack('<4BBBB' + str(len(data)) + 's', 0, 3 + len(data), 7, 11, address, stop, len(data), b''.join(chr(i) for i in data))
def ble_cmd_hardware_set_txpower(self, power):
return struct.pack('<4BB', 0, 1, 7, 12, power)
def ble_cmd_hardware_timer_comparator(self, timer, channel, mode, comparator_value):
return struct.pack('<4BBBBH', 0, 5, 7, 13, timer, channel, mode, comparator_value)
def ble_cmd_test_phy_tx(self, channel, length, type):
return struct.pack('<4BBBB', 0, 3, 8, 0, channel, length, type)
def ble_cmd_test_phy_rx(self, channel):
return struct.pack('<4BB', 0, 1, 8, 1, channel)
def ble_cmd_test_phy_end(self):
return struct.pack('<4B', 0, 0, 8, 2)
def ble_cmd_test_phy_reset(self):
return struct.pack('<4B', 0, 0, 8, 3)
def ble_cmd_test_get_channel_map(self):
return struct.pack('<4B', 0, 0, 8, 4)
def ble_cmd_test_debug(self, input):
return struct.pack('<4BB' + str(len(input)) + 's', 0, 1 + len(input), 8, 5, len(input), b''.join(chr(i) for i in input))
ble_rsp_system_reset = BGAPIEvent()
ble_rsp_system_hello = BGAPIEvent()
ble_rsp_system_address_get = BGAPIEvent()
ble_rsp_system_reg_write = BGAPIEvent()
ble_rsp_system_reg_read = BGAPIEvent()
ble_rsp_system_get_counters = BGAPIEvent()
ble_rsp_system_get_connections = BGAPIEvent()
ble_rsp_system_read_memory = BGAPIEvent()
ble_rsp_system_get_info = BGAPIEvent()
ble_rsp_system_endpoint_tx = BGAPIEvent()
ble_rsp_system_whitelist_append = BGAPIEvent()
ble_rsp_system_whitelist_remove = BGAPIEvent()
ble_rsp_system_whitelist_clear = BGAPIEvent()
ble_rsp_system_endpoint_rx = BGAPIEvent()
ble_rsp_system_endpoint_set_watermarks = BGAPIEvent()
ble_rsp_flash_ps_defrag = BGAPIEvent()
ble_rsp_flash_ps_dump = BGAPIEvent()
ble_rsp_flash_ps_erase_all = BGAPIEvent()
ble_rsp_flash_ps_save = BGAPIEvent()
ble_rsp_flash_ps_load = BGAPIEvent()
ble_rsp_flash_ps_erase = BGAPIEvent()
ble_rsp_flash_erase_page = BGAPIEvent()
ble_rsp_flash_write_words = BGAPIEvent()
ble_rsp_attributes_write = BGAPIEvent()
ble_rsp_attributes_read = BGAPIEvent()
ble_rsp_attributes_read_type = BGAPIEvent()
ble_rsp_attributes_user_read_response = BGAPIEvent()
ble_rsp_attributes_user_write_response = BGAPIEvent()
ble_rsp_connection_disconnect = BGAPIEvent()
ble_rsp_connection_get_rssi = BGAPIEvent()
ble_rsp_connection_update = BGAPIEvent()
ble_rsp_connection_version_update = BGAPIEvent()
ble_rsp_connection_channel_map_get = BGAPIEvent()
ble_rsp_connection_channel_map_set = BGAPIEvent()
ble_rsp_connection_features_get = BGAPIEvent()
ble_rsp_connection_get_status = BGAPIEvent()
ble_rsp_connection_raw_tx = BGAPIEvent()
ble_rsp_attclient_find_by_type_value = BGAPIEvent()
ble_rsp_attclient_read_by_group_type = BGAPIEvent()
ble_rsp_attclient_read_by_type = BGAPIEvent()
ble_rsp_attclient_find_information = BGAPIEvent()
ble_rsp_attclient_read_by_handle = BGAPIEvent()
ble_rsp_attclient_attribute_write = BGAPIEvent()
ble_rsp_attclient_write_command = BGAPIEvent()
ble_rsp_attclient_indicate_confirm = BGAPIEvent()
ble_rsp_attclient_read_long = BGAPIEvent()
ble_rsp_attclient_prepare_write = BGAPIEvent()
ble_rsp_attclient_execute_write = BGAPIEvent()
ble_rsp_attclient_read_multiple = BGAPIEvent()
ble_rsp_sm_encrypt_start = BGAPIEvent()
ble_rsp_sm_set_bondable_mode = BGAPIEvent()
ble_rsp_sm_delete_bonding = BGAPIEvent()
ble_rsp_sm_set_parameters = BGAPIEvent()
ble_rsp_sm_passkey_entry = BGAPIEvent()
ble_rsp_sm_get_bonds = BGAPIEvent()
ble_rsp_sm_set_oob_data = BGAPIEvent()
ble_rsp_gap_set_privacy_flags = BGAPIEvent()
ble_rsp_gap_set_mode = BGAPIEvent()
ble_rsp_gap_discover = BGAPIEvent()
ble_rsp_gap_connect_direct = BGAPIEvent()
ble_rsp_gap_end_procedure = BGAPIEvent()
ble_rsp_gap_connect_selective = BGAPIEvent()
ble_rsp_gap_set_filtering = BGAPIEvent()
ble_rsp_gap_set_scan_parameters = BGAPIEvent()
ble_rsp_gap_set_adv_parameters = BGAPIEvent()
ble_rsp_gap_set_adv_data = BGAPIEvent()
ble_rsp_gap_set_directed_connectable_mode = BGAPIEvent()
ble_rsp_hardware_io_port_config_irq = BGAPIEvent()
ble_rsp_hardware_set_soft_timer = BGAPIEvent()
ble_rsp_hardware_adc_read = BGAPIEvent()
ble_rsp_hardware_io_port_config_direction = BGAPIEvent()
ble_rsp_hardware_io_port_config_function = BGAPIEvent()
ble_rsp_hardware_io_port_config_pull = BGAPIEvent()
ble_rsp_hardware_io_port_write = BGAPIEvent()
ble_rsp_hardware_io_port_read = BGAPIEvent()
ble_rsp_hardware_spi_config = BGAPIEvent()
ble_rsp_hardware_spi_transfer = BGAPIEvent()
ble_rsp_hardware_i2c_read = BGAPIEvent()
ble_rsp_hardware_i2c_write = BGAPIEvent()
ble_rsp_hardware_set_txpower = BGAPIEvent()
ble_rsp_hardware_timer_comparator = BGAPIEvent()
ble_rsp_test_phy_tx = BGAPIEvent()
ble_rsp_test_phy_rx = BGAPIEvent()
ble_rsp_test_phy_end = BGAPIEvent()
ble_rsp_test_phy_reset = BGAPIEvent()
ble_rsp_test_get_channel_map = BGAPIEvent()
ble_rsp_test_debug = BGAPIEvent()
ble_evt_system_boot = BGAPIEvent()
ble_evt_system_debug = BGAPIEvent()
ble_evt_system_endpoint_watermark_rx = BGAPIEvent()
ble_evt_system_endpoint_watermark_tx = BGAPIEvent()
ble_evt_system_script_failure = BGAPIEvent()
ble_evt_system_no_license_key = BGAPIEvent()
ble_evt_flash_ps_key = BGAPIEvent()
ble_evt_attributes_value = BGAPIEvent()
ble_evt_attributes_user_read_request = BGAPIEvent()
ble_evt_attributes_status = BGAPIEvent()
ble_evt_connection_status = BGAPIEvent()
ble_evt_connection_version_ind = BGAPIEvent()
ble_evt_connection_feature_ind = BGAPIEvent()
ble_evt_connection_raw_rx = BGAPIEvent()
ble_evt_connection_disconnected = BGAPIEvent()
ble_evt_attclient_indicated = BGAPIEvent()
ble_evt_attclient_procedure_completed = BGAPIEvent()
ble_evt_attclient_group_found = BGAPIEvent()
ble_evt_attclient_attribute_found = BGAPIEvent()
ble_evt_attclient_find_information_found = BGAPIEvent()
ble_evt_attclient_attribute_value = BGAPIEvent()
ble_evt_attclient_read_multiple_response = BGAPIEvent()
ble_evt_sm_smp_data = BGAPIEvent()
ble_evt_sm_bonding_fail = BGAPIEvent()
ble_evt_sm_passkey_display = BGAPIEvent()
ble_evt_sm_passkey_request = BGAPIEvent()
ble_evt_sm_bond_status = BGAPIEvent()
ble_evt_gap_scan_response = BGAPIEvent()
ble_evt_gap_mode_changed = BGAPIEvent()
ble_evt_hardware_io_port_status = BGAPIEvent()
ble_evt_hardware_soft_timer = BGAPIEvent()
ble_evt_hardware_adc_result = BGAPIEvent()
def wifi_cmd_dfu_reset(self, dfu):
return struct.pack('<4BB', 0, 1, 0, 0, dfu)
def wifi_cmd_dfu_flash_set_address(self, address):
return struct.pack('<4BI', 0, 4, 0, 1, address)
def wifi_cmd_dfu_flash_upload(self):
return struct.pack('<4BB' + str(len(data)) + 's', 0, 1 + len(data), 0, 2, data, len(data), b''.join(chr(i) for i in data))
def wifi_cmd_dfu_flash_upload_finish(self):
return struct.pack('<4B', 0, 0, 0, 3)
def wifi_cmd_system_sync(self):
return struct.pack('<4B', 0, 0, 1, 0)
def wifi_cmd_system_reset(self, dfu):
return struct.pack('<4BB', 0, 1, 1, 1, dfu)
def wifi_cmd_system_hello(self):
return struct.pack('<4B', 0, 0, 1, 2)
def wifi_cmd_system_set_max_power_saving_state(self, state):
return struct.pack('<4BB', 0, 1, 1, 3, state)
def wifi_cmd_config_get_mac(self, hw_interface):
return struct.pack('<4BB', 0, 1, 2, 0, hw_interface)
def wifi_cmd_config_set_mac(self, hw_interface):
return struct.pack('<4BB', 0, 1, 2, 1, hw_interface, mac)
def wifi_cmd_sme_wifi_on(self):
return struct.pack('<4B', 0, 0, 3, 0)
def wifi_cmd_sme_wifi_off(self):
return struct.pack('<4B', 0, 0, 3, 1)
def wifi_cmd_sme_power_on(self, enable):
return struct.pack('<4BB', 0, 1, 3, 2, enable)
def wifi_cmd_sme_start_scan(self, hw_interface):
return struct.pack('<4BBB' + str(len(chList)) + 's', 0, 2 + len(chList), 3, 3, hw_interface, chList, len(chList), b''.join(chr(i) for i in chList))
def wifi_cmd_sme_stop_scan(self):
return struct.pack('<4B', 0, 0, 3, 4)
def wifi_cmd_sme_set_password(self):
return struct.pack('<4BB' + str(len(password)) + 's', 0, 1 + len(password), 3, 5, password, len(password), b''.join(chr(i) for i in password))
def wifi_cmd_sme_connect_bssid(self):
return struct.pack('<4B', 0, 0, 3, 6, bssid)
def wifi_cmd_sme_connect_ssid(self):
return struct.pack('<4BB' + str(len(ssid)) + 's', 0, 1 + len(ssid), 3, 7, ssid, len(ssid), b''.join(chr(i) for i in ssid))
def wifi_cmd_sme_disconnect(self):
return struct.pack('<4B', 0, 0, 3, 8)
def wifi_cmd_sme_set_scan_channels(self, hw_interface):
return struct.pack('<4BBB' + str(len(chList)) + 's', 0, 2 + len(chList), 3, 9, hw_interface, chList, len(chList), b''.join(chr(i) for i in chList))
def wifi_cmd_sme_set_operating_mode(self, mode):
return struct.pack('<4BB', 0, 1, 3, 10, mode)
def wifi_cmd_sme_start_ap_mode(self, channel, security):
return struct.pack('<4BBBB' + str(len(ssid)) + 's', 0, 3 + len(ssid), 3, 11, channel, security, ssid, len(ssid), b''.join(chr(i) for i in ssid))
def wifi_cmd_sme_stop_ap_mode(self):
return struct.pack('<4B', 0, 0, 3, 12)
def wifi_cmd_tcpip_start_tcp_server(self, port, default_destination):
return struct.pack('<4BHb', 0, 3, 4, 0, port, default_destination)
def wifi_cmd_tcpip_tcp_connect(self, port, routing):
return struct.pack('<4BHb', 0, 3, 4, 1, address, port, routing)
def wifi_cmd_tcpip_start_udp_server(self, port, default_destination):
return struct.pack('<4BHb', 0, 3, 4, 2, port, default_destination)
def wifi_cmd_tcpip_udp_connect(self, port, routing):
return struct.pack('<4BHb', 0, 3, 4, 3, address, port, routing)
def wifi_cmd_tcpip_configure(self, use_dhcp):
return struct.pack('<4BB', 0, 1, 4, 4, address, netmask, gateway, use_dhcp)
def wifi_cmd_tcpip_dns_configure(self, index):
return struct.pack('<4BB', 0, 1, 4, 5, index, address)
def wifi_cmd_tcpip_dns_gethostbyname(self):
return struct.pack('<4BB' + str(len(name)) + 's', 0, 1 + len(name), 4, 6, name, len(name), b''.join(chr(i) for i in name))
def wifi_cmd_endpoint_send(self, endpoint):
return struct.pack('<4BBB' + str(len(data)) + 's', 0, 2 + len(data), 5, 0, endpoint, data, len(data), b''.join(chr(i) for i in data))
def wifi_cmd_endpoint_set_streaming(self, endpoint, streaming):
return struct.pack('<4BBB', 0, 2, 5, 1, endpoint, streaming)
def wifi_cmd_endpoint_set_active(self, endpoint, active):
return struct.pack('<4BBB', 0, 2, 5, 2, endpoint, active)
def wifi_cmd_endpoint_set_streaming_destination(self, endpoint, streaming_destination):
return struct.pack('<4BBb', 0, 2, 5, 3, endpoint, streaming_destination)
def wifi_cmd_endpoint_close(self, endpoint):
return struct.pack('<4BB', 0, 1, 5, 4, endpoint)
def wifi_cmd_hardware_set_soft_timer(self, time, handle, single_shot):
return struct.pack('<4BIBB', 0, 6, 6, 0, time, handle, single_shot)
def wifi_cmd_hardware_external_interrupt_config(self, enable, polarity):
return struct.pack('<4BBB', 0, 2, 6, 1, enable, polarity)
def wifi_cmd_hardware_change_notification_config(self, enable):
return struct.pack('<4BI', 0, 4, 6, 2, enable)
def wifi_cmd_hardware_change_notification_pullup(self, pullup):
return struct.pack('<4BI', 0, 4, 6, 3, pullup)
def wifi_cmd_hardware_io_port_config_direction(self, port, mask, direction):
return struct.pack('<4BBHH', 0, 5, 6, 4, port, mask, direction)
def wifi_cmd_hardware_io_port_config_open_drain(self, port, mask, open_drain):
return struct.pack('<4BBHH', 0, 5, 6, 5, port, mask, open_drain)
def wifi_cmd_hardware_io_port_write(self, port, mask, data):
return struct.pack('<4BBHH', 0, 5, 6, 6, port, mask, data)
def wifi_cmd_hardware_io_port_read(self, port, mask):
return struct.pack('<4BBH', 0, 3, 6, 7, port, mask)
def wifi_cmd_hardware_output_compare(self, index, bit32, timer, mode, compare_value):
return struct.pack('<4BBBBBI', 0, 8, 6, 8, index, bit32, timer, mode, compare_value)
def wifi_cmd_hardware_adc_read(self, input):
return struct.pack('<4BB', 0, 1, 6, 9, input)
def wifi_cmd_flash_ps_defrag(self):
return struct.pack('<4B', 0, 0, 7, 0)
def wifi_cmd_flash_ps_dump(self):
return struct.pack('<4B', 0, 0, 7, 1)
def wifi_cmd_flash_ps_erase_all(self):
return struct.pack('<4B', 0, 0, 7, 2)
def wifi_cmd_flash_ps_save(self, key):
return struct.pack('<4BHB' + str(len(value)) + 's', 0, 3 + len(value), 7, 3, key, value, len(value), b''.join(chr(i) for i in value))
def wifi_cmd_flash_ps_load(self, key):
return struct.pack('<4BH', 0, 2, 7, 4, key)
def wifi_cmd_flash_ps_erase(self, key):
return struct.pack('<4BH', 0, 2, 7, 5, key)
def wifi_cmd_i2c_start_read(self, endpoint, slave_address, length):
return struct.pack('<4BBHB', 0, 4, 8, 0, endpoint, slave_address, length)
def wifi_cmd_i2c_start_write(self, endpoint, slave_address):
return struct.pack('<4BBH', 0, 3, 8, 1, endpoint, slave_address)
def wifi_cmd_i2c_stop(self, endpoint):
return struct.pack('<4BB', 0, 1, 8, 2, endpoint)
def wifi_cmd_https_enable(self, https, dhcps, dnss):
return struct.pack('<4BBBB', 0, 3, 9, 0, https, dhcps, dnss)
wifi_rsp_dfu_reset = BGAPIEvent()
wifi_rsp_dfu_flash_set_address = BGAPIEvent()
wifi_rsp_dfu_flash_upload = BGAPIEvent()
wifi_rsp_dfu_flash_upload_finish = BGAPIEvent()
wifi_rsp_system_sync = BGAPIEvent()
wifi_rsp_system_reset = BGAPIEvent()
wifi_rsp_system_hello = BGAPIEvent()
wifi_rsp_system_set_max_power_saving_state = BGAPIEvent()
wifi_rsp_config_get_mac = BGAPIEvent()
wifi_rsp_config_set_mac = BGAPIEvent()
wifi_rsp_sme_wifi_on = BGAPIEvent()
wifi_rsp_sme_wifi_off = BGAPIEvent()
wifi_rsp_sme_power_on = BGAPIEvent()
wifi_rsp_sme_start_scan = BGAPIEvent()
wifi_rsp_sme_stop_scan = BGAPIEvent()
wifi_rsp_sme_set_password = BGAPIEvent()
wifi_rsp_sme_connect_bssid = BGAPIEvent()
wifi_rsp_sme_connect_ssid = BGAPIEvent()
wifi_rsp_sme_disconnect = BGAPIEvent()
wifi_rsp_sme_set_scan_channels = BGAPIEvent()
wifi_rsp_sme_set_operating_mode = BGAPIEvent()
wifi_rsp_sme_start_ap_mode = BGAPIEvent()
wifi_rsp_sme_stop_ap_mode = BGAPIEvent()
wifi_rsp_tcpip_start_tcp_server = BGAPIEvent()
wifi_rsp_tcpip_tcp_connect = BGAPIEvent()
wifi_rsp_tcpip_start_udp_server = BGAPIEvent()
wifi_rsp_tcpip_udp_connect = BGAPIEvent()
wifi_rsp_tcpip_configure = BGAPIEvent()
wifi_rsp_tcpip_dns_configure = BGAPIEvent()
wifi_rsp_tcpip_dns_gethostbyname = BGAPIEvent()
wifi_rsp_endpoint_send = BGAPIEvent()
wifi_rsp_endpoint_set_streaming = BGAPIEvent()
wifi_rsp_endpoint_set_active = BGAPIEvent()
wifi_rsp_endpoint_set_streaming_destination = BGAPIEvent()
wifi_rsp_endpoint_close = BGAPIEvent()
wifi_rsp_hardware_set_soft_timer = BGAPIEvent()
wifi_rsp_hardware_external_interrupt_config = BGAPIEvent()
wifi_rsp_hardware_change_notification_config = BGAPIEvent()
wifi_rsp_hardware_change_notification_pullup = BGAPIEvent()
wifi_rsp_hardware_io_port_config_direction = BGAPIEvent()
wifi_rsp_hardware_io_port_config_open_drain = BGAPIEvent()
wifi_rsp_hardware_io_port_write = BGAPIEvent()
wifi_rsp_hardware_io_port_read = BGAPIEvent()
wifi_rsp_hardware_output_compare = BGAPIEvent()
wifi_rsp_hardware_adc_read = BGAPIEvent()
wifi_rsp_flash_ps_defrag = BGAPIEvent()
wifi_rsp_flash_ps_dump = BGAPIEvent()
wifi_rsp_flash_ps_erase_all = BGAPIEvent()
wifi_rsp_flash_ps_save = BGAPIEvent()
wifi_rsp_flash_ps_load = BGAPIEvent()
wifi_rsp_flash_ps_erase = BGAPIEvent()
wifi_rsp_i2c_start_read = BGAPIEvent()
wifi_rsp_i2c_start_write = BGAPIEvent()
wifi_rsp_i2c_stop = BGAPIEvent()
wifi_rsp_https_enable = BGAPIEvent()
wifi_evt_dfu_boot = BGAPIEvent()
wifi_evt_system_boot = BGAPIEvent()
wifi_evt_system_state = BGAPIEvent()
wifi_evt_system_sw_exception = BGAPIEvent()
wifi_evt_system_power_saving_state = BGAPIEvent()
wifi_evt_config_mac_address = BGAPIEvent()
wifi_evt_sme_wifi_is_on = BGAPIEvent()
wifi_evt_sme_wifi_is_off = BGAPIEvent()
wifi_evt_sme_scan_result = BGAPIEvent()
wifi_evt_sme_scan_result_drop = BGAPIEvent()
wifi_evt_sme_scanned = BGAPIEvent()
wifi_evt_sme_connected = BGAPIEvent()
wifi_evt_sme_disconnected = BGAPIEvent()
wifi_evt_sme_interface_status = BGAPIEvent()
wifi_evt_sme_connect_failed = BGAPIEvent()
wifi_evt_sme_connect_retry = BGAPIEvent()
wifi_evt_sme_ap_mode_started = BGAPIEvent()
wifi_evt_sme_ap_mode_stopped = BGAPIEvent()
wifi_evt_sme_ap_mode_failed = BGAPIEvent()
wifi_evt_sme_ap_client_joined = BGAPIEvent()
wifi_evt_sme_ap_client_left = BGAPIEvent()
wifi_evt_tcpip_configuration = BGAPIEvent()
wifi_evt_tcpip_dns_configuration = BGAPIEvent()
wifi_evt_tcpip_endpoint_status = BGAPIEvent()
wifi_evt_tcpip_dns_gethostbyname_result = BGAPIEvent()
wifi_evt_endpoint_syntax_error = BGAPIEvent()
wifi_evt_endpoint_data = BGAPIEvent()
wifi_evt_endpoint_status = BGAPIEvent()
wifi_evt_endpoint_closing = BGAPIEvent()
wifi_evt_hardware_soft_timer = BGAPIEvent()
wifi_evt_hardware_change_notification = BGAPIEvent()
wifi_evt_hardware_external_interrupt = BGAPIEvent()
wifi_evt_flash_ps_key = BGAPIEvent()
wifi_evt_https_on_req = BGAPIEvent()
wifi_evt_https_application_data_changed = BGAPIEvent()
on_busy = BGAPIEvent()
on_idle = BGAPIEvent()
on_timeout = BGAPIEvent()
on_before_tx_command = BGAPIEvent()
on_tx_command_complete = BGAPIEvent()
bgapi_rx_buffer = []
bgapi_rx_expected_length = 0
busy = False
packet_mode = False
debug = False
def send_command(self, ser, packet):
if self.packet_mode: packet = chr(len(packet) & 0xFF) + packet
if self.debug: print '=>[ ' + ' '.join(['%02X' % ord(b) for b in packet ]) + ' ]'
self.on_before_tx_command()
self.busy = True
self.on_busy()
ser.write(packet)
self.on_tx_command_complete()
def check_activity(self, ser, timeout=0):
if timeout > 0:
ser.timeout = timeout
while 1:
x = ser.read()
if len(x) > 0:
self.parse(ord(x))
else: # timeout
self.busy = False
self.on_idle()
self.on_timeout()
if not self.busy: # finished
break
else:
while ser.inWaiting(): self.parse(ord(ser.read()))
return self.busy
def parse(self, b):
if len(self.bgapi_rx_buffer) == 0 and (b == 0x00 or b == 0x80 or b == 0x08 or b == 0x88):
self.bgapi_rx_buffer.append(b)
elif len(self.bgapi_rx_buffer) == 1:
self.bgapi_rx_buffer.append(b)
self.bgapi_rx_expected_length = 4 + (self.bgapi_rx_buffer[0] & 0x07) + self.bgapi_rx_buffer[1]
elif len(self.bgapi_rx_buffer) > 1:
self.bgapi_rx_buffer.append(b)
"""
BGAPI packet structure (as of 2012-11-07):
Byte 0:
[7] - 1 bit, Message Type (MT) 0 = Command/Response, 1 = Event
[6:3] - 4 bits, Technology Type (TT) 0000 = Bluetooth 4.0 single mode, 0001 = Wi-Fi
[2:0] - 3 bits, Length High (LH) Payload length (high bits)
Byte 1: 8 bits, Length Low (LL) Payload length (low bits)
Byte 2: 8 bits, Class ID (CID) Command class ID
Byte 3: 8 bits, Command ID (CMD) Command ID
Bytes 4-n: 0 - 2048 Bytes, Payload (PL) Up to 2048 bytes of payload
"""
#print '%02X: %d, %d' % (b, len(self.bgapi_rx_buffer), self.bgapi_rx_expected_length)
if self.bgapi_rx_expected_length > 0 and len(self.bgapi_rx_buffer) == self.bgapi_rx_expected_length:
if self.debug: print '<=[ ' + ' '.join(['%02X' % b for b in self.bgapi_rx_buffer ]) + ' ]'
packet_type, payload_length, packet_class, packet_command = self.bgapi_rx_buffer[:4]
self.bgapi_rx_payload = b''.join(chr(i) for i in self.bgapi_rx_buffer[4:])
self.bgapi_rx_buffer = []
if packet_type & 0x88 == 0x00:
# 0x00 = BLE response packet
if packet_class == 0:
if packet_command == 0: # ble_rsp_system_reset
self.ble_rsp_system_reset({ })
self.busy = False
self.on_idle()
elif packet_command == 1: # ble_rsp_system_hello
self.ble_rsp_system_hello({ })
elif packet_command == 2: # ble_rsp_system_address_get
address = struct.unpack('<6s', self.bgapi_rx_payload[:6])[0]
address = [ord(b) for b in address]
self.ble_rsp_system_address_get({ 'address': address })
elif packet_command == 3: # ble_rsp_system_reg_write
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_system_reg_write({ 'result': result })
elif packet_command == 4: # ble_rsp_system_reg_read
address, value = struct.unpack('<HB', self.bgapi_rx_payload[:3])
self.ble_rsp_system_reg_read({ 'address': address, 'value': value })
elif packet_command == 5: # ble_rsp_system_get_counters
txok, txretry, rxok, rxfail, mbuf = struct.unpack('<BBBBB', self.bgapi_rx_payload[:5])
self.ble_rsp_system_get_counters({ 'txok': txok, 'txretry': txretry, 'rxok': rxok, 'rxfail': rxfail, 'mbuf': mbuf })
elif packet_command == 6: # ble_rsp_system_get_connections
maxconn = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
self.ble_rsp_system_get_connections({ 'maxconn': maxconn })
elif packet_command == 7: # ble_rsp_system_read_memory
address, data_len = struct.unpack('<IB', self.bgapi_rx_payload[:5])
data_data = [ord(b) for b in self.bgapi_rx_payload[5:]]
self.ble_rsp_system_read_memory({ 'address': address, 'data': data_data })
elif packet_command == 8: # ble_rsp_system_get_info
major, minor, patch, build, ll_version, protocol_version, hw = struct.unpack('<HHHHHBB', self.bgapi_rx_payload[:12])
self.ble_rsp_system_get_info({ 'major': major, 'minor': minor, 'patch': patch, 'build': build, 'll_version': ll_version, 'protocol_version': protocol_version, 'hw': hw })
elif packet_command == 9: # ble_rsp_system_endpoint_tx
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_system_endpoint_tx({ 'result': result })
elif packet_command == 10: # ble_rsp_system_whitelist_append
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_system_whitelist_append({ 'result': result })
elif packet_command == 11: # ble_rsp_system_whitelist_remove
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_system_whitelist_remove({ 'result': result })
elif packet_command == 12: # ble_rsp_system_whitelist_clear
self.ble_rsp_system_whitelist_clear({ })
elif packet_command == 13: # ble_rsp_system_endpoint_rx
result, data_len = struct.unpack('<HB', self.bgapi_rx_payload[:3])
data_data = [ord(b) for b in self.bgapi_rx_payload[3:]]
self.ble_rsp_system_endpoint_rx({ 'result': result, 'data': data_data })
elif packet_command == 14: # ble_rsp_system_endpoint_set_watermarks
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_system_endpoint_set_watermarks({ 'result': result })
elif packet_class == 1:
if packet_command == 0: # ble_rsp_flash_ps_defrag
self.ble_rsp_flash_ps_defrag({ })
elif packet_command == 1: # ble_rsp_flash_ps_dump
self.ble_rsp_flash_ps_dump({ })
elif packet_command == 2: # ble_rsp_flash_ps_erase_all
self.ble_rsp_flash_ps_erase_all({ })
elif packet_command == 3: # ble_rsp_flash_ps_save
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_flash_ps_save({ 'result': result })
elif packet_command == 4: # ble_rsp_flash_ps_load
result, value_len = struct.unpack('<HB', self.bgapi_rx_payload[:3])
value_data = [ord(b) for b in self.bgapi_rx_payload[3:]]
self.ble_rsp_flash_ps_load({ 'result': result, 'value': value_data })
elif packet_command == 5: # ble_rsp_flash_ps_erase
self.ble_rsp_flash_ps_erase({ })
elif packet_command == 6: # ble_rsp_flash_erase_page
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_flash_erase_page({ 'result': result })
elif packet_command == 7: # ble_rsp_flash_write_words
self.ble_rsp_flash_write_words({ })
elif packet_class == 2:
if packet_command == 0: # ble_rsp_attributes_write
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_attributes_write({ 'result': result })
elif packet_command == 1: # ble_rsp_attributes_read
handle, offset, result, value_len = struct.unpack('<HHHB', self.bgapi_rx_payload[:7])
value_data = [ord(b) for b in self.bgapi_rx_payload[7:]]
self.ble_rsp_attributes_read({ 'handle': handle, 'offset': offset, 'result': result, 'value': value_data })
elif packet_command == 2: # ble_rsp_attributes_read_type
handle, result, value_len = struct.unpack('<HHB', self.bgapi_rx_payload[:5])
value_data = [ord(b) for b in self.bgapi_rx_payload[5:]]
self.ble_rsp_attributes_read_type({ 'handle': handle, 'result': result, 'value': value_data })
elif packet_command == 3: # ble_rsp_attributes_user_read_response
self.ble_rsp_attributes_user_read_response({ })
elif packet_command == 4: # ble_rsp_attributes_user_write_response
self.ble_rsp_attributes_user_write_response({ })
elif packet_class == 3:
if packet_command == 0: # ble_rsp_connection_disconnect
#print ''.join(["%02X" % ord(x) for x in self.bgapi_rx_payload])
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_connection_disconnect({ 'connection': connection, 'result': result })
elif packet_command == 1: # ble_rsp_connection_get_rssi
connection, rssi = struct.unpack('<Bb', self.bgapi_rx_payload[:2])
self.ble_rsp_connection_get_rssi({ 'connection': connection, 'rssi': rssi })
elif packet_command == 2: # ble_rsp_connection_update
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_connection_update({ 'connection': connection, 'result': result })
elif packet_command == 3: # ble_rsp_connection_version_update
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_connection_version_update({ 'connection': connection, 'result': result })
elif packet_command == 4: # ble_rsp_connection_channel_map_get
connection, map_len = struct.unpack('<BB', self.bgapi_rx_payload[:2])
map_data = [ord(b) for b in self.bgapi_rx_payload[2:]]
self.ble_rsp_connection_channel_map_get({ 'connection': connection, 'map': map_data })
elif packet_command == 5: # ble_rsp_connection_channel_map_set
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_connection_channel_map_set({ 'connection': connection, 'result': result })
elif packet_command == 6: # ble_rsp_connection_features_get
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_connection_features_get({ 'connection': connection, 'result': result })
elif packet_command == 7: # ble_rsp_connection_get_status
connection = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
self.ble_rsp_connection_get_status({ 'connection': connection })
elif packet_command == 8: # ble_rsp_connection_raw_tx
connection = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
self.ble_rsp_connection_raw_tx({ 'connection': connection })
elif packet_class == 4:
if packet_command == 0: # ble_rsp_attclient_find_by_type_value
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_find_by_type_value({ 'connection': connection, 'result': result })
elif packet_command == 1: # ble_rsp_attclient_read_by_group_type
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_read_by_group_type({ 'connection': connection, 'result': result })
elif packet_command == 2: # ble_rsp_attclient_read_by_type
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_read_by_type({ 'connection': connection, 'result': result })
elif packet_command == 3: # ble_rsp_attclient_find_information
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_find_information({ 'connection': connection, 'result': result })
elif packet_command == 4: # ble_rsp_attclient_read_by_handle
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_read_by_handle({ 'connection': connection, 'result': result })
elif packet_command == 5: # ble_rsp_attclient_attribute_write
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_attribute_write({ 'connection': connection, 'result': result })
elif packet_command == 6: # ble_rsp_attclient_write_command
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_write_command({ 'connection': connection, 'result': result })
elif packet_command == 7: # ble_rsp_attclient_indicate_confirm
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_attclient_indicate_confirm({ 'result': result })
elif packet_command == 8: # ble_rsp_attclient_read_long
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_read_long({ 'connection': connection, 'result': result })
elif packet_command == 9: # ble_rsp_attclient_prepare_write
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_prepare_write({ 'connection': connection, 'result': result })
elif packet_command == 10: # ble_rsp_attclient_execute_write
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_execute_write({ 'connection': connection, 'result': result })
elif packet_command == 11: # ble_rsp_attclient_read_multiple
connection, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_attclient_read_multiple({ 'connection': connection, 'result': result })
elif packet_class == 5:
if packet_command == 0: # ble_rsp_sm_encrypt_start
handle, result = struct.unpack('<BH', self.bgapi_rx_payload[:3])
self.ble_rsp_sm_encrypt_start({ 'handle': handle, 'result': result })
elif packet_command == 1: # ble_rsp_sm_set_bondable_mode
self.ble_rsp_sm_set_bondable_mode({ })
elif packet_command == 2: # ble_rsp_sm_delete_bonding
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_sm_delete_bonding({ 'result': result })
elif packet_command == 3: # ble_rsp_sm_set_parameters
self.ble_rsp_sm_set_parameters({ })
elif packet_command == 4: # ble_rsp_sm_passkey_entry
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_sm_passkey_entry({ 'result': result })
elif packet_command == 5: # ble_rsp_sm_get_bonds
bonds = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
self.ble_rsp_sm_get_bonds({ 'bonds': bonds })
elif packet_command == 6: # ble_rsp_sm_set_oob_data
self.ble_rsp_sm_set_oob_data({ })
elif packet_class == 6:
if packet_command == 0: # ble_rsp_gap_set_privacy_flags
self.ble_rsp_gap_set_privacy_flags({ })
elif packet_command == 1: # ble_rsp_gap_set_mode
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_set_mode({ 'result': result })
elif packet_command == 2: # ble_rsp_gap_discover
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_discover({ 'result': result })
elif packet_command == 3: # ble_rsp_gap_connect_direct
result, connection_handle = struct.unpack('<HB', self.bgapi_rx_payload[:3])
self.ble_rsp_gap_connect_direct({ 'result': result, 'connection_handle': connection_handle })
elif packet_command == 4: # ble_rsp_gap_end_procedure
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_end_procedure({ 'result': result })
elif packet_command == 5: # ble_rsp_gap_connect_selective
result, connection_handle = struct.unpack('<HB', self.bgapi_rx_payload[:3])
self.ble_rsp_gap_connect_selective({ 'result': result, 'connection_handle': connection_handle })
elif packet_command == 6: # ble_rsp_gap_set_filtering
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_set_filtering({ 'result': result })
elif packet_command == 7: # ble_rsp_gap_set_scan_parameters
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_set_scan_parameters({ 'result': result })
elif packet_command == 8: # ble_rsp_gap_set_adv_parameters
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_set_adv_parameters({ 'result': result })
elif packet_command == 9: # ble_rsp_gap_set_adv_data
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_set_adv_data({ 'result': result })
elif packet_command == 10: # ble_rsp_gap_set_directed_connectable_mode
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_gap_set_directed_connectable_mode({ 'result': result })
elif packet_class == 7:
if packet_command == 0: # ble_rsp_hardware_io_port_config_irq
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_io_port_config_irq({ 'result': result })
elif packet_command == 1: # ble_rsp_hardware_set_soft_timer
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_set_soft_timer({ 'result': result })
elif packet_command == 2: # ble_rsp_hardware_adc_read
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_adc_read({ 'result': result })
elif packet_command == 3: # ble_rsp_hardware_io_port_config_direction
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_io_port_config_direction({ 'result': result })
elif packet_command == 4: # ble_rsp_hardware_io_port_config_function
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_io_port_config_function({ 'result': result })
elif packet_command == 5: # ble_rsp_hardware_io_port_config_pull
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_io_port_config_pull({ 'result': result })
elif packet_command == 6: # ble_rsp_hardware_io_port_write
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_io_port_write({ 'result': result })
elif packet_command == 7: # ble_rsp_hardware_io_port_read
result, port, data = struct.unpack('<HBB', self.bgapi_rx_payload[:4])
self.ble_rsp_hardware_io_port_read({ 'result': result, 'port': port, 'data': data })
elif packet_command == 8: # ble_rsp_hardware_spi_config
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_spi_config({ 'result': result })
elif packet_command == 9: # ble_rsp_hardware_spi_transfer
result, channel, data_len = struct.unpack('<HBB', self.bgapi_rx_payload[:4])
data_data = [ord(b) for b in self.bgapi_rx_payload[4:]]
self.ble_rsp_hardware_spi_transfer({ 'result': result, 'channel': channel, 'data': data_data })
elif packet_command == 10: # ble_rsp_hardware_i2c_read
result, data_len = struct.unpack('<HB', self.bgapi_rx_payload[:3])
data_data = [ord(b) for b in self.bgapi_rx_payload[3:]]
self.ble_rsp_hardware_i2c_read({ 'result': result, 'data': data_data })
elif packet_command == 11: # ble_rsp_hardware_i2c_write
written = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
self.ble_rsp_hardware_i2c_write({ 'written': written })
elif packet_command == 12: # ble_rsp_hardware_set_txpower
self.ble_rsp_hardware_set_txpower({ })
elif packet_command == 13: # ble_rsp_hardware_timer_comparator
result = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_hardware_timer_comparator({ 'result': result })
elif packet_class == 8:
if packet_command == 0: # ble_rsp_test_phy_tx
self.ble_rsp_test_phy_tx({ })
elif packet_command == 1: # ble_rsp_test_phy_rx
self.ble_rsp_test_phy_rx({ })
elif packet_command == 2: # ble_rsp_test_phy_end
counter = struct.unpack('<H', self.bgapi_rx_payload[:2])[0]
self.ble_rsp_test_phy_end({ 'counter': counter })
elif packet_command == 3: # ble_rsp_test_phy_reset
self.ble_rsp_test_phy_reset({ })
elif packet_command == 4: # ble_rsp_test_get_channel_map
channel_map_len = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
channel_map_data = [ord(b) for b in self.bgapi_rx_payload[1:]]
self.ble_rsp_test_get_channel_map({ 'channel_map': channel_map_data })
elif packet_command == 5: # ble_rsp_test_debug
output_len = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
output_data = [ord(b) for b in self.bgapi_rx_payload[1:]]
self.ble_rsp_test_debug({ 'output': output_data })
self.busy = False
self.on_idle()
elif packet_type & 0x88 == 0x80:
# 0x80 = BLE event packet
if packet_class == 0:
if packet_command == 0: # ble_evt_system_boot
major, minor, patch, build, ll_version, protocol_version, hw = struct.unpack('<HHHHHBB', self.bgapi_rx_payload[:12])
self.ble_evt_system_boot({ 'major': major, 'minor': minor, 'patch': patch, 'build': build, 'll_version': ll_version, 'protocol_version': protocol_version, 'hw': hw })
self.busy = False
self.on_idle()
elif packet_command == 1: # ble_evt_system_debug
data_len = struct.unpack('<B', self.bgapi_rx_payload[:1])[0]
data_data = [ord(b) for b in self.bgapi_rx_payload[1:]]
self.ble_evt_system_debug({ 'data': data_data })
elif packet_command == 2: # ble_evt_system_endpoint_watermark_rx
endpoint, data = struct.unpack('<BB', self.bgapi_rx_payload[:2])
self.ble_evt_system_endpoint_watermark_rx({ 'endpoint': endpoint, 'data': data })
elif packet_command == 3: # ble_evt_system_endpoint_watermark_tx
endpoint, data = struct.unpack('<BB', self.bgapi_rx_payload[:2])
self.ble_evt_system_endpoint_watermark_tx({ 'endpoint': endpoint, 'data': data })
elif packet_command == 4: # ble_evt_system_script_failure
address, reason = struct.unpack('<HH', self.bgapi_rx_payload[:4])
self.ble_evt_system_script_failure({ 'address': address, 'reason': reason })
elif packet_command == 5: # ble_evt_system_no_license_key
self.ble_evt_system_no_license_key({ })
elif packet_class == 1:
if packet_command == 0: # ble_evt_flash_ps_key
key, value_len = struct.unpack('<HB', self.bgapi_rx_payload[:3])
value_data = [ord(b) for b in self.bgapi_rx_payload[3:]]
self.ble_evt_flash_ps_key({ 'key': key, 'value': value_data })
elif packet_class == 2:
if packet_command == 0: # ble_evt_attributes_value
connection, reason, handle, offset, value_len = struct.unpack('<BBHHB', self.bgapi_rx_payload[:7])
value_data = [ord(b) for b in self.bgapi_rx_payload[7:]]
self.ble_evt_attributes_value({ 'connection': connection, 'reason': reason, 'handle': handle, 'offset': offset, 'value': value_data })
elif packet_command == 1: # ble_evt_attributes_user_read_request
connection, handle, offset, maxsize = struct.unpack('<BHHB', self.bgapi_rx_payload[:6])
self.ble_evt_attributes_user_read_request({ 'connection': connection, 'handle': handle, 'offset': offset, 'maxsize': maxsize })
elif packet_command == 2: # ble_evt_attributes_status
handle, flags = struct.unpack('<HB', self.bgapi_rx_payload[:3])
self.ble_evt_attributes_status({ 'handle': handle, 'flags': flags })
elif packet_class == 3:
if packet_command == 0: # ble_evt_connection_status
connection, flags, address, address_type, conn_interval, timeout, latency, bonding = struct.unpack('<BB6sBHHHB', self.bgapi_rx_payload[:16])