forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 87
/
modbluetooth.c
1772 lines (1537 loc) · 75.7 KB
/
modbluetooth.c
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
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ayke van Laethem
* Copyright (c) 2019-2020 Jim Mussared
*
* 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.
*/
#include "py/binary.h"
#include "py/gc.h"
#include "py/misc.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/obj.h"
#include "py/objarray.h"
#include "py/qstr.h"
#include "py/runtime.h"
#include "extmod/modbluetooth.h"
#include <string.h>
#if MICROPY_PY_BLUETOOTH
#if !MICROPY_ENABLE_SCHEDULER
#error modbluetooth requires MICROPY_ENABLE_SCHEDULER
#endif
#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS && !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
#error l2cap channels require synchronous modbluetooth events
#endif
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING && !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
#error pairing and bonding require synchronous modbluetooth events
#endif
// NimBLE can have fragmented data for GATTC events, so requires reassembly.
#define MICROPY_PY_BLUETOOTH_USE_GATTC_EVENT_DATA_REASSEMBLY MICROPY_BLUETOOTH_NIMBLE
#define MP_BLUETOOTH_CONNECT_DEFAULT_SCAN_DURATION_MS 2000
#define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN 5
#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
// This formula is intended to allow queuing the data of a large characteristic
// while still leaving room for a couple of normal (small, fixed size) events.
#define MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(ringbuf_size) (MAX((int)((ringbuf_size) / 2), (int)(ringbuf_size) - 64))
#endif
// bluetooth.BLE type. This is currently a singleton, however in the future
// this could allow having multiple BLE interfaces on different UARTs.
typedef struct {
mp_obj_base_t base;
mp_obj_t irq_handler;
#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
bool irq_scheduled;
mp_obj_t irq_data_tuple;
uint8_t irq_data_addr_bytes[6];
uint16_t irq_data_data_alloc;
mp_obj_array_t irq_data_addr;
mp_obj_array_t irq_data_data;
mp_obj_bluetooth_uuid_t irq_data_uuid;
ringbuf_t ringbuf;
#endif
} mp_obj_bluetooth_ble_t;
static const mp_obj_type_t mp_type_bluetooth_ble;
// TODO: this seems like it could be generic?
static mp_obj_t bluetooth_handle_errno(int err) {
if (err != 0) {
mp_raise_OSError(err);
}
return mp_const_none;
}
// ----------------------------------------------------------------------------
// UUID object
// ----------------------------------------------------------------------------
static mp_obj_t bluetooth_uuid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
(void)type;
mp_arg_check_num(n_args, n_kw, 1, 1, false);
mp_obj_bluetooth_uuid_t *self = mp_obj_malloc(mp_obj_bluetooth_uuid_t, &mp_type_bluetooth_uuid);
if (mp_obj_is_int(all_args[0])) {
self->type = MP_BLUETOOTH_UUID_TYPE_16;
mp_int_t value = mp_obj_get_int(all_args[0]);
if (value > 65535) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid UUID"));
}
self->data[0] = value & 0xff;
self->data[1] = (value >> 8) & 0xff;
} else {
mp_buffer_info_t uuid_bufinfo = {0};
mp_get_buffer_raise(all_args[0], &uuid_bufinfo, MP_BUFFER_READ);
if (uuid_bufinfo.len == 2 || uuid_bufinfo.len == 4 || uuid_bufinfo.len == 16) {
// Bytes data -- infer UUID type from length and copy data.
self->type = uuid_bufinfo.len;
memcpy(self->data, uuid_bufinfo.buf, self->type);
} else {
// Assume UUID string (e.g. '6E400001-B5A3-F393-E0A9-E50E24DCCA9E')
self->type = MP_BLUETOOTH_UUID_TYPE_128;
int uuid_i = 32;
for (size_t i = 0; i < uuid_bufinfo.len; i++) {
char c = ((char *)uuid_bufinfo.buf)[i];
if (c == '-') {
continue;
}
if (!unichar_isxdigit(c)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid char in UUID"));
}
c = unichar_xdigit_value(c);
uuid_i--;
if (uuid_i < 0) {
mp_raise_ValueError(MP_ERROR_TEXT("UUID too long"));
}
if (uuid_i % 2 == 0) {
// lower nibble
self->data[uuid_i / 2] |= c;
} else {
// upper nibble
self->data[uuid_i / 2] = c << 4;
}
}
if (uuid_i > 0) {
mp_raise_ValueError(MP_ERROR_TEXT("UUID too short"));
}
}
}
return MP_OBJ_FROM_PTR(self);
}
static mp_obj_t bluetooth_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
mp_obj_bluetooth_uuid_t *self = MP_OBJ_TO_PTR(self_in);
switch (op) {
case MP_UNARY_OP_HASH: {
// Use the QSTR hash function.
return MP_OBJ_NEW_SMALL_INT(qstr_compute_hash(self->data, self->type));
}
default:
return MP_OBJ_NULL; // op not supported
}
}
static mp_obj_t bluetooth_uuid_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
if (!mp_obj_is_type(rhs_in, &mp_type_bluetooth_uuid)) {
return MP_OBJ_NULL;
}
mp_obj_bluetooth_uuid_t *lhs = MP_OBJ_TO_PTR(lhs_in);
mp_obj_bluetooth_uuid_t *rhs = MP_OBJ_TO_PTR(rhs_in);
switch (op) {
case MP_BINARY_OP_EQUAL:
case MP_BINARY_OP_LESS:
case MP_BINARY_OP_LESS_EQUAL:
case MP_BINARY_OP_MORE:
case MP_BINARY_OP_MORE_EQUAL:
if (lhs->type == rhs->type) {
return mp_obj_new_bool(mp_seq_cmp_bytes(op, lhs->data, lhs->type, rhs->data, rhs->type));
} else {
return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT(lhs->type), MP_OBJ_NEW_SMALL_INT(rhs->type));
}
default:
return MP_OBJ_NULL; // op not supported
}
}
static void bluetooth_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_bluetooth_uuid_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "UUID(%s", self->type <= 4 ? "0x" : "'");
for (int i = 0; i < self->type; ++i) {
if (i == 4 || i == 6 || i == 8 || i == 10) {
mp_printf(print, "-");
}
mp_printf(print, "%02x", self->data[self->type - 1 - i]);
}
if (self->type == MP_BLUETOOTH_UUID_TYPE_128) {
mp_printf(print, "'");
}
mp_printf(print, ")");
}
static mp_int_t bluetooth_uuid_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
mp_obj_bluetooth_uuid_t *self = MP_OBJ_TO_PTR(self_in);
if (flags != MP_BUFFER_READ) {
return 1;
}
bufinfo->buf = self->data;
bufinfo->len = self->type;
bufinfo->typecode = 'B';
return 0;
}
#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
static void ringbuf_put_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) {
assert(ringbuf_free(ringbuf) >= (size_t)uuid->type + 1);
ringbuf_put(ringbuf, uuid->type);
for (int i = 0; i < uuid->type; ++i) {
ringbuf_put(ringbuf, uuid->data[i]);
}
}
#endif
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
static void ringbuf_get_uuid(ringbuf_t *ringbuf, mp_obj_bluetooth_uuid_t *uuid) {
assert(ringbuf_avail(ringbuf) >= 1);
uuid->type = ringbuf_get(ringbuf);
assert(ringbuf_avail(ringbuf) >= uuid->type);
for (int i = 0; i < uuid->type; ++i) {
uuid->data[i] = ringbuf_get(ringbuf);
}
}
#endif
#endif // !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
MP_DEFINE_CONST_OBJ_TYPE(
mp_type_bluetooth_uuid,
MP_QSTR_UUID,
MP_TYPE_FLAG_NONE,
make_new, bluetooth_uuid_make_new,
unary_op, bluetooth_uuid_unary_op,
binary_op, bluetooth_uuid_binary_op,
print, bluetooth_uuid_print,
buffer, bluetooth_uuid_get_buffer
);
// ----------------------------------------------------------------------------
// Bluetooth object: General
// ----------------------------------------------------------------------------
static mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
(void)type;
(void)n_args;
(void)n_kw;
(void)all_args;
if (MP_STATE_VM(bluetooth) == MP_OBJ_NULL) {
mp_obj_bluetooth_ble_t *o = m_new0(mp_obj_bluetooth_ble_t, 1);
o->base.type = &mp_type_bluetooth_ble;
o->irq_handler = mp_const_none;
#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
// Pre-allocate the event data tuple to prevent needing to allocate in the IRQ handler.
o->irq_data_tuple = mp_obj_new_tuple(MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN, NULL);
// Pre-allocated buffers for address, payload and uuid.
mp_obj_memoryview_init(&o->irq_data_addr, 'B', 0, 0, o->irq_data_addr_bytes);
o->irq_data_data_alloc = MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(MICROPY_PY_BLUETOOTH_RINGBUF_SIZE);
mp_obj_memoryview_init(&o->irq_data_data, 'B', 0, 0, m_new(uint8_t, o->irq_data_data_alloc));
o->irq_data_uuid.base.type = &mp_type_bluetooth_uuid;
// Allocate the default ringbuf.
ringbuf_alloc(&o->ringbuf, MICROPY_PY_BLUETOOTH_RINGBUF_SIZE);
#endif
MP_STATE_VM(bluetooth) = MP_OBJ_FROM_PTR(o);
}
return MP_STATE_VM(bluetooth);
}
static mp_obj_t bluetooth_ble_active(size_t n_args, const mp_obj_t *args) {
if (n_args == 2) {
// Boolean enable/disable argument supplied, set current state.
if (mp_obj_is_true(args[1])) {
int err = mp_bluetooth_init();
bluetooth_handle_errno(err);
} else {
mp_bluetooth_deinit();
}
}
// Return current state.
return mp_obj_new_bool(mp_bluetooth_is_active());
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_active_obj, 1, 2, bluetooth_ble_active);
static mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
if (kwargs->used == 0) {
// Get config value
if (n_args != 2) {
mp_raise_TypeError(MP_ERROR_TEXT("must query one param"));
}
switch (mp_obj_str_get_qstr(args[1])) {
case MP_QSTR_gap_name: {
const uint8_t *buf;
size_t len = mp_bluetooth_gap_get_device_name(&buf);
return mp_obj_new_bytes(buf, len);
}
case MP_QSTR_mac: {
uint8_t addr_type;
uint8_t addr[6];
mp_bluetooth_get_current_address(&addr_type, addr);
mp_obj_t items[] = { MP_OBJ_NEW_SMALL_INT(addr_type), mp_obj_new_bytes(addr, MP_ARRAY_SIZE(addr)) };
return mp_obj_new_tuple(2, items);
}
#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
case MP_QSTR_rxbuf: {
mp_obj_bluetooth_ble_t *self = MP_OBJ_TO_PTR(args[0]);
return mp_obj_new_int(self->ringbuf.size);
}
#endif
case MP_QSTR_mtu:
return mp_obj_new_int(mp_bluetooth_get_preferred_mtu());
default:
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
}
} else {
// Set config value(s)
if (n_args != 1) {
mp_raise_TypeError(MP_ERROR_TEXT("can't specify pos and kw args"));
}
for (size_t i = 0; i < kwargs->alloc; ++i) {
if (MP_MAP_SLOT_IS_FILLED(kwargs, i)) {
mp_map_elem_t *e = &kwargs->table[i];
switch (mp_obj_str_get_qstr(e->key)) {
case MP_QSTR_gap_name: {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(e->value, &bufinfo, MP_BUFFER_READ);
bluetooth_handle_errno(mp_bluetooth_gap_set_device_name(bufinfo.buf, bufinfo.len));
break;
}
#if !MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS
case MP_QSTR_rxbuf: {
// Determine new buffer sizes
mp_int_t ringbuf_alloc = mp_obj_get_int(e->value);
if (ringbuf_alloc < 16 || ringbuf_alloc > 0xffff) {
mp_raise_ValueError(NULL);
}
size_t irq_data_alloc = MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_BYTES_LEN(ringbuf_alloc);
// Allocate new buffers
uint8_t *ringbuf = m_new(uint8_t, ringbuf_alloc);
uint8_t *irq_data = m_new(uint8_t, irq_data_alloc);
// Get old buffer sizes and pointers
mp_obj_bluetooth_ble_t *self = MP_OBJ_TO_PTR(args[0]);
uint8_t *old_ringbuf_buf = self->ringbuf.buf;
size_t old_ringbuf_alloc = self->ringbuf.size;
uint8_t *old_irq_data_buf = (uint8_t *)self->irq_data_data.items;
size_t old_irq_data_alloc = self->irq_data_data_alloc;
// Atomically update the ringbuf and irq data
MICROPY_PY_BLUETOOTH_ENTER
self->ringbuf.size = ringbuf_alloc;
self->ringbuf.buf = ringbuf;
self->ringbuf.iget = 0;
self->ringbuf.iput = 0;
self->irq_data_data_alloc = irq_data_alloc;
self->irq_data_data.items = irq_data;
MICROPY_PY_BLUETOOTH_EXIT
// Free old buffers
m_del(uint8_t, old_ringbuf_buf, old_ringbuf_alloc);
m_del(uint8_t, old_irq_data_buf, old_irq_data_alloc);
break;
}
#endif
case MP_QSTR_mtu: {
mp_int_t mtu = mp_obj_get_int(e->value);
bluetooth_handle_errno(mp_bluetooth_set_preferred_mtu(mtu));
break;
}
case MP_QSTR_addr_mode: {
mp_int_t addr_mode = mp_obj_get_int(e->value);
mp_bluetooth_set_address_mode(addr_mode);
break;
}
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
case MP_QSTR_bond: {
bool bonding_enabled = mp_obj_is_true(e->value);
mp_bluetooth_set_bonding(bonding_enabled);
break;
}
case MP_QSTR_mitm: {
bool mitm_protection = mp_obj_is_true(e->value);
mp_bluetooth_set_mitm_protection(mitm_protection);
break;
}
case MP_QSTR_io: {
mp_int_t io_capability = mp_obj_get_int(e->value);
mp_bluetooth_set_io_capability(io_capability);
break;
}
case MP_QSTR_le_secure: {
bool le_secure_required = mp_obj_is_true(e->value);
mp_bluetooth_set_le_secure(le_secure_required);
break;
}
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
default:
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
}
}
}
return mp_const_none;
}
}
static MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_config_obj, 1, bluetooth_ble_config);
static mp_obj_t bluetooth_ble_irq(mp_obj_t self_in, mp_obj_t handler_in) {
(void)self_in;
if (handler_in != mp_const_none && !mp_obj_is_callable(handler_in)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid handler"));
}
// Update the callback.
MICROPY_PY_BLUETOOTH_ENTER
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
o->irq_handler = handler_in;
MICROPY_PY_BLUETOOTH_EXIT
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_irq_obj, bluetooth_ble_irq);
// ----------------------------------------------------------------------------
// Bluetooth object: GAP
// ----------------------------------------------------------------------------
static mp_obj_t bluetooth_ble_gap_advertise(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_interval_us, ARG_adv_data, ARG_resp_data, ARG_connectable };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_interval_us, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(500000)} },
{ MP_QSTR_adv_data, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_resp_data, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_connectable, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = MP_ROM_TRUE} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (args[ARG_interval_us].u_obj == mp_const_none) {
mp_bluetooth_gap_advertise_stop();
return mp_const_none;
}
mp_int_t interval_us = mp_obj_get_int(args[ARG_interval_us].u_obj);
bool connectable = mp_obj_is_true(args[ARG_connectable].u_obj);
mp_buffer_info_t adv_bufinfo = {0};
if (args[ARG_adv_data].u_obj != mp_const_none) {
mp_get_buffer_raise(args[ARG_adv_data].u_obj, &adv_bufinfo, MP_BUFFER_READ);
}
mp_buffer_info_t resp_bufinfo = {0};
if (args[ARG_resp_data].u_obj != mp_const_none) {
mp_get_buffer_raise(args[ARG_resp_data].u_obj, &resp_bufinfo, MP_BUFFER_READ);
}
return bluetooth_handle_errno(mp_bluetooth_gap_advertise_start(connectable, interval_us, adv_bufinfo.buf, adv_bufinfo.len, resp_bufinfo.buf, resp_bufinfo.len));
}
static MP_DEFINE_CONST_FUN_OBJ_KW(bluetooth_ble_gap_advertise_obj, 1, bluetooth_ble_gap_advertise);
static int bluetooth_gatts_register_service(mp_obj_t uuid_in, mp_obj_t characteristics_in, uint16_t **handles, size_t *num_handles) {
if (!mp_obj_is_type(uuid_in, &mp_type_bluetooth_uuid)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid service UUID"));
}
mp_obj_bluetooth_uuid_t *service_uuid = MP_OBJ_TO_PTR(uuid_in);
mp_obj_t len_in = mp_obj_len(characteristics_in);
size_t len = mp_obj_get_int(len_in);
mp_obj_iter_buf_t iter_buf;
mp_obj_t iterable = mp_getiter(characteristics_in, &iter_buf);
mp_obj_t characteristic_obj;
// Lists of characteristic uuids and flags.
mp_obj_bluetooth_uuid_t **characteristic_uuids = m_new(mp_obj_bluetooth_uuid_t *, len);
uint16_t *characteristic_flags = m_new(uint16_t, len);
// Flattened list of descriptor uuids and flags. Grows (realloc) as more descriptors are encountered.
mp_obj_bluetooth_uuid_t **descriptor_uuids = NULL;
uint16_t *descriptor_flags = NULL;
// How many descriptors in the flattened list per characteristic.
uint8_t *num_descriptors = m_new(uint8_t, len);
// Initially allocate enough room for the number of characteristics.
// Will be grown to accommodate descriptors as necessary.
*num_handles = len;
*handles = m_new(uint16_t, *num_handles);
// Extract out characteristic uuids & flags.
int characteristic_index = 0; // characteristic index.
int handle_index = 0; // handle index.
int descriptor_index = 0; // descriptor index.
while ((characteristic_obj = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
// (uuid, flags, (optional descriptors),)
size_t characteristic_len;
mp_obj_t *characteristic_items;
mp_obj_get_array(characteristic_obj, &characteristic_len, &characteristic_items);
if (characteristic_len < 2 || characteristic_len > 3) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid characteristic tuple"));
}
mp_obj_t uuid_obj = characteristic_items[0];
if (!mp_obj_is_type(uuid_obj, &mp_type_bluetooth_uuid)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid characteristic UUID"));
}
(*handles)[handle_index++] = 0xffff;
// Optional third element, iterable of descriptors.
if (characteristic_len >= 3) {
mp_int_t n = mp_obj_get_int(mp_obj_len(characteristic_items[2]));
if (n) {
num_descriptors[characteristic_index] = n;
// Grow the flattened uuids and flags arrays with this many more descriptors.
descriptor_uuids = m_renew(mp_obj_bluetooth_uuid_t *, descriptor_uuids, descriptor_index, descriptor_index + num_descriptors[characteristic_index]);
descriptor_flags = m_renew(uint16_t, descriptor_flags, descriptor_index, descriptor_index + num_descriptors[characteristic_index]);
// Also grow the handles array.
*handles = m_renew(uint16_t, *handles, *num_handles, *num_handles + num_descriptors[characteristic_index]);
mp_obj_iter_buf_t iter_buf_desc;
mp_obj_t iterable_desc = mp_getiter(characteristic_items[2], &iter_buf_desc);
mp_obj_t descriptor_obj;
// Extract out descriptors for this characteristic.
while ((descriptor_obj = mp_iternext(iterable_desc)) != MP_OBJ_STOP_ITERATION) {
// (uuid, flags,)
mp_obj_t *descriptor_items;
mp_obj_get_array_fixed_n(descriptor_obj, 2, &descriptor_items);
mp_obj_t desc_uuid_obj = descriptor_items[0];
if (!mp_obj_is_type(desc_uuid_obj, &mp_type_bluetooth_uuid)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid descriptor UUID"));
}
descriptor_uuids[descriptor_index] = MP_OBJ_TO_PTR(desc_uuid_obj);
descriptor_flags[descriptor_index] = mp_obj_get_int(descriptor_items[1]);
++descriptor_index;
(*handles)[handle_index++] = 0xffff;
}
// Reflect that we've grown the handles array.
*num_handles += num_descriptors[characteristic_index];
}
}
characteristic_uuids[characteristic_index] = MP_OBJ_TO_PTR(uuid_obj);
characteristic_flags[characteristic_index] = mp_obj_get_int(characteristic_items[1]);
++characteristic_index;
}
// Add service.
return mp_bluetooth_gatts_register_service(service_uuid, characteristic_uuids, characteristic_flags, descriptor_uuids, descriptor_flags, num_descriptors, *handles, len);
}
static mp_obj_t bluetooth_ble_gatts_register_services(mp_obj_t self_in, mp_obj_t services_in) {
(void)self_in;
mp_obj_t len_in = mp_obj_len(services_in);
size_t len = mp_obj_get_int(len_in);
mp_obj_iter_buf_t iter_buf;
mp_obj_t iterable = mp_getiter(services_in, &iter_buf);
mp_obj_t service_tuple_obj;
mp_obj_tuple_t *result = MP_OBJ_TO_PTR(mp_obj_new_tuple(len, NULL));
uint16_t **handles = m_new0(uint16_t *, len);
size_t *num_handles = m_new0(size_t, len);
// TODO: Add a `append` kwarg (defaulting to False) to make this behavior optional.
bool append = false;
int err = mp_bluetooth_gatts_register_service_begin(append);
if (err != 0) {
return bluetooth_handle_errno(err);
}
size_t i = 0;
while ((service_tuple_obj = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
// (uuid, chars)
mp_obj_t *service_items;
mp_obj_get_array_fixed_n(service_tuple_obj, 2, &service_items);
err = bluetooth_gatts_register_service(service_items[0], service_items[1], &handles[i], &num_handles[i]);
if (err != 0) {
return bluetooth_handle_errno(err);
}
++i;
}
// On Nimble, this will actually perform the registration, making the handles valid.
err = mp_bluetooth_gatts_register_service_end();
if (err != 0) {
return bluetooth_handle_errno(err);
}
// Return tuple of tuple of value handles.
// TODO: Also the Generic Access service characteristics?
for (i = 0; i < len; ++i) {
mp_obj_tuple_t *service_handles = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_handles[i], NULL));
for (size_t j = 0; j < num_handles[i]; ++j) {
service_handles->items[j] = MP_OBJ_NEW_SMALL_INT(handles[i][j]);
}
result->items[i] = MP_OBJ_FROM_PTR(service_handles);
}
// Free temporary arrays.
m_del(uint16_t *, handles, len);
m_del(size_t, num_handles, len);
return MP_OBJ_FROM_PTR(result);
}
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_register_services_obj, bluetooth_ble_gatts_register_services);
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
static mp_obj_t bluetooth_ble_gap_connect(size_t n_args, const mp_obj_t *args) {
if (n_args == 2) {
if (args[1] == mp_const_none) {
int err = mp_bluetooth_gap_peripheral_connect_cancel();
return bluetooth_handle_errno(err);
}
mp_raise_TypeError(MP_ERROR_TEXT("invalid addr"));
}
uint8_t addr_type = mp_obj_get_int(args[1]);
mp_buffer_info_t bufinfo = {0};
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
if (bufinfo.len != 6) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid addr"));
}
mp_int_t scan_duration_ms = MP_BLUETOOTH_CONNECT_DEFAULT_SCAN_DURATION_MS;
mp_int_t min_conn_interval_us = 0;
mp_int_t max_conn_interval_us = 0;
if (n_args >= 4 && args[3] != mp_const_none) {
scan_duration_ms = mp_obj_get_int(args[3]);
}
if (n_args >= 5 && args[4] != mp_const_none) {
min_conn_interval_us = mp_obj_get_int(args[4]);
}
if (n_args >= 6 && args[5] != mp_const_none) {
max_conn_interval_us = mp_obj_get_int(args[5]);
}
int err = mp_bluetooth_gap_peripheral_connect(addr_type, bufinfo.buf, scan_duration_ms, min_conn_interval_us, max_conn_interval_us);
return bluetooth_handle_errno(err);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_connect_obj, 2, 6, bluetooth_ble_gap_connect);
static mp_obj_t bluetooth_ble_gap_scan(size_t n_args, const mp_obj_t *args) {
// Default is indefinite scan, with the NimBLE "background scan" interval and window.
mp_int_t duration_ms = 0;
mp_int_t interval_us = 1280000;
mp_int_t window_us = 11250;
bool active_scan = false;
if (n_args > 1) {
if (args[1] == mp_const_none) {
// scan(None) --> stop scan.
return bluetooth_handle_errno(mp_bluetooth_gap_scan_stop());
}
duration_ms = mp_obj_get_int(args[1]);
if (n_args > 2) {
interval_us = mp_obj_get_int(args[2]);
if (n_args > 3) {
window_us = mp_obj_get_int(args[3]);
if (n_args > 4) {
active_scan = mp_obj_is_true(args[4]);
}
}
}
}
return bluetooth_handle_errno(mp_bluetooth_gap_scan_start(duration_ms, interval_us, window_us, active_scan));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_scan_obj, 1, 5, bluetooth_ble_gap_scan);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
static mp_obj_t bluetooth_ble_gap_disconnect(mp_obj_t self_in, mp_obj_t conn_handle_in) {
(void)self_in;
uint16_t conn_handle = mp_obj_get_int(conn_handle_in);
int err = mp_bluetooth_gap_disconnect(conn_handle);
if (err == 0) {
return mp_const_true;
} else if (err == MP_ENOTCONN) {
return mp_const_false;
} else {
return bluetooth_handle_errno(err);
}
}
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_disconnect_obj, bluetooth_ble_gap_disconnect);
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
static mp_obj_t bluetooth_ble_gap_pair(mp_obj_t self_in, mp_obj_t conn_handle_in) {
(void)self_in;
uint16_t conn_handle = mp_obj_get_int(conn_handle_in);
return bluetooth_handle_errno(mp_bluetooth_gap_pair(conn_handle));
}
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gap_pair_obj, bluetooth_ble_gap_pair);
static mp_obj_t bluetooth_ble_gap_passkey(size_t n_args, const mp_obj_t *args) {
uint16_t conn_handle = mp_obj_get_int(args[1]);
uint8_t action = mp_obj_get_int(args[2]);
mp_int_t passkey = mp_obj_get_int(args[3]);
return bluetooth_handle_errno(mp_bluetooth_gap_passkey(conn_handle, action, passkey));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gap_passkey_obj, 4, 4, bluetooth_ble_gap_passkey);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
// ----------------------------------------------------------------------------
// Bluetooth object: GATTS (Peripheral/Advertiser role)
// ----------------------------------------------------------------------------
static mp_obj_t bluetooth_ble_gatts_read(mp_obj_t self_in, mp_obj_t value_handle_in) {
(void)self_in;
size_t len = 0;
const uint8_t *buf;
mp_bluetooth_gatts_read(mp_obj_get_int(value_handle_in), &buf, &len);
return mp_obj_new_bytes(buf, len);
}
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gatts_read_obj, bluetooth_ble_gatts_read);
static mp_obj_t bluetooth_ble_gatts_write(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo = {0};
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
bool send_update = false;
if (n_args > 3) {
send_update = mp_obj_is_true(args[3]);
}
bluetooth_handle_errno(mp_bluetooth_gatts_write(mp_obj_get_int(args[1]), bufinfo.buf, bufinfo.len, send_update));
return MP_OBJ_NEW_SMALL_INT(bufinfo.len);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_write_obj, 3, 4, bluetooth_ble_gatts_write);
static mp_obj_t bluetooth_ble_gatts_notify_indicate(size_t n_args, const mp_obj_t *args, int gatts_op) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t value_handle = mp_obj_get_int(args[2]);
const uint8_t *value = NULL;
size_t value_len = 0;
if (n_args == 4 && args[3] != mp_const_none) {
mp_buffer_info_t bufinfo = {0};
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
value = bufinfo.buf;
value_len = bufinfo.len;
}
return bluetooth_handle_errno(mp_bluetooth_gatts_notify_indicate(conn_handle, value_handle, gatts_op, value, value_len));
}
static mp_obj_t bluetooth_ble_gatts_notify(size_t n_args, const mp_obj_t *args) {
return bluetooth_ble_gatts_notify_indicate(n_args, args, MP_BLUETOOTH_GATTS_OP_NOTIFY);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_notify_obj, 3, 4, bluetooth_ble_gatts_notify);
static mp_obj_t bluetooth_ble_gatts_indicate(size_t n_args, const mp_obj_t *args) {
return bluetooth_ble_gatts_notify_indicate(n_args, args, MP_BLUETOOTH_GATTS_OP_INDICATE);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_indicate_obj, 3, 4, bluetooth_ble_gatts_indicate);
static mp_obj_t bluetooth_ble_gatts_set_buffer(size_t n_args, const mp_obj_t *args) {
mp_int_t value_handle = mp_obj_get_int(args[1]);
mp_int_t len = mp_obj_get_int(args[2]);
bool append = n_args >= 4 && mp_obj_is_true(args[3]);
return bluetooth_handle_errno(mp_bluetooth_gatts_set_buffer(value_handle, len, append));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gatts_set_buffer_obj, 3, 4, bluetooth_ble_gatts_set_buffer);
// ----------------------------------------------------------------------------
// Bluetooth object: GATTC (Central/Scanner role)
// ----------------------------------------------------------------------------
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
static mp_obj_t bluetooth_ble_gattc_discover_services(size_t n_args, const mp_obj_t *args) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_obj_bluetooth_uuid_t *uuid = NULL;
if (n_args == 3 && args[2] != mp_const_none) {
if (!mp_obj_is_type(args[2], &mp_type_bluetooth_uuid)) {
mp_raise_TypeError(MP_ERROR_TEXT("UUID"));
}
uuid = MP_OBJ_TO_PTR(args[2]);
}
return bluetooth_handle_errno(mp_bluetooth_gattc_discover_primary_services(conn_handle, uuid));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_discover_services_obj, 2, 3, bluetooth_ble_gattc_discover_services);
static mp_obj_t bluetooth_ble_gattc_discover_characteristics(size_t n_args, const mp_obj_t *args) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t start_handle = mp_obj_get_int(args[2]);
mp_int_t end_handle = mp_obj_get_int(args[3]);
mp_obj_bluetooth_uuid_t *uuid = NULL;
if (n_args == 5 && args[4] != mp_const_none) {
if (!mp_obj_is_type(args[4], &mp_type_bluetooth_uuid)) {
mp_raise_TypeError(MP_ERROR_TEXT("UUID"));
}
uuid = MP_OBJ_TO_PTR(args[4]);
}
return bluetooth_handle_errno(mp_bluetooth_gattc_discover_characteristics(conn_handle, start_handle, end_handle, uuid));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_discover_characteristics_obj, 4, 5, bluetooth_ble_gattc_discover_characteristics);
static mp_obj_t bluetooth_ble_gattc_discover_descriptors(size_t n_args, const mp_obj_t *args) {
(void)n_args;
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t start_handle = mp_obj_get_int(args[2]);
mp_int_t end_handle = mp_obj_get_int(args[3]);
return bluetooth_handle_errno(mp_bluetooth_gattc_discover_descriptors(conn_handle, start_handle, end_handle));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_discover_descriptors_obj, 4, 4, bluetooth_ble_gattc_discover_descriptors);
static mp_obj_t bluetooth_ble_gattc_read(mp_obj_t self_in, mp_obj_t conn_handle_in, mp_obj_t value_handle_in) {
(void)self_in;
mp_int_t conn_handle = mp_obj_get_int(conn_handle_in);
mp_int_t value_handle = mp_obj_get_int(value_handle_in);
return bluetooth_handle_errno(mp_bluetooth_gattc_read(conn_handle, value_handle));
}
static MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_gattc_read_obj, bluetooth_ble_gattc_read);
static mp_obj_t bluetooth_ble_gattc_write(size_t n_args, const mp_obj_t *args) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t value_handle = mp_obj_get_int(args[2]);
mp_obj_t data = args[3];
mp_buffer_info_t bufinfo = {0};
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
unsigned int mode = MP_BLUETOOTH_WRITE_MODE_NO_RESPONSE;
if (n_args == 5) {
mode = mp_obj_get_int(args[4]);
}
return bluetooth_handle_errno(mp_bluetooth_gattc_write(conn_handle, value_handle, bufinfo.buf, bufinfo.len, mode));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_gattc_write_obj, 4, 5, bluetooth_ble_gattc_write);
static mp_obj_t bluetooth_ble_gattc_exchange_mtu(mp_obj_t self_in, mp_obj_t conn_handle_in) {
(void)self_in;
uint16_t conn_handle = mp_obj_get_int(conn_handle_in);
return bluetooth_handle_errno(mp_bluetooth_gattc_exchange_mtu(conn_handle));
}
static MP_DEFINE_CONST_FUN_OBJ_2(bluetooth_ble_gattc_exchange_mtu_obj, bluetooth_ble_gattc_exchange_mtu);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
static mp_obj_t bluetooth_ble_l2cap_listen(mp_obj_t self_in, mp_obj_t psm_in, mp_obj_t mtu_in) {
(void)self_in;
mp_int_t psm = mp_obj_get_int(psm_in);
mp_int_t mtu = MAX(32, MIN(UINT16_MAX, mp_obj_get_int(mtu_in)));
return bluetooth_handle_errno(mp_bluetooth_l2cap_listen(psm, mtu));
}
static MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_l2cap_listen_obj, bluetooth_ble_l2cap_listen);
static mp_obj_t bluetooth_ble_l2cap_connect(size_t n_args, const mp_obj_t *args) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t psm = mp_obj_get_int(args[2]);
mp_int_t mtu = MAX(32, MIN(UINT16_MAX, mp_obj_get_int(args[3])));
return bluetooth_handle_errno(mp_bluetooth_l2cap_connect(conn_handle, psm, mtu));
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_connect_obj, 4, 4, bluetooth_ble_l2cap_connect);
static mp_obj_t bluetooth_ble_l2cap_disconnect(mp_obj_t self_in, mp_obj_t conn_handle_in, mp_obj_t cid_in) {
(void)self_in;
mp_int_t conn_handle = mp_obj_get_int(conn_handle_in);
mp_int_t cid = mp_obj_get_int(cid_in);
return bluetooth_handle_errno(mp_bluetooth_l2cap_disconnect(conn_handle, cid));
}
static MP_DEFINE_CONST_FUN_OBJ_3(bluetooth_ble_l2cap_disconnect_obj, bluetooth_ble_l2cap_disconnect);
static mp_obj_t bluetooth_ble_l2cap_send(size_t n_args, const mp_obj_t *args) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t cid = mp_obj_get_int(args[2]);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
bool stalled = false;
bluetooth_handle_errno(mp_bluetooth_l2cap_send(conn_handle, cid, bufinfo.buf, bufinfo.len, &stalled));
// Return True if the channel is still ready to send.
return mp_obj_new_bool(!stalled);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_send_obj, 4, 4, bluetooth_ble_l2cap_send);
static mp_obj_t bluetooth_ble_l2cap_recvinto(size_t n_args, const mp_obj_t *args) {
mp_int_t conn_handle = mp_obj_get_int(args[1]);
mp_int_t cid = mp_obj_get_int(args[2]);
mp_buffer_info_t bufinfo = {0};
if (args[3] != mp_const_none) {
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_WRITE);
}
bluetooth_handle_errno(mp_bluetooth_l2cap_recvinto(conn_handle, cid, bufinfo.buf, &bufinfo.len));
return MP_OBJ_NEW_SMALL_INT(bufinfo.len);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_l2cap_recvinto_obj, 4, 4, bluetooth_ble_l2cap_recvinto);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD
static mp_obj_t bluetooth_ble_hci_cmd(size_t n_args, const mp_obj_t *args) {
mp_int_t ogf = mp_obj_get_int(args[1]);
mp_int_t ocf = mp_obj_get_int(args[2]);
mp_buffer_info_t bufinfo_request = {0};
mp_buffer_info_t bufinfo_response = {0};
mp_get_buffer_raise(args[3], &bufinfo_request, MP_BUFFER_READ);
mp_get_buffer_raise(args[4], &bufinfo_response, MP_BUFFER_WRITE);
uint8_t status = 0;
bluetooth_handle_errno(mp_bluetooth_hci_cmd(ogf, ocf, bufinfo_request.buf, bufinfo_request.len, bufinfo_response.buf, bufinfo_response.len, &status));
return MP_OBJ_NEW_SMALL_INT(status);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(bluetooth_ble_hci_cmd_obj, 5, 5, bluetooth_ble_hci_cmd);
#endif // MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD
// ----------------------------------------------------------------------------
// Bluetooth object: Definition
// ----------------------------------------------------------------------------
static const mp_rom_map_elem_t bluetooth_ble_locals_dict_table[] = {
// General
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&bluetooth_ble_active_obj) },
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&bluetooth_ble_config_obj) },
{ MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&bluetooth_ble_irq_obj) },
// GAP
{ MP_ROM_QSTR(MP_QSTR_gap_advertise), MP_ROM_PTR(&bluetooth_ble_gap_advertise_obj) },
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
{ MP_ROM_QSTR(MP_QSTR_gap_connect), MP_ROM_PTR(&bluetooth_ble_gap_connect_obj) },
{ MP_ROM_QSTR(MP_QSTR_gap_scan), MP_ROM_PTR(&bluetooth_ble_gap_scan_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_gap_disconnect), MP_ROM_PTR(&bluetooth_ble_gap_disconnect_obj) },
#if MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING
{ MP_ROM_QSTR(MP_QSTR_gap_pair), MP_ROM_PTR(&bluetooth_ble_gap_pair_obj) },
{ MP_ROM_QSTR(MP_QSTR_gap_passkey), MP_ROM_PTR(&bluetooth_ble_gap_passkey_obj) },
#endif
// GATT Server
{ MP_ROM_QSTR(MP_QSTR_gatts_register_services), MP_ROM_PTR(&bluetooth_ble_gatts_register_services_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_read), MP_ROM_PTR(&bluetooth_ble_gatts_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_write), MP_ROM_PTR(&bluetooth_ble_gatts_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_notify), MP_ROM_PTR(&bluetooth_ble_gatts_notify_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_indicate), MP_ROM_PTR(&bluetooth_ble_gatts_indicate_obj) },
{ MP_ROM_QSTR(MP_QSTR_gatts_set_buffer), MP_ROM_PTR(&bluetooth_ble_gatts_set_buffer_obj) },
#if MICROPY_PY_BLUETOOTH_ENABLE_GATT_CLIENT
// GATT Client
{ MP_ROM_QSTR(MP_QSTR_gattc_discover_services), MP_ROM_PTR(&bluetooth_ble_gattc_discover_services_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_discover_characteristics), MP_ROM_PTR(&bluetooth_ble_gattc_discover_characteristics_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_discover_descriptors), MP_ROM_PTR(&bluetooth_ble_gattc_discover_descriptors_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_read), MP_ROM_PTR(&bluetooth_ble_gattc_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_write), MP_ROM_PTR(&bluetooth_ble_gattc_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_gattc_exchange_mtu), MP_ROM_PTR(&bluetooth_ble_gattc_exchange_mtu_obj) },
#endif
#if MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
{ MP_ROM_QSTR(MP_QSTR_l2cap_listen), MP_ROM_PTR(&bluetooth_ble_l2cap_listen_obj) },
{ MP_ROM_QSTR(MP_QSTR_l2cap_connect), MP_ROM_PTR(&bluetooth_ble_l2cap_connect_obj) },
{ MP_ROM_QSTR(MP_QSTR_l2cap_disconnect), MP_ROM_PTR(&bluetooth_ble_l2cap_disconnect_obj) },
{ MP_ROM_QSTR(MP_QSTR_l2cap_send), MP_ROM_PTR(&bluetooth_ble_l2cap_send_obj) },
{ MP_ROM_QSTR(MP_QSTR_l2cap_recvinto), MP_ROM_PTR(&bluetooth_ble_l2cap_recvinto_obj) },
#endif
#if MICROPY_PY_BLUETOOTH_ENABLE_HCI_CMD
{ MP_ROM_QSTR(MP_QSTR_hci_cmd), MP_ROM_PTR(&bluetooth_ble_hci_cmd_obj) },
#endif
};
static MP_DEFINE_CONST_DICT(bluetooth_ble_locals_dict, bluetooth_ble_locals_dict_table);
static MP_DEFINE_CONST_OBJ_TYPE(
mp_type_bluetooth_ble,
MP_QSTR_BLE,
MP_TYPE_FLAG_NONE,
make_new, bluetooth_ble_make_new,
locals_dict, &bluetooth_ble_locals_dict
);
static const mp_rom_map_elem_t mp_module_bluetooth_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_bluetooth) },
{ MP_ROM_QSTR(MP_QSTR_BLE), MP_ROM_PTR(&mp_type_bluetooth_ble) },
{ MP_ROM_QSTR(MP_QSTR_UUID), MP_ROM_PTR(&mp_type_bluetooth_uuid) },
// TODO: Deprecate these flags (recommend copying the constants from modbluetooth.h instead).
{ MP_ROM_QSTR(MP_QSTR_FLAG_READ), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_READ) },
{ MP_ROM_QSTR(MP_QSTR_FLAG_WRITE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE) },
{ MP_ROM_QSTR(MP_QSTR_FLAG_NOTIFY), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_NOTIFY) },
{ MP_ROM_QSTR(MP_QSTR_FLAG_INDICATE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_INDICATE) },
{ MP_ROM_QSTR(MP_QSTR_FLAG_WRITE_NO_RESPONSE), MP_ROM_INT(MP_BLUETOOTH_CHARACTERISTIC_FLAG_WRITE_NO_RESPONSE) },
};
static MP_DEFINE_CONST_DICT(mp_module_bluetooth_globals, mp_module_bluetooth_globals_table);