WIP: debug; add hash and __eq__ to UUID
This commit is contained in:
parent
6fb7590280
commit
3164b16196
|
@ -33,12 +33,6 @@ void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_u
|
|||
self->uuid = uuid;
|
||||
}
|
||||
|
||||
void common_hal_bleio_descriptor_print(bleio_descriptor_obj_t *self, const mp_print_t *print) {
|
||||
mp_printf(print, "Descriptor(uuid=");
|
||||
common_hal_bleio_uuid_print(self->uuid, print);
|
||||
mp_printf(print, ", handle=%0x", self->handle);
|
||||
}
|
||||
|
||||
mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self) {
|
||||
return self->handle;
|
||||
}
|
||||
|
|
|
@ -156,7 +156,7 @@ STATIC uint32_t set_advertisement_data(bleio_device_obj_t *device, bool connecta
|
|||
}
|
||||
|
||||
ble_uuid_t uuid;
|
||||
bleio_uuid_convert_to_nrf_uuid(service->uuid, &uuid);
|
||||
bleio_uuid_convert_to_nrf_ble_uuid(service->uuid, &uuid);
|
||||
|
||||
err_code = sd_ble_uuid_encode(&uuid, &encoded_size, &adv_data[byte_pos]);
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
|
@ -188,7 +188,7 @@ STATIC uint32_t set_advertisement_data(bleio_device_obj_t *device, bool connecta
|
|||
}
|
||||
|
||||
ble_uuid_t uuid;
|
||||
bleio_uuid_convert_to_nrf_uuid(service->uuid, &uuid);
|
||||
bleio_uuid_convert_to_nrf_ble_uuid(service->uuid, &uuid);
|
||||
|
||||
err_code = sd_ble_uuid_encode(&uuid, &encoded_size, &adv_data[byte_pos]);
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
|
@ -327,7 +327,7 @@ STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *res
|
|||
service->handle = gattc_service->handle_range.start_handle;
|
||||
|
||||
bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t);
|
||||
bleio_uuid_construct_from_nrf_uuid(uuid, &gattc_service->uuid);
|
||||
bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_service->uuid);
|
||||
service->uuid = uuid;
|
||||
|
||||
mp_obj_list_append(device->service_list, service);
|
||||
|
@ -352,7 +352,7 @@ STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio
|
|||
|
||||
bleio_uuid_obj_t *uuid = m_new_obj(bleio_uuid_obj_t);
|
||||
uuid->base.type = &bleio_uuid_type;
|
||||
bleio_uuid_construct_from_nrf_uuid(uuid, &gattc_char->uuid);
|
||||
bleio_uuid_construct_from_nrf_ble_uuid(uuid, &gattc_char->uuid);
|
||||
characteristic->uuid = uuid;
|
||||
|
||||
characteristic->props.broadcast = gattc_char->char_props.broadcast;
|
||||
|
@ -475,7 +475,7 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *device_in) {
|
|||
|
||||
void common_hal_bleio_device_add_service(bleio_device_obj_t *device, bleio_service_obj_t *service) {
|
||||
ble_uuid_t uuid;
|
||||
bleio_uuid_convert_to_nrf_uuid(service->uuid, &uuid);
|
||||
bleio_uuid_convert_to_nrf_ble_uuid(service->uuid, &uuid);
|
||||
|
||||
uint8_t service_type = BLE_GATTS_SRVC_TYPE_PRIMARY;
|
||||
if (service->is_secondary) {
|
||||
|
|
|
@ -52,7 +52,7 @@ void common_hal_bleio_service_add_characteristic(bleio_service_obj_t *self, blei
|
|||
}
|
||||
|
||||
ble_uuid_t uuid;
|
||||
bleio_uuid_convert_to_nrf_uuid(characteristic->uuid, &uuid);
|
||||
bleio_uuid_convert_to_nrf_ble_uuid(characteristic->uuid, &uuid);
|
||||
|
||||
ble_gatts_attr_md_t attr_md = {
|
||||
.vloc = BLE_GATTS_VLOC_STACK,
|
||||
|
|
|
@ -39,55 +39,60 @@
|
|||
// If uuid128 is not NULL, it's a 128-bit (16-byte) UUID, with bytes 12 and 13 zero'd out, where
|
||||
// the 16-bit part goes. Those 16 bits are passed in uuid16.
|
||||
void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, uint32_t uuid16, uint8_t uuid128[]) {
|
||||
self->uuid16 = uuid16;
|
||||
self->uuid_vs_idx = 0;
|
||||
if (uuid128 != NULL) {
|
||||
common_hal_bleio_adapter_set_enabled(true);
|
||||
|
||||
self->nrf_ble_uuid.uuid = uuid16;
|
||||
if (uuid128 == NULL) {
|
||||
self->nrf_ble_uuid.type = BLE_UUID_TYPE_BLE;
|
||||
} else {
|
||||
ble_uuid128_t vs_uuid;
|
||||
memcpy(vs_uuid.uuid128, uuid128, sizeof(vs_uuid.uuid128));
|
||||
|
||||
// Register this vendor-specific UUID. Bytes 12 and 13 will be zero.
|
||||
common_hal_bleio_adapter_set_enabled(true);
|
||||
const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->uuid_vs_idx);
|
||||
const uint32_t err_code = sd_ble_uuid_vs_add(&vs_uuid, &self->nrf_ble_uuid.type);
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
mp_raise_OSError_msg(translate("Could not register Vendor-Specific UUID"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print) {
|
||||
if (self->uuid_vs_idx != 0) {
|
||||
mp_printf(print, "UUID(uuid16=0x%04x, uuid128_handle=" HEX2_FMT ")",
|
||||
self->uuid16, self->uuid_vs_idx);
|
||||
} else {
|
||||
mp_printf(print, "UUID(0x%04x)", self->uuid16);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t common_hal_bleio_uuid_get_size(bleio_uuid_obj_t *self) {
|
||||
return self->uuid_vs_idx != 0 ? 128 : 16;
|
||||
return self->nrf_ble_uuid.type == BLE_UUID_TYPE_BLE ? 16 : 128;
|
||||
}
|
||||
|
||||
uint32_t common_hal_bleio_uuid_get_uuid16(bleio_uuid_obj_t *self) {
|
||||
return self->uuid16;
|
||||
return self->nrf_ble_uuid.uuid;
|
||||
}
|
||||
|
||||
// Returns 0 if there is no handle, otherwise returns a non-zero index.
|
||||
uint32_t common_hal_bleio_uuid_get_uuid128_handle(bleio_uuid_obj_t *self) {
|
||||
return self->uuid_vs_idx;
|
||||
// True if uuid128 has been successfully filled in.
|
||||
bool common_hal_bleio_uuid_get_uuid128(bleio_uuid_obj_t *self, uint8_t uuid128[16]) {
|
||||
uint8_t length;
|
||||
const uint32_t err_code = sd_ble_uuid_encode(&self->nrf_ble_uuid, &length, uuid128);
|
||||
|
||||
if (err_code != NRF_SUCCESS) {
|
||||
mp_raise_RuntimeError(translate("Could not decode ble_uuid"));
|
||||
}
|
||||
// If not 16 bytes, this is not a 128-bit UUID, so return.
|
||||
return length == 16;
|
||||
}
|
||||
|
||||
// Returns 0 if this is a 16-bit UUID, otherwise returns a non-zero index
|
||||
// into the 128-bit uuid registration table.
|
||||
uint32_t common_hal_bleio_uuid_get_uuid128_reference(bleio_uuid_obj_t *self) {
|
||||
return self->nrf_ble_uuid.type == BLE_UUID_TYPE_BLE ? 0 : self->nrf_ble_uuid.type;
|
||||
}
|
||||
|
||||
|
||||
void bleio_uuid_construct_from_nrf_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_uuid) {
|
||||
if (nrf_uuid->type == BLE_UUID_TYPE_UNKNOWN) {
|
||||
void bleio_uuid_construct_from_nrf_ble_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_ble_uuid) {
|
||||
if (nrf_ble_uuid->type == BLE_UUID_TYPE_UNKNOWN) {
|
||||
mp_raise_RuntimeError(translate("Unexpected nrfx uuid type"));
|
||||
}
|
||||
|
||||
self->uuid16 = nrf_uuid->uuid;
|
||||
self->uuid_vs_idx = nrf_uuid->type == BLE_UUID_TYPE_BLE ? 0 : nrf_uuid->type;
|
||||
self->nrf_ble_uuid.uuid = nrf_ble_uuid->uuid;
|
||||
self->nrf_ble_uuid.type = nrf_ble_uuid->type;
|
||||
}
|
||||
|
||||
// Fill in a ble_uuid_t from my values.
|
||||
void bleio_uuid_convert_to_nrf_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_uuid) {
|
||||
nrf_uuid->uuid = self->uuid16;
|
||||
nrf_uuid->type = self->uuid_vs_idx == 0 ? BLE_UUID_TYPE_BLE : self->uuid_vs_idx;
|
||||
void bleio_uuid_convert_to_nrf_ble_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_ble_uuid) {
|
||||
nrf_ble_uuid->uuid = self->nrf_ble_uuid.uuid;
|
||||
nrf_ble_uuid->type = self->nrf_ble_uuid.type;
|
||||
}
|
||||
|
|
|
@ -34,15 +34,15 @@
|
|||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
// If non-zero, `uuid_vs_idx` is an index into the SoftDevice's table of registered vendor-specific UUID's.
|
||||
// If zero, `value` is a 16-bit Bluetooth SIG UUID, which would convert to this 128-bit UUID.
|
||||
// 0000xxxx-0000-1000-8000-00805F9B34FB
|
||||
uint8_t uuid_vs_idx;
|
||||
// The 16-bit part of the UUID. This replaces bytes 12 and 13 in the registered 128-bit UUID.
|
||||
uint16_t uuid16;
|
||||
// Use the native way of storing UUID's:
|
||||
// - ble_uuid_t.uuid is a 16-bit uuid.
|
||||
// - ble_uuid_t.type is BLE_UUID_TYPE_BLE if it's a 16-bit Bluetooth SIG UUID.
|
||||
// or is BLE_UUID_TYPE_VENDOR_BEGIN and higher, which indexes into a table of registered
|
||||
// 128-bit UUIDs.
|
||||
ble_uuid_t nrf_ble_uuid;
|
||||
} bleio_uuid_obj_t;
|
||||
|
||||
void bleio_uuid_construct_from_nrf_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_uuid);
|
||||
void bleio_uuid_convert_to_nrf_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_uuid);
|
||||
void bleio_uuid_construct_from_nrf_ble_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_uuid);
|
||||
void bleio_uuid_convert_to_nrf_ble_uuid(bleio_uuid_obj_t *self, ble_uuid_t *nrf_uuid);
|
||||
|
||||
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_UUID_H
|
||||
|
|
|
@ -91,7 +91,7 @@ STATIC void bleio_characteristic_print(const mp_print_t *print, mp_obj_t self_in
|
|||
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
mp_printf(print, "Characteristic(");
|
||||
common_hal_bleio_uuid_print(self->uuid, print);
|
||||
bleio_uuid_print(print, self->uuid, kind);
|
||||
mp_printf(print, ")");
|
||||
}
|
||||
|
||||
|
|
|
@ -101,7 +101,9 @@ STATIC mp_obj_t bleio_descriptor_make_new(const mp_obj_type_t *type, size_t n_ar
|
|||
|
||||
STATIC void bleio_descriptor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
bleio_descriptor_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
common_hal_bleio_descriptor_print(self, print);
|
||||
mp_printf(print, "Descriptor(uuid=");
|
||||
bleio_uuid_print(print, self->uuid, kind);
|
||||
mp_printf(print, ", handle=%04x", common_hal_bleio_descriptor_get_handle(self));
|
||||
}
|
||||
|
||||
STATIC mp_obj_t bleio_descriptor_get_handle(mp_obj_t self_in) {
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
extern const mp_obj_type_t bleio_descriptor_type;
|
||||
|
||||
extern void common_hal_bleio_descriptor_construct(bleio_descriptor_obj_t *self, bleio_uuid_obj_t *uuid);
|
||||
extern void common_hal_bleio_descriptor_print(bleio_descriptor_obj_t *self, const mp_print_t *print);
|
||||
extern mp_int_t common_hal_bleio_descriptor_get_handle(bleio_descriptor_obj_t *self);
|
||||
extern mp_obj_t common_hal_bleio_descriptor_get_uuid(bleio_descriptor_obj_t *self);
|
||||
|
||||
|
|
|
@ -67,7 +67,7 @@ STATIC void bleio_service_print(const mp_print_t *print, mp_obj_t self_in, mp_pr
|
|||
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
mp_printf(print, "Service(");
|
||||
common_hal_bleio_uuid_print(self->uuid, print);
|
||||
bleio_uuid_print(print, self->uuid, kind);
|
||||
mp_printf(print, ")");
|
||||
}
|
||||
|
||||
|
@ -112,8 +112,8 @@ STATIC mp_obj_t bleio_service_add_characteristic(mp_obj_t self_in, mp_obj_t char
|
|||
bleio_service_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
bleio_characteristic_obj_t *characteristic = MP_OBJ_TO_PTR(characteristic_in);
|
||||
|
||||
if (common_hal_bleio_uuid_get_uuid128_handle(self->uuid) !=
|
||||
common_hal_bleio_uuid_get_uuid128_handle(characteristic->uuid)) {
|
||||
if (common_hal_bleio_uuid_get_uuid128_reference(self->uuid) !=
|
||||
common_hal_bleio_uuid_get_uuid128_reference(characteristic->uuid)) {
|
||||
// The descriptor base UUID doesn't match the characteristic base UUID.
|
||||
mp_raise_ValueError(translate("Characteristic UUID doesn't match Descriptor UUID"));
|
||||
}
|
||||
|
|
|
@ -34,7 +34,7 @@
|
|||
// Including hyphens.
|
||||
#define UUID128_STR_LEN 36
|
||||
// Number of bytes
|
||||
#define UUID128_BYTE_LEN 32
|
||||
#define UUID128_BYTE_LEN 16
|
||||
|
||||
STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
|
||||
return unichar_xdigit_value(nibble1) | (unichar_xdigit_value(nibble2) << 4);
|
||||
|
@ -43,13 +43,13 @@ STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
|
|||
|
||||
//| .. currentmodule:: bleio
|
||||
//|
|
||||
//| :class:`UUID16` -- BLE UUID16
|
||||
//| :class:`UUID` -- BLE UUID
|
||||
//| =========================================================
|
||||
//|
|
||||
//| A 16-bit or 128-bit UUID. Can be used for services, characteristics, descriptors and more.
|
||||
//|
|
||||
|
||||
//| .. class:: UUID(uuid)
|
||||
//| .. class:: UUID(value, *, base_uuid=None)
|
||||
//|
|
||||
//| Create a new UUID or UUID object encapsulating the uuid value.
|
||||
//| The value can be one of:
|
||||
|
@ -57,8 +57,9 @@ STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
|
|||
//| - an `int` value in range 0 to 0xFFFF (Bluetooth SIG 16-bit UUID)
|
||||
//| - a `str` value in the format 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', where the X's are hex digits.
|
||||
//| (128 bit UUID)
|
||||
//| - a buffer object (bytearray, bytes) of 16 bytes in little-endian order (128-bit UUID)
|
||||
//|
|
||||
//| :param int/str uuid: The uuid to encapsulate
|
||||
//| :param int/str/buffer value: The uuid value to encapsulate
|
||||
//|
|
||||
|
||||
STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
|
||||
|
@ -70,31 +71,45 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, si
|
|||
mp_map_t kw_args;
|
||||
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
|
||||
|
||||
enum { ARG_uuid };
|
||||
enum { ARG_value, ARG_base_uuid };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ ARG_uuid, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_value, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_base_uuid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
||||
};
|
||||
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
const mp_obj_t uuid = args[ARG_uuid].u_obj;
|
||||
const mp_obj_t value = args[ARG_value].u_obj;
|
||||
const mp_obj_t base_uuid = args[ARG_base_uuid].u_obj;
|
||||
uint8_t uuid128[16];
|
||||
|
||||
if (MP_OBJ_IS_INT(uuid)) {
|
||||
mp_int_t uuid16 = mp_obj_get_int(uuid);
|
||||
if (base_uuid != mp_const_none) {
|
||||
if (MP_OBJ_IS_TYPE(base_uuid, &bleio_uuid_type)) {
|
||||
if (!common_hal_bleio_uuid_get_uuid128(base_uuid, uuid128)) {
|
||||
mp_raise_ValueError(translate("base_uuid is not 128 bits"));
|
||||
}
|
||||
} else {
|
||||
mp_raise_ValueError(translate("base_uuid is not a UUID"));
|
||||
}
|
||||
}
|
||||
|
||||
if (MP_OBJ_IS_INT(value)) {
|
||||
mp_int_t uuid16 = mp_obj_get_int(value);
|
||||
if (uuid16 < 0 || uuid16 > 0xffff) {
|
||||
mp_raise_ValueError(translate("Integer UUID not in range 0 to 0xffff"));
|
||||
mp_raise_ValueError(translate("UUID integer value not in range 0 to 0xffff"));
|
||||
}
|
||||
|
||||
// This is a 16-bit Bluetooth SIG UUID. NULL means no 128-bit value.
|
||||
common_hal_bleio_uuid_construct(self, uuid16, NULL);
|
||||
// If a base_uuid was supplied, merge the uuid16 into it. Otherwise
|
||||
// it's a 16-bit Bluetooth SIG UUID. NULL means no 128-bit value.
|
||||
common_hal_bleio_uuid_construct(self, uuid16, base_uuid == mp_const_none ? NULL : uuid128);
|
||||
|
||||
} else if (MP_OBJ_IS_STR(uuid)) {
|
||||
} else if (MP_OBJ_IS_STR(value)) {
|
||||
uint8_t uuid128[UUID128_BYTE_LEN];
|
||||
GET_STR_DATA_LEN(uuid, str, str_len);
|
||||
GET_STR_DATA_LEN(value, str, str_len);
|
||||
if (str_len == UUID128_STR_LEN &&
|
||||
str[8] == '-' && str[13] == '-' && str[18] == '-' && str[23] == '-') {
|
||||
size_t str_index = UUID128_STR_LEN - 1;
|
||||
int str_index = UUID128_STR_LEN - 1;
|
||||
size_t uuid128_index = 0;
|
||||
bool error = false;
|
||||
|
||||
|
@ -123,21 +138,27 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, si
|
|||
uuid128[12] = 0;
|
||||
uuid128[13] = 0;
|
||||
common_hal_bleio_uuid_construct(self, uuid16, uuid128);
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
} else {
|
||||
mp_raise_ValueError(translate("UUID string must be of the form xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));
|
||||
}
|
||||
}
|
||||
mp_raise_ValueError(translate("UUID string must be of the form xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"));
|
||||
} else {
|
||||
mp_raise_ValueError(translate("UUID value is not int or string"));
|
||||
}
|
||||
// Unreachable.
|
||||
return mp_const_none;
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
|
||||
common_hal_bleio_uuid_print(self, print);
|
||||
if (common_hal_bleio_uuid_get_size(self) == 128) {
|
||||
mp_printf(print, "UUID(uuid16=0x%04x, uuid128_reference=0x%02x)",
|
||||
common_hal_bleio_uuid_get_uuid16(self),
|
||||
common_hal_bleio_uuid_get_uuid128_reference(self));
|
||||
} else {
|
||||
mp_printf(print, "UUID(0x%04x)", common_hal_bleio_uuid_get_uuid16(self));
|
||||
}
|
||||
}
|
||||
|
||||
//| .. attribute:: uuid16
|
||||
|
@ -158,22 +179,46 @@ const mp_obj_property_t bleio_uuid_uuid16_obj = {
|
|||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
//| .. attribute:: uuid128_handle
|
||||
//| .. attribute:: uuid128
|
||||
//|
|
||||
//| An opaque handle representing the 128-bit UUID. (read-only)
|
||||
//| Returns None if this is a 16-bit UUID.
|
||||
//| The 128-bit value of the UUID, return as a bytes().
|
||||
//| Throws AttributeError if this is a 16-bit UUID. (read-only)
|
||||
//|
|
||||
STATIC mp_obj_t bleio_uuid_get_uuid128_handle(mp_obj_t self_in) {
|
||||
STATIC mp_obj_t bleio_uuid_get_uuid128(mp_obj_t self_in) {
|
||||
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
uint32_t handle = common_hal_bleio_uuid_get_uuid128_handle(self);
|
||||
return handle == 0 ? mp_const_none : MP_OBJ_NEW_SMALL_INT(handle);
|
||||
|
||||
uint8_t uuid128[16];
|
||||
if (!common_hal_bleio_uuid_get_uuid128(self, uuid128)) {
|
||||
mp_raise_AttributeError(translate("not a 128-bit UUID"));
|
||||
}
|
||||
return mp_obj_new_bytes(uuid128, 16);
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_uuid128_handle_obj, bleio_uuid_get_uuid128_handle);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_uuid128_obj, bleio_uuid_get_uuid128);
|
||||
|
||||
const mp_obj_property_t bleio_uuid_uuid128_handle_obj = {
|
||||
const mp_obj_property_t bleio_uuid_uuid128_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&bleio_uuid_get_uuid128_handle_obj,
|
||||
.proxy = {(mp_obj_t)&bleio_uuid_get_uuid128_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
||||
//| .. attribute:: uuid128_reference
|
||||
//|
|
||||
//| An opaque reference representing the 128-bit UUID. (read-only)
|
||||
//| Returns None if this is a 16-bit UUID.
|
||||
//|
|
||||
STATIC mp_obj_t bleio_uuid_get_uuid128_reference(mp_obj_t self_in) {
|
||||
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
uint32_t reference = common_hal_bleio_uuid_get_uuid128_reference(self);
|
||||
return reference == 0 ? mp_const_none : MP_OBJ_NEW_SMALL_INT(reference);
|
||||
}
|
||||
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_uuid128_reference_obj, bleio_uuid_get_uuid128_reference);
|
||||
|
||||
const mp_obj_property_t bleio_uuid_uuid128_reference_obj = {
|
||||
.base.type = &mp_type_property,
|
||||
.proxy = {(mp_obj_t)&bleio_uuid_get_uuid128_reference_obj,
|
||||
(mp_obj_t)&mp_const_none_obj,
|
||||
(mp_obj_t)&mp_const_none_obj},
|
||||
};
|
||||
|
@ -200,16 +245,60 @@ const mp_obj_property_t bleio_uuid_size_obj = {
|
|||
|
||||
STATIC const mp_rom_map_elem_t bleio_uuid_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_uuid16), MP_ROM_PTR(&bleio_uuid_uuid16_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_uuid128_handle), MP_ROM_PTR(&bleio_uuid_uuid128_handle_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_uuid128), MP_ROM_PTR(&bleio_uuid_uuid128_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_uuid128_reference), MP_ROM_PTR(&bleio_uuid_uuid128_reference_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&bleio_uuid_size_obj) },
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(bleio_uuid_locals_dict, bleio_uuid_locals_dict_table);
|
||||
|
||||
STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
||||
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
switch (op) {
|
||||
case MP_UNARY_OP_HASH:
|
||||
if (common_hal_bleio_uuid_get_size(self) == 16) {
|
||||
return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_uuid_get_uuid16(self));
|
||||
} else {
|
||||
union {
|
||||
uint8_t uuid128_bytes[16];
|
||||
uint16_t uuid128_uint16[8];
|
||||
} uuid128;
|
||||
common_hal_bleio_uuid_get_uuid128(self, uuid128.uuid128_bytes);
|
||||
int hash = 0;
|
||||
for (size_t i = 0; i < MP_ARRAY_SIZE(uuid128.uuid128_uint16); i++) {
|
||||
hash += uuid128.uuid128_uint16[i];
|
||||
}
|
||||
return MP_OBJ_NEW_SMALL_INT(hash);
|
||||
}
|
||||
default:
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
}
|
||||
|
||||
STATIC mp_obj_t bleio_uuid_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
|
||||
switch (op) {
|
||||
// Two UUID's are equal if their uuid16 values and uuid128 references match.
|
||||
case MP_BINARY_OP_EQUAL:
|
||||
if (MP_OBJ_IS_TYPE(rhs_in, &bleio_uuid_type)) {
|
||||
return mp_obj_new_bool(
|
||||
common_hal_bleio_uuid_get_uuid16(lhs_in) == common_hal_bleio_uuid_get_uuid16(rhs_in) &&
|
||||
common_hal_bleio_uuid_get_uuid128_reference(lhs_in) ==
|
||||
common_hal_bleio_uuid_get_uuid128_reference(rhs_in));
|
||||
} else {
|
||||
return mp_const_false;
|
||||
}
|
||||
|
||||
default:
|
||||
return MP_OBJ_NULL; // op not supported
|
||||
}
|
||||
}
|
||||
|
||||
const mp_obj_type_t bleio_uuid_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_UUID,
|
||||
.print = bleio_uuid_print,
|
||||
.make_new = bleio_uuid_make_new,
|
||||
.unary_op = bleio_uuid_unary_op,
|
||||
.binary_op = bleio_uuid_binary_op,
|
||||
.locals_dict = (mp_obj_dict_t*)&bleio_uuid_locals_dict,
|
||||
};
|
||||
|
|
|
@ -29,12 +29,14 @@
|
|||
|
||||
#include "common-hal/bleio/UUID.h"
|
||||
|
||||
void bleio_uuid_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
|
||||
extern const mp_obj_type_t bleio_uuid_type;
|
||||
|
||||
extern void common_hal_bleio_uuid_construct(bleio_uuid_obj_t *self, mp_int_t uuid16, uint8_t uuid128[]);
|
||||
extern void common_hal_bleio_uuid_print(bleio_uuid_obj_t *self, const mp_print_t *print);
|
||||
extern uint32_t common_hal_bleio_uuid_get_uuid16(bleio_uuid_obj_t *self);
|
||||
extern uint32_t common_hal_bleio_uuid_get_uuid128_handle(bleio_uuid_obj_t *self);
|
||||
extern bool common_hal_bleio_uuid_get_uuid128(bleio_uuid_obj_t *self, uint8_t uuid128[16]);
|
||||
extern uint32_t common_hal_bleio_uuid_get_uuid128_reference(bleio_uuid_obj_t *self);
|
||||
extern uint32_t common_hal_bleio_uuid_get_size(bleio_uuid_obj_t *self);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_UUID_H
|
||||
|
|
Loading…
Reference in New Issue