35
35
#define NRF52 // Needed for SD132 v2
36
36
#endif
37
37
38
+ #include "shared-bindings/bleio/Characteristic.h"
38
39
#include "shared-bindings/bleio/ScanEntry.h"
39
40
#include "shared-bindings/bleio/Service.h"
40
41
#include "shared-bindings/bleio/UUID.h"
@@ -91,7 +92,6 @@ static volatile bool m_write_done;
91
92
92
93
static volatile ble_drv_adv_evt_callback_t adv_event_handler ;
93
94
static volatile ble_drv_gattc_evt_callback_t gattc_event_handler ;
94
- static volatile ble_drv_disc_add_char_callback_t disc_add_char_handler ;
95
95
static volatile ble_drv_gattc_char_data_callback_t gattc_char_data_handle ;
96
96
97
97
static bleio_scanner_obj_t * mp_adv_observer ;
@@ -820,11 +820,10 @@ bool ble_drv_discover_services(bleio_device_obj_t *device, uint16_t start_handle
820
820
return m_primary_service_found ;
821
821
}
822
822
823
- bool ble_drv_discover_characteristic (bleio_device_obj_t * device , bleio_service_obj_t * service , uint16_t start_handle , ble_drv_disc_add_char_callback_t cb ) {
823
+ bool ble_drv_discover_characteristic (bleio_device_obj_t * device , bleio_service_obj_t * service , uint16_t start_handle ) {
824
824
BLE_DRIVER_LOG ("Discover characteristicts. Conn handle: 0x" HEX2_FMT "\n" , device -> conn_handle );
825
825
826
826
mp_gattc_disc_char_observer = service ;
827
- disc_add_char_handler = cb ;
828
827
829
828
ble_gattc_handle_range_t handle_range ;
830
829
handle_range .start_handle = start_handle ;
@@ -837,7 +836,7 @@ bool ble_drv_discover_characteristic(bleio_device_obj_t *device, bleio_service_o
837
836
return false;
838
837
}
839
838
840
- while (disc_add_char_handler != NULL ) {
839
+ while (mp_gattc_disc_char_observer != NULL ) {
841
840
#ifdef MICROPY_VM_HOOK_LOOP
842
841
MICROPY_VM_HOOK_LOOP
843
842
#endif
@@ -902,6 +901,44 @@ STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *res
902
901
mp_gattc_disc_service_observer = NULL ;
903
902
}
904
903
904
+ STATIC void on_characteristic_discovery_rsp (ble_gattc_evt_char_disc_rsp_t * response ) {
905
+ BLE_DRIVER_LOG (">>> characteristic count: %d\n" , response -> count );
906
+
907
+ for (size_t i = 0 ; i < response -> count ; ++ i ) {
908
+ const ble_gattc_char_t * gattc_char = & response -> chars [i ];
909
+
910
+ bleio_characteristic_obj_t * characteristic = m_new_obj (bleio_characteristic_obj_t );
911
+ characteristic -> base .type = & bleio_characteristic_type ;
912
+
913
+ bleio_uuid_obj_t * uuid = m_new_obj (bleio_uuid_obj_t );
914
+ uuid -> base .type = & bleio_uuid_type ;
915
+ uuid -> type = (gattc_char -> uuid .type == BLE_UUID_TYPE_BLE ) ? UUID_TYPE_16BIT : UUID_TYPE_128BIT ;
916
+ uuid -> value [0 ] = gattc_char -> uuid .uuid & 0xFF ;
917
+ uuid -> value [1 ] = gattc_char -> uuid .uuid >> 8 ;
918
+ characteristic -> uuid = uuid ;
919
+
920
+ characteristic -> props .broadcast = gattc_char -> char_props .broadcast ;
921
+ characteristic -> props .indicate = gattc_char -> char_props .indicate ;
922
+ characteristic -> props .notify = gattc_char -> char_props .notify ;
923
+ characteristic -> props .read = gattc_char -> char_props .read ;
924
+ characteristic -> props .write = gattc_char -> char_props .write ;
925
+ characteristic -> props .write_wo_resp = gattc_char -> char_props .write_wo_resp ;
926
+ characteristic -> handle = gattc_char -> handle_value ;
927
+
928
+ characteristic -> service_handle = mp_gattc_disc_char_observer -> handle ;
929
+ characteristic -> service = mp_gattc_disc_char_observer ;
930
+
931
+ mp_obj_list_append (mp_gattc_disc_char_observer -> char_list , MP_OBJ_FROM_PTR (characteristic ));
932
+ }
933
+
934
+ if (response -> count > 0 ) {
935
+ m_characteristic_found = true;
936
+ }
937
+
938
+ // mark end of characteristic discovery
939
+ mp_gattc_disc_char_observer = NULL ;
940
+ }
941
+
905
942
STATIC void ble_evt_handler (ble_evt_t * p_ble_evt ) {
906
943
printf ("%s - 0x%02X\r\n" , __func__ , p_ble_evt -> header .evt_id );
907
944
@@ -984,35 +1021,7 @@ STATIC void ble_evt_handler(ble_evt_t *p_ble_evt) {
984
1021
break ;
985
1022
986
1023
case BLE_GATTC_EVT_CHAR_DISC_RSP :
987
- BLE_DRIVER_LOG ("BLE EVT CHAR DISCOVERY RESPONSE\n" );
988
- BLE_DRIVER_LOG (">>> characteristic count: %d\n" , p_ble_evt -> evt .gattc_evt .params .char_disc_rsp .count );
989
-
990
- for (uint16_t i = 0 ; i < p_ble_evt -> evt .gattc_evt .params .char_disc_rsp .count ; i ++ ) {
991
- ble_gattc_char_t * p_char = & p_ble_evt -> evt .gattc_evt .params .char_disc_rsp .chars [i ];
992
-
993
- ble_drv_char_data_t char_data ;
994
- char_data .uuid_type = p_char -> uuid .type ;
995
- char_data .uuid = p_char -> uuid .uuid ;
996
- char_data .decl_handle = p_char -> handle_decl ;
997
- char_data .value_handle = p_char -> handle_value ;
998
-
999
- char_data .props .broadcast = p_char -> char_props .broadcast ;
1000
- char_data .props .read = p_char -> char_props .read ;
1001
- char_data .props .write_wo_resp = p_char -> char_props .write_wo_resp ;
1002
- char_data .props .write = p_char -> char_props .write ;
1003
- char_data .props .notify = p_char -> char_props .notify ;
1004
- char_data .props .indicate = p_char -> char_props .indicate ;
1005
-
1006
- disc_add_char_handler (mp_gattc_disc_char_observer , & char_data );
1007
- }
1008
-
1009
- if (p_ble_evt -> evt .gattc_evt .params .char_disc_rsp .count > 0 ) {
1010
- m_characteristic_found = true;
1011
- }
1012
-
1013
- // mark end of characteristic discovery
1014
- disc_add_char_handler = NULL ;
1015
-
1024
+ on_characteristic_discovery_rsp (& p_ble_evt -> evt .gattc_evt .params .char_disc_rsp );
1016
1025
break ;
1017
1026
1018
1027
case BLE_GATTC_EVT_READ_RSP :
0 commit comments