34
34
35
35
#include "common-hal/_bleio/Adapter.h"
36
36
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
- // }
84
-
85
37
void 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 ) {
86
38
self -> service = service ;
87
39
self -> uuid = uuid ;
88
- //FIX self->handle = BLE_GATT_HANDLE_INVALID;
40
+ self -> handle = BLE_GATT_HANDLE_INVALID ;
89
41
self -> props = props ;
90
42
self -> read_perm = read_perm ;
91
43
self -> write_perm = write_perm ;
@@ -153,30 +105,23 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
153
105
// Always write the value locally even if no connections are active.
154
106
// conn_handle is ignored for non-system attributes, so we use BLE_CONN_HANDLE_INVALID.
155
107
common_hal_bleio_gatts_write (self -> handle , BLE_CONN_HANDLE_INVALID , bufinfo );
156
- // Check to see if we need to notify or indicate any active connections.
157
- for (size_t i = 0 ; i < BLEIO_TOTAL_CONNECTION_COUNT ; i ++ ) {
158
- bleio_connection_internal_t * connection = & bleio_connections [i ];
159
- uint16_t conn_handle = connection -> conn_handle ;
160
- if (conn_handle == BLE_CONN_HANDLE_INVALID ) {
161
- continue ;
162
- }
163
-
164
- //FIX
165
- // uint16_t cccd = 0;
108
+ // Notify or indicate all active connections.
109
+ uint16_t cccd = 0 ;
110
+
111
+ const bool notify = self -> props & CHAR_PROP_NOTIFY ;
112
+ const bool indicate = self -> props & CHAR_PROP_INDICATE ;
113
+ // Read the CCCD value, if there is one.
114
+ if ((notify | indicate ) && self -> cccd_handle != BLE_GATT_HANDLE_INVALID ) {
115
+ common_hal_bleio_gatts_read (self -> cccd_handle , conn_handle , & cccd , sizeof (cccd ));
116
+ }
166
117
167
- // const bool notify = self->props & CHAR_PROP_NOTIFY;
168
- // const bool indicate = self->props & CHAR_PROP_INDICATE;
169
- // if (notify | indicate) {
170
- // cccd = characteristic_get_cccd(self->cccd_handle, conn_handle);
171
- // }
118
+ // It's possible that both notify and indicate are set.
119
+ if (notify && (cccd & BLE_GATT_HVX_NOTIFICATION )) {
120
+ att_notify (self -> handle , bufinfo -> buf , MIN (bufinfo -> len , self -> max_length ));
121
+ }
122
+ if (indicate && (cccd & BLE_GATT_HVX_INDICATION )) {
123
+ att_indicate (self -> handle , bufinfo -> buf , MIN (bufinfo -> len , self -> max_length ));
172
124
173
- // // It's possible that both notify and indicate are set.
174
- // if (notify && (cccd & BLE_GATT_HVX_NOTIFICATION)) {
175
- // characteristic_gatts_notify_indicate(self->handle, conn_handle, bufinfo, BLE_GATT_HVX_NOTIFICATION);
176
- // }
177
- // if (indicate && (cccd & BLE_GATT_HVX_INDICATION)) {
178
- // characteristic_gatts_notify_indicate(self->handle, conn_handle, bufinfo, BLE_GATT_HVX_INDICATION);
179
- // }
180
125
}
181
126
}
182
127
}
@@ -191,35 +136,16 @@ bleio_characteristic_properties_t common_hal_bleio_characteristic_get_properties
191
136
}
192
137
193
138
void common_hal_bleio_characteristic_add_descriptor (bleio_characteristic_obj_t * self , bleio_descriptor_obj_t * descriptor ) {
194
- //FIX
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
- // };
139
+ if (self -> handle != common_hal_bleio_adapter_obj -> last_added_characteristic_handle ) {
140
+ mp_raise_bleio_BluetoothError (
141
+ translate ("Descriptor can only be added to most recently added characteristic" ));
142
+ }
218
143
219
- // check_nrf_error(sd_ble_gatts_descriptor_add(self ->handle, &desc_attr, & descriptor->handle) );
144
+ descriptor -> handle = bleio_adapter_add_attribute ( common_hal_bleio_adapter_obj , descriptor );
220
145
221
- // descriptor->next = self->descriptor_list;
222
- // self->descriptor_list = descriptor;
146
+ // Link together all the descriptors for this characteristic.
147
+ descriptor -> next = self -> descriptor_list ;
148
+ self -> descriptor_list = descriptor ;
223
149
}
224
150
225
151
void common_hal_bleio_characteristic_set_cccd (bleio_characteristic_obj_t * self , bool notify , bool indicate ) {
@@ -234,11 +160,12 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
234
160
const uint16_t conn_handle = bleio_connection_get_conn_handle (self -> service -> connection );
235
161
common_hal_bleio_check_connected (conn_handle );
236
162
237
- //FIX
238
- // uint16_t cccd_value =
239
- // (notify ? BLE_GATT_HVX_NOTIFICATION : 0) |
240
- // (indicate ? BLE_GATT_HVX_INDICATION : 0);
163
+ uint16_t cccd_value =
164
+ (notify ? BLE_GATT_HVX_NOTIFICATION : 0 ) |
165
+ (indicate ? BLE_GATT_HVX_INDICATION : 0 );
241
166
167
+ (void ) cccd_value ;
168
+ //FIX call att_something to set remote CCCD
242
169
// ble_gattc_write_params_t write_params = {
243
170
// .write_op = BLE_GATT_OP_WRITE_REQ,
244
171
// .handle = self->cccd_handle,
0 commit comments