Skip to content

Commit 91d791a

Browse files
committed
cleanup adapter.address; add uniquish suffix to BLE device name
1 parent 83129b8 commit 91d791a

8 files changed

Lines changed: 85 additions & 49 deletions

File tree

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

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
#include "nrfx_power.h"
3535
#include "nrf_nvic.h"
3636
#include "nrf_sdm.h"
37+
#include "py/objstr.h"
3738
#include "py/runtime.h"
38-
#include "shared-bindings/bleio/Adapter.h"
39-
4039
#include "supervisor/usb.h"
40+
#include "shared-bindings/bleio/Adapter.h"
41+
#include "shared-bindings/bleio/Address.h"
4142

4243
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
4344
mp_raise_msg_varg(&mp_type_AssertionError,
@@ -132,20 +133,42 @@ bool common_hal_bleio_adapter_get_enabled(void) {
132133
return is_enabled;
133134
}
134135

135-
void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address) {
136-
ble_gap_addr_t local_address;
136+
void get_address(ble_gap_addr_t *address) {
137137
uint32_t err_code;
138138

139139
common_hal_bleio_adapter_set_enabled(true);
140-
err_code = sd_ble_gap_addr_get(&local_address);
140+
err_code = sd_ble_gap_addr_get(address);
141141

142142
if (err_code != NRF_SUCCESS) {
143143
mp_raise_OSError_msg(translate("Failed to get local address"));
144144
}
145+
}
146+
147+
bleio_address_obj_t *common_hal_bleio_adapter_get_address(void) {
148+
common_hal_bleio_adapter_set_enabled(true);
149+
150+
ble_gap_addr_t local_address;
151+
get_address(&local_address);
152+
153+
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
154+
address->base.type = &bleio_address_type;
155+
156+
common_hal_bleio_address_construct(address, local_address.addr, local_address.addr_type);
157+
return address;
158+
}
159+
160+
mp_obj_t common_hal_bleio_adapter_get_default_name(void) {
161+
common_hal_bleio_adapter_set_enabled(true);
162+
163+
ble_gap_addr_t local_address;
164+
get_address(&local_address);
165+
166+
char name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0 };
145167

146-
address->type = local_address.addr_type;
168+
name[sizeof(name) - 4] = nibble_to_hex_lower[local_address.addr[1] >> 4 & 0xf];
169+
name[sizeof(name) - 3] = nibble_to_hex_lower[local_address.addr[1] & 0xf];
170+
name[sizeof(name) - 2] = nibble_to_hex_lower[local_address.addr[0] >> 4 & 0xf];
171+
name[sizeof(name) - 1] = nibble_to_hex_lower[local_address.addr[0] & 0xf];
147172

148-
mp_buffer_info_t buf_info;
149-
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
150-
memcpy(address->bytes, buf_info.buf, NUM_BLEIO_ADDRESS_BYTES);
173+
return mp_obj_new_str(name, sizeof(name));
151174
}

py/objstr.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
4141
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf);
4242
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
4343

44+
const char nibble_to_hex_upper[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
45+
'A', 'B', 'C', 'D', 'E', 'F'};
46+
47+
const char nibble_to_hex_lower[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
48+
'a', 'b', 'c', 'd', 'e', 'f'};
49+
4450
/******************************************************************************/
4551
/* str */
4652

py/objstr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
7777
mp_obj_t index, bool is_slice);
7878
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
7979

80+
const char nibble_to_hex_upper[16];
81+
const char nibble_to_hex_lower[16];
82+
8083
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj);
8184
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj);
8285
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj);

shared-bindings/bleio/Adapter.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@
5353
//|
5454
//| State of the BLE adapter.
5555
//|
56-
57-
//| .. attribute:: adapter.address
58-
//|
59-
//| MAC address of the BLE adapter. (read-only)
60-
//|
61-
6256
STATIC mp_obj_t bleio_adapter_get_enabled(mp_obj_t self) {
6357
return mp_obj_new_bool(common_hal_bleio_adapter_get_enabled());
6458
}
@@ -80,13 +74,13 @@ const mp_obj_property_t bleio_adapter_enabled_obj = {
8074
(mp_obj_t)&mp_const_none_obj },
8175
};
8276

77+
//| .. attribute:: adapter.address
78+
//|
79+
//| MAC address of the BLE adapter. (read-only)
80+
//|
8381
STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) {
84-
mp_obj_t obj = bleio_address_type.make_new(&bleio_address_type, 1, 0, mp_const_none);
85-
bleio_address_obj_t *address = MP_OBJ_TO_PTR(obj);
86-
87-
common_hal_bleio_adapter_get_address(address);
82+
return MP_OBJ_FROM_PTR(common_hal_bleio_adapter_get_address());
8883

89-
return obj;
9084
}
9185
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address);
9286

@@ -97,9 +91,29 @@ const mp_obj_property_t bleio_adapter_address_obj = {
9791
(mp_obj_t)&mp_const_none_obj },
9892
};
9993

