Skip to content

Commit 39f4046

Browse files
committed
Fix pairing when peripheral. Central untested.
1 parent 2adecda commit 39f4046

File tree

8 files changed

+79
-20
lines changed

8 files changed

+79
-20
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ STATIC bool adapter_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
178178

179179
connection->conn_handle = ble_evt->evt.gap_evt.conn_handle;
180180
connection->connection_obj = mp_const_none;
181+
connection->pair_status = PAIR_NOT_PAIRED;
181182
ble_drv_add_event_handler_entry(&connection->handler_entry, connection_on_ble_evt, connection);
182183
self->connection_objs = NULL;
183184

@@ -436,7 +437,7 @@ STATIC bool connect_on_ble_evt(ble_evt_t *ble_evt, void *info_in) {
436437
return true;
437438
}
438439

439-
mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_address_obj_t *address, mp_float_t timeout, bool pair) {
440+
mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_address_obj_t *address, mp_float_t timeout) {
440441

441442
ble_gap_addr_t addr;
442443

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "ble_drv.h"
3535
#include "ble_hci.h"
3636
#include "nrf_soc.h"
37+
#include "lib/utils/interrupt_char.h"
3738
#include "py/gc.h"
3839
#include "py/objlist.h"
3940
#include "py/objstr.h"
@@ -51,7 +52,7 @@
5152
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
5253

5354
static const ble_gap_sec_params_t pairing_sec_params = {
54-
.bond = 1,
55+
.bond = 0,
5556
.mitm = 0,
5657
.lesc = 0,
5758
.keypress = 0,
@@ -109,7 +110,7 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
109110
// SoftDevice will respond to a length update request.
110111
sd_ble_gap_data_length_update(self->conn_handle, NULL, NULL);
111112
break;
112-
113+
113114
case BLE_GAP_EVT_DATA_LENGTH_UPDATE: // 0x24
114115
break;
115116

@@ -214,7 +215,7 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
214215
if (dump_events) {
215216
mp_printf(&mp_plat_print, "Unhandled connection event: 0x%04x\n", ble_evt->header.evt_id);
216217
}
217-
218+
218219
return false;
219220
}
220221
return true;
@@ -229,6 +230,13 @@ void bleio_connection_clear(bleio_connection_internal_t *self) {
229230
memset(&self->bonding_keys, 0, sizeof(self->bonding_keys));
230231
}
231232

233+
bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self) {
234+
if (self->connection == NULL) {
235+
return false;
236+
}
237+
return self->connection->pair_status == PAIR_PAIRED;
238+
}
239+
232240
bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self) {
233241
if (self->connection == NULL) {
234242
return false;
@@ -240,15 +248,17 @@ void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self) {
240248
sd_ble_gap_disconnect(self->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
241249
}
242250

243-
void common_hal_bleio_connection_pair(bleio_connection_internal_t *self) {
251+
void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond) {
244252
self->pair_status = PAIR_WAITING;
245253

246254
check_nrf_error(sd_ble_gap_authenticate(self->conn_handle, &pairing_sec_params));
247255

248-
while (self->pair_status == PAIR_WAITING) {
256+
while (self->pair_status == PAIR_WAITING && !mp_hal_is_interrupted()) {
249257
RUN_BACKGROUND_TASKS;
250258
}
251-
259+
if (mp_hal_is_interrupted()) {
260+
return;
261+
}
252262
check_sec_status(self->sec_status);
253263
}
254264

@@ -280,6 +290,7 @@ STATIC bool discover_next_characteristics(bleio_connection_internal_t* connectio
280290

281291
uint32_t err_code = sd_ble_gattc_characteristics_discover(connection->conn_handle, &handle_range);
282292
if (err_code != NRF_SUCCESS) {
293+
asm("bkpt");
283294
return false;
284295
}
285296

@@ -574,10 +585,9 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
574585
// discovery call returns nothing.
575586
// discover_next_descriptors() appends to the descriptor_list.
576587
while (next_desc_start_handle <= service->end_handle &&
577-
next_desc_start_handle < next_desc_end_handle &&
588+
next_desc_start_handle <= next_desc_end_handle &&
578589
discover_next_descriptors(self, characteristic,
579590
next_desc_start_handle, next_desc_end_handle)) {
580-
581591
// Get the most recently discovered descriptor, and then ask for descriptors
582592
// whose handles start after that descriptor's handle.
583593
const bleio_descriptor_obj_t *descriptor = characteristic->descriptor_list;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ void check_gatt_status(uint16_t gatt_status) {
6262
case BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION:
6363
mp_raise_bleio_SecurityError(translate("Insufficient authentication"));
6464
return;
65+
case BLE_GATT_STATUS_ATTERR_INSUF_ENCRYPTION:
66+
mp_raise_bleio_SecurityError(translate("Insufficient encryption"));
67+
return;
6568
default:
6669
mp_raise_bleio_BluetoothError(translate("Unknown gatt error: 0x%04x"), gatt_status);
6770
}

shared-bindings/_bleio/Adapter.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ const mp_obj_property_t bleio_adapter_connections_obj = {
321321
(mp_obj_t)&mp_const_none_obj },
322322
};
323323

324-
//| .. method:: connect(address, *, timeout, pair=False)
324+
//| .. method:: connect(address, *, timeout)
325325
//|
326326
//| Attempts a connection to the device with the given address.
327327
//|
@@ -331,11 +331,10 @@ const mp_obj_property_t bleio_adapter_connections_obj = {
331331
STATIC mp_obj_t bleio_adapter_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
332332
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
333333

334-
enum { ARG_address, ARG_timeout, ARG_pair };
334+
enum { ARG_address, ARG_timeout };
335335
static const mp_arg_t allowed_args[] = {
336336
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_OBJ },
337337
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
338-
{ MP_QSTR_pair, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = false } },
339338
};
340339

341340
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -348,7 +347,7 @@ STATIC mp_obj_t bleio_adapter_connect(mp_uint_t n_args, const mp_obj_t *pos_args
348347
bleio_address_obj_t *address = MP_OBJ_TO_PTR(args[ARG_address].u_obj);
349348
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
350349

351-
return common_hal_bleio_adapter_connect(self, address, timeout, args[ARG_pair].u_bool);
350+
return common_hal_bleio_adapter_connect(self, address, timeout);
352351
}
353352
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_connect_obj, 2, bleio_adapter_connect);
354353

shared-bindings/_bleio/Adapter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ void common_hal_bleio_adapter_stop_scan(bleio_adapter_obj_t *self);
5555

5656
bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self);
5757
mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self);
58-
mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_address_obj_t *address, mp_float_t timeout, bool pair);
58+
mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_address_obj_t *address, mp_float_t timeout);
5959

