Skip to content

Commit 242d572

Browse files
committed
wip
1 parent 6c3d555 commit 242d572

File tree

9 files changed

+359
-28
lines changed

9 files changed

+359
-28
lines changed

ports/atmel-samd/common-hal/nvm/ByteArray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ bool common_hal_nvm_bytearray_set_bytes(nvm_bytearray_obj_t *self,
4343
// whenever we need it instead of storing it long term.
4444
struct flash_descriptor desc;
4545
desc.dev.hw = NVMCTRL;
46-
flash_write(&desc, (uint32_t) self->start_address + start_index, values, len);
46+
bool status = flash_write(&desc, (uint32_t) self->start_address + start_index, values, len) == ERR_NONE;
4747
assert_heap_ok();
48-
return true;
48+
return status;
4949
}
5050

5151
// NVM memory is memory mapped so reading it is easy.

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ STATIC void characteristic_gatts_notify_indicate(uint16_t handle, uint16_t conn_
8282
}
8383
}
8484

85+
STATIC bool characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
86+
bleio_characteristic_obj_t *self = (bleio_characteristic_obj_t *) param;
87+
switch (ble_evt->header.evt_id) {
88+
case BLE_GATTS_EVT_WRITE: {
89+
// A client wrote to this server characteristic.
90+
// If we are bonded, stored the CCCD value.
91+
if (self->service != MP_OBJ_NULL) {
92+
bleio_connection_obj_t *connection = self->service->connection;
93+
uint16_t conn_handle = bleio_connection_get_conn_handle(connection);
94+
if (conn_handle != BLE_CONN_HANDLE_INVALID &&
95+
connection->pairing_status == PAIR_PAIRED &&
96+
ble_evt->gatts_evt.params.write.handle == self->cccd_handle) {
97+
bonding_save_cccd_later(connection->is_central, conn_handle, connection->ediv);
98+
}
99+
break;
100+
}
101+
}
102+
103+
return true;
104+
}
105+
85106
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) {
86107
self->service = service;
87108
self->uuid = uuid;
@@ -108,6 +129,8 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
108129
if (initial_value_bufinfo != NULL) {
109130
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
110131
}
132+
133+
ble_drv_add_event_handler(characteristic_on_ble_evt, self);
111134
}
112135

113136
bleio_descriptor_obj_t *common_hal_bleio_characteristic_get_descriptor_list(bleio_characteristic_obj_t *self) {

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

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
171171
break;
172172
}
173173
case BLE_GAP_EVT_SEC_PARAMS_REQUEST: {
174+
// First time pairing.
175+
// 1. Either we or peer initiate the process
176+
// 2. Peer asks for security parameters using BLE_GAP_EVT_SEC_PARAMS_REQUEST.
177+
// 3. Pair Key exchange ("just works" implemented now; TODO key pairing)
178+
// 4. Connection is secured: BLE_GAP_EVT_CONN_SEC_UPDATE
179+
// 5. Long-term Keys exchanged: BLE_GAP_EVT_AUTH_STATUS
180+
181+
bonding_clear_keys(&self->bonding_keys);
182+
self->ediv = EDIV_INVALID;
174183
ble_gap_sec_keyset_t keyset = {
175184
.keys_own = {
176185
.p_enc_key = &self->bonding_keys.own_enc,
@@ -188,7 +197,8 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
188197
};
189198

190199
sd_ble_gap_sec_params_reply(self->conn_handle, BLE_GAP_SEC_STATUS_SUCCESS,
191-
&pairing_sec_params, &keyset);
200+
self->is_central ? NULL : &pairing_sec_params,
201+
&keyset);
192202
break;
193203
}
194204