94+
//| .. attribute:: adapter.default_name
95+
//|
96+
//| default_name of the BLE adapter. (read-only)
97+
//| The name is "CIRCUITPY" + the last four hex digits of ``adapter.address``,
98+
//| to make it easy to distinguish multiple CircuitPython boards.
99+
//|
100+
STATIC mp_obj_t bleio_adapter_get_default_name(mp_obj_t self) {
101+
return common_hal_bleio_adapter_get_default_name();
102+
103+
}
104+
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_default_name_obj, bleio_adapter_get_default_name);
105+
106+
const mp_obj_property_t bleio_adapter_default_name_obj = {
107+
.base.type = &mp_type_property,
108+
.proxy = { (mp_obj_t)&bleio_adapter_get_default_name_obj,
109+
(mp_obj_t)&mp_const_none_obj,
110+
(mp_obj_t)&mp_const_none_obj },
111+
};
112+
100113
STATIC const mp_rom_map_elem_t bleio_adapter_locals_dict_table[] = {
101114
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&bleio_adapter_enabled_obj) },
102115
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_adapter_address_obj) },
116+
{ MP_ROM_QSTR(MP_QSTR_default_name), MP_ROM_PTR(&bleio_adapter_default_name_obj) },
103117
};
104118

105119
STATIC MP_DEFINE_CONST_DICT(bleio_adapter_locals_dict, bleio_adapter_locals_dict_table);

shared-bindings/bleio/Adapter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const mp_obj_type_t bleio_adapter_type;
3434

3535
extern bool common_hal_bleio_adapter_get_enabled(void);
3636
extern void common_hal_bleio_adapter_set_enabled(bool enabled);
37-
extern void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address);
37+
extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(void);
38+
extern mp_obj_t common_hal_bleio_adapter_get_default_name(void);
3839

3940
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H

shared-bindings/bleio/Address.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,13 @@ STATIC mp_obj_t bleio_address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_o
147147

148148
STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
149149
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
150-
if (kind == PRINT_STR) {
151-
mp_buffer_info_t buf_info;
152-
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
153-
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
154-
155-
const uint8_t *buf = (uint8_t *) buf_info.buf;
156-
mp_printf(print,
157-
"%02x:%02x:%02x:%02x:%02x:%02x",
158-
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
159-
} else {
160-
mp_printf(print, "<Address>");
161-
}
150+
mp_buffer_info_t buf_info;
151+
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
152+
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
153+
154+
const uint8_t *buf = (uint8_t *) buf_info.buf;
155+
mp_printf(print, "<Address %02x:%02x:%02x:%02x:%02x:%02x>",
156+
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
162157
}
163158

164159
//| .. data:: PUBLIC

shared-bindings/bleio/Peripheral.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545

4646
#include "common-hal/bleio/Peripheral.h"
4747

48-
static const char default_name[] = "CIRCUITPY";
49-
5048
#define ADV_INTERVAL_DEFAULT (1.0f)
5149
#define ADV_INTERVAL_MIN (0.0020f)
5250
#define ADV_INTERVAL_MIN_STRING "0.0020"
@@ -81,14 +79,14 @@ static const char default_name[] = "CIRCUITPY";
8179
//| # Wait for connection.
8280
//| pass
8381
//|
84-
//| .. class:: Peripheral(services=(), \*, name='CIRCUITPY')
82+
//| .. class:: Peripheral(services=(), \*, name=None)
8583
//|
8684
//| Create a new Peripheral object.
8785
//|
8886
//| :param iterable services: the Service objects representing services available from this peripheral, if any.
8987
//| A non-connectable peripheral will have no services.
90-
//| :param str name: The name used when advertising this peripheral. Use ``None`` when a name is not needed,
91-
//| such as when the peripheral is a beacon
88+
//| :param str name: The name used when advertising this peripheral. If name is None,
89+
//| bleio.adapter.default_name will be used.
9290
//|
9391
STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
9492
enum { ARG_services, ARG_name };
@@ -119,22 +117,19 @@ STATIC mp_obj_t bleio_peripheral_make_new(const mp_obj_type_t *type, size_t n_ar
119117
mp_obj_list_append(services_list, service);
120118
}
121119

122-
const mp_obj_t name = args[ARG_name].u_obj;
123-
mp_obj_t name_str;
120+
mp_obj_t name = args[ARG_name].u_obj;
124121
if (name == MP_OBJ_NULL || name == mp_const_none) {
125-
name_str = mp_obj_new_str(default_name, strlen(default_name));
126-
} else if (MP_OBJ_IS_STR(name)) {
127-
name_str = name;
128-
} else {
122+
name = common_hal_bleio_adapter_get_default_name();
123+
} else if (!MP_OBJ_IS_STR(name)) {
129124
mp_raise_ValueError(translate("name must be a string"));
130125
}
131126

132-
common_hal_bleio_peripheral_construct(self, services_list, name_str);
127+
common_hal_bleio_peripheral_construct(self, services_list, name);
133128

134129
return MP_OBJ_FROM_PTR(self);
135130
}
136131

137-
//| .. attribute:: connected
132+
//| .. attribute:: connected (read-only)
138133
//|
139134
//| True if connected to a BLE Central device.
140135
//|

supervisor/shared/usb/usb.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "tick.h"
28+
#include "py/objstr.h"
2829
#include "shared-bindings/microcontroller/Processor.h"
2930
#include "shared-module/usb_midi/__init__.h"
3031
#include "supervisor/port.h"
@@ -43,13 +44,11 @@ void load_serial_number(void) {
4344
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
4445
common_hal_mcu_processor_get_uid(raw_id);
4546

46-
static const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
47-
'A', 'B', 'C', 'D', 'E', 'F'};
4847
for (int i = 0; i < COMMON_HAL_MCU_PROCESSOR_UID_LENGTH; i++) {
4948
for (int j = 0; j < 2; j++) {
5049
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
5150
// Strings are UTF-16-LE encoded.
52-
usb_serial_number[1 + i * 2 + j] = nibble_to_hex[nibble];
51+
usb_serial_number[1 + i * 2 + j] = nibble_to_hex_upper[nibble];
5352
}
5453
}
5554
}

0 commit comments

Comments
 (0)