From 56c898da80df57d0e83f4e8d1ee44f4351ce1d8c Mon Sep 17 00:00:00 2001 From: Taku Fukada Date: Mon, 3 Aug 2020 13:35:43 +0900 Subject: Modify some Python stubs --- shared-bindings/_bleio/Adapter.c | 8 ++++---- shared-bindings/_bleio/Address.c | 4 ++-- shared-bindings/_bleio/Characteristic.c | 2 +- shared-bindings/_bleio/Connection.c | 8 ++++---- shared-bindings/_bleio/Descriptor.c | 4 ++-- shared-bindings/_bleio/UUID.c | 4 ++-- shared-bindings/_bleio/__init__.c | 13 +++++++------ 7 files changed, 22 insertions(+), 21 deletions(-) (limited to 'shared-bindings/_bleio') diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c index f898d404a..7a44d560f 100644 --- a/shared-bindings/_bleio/Adapter.c +++ b/shared-bindings/_bleio/Adapter.c @@ -145,8 +145,8 @@ const mp_obj_property_t bleio_adapter_name_obj = { //| .. note: If you set ``anonymous=True``, then a timeout must be specified. If no timeout is //| specified, then the maximum allowed timeout will be selected automatically. //| -//| :param buf data: advertising data packet bytes -//| :param buf scan_response: scan response data packet bytes. ``None`` if no scan response is needed. +//| :param ~_typing.ReadableBuffer data: advertising data packet bytes +//| :param ~_typing.ReadableBuffer scan_response: scan response data packet bytes. ``None`` if no scan response is needed. //| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral. //| :param bool anonymous: If `True` then this device's MAC address is randomized before advertising. //| :param int timeout: If set, we will only advertise for this many seconds. @@ -219,7 +219,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_stop_advertising_obj, bleio_adapt //| """Starts a BLE scan and returns an iterator of results. Advertisements and scan responses are //| filtered and returned separately. //| -//| :param sequence prefixes: Sequence of byte string prefixes to filter advertising packets +//| :param ~_typing.ReadableBuffer prefixes: Sequence of byte string prefixes to filter advertising packets //| with. A packet without an advertising structure that matches one of the prefixes is //| ignored. Format is one byte for length (n) and n bytes of prefix and can be repeated. //| :param int buffer_size: the maximum number of advertising bytes to buffer. @@ -334,7 +334,7 @@ const mp_obj_property_t bleio_adapter_connected_obj = { (mp_obj_t)&mp_const_none_obj }, }; -//| connections: tuple +//| connections: Tuple[Connection] //| """Tuple of active connections including those initiated through //| :py:meth:`_bleio.Adapter.connect`. (read-only)""" //| diff --git a/shared-bindings/_bleio/Address.c b/shared-bindings/_bleio/Address.c index 94994fb70..1f168d31a 100644 --- a/shared-bindings/_bleio/Address.c +++ b/shared-bindings/_bleio/Address.c @@ -42,7 +42,7 @@ //| """Create a new Address object encapsulating the address value. //| The value itself can be one of: //| -//| :param buf address: The address value to encapsulate. A buffer object (bytearray, bytes) of 6 bytes. +//| :param ~_typing.ReadableBuffer address: The address value to encapsulate. A buffer object (bytearray, bytes) of 6 bytes. //| :param int address_type: one of the integer values: `PUBLIC`, `RANDOM_STATIC`, //| `RANDOM_PRIVATE_RESOLVABLE`, or `RANDOM_PRIVATE_NON_RESOLVABLE`.""" //| ... @@ -128,7 +128,7 @@ const mp_obj_property_t bleio_address_type_obj = { (mp_obj_t)&mp_const_none_obj}, }; -//| def __eq__(self, other: Address) -> bool: +//| def __eq__(self, other: object) -> bool: //| """Two Address objects are equal if their addresses and address types are equal.""" //| ... //| diff --git a/shared-bindings/_bleio/Characteristic.c b/shared-bindings/_bleio/Characteristic.c index 557a356b6..dbbacb7d5 100644 --- a/shared-bindings/_bleio/Characteristic.c +++ b/shared-bindings/_bleio/Characteristic.c @@ -63,7 +63,7 @@ //| is 512, or possibly 510 if ``fixed_length`` is False. The default, 20, is the maximum //| number of data bytes that fit in a single BLE 4.x ATT packet. //| :param bool fixed_length: True if the characteristic value is of fixed length. -//| :param buf initial_value: The initial value for this characteristic. If not given, will be +//| :param ~_typing.ReadableBuffer initial_value: The initial value for this characteristic. If not given, will be //| filled with zeros. //| //| :return: the new Characteristic.""" diff --git a/shared-bindings/_bleio/Connection.c b/shared-bindings/_bleio/Connection.c index ed480ec04..5d1b63bdd 100644 --- a/shared-bindings/_bleio/Connection.c +++ b/shared-bindings/_bleio/Connection.c @@ -111,12 +111,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_connection_pair_obj, 1, bleio_connection //| def discover_remote_services(self, service_uuids_whitelist: Optional[Iterable[UUID]] = None) -> Tuple[Service, ...]: //| """Do BLE discovery for all services or for the given service UUIDS, -//| to find their handles and characteristics, and return the discovered services. -//| `Connection.connected` must be True. +//| to find their handles and characteristics, and return the discovered services. +//| `Connection.connected` must be True. //| //| :param iterable service_uuids_whitelist: //| -//| an iterable of :py:class:~`UUID` objects for the services provided by the peripheral +//| an iterable of :py:class:`UUID` objects for the services provided by the peripheral //| that you want to use. //| //| The peripheral may provide more services, but services not listed are ignored @@ -126,7 +126,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_connection_pair_obj, 1, bleio_connection //| slow. //| //| If the service UUID is 128-bit, or its characteristic UUID's are 128-bit, you -//| you must have already created a :py:class:~`UUID` object for that UUID in order for the +//| you must have already created a :py:class:`UUID` object for that UUID in order for the //| service or characteristic to be discovered. Creating the UUID causes the UUID to be //| registered for use. (This restriction may be lifted in the future.) //| diff --git a/shared-bindings/_bleio/Descriptor.c b/shared-bindings/_bleio/Descriptor.c index e24751f75..97422f163 100644 --- a/shared-bindings/_bleio/Descriptor.c +++ b/shared-bindings/_bleio/Descriptor.c @@ -46,7 +46,7 @@ //| as part of remote Characteristics in the remote Services that are discovered.""" //| //| @classmethod -//| def add_to_characteristic(characteristic: Characteristic, uuid: UUID, *, read_perm: int = Attribute.OPEN, write_perm: int = Attribute.OPEN, max_length = 20, fixed_length: bool = False, initial_value: ReadableBuffer = b'') -> Descriptor: +//| def add_to_characteristic(cls, characteristic: Characteristic, uuid: UUID, *, read_perm: int = Attribute.OPEN, write_perm: int = Attribute.OPEN, max_length: int = 20, fixed_length: bool = False, initial_value: ReadableBuffer = b'') -> Descriptor: //| """Create a new Descriptor object, and add it to this Service. //| //| :param Characteristic characteristic: The characteristic that will hold this descriptor @@ -61,7 +61,7 @@ //| is 512, or possibly 510 if ``fixed_length`` is False. The default, 20, is the maximum //| number of data bytes that fit in a single BLE 4.x ATT packet. //| :param bool fixed_length: True if the descriptor value is of fixed length. -//| :param buf initial_value: The initial value for this descriptor. +//| :param ~_typing.ReadableBuffer initial_value: The initial value for this descriptor. //| //| :return: the new Descriptor.""" //| ... diff --git a/shared-bindings/_bleio/UUID.c b/shared-bindings/_bleio/UUID.c index 6d92cf793..18eda0b33 100644 --- a/shared-bindings/_bleio/UUID.c +++ b/shared-bindings/_bleio/UUID.c @@ -48,7 +48,7 @@ //| temporary 16-bit UUID that can be used in place of the full 128-bit UUID. //| //| :param value: The uuid value to encapsulate -//| :type value: int or typing.ByteString""" +//| :type value: int, ~_typing.ReadableBuffer or str""" //| ... //| 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) { @@ -248,7 +248,7 @@ STATIC mp_obj_t bleio_uuid_unary_op(mp_unary_op_t op, mp_obj_t self_in) { } } -//| def __eq__(self, other: UUID) -> bool: +//| def __eq__(self, other: object) -> bool: //| """Two UUID objects are equal if their values match and they are both 128-bit or both 16-bit.""" //| ... //| diff --git a/shared-bindings/_bleio/__init__.c b/shared-bindings/_bleio/__init__.c index 3002ecd18..ee9c4bf4a 100644 --- a/shared-bindings/_bleio/__init__.c +++ b/shared-bindings/_bleio/__init__.c @@ -41,7 +41,8 @@ #include "shared-bindings/_bleio/Service.h" #include "shared-bindings/_bleio/UUID.h" -//| """ +//| """Bluetooth Low Energy (BLE) communication +//| //| The `_bleio` module provides necessary low-level functionality for communicating //| using Bluetooth Low Energy (BLE). The '_' prefix indicates this module is meant //| for internal use by libraries but not by the end user. Its API may change incompatibly @@ -50,12 +51,12 @@ //| `adafruit_ble `_ //| CircuitPython library instead, which builds on `_bleio`, and //| provides higher-level convenience functionality, including predefined beacons, clients, -//| servers. -//| -//| .. attribute:: adapter +//| servers.""" //| -//| BLE Adapter used to manage device discovery and connections. -//| This object is the sole instance of `_bleio.Adapter`.""" + +//| adapter: Adapter +//| """BLE Adapter used to manage device discovery and connections. +//| This object is the sole instance of `_bleio.Adapter`.""" //| //| class BluetoothError(Exception): -- cgit v1.2.3 From e7da652711f8d7ccdffce1a4da2a25aaf61ae150 Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Fri, 7 Aug 2020 23:01:49 +0800 Subject: able to change ble mac address --- ports/nrf/common-hal/_bleio/Adapter.c | 9 +++++++++ shared-bindings/_bleio/Adapter.c | 10 ++++++++-- shared-bindings/_bleio/Adapter.h | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) (limited to 'shared-bindings/_bleio') diff --git a/ports/nrf/common-hal/_bleio/Adapter.c b/ports/nrf/common-hal/_bleio/Adapter.c index 0b23bb7bf..d34f5d287 100644 --- a/ports/nrf/common-hal/_bleio/Adapter.c +++ b/ports/nrf/common-hal/_bleio/Adapter.c @@ -386,6 +386,15 @@ bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *s return address; } +uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) +{ + ble_gap_addr_t local_address; + local_address.addr_type = address->type; + const char *data = mp_obj_str_get_str(address->bytes); + memcpy(local_address.addr, data, NUM_BLEIO_ADDRESS_BYTES); + return sd_ble_gap_addr_set(&local_address); +} + mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self) { uint16_t len = 0; sd_ble_gap_device_name_get(NULL, &len); diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c index 7a44d560f..861b640b5 100644 --- a/shared-bindings/_bleio/Adapter.c +++ b/shared-bindings/_bleio/Adapter.c @@ -96,7 +96,7 @@ const mp_obj_property_t bleio_adapter_enabled_obj = { }; //| address: Address -//| """MAC address of the BLE adapter. (read-only)""" +//| """MAC address of the BLE adapter.""" //| STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) { return MP_OBJ_FROM_PTR(common_hal_bleio_adapter_get_address(self)); @@ -104,10 +104,16 @@ STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) { } MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address); +STATIC mp_obj_t bleio_adapter_set_address(mp_obj_t self, mp_obj_t new_address) { + common_hal_bleio_adapter_set_address(self, new_address); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_address_obj, bleio_adapter_set_address); + const mp_obj_property_t bleio_adapter_address_obj = { .base.type = &mp_type_property, .proxy = { (mp_obj_t)&bleio_adapter_get_address_obj, - (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&bleio_adapter_set_address_obj, (mp_obj_t)&mp_const_none_obj }, }; diff --git a/shared-bindings/_bleio/Adapter.h b/shared-bindings/_bleio/Adapter.h index 39147b6eb..ad84bf9aa 100644 --- a/shared-bindings/_bleio/Adapter.h +++ b/shared-bindings/_bleio/Adapter.h @@ -42,6 +42,7 @@ extern bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled); extern bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self); extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self); +extern uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address); extern mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char* name); -- cgit v1.2.3 From 52990a332d602db17564ae3b1d27444f25f4531e Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Fri, 14 Aug 2020 17:48:15 +0800 Subject: fix --- ports/nrf/common-hal/_bleio/Adapter.c | 12 +++++++----- shared-bindings/_bleio/Adapter.c | 4 +++- shared-bindings/_bleio/Adapter.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) (limited to 'shared-bindings/_bleio') diff --git a/ports/nrf/common-hal/_bleio/Adapter.c b/ports/nrf/common-hal/_bleio/Adapter.c index d34f5d287..6d6669c5e 100644 --- a/ports/nrf/common-hal/_bleio/Adapter.c +++ b/ports/nrf/common-hal/_bleio/Adapter.c @@ -386,13 +386,15 @@ bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *s return address; } -uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) -{ +bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) { ble_gap_addr_t local_address; + mp_buffer_info_t bufinfo; + if (!mp_get_buffer(address->bytes, &bufinfo, MP_BUFFER_READ)) { + return false; + } local_address.addr_type = address->type; - const char *data = mp_obj_str_get_str(address->bytes); - memcpy(local_address.addr, data, NUM_BLEIO_ADDRESS_BYTES); - return sd_ble_gap_addr_set(&local_address); + memcpy(local_address.addr, bufinfo.buf, NUM_BLEIO_ADDRESS_BYTES); + return sd_ble_gap_addr_set(&local_address) == NRF_SUCCESS; } mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self) { diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c index 861b640b5..3ffe65be3 100644 --- a/shared-bindings/_bleio/Adapter.c +++ b/shared-bindings/_bleio/Adapter.c @@ -105,7 +105,9 @@ STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) { MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address); STATIC mp_obj_t bleio_adapter_set_address(mp_obj_t self, mp_obj_t new_address) { - common_hal_bleio_adapter_set_address(self, new_address); + if (!common_hal_bleio_adapter_set_address(self, new_address)) { + mp_raise_bleio_BluetoothError(translate("Could not set address")); + } return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_address_obj, bleio_adapter_set_address); diff --git a/shared-bindings/_bleio/Adapter.h b/shared-bindings/_bleio/Adapter.h index ad84bf9aa..352373357 100644 --- a/shared-bindings/_bleio/Adapter.h +++ b/shared-bindings/_bleio/Adapter.h @@ -42,7 +42,7 @@ extern bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled); extern bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self); extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self); -extern uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address); +extern bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address); extern mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char* name); -- cgit v1.2.3