2018-07-08 15:13:05 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
|
2018-07-08 15:13:05 -04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
2018-07-08 15:13:05 -04:00
|
|
|
#include "py/objproperty.h"
|
2019-09-14 15:40:24 -04:00
|
|
|
#include "py/runtime.h"
|
2019-12-12 16:46:51 -05:00
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/Address.h"
|
|
|
|
#include "shared-bindings/_bleio/Adapter.h"
|
2018-07-08 15:13:05 -04:00
|
|
|
|
2021-01-10 13:16:19 -05:00
|
|
|
#define ADV_INTERVAL_MIN (0.02f)
|
|
|
|
#define ADV_INTERVAL_MIN_STRING "0.02"
|
2019-09-14 15:40:24 -04:00
|
|
|
#define ADV_INTERVAL_MAX (10.24f)
|
|
|
|
#define ADV_INTERVAL_MAX_STRING "10.24"
|
|
|
|
// 20ms is recommended by Apple
|
|
|
|
#define ADV_INTERVAL_DEFAULT (0.1f)
|
|
|
|
|
|
|
|
#define INTERVAL_DEFAULT (0.1f)
|
|
|
|
#define INTERVAL_MIN (0.0025f)
|
|
|
|
#define INTERVAL_MIN_STRING "0.0025"
|
|
|
|
#define INTERVAL_MAX (40.959375f)
|
|
|
|
#define INTERVAL_MAX_STRING "40.959375"
|
|
|
|
#define WINDOW_DEFAULT (0.1f)
|
|
|
|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| class Adapter:
|
2020-08-30 14:06:48 -04:00
|
|
|
//| """
|
|
|
|
//| The BLE Adapter object manages the discovery and connection to other nearby Bluetooth Low Energy devices.
|
2020-05-01 18:23:27 -04:00
|
|
|
//| This part of the Bluetooth Low Energy Specification is known as Generic Access Profile (GAP).
|
2018-07-08 15:13:05 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| Discovery of other devices happens during a scanning process that listens for small packets of
|
|
|
|
//| information, known as advertisements, that are broadcast unencrypted. The advertising packets
|
|
|
|
//| have two different uses. The first is to broadcast a small piece of data to anyone who cares and
|
2020-08-30 14:06:48 -04:00
|
|
|
//| and nothing more. These are known as beacons. The second class of advertisement is to promote
|
2020-05-01 18:23:27 -04:00
|
|
|
//| additional functionality available after the devices establish a connection. For example, a
|
2020-08-30 14:06:48 -04:00
|
|
|
//| BLE heart rate monitor would advertise that it provides the standard BLE Heart Rate Service.
|
2018-07-08 15:13:05 -04:00
|
|
|
//|
|
2020-08-30 14:06:48 -04:00
|
|
|
//| The Adapter can do both parts of this process: it can scan for other device
|
2020-05-01 18:23:27 -04:00
|
|
|
//| advertisements and it can advertise its own data. Furthermore, Adapters can accept incoming
|
|
|
|
//| connections and also initiate connections."""
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2018-07-08 15:13:05 -04:00
|
|
|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def __init__(
|
|
|
|
//| self, *, uart: busio.UART, rts: digitalio.DigitalInOut, cts: digitalio.DigitalInOut
|
|
|
|
//| ) -> None:
|
2020-08-30 14:06:48 -04:00
|
|
|
//| """On boards that do not have native BLE, you can use an HCI co-processor.
|
2020-08-21 17:03:45 -04:00
|
|
|
//| Pass the uart and pins used to communicate with the co-processor, such as an Adafruit AirLift.
|
2020-06-26 19:27:05 -04:00
|
|
|
//| The co-processor must have been reset and put into BLE mode beforehand
|
|
|
|
//| by the appropriate pin manipulation.
|
2020-08-21 17:03:45 -04:00
|
|
|
//| The ``uart``, ``rts``, and ``cts`` objects are used to
|
2020-07-08 09:33:41 -04:00
|
|
|
//| communicate with the HCI co-processor in HCI mode.
|
2020-08-30 14:06:48 -04:00
|
|
|
//| The `Adapter` object is enabled during this call.
|
2020-06-25 20:57:17 -04:00
|
|
|
//|
|
2020-09-01 12:41:58 -04:00
|
|
|
//| After instantiating an Adapter, call `_bleio.set_adapter()` to set `_bleio.adapter`
|
2020-07-15 09:46:13 -04:00
|
|
|
//|
|
2020-08-30 14:06:48 -04:00
|
|
|
//| On boards with native BLE, you cannot create an instance of `_bleio.Adapter`;
|
|
|
|
//| this constructor will raise `NotImplementedError`.
|
2020-08-30 14:59:03 -04:00
|
|
|
//| Use `_bleio.adapter` to access the sole instance already available.
|
2020-08-21 11:37:15 -04:00
|
|
|
//| """
|
|
|
|
//| ...
|
2021-10-15 14:43:12 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if CIRCUITPY_BLEIO_HCI
|
2020-08-30 14:06:48 -04:00
|
|
|
bleio_adapter_obj_t *self = common_hal_bleio_allocate_adapter_or_raise();
|
2020-06-26 19:27:05 -04:00
|
|
|
|
2020-07-08 09:33:41 -04:00
|
|
|
enum { ARG_uart, ARG_rts, ARG_cts };
|
2020-06-25 20:57:17 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2020-07-08 09:33:41 -04:00
|
|
|
{ MP_QSTR_uart, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
|
2020-06-25 20:57:17 -04:00
|
|
|
{ MP_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
};
|
2018-07-08 15:13:05 -04:00
|
|
|
|
2020-06-25 20:57:17 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2021-10-15 14:43:12 -04:00
|
|
|
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-06-26 19:27:05 -04:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
busio_uart_obj_t *uart = mp_arg_validate_type(args[ARG_uart].u_obj, &busio_uart_type, MP_QSTR_uart);
|
2020-06-26 19:27:05 -04:00
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
digitalio_digitalinout_obj_t *rts = mp_arg_validate_type(args[ARG_rts].u_obj, &digitalio_digitalinout_type, MP_QSTR_rts);
|
|
|
|
digitalio_digitalinout_obj_t *cts = mp_arg_validate_type(args[ARG_cts].u_obj, &digitalio_digitalinout_type, MP_QSTR_cts);
|
2020-07-15 09:46:13 -04:00
|
|
|
|
|
|
|
// Will enable the adapter.
|
2020-08-30 14:06:48 -04:00
|
|
|
common_hal_bleio_adapter_construct_hci_uart(self, uart, rts, cts);
|
2020-06-30 23:19:40 -04:00
|
|
|
|
2020-08-30 14:06:48 -04:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
2021-03-15 09:57:36 -04:00
|
|
|
#else
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_NotImplementedError(MP_ERROR_TEXT("Cannot create a new Adapter; use _bleio.adapter;"));
|
2020-06-26 19:27:05 -04:00
|
|
|
return mp_const_none;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif // CIRCUITPY_BLEIO_HCI
|
2020-06-25 20:57:17 -04:00
|
|
|
}
|
2020-06-26 19:27:05 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| enabled: bool
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """State of the BLE adapter."""
|
2018-07-08 15:13:05 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_get_enabled(mp_obj_t self) {
|
2019-09-14 15:40:24 -04:00
|
|
|
return mp_obj_new_bool(common_hal_bleio_adapter_get_enabled(self));
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_enabled_obj, bleio_adapter_get_enabled);
|
|
|
|
|
|
|
|
static mp_obj_t bleio_adapter_set_enabled(mp_obj_t self, mp_obj_t value) {
|
|
|
|
const bool enabled = mp_obj_is_true(value);
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
common_hal_bleio_adapter_set_enabled(self, enabled);
|
2018-07-08 15:13:05 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_enabled_obj, bleio_adapter_set_enabled);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(bleio_adapter_enabled_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_adapter_get_enabled_obj,
|
|
|
|
(mp_obj_t)&bleio_adapter_set_enabled_obj);
|
2018-07-08 15:13:05 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| address: Address
|
2020-08-07 11:01:49 -04:00
|
|
|
//| """MAC address of the BLE adapter."""
|
2018-07-08 15:13:05 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) {
|
2019-09-14 15:40:24 -04:00
|
|
|
return MP_OBJ_FROM_PTR(common_hal_bleio_adapter_get_address(self));
|
2018-07-08 15:13:05 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address);
|
|
|
|
|
2020-08-07 11:01:49 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_set_address(mp_obj_t self, mp_obj_t new_address) {
|
2020-08-14 05:48:15 -04:00
|
|
|
if (!common_hal_bleio_adapter_set_address(self, new_address)) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Could not set address"));
|
2020-08-14 05:48:15 -04:00
|
|
|
}
|
2020-08-07 11:01:49 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_address_obj, bleio_adapter_set_address);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(bleio_adapter_address_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_adapter_get_address_obj,
|
|
|
|
(mp_obj_t)&bleio_adapter_set_address_obj);
|
2018-07-08 15:13:05 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| name: str
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """name of the BLE adapter used once connected.
|
|
|
|
//| The name is "CIRCUITPY" + the last four hex digits of ``adapter.address``,
|
|
|
|
//| to make it easy to distinguish multiple CircuitPython boards."""
|
2022-08-03 17:35:28 -04:00
|
|
|
STATIC mp_obj_t _bleio_adapter_get_name(mp_obj_t self) {
|
2019-09-14 15:40:24 -04:00
|
|
|
return MP_OBJ_FROM_PTR(common_hal_bleio_adapter_get_name(self));
|
|
|
|
}
|
2022-08-03 17:35:28 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_name_obj, _bleio_adapter_get_name);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
|
|
|
|
STATIC mp_obj_t bleio_adapter_set_name(mp_obj_t self, mp_obj_t new_name) {
|
|
|
|
common_hal_bleio_adapter_set_name(self, mp_obj_str_get_str(new_name));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_name_obj, bleio_adapter_set_name);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETSET(bleio_adapter_name_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_adapter_get_name_obj,
|
|
|
|
(mp_obj_t)&bleio_adapter_set_name_obj);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def start_advertising(
|
|
|
|
//| self,
|
|
|
|
//| data: ReadableBuffer,
|
|
|
|
//| *,
|
|
|
|
//| scan_response: Optional[ReadableBuffer] = None,
|
|
|
|
//| connectable: bool = True,
|
|
|
|
//| anonymous: bool = False,
|
|
|
|
//| timeout: int = 0,
|
|
|
|
//| interval: float = 0.1,
|
|
|
|
//| tx_power: int = 0,
|
|
|
|
//| directed_to: Optional[Address] = None
|
|
|
|
//| ) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Starts advertising until `stop_advertising` is called or if connectable, another device
|
|
|
|
//| connects to us.
|
2019-09-14 15:40:24 -04:00
|
|
|
//|
|
2021-08-12 14:15:04 -04:00
|
|
|
//| .. warning:: If data is longer than 31 bytes, then this will automatically advertise as an
|
2020-05-01 18:23:27 -04:00
|
|
|
//| extended advertisement that older BLE 4.x clients won't be able to scan for.
|
2019-11-19 16:55:58 -05:00
|
|
|
//|
|
2021-08-12 14:15:04 -04:00
|
|
|
//| .. note:: If you set ``anonymous=True``, then a timeout must be specified. If no timeout is
|
2020-05-19 02:56:04 -04:00
|
|
|
//| specified, then the maximum allowed timeout will be selected automatically.
|
|
|
|
//|
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer data: advertising data packet bytes
|
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer scan_response: scan response data packet bytes. ``None`` if no scan response is needed.
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral.
|
2020-05-19 02:56:04 -04:00
|
|
|
//| :param bool anonymous: If `True` then this device's MAC address is randomized before advertising.
|
2021-01-10 13:16:19 -05:00
|
|
|
//| :param int timeout: If set, we will only advertise for this many seconds. Zero means no timeout.
|
2021-05-19 15:21:48 -04:00
|
|
|
//| :param float interval: advertising interval, in seconds
|
2021-05-19 19:22:07 -04:00
|
|
|
//| :param tx_power int: transmitter power while advertising in dBm
|
|
|
|
//| :param directed_to Address: peer to advertise directly to"""
|
2020-05-01 18:23:27 -04:00
|
|
|
//| ...
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
enum { ARG_data, ARG_scan_response, ARG_connectable, ARG_anonymous, ARG_timeout, ARG_interval, ARG_tx_power, ARG_directed_to };
|
2019-09-14 15:40:24 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_scan_response, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
|
|
|
{ MP_QSTR_connectable, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
2020-05-17 10:06:21 -04:00
|
|
|
{ MP_QSTR_anonymous, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2020-05-19 02:56:04 -04:00
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
2019-09-14 15:40:24 -04:00
|
|
|
{ MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
2021-05-19 15:21:48 -04:00
|
|
|
{ MP_QSTR_tx_power, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
2021-10-24 22:46:14 -04:00
|
|
|
{ MP_QSTR_directed_to, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2019-09-14 15:40:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
|
|
|
mp_buffer_info_t data_bufinfo;
|
|
|
|
mp_get_buffer_raise(args[ARG_data].u_obj, &data_bufinfo, MP_BUFFER_READ);
|
|
|
|
|
|
|
|
// Pass an empty buffer if scan_response not provided.
|
|
|
|
mp_buffer_info_t scan_response_bufinfo = { 0 };
|
|
|
|
if (args[ARG_scan_response].u_obj != mp_const_none) {
|
|
|
|
mp_get_buffer_raise(args[ARG_scan_response].u_obj, &scan_response_bufinfo, MP_BUFFER_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[ARG_interval].u_obj == MP_OBJ_NULL) {
|
|
|
|
args[ARG_interval].u_obj = mp_obj_new_float(ADV_INTERVAL_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:16:19 -05:00
|
|
|
const mp_float_t interval = mp_obj_get_float(args[ARG_interval].u_obj);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (interval < ADV_INTERVAL_MIN || interval > ADV_INTERVAL_MAX) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError_varg(MP_ERROR_TEXT("interval must be in range %s-%s"),
|
2021-03-15 09:57:36 -04:00
|
|
|
ADV_INTERVAL_MIN_STRING, ADV_INTERVAL_MAX_STRING);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
2019-12-12 16:46:51 -05:00
|
|
|
bool connectable = args[ARG_connectable].u_bool;
|
2020-05-17 10:06:21 -04:00
|
|
|
bool anonymous = args[ARG_anonymous].u_bool;
|
2020-05-19 02:56:04 -04:00
|
|
|
uint32_t timeout = args[ARG_timeout].u_int;
|
2019-12-12 16:46:51 -05:00
|
|
|
if (data_bufinfo.len > 31 && connectable && scan_response_bufinfo.len > 0) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Cannot have scan responses for extended, connectable advertisements."));
|
2019-12-12 16:46:51 -05:00
|
|
|
}
|
|
|
|
|
2021-10-24 22:46:14 -04:00
|
|
|
const bleio_address_obj_t *address = NULL;
|
|
|
|
if (args[ARG_directed_to].u_obj != mp_const_none) {
|
|
|
|
if (!connectable) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_bleio_BluetoothError(MP_ERROR_TEXT("Only connectable advertisements can be directed"));
|
2021-10-24 22:46:14 -04:00
|
|
|
}
|
|
|
|
address = mp_arg_validate_type(args[ARG_directed_to].u_obj, &bleio_address_type, MP_QSTR_directed_to);
|
2021-05-19 19:22:07 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 02:56:04 -04:00
|
|
|
common_hal_bleio_adapter_start_advertising(self, connectable, anonymous, timeout, interval,
|
2021-05-19 19:22:07 -04:00
|
|
|
&data_bufinfo, &scan_response_bufinfo, args[ARG_tx_power].u_int, address);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2021-10-07 16:36:06 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_start_advertising_obj, 1, bleio_adapter_start_advertising);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def stop_advertising(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Stop sending advertising packets."""
|
|
|
|
//| ...
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_stop_advertising(mp_obj_t self_in) {
|
|
|
|
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_stop_advertising_obj, bleio_adapter_stop_advertising);
|
|
|
|
|
2022-09-27 16:21:42 -04:00
|
|
|
//| def start_scan(
|
|
|
|
//| self,
|
|
|
|
//| prefixes: ReadableBuffer = b"",
|
|
|
|
//| *,
|
|
|
|
//| buffer_size: int = 512,
|
|
|
|
//| extended: bool = False,
|
|
|
|
//| timeout: Optional[float] = None,
|
|
|
|
//| interval: float = 0.1,
|
|
|
|
//| window: float = 0.1,
|
|
|
|
//| minimum_rssi: int = -80,
|
|
|
|
//| active: bool = True
|
|
|
|
//| ) -> Iterable[ScanEntry]:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Starts a BLE scan and returns an iterator of results. Advertisements and scan responses are
|
|
|
|
//| filtered and returned separately.
|
2019-09-14 15:40:24 -04:00
|
|
|
//|
|
2021-12-21 15:28:49 -05:00
|
|
|
//| :param ~circuitpython_typing.ReadableBuffer prefixes: Sequence of byte string prefixes to filter advertising packets
|
2020-05-01 18:23:27 -04:00
|
|
|
//| 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.
|
|
|
|
//| :param bool extended: When True, support extended advertising packets. Increasing buffer_size is recommended when this is set.
|
2021-01-10 13:16:19 -05:00
|
|
|
//| :param float timeout: the scan timeout in seconds. If None or zero, will scan until `stop_scan` is called.
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param float interval: the interval (in seconds) between the start of two consecutive scan windows
|
|
|
|
//| Must be in the range 0.0025 - 40.959375 seconds.
|
|
|
|
//| :param float window: the duration (in seconds) to scan a single BLE channel.
|
|
|
|
//| window must be <= interval.
|
|
|
|
//| :param int minimum_rssi: the minimum rssi of entries to return.
|
|
|
|
//| :param bool active: retrieve scan responses for scannable advertisements.
|
|
|
|
//| :returns: an iterable of `_bleio.ScanEntry` objects
|
|
|
|
//| :rtype: iterable"""
|
|
|
|
//| ...
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_prefixes, ARG_buffer_size, ARG_extended, ARG_timeout, ARG_interval, ARG_window, ARG_minimum_rssi, ARG_active };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_prefixes, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 512} },
|
|
|
|
{ MP_QSTR_extended, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
2019-11-13 21:34:20 -05:00
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
|
2019-09-14 15:40:24 -04:00
|
|
|
{ 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} },
|
|
|
|
{ MP_QSTR_minimum_rssi, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -80} },
|
|
|
|
{ MP_QSTR_active, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
|
|
|
};
|
|
|
|
|
|
|
|
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2020-12-21 22:58:17 -05:00
|
|
|
mp_float_t timeout = 0.0f;
|
2019-11-13 21:34:20 -05:00
|
|
|
if (args[ARG_timeout].u_obj != mp_const_none) {
|
2019-09-14 15:40:24 -04:00
|
|
|
timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[ARG_interval].u_obj == MP_OBJ_NULL) {
|
|
|
|
args[ARG_interval].u_obj = mp_obj_new_float(INTERVAL_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args[ARG_window].u_obj == MP_OBJ_NULL) {
|
|
|
|
args[ARG_window].u_obj = mp_obj_new_float(WINDOW_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:16:19 -05:00
|
|
|
const mp_float_t interval = mp_obj_get_float(args[ARG_interval].u_obj);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (interval < INTERVAL_MIN || interval > INTERVAL_MAX) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError_varg(MP_ERROR_TEXT("interval must be in range %s-%s"), INTERVAL_MIN_STRING, INTERVAL_MAX_STRING);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
2020-12-21 22:58:17 -05:00
|
|
|
if (timeout != 0.0f && timeout < interval) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("non-zero timeout must be >= interval"));
|
2020-12-21 22:58:17 -05:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#pragma GCC diagnostic pop
|
2020-12-21 22:58:17 -05:00
|
|
|
|
2021-01-10 13:16:19 -05:00
|
|
|
const mp_float_t window = mp_obj_get_float(args[ARG_window].u_obj);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (window > interval) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("window must be <= interval"));
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_buffer_info_t prefix_bufinfo;
|
|
|
|
prefix_bufinfo.len = 0;
|
|
|
|
if (args[ARG_prefixes].u_obj != MP_OBJ_NULL) {
|
|
|
|
mp_get_buffer_raise(args[ARG_prefixes].u_obj, &prefix_bufinfo, MP_BUFFER_READ);
|
2023-11-20 20:05:43 -05:00
|
|
|
// An empty buffer may not be on the heap, but that doesn't matter.
|
|
|
|
if (prefix_bufinfo.len > 0 && gc_nbytes(prefix_bufinfo.buf) == 0) {
|
2023-10-30 04:49:06 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("Prefix buffer must be on the heap"));
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return common_hal_bleio_adapter_start_scan(self, prefix_bufinfo.buf, prefix_bufinfo.len, args[ARG_extended].u_bool, args[ARG_buffer_size].u_int, timeout, interval, window, args[ARG_minimum_rssi].u_int, args[ARG_active].u_bool);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_start_scan_obj, 1, bleio_adapter_start_scan);
|
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def stop_scan(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Stop the current scan."""
|
|
|
|
//| ...
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_stop_scan(mp_obj_t self_in) {
|
|
|
|
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
common_hal_bleio_adapter_stop_scan(self);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_stop_scan_obj, bleio_adapter_stop_scan);
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| advertising: bool
|
2020-05-19 02:56:04 -04:00
|
|
|
//| """True when the adapter is currently advertising. (read-only)"""
|
|
|
|
STATIC mp_obj_t bleio_adapter_get_advertising(mp_obj_t self) {
|
|
|
|
return mp_obj_new_bool(common_hal_bleio_adapter_get_advertising(self));
|
|
|
|
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_advertising_obj, bleio_adapter_get_advertising);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_adapter_advertising_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_adapter_get_advertising_obj);
|
2020-05-19 02:56:04 -04:00
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| connected: bool
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """True when the adapter is connected to another device regardless of who initiated the
|
|
|
|
//| connection. (read-only)"""
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_get_connected(mp_obj_t self) {
|
|
|
|
return mp_obj_new_bool(common_hal_bleio_adapter_get_connected(self));
|
2019-07-31 00:30:24 -04:00
|
|
|
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_connected_obj, bleio_adapter_get_connected);
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_adapter_connected_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_adapter_get_connected_obj);
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| connections: Tuple[Connection]
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Tuple of active connections including those initiated through
|
|
|
|
//| :py:meth:`_bleio.Adapter.connect`. (read-only)"""
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_get_connections(mp_obj_t self) {
|
|
|
|
return common_hal_bleio_adapter_get_connections(self);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_connections_obj, bleio_adapter_get_connections);
|
|
|
|
|
2022-05-01 12:24:05 -04:00
|
|
|
MP_PROPERTY_GETTER(bleio_adapter_connections_obj,
|
2022-05-01 11:16:53 -04:00
|
|
|
(mp_obj_t)&bleio_adapter_get_connections_obj);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def connect(self, address: Address, *, timeout: float) -> Connection:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Attempts a connection to the device with the given address.
|
2019-09-14 15:40:24 -04:00
|
|
|
//|
|
2020-05-01 18:23:27 -04:00
|
|
|
//| :param Address address: The address of the peripheral to connect to
|
|
|
|
//| :param float/int timeout: Try to connect for timeout seconds."""
|
|
|
|
//| ...
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC mp_obj_t bleio_adapter_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
|
|
|
|
2019-11-21 13:35:15 -05:00
|
|
|
enum { ARG_address, ARG_timeout };
|
2019-09-14 15:40:24 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_address, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
|
2021-10-10 02:46:14 -04:00
|
|
|
bleio_address_obj_t *address = mp_arg_validate_type(args[ARG_address].u_obj, &bleio_address_type, MP_QSTR_address);
|
2019-09-14 15:40:24 -04:00
|
|
|
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
|
|
|
|
|
2019-11-21 13:35:15 -05:00
|
|
|
return common_hal_bleio_adapter_connect(self, address, timeout);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2021-10-07 16:36:06 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_adapter_connect_obj, 1, bleio_adapter_connect);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-07-02 14:11:25 -04:00
|
|
|
//| def erase_bonding(self) -> None:
|
2020-05-01 18:23:27 -04:00
|
|
|
//| """Erase all bonding information stored in flash memory."""
|
|
|
|
//| ...
|
2022-09-29 20:22:32 -04:00
|
|
|
//|
|
2020-01-10 23:55:45 -05:00
|
|
|
STATIC mp_obj_t bleio_adapter_erase_bonding(mp_obj_t self_in) {
|
|
|
|
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
|
|
|
|
common_hal_bleio_adapter_erase_bonding(self);
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_erase_bonding_obj, bleio_adapter_erase_bonding);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2018-07-08 15:13:05 -04:00
|
|
|
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) },
|
2020-01-10 23:55:45 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_adapter_name_obj) },
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-01-10 23:55:45 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_start_advertising), MP_ROM_PTR(&bleio_adapter_start_advertising_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stop_advertising), MP_ROM_PTR(&bleio_adapter_stop_advertising_obj) },
|
2020-05-19 02:56:04 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_advertising), MP_ROM_PTR(&bleio_adapter_advertising_obj) },
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_start_scan), MP_ROM_PTR(&bleio_adapter_start_scan_obj) },
|
2020-01-10 23:55:45 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_stop_scan), MP_ROM_PTR(&bleio_adapter_stop_scan_obj) },
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-01-10 23:55:45 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&bleio_adapter_connect_obj) },
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-01-10 23:55:45 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_connected), MP_ROM_PTR(&bleio_adapter_connected_obj) },
|
2019-09-14 15:40:24 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_connections), MP_ROM_PTR(&bleio_adapter_connections_obj) },
|
2020-01-10 23:55:45 -05:00
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_erase_bonding), MP_ROM_PTR(&bleio_adapter_erase_bonding_obj) },
|
2018-07-08 15:13:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(bleio_adapter_locals_dict, bleio_adapter_locals_dict_table);
|
|
|
|
|
2023-09-19 21:09:29 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
bleio_adapter_type,
|
|
|
|
MP_QSTR_Adapter,
|
2023-10-19 17:27:20 -04:00
|
|
|
MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS,
|
2023-09-19 21:09:29 -04:00
|
|
|
make_new, bleio_adapter_make_new,
|
|
|
|
locals_dict, &bleio_adapter_locals_dict
|
|
|
|
);
|