Skip to content

Commit 390337b

Browse files
committed
wip; compiles
1 parent 242d572 commit 390337b

File tree

9 files changed

+274
-129
lines changed

9 files changed

+274
-129
lines changed

ports/nrf/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ INC += -I$(BUILD)
7373
INC += -I$(BUILD)/genhdr
7474
INC += -I./../../lib/cmsis/inc
7575
INC += -I./boards/$(BOARD)
76-
INC += -I./modules/ubluepy
77-
INC += -I./modules/ble
7876
INC += -I./nrfx
7977
INC += -I./nrfx/hal
8078
INC += -I./nrfx/mdk
@@ -156,6 +154,7 @@ SRC_C += \
156154
boards/$(BOARD)/pins.c \
157155
device/$(MCU_VARIANT)/startup_$(MCU_SUB_VARIANT).c \
158156
bluetooth/ble_drv.c \
157+
common-hal/_bleio/bonding.c \
159158
lib/libc/string0.c \
160159
lib/mp-readline/readline.c \
161160
lib/oofatfs/ff.c \

ports/nrf/background.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#if CIRCUITPY_BLEIO
4545
#include "supervisor/shared/bluetooth.h"
46+
#include "common-hal/_bleio/bonding.h"
4647
#endif
4748

4849
static bool running_background_tasks = false;
@@ -68,6 +69,7 @@ void run_background_tasks(void) {
6869

6970
#if CIRCUITPY_BLEIO
7071
supervisor_bluetooth_background();
72+
bonding_background();
7173
#endif
7274

7375
#if CIRCUITPY_DISPLAYIO

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "ble.h"
3434
#include "ble_drv.h"
35+
#include "bonding.h"
3536
#include "nrfx_power.h"
3637
#include "nrf_nvic.h"
3738
#include "nrf_sdm.h"
@@ -669,4 +670,5 @@ void bleio_adapter_reset(bleio_adapter_obj_t* adapter) {
669670
bleio_connection_internal_t *connection = &connections[i];
670671
connection->connection_obj = mp_const_none;
671672
}
673+
bonding_reset();
672674
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "shared-bindings/_bleio/Service.h"
3434

3535
#include "common-hal/_bleio/Adapter.h"
36+
#include "common-hal/_bleio/bonding.h"
3637

3738
STATIC uint16_t characteristic_get_cccd(uint16_t cccd_handle, uint16_t conn_handle) {
3839
uint16_t cccd;
@@ -92,9 +93,11 @@ STATIC bool characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
9293
bleio_connection_obj_t *connection = self->service->connection;
9394
uint16_t conn_handle = bleio_connection_get_conn_handle(connection);
9495
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);
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+
}
98101
}
99102
break;
100103
}

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
#include "shared-bindings/_bleio/Service.h"
4848
#include "shared-bindings/_bleio/UUID.h"
4949

50+
#include "common-hal/_bleio/bonding.h"
51+
5052
#define BLE_ADV_LENGTH_FIELD_SIZE 1
5153
#define BLE_ADV_AD_TYPE_FIELD_SIZE 1
5254
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
@@ -212,7 +214,7 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
212214
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
213215
self->sec_status = status->auth_status;
214216
if (status->auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
215-
self->ediv = bonding_keys->own_enc.master_id.ediv;
217+
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
216218
self->pair_status = PAIR_PAIRED;
217219
bonding_save_keys(self->is_central, self->conn_handle, &self->bonding_keys);
218220
} else {
@@ -227,13 +229,12 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
227229
// - Else return NULL --> Initiate key exchange
228230
ble_gap_evt_sec_info_request_t* sec_info_request = &ble_evt->evt.gap_evt.params.sec_info_request;
229231
(void) sec_info_request;
230-
bond_keys bond_keys_t;
231232
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+
sd_ble_gap_sec_info_reply(self->conn_handle,
233234
&self->bonding_keys.own_enc.enc_info,
234235
&self->bonding_keys.peer_id.id_info,
235236
NULL);
236-
self->ediv = bond_keys.own_enc.master_id.ediv;
237+
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
237238
} else {
238239
sd_ble_gap_sec_info_reply(self->conn_handle, NULL, NULL, NULL);
239240
}
@@ -249,12 +250,10 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
249250
// mode >=1 and/or level >=1 means encryption is set up
250251
self->pair_status = PAIR_NOT_PAIRED;
251252
} else {
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);
256-
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS SUCCESS.
257-
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
253+
// Does an sd_ble_gatts_sys_attr_set() with the stored values.
254+
if (bonding_load_cccd_info(self->is_central, self->conn_handle, self->ediv)) {
255+
// Not quite paired yet: wait for BLE_GAP_EVT_AUTH_STATUS SUCCESS.
256+
self->ediv = self->bonding_keys.own_enc.master_id.ediv;
258257
} else {
259258
// No matching bonding found, so use fresh system attributes.
260259
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
@@ -263,10 +262,6 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
263262
break;
264263
}
265264

266-
case BLE_GATTS_EVT_WRITE: {
267-
if (self->pair_status == PAIR_PAIRED) &&
268-
269-
270265
default:
271266
return false;
272267
}
@@ -278,7 +273,7 @@ void bleio_connection_clear(bleio_connection_internal_t *self) {
278273

279274
self->conn_handle = BLE_CONN_HANDLE_INVALID;
280275
self->pair_status = PAIR_NOT_PAIRED;
281-
bonding_clear_keys(self);
276+
bonding_clear_keys(&self->bonding_keys);
282277
}
283278

284279
bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
void bleio_reset(void);
3131

3232
typedef struct {
33-
ble_gap_enc_key_t own_enc;
34-
ble_gap_enc_key_t peer_enc;
35-
ble_gap_id_key_t peer_id;
33+
ble_gap_enc_key_t own_enc;
34+
ble_gap_enc_key_t peer_enc;
35+
ble_gap_id_key_t peer_id;
3636
} bonding_keys_t;
3737

3838
// We assume variable length data.

0 commit comments

Comments
 (0)