cleanup adapter.address; add uniquish suffix to BLE device name
This commit is contained in:
parent
83129b8c63
commit
91d791afd0
|
@ -34,10 +34,11 @@
|
|||
#include "nrfx_power.h"
|
||||
#include "nrf_nvic.h"
|
||||
#include "nrf_sdm.h"
|
||||
#include "py/objstr.h"
|
||||
#include "py/runtime.h"
|
||||
#include "shared-bindings/bleio/Adapter.h"
|
||||
|
||||
#include "supervisor/usb.h"
|
||||
#include "shared-bindings/bleio/Adapter.h"
|
||||
#include "shared-bindings/bleio/Address.h"
|
||||
|
||||
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
|
||||
mp_raise_msg_varg(&mp_type_AssertionError,
|
||||
|
@ -132,20 +133,42 @@ bool common_hal_bleio_adapter_get_enabled(void) {
|
|||
return is_enabled;
|
||||
}
|
||||
|
||||
void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address) {
|
||||
ble_gap_addr_t local_address;
|
||||
void get_address(ble_gap_addr_t *address) {
|
||||
uint32_t err_code;
|
||||
|
||||
common_hal_bleio_adapter_set_enabled(true);
|
||||
err_code = sd_ble_gap_addr_get(&local_address);
|
||||
err_code = sd_ble_gap_addr_get(address);
|
||||
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
mp_raise_OSError_msg(translate("Failed to get local address"));
|
||||
}
|
||||
|
||||
address->type = local_address.addr_type;
|
||||
|
||||
mp_buffer_info_t buf_info;
|
||||
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
|
||||
memcpy(address->bytes, buf_info.buf, NUM_BLEIO_ADDRESS_BYTES);
|
||||
}
|
||||
|
||||
bleio_address_obj_t *common_hal_bleio_adapter_get_address(void) {
|
||||
common_hal_bleio_adapter_set_enabled(true);
|
||||
|
||||
ble_gap_addr_t local_address;
|
||||
get_address(&local_address);
|
||||
|
||||
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
|
||||
address->base.type = &bleio_address_type;
|
||||
|
||||
common_hal_bleio_address_construct(address, local_address.addr, local_address.addr_type);
|
||||
return address;
|
||||
}
|
||||
|
||||
mp_obj_t common_hal_bleio_adapter_get_default_name(void) {
|
||||
common_hal_bleio_adapter_set_enabled(true);
|
||||
|
||||
ble_gap_addr_t local_address;
|
||||
get_address(&local_address);
|
||||
|
||||
char name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0 };
|
||||
|
||||
name[sizeof(name) - 4] = nibble_to_hex_lower[local_address.addr[1] >> 4 & 0xf];
|
||||
name[sizeof(name) - 3] = nibble_to_hex_lower[local_address.addr[1] & 0xf];
|
||||
name[sizeof(name) - 2] = nibble_to_hex_lower[local_address.addr[0] >> 4 & 0xf];
|
||||
name[sizeof(name) - 1] = nibble_to_hex_lower[local_address.addr[0] & 0xf];
|
||||
|
||||
return mp_obj_new_str(name, sizeof(name));
|
||||
}
|
||||
|
|
|
@ -41,6 +41,12 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_
|
|||
STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf);
|
||||
STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in);
|
||||
|
||||
const char nibble_to_hex_upper[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
|
||||
const char nibble_to_hex_lower[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'a', 'b', 'c', 'd', 'e', 'f'};
|
||||
|
||||
/******************************************************************************/
|
||||
/* str */
|
||||
|
||||
|
|
|
@ -77,6 +77,9 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
|
|||
mp_obj_t index, bool is_slice);
|
||||
const byte *find_subbytes(const byte *haystack, size_t hlen, const byte *needle, size_t nlen, int direction);
|
||||
|
||||
const char nibble_to_hex_upper[16];
|
||||
const char nibble_to_hex_lower[16];
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_encode_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_find_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(str_rfind_obj);
|
||||
|
|
|
@ -53,12 +53,6 @@
|
|||
//|
|
||||
//| State of the BLE adapter.
|
||||
//|
|
||||
|
||||
//| .. attribute:: adapter.address
|
||||
//|
|
||||
//| MAC address of the BLE adapter. (read-only)
|
||||
//|
|
||||
|
||||
STATIC mp_obj_t bleio_adapter_get_enabled(mp_obj_t self) {
|
||||
return mp_obj_new_bool(common_hal_bleio_adapter_get_enabled());
|
||||
}
|
||||
|
@ -80,13 +74,13 @@ const mp_obj_property_t bleio_adapter_enabled_obj = {
|
|||
(mp_obj_t)&mp_const_none_obj },
|
||||
};
|
||||
|
||||
//| .. attribute:: adapter.address
|
||||
//|
|
||||
//| MAC address of the BLE adapter. (read-only)
|
||||
//|
|
||||
STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) {
|
||||
mp_obj_t obj = bleio_address_type.make_new(&bleio_address_type, 1, 0, mp_const_none);
|
||||
bleio_address_obj_t *address = MP_OBJ_TO_PTR(obj);
|
||||
return MP_OBJ_FROM_PTR(common_hal_bleio_adapter_get_address());
|
||||
|
||||
common_hal_bleio_adapter_get_address(address);
|
||||
|
||||
return obj;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address);
|
||||
|
||||
|
@ -97,9 +91,29 @@ const mp_obj_property_t bleio_adapter_address_obj = {
|
|||
(mp_obj_t)&mp_const_none_obj },
|
||||
};
|
||||
|
||||
//| .. attribute:: adapter.default_name
|
||||
//|
|
||||
//| default_name of the BLE adapter. (read-only)
|
||||
//| The name is "CIRCUITPY" + the last four hex digits of ``adapter.address``,
|
||||
//| to make it easy to distinguish multiple CircuitPython boards.
|
||||
//|
|
||||
STATIC mp_obj_t bleio_adapter_get_default_name(mp_obj_t self) {
|
||||
return common_hal_bleio_adapter_get_default_name();
|
||||
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_default_name_obj, bleio_adapter_get_default_name);
|
||||
|
||||
const mp_obj_property_t bleio_adapter_default_name_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = { (mp_obj_t)&bleio_adapter_get_default_name_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj },
|
||||
};
|
||||
|
||||
STATIC const mp_rom_map_elem_t bleio_adapter_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&bleio_adapter_enabled_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_adapter_address_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_default_name), MP_ROM_PTR(&bleio_adapter_default_name_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(bleio_adapter_locals_dict, bleio_adapter_locals_dict_table);
|
||||
|
|
|
@ -34,6 +34,7 @@ const mp_obj_type_t bleio_adapter_type;
|
|||
|
||||
extern bool common_hal_bleio_adapter_get_enabled(void);
|
||||
extern void common_hal_bleio_adapter_set_enabled(bool enabled);
|
||||
extern void common_hal_bleio_adapter_get_address(bleio_address_obj_t *address);
|
||||
extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(void);
|
||||
extern mp_obj_t common_hal_bleio_adapter_get_default_name(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADAPTER_H
|
||||
|
|
|
@ -147,18 +147,13 @@ STATIC mp_obj_t bleio_address_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_o
|
|||
|
||||
STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
if (kind == PRINT_STR) {
|
||||
mp_buffer_info_t buf_info;
|
||||
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
|
||||
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
|
||||
mp_buffer_info_t buf_info;
|
||||
mp_obj_t address_bytes = common_hal_bleio_address_get_address_bytes(self);
|
||||
mp_get_buffer_raise(address_bytes, &buf_info, MP_BUFFER_READ);
|
||||
|
||||
const uint8_t *buf = (uint8_t *) buf_info.buf;
|
||||
mp_printf(print,
|
||||
"%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
|
||||
} else {
|
||||
mp_printf(print, "<Address>");
|
||||
}
|
||||
const uint8_t *buf = (uint8_t *) buf_info.buf;
|
||||
mp_printf(print, "<Address %02x:%02x:%02x:%02x:%02x:%02x>",
|
||||
buf[5], buf[4], buf[3], buf[2], buf[1], buf[0]);
|
||||
}
|
||||
|
||||
//| .. data:: PUBLIC
|
||||
|
|
|
@ -45,8 +45,6 @@
|
|||
|
||||
#include "common-hal/bleio/Peripheral.h"
|
||||
|
||||
static const char default_name[] = "CIRCUITPY";
|
||||
|
||||
#define ADV_INTERVAL_DEFAULT (1.0f)
|
||||
#define ADV_INTERVAL_MIN (0.0020f)
|
||||
#define ADV_INTERVAL_MIN_STRING "0.0020"
|
||||
|
@ -81,14 +79,14 @@ static const char default_name[] = "CIRCUITPY";
|
|||
//| # Wait for connection.
|
||||
//| pass
|
||||
//|
|
||||
//| .. class:: Peripheral(services=(), \*, name='CIRCUITPY')
|
||||
//| .. class:: Peripheral(services=(), \*, name=None)
|
||||
//|
|
||||
//| Create a new Peripheral object.
|
||||
//|
|
||||
//| :param iterable services: the Service objects representing services available from this peripheral, if any.
|
||||
//| A non-connectable peripheral will have no services.
|
||||
//| :param str name: The name used when advertising this peripheral. Use ``None`` when a name is not needed,
|
||||
//| such as when the peripheral is a beacon
|
||||
//| :param str name: The name used when advertising this peripheral. If name is None,
|
||||
//| bleio.adapter.default_name will be used.
|
||||
//|
|
||||
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) {
|
||||
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
|
|||
mp_obj_list_append(services_list, service);
|
||||
}
|
||||
|
||||
const mp_obj_t name = args[ARG_name].u_obj;
|
||||
mp_obj_t name_str;
|
||||
mp_obj_t name = args[ARG_name].u_obj;
|
||||
if (name == MP_OBJ_NULL || name == mp_const_none) {
|
||||
name_str = mp_obj_new_str(default_name, strlen(default_name));
|
||||
} else if (MP_OBJ_IS_STR(name)) {
|
||||
name_str = name;
|
||||
} else {
|
||||
name = common_hal_bleio_adapter_get_default_name();
|
||||
} else if (!MP_OBJ_IS_STR(name)) {
|
||||
mp_raise_ValueError(translate("name must be a string"));
|
||||
}
|
||||
|
||||
common_hal_bleio_peripheral_construct(self, services_list, name_str);
|
||||
common_hal_bleio_peripheral_construct(self, services_list, name);
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
//| .. attribute:: connected
|
||||
//| .. attribute:: connected (read-only)
|
||||
//|
|
||||
//| True if connected to a BLE Central device.
|
||||
//|
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include "tick.h"
|
||||
#include "py/objstr.h"
|
||||
#include "shared-bindings/microcontroller/Processor.h"
|
||||
#include "shared-module/usb_midi/__init__.h"
|
||||
#include "supervisor/port.h"
|
||||
|
@ -43,13 +44,11 @@ void load_serial_number(void) {
|
|||
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
|
||||
common_hal_mcu_processor_get_uid(raw_id);
|
||||
|
||||
static const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
for (int i = 0; i < COMMON_HAL_MCU_PROCESSOR_UID_LENGTH; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
|
||||
// Strings are UTF-16-LE encoded.
|
||||
usb_serial_number[1 + i * 2 + j] = nibble_to_hex[nibble];
|
||||
usb_serial_number[1 + i * 2 + j] = nibble_to_hex_upper[nibble];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue