WIP
This commit is contained in:
parent
1905d90eaa
commit
6fed24e1b6
@ -53,9 +53,8 @@ STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) {
|
||||
//| Create a new Address object encapsulating the address value.
|
||||
//| The value itself can be one of:
|
||||
//|
|
||||
//| - a `str` value in the format of 'XXXXXXXXXXXX' or 'XX:XX:XX:XX:XX:XX' (12 hex digits)
|
||||
//| - a `bytes` or `bytearray` containing 6 bytes
|
||||
//| - another Address object
|
||||
//| :param buf: The address value to encapsulate
|
||||
//| - a buffer object (bytearray, bytes) of 6 bytes
|
||||
//|
|
||||
//| :param address: The address to encapsulate
|
||||
//|
|
||||
@ -84,54 +83,15 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
|
||||
|
||||
const mp_obj_t address = args[ARG_address].u_obj;
|
||||
|
||||
if (MP_OBJ_IS_STR(address)) {
|
||||
GET_STR_DATA_LEN(address, str, str_len);
|
||||
|
||||
size_t value_index = 0;
|
||||
int str_index = str_len;
|
||||
bool error = false;
|
||||
|
||||
// Loop until fewer than two characters left.
|
||||
while (str_index >= 1 && value_index < sizeof(self->value)) {
|
||||
if (str[str_index] == ':') {
|
||||
// Skip colon separators.
|
||||
str_index--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!unichar_isxdigit(str[str_index]) ||
|
||||
!unichar_isxdigit(str[str_index-1])) {
|
||||
error = true;
|
||||
break;
|
||||
}
|
||||
|
||||
self->value[value_index] = xdigit_8b_value(str[str_index],
|
||||
str[str_index-1]);
|
||||
value_index += 1;
|
||||
str_index -= 2;
|
||||
}
|
||||
// Check for correct number of hex digits and no parsing errors.
|
||||
if (error || value_index != ADDRESS_BYTE_LEN || str_index != -1) {
|
||||
mp_raise_ValueError_varg(translate("Address is not %d bytes long or is in wrong format"),
|
||||
ADDRESS_BYTE_LEN);
|
||||
}
|
||||
} else if (MP_OBJ_IS_TYPE(address, &mp_type_bytearray) || MP_OBJ_IS_TYPE(address, &mp_type_bytes)) {
|
||||
mp_buffer_info_t buf_info;
|
||||
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
|
||||
if (buf_info.len != BLEIO_ADDRESS_BYTES) {
|
||||
mp_raise_ValueError_varg(translate("Address must be %d bytes long"), BLEIO_ADDRESS_BYTES);
|
||||
}
|
||||
|
||||
for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) {
|
||||
self->value[BLEIO_ADDRESS_BYTES - b - 1] = ((uint8_t*)buf_info.buf)[b];
|
||||
}
|
||||
} else if (MP_OBJ_IS_TYPE(address, &bleio_address_type)) {
|
||||
// deep copy
|
||||
bleio_address_obj_t *other = MP_OBJ_TO_PTR(address);
|
||||
self->type = other->type;
|
||||
memcpy(self->value, other->value, BLEIO_ADDRESS_BYTES);
|
||||
mp_buffer_info_t buf_info;
|
||||
mp_get_buffer_raise(address, &buf_info, MP_BUFFER_READ);
|
||||
if (buf_info.len != BLEIO_ADDRESS_BYTES) {
|
||||
mp_raise_ValueError_varg(translate("Address must be %d bytes long"), BLEIO_ADDRESS_BYTES);
|
||||
}
|
||||
|
||||
for (size_t b = 0; b < BLEIO_ADDRESS_BYTES; ++b) {
|
||||
self->value[BLEIO_ADDRESS_BYTES - b - 1] = ((uint8_t*)buf_info.buf)[b];
|
||||
}
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
|
@ -48,8 +48,9 @@
|
||||
//|
|
||||
//| - an `int` value in range 0 to 0xFFFF (Bluetooth SIG 16-bit UUID)
|
||||
//| - a buffer object (bytearray, bytes) of 16 bytes in little-endian order (128-bit UUID)
|
||||
//| - a string of hex digits of the form 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
|
||||
//|
|
||||
//| :param int/buffer value: The uuid value to encapsulate
|
||||
//| :param value: The uuid value to encapsulate
|
||||
//|
|
||||
STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
mp_arg_check_num(n_args, kw_args, 1, 1, false);
|
||||
@ -63,7 +64,7 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, co
|
||||
if (MP_OBJ_IS_INT(value)) {
|
||||
mp_int_t uuid16 = mp_obj_get_int(value);
|
||||
if (uuid16 < 0 || uuid16 > 0xffff) {
|
||||
mp_raise_ValueError(translate("UUID integer value not in range 0 to 0xffff"));
|
||||
mp_raise_ValueError(translate("UUID integer value must be 0-0xffff"));
|
||||
}
|
||||
|
||||
// NULL means no 128-bit value.
|
||||
|
Loading…
x
Reference in New Issue
Block a user