4
4
* The MIT License (MIT)
5
5
*
6
6
* Copyright (c) 2018 Ayke van Laethem
7
- * Copyright (c) 2019 Jim Mussared
7
+ * Copyright (c) 2019-2020 Jim Mussared
8
8
*
9
9
* Permission is hereby granted, free of charge, to any person obtaining a copy
10
10
* of this software and associated documentation files (the "Software"), to deal
@@ -57,7 +57,6 @@ STATIC const mp_obj_type_t bluetooth_uuid_type;
57
57
typedef struct {
58
58
mp_obj_base_t base ;
59
59
mp_obj_t irq_handler ;
60
- uint16_t irq_trigger ;
61
60
bool irq_scheduled ;
62
61
mp_obj_t irq_data_tuple ;
63
62
uint8_t irq_data_addr_bytes [6 ];
@@ -249,7 +248,6 @@ STATIC mp_obj_t bluetooth_ble_make_new(const mp_obj_type_t *type, size_t n_args,
249
248
o -> base .type = & bluetooth_ble_type ;
250
249
251
250
o -> irq_handler = mp_const_none ;
252
- o -> irq_trigger = 0 ;
253
251
254
252
// Pre-allocate the event data tuple to prevent needing to allocate in the IRQ handler.
255
253
o -> irq_data_tuple = mp_obj_new_tuple (MICROPY_PY_BLUETOOTH_MAX_EVENT_DATA_TUPLE_LEN , NULL );
@@ -372,10 +370,9 @@ STATIC mp_obj_t bluetooth_ble_config(size_t n_args, const mp_obj_t *args, mp_map
372
370
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (bluetooth_ble_config_obj , 1 , bluetooth_ble_config );
373
371
374
372
STATIC mp_obj_t bluetooth_ble_irq (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
375
- enum { ARG_handler , ARG_trigger };
373
+ enum { ARG_handler };
376
374
static const mp_arg_t allowed_args [] = {
377
375
{ MP_QSTR_handler , MP_ARG_OBJ | MP_ARG_REQUIRED , {.u_rom_obj = MP_ROM_NONE } },
378
- { MP_QSTR_trigger , MP_ARG_INT , {.u_int = MP_BLUETOOTH_IRQ_ALL } },
379
376
};
380
377
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
381
378
mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
@@ -388,7 +385,6 @@ STATIC mp_obj_t bluetooth_ble_irq(size_t n_args, const mp_obj_t *pos_args, mp_ma
388
385
MICROPY_PY_BLUETOOTH_ENTER
389
386
mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
390
387
o -> irq_handler = callback ;
391
- o -> irq_trigger = args [ARG_trigger ].u_int ;
392
388
MICROPY_PY_BLUETOOTH_EXIT
393
389
394
390
return mp_const_none ;
@@ -854,7 +850,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
854
850
for (;;) {
855
851
MICROPY_PY_BLUETOOTH_ENTER
856
852
857
- mp_int_t event = event = ringbuf_get16 (& o -> ringbuf );
853
+ mp_int_t event = ringbuf_get (& o -> ringbuf );
858
854
if (event < 0 ) {
859
855
// Nothing available in ringbuf.
860
856
MICROPY_PY_BLUETOOTH_EXIT
@@ -878,7 +874,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
878
874
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT ) {
879
875
// addr_type, addr, adv_type, rssi, adv_data
880
876
ringbuf_extract (& o -> ringbuf , data_tuple , 0 , 1 , & o -> irq_data_addr , 2 , NULL , & o -> irq_data_data );
881
- } else if (event == MP_BLUETOOTH_IRQ_SCAN_COMPLETE ) {
877
+ } else if (event == MP_BLUETOOTH_IRQ_SCAN_DONE ) {
882
878
// No params required.
883
879
data_tuple -> len = 0 ;
884
880
} else if (event == MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT ) {
@@ -893,7 +889,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
893
889
} else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE ) {
894
890
// conn_handle, value_handle, data
895
891
ringbuf_extract (& o -> ringbuf , data_tuple , 2 , 0 , NULL , 0 , NULL , & o -> irq_data_data );
896
- } else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_STATUS ) {
892
+ } else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE ) {
897
893
// conn_handle, value_handle, status
898
894
ringbuf_extract (& o -> ringbuf , data_tuple , 3 , 0 , NULL , 0 , NULL , NULL );
899
895
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
@@ -915,23 +911,24 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluetooth_ble_invoke_irq_obj, bluetooth_ble_inv
915
911
// Callbacks are called in interrupt context (i.e. can't allocate), so we need to push the data
916
912
// into the ringbuf and schedule the callback via mp_sched_schedule.
917
913
918
- STATIC bool enqueue_irq (mp_obj_bluetooth_ble_t * o , size_t len , uint16_t event ) {
919
- if (!o || !( o -> irq_trigger & event ) || o -> irq_handler == mp_const_none ) {
914
+ STATIC bool enqueue_irq (mp_obj_bluetooth_ble_t * o , size_t len , uint8_t event ) {
915
+ if (!o || o -> irq_handler == mp_const_none ) {
920
916
return false;
921
917
}
922
918
923
- if (ringbuf_free (& o -> ringbuf ) < len + 2 ) {
919
+ // Check if there is enough room for <event-type><payload>.
920
+ if (ringbuf_free (& o -> ringbuf ) < len + 1 ) {
924
921
// Ringbuffer doesn't have room (and is therefore non-empty).
925
922
926
923
// If this is another scan result, or the front of the ringbuffer isn't a scan result, then nothing to do.
927
- if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT || ringbuf_peek16 (& o -> ringbuf ) != MP_BLUETOOTH_IRQ_SCAN_RESULT ) {
924
+ if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT || ringbuf_peek (& o -> ringbuf ) != MP_BLUETOOTH_IRQ_SCAN_RESULT ) {
928
925
return false;
929
926
}
930
927
931
928
// Front of the queue is a scan result, remove it.
932
929
933
930
// event, addr_type, addr, adv_type, rssi
934
- int n = 2 + 1 + 6 + 1 + 1 ;
931
+ int n = 1 + 1 + 6 + 1 + 1 ;
935
932
for (int i = 0 ; i < n ; ++ i ) {
936
933
ringbuf_get (& o -> ringbuf );
937
934
}
@@ -943,7 +940,7 @@ STATIC bool enqueue_irq(mp_obj_bluetooth_ble_t *o, size_t len, uint16_t event) {
943
940
}
944
941
945
942
// Append this event, the caller will then append the arguments.
946
- ringbuf_put16 (& o -> ringbuf , event );
943
+ ringbuf_put (& o -> ringbuf , event );
947
944
return true;
948
945
}
949
946
@@ -959,7 +956,7 @@ STATIC void schedule_ringbuf(mp_uint_t atomic_state) {
959
956
}
960
957
}
961
958
962
- void mp_bluetooth_gap_on_connected_disconnected (uint16_t event , uint16_t conn_handle , uint8_t addr_type , const uint8_t * addr ) {
959
+ void mp_bluetooth_gap_on_connected_disconnected (uint8_t event , uint16_t conn_handle , uint8_t addr_type , const uint8_t * addr ) {
963
960
MICROPY_PY_BLUETOOTH_ENTER
964
961
mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
965
962
if (enqueue_irq (o , 2 + 1 + 6 , event )) {
@@ -986,7 +983,7 @@ void mp_bluetooth_gatts_on_write(uint16_t conn_handle, uint16_t value_handle) {
986
983
void mp_bluetooth_gap_on_scan_complete (void ) {
987
984
MICROPY_PY_BLUETOOTH_ENTER
988
985
mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
989
- if (enqueue_irq (o , 0 , MP_BLUETOOTH_IRQ_SCAN_COMPLETE )) {
986
+ if (enqueue_irq (o , 0 , MP_BLUETOOTH_IRQ_SCAN_DONE )) {
990
987
}
991
988
schedule_ringbuf (atomic_state );
992
989
}
@@ -1048,7 +1045,7 @@ void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t hand
1048
1045
schedule_ringbuf (atomic_state );
1049
1046
}
1050
1047
1051
- size_t mp_bluetooth_gattc_on_data_available_start (uint16_t event , uint16_t conn_handle , uint16_t value_handle , size_t data_len , mp_uint_t * atomic_state_out ) {
1048
+ size_t mp_bluetooth_gattc_on_data_available_start (uint8_t event , uint16_t conn_handle , uint16_t value_handle , size_t data_len , mp_uint_t * atomic_state_out ) {
1052
1049
MICROPY_PY_BLUETOOTH_ENTER
1053
1050
* atomic_state_out = atomic_state ;
1054
1051
mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
@@ -1077,7 +1074,7 @@ void mp_bluetooth_gattc_on_data_available_end(mp_uint_t atomic_state) {
1077
1074
void mp_bluetooth_gattc_on_write_status (uint16_t conn_handle , uint16_t value_handle , uint16_t status ) {
1078
1075
MICROPY_PY_BLUETOOTH_ENTER
1079
1076
mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
1080
- if (enqueue_irq (o , 2 + 2 + 2 , MP_BLUETOOTH_IRQ_GATTC_WRITE_STATUS )) {
1077
+ if (enqueue_irq (o , 2 + 2 + 2 , MP_BLUETOOTH_IRQ_GATTC_WRITE_DONE )) {
1081
1078
ringbuf_put16 (& o -> ringbuf , conn_handle );
1082
1079
ringbuf_put16 (& o -> ringbuf , value_handle );
1083
1080
ringbuf_put16 (& o -> ringbuf , status );
10000
@@ -1092,7 +1089,7 @@ void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_han
1092
1089
// On ESP32, for example, this is not the case.
1093
1090
bool mp_bluetooth_gatts_on_read_request (uint16_t conn_handle , uint16_t value_handle ) {
1094
1091
mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
1095
- if (( o -> irq_trigger & MP_BLUETOOTH_IRQ_GATTS_READ_REQUEST ) && o -> irq_handler != mp_const_none ) {
1092
+ if (o -> irq_handler != mp_const_none ) {
1096
1093
// Use pre-allocated tuple because this is a hard IRQ.
1097
1094
mp_obj_tuple_t * data = MP_OBJ_TO_PTR (o -> irq_data_tuple );
1098
1095
data -> items [0 ] = MP_OBJ_NEW_SMALL_INT (conn_handle );
0 commit comments