diff options
| author | Dan Halbert <halbert@halwitz.org> | 2019-06-13 21:55:07 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2019-06-13 21:55:07 -0400 |
| commit | bed6d43a768838fd3bc5d2048bf7acdc6e9b0a33 (patch) | |
| tree | 814683e585940b502d4e47f8556a69045729c77a /shared-bindings/bleio | |
| parent | 6fed24e1b6fce58f2c06328b9bc8b8f9d9512b42 (diff) | |
| parent | 6be7cf7440acb827d4b0f767b6083a859499b045 (diff) | |
merge from upstream; WIP redo Address; no more AddressType
Diffstat (limited to 'shared-bindings/bleio')
| -rw-r--r-- | shared-bindings/bleio/Address.c | 95 | ||||
| -rw-r--r-- | shared-bindings/bleio/Address.h | 8 | ||||
| -rw-r--r-- | shared-bindings/bleio/AddressType.c | 99 | ||||
| -rw-r--r-- | shared-bindings/bleio/AddressType.h | 50 | ||||
| -rw-r--r-- | shared-bindings/bleio/CharacteristicBuffer.c | 16 | ||||
| -rw-r--r-- | shared-bindings/bleio/Peripheral.c | 1 | ||||
| -rw-r--r-- | shared-bindings/bleio/ScanEntry.c | 226 | ||||
| -rw-r--r-- | shared-bindings/bleio/Scanner.c | 4 | ||||
| -rw-r--r-- | shared-bindings/bleio/UUID.c | 16 | ||||
| -rw-r--r-- | shared-bindings/bleio/__init__.c | 5 |
10 files changed, 99 insertions, 421 deletions
diff --git a/shared-bindings/bleio/Address.c b/shared-bindings/bleio/Address.c index 226c011a3..7367da535 100644 --- a/shared-bindings/bleio/Address.c +++ b/shared-bindings/bleio/Address.c @@ -33,13 +33,6 @@ #include "shared-bindings/bleio/Address.h" #include "shared-module/bleio/Address.h" -#define ADDRESS_BYTE_LEN 12 - -STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) { - return unichar_xdigit_value(nibble1) | (unichar_xdigit_value(nibble2) << 4); -} - - //| .. currentmodule:: bleio //| //| :class:`Address` -- BLE address @@ -48,30 +41,24 @@ STATIC uint8_t xdigit_8b_value(byte nibble1, byte nibble2) { //| Encapsulates the address of a BLE device. //| -//| .. class:: Address(address) +//| .. class:: Address(address, address_type) //| //| Create a new Address object encapsulating the address value. //| The value itself can be one of: //| -//| :param buf: The address value to encapsulate -//| - a buffer object (bytearray, bytes) of 6 bytes -//| -//| :param address: The address to encapsulate +//| :param buf address: The address value to encapsulate. A buffer object (bytearray, bytes) of 6 bytes. +//| :param int address_type: one of these integers: +//| - `bleio.Address.PUBLIC` = 0 +//| - `bleio.Address.RANDOM_STATIC` = 1 +//| - `bleio.Address.RANDOM_PRIVATE_RESOLVABLE` = 2 +//| - `bleio.Address.RANDOM_PRIVATE_NON_RESOLVABLE` = 3 //| -//| .. attribute:: type -//| -//| The address type. One of: -//| -//| - `bleio.AddressType.PUBLIC` -//| - `bleio.AddressType.RANDOM_STATIC` -//| - `bleio.AddressType.RANDOM_PRIVATE_RESOLVABLE` -//| - `bleio.AddressType.RANDOM_PRIVATE_NON_RESOLVABLE` -//| STATIC mp_obj_t bleio_address_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_address }; + enum { ARG_address, ARG_address_type }; static const mp_arg_t allowed_args[] = { - { MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_address, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_address_type, MP_ARG_INT, {.u_int = BLEIO_ADDRESS_TYPE_PUBLIC } }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; @@ -79,44 +66,54 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args, bleio_address_obj_t *self = m_new_obj(bleio_address_obj_t); self->base.type = &bleio_address_type; - self->type = ADDRESS_PUBLIC; const mp_obj_t address = args[ARG_address].u_obj; - 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); + if (buf_info.len != NUM_BLEIO_ADDRESS_BYTES) { + mp_raise_ValueError_varg(translate("Address must be %d bytes long"), NUM_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]; + memcpy(self->bytes, buf_info.buf, buf_info.len); + + const mp_int_t address_type = args[ARG_address_type].u_int; + if (address_type < BLEIO_ADDRESS_TYPE_MIN || address_type > BLEIO_ADDRESS_TYPE_MAX) { + mp_raise_ValueError(translate("Address type out of range")); } + self->type = address_type; + return MP_OBJ_FROM_PTR(self); } -STATIC void bleio_address_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +//| .. attribute:: address_bytes +//| +//| The bytes that make up the device address (read-only) +//| +//| - `bleio.Address.PUBLIC` +//| - `bleio.Address.RANDOM_STATIC` +//| - `bleio.Address.RANDOM_PRIVATE_RESOLVABLE` +//| - `bleio.Address.RANDOM_PRIVATE_NON_RESOLVABLE` +//| +STATIC mp_obj_t bleio_address_get_address_bytes(mp_obj_t self_in) { bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_printf(print, "Address('%02x:%02x:%02x:%02x:%02x:%02x')", - self->value[5], self->value[4], self->value[3], - self->value[2], self->value[1], self->value[0]); + return mp_obj_new_bytes(self->bytes, NUM_BLEIO_ADDRESS_BYTES); } +MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_address_bytes_obj, bleio_address_get_address_bytes); +//| .. attribute:: type +//| +//| The address type (read-only). One of these integers: +//| +//| - `bleio.Address.PUBLIC` +//| - `bleio.Address.RANDOM_STATIC` +//| - `bleio.Address.RANDOM_PRIVATE_RESOLVABLE` +//| - `bleio.Address.RANDOM_PRIVATE_NON_RESOLVABLE` +//| STATIC mp_obj_t bleio_address_get_type(mp_obj_t self_in) { bleio_address_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (self->type == ADDRESS_PUBLIC) { - return (mp_obj_t)&bleio_addresstype_public_obj; - } else if (self->type == ADDRESS_RANDOM_STATIC) { - return (mp_obj_t)&bleio_addresstype_random_static_obj; - } else if (self->type == ADDRESS_RANDOM_PRIVATE_RESOLVABLE) { - return (mp_obj_t)&bleio_addresstype_random_private_resolvable_obj; - } else if (self->type == ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE) { - return (mp_obj_t)&bleio_addresstype_random_private_non_resolvable_obj; - } - - return mp_const_none; + return MP_OBJ_NEW_SMALL_INT(self->type); } MP_DEFINE_CONST_FUN_OBJ_1(bleio_address_get_type_obj, bleio_address_get_type); @@ -128,7 +125,14 @@ const mp_obj_property_t bleio_address_type_obj = { }; STATIC const mp_rom_map_elem_t bleio_address_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_type_obj) }, + { MP_ROM_QSTR(MP_QSTR_address_bytes), MP_ROM_PTR(&bleio_address_get_address_bytes_obj) }, + { MP_ROM_QSTR(MP_QSTR_type), MP_ROM_PTR(&bleio_address_get_type_obj) }, + // These match the BLE_GAP_ADDR_TYPES values used by the nRF library. + { MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_OBJ_NEW_SMALL_INT(0) }, + { MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_OBJ_NEW_SMALL_INT(1) }, + { MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_RESOLVABLE), MP_OBJ_NEW_SMALL_INT(2) }, + { MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE), MP_OBJ_NEW_SMALL_INT(3) }, + }; STATIC MP_DEFINE_CONST_DICT(bleio_address_locals_dict, bleio_address_locals_dict_table); @@ -136,7 +140,6 @@ STATIC MP_DEFINE_CONST_DICT(bleio_address_locals_dict, bleio_address_locals_dict const mp_obj_type_t bleio_address_type = { { &mp_type_type }, .name = MP_QSTR_Address, - .print = bleio_address_print, .make_new = bleio_address_make_new, .locals_dict = (mp_obj_dict_t*)&bleio_address_locals_dict }; diff --git a/shared-bindings/bleio/Address.h b/shared-bindings/bleio/Address.h index 9c9e20968..ef84d7432 100644 --- a/shared-bindings/bleio/Address.h +++ b/shared-bindings/bleio/Address.h @@ -29,6 +29,14 @@ #include "py/objtype.h" +#define BLEIO_ADDRESS_TYPE_PUBLIC (0) +#define BLEIO_ADDRESS_TYPE_RANDOM_STATIC (1) +#define BLEIO_ADDRESS_TYPE_RANDOM_PRIVATE_RESOLVABLE (2) +#define BLEIO_ADDRESS_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE (3) + +#define BLEIO_ADDRESS_TYPE_MIN BLEIO_ADDRESS_TYPE_PUBLIC +#define BLEIO_ADDRESS_TYPE_MAX BLEIO_ADDRESS_TYPE_RANDOM_PRIVATE_NON_RESOLVABLE + extern const mp_obj_type_t bleio_address_type; #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESS_H diff --git a/shared-bindings/bleio/AddressType.c b/shared-bindings/bleio/AddressType.c deleted file mode 100644 index cf2151c94..000000000 --- a/shared-bindings/bleio/AddressType.c +++ /dev/null @@ -1,99 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Artur Pacholec - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "shared-bindings/bleio/AddressType.h" - -//| .. currentmodule:: bleio -//| -//| :class:`AddressType` -- defines the type of a BLE address -//| ============================================================= -//| -//| .. class:: bleio.AddressType -//| -//| Enum-like class to define the type of a BLE address, see also `bleio.Address`. -//| -//| .. data:: PUBLIC -//| -//| The address is public -//| -//| .. data:: RANDOM_STATIC -//| -//| The address is random static -//| -//| .. data:: RANDOM_PRIVATE_RESOLVABLE -//| -//| The address is random private resolvable -//| -//| .. data:: RANDOM_PRIVATE_NON_RESOLVABLE -//| -//| The address is private non-resolvable -//| -const mp_obj_type_t bleio_addresstype_type; - -const bleio_addresstype_obj_t bleio_addresstype_public_obj = { - { &bleio_addresstype_type }, -}; - -const bleio_addresstype_obj_t bleio_addresstype_random_static_obj = { - { &bleio_addresstype_type }, -}; - -const bleio_addresstype_obj_t bleio_addresstype_random_private_resolvable_obj = { - { &bleio_addresstype_type }, -}; - -const bleio_addresstype_obj_t bleio_addresstype_random_private_non_resolvable_obj = { - { &bleio_addresstype_type }, -}; - -STATIC const mp_rom_map_elem_t bleio_addresstype_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_PUBLIC), MP_ROM_PTR(&bleio_addresstype_public_obj) }, - { MP_ROM_QSTR(MP_QSTR_RANDOM_STATIC), MP_ROM_PTR(&bleio_addresstype_random_static_obj) }, - { MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_RESOLVABLE), MP_ROM_PTR(&bleio_addresstype_random_private_resolvable_obj) }, - { MP_ROM_QSTR(MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE), MP_ROM_PTR(&bleio_addresstype_random_private_non_resolvable_obj) }, -}; -STATIC MP_DEFINE_CONST_DICT(bleio_addresstype_locals_dict, bleio_addresstype_locals_dict_table); - -STATIC void bleio_addresstype_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - qstr type = MP_QSTR_PUBLIC; - - if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_addresstype_random_static_obj)) { - type = MP_QSTR_RANDOM_STATIC; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_addresstype_random_private_resolvable_obj)) { - type = MP_QSTR_RANDOM_PRIVATE_RESOLVABLE; - } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&bleio_addresstype_random_private_non_resolvable_obj)) { - type = MP_QSTR_RANDOM_PRIVATE_NON_RESOLVABLE; - } - - mp_printf(print, "%q.%q.%q", MP_QSTR_bleio, MP_QSTR_AddressType, type); -} - -const mp_obj_type_t bleio_addresstype_type = { - { &mp_type_type }, - .name = MP_QSTR_AddressType, - .print = bleio_addresstype_print, - .locals_dict = (mp_obj_t)&bleio_addresstype_locals_dict, -}; diff --git a/shared-bindings/bleio/AddressType.h b/shared-bindings/bleio/AddressType.h deleted file mode 100644 index e69caffab..000000000 --- a/shared-bindings/bleio/AddressType.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 Artur Pacholec - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESSTYPE_H -#define MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESSTYPE_H - -#include "py/obj.h" - -typedef enum { - ADDRESS_PUBLIC, - ADDRESS_RANDOM_STATIC, - ADDRESS_RANDOM_PRIVATE_RESOLVABLE, - ADDRESS_RANDOM_PRIVATE_NON_RESOLVABLE -} bleio_address_type_t; - -extern const mp_obj_type_t bleio_addresstype_type; - -typedef struct { - mp_obj_base_t base; -} bleio_addresstype_obj_t; - -extern const bleio_addresstype_obj_t bleio_addresstype_public_obj; -extern const bleio_addresstype_obj_t bleio_addresstype_random_static_obj; -extern const bleio_addresstype_obj_t bleio_addresstype_random_private_resolvable_obj; -extern const bleio_addresstype_obj_t bleio_addresstype_random_private_non_resolvable_obj; - -#endif // MICROPY_INCLUDED_SHARED_BINDINGS_BLEIO_ADDRESSTYPE_H diff --git a/shared-bindings/bleio/CharacteristicBuffer.c b/shared-bindings/bleio/CharacteristicBuffer.c index c368de361..a1dc663fd 100644 --- a/shared-bindings/bleio/CharacteristicBuffer.c +++ b/shared-bindings/bleio/CharacteristicBuffer.c @@ -47,7 +47,7 @@ STATIC void raise_error_if_not_connected(bleio_characteristic_buffer_obj_t *self //| //| Accumulates a Characteristic's incoming values in a FIFO buffer. //| -//| .. class:: CharacteristicBuffer(Characteristic, *, timeout=1, buffer_size=64) +//| .. class:: CharacteristicBuffer(characteristic, *, timeout=1, buffer_size=64) //| //| Create a new Characteristic object identified by the specified UUID. //| @@ -92,6 +92,12 @@ STATIC mp_obj_t bleio_characteristic_buffer_make_new(const mp_obj_type_t *type, return MP_OBJ_FROM_PTR(self); } +STATIC void check_for_deinit(bleio_characteristic_buffer_obj_t *self) { + if (common_hal_bleio_characteristic_buffer_deinited(self)) { + raise_deinited_error(); + } +} + // These are standard stream methods. Code is in py/stream.c. // //| .. method:: read(nbytes=None) @@ -122,7 +128,7 @@ STATIC mp_obj_t bleio_characteristic_buffer_make_new(const mp_obj_type_t *type, // These three methods are used by the shared stream methods. STATIC mp_uint_t bleio_characteristic_buffer_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - raise_error_if_deinited(common_hal_bleio_characteristic_buffer_deinited(self)); + check_for_deinit(self); raise_error_if_not_connected(self); byte *buf = buf_in; @@ -141,7 +147,7 @@ STATIC mp_uint_t bleio_characteristic_buffer_write(mp_obj_t self_in, const void STATIC mp_uint_t bleio_characteristic_buffer_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - raise_error_if_deinited(common_hal_bleio_characteristic_buffer_deinited(self)); + check_for_deinit(self); raise_error_if_not_connected(self); if (!common_hal_bleio_characteristic_buffer_connected(self)) { mp_raise_ValueError(translate("Not connected")); @@ -170,7 +176,7 @@ STATIC mp_uint_t bleio_characteristic_buffer_ioctl(mp_obj_t self_in, mp_uint_t r //| STATIC mp_obj_t bleio_characteristic_buffer_obj_get_in_waiting(mp_obj_t self_in) { bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - raise_error_if_deinited(common_hal_bleio_characteristic_buffer_deinited(self)); + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_characteristic_buffer_rx_characters_available(self)); } MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_buffer_get_in_waiting_obj, bleio_characteristic_buffer_obj_get_in_waiting); @@ -188,7 +194,7 @@ const mp_obj_property_t bleio_characteristic_buffer_in_waiting_obj = { //| STATIC mp_obj_t bleio_characteristic_buffer_obj_reset_input_buffer(mp_obj_t self_in) { bleio_characteristic_buffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - raise_error_if_deinited(common_hal_bleio_characteristic_buffer_deinited(self)); + check_for_deinit(self); common_hal_bleio_characteristic_buffer_clear_rx_buffer(self); return mp_const_none; } diff --git a/shared-bindings/bleio/Peripheral.c b/shared-bindings/bleio/Peripheral.c index 1d657635f..ca703bd53 100644 --- a/shared-bindings/bleio/Peripheral.c +++ b/shared-bindings/bleio/Peripheral.c @@ -35,7 +35,6 @@ #include "py/runtime.h" #include "shared-bindings/bleio/Adapter.h" -#include "shared-bindings/bleio/AddressType.h" #include "shared-bindings/bleio/Characteristic.h" #include "shared-bindings/bleio/Peripheral.h" #include "shared-bindings/bleio/Service.h" diff --git a/shared-bindings/bleio/ScanEntry.c b/shared-bindings/bleio/ScanEntry.c index 67e4a38c0..475bade15 100644 --- a/shared-bindings/bleio/ScanEntry.c +++ b/shared-bindings/bleio/ScanEntry.c @@ -43,94 +43,21 @@ //| ========================================================= //| //| Encapsulates information about a device that was received as a -//| response to a BLE scan request. +//| response to a BLE scan request. This object may only be created +//| by a `bleio.Scanner`: it has no user-visible constructor. //| //| .. attribute:: address //| -//| The address of the device. (read-only) -//| This attribute is of type `bleio.Address`. +//| The address of the device (read-only), of type `bleio.Address`. //| - -//| .. attribute:: manufacturer_specific_data -//| -//| The manufacturer-specific data present in the advertisement packet. (read-only) -//| - -//| .. attribute:: name -//| -//| The name of the device. (read-only) -//| Will be be `None` if the data was missing from the advertisement packet. -//| - -//| .. attribute:: raw_data -//| -//| All the advertisement data present in the packet. (read-only) -//| - -//| .. attribute:: rssi -//| -//| The signal strength of the device at the time of the scan. (read-only) -//| - -//| .. attribute:: service_uuids -//| -//| The address of the device. (read-only) -//| This attribute is a list of `bleio.UUID`. -//| This attribute might be empty or incomplete, depending on the advertisement packet. -//| Currently only 16-bit UUIDS are listed. -//| - -//| .. attribute:: tx_power_level -//| -//| The transmit power level of the device. (read-only) -//| Will be `None` if the data was missing from the advertisement packet. -//| -static uint8_t find_data_item(mp_obj_array_t *data_in, uint8_t type, uint8_t **data_out) { - uint16_t i = 0; - while (i < data_in->len) { - const uint8_t item_len = ((uint8_t*)data_in->items)[i]; - const uint8_t item_type = ((uint8_t*)data_in->items)[i + 1]; - if (item_type != type) { - i += (item_len + 1); - continue; - } - - *data_out = &((uint8_t*)data_in->items)[i + 2]; - - return item_len; - } - - return 0; -} - -STATIC mp_obj_t scanentry_get_name(mp_obj_t self_in); - -STATIC void bleio_scanentry_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - bleio_scanentry_obj_t *self = (bleio_scanentry_obj_t *)self_in; - mp_printf(print, "ScanEntry(address: %02x:%02x:%02x:%02x:%02x:%02x", - self->address.value[5], self->address.value[4], self->address.value[3], - self->address.value[1], self->address.value[1], self->address.value[0]); - - const mp_obj_t name_obj = scanentry_get_name(self_in); - if (name_obj != mp_const_none) { - mp_obj_str_t *str = MP_OBJ_TO_PTR(name_obj); - mp_printf(print, " name: %s", str->data); - } - - mp_print_str(print, ")"); -} - STATIC mp_obj_t bleio_scanentry_get_address(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_t obj = bleio_address_type.make_new(&bleio_address_type, 1, 0, (mp_obj_t)&mp_const_none_obj); - bleio_address_obj_t *address = MP_OBJ_TO_PTR(obj); - + bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t); + memcpy(address->bytes, self->address.bytes, NUM_BLEIO_ADDRESS_BYTES); address->type = self->address.type; - memcpy(address->value, self->address.value, BLEIO_ADDRESS_BYTES); - - return obj; + return MP_OBJ_TO_PTR(address); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_address_obj, bleio_scanentry_get_address); @@ -141,75 +68,13 @@ const mp_obj_property_t bleio_scanentry_address_obj = { (mp_obj_t)&mp_const_none_obj }, }; -STATIC mp_obj_t scanentry_get_manufacturer_specific_data(mp_obj_t self_in) { - bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data); - uint8_t *manuf_data; - - const uint8_t manuf_data_len = find_data_item(data, AdManufacturerSpecificData, &manuf_data); - if (manuf_data_len == 0) { - return mp_const_none; - } - - return mp_obj_new_bytearray_by_ref(manuf_data_len, manuf_data); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(scanentry_get_manufacturer_specific_data_obj, scanentry_get_manufacturer_specific_data); - -const mp_obj_property_t bleio_scanentry_manufacturer_specific_data_obj = { - .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&scanentry_get_manufacturer_specific_data_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj }, -}; - -STATIC mp_obj_t scanentry_get_name(mp_obj_t self_in) { - bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data); - uint8_t *name; - - // Try for Complete but settle for Shortened - uint8_t name_len = find_data_item(data, AdCompleteLocalName, &name); - if (name_len == 0) { - name_len = find_data_item(data, AdShortenedLocalName, &name); - } - - if (name_len == 0) { - return mp_const_none; - } - - return mp_obj_new_str((const char*)name, name_len - 1); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(bluepy_scanentry_get_name_obj, scanentry_get_name); - -const mp_obj_property_t bleio_scanentry_name_obj = { - .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&bluepy_scanentry_get_name_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj }, -}; - +//| .. attribute:: raw_data +//| +//| All the advertisement data present in the packet, returned as a ``bytes`` object. (read-only) +//| STATIC mp_obj_t scanentry_get_raw_data(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - - mp_obj_t entries = mp_obj_new_list(0, NULL); - - mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data); - - uint16_t i = 0; - while (i < data->len) { - mp_obj_tuple_t *entry = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL)); - - const uint8_t item_len = ((uint8_t*)data->items)[i]; - const uint8_t item_type = ((uint8_t*)data->items)[i + 1]; - - entry->items[0] = MP_OBJ_NEW_SMALL_INT(item_type); - entry->items[1] = mp_obj_new_bytearray(item_len - 1, &((uint8_t*)data->items)[i + 2]); - mp_obj_list_append(entries, MP_OBJ_FROM_PTR(entry)); - - i += (item_len + 1); - } - - return entries; + return self->data; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_scanentry_get_raw_data_obj, scanentry_get_raw_data); @@ -220,6 +85,10 @@ const mp_obj_property_t bleio_scanentry_raw_data_obj = { (mp_obj_t)&mp_const_none_obj }, }; +//| .. attribute:: rssi +//| +//| The signal strength of the device at the time of the scan, in integer dBm. (read-only) +//| STATIC mp_obj_t scanentry_get_rssi(mp_obj_t self_in) { bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -234,68 +103,10 @@ const mp_obj_property_t bleio_scanentry_rssi_obj = { (mp_obj_t)&mp_const_none_obj }, }; -STATIC mp_obj_t scanentry_get_service_uuids(mp_obj_t self_in) { - bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data); - uint8_t *uuids; - - // Try for Complete but settle for Incomplete - uint8_t uuids_len = find_data_item(data, AdCompleteListOf16BitServiceClassUUIDs, &uuids); - if (uuids_len == 0) { - uuids_len = find_data_item(data, AdIncompleteListOf16BitServiceClassUUIDs, &uuids); - } - - mp_obj_t entries = mp_obj_new_list(0, NULL); - for (size_t i = 0; i < uuids_len / sizeof(uint16_t); ++i) { - const mp_obj_t uuid_int = mp_obj_new_int(uuids[sizeof(uint16_t) * i] | (uuids[sizeof(uint16_t) * i + 1] << 8)); - const mp_obj_t uuid_obj = bleio_uuid_type.make_new(&bleio_uuid_type, 1, &uuid_int, NULL); - - mp_obj_list_append(entries, uuid_obj); - } - - // TODO: 32-bit UUIDs - // TODO: 128-bit UUIDs - - return entries; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(scanentry_get_service_uuids_obj, scanentry_get_service_uuids); - -const mp_obj_property_t bleio_scanentry_service_uuids_obj = { - .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&scanentry_get_service_uuids_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj }, -}; - -STATIC mp_obj_t scanentry_get_tx_power_level(mp_obj_t self_in) { - bleio_scanentry_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_array_t *data = MP_OBJ_TO_PTR(self->data); - uint8_t *tx_power; - - const uint8_t tx_power_len = find_data_item(data, AdTxPowerLevel, &tx_power); - if (tx_power_len == 0) { - return mp_const_none; - } - - return mp_obj_new_int((int8_t)(*tx_power)); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(scanentry_get_tx_power_level_obj, scanentry_get_tx_power_level); - -const mp_obj_property_t bleio_scanentry_tx_power_level_obj = { - .base.type = &mp_type_property, - .proxy = { (mp_obj_t)&scanentry_get_tx_power_level_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj }, -}; - STATIC const mp_rom_map_elem_t bleio_scanentry_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) }, - { MP_ROM_QSTR(MP_QSTR_manufacturer_specific_data), MP_ROM_PTR(&bleio_scanentry_manufacturer_specific_data_obj) }, - { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_scanentry_name_obj) }, - { MP_ROM_QSTR(MP_QSTR_raw_data), MP_ROM_PTR(&bleio_scanentry_raw_data_obj) }, - { MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) }, - { MP_ROM_QSTR(MP_QSTR_service_uuids), MP_ROM_PTR(&bleio_scanentry_service_uuids_obj) }, - { MP_ROM_QSTR(MP_QSTR_tx_power_level), MP_ROM_PTR(&bleio_scanentry_tx_power_level_obj) }, + { MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_scanentry_address_obj) }, + { MP_ROM_QSTR(MP_QSTR_raw_data), MP_ROM_PTR(&bleio_scanentry_raw_data_obj) }, + { MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&bleio_scanentry_rssi_obj) }, }; STATIC MP_DEFINE_CONST_DICT(bleio_scanentry_locals_dict, bleio_scanentry_locals_dict_table); @@ -303,6 +114,5 @@ STATIC MP_DEFINE_CONST_DICT(bleio_scanentry_locals_dict, bleio_scanentry_locals_ const mp_obj_type_t bleio_scanentry_type = { { &mp_type_type }, .name = MP_QSTR_ScanEntry, - .print = bleio_scanentry_print, .locals_dict = (mp_obj_dict_t*)&bleio_scanentry_locals_dict }; diff --git a/shared-bindings/bleio/Scanner.c b/shared-bindings/bleio/Scanner.c index 5615ce103..a249d5343 100644 --- a/shared-bindings/bleio/Scanner.c +++ b/shared-bindings/bleio/Scanner.c @@ -80,8 +80,8 @@ STATIC mp_obj_t bleio_scanner_scan(size_t n_args, const mp_obj_t *pos_args, mp_m enum { ARG_timeout, ARG_interval, ARG_window }; static const mp_arg_t allowed_args[] = { { MP_QSTR_timeout, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_OBJ_NULL} }, - { MP_QSTR_window, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { MP_QSTR_window, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, }; bleio_scanner_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); diff --git a/shared-bindings/bleio/UUID.c b/shared-bindings/bleio/UUID.c index d054ea719..f79dfa675 100644 --- a/shared-bindings/bleio/UUID.c +++ b/shared-bindings/bleio/UUID.c @@ -47,10 +47,11 @@ //| The value can be one of: //| //| - 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 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 value: The uuid value to encapsulate +//| :type value: int or typing.ByteString //| 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); @@ -125,6 +126,8 @@ STATIC mp_obj_t bleio_uuid_make_new(const mp_obj_type_t *type, size_t n_args, co //| //| The 16-bit part of the UUID. (read-only) //| +//| :type: int +//| STATIC mp_obj_t bleio_uuid_get_uuid16(mp_obj_t self_in) { bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_uuid_get_uuid16(self)); @@ -141,9 +144,11 @@ const mp_obj_property_t bleio_uuid_uuid16_obj = { //| .. attribute:: uuid128 //| -//| The 128-bit value of the UUID, returned as bytes. +//| The 128-bit value of the UUID //| Raises AttributeError if this is a 16-bit UUID. (read-only) //| +//| :type: bytes +//| STATIC mp_obj_t bleio_uuid_get_uuid128(mp_obj_t self_in) { bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -165,9 +170,10 @@ const mp_obj_property_t bleio_uuid_uuid128_obj = { //| .. attribute:: size //| -//| Returns 128 if this UUID represents a 128-bit vendor-specific UUID. -//| Returns 16 if this UUID represents a 16-bit Bluetooth SIG assigned UUID. (read-only) -//| 32-bit UUIDs are not currently supported. +//| 128 if this UUID represents a 128-bit vendor-specific UUID. 16 if this UUID represents a +//| 16-bit Bluetooth SIG assigned UUID. (read-only) 32-bit UUIDs are not currently supported. +//| +//| :type: int //| STATIC mp_obj_t bleio_uuid_get_size(mp_obj_t self_in) { bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); diff --git a/shared-bindings/bleio/__init__.c b/shared-bindings/bleio/__init__.c index 4b98e4673..06c00d588 100644 --- a/shared-bindings/bleio/__init__.c +++ b/shared-bindings/bleio/__init__.c @@ -27,7 +27,6 @@ #include "shared-bindings/bleio/__init__.h" #include "shared-bindings/bleio/Address.h" -#include "shared-bindings/bleio/AddressType.h" #include "shared-bindings/bleio/Characteristic.h" #include "shared-bindings/bleio/CharacteristicBuffer.h" #include "shared-bindings/bleio/Descriptor.h" @@ -52,7 +51,6 @@ //| :maxdepth: 3 //| //| Address -//| AddressType //| AdvertisementData //| Adapter //| Characteristic @@ -87,9 +85,6 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = { // Properties { MP_ROM_QSTR(MP_QSTR_adapter), MP_ROM_PTR(&common_hal_bleio_adapter_obj) }, - - // Enum-like Classes. - { MP_ROM_QSTR(MP_QSTR_AddressType), MP_ROM_PTR(&bleio_addresstype_type) }, }; STATIC MP_DEFINE_CONST_DICT(bleio_module_globals, bleio_module_globals_table); |