@@ -202,8 +212,9 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
202212
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
203213
self->sec_status = status->auth_status;
204214
if (status->auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
205-
// TODO _ediv = bonding_keys->own_enc.master_id.ediv;
215+
self->ediv = bonding_keys->own_enc.master_id.ediv;
206216
self->pair_status = PAIR_PAIRED;
217+
bonding_save_keys(self->is_central, self->conn_handle, &self->bonding_keys);
207218
} else {
208219
self->pair_status = PAIR_NOT_PAIRED;
209220
}
@@ -216,14 +227,17 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
216227
// - Else return NULL --> Initiate key exchange
217228
ble_gap_evt_sec_info_request_t* sec_info_request = &ble_evt->evt.gap_evt.params.sec_info_request;
218229
(void) sec_info_request;
219-
//if ( bond_load_keys(_role, sec_req->master_id.ediv, &bkeys) ) {
220-
//sd_ble_gap_sec_info_reply(_conn_hdl, &bkeys.own_enc.enc_info, &bkeys.peer_id.id_info, NULL);
221-
//
222-
//_ediv = bkeys.own_enc.master_id.ediv;
223-
// } else {
230+
bond_keys bond_keys_t;
231+
if ( bonding_load_keys(self->is_central, sec_info_request->master_id.ediv, &self->bonding_keys) ) {
232+
sd_ble_gap_sec_info_reply(self->conn_handle
233+
&self->bonding_keys.own_enc.enc_info,
234+
&self->bonding_keys.peer_id.id_info,
235+
NULL);
236+
self->ediv = bond_keys.own_enc.master_id.ediv;
237+
} else {
224238
sd_ble_gap_sec_info_reply(self->conn_handle, NULL, NULL, NULL);
225-
// }
226-
break;
239+
}
240+
break;
227241
}
228242

229243
case BLE_GAP_EVT_CONN_SEC_UPDATE: { // 0x1a
@@ -235,17 +249,23 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
235249
// mode >=1 and/or level >=1 means encryption is set up
236250
self->pair_status = PAIR_NOT_PAIRED;
237251
} else {
238-
//if ( !bond_load_cccd(_role, _conn_hdl, _ediv) ) {
239-
if (true) { // TODO: no bonding yet
240-
// Initialize system attributes fresh.
241-
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
242-
}
252+
uint8_t *sys_attr;
253+
uint16_t sys_attr_len;
254+
if (bonding_load_cccd_info(self->is_central, self->conn_handle, self->ediv, sys_attr, sys_attr_len)) {
255+
sd_ble_gatts_sys_attr_set(self->conn_handle, sys_attr, sys_attr_len, SVC_CONTEXT_FLAG);
243256
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS SUCCESS.
244257
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
258+
} else {
259+
// No matching bonding found, so use fresh system attributes.
260+
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
261+
}
245262
}
246263
break;
247264
}
248265

266+
case BLE_GATTS_EVT_WRITE: {
267+
if (self->pair_status == PAIR_PAIRED) &&
268+
249269

250270
default:
251271
return false;
@@ -258,8 +278,7 @@ void bleio_connection_clear(bleio_connection_internal_t *self) {
258278

259279
self->conn_handle = BLE_CONN_HANDLE_INVALID;
260280
self->pair_status = PAIR_NOT_PAIRED;
261-
262-
memset(&self->bonding_keys, 0, sizeof(self->bonding_keys));
281+
bonding_clear_keys(self);
263282
}
264283

265284
bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self) {
@@ -480,7 +499,7 @@ STATIC void on_desc_discovery_rsp(ble_gattc_evt_desc_disc_rsp_t *response, bleio
480499
default:
481500
// TODO: sd_ble_gattc_descriptors_discover() can return things that are not descriptors,
482501
// so ignore those.
483-
// https://devzone.nordicsemi.com/f/nordic-q-a/49500/sd_ble_gattc_descriptors_discover-is-returning-attributes-that-are-not-descriptors
502+
// htts:p//devzone.nordicsemi.com/f/nordic-q-a/49500/sd_ble_gattc_descriptors_discover-is-returning-attributes-that-are-not-descriptors
484503
break;
485504
}
486505

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "py/objlist.h"
3737

3838
#include "common-hal/_bleio/__init__.h"
39+
#include "common-hal/_bleio/bonding.h"
3940
#include "shared-module/_bleio/Address.h"
4041
#include "common-hal/_bleio/Service.h"
4142

@@ -59,7 +60,7 @@ typedef struct {
5960
// EDIV: Encrypted Diversifier: Identifies LTK during legacy pairing.
6061
bonding_keys_t bonding_keys;
6162
uint16_t ediv;
62-
pair_status_t pair_status;
63+
volatile pair_status_t pair_status;
6364
uint8_t sec_status; // Internal security status.
6465
mp_obj_t connection_obj;
6566
ble_drv_evt_handler_entry_t handler_entry;

0 commit comments

Comments
 (0)