Skip to content

Commit 4584ef4

Browse files
authored
Support address types (#18)
* nrf5/modules/ubluepy: Adding new enumeration of address types. * nrf5/modules/ubluepy: Adding constants that can be used from micropython for public and random static address types. * nrf5/modules/ubluepy: Adding support for optionally setting address type in Peripheral.connect(). Public address is used as default. Address types can be retrieved from 'constants'. Either constants.ADDR_TYPE_PUBLIC or constants.ADDR_TYPE_RANDOM_STATIC. * nrf5/modules/ubluepy: Register central GAP event handler before issuing connect to a peripheral. Has to be done before connect() function as a connected event will be propergated upon successfull connection. The handler will set the connection handle which gets connect function out of the busy loop waiting for connection to succeed. * nrf5/modules/ubluepy: Removing duplicate setting of GAP event handler in connect().
1 parent 0865fbd commit 4584ef4

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

nrf5/modules/ubluepy/modubluepy.h

+9
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ typedef enum {
9292
UBLUEPY_SERVICE_SECONDARY = 2
9393
} ubluepy_service_type_t;
9494

95+
typedef enum {
96+
UBLUEPY_ADDR_TYPE_PUBLIC = 0,
97+
UBLUEPY_ADDR_TYPE_RANDOM_STATIC = 1,
98+
#if 0
99+
UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE = 2,
100+
UBLUEPY_ADDR_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE = 3,
101+
#endif
102+
} ubluepy_addr_type_t;
103+
95104
typedef struct _ubluepy_uuid_obj_t {
96105
mp_obj_base_t base;
97106
ubluepy_uuid_type_t type;

nrf5/modules/ubluepy/ubluepy_constants.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,14 @@ const mp_obj_type_t ubluepy_constants_ad_types_type = {
7777

7878
STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = {
7979
// GAP events
80-
{ MP_ROM_QSTR(MP_QSTR_EVT_GAP_CONNECTED), MP_ROM_INT(16) },
81-
{ MP_ROM_QSTR(MP_QSTR_EVT_GAP_DISCONNECTED), MP_ROM_INT(17) },
82-
{ MP_ROM_QSTR(MP_QSTR_UUID_CCCD), MP_ROM_INT(0x2902) },
80+
{ MP_ROM_QSTR(MP_QSTR_EVT_GAP_CONNECTED), MP_ROM_INT(16) },
81+
{ MP_ROM_QSTR(MP_QSTR_EVT_GAP_DISCONNECTED), MP_ROM_INT(17) },
82+
{ MP_ROM_QSTR(MP_QSTR_UUID_CCCD), MP_ROM_INT(0x2902) },
8383

84-
{ MP_ROM_QSTR(MP_QSTR_ad_types), MP_ROM_PTR(&ubluepy_constants_ad_types_type) },
84+
{ MP_ROM_QSTR(MP_QSTR_ADDR_TYPE_PUBLIC), MP_ROM_INT(UBLUEPY_ADDR_TYPE_PUBLIC) },
85+
{ MP_ROM_QSTR(MP_QSTR_ADDR_TYPE_RANDOM_STATIC), MP_ROM_INT(UBLUEPY_ADDR_TYPE_RANDOM_STATIC) },
86+
87+
{ MP_ROM_QSTR(MP_QSTR_ad_types), MP_ROM_PTR(&ubluepy_constants_ad_types_type) },
8588
};
8689

8790
STATIC MP_DEFINE_CONST_DICT(ubluepy_constants_locals_dict, ubluepy_constants_locals_dict_table);

nrf5/modules/ubluepy/ubluepy_peripheral.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,26 @@ void static disc_add_char(mp_obj_t service_in, ble_drv_char_data_t * p_desc_data
336336
mp_obj_list_append(p_service->char_list, MP_OBJ_FROM_PTR(p_char));
337337
}
338338

339-
/// \method connect(device_address)
339+
/// \method connect(device_address [, addr_type=ADDR_TYPE_PUBLIC])
340340
/// Connect to device peripheral with the given device address.
341+
/// addr_type can be either ADDR_TYPE_PUBLIC (default) or
342+
/// ADDR_TYPE_RANDOM_STATIC.
341343
///
342-
STATIC mp_obj_t peripheral_connect(mp_obj_t self_in, mp_obj_t dev_addr) {
343-
ubluepy_peripheral_obj_t * self = MP_OBJ_TO_PTR(self_in);
344+
STATIC mp_obj_t peripheral_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
345+
ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
346+
mp_obj_t dev_addr = pos_args[1];
347+
348+
static const mp_arg_t allowed_args[] = {
349+
{ MP_QSTR_addr_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UBLUEPY_ADDR_TYPE_PUBLIC } },
350+
};
351+
352+
// parse args
353+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
354+
mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
355+
356+
uint8_t addr_type = args[0].u_int;
357+
358+
ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(self), gap_event_handler);
344359

345360
ble_drv_gap_event_handler_set(MP_OBJ_FROM_PTR(self), gap_event_handler);
346361

@@ -363,7 +378,7 @@ STATIC mp_obj_t peripheral_connect(mp_obj_t self_in, mp_obj_t dev_addr) {
363378
p_addr[5] = unichar_xdigit_value(str_data[1]);
364379
p_addr[5] += unichar_xdigit_value(str_data[0]) << 4;
365380

366-
ble_drv_connect(p_addr, 1);
381+
ble_drv_connect(p_addr, addr_type);
367382

368383
m_del(uint8_t, p_addr, 6);
369384
}
@@ -403,7 +418,7 @@ STATIC mp_obj_t peripheral_connect(mp_obj_t self_in, mp_obj_t dev_addr) {
403418

404419
return mp_const_none;
405420
}
406-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(ubluepy_peripheral_connect_obj, peripheral_connect);
421+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ubluepy_peripheral_connect_obj, 2, peripheral_connect);
407422

408423
#endif
409424

0 commit comments

Comments
 (0)