2222
2323static int characteristic_on_ble_gap_evt (struct ble_gap_event * event , void * param );
2424
25- static volatile int _completion_status ;
26- static uint64_t _timeout_start_time ;
27-
28- static void _reset_completion_status (void ) {
29- _completion_status = 0 ;
30- }
31-
32- // Wait for a status change, recorded in a callback.
33- // Try twice because sometimes we get a BLE_HS_EAGAIN.
34- // Maybe we should try more than twice.
35- static int _wait_for_completion (uint32_t timeout_msecs ) {
36- for (int tries = 1 ; tries <= 2 ; tries ++ ) {
37- _timeout_start_time = common_hal_time_monotonic_ms ();
38- while ((_completion_status == 0 ) &&
39- (common_hal_time_monotonic_ms () < _timeout_start_time + timeout_msecs ) &&
40- !mp_hal_is_interrupted ()) {
41- RUN_BACKGROUND_TASKS ;
42- }
43- if (_completion_status != BLE_HS_EAGAIN ) {
44- // Quit, because either the status is either zero (OK) or it's an error.
45- break ;
46- }
47- }
48- return _completion_status ;
49- }
50-
5125void common_hal_bleio_characteristic_construct (bleio_characteristic_obj_t * self , bleio_service_obj_t * service ,
5226 uint16_t handle , bleio_uuid_obj_t * uuid , bleio_characteristic_properties_t props ,
5327 bleio_attribute_security_mode_t read_perm , bleio_attribute_security_mode_t write_perm ,
@@ -151,35 +125,7 @@ bleio_service_obj_t *common_hal_bleio_characteristic_get_service(bleio_character
151125 return self -> service ;
152126}
153127
154- typedef struct {
155- uint8_t * buf ;
156- uint16_t len ;
157- } _read_info_t ;
158-
159- static int _read_cb (uint16_t conn_handle ,
160- const struct ble_gatt_error * error ,
161- struct ble_gatt_attr * attr ,
162- void * arg ) {
163- _read_info_t * read_info = (_read_info_t * )arg ;
164- switch (error -> status ) {
165- case 0 : {
166- int len = MIN (read_info -> len , OS_MBUF_PKTLEN (attr -> om ));
167- os_mbuf_copydata (attr -> om , attr -> offset , len , read_info -> buf );
168- read_info -> len = len ;
169- }
170- MP_FALLTHROUGH ;
171-
172- default :
173- #if CIRCUITPY_VERBOSE_BLE
174- // For debugging.
175- mp_printf (& mp_plat_print , "Read status: %d\n" , error -> status );
176- #endif
177- break ;
178- }
179- _completion_status = error -> status ;
180128
181- return 0 ;
182- }
183129
184130size_t common_hal_bleio_characteristic_get_value (bleio_characteristic_obj_t * self , uint8_t * buf , size_t len ) {
185131 // Do GATT operations only if this characteristic has been added to a registered service.
@@ -188,14 +134,7 @@ size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *sel
188134 }
189135 uint16_t conn_handle = bleio_connection_get_conn_handle (self -> service -> connection );
190136 if (common_hal_bleio_service_get_is_remote (self -> service )) {
191- _read_info_t read_info = {
192- .buf = buf ,
193- .len = len
194- };
195- _reset_completion_status ();
196- CHECK_NIMBLE_ERROR (ble_gattc_read (conn_handle , self -> handle , _read_cb , & read_info ));
197- CHECK_NIMBLE_ERROR (_wait_for_completion (2000 ));
198- return read_info .len ;
137+ return bleio_gattc_read (conn_handle , self -> handle , buf , len );
199138 } else {
200139 len = MIN (self -> current_value_len , len );
201140 memcpy (buf , self -> current_value , len );
@@ -209,24 +148,13 @@ size_t common_hal_bleio_characteristic_get_max_length(bleio_characteristic_obj_t
209148 return self -> max_length ;
210149}
211150
212- static int _write_cb (uint16_t conn_handle ,
213- const struct ble_gatt_error * error ,
214- struct ble_gatt_attr * attr ,
215- void * arg ) {
216- _completion_status = error -> status ;
217-
218- return 0 ;
219- }
220-
221151void common_hal_bleio_characteristic_set_value (bleio_characteristic_obj_t * self , mp_buffer_info_t * bufinfo ) {
222152 if (common_hal_bleio_service_get_is_remote (self -> service )) {
223153 uint16_t conn_handle = bleio_connection_get_conn_handle (self -> service -> connection );
224154 if ((self -> props & CHAR_PROP_WRITE_NO_RESPONSE ) != 0 ) {
225155 CHECK_NIMBLE_ERROR (ble_gattc_write_no_rsp_flat (conn_handle , self -> handle , bufinfo -> buf , bufinfo -> len ));
226156 } else {
227- _reset_completion_status ();
228- CHECK_NIMBLE_ERROR (ble_gattc_write_flat (conn_handle , self -> handle , bufinfo -> buf , bufinfo -> len , _write_cb , NULL ));
229- CHECK_NIMBLE_ERROR (_wait_for_completion (2000 ));
157+ bleio_gattc_write (conn_handle , self -> handle , bufinfo -> buf , bufinfo -> len );
230158 }
231159 } else {
232160 // Validate data length for local characteristics only.
@@ -394,9 +322,7 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
394322 (notify ? 1 << 0 : 0 ) |
395323 (indicate ? 1 << 1 : 0 );
396324
397- _reset_completion_status ();
398- CHECK_NIMBLE_ERROR (ble_gattc_write_flat (conn_handle , self -> cccd_handle , & cccd_value , 2 , _write_cb , NULL ));
399- CHECK_NIMBLE_ERROR (_wait_for_completion (2000 ));
325+ bleio_gattc_write (conn_handle , self -> cccd_handle , (uint8_t * )& cccd_value , 2 );
400326}
401327
402328void bleio_characteristic_set_observer (bleio_characteristic_obj_t * self , mp_obj_t observer ) {
0 commit comments