Skip to content

Commit 83129b8

Browse files
committed
BLE: peripheral client pairing (not yet bonding); fix time doc formatting
1 parent 28ca05c commit 83129b8

6 files changed

Lines changed: 146 additions & 22 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
203203
break;
204204
}
205205

206-
// For debugging.
207206
default:
207+
// For debugging.
208208
// mp_printf(&mp_plat_print, "Unhandled characteristic event: 0x%04x\n", ble_evt->header.evt_id);
209209
break;
210210
}

ports/nrf/common-hal/bleio/Peripheral.c

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@
5252
#define BLE_ADV_AD_TYPE_FIELD_SIZE 1
5353
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
5454

55+
static const ble_gap_sec_params_t pairing_sec_params = {
56+
.bond = 0, // TODO: add bonding
57+
.mitm = 0,
58+
.lesc = 0,
59+
.keypress = 0,
60+
.oob = 0,
61+
.io_caps = BLE_GAP_IO_CAPS_NONE,
62+
.min_key_size = 7,
63+
.max_key_size = 16,
64+
.kdist_own = { .enc = 1, .id = 1},
65+
.kdist_peer = { .enc = 1, .id = 1},
66+
};
67+
5568
STATIC void check_data_fit(size_t data_len) {
5669
if (data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX) {
5770
mp_raise_ValueError(translate("Data too large for advertisement packet"));
@@ -61,6 +74,9 @@ STATIC void check_data_fit(size_t data_len) {
6174
STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
6275
bleio_peripheral_obj_t *self = (bleio_peripheral_obj_t*)self_in;
6376

77+
// For debugging.
78+
// mp_printf(&mp_plat_print, "Peripheral event: 0x%04x\n", ble_evt->header.evt_id);
79+
6480
switch (ble_evt->header.evt_id) {
6581
case BLE_GAP_EVT_CONNECTED: {
6682
// Central has connected.
@@ -89,10 +105,6 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
89105
// Someday may handle timeouts or limit reached.
90106
break;
91107

92-
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
93-
sd_ble_gap_sec_params_reply(self->conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
94-
break;
95-
96108
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: {
97109
ble_gap_evt_conn_param_update_request_t *request =
98110
&ble_evt->evt.gap_evt.params.conn_param_update_request;
@@ -113,6 +125,50 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
113125
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
114126
break;
115127

128+
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
129+
sd_ble_gap_sec_params_reply(self->conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &pairing_sec_params, NULL);
130+
break;
131+
132+
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
133+
// TODO for LESC pairing:
134+
// sd_ble_gap_lesc_dhkey_reply(...);
135+
break;
136+
137+
case BLE_GAP_EVT_AUTH_STATUS: {
138+
// Pairing process completed
139+
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
140+
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) {
141+
mp_printf(&mp_plat_print, "Pairing succeeded, status: 0x%04x\n", status->auth_status);
142+
self->pair_status = PAIR_PAIRED;
143+
} else {
144+
mp_printf(&mp_plat_print, "Pairing failed, status: 0x%04x\n", status->auth_status);
145+
self->pair_status = PAIR_NOT_PAIRED;
146+
}
147+
break;
148+
}
149+
150+
case BLE_GAP_EVT_CONN_SEC_UPDATE: {
151+
ble_gap_conn_sec_t* conn_sec = &ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec;
152+
mp_printf(&mp_plat_print, "sm: %d, lv: %d\n", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
153+
if (conn_sec->sec_mode.sm <= 1 && conn_sec->sec_mode.lv <= 1) {
154+
// Security setup did not succeed:
155+
// mode 0, level 0 means no access
156+
// mode 1, level 1 means open link
157+
// mode >=1 and/or level >=1 means encryption is set up
158+
self->pair_status = PAIR_NOT_PAIRED;
159+
mp_printf(&mp_plat_print, "PAIR_NOT_PAIRED\n");
160+
} else {
161+
// TODO: see Bluefruit lib
162+
// if ( !bond_load_cccd(_role, _conn_hdl, _ediv) ) {
163+
// sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
164+
// }
165+
self->pair_status = PAIR_PAIRED;
166+
mp_printf(&mp_plat_print, "PAIR_PAIRED\n");
167+
}
168+
break;
169+
}
170+
171+
116172
default:
117173
// For debugging.
118174
// mp_printf(&mp_plat_print, "Unhandled peripheral event: 0x%04x\n", ble_evt->header.evt_id);
@@ -130,6 +186,7 @@ void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_
130186

131187
self->conn_handle = BLE_CONN_HANDLE_INVALID;
132188
self->adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
189+
self->pair_status = PAIR_NOT_PAIRED;
133190

134191
// Add all the services.
135192

@@ -157,7 +214,7 @@ void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_
157214
}
158215

159216

160-
mp_obj_list_t *common_hal_bleio_peripheral_get_services_list(bleio_peripheral_obj_t *self) {
217+
mp_obj_list_t *common_hal_bleio_peripheral_get_services(bleio_peripheral_obj_t *self) {
161218
return self->services_list;
162219
}
163220

@@ -248,3 +305,25 @@ void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *self)
248305
void common_hal_bleio_peripheral_disconnect(bleio_peripheral_obj_t *self) {
249306
sd_ble_gap_disconnect(self->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
250307
}
308+
309+
mp_obj_list_t *common_hal_bleio_peripheral_get_remote_services(bleio_peripheral_obj_t *self) {
310+
return self->remote_services_list;
311+
}
312+
313+
void common_hal_bleio_peripheral_pair(bleio_peripheral_obj_t *self) {
314+
self->pair_status = PAIR_WAITING;
315+
316+
uint32_t err_code = sd_ble_gap_authenticate(self->conn_handle, &pairing_sec_params);
317+
318+
if (err_code != NRF_SUCCESS) {
319+
mp_raise_OSError_msg_varg(translate("Failed to start pairing, error 0x%04x"), err_code);
320+
}
321+
322+
while (self->pair_status == PAIR_WAITING) {
323+
MICROPY_VM_HOOK_LOOP;
324+
}
325+
326+
if (self->pair_status == PAIR_NOT_PAIRED) {
327+
mp_raise_OSError_msg(translate("Failed to pair"));
328+
}
329+
}

ports/nrf/common-hal/bleio/Peripheral.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
#include "shared-module/bleio/__init__.h"
3939
#include "shared-module/bleio/Address.h"
4040

41+
typedef enum {
42+
PAIR_NOT_PAIRED,
43+
PAIR_WAITING,
44+
PAIR_PAIRED,
45+
} pair_status_t;
46+
4147
typedef struct {
4248
mp_obj_base_t base;
4349
mp_obj_t name;
@@ -52,7 +58,7 @@ typedef struct {
5258
uint8_t* advertising_data;
5359
uint8_t* scan_response_data;
5460
uint8_t adv_handle;
55-
61+
pair_status_t pair_status;
5662
} bleio_peripheral_obj_t;
5763

5864
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_PERIPHERAL_H

shared-bindings/bleio/Peripheral.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const mp_obj_property_t bleio_peripheral_connected_obj = {
159159
STATIC mp_obj_t bleio_peripheral_get_services(mp_obj_t self_in) {
160160
bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
161161
// Return list as a tuple so user won't be able to change it.
162-
mp_obj_list_t *services_list = common_hal_bleio_peripheral_get_services_list(self);
162+
mp_obj_list_t *services_list = common_hal_bleio_peripheral_get_services(self);
163163
return mp_obj_new_tuple(services_list->len, services_list->items);
164164
}
165165
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_services_obj, bleio_peripheral_get_services);
@@ -312,17 +312,53 @@ STATIC mp_obj_t bleio_peripheral_discover_remote_services(mp_uint_t n_args, cons
312312
}
313313
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_discover_remote_services_obj, 1, bleio_peripheral_discover_remote_services);
314314

315+
//| .. attribute:: remote_services (read-only)
316+
//|
317+
//| A tuple of services provided by the remote central.
318+
//| If discovery did not occur, an empty tuple will be returned.
319+
//|
320+
STATIC mp_obj_t bleio_peripheral_get_remote_services(mp_obj_t self_in) {
321+
bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
322+
323+
// Return list as a tuple so user won't be able to change it.
324+
mp_obj_list_t *service_list = common_hal_bleio_peripheral_get_remote_services(self);
325+
return mp_obj_new_tuple(service_list->len, service_list->items);
326+
}
327+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_remote_services_obj, bleio_peripheral_get_remote_services);
328+
329+
//| .. method:: pair()
330+
//|
331+
//| Request pairing with connected central.
332+
STATIC mp_obj_t bleio_peripheral_pair(mp_obj_t self_in) {
333+
bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
334+
335+
common_hal_bleio_peripheral_pair(self);
336+
337+
return mp_const_none;
338+
}
339+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_pair_obj, bleio_peripheral_pair);
340+
341+
const mp_obj_property_t bleio_peripheral_remote_services_obj = {
342+
.base.type = &mp_type_property,
343+
.proxy = { (mp_obj_t)&bleio_peripheral_get_remote_services_obj,
344+
(mp_obj_t)&mp_const_none_obj,
345+
(mp_obj_t)&mp_const_none_obj },
346+
};
347+
348+
315349
STATIC const mp_rom_map_elem_t bleio_peripheral_locals_dict_table[] = {
316350
// Methods
317351
{ MP_ROM_QSTR(MP_QSTR_start_advertising), MP_ROM_PTR(&bleio_peripheral_start_advertising_obj) },
318352
{ MP_ROM_QSTR(MP_QSTR_stop_advertising), MP_ROM_PTR(&bleio_peripheral_stop_advertising_obj) },
319353
{ MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&bleio_peripheral_disconnect_obj) },
320354
{ MP_ROM_QSTR(MP_QSTR_discover_remote_services), MP_ROM_PTR(&bleio_peripheral_discover_remote_services_obj) },
355+
{ MP_ROM_QSTR(MP_QSTR_pair) , MP_ROM_PTR(&bleio_peripheral_pair_obj) },
321356

322357
// Properties
323-
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_peripheral_connected_obj) },
324-
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_peripheral_name_obj) },
325-
{ MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&bleio_peripheral_services_obj) },
358+
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_peripheral_connected_obj) },
359+
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_peripheral_name_obj) },
360+
{ MP_ROM_QSTR(MP_QSTR_remote_services), MP_ROM_PTR(&bleio_peripheral_remote_services_obj) },
361+
{ MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&bleio_peripheral_services_obj) },
326362
};
327363