6060
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H

shared-bindings/_bleio/Characteristic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const mp_obj_property_t bleio_characteristic_properties_obj = {
168168
//| .. attribute:: uuid
169169
//|
170170
//| The UUID of this characteristic. (read-only)
171-
//|
171+
//|
172172
//| Will be ``None`` if the 128-bit UUID for this characteristic is not known.
173173
//|
174174
STATIC mp_obj_t bleio_characteristic_get_uuid(mp_obj_t self_in) {
@@ -338,7 +338,7 @@ STATIC MP_DEFINE_CONST_DICT(bleio_characteristic_locals_dict, bleio_characterist
338338
STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
339339
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
340340
if (self->uuid) {
341-
mp_printf(print, "Characteristic(");
341+
mp_printf(print, "0x%08x Characteristic(", (uint32_t) self_in);
342342
bleio_uuid_print(print, MP_OBJ_FROM_PTR(self->uuid), kind);
343343
mp_printf(print, ")");
344344
} else {

shared-bindings/_bleio/Connection.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,49 @@ STATIC mp_obj_t bleio_connection_disconnect(mp_obj_t self_in) {
9292
}
9393
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_connection_disconnect_obj, bleio_connection_disconnect);
9494

95+
96+
//| .. method:: pair(*, bond=True)
97+
//|
98+
//| Pair to the peer to improve security.
99+
//|
100+
STATIC mp_obj_t bleio_connection_pair(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
101+
bleio_connection_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
102+
103+
enum { ARG_bond };
104+
static const mp_arg_t allowed_args[] = {
105+
{ MP_QSTR_bond, MP_ARG_BOOL, {.u_bool = true} },
106+
};
107+
108+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
109+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
110+
111+
ensure_connected(self);
112+
113+
common_hal_bleio_connection_pair(self->connection, args[ARG_bond].u_bool);
114+
return mp_const_none;
115+
}
116+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_connection_pair_obj, 1, bleio_connection_pair);
117+
95118
//| .. method:: discover_remote_services(service_uuids_whitelist=None)
96119
//|
97120
//| Do BLE discovery for all services or for the given service UUIDS,
98121
//| to find their handles and characteristics, and return the discovered services.
99122
//| `Connection.connected` must be True.
100123
//|
101124
//| :param iterable service_uuids_whitelist:
102-
//|
125+
//|
103126
//| an iterable of :py:class:~`UUID` objects for the services provided by the peripheral
104127
//| that you want to use.
105128
//|
106129
//| The peripheral may provide more services, but services not listed are ignored
107130
//| and will not be returned.
108131
//|
109-
//| If service_uuids_whitelist is None, then all services will undergo discovery, which can be
132+
//| If service_uuids_whitelist is None, then all services will undergo discovery, which can be
110133
//| slow.
111134
//|
112135
//| If the service UUID is 128-bit, or its characteristic UUID's are 128-bit, you
113136
//| you must have already created a :py:class:~`UUID` object for that UUID in order for the
114-
//| service or characteristic to be discovered. Creating the UUID causes the UUID to be
137+
//| service or characteristic to be discovered. Creating the UUID causes the UUID to be
115138
//| registered for use. (This restriction may be lifted in the future.)
116139
//|
117140
//| :return: A tuple of `_bleio.Service` objects provided by the remote peripheral.
@@ -137,7 +160,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_connection_discover_remote_services_obj,
137160

138161
//| .. attribute:: connected
139162
//|
140-
//| True if connected to a remote peer.
163+
//| True if connected to the remote peer.
141164
//|
142165
STATIC mp_obj_t bleio_connection_get_connected(mp_obj_t self_in) {
143166
bleio_connection_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -153,13 +176,34 @@ const mp_obj_property_t bleio_connection_connected_obj = {
153176
(mp_obj_t)&mp_const_none_obj },
154177
};
155178

179+
180+
//| .. attribute:: paired
181+
//|
182+
//| True if paired to the remote peer.
183+
//|
184+
STATIC mp_obj_t bleio_connection_get_paired(mp_obj_t self_in) {
185+
bleio_connection_obj_t *self = MP_OBJ_TO_PTR(self_in);
186+
187+
return mp_obj_new_bool(common_hal_bleio_connection_get_paired(self));
188+
}
189+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_connection_get_paired_obj, bleio_connection_get_paired);
190+
191+
const mp_obj_property_t bleio_connection_paired_obj = {
192+
.base.type = &mp_type_property,
193+
.proxy = { (mp_obj_t)&bleio_connection_get_paired_obj,
194+
(mp_obj_t)&mp_const_none_obj,
195+
(mp_obj_t)&mp_const_none_obj },
196+
};
197+
156198
STATIC const mp_rom_map_elem_t bleio_connection_locals_dict_table[] = {
157199
// Methods
200+
{ MP_ROM_QSTR(MP_QSTR_pair), MP_ROM_PTR(&bleio_connection_pair_obj) },
158201
{ MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&bleio_connection_disconnect_obj) },
159202
{ MP_ROM_QSTR(MP_QSTR_discover_remote_services), MP_ROM_PTR(&bleio_connection_discover_remote_services_obj) },
160203

161204
// Properties
162205
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_connection_connected_obj) },
206+
{ MP_ROM_QSTR(MP_QSTR_paired), MP_ROM_PTR(&bleio_connection_paired_obj) },
163207
};
164208

165209
STATIC MP_DEFINE_CONST_DICT(bleio_connection_locals_dict, bleio_connection_locals_dict_table);

shared-bindings/_bleio/Connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@
3434

3535
extern const mp_obj_type_t bleio_connection_type;
3636

37+
extern void common_hal_bleio_connection_pair(bleio_connection_internal_t *self, bool bond);
3738
extern void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self);
3839
extern bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self);
40+
extern bool common_hal_bleio_connection_get_paired(bleio_connection_obj_t *self);
3941
extern mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist);
4042

4143
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_CONNECTION_H

0 commit comments

Comments
 (0)