2929#include "ble.h"
3030#include "py/runtime.h"
3131#include "common-hal/bleio/__init__.h"
32- #include "common-hal/bleio/Characteristic.h"
3332#include "shared-bindings/bleio/Characteristic.h"
33+ #include "shared-bindings/bleio/Descriptor.h"
3434#include "shared-bindings/bleio/Service.h"
3535#include "shared-bindings/bleio/Adapter.h"
3636
@@ -74,12 +74,12 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
7474 MP_OBJ_TO_PTR (self -> characteristic_list -> items [characteristic_idx ]);
7575
7676 ble_gatts_char_md_t char_md = {
77- .char_props .broadcast = characteristic -> props . broadcast ,
78- .char_props .read = characteristic -> props . read ,
79- .char_props .write_wo_resp = characteristic -> props . write_no_response ,
80- .char_props .write = characteristic -> props . write ,
81- .char_props .notify = characteristic -> props . notify ,
82- .char_props .indicate = characteristic -> props . indicate ,
77+ .char_props .broadcast = ( bool ) characteristic -> props & CHAR_PROP_BROADCAST ,
78+ .char_props .read = ( bool ) characteristic -> props & CHAR_PROP_READ ,
79+ .char_props .write_wo_resp = ( bool ) characteristic -> props & CHAR_PROP_WRITE_NO_RESPONSE ,
80+ .char_props .write = ( bool ) characteristic -> props & CHAR_PROP_WRITE ,
81+ .char_props .notify = ( bool ) characteristic -> props & CHAR_PROP_NOTIFY ,
82+ .char_props .indicate = ( bool ) characteristic -> props & CHAR_PROP_INDICATE ,
8383 };
8484
8585 ble_gatts_attr_md_t cccd_md = {
@@ -93,28 +93,28 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
9393 char_md .p_cccd_md = & cccd_md ;
9494 }
9595
96- ble_uuid_t uuid ;
97- bleio_uuid_convert_to_nrf_ble_uuid (characteristic -> uuid , & uuid );
96+ ble_uuid_t char_uuid ;
97+ bleio_uuid_convert_to_nrf_ble_uuid (characteristic -> uuid , & char_uuid );
9898
99- ble_gatts_attr_md_t attr_md = {
99+ ble_gatts_attr_md_t char_attr_md = {
100100 .vloc = BLE_GATTS_VLOC_STACK ,
101101 .vlen = 1 ,
102102 };
103103
104- BLE_GAP_CONN_SEC_MODE_SET_OPEN (& attr_md .read_perm );
105- BLE_GAP_CONN_SEC_MODE_SET_OPEN (& attr_md .write_perm );
104+ BLE_GAP_CONN_SEC_MODE_SET_OPEN (& char_attr_md .read_perm );
105+ BLE_GAP_CONN_SEC_MODE_SET_OPEN (& char_attr_md .write_perm );
106106
107- ble_gatts_attr_t attr_char_value = {
108- .p_uuid = & uuid ,
109- .p_attr_md = & attr_md ,
107+ ble_gatts_attr_t char_attr = {
108+ .p_uuid = & char_uuid ,
109+ .p_attr_md = & char_attr_md ,
110110 .init_len = sizeof (uint8_t ),
111111 .max_len = GATT_MAX_DATA_LENGTH ,
112112 };
113113
114- ble_gatts_char_handles_t handles ;
114+ ble_gatts_char_handles_t char_handles ;
115115
116116 uint32_t err_code ;
117- err_code = sd_ble_gatts_characteristic_add (self -> handle , & char_md , & attr_char_value , & handles );
117+ err_code = sd_ble_gatts_characteristic_add (self -> handle , & char_md , & char_attr , & char_handles );
118118 if (err_code != NRF_SUCCESS ) {
119119 mp_raise_OSError_msg_varg (translate ("Failed to add characteristic, err 0x%04x" ), err_code );
120120 }
@@ -123,9 +123,41 @@ void common_hal_bleio_service_add_all_characteristics(bleio_service_obj_t *self)
123123 mp_raise_ValueError (translate ("Characteristic already in use by another Service." ));
124124 }
125125
126- characteristic -> user_desc_handle = handles .user_desc_handle ;
127- characteristic -> cccd_handle = handles .cccd_handle ;
128- characteristic -> sccd_handle = handles .sccd_handle ;
129- characteristic -> handle = handles .value_handle ;
126+ characteristic -> user_desc_handle = char_handles .user_desc_handle ;
127+ characteristic -> cccd_handle = char_handles .cccd_handle ;
128+ characteristic -> sccd_handle = char_handles .sccd_handle ;
129+ characteristic -> handle = char_handles .value_handle ;
130+
131+ // Add the descriptors for this characteristic.
132+ for (size_t descriptor_idx = 0 ; descriptor_idx < characteristic -> descriptor_list -> len ; ++ descriptor_idx ) {
133+ bleio_descriptor_obj_t * descriptor =
134+ MP_OBJ_TO_PTR (characteristic -> descriptor_list -> items [descriptor_idx ]);
135+
136+ ble_uuid_t desc_uuid ;
137+ bleio_uuid_convert_to_nrf_ble_uuid (descriptor -> uuid , & desc_uuid );
138+
139+ ble_gatts_attr_md_t desc_attr_md = {
140+ // Data passed is not in a permanent location and should be copied.
141+ .vloc = BLE_GATTS_VLOC_STACK ,
142+ .vlen = 1 ,
143+ };
144+
145+ BLE_GAP_CONN_SEC_MODE_SET_OPEN (& desc_attr_md .read_perm );
146+ BLE_GAP_CONN_SEC_MODE_SET_OPEN (& desc_attr_md .write_perm );
147+
148+ mp_buffer_info_t bufinfo ;
149+ mp_get_buffer_raise (descriptor -> value_data , & bufinfo , MP_BUFFER_READ );
150+
151+ ble_gatts_attr_t desc_attr = {
152+ .p_uuid = & desc_uuid ,
153+ .p_attr_md = & desc_attr_md ,
154+ .init_len = bufinfo .len ,
155+ .p_value = bufinfo .buf ,
156+ .init_offs = 0 ,
157+ .max_len = GATT_MAX_DATA_LENGTH ,
158+ };
159+
160+ err_code = sd_ble_gatts_descriptor_add (characteristic -> handle , & desc_attr , & descriptor -> handle );
161+ }
130162 }
131163}
0 commit comments