328364
STATIC MP_DEFINE_CONST_DICT(bleio_peripheral_locals_dict, bleio_peripheral_locals_dict_table);

shared-bindings/bleio/Peripheral.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
extern const mp_obj_type_t bleio_peripheral_type;
3434

3535
extern void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_list_t *service_list, mp_obj_t name);
36-
extern mp_obj_list_t *common_hal_bleio_peripheral_get_services_list(bleio_peripheral_obj_t *self);
36+
extern mp_obj_list_t *common_hal_bleio_peripheral_get_services(bleio_peripheral_obj_t *self);
3737
extern bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self);
3838
extern mp_obj_t common_hal_bleio_peripheral_get_name(bleio_peripheral_obj_t *self);
3939
extern void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *device, bool connectable, float interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo);
4040
extern void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *device);
4141
extern void common_hal_bleio_peripheral_disconnect(bleio_peripheral_obj_t *device);
42+
extern mp_obj_list_t *common_hal_bleio_peripheral_get_remote_services(bleio_peripheral_obj_t *self);
43+
extern void common_hal_bleio_peripheral_pair(bleio_peripheral_obj_t *device);
4244

4345
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_PERIPHERAL_H

shared-bindings/time/__init__.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ mp_obj_t struct_time_make_new(const mp_obj_type_t *type, size_t n_args, const mp
9999
//|
100100
//| Structure used to capture a date and time. Note that it takes a tuple!
101101
//|
102-
//| :param Tuple[tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst] time_tuple: Tuple of time info.
103-
//| * the year, 2017 for example
104-
//| * the month, range [1, 12]
105-
//| * the day of the month, range [1, 31]
106-
//| * the hour, range [0, 23]
107-
//| * the minute, range [0, 59]
108-
//| * the second, range [0, 61]
109-
//| * the day of the week, range [0, 6], Monday is 0
110-
//| * the day of the year, range [1, 366], -1 indicates not known
111-
//| * 1 when in daylight savings, 0 when not, -1 if unknown.
102+
//| :param tuple time_tuple: Tuple of time info: ``(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)``
103+
//|
104+
//| * ``tm_year``: the year, 2017 for example
105+
//| * ``tm_month``: the month, range [1, 12]
106+
//| * ``tm_mday``: the day of the month, range [1, 31]
107+
//| * ``tm_hour``: the hour, range [0, 23]
108+
//| * ``tm_minute``: the minute, range [0, 59]
109+
//| * ``tm_sec``: the second, range [0, 61]
110+
//| * ``tm_wday``: the day of the week, range [0, 6], Monday is 0
111+
//| * ``tm_yday``: the day of the year, range [1, 366], -1 indicates not known
112+
//| * ``tm_isdst``: 1 when in daylight savings, 0 when not, -1 if unknown.
112113
//|
113114
const mp_obj_namedtuple_type_t struct_time_type_obj = {
114115
.base = {

0 commit comments

Comments
 (0)