44
44
#include "py/runtime.h"
45
45
#include "supervisor/shared/translate.h"
46
46
#include "ble_drv.h"
47
- #include "mpconfigport .h"
47
+ #include "nrf_nvic .h"
48
48
#include "nrf_sdm.h"
49
49
#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"
53
55
54
56
#define BLE_DRIVER_LOG printf
55
57
60
62
#define MSEC_TO_UNITS (TIME , RESOLUTION ) (((TIME) * 1000) / (RESOLUTION))
61
63
#define UNIT_0_625_MS (625)
62
64
#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)
65
65
66
66
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
67
67
#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(300, UNIT_0_625_MS)
@@ -77,29 +77,24 @@ if (ble_drv_stack_enabled() == 0) { \
77
77
(void)ble_drv_stack_enable(); \
78
78
}
79
79
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 ;
85
83
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 ;
87
88
static 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 ;
88
91
89
92
static volatile bool m_primary_service_found ;
90
93
static volatile bool m_characteristic_found ;
94
+ static volatile bool m_tx_in_progress ;
91
95
static volatile bool m_write_done ;
92
96
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 };
103
98
104
99
#if (BLUETOOTH_SD == 140 )
105
100
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET ;
@@ -112,18 +107,11 @@ static ble_data_t m_scan_buffer =
112
107
};
113
108
#endif
114
109
115
- #include "nrf_nvic.h"
116
-
117
- nrf_nvic_state_t nrf_nvic_state = {0 };
118
-
119
110
void softdevice_assert_handler (uint32_t id , uint32_t pc , uint32_t info ) {
120
111
BLE_DRIVER_LOG ("ERROR: SoftDevice assert!!!" );
121
112
}
122
113
123
114
uint32_t ble_drv_stack_enable (void ) {
124
- m_adv_in_progress = false;
125
- m_tx_in_progress = false;
126
-
127
115
nrf_clock_lf_cfg_t clock_config = {
128
116
.source = NRF_CLOCK_LF_SRC_XTAL ,
129
117
.rc_ctiv = 0 ,
@@ -173,10 +161,6 @@ uint32_t ble_drv_stack_enable(void) {
173
161
return err_code ;
174
162
}
175
163
176
- void ble_drv_stack_disable (void ) {
177
- sd_softdevice_disable ();
178
- }
179
-
180
164
uint8_t ble_drv_stack_enabled (void ) {
181
165
uint8_t is_enabled ;
182
166
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) {
549
533
translate ("Can not start advertisement. status: 0x%02x" ), (uint16_t )err_code ));
550
534
}
551
535
552
- m_adv_in_progress = true;
553
-
554
536
return true;
555
537
}
556
538
557
539
void ble_drv_advertise_stop (void ) {
558
- if ( m_adv_in_progress == true) {
559
- uint32_t err_code ;
540
+ uint32_t err_code ;
541
+
560
542
#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 );
562
547
#else
563
- if (( err_code = sd_ble_gap_adv_stop ()) != 0 ) {
548
+ err_code = sd_ble_gap_adv_stop ();
564
549
#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 ));
568
554
}
569
- m_adv_in_progress = false;
570
555
}
571
556
572
557
void 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
644
629
gatts_event_handler = evt_handler ;
645
630
}
646
631
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
-
652
632
void ble_drv_adv_report_handler_set (bleio_scanner_obj_t * device , ble_drv_adv_evt_callback_t evt_handler ) {
653
633
mp_adv_observer = device ;
654
634
adv_event_handler = evt_handler ;
655
635
}
656
636
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 ) {
658
638
bleio_service_obj_t * service = characteristic -> service ;
659
639
bleio_device_obj_t * device = MP_OBJ_TO_PTR (service -> device );
660
640
661
641
mp_gattc_char_data_observer = characteristic ;
662
- gattc_char_data_handle = cb ;
663
642
664
643
const uint32_t err_code = sd_ble_gattc_read (device -> conn_handle , characteristic -> handle , 0 );
665
644
if (err_code != 0 ) {
666
645
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
667
646
translate ("Can not read attribute value. status: 0x%02x" ), (uint16_t )err_code ));
668
647
}
669
648
670
- while (gattc_char_data_handle != NULL ) {
649
+ while (mp_gattc_char_data_observer != NULL ) {
671
650
#ifdef MICROPY_VM_HOOK_LOOP
672
651
MICROPY_VM_HOOK_LOOP
673
652
#endif
@@ -939,13 +918,18 @@ STATIC void on_characteristic_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *respo
939
918
mp_gattc_disc_char_observer = NULL ;
940
919
}
941
920
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
+
942
927
STATIC void ble_evt_handler (ble_evt_t * p_ble_evt ) {
943
928
printf ("%s - 0x%02X\r\n" , __func__ , p_ble_evt -> header .evt_id );
944
929
945
930
switch (p_ble_evt -> header .evt_id ) {
946
931
case BLE_GAP_EVT_CONNECTED :
947
932
BLE_DRIVER_LOG ("GAP CONNECT\n" );
948
- m_adv_in_progress = false;
949
933
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 );
950
934
951
935
ble_gap_conn_params_t conn_params ;
@@ -1025,17 +1009,7 @@ STATIC void ble_evt_handler(ble_evt_t *p_ble_evt) {
1025
1009
break ;
1026
1010
1027
1011
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 );
1039
1013
break ;
1040
1014
1041
1015
case BLE_GATTC_EVT_WRITE_RSP :
0 commit comments