44 * The MIT License (MIT)
55 *
66 * Copyright (c) 2018 Ayke van Laethem
7- * Copyright (c) 2019 Jim Mussared
7+ * Copyright (c) 2019-2020 Jim Mussared
88 *
99 * Permission is hereby granted, free of charge, to any person obtaining a copy
1010 * of this software and associated documentation files (the "Software"), to deal
@@ -57,7 +57,6 @@ STATIC const mp_obj_type_t bluetooth_uuid_type;
5757typedef struct {
5858 mp_obj_base_t base ;
5959 mp_obj_t irq_handler ;
60- uint16_t irq_trigger ;
6160 bool irq_scheduled ;
6261 mp_obj_t irq_data_tuple ;
6362 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,
249248 o -> base .type = & bluetooth_ble_type ;
250249
251250 o -> irq_handler = mp_const_none ;
252- o -> irq_trigger = 0 ;
253251
254252 // Pre-allocate the event data tuple to prevent needing to allocate in the IRQ handler.
255253 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
372370STATIC MP_DEFINE_CONST_FUN_OBJ_KW (bluetooth_ble_config_obj , 1 , bluetooth_ble_config );
373371
374372STATIC 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 };
376374 static const mp_arg_t allowed_args [] = {
377375 { 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 } },
379376 };
380377 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
381378 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
388385 MICROPY_PY_BLUETOOTH_ENTER
389386 mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
390387 o -> irq_handler = callback ;
391- o -> irq_trigger = args [ARG_trigger ].u_int ;
392388 MICROPY_PY_BLUETOOTH_EXIT
393389
394390 return mp_const_none ;
@@ -854,7 +850,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
854850 for (;;) {
855851 MICROPY_PY_BLUETOOTH_ENTER
856852
857- mp_int_t event = event = ringbuf_get16 (& o -> ringbuf );
853+ mp_int_t event = ringbuf_get (& o -> ringbuf );
858854 if (event < 0 ) {
859855 // Nothing available in ringbuf.
860856 MICROPY_PY_BLUETOOTH_EXIT
@@ -878,7 +874,7 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
878874 } else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT ) {
879875 // addr_type, addr, adv_type, rssi, adv_data
880876 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 ) {
882878 // No params required.
883879 data_tuple -> len = 0 ;
884880 } 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) {
893889 } else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE ) {
894890 // conn_handle, value_handle, data
895891 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 ) {
897893 // conn_handle, value_handle, status
898894 ringbuf_extract (& o -> ringbuf , data_tuple , 3 , 0 , NULL , 0 , NULL , NULL );
899895 #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
915911// Callbacks are called in interrupt context (i.e. can't allocate), so we need to push the data
916912// into the ringbuf and schedule the callback via mp_sched_schedule.
917913
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 ) {
920916 return false;
921917 }
922918
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 ) {
924921 // Ringbuffer doesn't have room (and is therefore non-empty).
925922
926923 // 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 ) {
928925 return false;
929926 }
930927
931928 // Front of the queue is a scan result, remove it.
932929
933930 // event, addr_type, addr, adv_type, rssi
934- int n = 2 + 1 + 6 + 1 + 1 ;
931+ int n = 1 + 1 + 6 + 1 + 1 ;
935932 for (int i = 0 ; i < n ; ++ i ) {
936933 ringbuf_get (& o -> ringbuf );
937934 }
@@ -943,7 +940,7 @@ STATIC bool enqueue_irq(mp_obj_bluetooth_ble_t *o, size_t len, uint16_t event) {
943940 }
944941
945942 // Append this event, the caller will then append the arguments.
946- ringbuf_put16 (& o -> ringbuf , event );
943+ ringbuf_put (& o -> ringbuf , event );
947944 return true;
948945}
949946
@@ -959,7 +956,7 @@ STATIC void schedule_ringbuf(mp_uint_t atomic_state) {
959956 }
960957}
961958
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 ) {
963960 MICROPY_PY_BLUETOOTH_ENTER
964961 mp_obj_bluetooth_ble_t * o = MP_OBJ_TO_PTR (MP_STATE_VM (bluetooth ));
965962 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) {
986983void mp_bluetooth_gap_on_scan_complete (void ) {
987984 MICROPY_PY_BLUETOOTH_ENTER
988985 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 )) {
990987 }
991988 schedule_ringbuf (atomic_state );
992989}
@@ -1048,7 +1045,7 @@ void mp_bluetooth_gattc_on_descriptor_result(uint16_t conn_handle, uint16_t hand
10481045 schedule_ringbuf (atomic_state );
10491046}
10501047
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 ) {
10521049 MICROPY_PY_BLUETOOTH_ENTER
10531050 * atomic_state_out = atomic_state ;
10541051 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) {
10771074void mp_bluetooth_gattc_on_write_status (uint16_t conn_handle , uint16_t value_handle , uint16_t status ) {
10781075 MICROPY_PY_BLUETOOTH_ENTER
10791076 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 )) {
10811078 ringbuf_put16 (& o -> ringbuf , conn_handle );
10821079 ringbuf_put16 (& o -> ringbuf , value_handle );
10831080 ringbuf_put16 (& o -> ringbuf , status );
@@ -1092,7 +1089,7 @@ void mp_bluetooth_gattc_on_write_status(uint16_t conn_handle, uint16_t value_han
10921089// On ESP32, for example, this is not the case.
10931090bool mp_bluetooth_gatts_on_read_request (uint16_t conn_handle , uint16_t value_handle ) {
10941091 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 ) {
10961093 // Use pre-allocated tuple because this is a hard IRQ.
10971094 mp_obj_tuple_t * data = MP_OBJ_TO_PTR (o -> irq_data_tuple );
10981095 data -> items [0 ] = MP_OBJ_NEW_SMALL_INT (conn_handle );
0 commit comments