4444#include "py/runtime.h"
4545#include "supervisor/shared/translate.h"
4646#include "ble_drv.h"
47- #include "mpconfigport .h"
47+ #include "nrf_nvic .h"
4848#include "nrf_sdm.h"
4949#include "nrfx_power.h"
50- #include "ble_gap.h"
51- #include "ble_hci.h"
52- #include "ble.h" // sd_ble_uuid_encode
50+ #include "py/objstr.h"
51+ #include "py/runtime.h"
52+ #include "shared-bindings/bleio/Characteristic.h"
53+ #include "shared-bindings/bleio/ScanEntry.h"
54+ #include "shared-bindings/bleio/UUID.h"
5355
5456#define BLE_DRIVER_LOG printf
5557
6062#define MSEC_TO_UNITS (TIME , RESOLUTION ) (((TIME) * 1000) / (RESOLUTION))
6163#define UNIT_0_625_MS (625)
6264#define UNIT_10_MS (10000)
63- #define APP_CFG_NON_CONN_ADV_TIMEOUT 0 // Disable timeout.
64- #define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(100, UNIT_0_625_MS)
6565
6666#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
6767#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(300, UNIT_0_625_MS)
@@ -77,29 +77,24 @@ if (ble_drv_stack_enabled() == 0) { \
7777 (void)ble_drv_stack_enable(); \
7878}
7979
80- static volatile bool m_adv_in_progress ;
81- static volatile bool m_tx_in_progress ;
82-
83- static ble_drv_gap_evt_callback_t gap_event_handler ;
84- static ble_drv_gatts_evt_callback_t gatts_event_handler ;
80+ static ble_drv_adv_evt_callback_t adv_event_handler ;
81+ static ble_drv_gatts_evt_callback_t gatts_event_handler ;
82+ static ble_drv_gap_evt_callback_t gap_event_handler ;
8583
86- static bleio_device_obj_t * mp_gap_observer ;
84+ static bleio_characteristic_obj_t * mp_gattc_char_data_observer ;
85+ static bleio_device_obj_t * mp_gattc_disc_service_observer ;
86+ static bleio_service_obj_t * mp_gattc_disc_char_observer ;
87+ static bleio_address_obj_t * mp_connect_address ;
8788static bleio_device_obj_t * mp_gatts_observer ;
89+ static bleio_scanner_obj_t * mp_adv_observer ;
90+ static bleio_device_obj_t * mp_gap_observer ;
8891
8992static volatile bool m_primary_service_found ;
9093static volatile bool m_characteristic_found ;
94+ static volatile bool m_tx_in_progress ;
9195static volatile bool m_write_done ;
9296
93- static volatile ble_drv_adv_evt_callback_t adv_event_handler ;
94- static volatile ble_drv_gattc_evt_callback_t gattc_event_handler ;
95- static volatile ble_drv_gattc_char_data_callback_t gattc_char_data_handle ;
96-
97- static bleio_scanner_obj_t * mp_adv_observer ;
98- static bleio_device_obj_t * mp_gattc_observer ;
99- static bleio_device_obj_t * mp_gattc_disc_service_observer ;
100- static bleio_service_obj_t * mp_gattc_disc_char_observer ;
101- static bleio_characteristic_obj_t * mp_gattc_char_data_observer ;
102- static bleio_address_obj_t * mp_connect_address ;
97+ nrf_nvic_state_t nrf_nvic_state = { 0 };
10398
10499#if (BLUETOOTH_SD == 140 )
105100static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET ;
@@ -112,18 +107,11 @@ static ble_data_t m_scan_buffer =
112107};
113108#endif
114109
115- #include "nrf_nvic.h"
116-
117- nrf_nvic_state_t nrf_nvic_state = {0 };
118-
119110void softdevice_assert_handler (uint32_t id , uint32_t pc , uint32_t info ) {
120111 BLE_DRIVER_LOG ("ERROR: SoftDevice assert!!!" );
121112}
122113
123114uint32_t ble_drv_stack_enable (void ) {
124- m_adv_in_progress = false;
125- m_tx_in_progress = false;
126-
127115 nrf_clock_lf_cfg_t clock_config = {
128116 .source = NRF_CLOCK_LF_SRC_XTAL ,
129117 .rc_ctiv = 0 ,
@@ -173,10 +161,6 @@ uint32_t ble_drv_stack_enable(void) {
173161 return err_code ;
174162}
175163
176- void ble_drv_stack_disable (void ) {
177- sd_softdevice_disable ();
178- }
179-
180164uint8_t ble_drv_stack_enabled (void ) {
181165 uint8_t is_enabled ;
182166 uint32_t err_code = sd_softdevice_is_enabled (& is_enabled );
@@ -549,24 +533,25 @@ bool ble_drv_advertise_data(bleio_advertisement_data_t *adv_params) {
549533 translate ("Can not start advertisement. status: 0x%02x" ), (uint16_t )err_code ));
550534 }
551535
552- m_adv_in_progress = true;
553-
554536 return true;
555537}
556538
557539void ble_drv_advertise_stop (void ) {
558- if ( m_adv_in_progress == true) {
559- uint32_t err_code ;
540+ uint32_t err_code ;
541+
560542#if (BLUETOOTH_SD == 140 )
561- if ((err_code = sd_ble_gap_adv_stop (m_adv_handle )) != 0 ) {
543+ if (m_adv_handle == BLE_GAP_ADV_SET_HANDLE_NOT_SET )
544+ return ;
545+
546+ err_code = sd_ble_gap_adv_stop (m_adv_handle );
562547#else
563- if (( err_code = sd_ble_gap_adv_stop ()) != 0 ) {
548+ err_code = sd_ble_gap_adv_stop ();
564549#endif
565- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
566- translate ("Can not stop advertisement. status: 0x%02x" ), (uint16_t )err_code ));
567- }
550+
551+ if ((err_code != NRF_SUCCESS ) && (err_code != NRF_ERROR_INVALID_STATE )) {
552+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
553+ translate ("Can not stop advertisement. status: 0x%02x" ), (uint16_t )err_code ));
568554 }
569- m_adv_in_progress = false;
570555}
571556
572557void ble_drv_attr_s_read (uint16_t conn_handle , uint16_t handle , uint16_t len , uint8_t * p_data ) {
@@ -644,30 +629,24 @@ void ble_drv_gatts_event_handler_set(bleio_device_obj_t *device, ble_drv_gatts_e
644629 gatts_event_handler = evt_handler ;
645630}
646631
647- void ble_drv_gattc_event_handler_set (bleio_device_obj_t * device , ble_drv_gattc_evt_callback_t evt_handler ) {
648- mp_gattc_observer = device ;
649- gattc_event_handler = evt_handler ;
650- }
651-
652632void ble_drv_adv_report_handler_set (bleio_scanner_obj_t * device , ble_drv_adv_evt_callback_t evt_handler ) {
653633 mp_adv_observer = device ;
654634 adv_event_handler = evt_handler ;
655635}
656636
657- void ble_drv_attr_c_read (bleio_characteristic_obj_t * characteristic , ble_drv_gattc_char_data_callback_t cb ) {
637+ void ble_drv_attr_c_read (bleio_characteristic_obj_t * characteristic ) {
658638 bleio_service_obj_t * service = characteristic -> service ;
659639 bleio_device_obj_t * device = MP_OBJ_TO_PTR (service -> device );
660640
661641 mp_gattc_char_data_observer = characteristic ;
662- gattc_char_data_handle = cb ;
663642
664643 const uint32_t err_code = sd_ble_gattc_read (device -> conn_handle , characteristic -> handle , 0 );
665644 if (err_code != 0 ) {
666645 nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
667646 translate ("Can not read attribute value. status: 0x%02x" ), (uint16_t )err_code ));
668647 }
669648
670- while (gattc_char_data_handle != NULL ) {
649+ while (mp_gattc_char_data_observer != NULL ) {
671650#ifdef MICROPY_VM_HOOK_LOOP
672651 MICROPY_VM_HOOK_LOOP
673652#endif
@@ -939,13 +918,18 @@ STATIC void on_characteristic_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *respo
939918 mp_gattc_disc_char_observer = NULL ;
940919}
941920
921+ STATIC void on_read_rsp (ble_gattc_evt_read_rsp_t * response ) {
922+ mp_gattc_char_data_observer -> value_data = mp_obj_new_bytearray (response -> len , response -> data );
923+
924+ mp_gattc_char_data_observer = NULL ;
925+ }
926+
942927STATIC void ble_evt_handler (ble_evt_t * p_ble_evt ) {
943928 printf ("%s - 0x%02X\r\n" , __func__ , p_ble_evt -> header .evt_id );
944929
945930 switch (p_ble_evt -> header .evt_id ) {
946931 case BLE_GAP_EVT_CONNECTED :
947932 BLE_DRIVER_LOG ("GAP CONNECT\n" );
948- m_adv_in_progress = false;
949933 gap_event_handler (mp_gap_observer , p_ble_evt -> header .evt_id , p_ble_evt -> evt .gap_evt .conn_handle , p_ble_evt -> header .evt_len - (2 * sizeof (uint16_t )), NULL );
950934
951935 ble_gap_conn_params_t conn_params ;
@@ -1025,17 +1009,7 @@ STATIC void ble_evt_handler(ble_evt_t *p_ble_evt) {
10251009 break ;
10261010
10271011 case BLE_GATTC_EVT_READ_RSP :
1028- BLE_DRIVER_LOG ("BLE EVT READ RESPONSE, offset: 0x" HEX2_FMT ", length: 0x" HEX2_FMT "\n" ,
1029- p_ble_evt -> evt .gattc_evt .params .read_rsp .offset ,
1030- p_ble_evt -> evt .gattc_evt .params .read_rsp .len );
1031-
1032- gattc_char_data_handle (mp_gattc_char_data_observer ,
1033- p_ble_evt -> evt .gattc_evt .params .read_rsp .len ,
1034- p_ble_evt -> evt .gattc_evt .params .read_rsp .data );
1035-
1036- // mark end of read
1037- gattc_char_data_handle = NULL ;
1038-
1012+ on_read_rsp (& p_ble_evt -> evt .gattc_evt .params .read_rsp );
10391013 break ;
10401014
10411015 case BLE_GATTC_EVT_WRITE_RSP :
0 commit comments