3333#include "shared-bindings/_bleio/Service.h"
3434
3535#include "common-hal/_bleio/Adapter.h"
36- #include "common-hal/_bleio/bonding.h"
3736
38- STATIC uint16_t characteristic_get_cccd (uint16_t cccd_handle , uint16_t conn_handle ) {
39- uint16_t cccd ;
40- ble_gatts_value_t value = {
41- .p_value = (uint8_t * ) & cccd ,
42- .len = 2 ,
43- };
44-
45- const uint32_t err_code = sd_ble_gatts_value_get (conn_handle , cccd_handle , & value );
46-
47- if (err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING ) {
48- // CCCD is not set, so say that neither Notify nor Indicate is enabled.
49- cccd = 0 ;
50- } else {
51- check_nrf_error (err_code );
52- }
53-
54- return cccd ;
55- }
56-
57-
58- STATIC void characteristic_gatts_notify_indicate (uint16_t handle , uint16_t conn_handle , mp_buffer_info_t * bufinfo , uint16_t hvx_type ) {
59- uint16_t hvx_len = bufinfo -> len ;
60-
61- ble_gatts_hvx_params_t hvx_params = {
62- .handle = handle ,
63- .type = hvx_type ,
64- .offset = 0 ,
65- .p_len = & hvx_len ,
66- .p_data = bufinfo -> buf ,
67- };
68-
69- while (1 ) {
70- const uint32_t err_code = sd_ble_gatts_hvx (conn_handle , & hvx_params );
71- if (err_code == NRF_SUCCESS ) {
72- break ;
73- }
74- // TX buffer is full
75- // We could wait for an event indicating the write is complete, but just retrying is easier.
76- if (err_code == NRF_ERROR_RESOURCES ) {
77- RUN_BACKGROUND_TASKS ;
78- continue ;
79- }
80-
81- // Some real error has occurred.
82- check_nrf_error (err_code );
83- }
84- }
37+ // STATIC uint16_t characteristic_get_cccd(uint16_t cccd_handle, uint16_t conn_handle) {
38+ // uint16_t cccd;
39+ // // ble_gatts_value_t value = {
40+ // // .p_value = (uint8_t*) &cccd,
41+ // // .len = 2,
42+ // // };
43+
44+ // // const uint32_t err_code = sd_ble_gatts_value_get(conn_handle, cccd_handle, &value);
45+
46+ // // if (err_code == BLE_ERROR_GATTS_SYS_ATTR_MISSING) {
47+ // // // CCCD is not set, so say that neither Notify nor Indicate is enabled.
48+ // // cccd = 0;
49+ // // } else {
50+ // // check_nrf_error(err_code);
51+ // // }
52+
53+ // return cccd;
54+ // }
55+
56+
57+ // STATIC void characteristic_gatts_notify_indicate(uint16_t handle, uint16_t conn_handle, mp_buffer_info_t *bufinfo, uint16_t hvx_type) {
58+ // uint16_t hvx_len = bufinfo->len;
59+
60+ // ble_gatts_hvx_params_t hvx_params = {
61+ // .handle = handle,
62+ // .type = hvx_type,
63+ // .offset = 0,
64+ // .p_len = &hvx_len,
65+ // .p_data = bufinfo->buf,
66+ // };
67+
68+ // while (1) {
69+ // const uint32_t err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
70+ // if (err_code == NRF_SUCCESS) {
71+ // break;
72+ // }
73+ // // TX buffer is full
74+ // // We could wait for an event indicating the write is complete, but just retrying is easier.
75+ // if (err_code == NRF_ERROR_RESOURCES) {
76+ // RUN_BACKGROUND_TASKS;
77+ // continue;
78+ // }
79+
80+ // // Some real error has occurred.
81+ // check_nrf_error(err_code);
82+ // }
83+ // }
8584
8685void common_hal_bleio_characteristic_construct (bleio_characteristic_obj_t * self , bleio_service_obj_t * service , uint16_t handle , bleio_uuid_obj_t * uuid , bleio_characteristic_properties_t props , bleio_attribute_security_mode_t read_perm , bleio_attribute_security_mode_t write_perm , mp_int_t max_length , bool fixed_length , mp_buffer_info_t * initial_value_bufinfo ) {
8786 self -> service = service ;
8887 self -> uuid = uuid ;
89- self -> handle = BLE_GATT_HANDLE_INVALID ;
88+ //FIX self->handle = BLE_GATT_HANDLE_INVALID;
9089 self -> props = props ;
9190 self -> read_perm = read_perm ;
9291 self -> write_perm = write_perm ;
9392 self -> descriptor_list = NULL ;
9493
95- const mp_int_t max_length_max = fixed_length ? BLE_GATTS_FIX_ATTR_LEN_MAX : BLE_GATTS_VAR_ATTR_LEN_MAX ;
96- if (max_length < 0 || max_length > max_length_max ) {
97- mp_raise_ValueError_varg (translate ("max_length must be 0-%d when fixed_length is %s" ),
98- max_length_max , fixed_length ? "True" : "False" );
99- }
94+ //FIX
95+ // const mp_int_t max_length_max = fixed_length ? BLE_GATTS_FIX_ATTR_LEN_MAX : BLE_GATTS_VAR_ATTR_LEN_MAX;
96+ // if (max_length < 0 || max_length > max_length_max) {
97+ // mp_raise_ValueError_varg(translate("max_length must be 0-%d when fixed_length is %s"),
98+ // max_length_max, fixed_length ? "True" : "False");
99+ // }
100100 self -> max_length = max_length ;
101101 self -> fixed_length = fixed_length ;
102102
@@ -159,25 +159,26 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
159159 for (size_t i = 0 ; i < BLEIO_TOTAL_CONNECTION_COUNT ; i ++ ) {
160160 bleio_connection_internal_t * connection = & bleio_connections [i ];
161161 uint16_t conn_handle = connection -> conn_handle ;
162- if (connection -> conn_handle == BLE_CONN_HANDLE_INVALID ) {
162+ if (conn_handle == BLE_CONN_HANDLE_INVALID ) {
163163 continue ;
164164 }
165165
166- uint16_t cccd = 0 ;
167-
168- const bool notify = self -> props & CHAR_PROP_NOTIFY ;
169- const bool indicate = self -> props & CHAR_PROP_INDICATE ;
170- if (notify | indicate ) {
171- cccd = characteristic_get_cccd (self -> cccd_handle , conn_handle );
172- }
173-
174- // It's possible that both notify and indicate are set.
175- if (notify && (cccd & BLE_GATT_HVX_NOTIFICATION )) {
176- characteristic_gatts_notify_indicate (self -> handle , conn_handle , bufinfo , BLE_GATT_HVX_NOTIFICATION );
177- }
178- if (indicate && (cccd & BLE_GATT_HVX_INDICATION )) {
179- characteristic_gatts_notify_indicate (self -> handle , conn_handle , bufinfo , BLE_GATT_HVX_INDICATION );
180- }
166+ //FIX
167+ // uint16_t cccd = 0;
168+
169+ // const bool notify = self->props & CHAR_PROP_NOTIFY;
170+ // const bool indicate = self->props & CHAR_PROP_INDICATE;
171+ // if (notify | indicate) {
172+ // cccd = characteristic_get_cccd(self->cccd_handle, conn_handle);
173+ // }
174+
175+ // // It's possible that both notify and indicate are set.
176+ // if (notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) {
177+ // characteristic_gatts_notify_indicate(self->handle, conn_handle, bufinfo, BLE_GATT_HVX_NOTIFICATION);
178+ // }
179+ // if (indicate && (cccd & BLE_GATT_HVX_INDICATION)) {
180+ // characteristic_gatts_notify_indicate(self->handle, conn_handle, bufinfo, BLE_GATT_HVX_INDICATION);
181+ // }
181182 }
182183 }
183184 }
@@ -192,34 +193,35 @@ bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties
192193}
193194
194195void common_hal_bleio_characteristic_add_descriptor (bleio_characteristic_obj_t * self , bleio_descriptor_obj_t * descriptor ) {
195- ble_uuid_t desc_uuid ;
196- bleio_uuid_convert_to_nrf_ble_uuid (descriptor -> uuid , & desc_uuid );
197-
198- ble_gatts_attr_md_t desc_attr_md = {
199- // Data passed is not in a permanent location and should be copied.
200- .vloc = BLE_GATTS_VLOC_STACK ,
201- .vlen = !descriptor -> fixed_length ,
202- };
203-
204- bleio_attribute_gatts_set_security_mode (& desc_attr_md .read_perm , descriptor -> read_perm );
205- bleio_attribute_gatts_set_security_mode (& desc_attr_md .write_perm , descriptor -> write_perm );
206-
207- mp_buffer_info_t desc_value_bufinfo ;
208- mp_get_buffer_raise (descriptor -> value , & desc_value_bufinfo , MP_BUFFER_READ );
209-
210- ble_gatts_attr_t desc_attr = {
211- .p_uuid = & desc_uuid ,
212- .p_attr_md = & desc_attr_md ,
213- .init_len = desc_value_bufinfo .len ,
214- .p_value = desc_value_bufinfo .buf ,
215- .init_offs = 0 ,
216- .max_len = descriptor -> max_length ,
217- };
218-
219- check_nrf_error (sd_ble_gatts_descriptor_add (self -> handle , & desc_attr , & descriptor -> handle ));
220-
221- descriptor -> next = self -> descriptor_list ;
222- self -> descriptor_list = descriptor ;
196+ //FIX
197+ // ble_uuid_t desc_uuid;
198+ // bleio_uuid_convert_to_nrf_ble_uuid(descriptor->uuid, &desc_uuid);
199+
200+ // ble_gatts_attr_md_t desc_attr_md = {
201+ // // Data passed is not in a permanent location and should be copied.
202+ // .vloc = BLE_GATTS_VLOC_STACK,
203+ // .vlen = !descriptor->fixed_length,
204+ // };
205+
206+ // bleio_attribute_gatts_set_security_mode(&desc_attr_md.read_perm, descriptor->read_perm);
207+ // bleio_attribute_gatts_set_security_mode(&desc_attr_md.write_perm, descriptor->write_perm);
208+
209+ // mp_buffer_info_t desc_value_bufinfo;
210+ // mp_get_buffer_raise(descriptor->value, &desc_value_bufinfo, MP_BUFFER_READ);
211+
212+ // ble_gatts_attr_t desc_attr = {
213+ // .p_uuid = &desc_uuid,
214+ // .p_attr_md = &desc_attr_md,
215+ // .init_len = desc_value_bufinfo.len,
216+ // .p_value = desc_value_bufinfo.buf,
217+ // .init_offs = 0,
218+ // .max_len = descriptor->max_length,
219+ // };
220+
221+ // check_nrf_error(sd_ble_gatts_descriptor_add(self->handle, &desc_attr, &descriptor->handle));
222+
223+ // descriptor->next = self->descriptor_list;
224+ // self->descriptor_list = descriptor;
223225}
224226
225227void common_hal_bleio_characteristic_set_cccd (bleio_characteristic_obj_t * self , bool notify , bool indicate ) {
@@ -234,33 +236,34 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
234236 const uint16_t conn_handle = bleio_connection_get_conn_handle (self -> service -> connection );
235237 common_hal_bleio_check_connected (conn_handle );
236238
237- uint16_t cccd_value =
238- (notify ? BLE_GATT_HVX_NOTIFICATION : 0 ) |
239- (indicate ? BLE_GATT_HVX_INDICATION : 0 );
240-
241- ble_gattc_write_params_t write_params = {
242- .write_op = BLE_GATT_OP_WRITE_REQ ,
243- .handle = self -> cccd_handle ,
244- .p_value = (uint8_t * ) & cccd_value ,
245- .len = 2 ,
246- };
247-
248- while (1 ) {
249- uint32_t err_code = sd_ble_gattc_write (conn_handle , & write_params );
250- if (err_code == NRF_SUCCESS ) {
251- break ;
252- }
253-
254- // Write with response will return NRF_ERROR_BUSY if the response has not been received.
255- // Write without reponse will return NRF_ERROR_RESOURCES if too many writes are pending.
256- if (err_code == NRF_ERROR_BUSY || err_code == NRF_ERROR_RESOURCES ) {
257- // We could wait for an event indicating the write is complete, but just retrying is easier.
258- RUN_BACKGROUND_TASKS ;
259- continue ;
260- }
261-
262- // Some real error occurred.
263- check_nrf_error (err_code );
264- }
239+ //FIX
240+ // uint16_t cccd_value =
241+ // (notify ? BLE_GATT_HVX_NOTIFICATION : 0) |
242+ // (indicate ? BLE_GATT_HVX_INDICATION : 0);
243+
244+ // ble_gattc_write_params_t write_params = {
245+ // .write_op = BLE_GATT_OP_WRITE_REQ,
246+ // .handle = self->cccd_handle,
247+ // .p_value = (uint8_t *) &cccd_value,
248+ // .len = 2,
249+ // };
250+
251+ // while (1) {
252+ // uint32_t err_code = sd_ble_gattc_write(conn_handle, &write_params);
253+ // if (err_code == NRF_SUCCESS) {
254+ // break;
255+ // }
256+
257+ // // Write with response will return NRF_ERROR_BUSY if the response has not been received.
258+ // // Write without reponse will return NRF_ERROR_RESOURCES if too many writes are pending.
259+ // if (err_code == NRF_ERROR_BUSY || err_code == NRF_ERROR_RESOURCES) {
260+ // // We could wait for an event indicating the write is complete, but just retrying is easier.
261+ // RUN_BACKGROUND_TASKS;
262+ // continue;
263+ // }
264+
265+ // // Some real error occurred.
266+ // check_nrf_error(err_code);
267+ // }
265268
266269}
0 commit comments