BLE: peripheral client pairing (not yet bonding); fix time doc formatting
This commit is contained in:
parent
28ca05ccdc
commit
83129b8c63
|
@ -203,8 +203,8 @@ STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
|
|||
break;
|
||||
}
|
||||
|
||||
// For debugging.
|
||||
default:
|
||||
// For debugging.
|
||||
// mp_printf(&mp_plat_print, "Unhandled characteristic event: 0x%04x\n", ble_evt->header.evt_id);
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -52,6 +52,19 @@
|
|||
#define BLE_ADV_AD_TYPE_FIELD_SIZE 1
|
||||
#define BLE_AD_TYPE_FLAGS_DATA_SIZE 1
|
||||
|
||||
static const ble_gap_sec_params_t pairing_sec_params = {
|
||||
.bond = 0, // TODO: add bonding
|
||||
.mitm = 0,
|
||||
.lesc = 0,
|
||||
.keypress = 0,
|
||||
.oob = 0,
|
||||
.io_caps = BLE_GAP_IO_CAPS_NONE,
|
||||
.min_key_size = 7,
|
||||
.max_key_size = 16,
|
||||
.kdist_own = { .enc = 1, .id = 1},
|
||||
.kdist_peer = { .enc = 1, .id = 1},
|
||||
};
|
||||
|
||||
STATIC void check_data_fit(size_t data_len) {
|
||||
if (data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX) {
|
||||
mp_raise_ValueError(translate("Data too large for advertisement packet"));
|
||||
|
@ -61,6 +74,9 @@ STATIC void check_data_fit(size_t data_len) {
|
|||
STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
|
||||
bleio_peripheral_obj_t *self = (bleio_peripheral_obj_t*)self_in;
|
||||
|
||||
// For debugging.
|
||||
// mp_printf(&mp_plat_print, "Peripheral event: 0x%04x\n", ble_evt->header.evt_id);
|
||||
|
||||
switch (ble_evt->header.evt_id) {
|
||||
case BLE_GAP_EVT_CONNECTED: {
|
||||
// Central has connected.
|
||||
|
@ -89,10 +105,6 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
|
|||
// Someday may handle timeouts or limit reached.
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
sd_ble_gap_sec_params_reply(self->conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST: {
|
||||
ble_gap_evt_conn_param_update_request_t *request =
|
||||
&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) {
|
|||
sd_ble_gatts_sys_attr_set(self->conn_handle, NULL, 0, 0);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
|
||||
sd_ble_gap_sec_params_reply(self->conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, &pairing_sec_params, NULL);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_LESC_DHKEY_REQUEST:
|
||||
// TODO for LESC pairing:
|
||||
// sd_ble_gap_lesc_dhkey_reply(...);
|
||||
break;
|
||||
|
||||
case BLE_GAP_EVT_AUTH_STATUS: {
|
||||
// Pairing process completed
|
||||
ble_gap_evt_auth_status_t* status = &ble_evt->evt.gap_evt.params.auth_status;
|
||||
if (BLE_GAP_SEC_STATUS_SUCCESS == status->auth_status) {
|
||||
mp_printf(&mp_plat_print, "Pairing succeeded, status: 0x%04x\n", status->auth_status);
|
||||
self->pair_status = PAIR_PAIRED;
|
||||
} else {
|
||||
mp_printf(&mp_plat_print, "Pairing failed, status: 0x%04x\n", status->auth_status);
|
||||
self->pair_status = PAIR_NOT_PAIRED;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case BLE_GAP_EVT_CONN_SEC_UPDATE: {
|
||||
ble_gap_conn_sec_t* conn_sec = &ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec;
|
||||
mp_printf(&mp_plat_print, "sm: %d, lv: %d\n", conn_sec->sec_mode.sm, conn_sec->sec_mode.lv);
|
||||
if (conn_sec->sec_mode.sm <= 1 && conn_sec->sec_mode.lv <= 1) {
|
||||
// Security setup did not succeed:
|
||||
// mode 0, level 0 means no access
|
||||
// mode 1, level 1 means open link
|
||||
// mode >=1 and/or level >=1 means encryption is set up
|
||||
self->pair_status = PAIR_NOT_PAIRED;
|
||||
mp_printf(&mp_plat_print, "PAIR_NOT_PAIRED\n");
|
||||
} else {
|
||||
// TODO: see Bluefruit lib
|
||||
// if ( !bond_load_cccd(_role, _conn_hdl, _ediv) ) {
|
||||
// sd_ble_gatts_sys_attr_set(_conn_hdl, NULL, 0, 0);
|
||||
// }
|
||||
self->pair_status = PAIR_PAIRED;
|
||||
mp_printf(&mp_plat_print, "PAIR_PAIRED\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
default:
|
||||
// For debugging.
|
||||
// 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_
|
|||
|
||||
self->conn_handle = BLE_CONN_HANDLE_INVALID;
|
||||
self->adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
|
||||
self->pair_status = PAIR_NOT_PAIRED;
|
||||
|
||||
// Add all the services.
|
||||
|
||||
|
@ -157,7 +214,7 @@ void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_
|
|||
}
|
||||
|
||||
|
||||
mp_obj_list_t *common_hal_bleio_peripheral_get_services_list(bleio_peripheral_obj_t *self) {
|
||||
mp_obj_list_t *common_hal_bleio_peripheral_get_services(bleio_peripheral_obj_t *self) {
|
||||
return self->services_list;
|
||||
}
|
||||
|
||||
|
@ -248,3 +305,25 @@ void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *self)
|
|||
void common_hal_bleio_peripheral_disconnect(bleio_peripheral_obj_t *self) {
|
||||
sd_ble_gap_disconnect(self->conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
|
||||
}
|
||||
|
||||
mp_obj_list_t *common_hal_bleio_peripheral_get_remote_services(bleio_peripheral_obj_t *self) {
|
||||
return self->remote_services_list;
|
||||
}
|
||||
|
||||
void common_hal_bleio_peripheral_pair(bleio_peripheral_obj_t *self) {
|
||||
self->pair_status = PAIR_WAITING;
|
||||
|
||||
uint32_t err_code = sd_ble_gap_authenticate(self->conn_handle, &pairing_sec_params);
|
||||
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
mp_raise_OSError_msg_varg(translate("Failed to start pairing, error 0x%04x"), err_code);
|
||||
}
|
||||
|
||||
while (self->pair_status == PAIR_WAITING) {
|
||||
MICROPY_VM_HOOK_LOOP;
|
||||
}
|
||||
|
||||
if (self->pair_status == PAIR_NOT_PAIRED) {
|
||||
mp_raise_OSError_msg(translate("Failed to pair"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,12 @@
|
|||
#include "shared-module/bleio/__init__.h"
|
||||
#include "shared-module/bleio/Address.h"
|
||||
|
||||
typedef enum {
|
||||
PAIR_NOT_PAIRED,
|
||||
PAIR_WAITING,
|
||||
PAIR_PAIRED,
|
||||
} pair_status_t;
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
mp_obj_t name;
|
||||
|
@ -52,7 +58,7 @@ typedef struct {
|
|||
uint8_t* advertising_data;
|
||||
uint8_t* scan_response_data;
|
||||
uint8_t adv_handle;
|
||||
|
||||
pair_status_t pair_status;
|
||||
} bleio_peripheral_obj_t;
|
||||
|
||||
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_PERIPHERAL_H
|
||||
|
|
|
@ -159,7 +159,7 @@ const mp_obj_property_t bleio_peripheral_connected_obj = {
|
|||
STATIC mp_obj_t bleio_peripheral_get_services(mp_obj_t self_in) {
|
||||
bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
// Return list as a tuple so user won't be able to change it.
|
||||
mp_obj_list_t *services_list = common_hal_bleio_peripheral_get_services_list(self);
|
||||
mp_obj_list_t *services_list = common_hal_bleio_peripheral_get_services(self);
|
||||
return mp_obj_new_tuple(services_list->len, services_list->items);
|
||||
}
|
||||
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
|
|||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_peripheral_discover_remote_services_obj, 1, bleio_peripheral_discover_remote_services);
|
||||
|
||||
//| .. attribute:: remote_services (read-only)
|
||||
//|
|
||||
//| A tuple of services provided by the remote central.
|
||||
//| If discovery did not occur, an empty tuple will be returned.
|
||||
//|
|
||||
STATIC mp_obj_t bleio_peripheral_get_remote_services(mp_obj_t self_in) {
|
||||
bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
// Return list as a tuple so user won't be able to change it.
|
||||
mp_obj_list_t *service_list = common_hal_bleio_peripheral_get_remote_services(self);
|
||||
return mp_obj_new_tuple(service_list->len, service_list->items);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_get_remote_services_obj, bleio_peripheral_get_remote_services);
|
||||
|
||||
//| .. method:: pair()
|
||||
//|
|
||||
//| Request pairing with connected central.
|
||||
STATIC mp_obj_t bleio_peripheral_pair(mp_obj_t self_in) {
|
||||
bleio_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
common_hal_bleio_peripheral_pair(self);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_peripheral_pair_obj, bleio_peripheral_pair);
|
||||
|
||||
const mp_obj_property_t bleio_peripheral_remote_services_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = { (mp_obj_t)&bleio_peripheral_get_remote_services_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj },
|
||||
};
|
||||
|
||||
|
||||
STATIC const mp_rom_map_elem_t bleio_peripheral_locals_dict_table[] = {
|
||||
// Methods
|
||||
{ MP_ROM_QSTR(MP_QSTR_start_advertising), MP_ROM_PTR(&bleio_peripheral_start_advertising_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stop_advertising), MP_ROM_PTR(&bleio_peripheral_stop_advertising_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&bleio_peripheral_disconnect_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_discover_remote_services), MP_ROM_PTR(&bleio_peripheral_discover_remote_services_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_pair) , MP_ROM_PTR(&bleio_peripheral_pair_obj) },
|
||||
|
||||
// Properties
|
||||
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_peripheral_connected_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_peripheral_name_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&bleio_peripheral_services_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_peripheral_connected_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_peripheral_name_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_remote_services), MP_ROM_PTR(&bleio_peripheral_remote_services_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_services), MP_ROM_PTR(&bleio_peripheral_services_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(bleio_peripheral_locals_dict, bleio_peripheral_locals_dict_table);
|
||||
|
|
|
@ -33,11 +33,13 @@
|
|||
extern const mp_obj_type_t bleio_peripheral_type;
|
||||
|
||||
extern void common_hal_bleio_peripheral_construct(bleio_peripheral_obj_t *self, mp_obj_list_t *service_list, mp_obj_t name);
|
||||
extern mp_obj_list_t *common_hal_bleio_peripheral_get_services_list(bleio_peripheral_obj_t *self);
|
||||
extern mp_obj_list_t *common_hal_bleio_peripheral_get_services(bleio_peripheral_obj_t *self);
|
||||
extern bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self);
|
||||
extern mp_obj_t common_hal_bleio_peripheral_get_name(bleio_peripheral_obj_t *self);
|
||||
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);
|
||||
extern void common_hal_bleio_peripheral_stop_advertising(bleio_peripheral_obj_t *device);
|
||||
extern void common_hal_bleio_peripheral_disconnect(bleio_peripheral_obj_t *device);
|
||||
extern mp_obj_list_t *common_hal_bleio_peripheral_get_remote_services(bleio_peripheral_obj_t *self);
|
||||
extern void common_hal_bleio_peripheral_pair(bleio_peripheral_obj_t *device);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_PERIPHERAL_H
|
||||
|
|
|
@ -99,16 +99,17 @@ mp_obj_t struct_time_make_new(const mp_obj_type_t *type, size_t n_args, const mp
|
|||
//|
|
||||
//| Structure used to capture a date and time. Note that it takes a tuple!
|
||||
//|
|
||||
//| :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.
|
||||
//| * the year, 2017 for example
|
||||
//| * the month, range [1, 12]
|
||||
//| * the day of the month, range [1, 31]
|
||||
//| * the hour, range [0, 23]
|
||||
//| * the minute, range [0, 59]
|
||||
//| * the second, range [0, 61]
|
||||
//| * the day of the week, range [0, 6], Monday is 0
|
||||
//| * the day of the year, range [1, 366], -1 indicates not known
|
||||
//| * 1 when in daylight savings, 0 when not, -1 if unknown.
|
||||
//| :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)``
|
||||
//|
|
||||
//| * ``tm_year``: the year, 2017 for example
|
||||
//| * ``tm_month``: the month, range [1, 12]
|
||||
//| * ``tm_mday``: the day of the month, range [1, 31]
|
||||
//| * ``tm_hour``: the hour, range [0, 23]
|
||||
//| * ``tm_minute``: the minute, range [0, 59]
|
||||
//| * ``tm_sec``: the second, range [0, 61]
|
||||
//| * ``tm_wday``: the day of the week, range [0, 6], Monday is 0
|
||||
//| * ``tm_yday``: the day of the year, range [1, 366], -1 indicates not known
|
||||
//| * ``tm_isdst``: 1 when in daylight savings, 0 when not, -1 if unknown.
|
||||
//|
|
||||
const mp_obj_namedtuple_type_t struct_time_type_obj = {
|
||||
.base = {
|
||||
|
|
Loading…
Reference in New Issue