@@ -98,7 +98,7 @@ static bleio_scanner_obj_t *mp_adv_observer;
98
98
static mp_obj_t mp_gattc_observer ;
99
99
static mp_obj_t mp_gattc_disc_service_observer ;
100
100
static mp_obj_t mp_gattc_disc_char_observer ;
101
- static mp_obj_t mp_gattc_char_data_observer ;
101
+ static bleio_characteristic_obj_t * mp_gattc_char_data_observer ;
102
102
103
103
#if (BLUETOOTH_SD == 140 )
104
104
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET ;
@@ -270,38 +270,26 @@ bool ble_drv_uuid_add_vs(uint8_t * p_uuid, uint8_t * idx) {
270
270
bool ble_drv_service_add (ubluepy_service_obj_t * p_service_obj ) {
271
271
SD_TEST_OR_ENABLE ();
272
272
273
- if (p_service_obj -> p_uuid -> type > BLE_UUID_TYPE_BLE ) {
273
+ ble_uuid_t uuid ;
274
+ uuid .type = BLE_UUID_TYPE_BLE ;
275
+ uuid .uuid = p_service_obj -> p_uuid -> value [0 ] | (p_service_obj -> p_uuid -> value [1 ] << 8 );
274
276
275
- ble_uuid_t uuid ;
277
+ if ( p_service_obj -> p_uuid -> type == UUID_TYPE_128BIT ) {
276
278
uuid .type = p_service_obj -> p_uuid -> uuid_vs_idx ;
277
- uuid .uuid = p_service_obj -> p_uuid -> value [0 ];
278
- uuid .uuid += p_service_obj -> p_uuid -> value [1 ] << 8 ;
279
-
280
- if (sd_ble_gatts_service_add (p_service_obj -> type ,
281
- & uuid ,
282
- & p_service_obj -> handle ) != 0 ) {
283
- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
284
- translate ("Can not add Service." )));
285
- }
286
- } else if (p_service_obj -> p_uuid -> type == BLE_UUID_TYPE_BLE ) {
287
- BLE_DRIVER_LOG ("adding service\n" );
288
-
289
- ble_uuid_t uuid ;
290
- uuid .type = p_service_obj -> p_uuid -> type ;
291
- uuid .uuid = p_service_obj -> p_uuid -> value [0 ];
292
- uuid .uuid += p_service_obj -> p_uuid -> value [1 ] << 8 ;
279
+ }
293
280
294
- if ( sd_ble_gatts_service_add (p_service_obj -> type ,
295
- & uuid ,
296
- & p_service_obj -> handle ) != 0 ) {
297
- nlr_raise ( mp_obj_new_exception_msg_varg ( & mp_type_OSError ,
298
- translate ( "Can not add Service." )));
299
- }
281
+ uint32_t err_code = sd_ble_gatts_service_add (p_service_obj -> type ,
282
+ & uuid ,
283
+ & p_service_obj -> handle );
284
+ if ( err_code != 0 ) {
285
+ nlr_raise ( mp_obj_new_exception_msg_varg ( & mp_type_OSError ,
286
+ translate ( "Can not add Service. status: 0x%08lX" ), err_code ));
300
287
}
288
+
301
289
return true;
302
290
}
303
291
304
- bool ble_drv_characteristic_add (ubluepy_characteristic_obj_t * p_char_obj ) {
292
+ bool ble_drv_characteristic_add (bleio_characteristic_obj_t * characteristic ) {
305
293
ble_gatts_char_md_t char_md ;
306
294
ble_gatts_attr_md_t cccd_md ;
307
295
ble_gatts_attr_t attr_char_value ;
@@ -310,24 +298,19 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
310
298
311
299
memset (& char_md , 0 , sizeof (char_md ));
312
300
313
- char_md .char_props .broadcast = (p_char_obj -> props & UBLUEPY_PROP_BROADCAST ) ? 1 : 0 ;
314
- char_md .char_props .read = (p_char_obj -> props & UBLUEPY_PROP_READ ) ? 1 : 0 ;
315
- char_md .char_props .write_wo_resp = (p_char_obj -> props & UBLUEPY_PROP_WRITE_WO_RESP ) ? 1 : 0 ;
316
- char_md .char_props .write = (p_char_obj -> props & UBLUEPY_PROP_WRITE ) ? 1 : 0 ;
317
- char_md .char_props .notify = (p_char_obj -> props & UBLUEPY_PROP_NOTIFY ) ? 1 : 0 ;
318
- char_md .char_props .indicate = (p_char_obj -> props & UBLUEPY_PROP_INDICATE ) ? 1 : 0 ;
319
- #if 0
320
- char_md .char_props .auth_signed_wr = (p_char_obj -> props & UBLUEPY_PROP_NOTIFY ) ? 1 : 0 ;
321
- #endif
322
-
301
+ char_md .char_props .broadcast = characteristic -> props .broadcast ;
302
+ char_md .char_props .read = characteristic -> props .read ;
303
+ char_md .char_props .write_wo_resp = characteristic -> props .write_wo_resp ;
304
+ char_md .char_props .write = characteristic -> props .write ;
305
+ char_md .char_props .notify = characteristic -> props .notify ;
306
+ char_md .char_props .indicate = characteristic -> props .indicate ;
323
307
324
308
char_md .p_char_user_desc = NULL ;
325
309
char_md .p_char_pf = NULL ;
326
310
char_md .p_user_desc_md = NULL ;
327
311
char_md .p_sccd_md = NULL ;
328
312
329
- // if cccd
330
- if (p_char_obj -> attrs & UBLUEPY_ATTR_CCCD ) {
313
+ if (characteristic -> props .notify || characteristic -> props .notify ) {
331
314
memset (& cccd_md , 0 , sizeof (cccd_md ));
332
315
BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cccd_md .read_perm );
333
316
BLE_GAP_CONN_SEC_MODE_SET_OPEN (& cccd_md .write_perm );
@@ -337,9 +320,12 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
337
320
char_md .p_cccd_md = NULL ;
338
321
}
339
322
340
- uuid .type = p_char_obj -> p_uuid -> type ;
341
- uuid .uuid = p_char_obj -> p_uuid -> value [0 ];
342
- uuid .uuid += p_char_obj -> p_uuid -> value [1 ] << 8 ;
323
+ uuid .type = BLE_UUID_TYPE_BLE ;
324
+ if (characteristic -> uuid -> type == UUID_TYPE_128BIT )
325
+ uuid .type = characteristic -> uuid -> uuid_vs_idx ;
326
+
327
+ uuid .uuid = characteristic -> uuid -> value [0 ];
328
+ uuid .uuid += characteristic -> uuid -> value [1 ] << 8 ;
343
329
344
330
memset (& attr_md , 0 , sizeof (attr_md ));
345
331
@@ -365,19 +351,20 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) {
365
351
366
352
ble_gatts_char_handles_t handles ;
367
353
368
- if (sd_ble_gatts_characteristic_add (p_char_obj -> service_handle ,
369
- & char_md ,
370
- & attr_char_value ,
371
- & handles ) != 0 ) {
354
+ uint32_t err_code = sd_ble_gatts_characteristic_add (characteristic -> service_handle ,
355
+ & char_md ,
356
+ & attr_char_value ,
357
+ & handles );
358
+ if (err_code != 0 ) {
372
359
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
373
- translate ("Can not add Characteristic." ) ));
360
+ translate ("Can not add Characteristic. status: 0x%08lX" ), err_code ));
374
361
}
375
362
376
363
// apply handles to object instance
377
- p_char_obj -> handle = handles .value_handle ;
378
- p_char_obj -> user_desc_handle = handles .user_desc_handle ;
379
- p_char_obj -> cccd_handle = handles .cccd_handle ;
380
- p_char_obj -> sccd_handle = handles .sccd_handle ;
364
+ characteristic -> handle = handles .value_handle ;
365
+ characteristic -> user_desc_handle = handles .user_desc_handle ;
366
+ characteristic -> cccd_handle = handles .cccd_handle ;
367
+ characteristic -> sccd_handle = handles .sccd_handle ;
381
368
382
369
return true;
383
370
}
@@ -652,33 +639,38 @@ void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui
652
639
653
640
}
654
641
655
- void ble_drv_attr_s_write (uint16_t conn_handle , uint16_t handle , uint16_t len , uint8_t * p_data ) {
642
+ void ble_drv_attr_s_write (bleio_characteristic_obj_t * characteristic , mp_buffer_info_t * bufinfo ) {
643
+ ubluepy_service_obj_t * service = MP_OBJ_TO_PTR (characteristic -> service );
644
+ uint16_t conn_handle = service -> p_periph -> conn_handle ;
656
645
ble_gatts_value_t gatts_value ;
646
+
657
647
memset (& gatts_value , 0 , sizeof (gatts_value ));
658
648
659
- gatts_value .len = len ;
649
+ gatts_value .len = bufinfo -> len ;
660
650
gatts_value .offset = 0 ;
661
- gatts_value .p_value = p_data ;
651
+ gatts_value .p_value = bufinfo -> buf ;
662
652
663
- uint32_t err_code = sd_ble_gatts_value_set (conn_handle , handle , & gatts_value );
653
+ uint32_t err_code = sd_ble_gatts_value_set (conn_handle , characteristic -> handle , & gatts_value );
664
654
665
655
if (err_code != 0 ) {
666
656
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
667
657
translate ("Can not write attribute value. status: 0x%02x" ), (uint16_t )err_code ));
668
658
}
669
659
}
670
660
671
- void ble_drv_attr_s_notify (uint16_t conn_handle , uint16_t handle , uint16_t len , uint8_t * p_data ) {
672
- uint16_t hvx_len = len ;
661
+ void ble_drv_attr_s_notify (bleio_characteristic_obj_t * characteristic , mp_buffer_info_t * bufinfo ) {
662
+ ubluepy_service_obj_t * service = MP_OBJ_TO_PTR (characteristic -> service );
663
+ uint16_t conn_handle = service -> p_periph -> conn_handle ;
673
664
ble_gatts_hvx_params_t hvx_params ;
665
+ uint16_t hvx_len = bufinfo -> len ;
674
666
675
667
memset (& hvx_params , 0 , sizeof (hvx_params ));
676
668
677
- hvx_params .handle = handle ;
669
+ hvx_params .handle = characteristic -> handle ;
678
670
hvx_params .type = BLE_GATT_HVX_NOTIFICATION ;
679
671
hvx_params .offset = 0 ;
680
672
hvx_params .p_len = & hvx_len ;
681
- hvx_params .p_data = p_data ;
673
+ hvx_params .p_data = bufinfo -> buf ;
682
674
683
675
while (m_tx_in_progress ) {
684
676
;
@@ -713,13 +705,13 @@ void ble_drv_adv_report_handler_set(bleio_scanner_obj_t *self, ble_drv_adv_evt_c
713
705
}
714
706
715
707
716
- void ble_drv_attr_c_read (uint16_t conn_handle , uint16_t handle , mp_obj_t obj , ble_drv_gattc_char_data_callback_t cb ) {
717
-
718
- mp_gattc_char_data_observer = obj ;
708
+ void ble_drv_attr_c_read (bleio_characteristic_obj_t * characteristic , ble_drv_gattc_char_data_callback_t cb ) {
709
+ ubluepy_service_obj_t * service = MP_OBJ_TO_PTR ( characteristic -> service );
710
+ mp_gattc_char_data_observer = characteristic ;
719
711
gattc_char_data_handle = cb ;
720
712
721
- uint32_t err_code = sd_ble_gattc_read (conn_handle ,
722
- handle ,
713
+ const uint32_t err_code = sd_ble_gattc_read (service -> p_periph -> conn_handle ,
714
+ characteristic -> handle ,
723
715
0 );
724
716
if (err_code != 0 ) {
725
717
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
@@ -731,26 +723,26 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, bl
731
723
}
732
724
}
733
725
734
- void ble_drv_attr_c_write (uint16_t conn_handle , uint16_t handle , uint16_t len , uint8_t * p_data , bool w_response ) {
726
+ void ble_drv_attr_c_write (bleio_characteristic_obj_t * characteristic , mp_buffer_info_t * bufinfo ) {
727
+ ubluepy_service_obj_t * service = MP_OBJ_TO_PTR (characteristic -> service );
728
+ uint16_t conn_handle = service -> p_periph -> conn_handle ;
735
729
736
730
ble_gattc_write_params_t write_params ;
731
+ write_params .write_op = BLE_GATT_OP_WRITE_REQ ;
737
732
738
- if (w_response ) {
739
- write_params .write_op = BLE_GATT_OP_WRITE_REQ ;
740
- } else {
733
+ if (characteristic -> props .write_wo_resp ) {
741
734
write_params .write_op = BLE_GATT_OP_WRITE_CMD ;
742
735
}
743
736
744
737
write_params .flags = BLE_GATT_EXEC_WRITE_FLAG_PREPARED_CANCEL ;
745
- write_params .handle = handle ;
738
+ write_params .handle = characteristic -> handle ;
746
739
write_params .offset = 0 ;
747
- write_params .len = len ;
748
- write_params .p_value = p_data ;
740
+ write_params .len = bufinfo -> len ;
741
+ write_params .p_value = bufinfo -> buf ;
749
742
750
- m_write_done = ! w_response ;
743
+ m_write_done = ( write_params . write_op == BLE_GATT_OP_WRITE_CMD ) ;
751
744
752
745
uint32_t err_code = sd_ble_gattc_write (conn_handle , & write_params );
753
-
754
746
if (err_code != 0 ) {
755
747
nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_OSError ,
756
748
translate ("Can not write attribute value. status: 0x%02x" ), (uint16_t )err_code ));
@@ -1053,15 +1045,12 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) {
1053
1045
char_data .decl_handle = p_char -> handle_decl ;
1054
1046
char_data .value_handle = p_char -> handle_value ;
1055
1047
1056
- char_data .props |= (p_char -> char_props .broadcast ) ? UBLUEPY_PROP_BROADCAST : 0 ;
1057
- char_data .props |= (p_char -> char_props .read ) ? UBLUEPY_PROP_READ : 0 ;
1058
- char_data .props |= (p_char -> char_props .write_wo_resp ) ? UBLUEPY_PROP_WRITE_WO_RESP : 0 ;
1059
- char_data .props |= (p_char -> char_props .write ) ? UBLUEPY_PROP_WRITE : 0 ;
1060
- char_data .props |= (p_char -> char_props .notify ) ? UBLUEPY_PROP_NOTIFY : 0 ;
1061
- char_data .props |= (p_char -> char_props .indicate ) ? UBLUEPY_PROP_INDICATE : 0 ;
1062
- #if 0
1063
- char_data .props |= (p_char -> char_props .auth_signed_wr ) ? UBLUEPY_PROP_NOTIFY : 0 ;
1064
- #endif
1048
+ char_data .props .broadcast = p_char -> char_props .broadcast ;
1049
+ char_data .props .read = p_char -> char_props .read ;
1050
+ char_data .props .write_wo_resp = p_char -> char_props .write_wo_resp ;
1051
+ char_data .props .write = p_char -> char_props .write ;
1052
+ char_data .props .notify = p_char -> char_props .notify ;
1053
+ char_data .props .indicate = p_char -> char_props .indicate ;
1065
1054
1066
1055
disc_add_char_handler (mp_gattc_disc_char_observer , & char_data );
1067
1056
}
0 commit comments