Skip to content

Commit 9e7f874

Browse files
committed
fix CCCD bonding store; avoid excessive bonding writes
1 parent 346ce3b commit 9e7f874

File tree

8 files changed

+117
-114
lines changed

8 files changed

+117
-114
lines changed

ports/nrf/common-hal/_bleio/Characteristic.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,6 @@ STATIC void characteristic_gatts_notify_indicate(uint16_t handle, uint16_t conn_
8383
}
8484
}
8585

86-
STATIC bool characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
87-
bleio_characteristic_obj_t *self = (bleio_characteristic_obj_t *) param;
88-
switch (ble_evt->header.evt_id) {
89-
case BLE_GATTS_EVT_WRITE: {
90-
// A client wrote to this server characteristic.
91-
// If we are bonded, stored the CCCD value.
92-
if (self->service != MP_OBJ_NULL) {
93-
bleio_connection_obj_t *connection = self->service->connection;
94-
uint16_t conn_handle = bleio_connection_get_conn_handle(connection);
95-
if (conn_handle != BLE_CONN_HANDLE_INVALID &&
96-
common_hal_bleio_connection_get_paired(connection) &&
97-
ble_evt->evt.gatts_evt.params.write.handle == self->cccd_handle) {
98-
bonding_save_cccd_info(
99-
connection->connection->is_central, conn_handle, connection->connection->ediv);
100-
}
101-
}
102-
break;
103-
}
104-
}
105-
106-
return true;
107-
}
108-
10986
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) {
11087
self->service = service;
11188
self->uuid = uuid;
@@ -132,9 +109,6 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
132109
if (initial_value_bufinfo != NULL) {
133110
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
134111
}
135-
136-
self->handler_entry.next = NULL;
137-
//////////////// ble_drv_add_event_handler_entry(&self->handler_entry, characteristic_on_ble_evt, self);
138112
}
139113

140114
bleio_descriptor_obj_t *common_hal_bleio_characteristic_get_descriptor_list(bleio_characteristic_obj_t *self) {
@@ -288,8 +262,3 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
288262
}
289263

290264
}
291-
292-
void common_hal_bleio_characteristic_del(bleio_characteristic_obj_t *self) {
293-
// Remove from event handler list, since the evt handler entry is built-in and not a heap object.
294-
ble_drv_remove_event_handler(characteristic_on_ble_evt, self);
295-
}

ports/nrf/common-hal/_bleio/Characteristic.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ typedef struct _bleio_characteristic_obj {
4747
bleio_attribute_security_mode_t read_perm;
4848
bleio_attribute_security_mode_t write_perm;
4949
bleio_descriptor_obj_t *descriptor_list;
50-
ble_drv_evt_handler_entry_t handler_entry;
5150
uint16_t user_desc_handle;
5251
uint16_t cccd_handle;
5352
uint16_t sccd_handle;

ports/nrf/common-hal/_bleio/Connection.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,17 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
123123
break;
124124
}
125125

126+
case BLE_GATTS_EVT_WRITE:
127+
// A client wrote a value.
128+
// If we are bonded and it's a CCCD (UUID 0x2902), store the CCCD value.
129+
if (self->conn_handle != BLE_CONN_HANDLE_INVALID &&
130+
self->pair_status == PAIR_PAIRED &&
131+
ble_evt->evt.gatts_evt.params.write.uuid.type == BLE_UUID_TYPE_BLE &&
132+
ble_evt->evt.gatts_evt.params.write.uuid.uuid == 0x2902) {
133+
bonding_save_cccd_info(self->is_central, self->conn_handle, self->ediv);
134+
}
135+
break;
136+
126137
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
127138
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
128139
break;
@@ -223,7 +234,7 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
223234

224235
case BLE_GAP_EVT_AUTH_STATUS: { // 0x19
225236
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_AUTH_STATUS\n");
226-
// Pairing process completed
237+
// Key exchange completed.
227238
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
228239
self->sec_status = status->auth_status;
229240
if (status->auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
@@ -264,8 +275,10 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
264275
}
265276

266277
case BLE_GAP_EVT_CONN_SEC_UPDATE: { // 0x1a
267-
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_CONN_SEC_UPDATE\n");
278+
// We get this both on first-time pairing and on subsequent pairings using stored keys.
268279
ble_gap_conn_sec_t* conn_sec = &ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec;
280+
CONNECTION_DEBUG_PRINTF("BLE_GAP_EVT_CONN_SEC_UPDATE, sm: %d, lv: %d\n",
281+
conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
269282
if (conn_sec->sec_mode.sm <= 1 && conn_sec->sec_mode.lv <= 1) {
270283
// Security setup did not succeed:
271284
// mode 0, level 0 means no access
@@ -282,6 +295,7 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
282295
CONNECTION_DEBUG_PRINTF("bonding_load_cccd_info() failed\n");
283296
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
284297
}
298+
self->pair_status = PAIR_PAIRED;
285299
}
286300
break;
287301
}

0 commit comments

Comments
 (0)