2016-11-16 15:38:25 -05:00
|
|
|
/*
|
2017-08-04 12:05:38 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-06-19 10:42:36 -04:00
|
|
|
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
|
2016-11-16 15:38:25 -05:00
|
|
|
* Copyright (c) 2016 Glenn Ruben Bakke
|
2018-07-08 15:13:05 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-05-19 02:56:04 -04:00
|
|
|
#include <math.h>
|
2019-09-14 15:40:24 -04:00
|
|
|
#include <stdint.h>
|
2018-07-08 15:13:05 -04:00
|
|
|
#include <stdio.h>
|
2018-07-17 05:44:55 -04:00
|
|
|
#include <string.h>
|
2016-11-16 15:38:25 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "ble.h"
|
2018-07-08 15:13:05 -04:00
|
|
|
#include "ble_drv.h"
|
2020-01-05 23:33:42 -05:00
|
|
|
#include "bonding.h"
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "nrfx_power.h"
|
|
|
|
#include "nrf_nvic.h"
|
2018-07-19 17:51:34 -04:00
|
|
|
#include "nrf_sdm.h"
|
2019-09-14 15:40:24 -04:00
|
|
|
#include "py/gc.h"
|
2019-07-31 00:30:24 -04:00
|
|
|
#include "py/objstr.h"
|
2018-11-20 23:04:58 -05:00
|
|
|
#include "py/runtime.h"
|
2021-06-25 17:58:32 -04:00
|
|
|
#include "supervisor/shared/bluetooth/bluetooth.h"
|
2019-09-14 15:40:24 -04:00
|
|
|
#include "supervisor/shared/safe_mode.h"
|
2019-11-18 09:22:41 -05:00
|
|
|
#include "supervisor/shared/tick.h"
|
2019-01-30 02:13:07 -05:00
|
|
|
#include "supervisor/usb.h"
|
2019-09-14 15:40:24 -04:00
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
2019-08-29 18:44:27 -04:00
|
|
|
#include "shared-bindings/_bleio/Adapter.h"
|
|
|
|
#include "shared-bindings/_bleio/Address.h"
|
2019-10-20 23:50:12 -04:00
|
|
|
#include "shared-bindings/nvm/ByteArray.h"
|
2019-09-14 15:40:24 -04:00
|
|
|
#include "shared-bindings/_bleio/Connection.h"
|
|
|
|
#include "shared-bindings/_bleio/ScanEntry.h"
|
|
|
|
#include "shared-bindings/time/__init__.h"
|
2019-01-30 02:13:07 -05:00
|
|
|
|
2022-12-01 17:43:08 -05:00
|
|
|
#if CIRCUITPY_ENVIRON
|
|
|
|
#include "shared-bindings/_environ/__init__.h"
|
2022-07-02 15:47:14 -04:00
|
|
|
#endif
|
|
|
|
|
2019-08-25 21:38:13 -04:00
|
|
|
#define BLE_MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
|
|
|
|
#define BLE_MAX_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_0_625_MS)
|
|
|
|
#define BLE_SLAVE_LATENCY 0
|
|
|
|
#define BLE_CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
|
|
|
|
|
2020-04-20 23:28:36 -04:00
|
|
|
#ifndef BLEIO_VS_UUID_COUNT
|
|
|
|
#define BLEIO_VS_UUID_COUNT 75
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BLEIO_HVN_TX_QUEUE_SIZE
|
2020-08-15 10:16:57 -04:00
|
|
|
#define BLEIO_HVN_TX_QUEUE_SIZE 5
|
2020-04-20 23:28:36 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BLEIO_CENTRAL_ROLE_COUNT
|
|
|
|
#define BLEIO_CENTRAL_ROLE_COUNT 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BLEIO_PERIPH_ROLE_COUNT
|
|
|
|
#define BLEIO_PERIPH_ROLE_COUNT 4
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef BLEIO_ATTR_TAB_SIZE
|
|
|
|
#define BLEIO_ATTR_TAB_SIZE (BLE_GATTS_ATTR_TAB_SIZE_DEFAULT * 5)
|
|
|
|
#endif
|
|
|
|
|
2019-10-20 23:50:12 -04:00
|
|
|
const nvm_bytearray_obj_t common_hal_bleio_nvm_obj = {
|
|
|
|
.base = {
|
|
|
|
.type = &nvm_bytearray_type,
|
|
|
|
},
|
2021-03-15 09:57:36 -04:00
|
|
|
.start_address = (uint8_t *)CIRCUITPY_BLE_CONFIG_START_ADDR,
|
2019-12-11 15:43:06 -05:00
|
|
|
.len = CIRCUITPY_BLE_CONFIG_SIZE,
|
2019-10-20 23:50:12 -04:00
|
|
|
};
|
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
STATIC void softdevice_assert_handler(uint32_t id, uint32_t pc, uint32_t info) {
|
2019-09-14 15:40:24 -04:00
|
|
|
reset_into_safe_mode(NORDIC_SOFT_DEVICE_ASSERT);
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
// Linker script provided ram start.
|
|
|
|
extern uint32_t _ram_start;
|
2018-07-22 10:06:19 -04:00
|
|
|
STATIC uint32_t ble_stack_enable(void) {
|
|
|
|
nrf_clock_lf_cfg_t clock_config = {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if BOARD_HAS_32KHZ_XTAL
|
|
|
|
.source = NRF_CLOCK_LF_SRC_XTAL,
|
|
|
|
.rc_ctiv = 0,
|
2019-08-22 01:04:00 -04:00
|
|
|
.rc_temp_ctiv = 0,
|
2021-03-15 09:57:36 -04:00
|
|
|
.accuracy = NRF_CLOCK_LF_ACCURACY_20_PPM,
|
|
|
|
#else
|
|
|
|
.source = NRF_CLOCK_LF_SRC_RC,
|
|
|
|
.rc_ctiv = 16,
|
2019-08-22 01:04:00 -04:00
|
|
|
.rc_temp_ctiv = 2,
|
2021-03-15 09:57:36 -04:00
|
|
|
.accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM,
|
|
|
|
#endif
|
2018-07-22 10:06:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t err_code = sd_softdevice_enable(&clock_config, softdevice_assert_handler);
|
2019-08-28 16:15:09 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-07-22 10:06:19 -04:00
|
|
|
return err_code;
|
2019-08-28 16:15:09 -04:00
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
err_code = sd_nvic_EnableIRQ(SD_EVT_IRQn);
|
2019-08-28 16:15:09 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-07-22 10:06:19 -04:00
|
|
|
return err_code;
|
2019-08-28 16:15:09 -04:00
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2018-12-28 22:55:29 -05:00
|
|
|
// Start with no event handlers, etc.
|
|
|
|
ble_drv_reset();
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2020-08-15 10:16:57 -04:00
|
|
|
// In the future we might move .data and .bss to the other side of the stack and
|
2019-09-14 15:40:24 -04:00
|
|
|
// dynamically adjust for different memory requirements of the SD based on boot.py
|
2020-08-15 10:16:57 -04:00
|
|
|
// configuration. But we still need to keep the SPIM3 buffer (if needed) in the first 64kB of RAM.
|
|
|
|
|
|
|
|
uint32_t sd_ram_end = SOFTDEVICE_RAM_START_ADDR + SOFTDEVICE_RAM_SIZE;
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
ble_cfg_t ble_conf;
|
|
|
|
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
|
2020-01-31 14:40:50 -05:00
|
|
|
// Each additional connection costs:
|
|
|
|
// about 3700-4300 bytes when .hvn_tx_queue_size is 1
|
|
|
|
// about 9000 bytes when .hvn_tx_queue_size is 10
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_conf.conn_cfg.params.gap_conn_cfg.conn_count = BLEIO_TOTAL_CONNECTION_COUNT;
|
|
|
|
// Event length here can influence throughput so perhaps make multiple connection profiles
|
|
|
|
// available.
|
2018-07-22 10:06:19 -04:00
|
|
|
ble_conf.conn_cfg.params.gap_conn_cfg.event_length = BLE_GAP_EVENT_LENGTH_DEFAULT;
|
2020-08-15 10:16:57 -04:00
|
|
|
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GAP, &ble_conf, sd_ram_end);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-07-22 10:06:19 -04:00
|
|
|
return err_code;
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
2020-01-31 14:40:50 -05:00
|
|
|
// adv_set_count must be == 1 for S140. Cannot be increased.
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_conf.gap_cfg.role_count_cfg.adv_set_count = 1;
|
2020-01-31 14:40:50 -05:00
|
|
|
// periph_role_count costs 1232 bytes for 2 to 3, then ~1840 for each further increment.
|
2020-04-20 23:28:36 -04:00
|
|
|
ble_conf.gap_cfg.role_count_cfg.periph_role_count = BLEIO_PERIPH_ROLE_COUNT;
|
2020-01-31 14:40:50 -05:00
|
|
|
// central_role_count costs 648 bytes for 1 to 2, then ~1250 for each further increment.
|
2020-04-20 23:28:36 -04:00
|
|
|
ble_conf.gap_cfg.role_count_cfg.central_role_count = BLEIO_CENTRAL_ROLE_COUNT;
|
2021-03-31 19:04:04 -04:00
|
|
|
// The number of concurrent pairing processes. Takes 392 bytes.
|
|
|
|
ble_conf.gap_cfg.role_count_cfg.central_sec_count = BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT;
|
2020-08-15 10:16:57 -04:00
|
|
|
err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_conf, sd_ram_end);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-07-22 10:06:19 -04:00
|
|
|
return err_code;
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
|
|
|
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
|
2020-01-31 14:40:50 -05:00
|
|
|
// Each increment to hvn_tx_queue_size costs 2064 bytes.
|
|
|
|
// DevZone recommends not setting this directly, but instead changing gap_conn_cfg.event_length.
|
|
|
|
// However, we are setting connection extension, so this seems to make sense.
|
2020-04-20 23:28:36 -04:00
|
|
|
ble_conf.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = BLEIO_HVN_TX_QUEUE_SIZE;
|
2020-08-15 10:16:57 -04:00
|
|
|
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_conf, sd_ram_end);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2018-07-22 10:06:19 -04:00
|
|
|
return err_code;
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-12-06 15:27:46 -05:00
|
|
|
// Set ATT_MTU so that the maximum MTU we can negotiate is up to the full characteristic size.
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
|
|
|
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
|
|
|
|
ble_conf.conn_cfg.params.gatt_conn_cfg.att_mtu = BLE_GATTS_VAR_ATTR_LEN_MAX;
|
2020-08-15 10:16:57 -04:00
|
|
|
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATT, &ble_conf, sd_ram_end);
|
2019-12-06 15:27:46 -05:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
|
|
|
|
// Make sure service changed characteristic is on. This lets us prompt a peer to re-discover
|
|
|
|
// portions of our attribute table.
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
|
|
|
ble_conf.gatts_cfg.service_changed.service_changed = 1;
|
|
|
|
err_code = sd_ble_cfg_set(BLE_GATTS_CFG_SERVICE_CHANGED, &ble_conf, sd_ram_end);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
|
|
|
|
2020-01-31 14:40:50 -05:00
|
|
|
// Increase the GATT Server attribute size to accomodate both the CircuitPython built-in service
|
2019-09-14 15:40:24 -04:00
|
|
|
// and anything the user does.
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
2020-01-31 14:40:50 -05:00
|
|
|
// Each increment to the BLE_GATTS_ATTR_TAB_SIZE_DEFAULT multiplier costs 1408 bytes.
|
2020-04-20 23:28:36 -04:00
|
|
|
ble_conf.gatts_cfg.attr_tab_size.attr_tab_size = BLEIO_ATTR_TAB_SIZE;
|
2020-08-15 10:16:57 -04:00
|
|
|
err_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_conf, sd_ram_end);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:27:46 -05:00
|
|
|
// Increase the number of vendor UUIDs supported. Apple uses a complete random number per
|
|
|
|
// service and characteristic.
|
|
|
|
memset(&ble_conf, 0, sizeof(ble_conf));
|
2020-01-31 14:40:50 -05:00
|
|
|
// Each additional vs_uuid_count costs 16 bytes.
|
2020-04-20 23:28:36 -04:00
|
|
|
ble_conf.common_cfg.vs_uuid_cfg.vs_uuid_count = BLEIO_VS_UUID_COUNT; // Defaults to 10.
|
2020-08-15 10:16:57 -04:00
|
|
|
err_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_conf, sd_ram_end);
|
2019-12-06 15:27:46 -05:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2020-08-15 10:16:57 -04:00
|
|
|
// This sets sd_ram_end to the minimum value needed for the settings set above.
|
|
|
|
// You can set a breakpoint just after this call and examine sd_ram_end to see
|
|
|
|
// how much RAM the SD needs with the configuration above.
|
|
|
|
err_code = sd_ble_enable(&sd_ram_end);
|
2019-09-14 15:40:24 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2019-08-25 21:38:13 -04:00
|
|
|
return err_code;
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2019-08-25 21:38:13 -04:00
|
|
|
|
2019-12-06 15:27:46 -05:00
|
|
|
// Turn on connection event extension so we can transmit for a longer period of time as needed.
|
|
|
|
ble_opt_t opt;
|
|
|
|
opt.common_opt.conn_evt_ext.enable = true;
|
|
|
|
err_code = sd_ble_opt_set(BLE_COMMON_OPT_CONN_EVT_EXT, &opt);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
|
|
|
|
2019-08-25 21:38:13 -04:00
|
|
|
ble_gap_conn_params_t gap_conn_params = {
|
|
|
|
.min_conn_interval = BLE_MIN_CONN_INTERVAL,
|
|
|
|
.max_conn_interval = BLE_MAX_CONN_INTERVAL,
|
2021-03-15 09:57:36 -04:00
|
|
|
.slave_latency = BLE_SLAVE_LATENCY,
|
|
|
|
.conn_sup_timeout = BLE_CONN_SUP_TIMEOUT,
|
2019-08-25 21:38:13 -04:00
|
|
|
};
|
2021-03-15 09:57:36 -04:00
|
|
|
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_UNKNOWN);
|
|
|
|
return err_code;
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC bool adapter_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
|
2021-03-15 09:57:36 -04:00
|
|
|
bleio_adapter_obj_t *self = (bleio_adapter_obj_t *)self_in;
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
// For debugging.
|
|
|
|
// mp_printf(&mp_plat_print, "Adapter event: 0x%04x\n", ble_evt->header.evt_id);
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
// Always queue a background run after a BLE event.
|
|
|
|
background_callback_add_core(&self->background_callback);
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
switch (ble_evt->header.evt_id) {
|
|
|
|
case BLE_GAP_EVT_CONNECTED: {
|
2019-10-23 02:09:56 -04:00
|
|
|
// Find an empty connection. One must always be available because the SD has the same
|
|
|
|
// total connection limit.
|
2019-09-14 15:40:24 -04:00
|
|
|
bleio_connection_internal_t *connection;
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
connection = &bleio_connections[i];
|
2019-09-14 15:40:24 -04:00
|
|
|
if (connection->conn_handle == BLE_CONN_HANDLE_INVALID) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 16:53:50 -05:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
// Central has connected.
|
2021-03-15 09:57:36 -04:00
|
|
|
ble_gap_evt_connected_t *connected = &ble_evt->evt.gap_evt.params.connected;
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
connection->conn_handle = ble_evt->evt.gap_evt.conn_handle;
|
|
|
|
connection->connection_obj = mp_const_none;
|
2019-11-21 13:35:15 -05:00
|
|
|
connection->pair_status = PAIR_NOT_PAIRED;
|
2019-12-06 15:27:46 -05:00
|
|
|
connection->mtu = 0;
|
2019-11-22 19:33:48 -05:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_drv_add_event_handler_entry(&connection->handler_entry, connection_on_ble_evt, connection);
|
|
|
|
self->connection_objs = NULL;
|
|
|
|
|
2019-11-22 19:33:48 -05:00
|
|
|
// Save the current connection parameters.
|
|
|
|
memcpy(&connection->conn_params, &connected->conn_params, sizeof(ble_gap_conn_params_t));
|
|
|
|
|
|
|
|
#if CIRCUITPY_VERBOSE_BLE
|
|
|
|
ble_gap_conn_params_t *cp = &connected->conn_params;
|
|
|
|
mp_printf(&mp_plat_print, "conn params: min_ci %d max_ci %d s_l %d sup_timeout %d\n", cp->min_conn_interval, cp->max_conn_interval, cp->slave_latency, cp->conn_sup_timeout);
|
|
|
|
#endif
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
// See if connection interval set by Central is out of range.
|
|
|
|
// If so, negotiate our preferred range.
|
|
|
|
ble_gap_conn_params_t conn_params;
|
|
|
|
sd_ble_gap_ppcp_get(&conn_params);
|
|
|
|
if (conn_params.min_conn_interval < connected->conn_params.min_conn_interval ||
|
|
|
|
conn_params.min_conn_interval > connected->conn_params.max_conn_interval) {
|
|
|
|
sd_ble_gap_conn_param_update(ble_evt->evt.gap_evt.conn_handle, &conn_params);
|
|
|
|
}
|
|
|
|
self->current_advertising_data = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BLE_GAP_EVT_DISCONNECTED: {
|
|
|
|
// Find the connection that was disconnected.
|
|
|
|
bleio_connection_internal_t *connection;
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
connection = &bleio_connections[i];
|
2019-09-14 15:40:24 -04:00
|
|
|
if (connection->conn_handle == ble_evt->evt.gap_evt.conn_handle) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ble_drv_remove_event_handler(connection_on_ble_evt, connection);
|
|
|
|
connection->conn_handle = BLE_CONN_HANDLE_INVALID;
|
2020-01-10 23:55:45 -05:00
|
|
|
connection->pair_status = PAIR_NOT_PAIRED;
|
2021-03-31 19:04:04 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_VERBOSE_BLE
|
|
|
|
mp_printf(&mp_plat_print, "disconnected %02x\n", ble_evt->evt.gap_evt.params.disconnected.reason);
|
|
|
|
#endif
|
2019-09-14 15:40:24 -04:00
|
|
|
if (connection->connection_obj != mp_const_none) {
|
2021-03-15 09:57:36 -04:00
|
|
|
bleio_connection_obj_t *obj = connection->connection_obj;
|
2019-09-14 15:40:24 -04:00
|
|
|
obj->connection = NULL;
|
|
|
|
obj->disconnect_reason = ble_evt->evt.gap_evt.params.disconnected.reason;
|
|
|
|
}
|
|
|
|
self->connection_objs = NULL;
|
2019-11-20 16:53:50 -05:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
// For debugging.
|
|
|
|
// mp_printf(&mp_plat_print, "Unhandled adapter event: 0x%04x\n", ble_evt->header.evt_id);
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void get_address(bleio_adapter_obj_t *self, ble_gap_addr_t *address) {
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(sd_ble_gap_addr_get(address));
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
char default_ble_name[] = { 'C', 'I', 'R', 'C', 'U', 'I', 'T', 'P', 'Y', 0, 0, 0, 0, 0};
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
|
2022-07-02 15:47:14 -04:00
|
|
|
// setup the default name
|
|
|
|
ble_gap_addr_t addr; // local_address
|
|
|
|
get_address(self, &addr);
|
|
|
|
mp_int_t len = sizeof(default_ble_name) - 1;
|
|
|
|
default_ble_name[len - 4] = nibble_to_hex_lower[addr.addr[1] >> 4 & 0xf];
|
|
|
|
default_ble_name[len - 3] = nibble_to_hex_lower[addr.addr[1] & 0xf];
|
|
|
|
default_ble_name[len - 2] = nibble_to_hex_lower[addr.addr[0] >> 4 & 0xf];
|
|
|
|
default_ble_name[len - 1] = nibble_to_hex_lower[addr.addr[0] & 0xf];
|
|
|
|
default_ble_name[len] = '\0'; // for now we add null for compatibility with C ASCIIZ strings
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2022-07-02 15:47:14 -04:00
|
|
|
mp_int_t name_len = 0;
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2022-12-01 17:43:08 -05:00
|
|
|
#if CIRCUITPY_ENVIRON
|
|
|
|
mp_obj_t ble_name = common_hal__environ_get_key("CIRCUITPY_BLE_NAME");
|
|
|
|
if (ble_name != mp_const_none) {
|
|
|
|
common_hal_bleio_adapter_set_name(self, mp_obj_str_get_str(ble_name));
|
|
|
|
return;
|
2022-07-02 15:47:14 -04:00
|
|
|
}
|
|
|
|
#endif
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2022-07-02 15:47:14 -04:00
|
|
|
if (name_len <= 0) {
|
|
|
|
common_hal_bleio_adapter_set_name(self, (char *)default_ble_name);
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
static void bluetooth_adapter_background(void *data) {
|
|
|
|
supervisor_bluetooth_background();
|
|
|
|
bleio_background();
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled) {
|
|
|
|
const bool is_enabled = common_hal_bleio_adapter_get_enabled(self);
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
// Don't enable or disable twice
|
2019-09-14 15:40:24 -04:00
|
|
|
if (is_enabled == enabled) {
|
2018-07-22 10:06:19 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
uint32_t err_code = NRF_SUCCESS;
|
2018-07-19 17:51:34 -04:00
|
|
|
if (enabled) {
|
2019-01-30 02:13:07 -05:00
|
|
|
// The SD takes over the POWER module and will fail if the module is already in use.
|
|
|
|
// Occurs when USB is initialized previously
|
|
|
|
nrfx_power_uninit();
|
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
err_code = ble_stack_enable();
|
2018-07-08 15:13:05 -04:00
|
|
|
} else {
|
2018-07-19 17:51:34 -04:00
|
|
|
err_code = sd_softdevice_disable();
|
|
|
|
}
|
2021-04-16 10:59:11 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_USB
|
2019-09-14 15:40:24 -04:00
|
|
|
// Re-init USB hardware
|
|
|
|
init_usb_hardware();
|
2021-04-16 10:59:11 -04:00
|
|
|
#endif
|
2019-01-30 02:13:07 -05:00
|
|
|
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(err_code);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
// Add a handler for incoming peripheral connections.
|
|
|
|
if (enabled) {
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_connection_internal_t *connection = &bleio_connections[i];
|
2020-08-11 16:21:16 -04:00
|
|
|
// Reset connection.
|
|
|
|
bleio_connection_clear(connection);
|
2021-05-19 19:22:07 -04:00
|
|
|
ble_drv_remove_event_handler(connection_on_ble_evt, connection);
|
2019-09-14 15:40:24 -04:00
|
|
|
connection->conn_handle = BLE_CONN_HANDLE_INVALID;
|
|
|
|
}
|
2021-05-19 19:22:07 -04:00
|
|
|
self->background_callback.fun = bluetooth_adapter_background;
|
|
|
|
self->background_callback.data = self;
|
2019-09-14 15:40:24 -04:00
|
|
|
bleio_adapter_reset_name(self);
|
2021-05-19 19:22:07 -04:00
|
|
|
ble_drv_add_event_handler_entry(&self->connection_handler_entry, adapter_on_ble_evt, self);
|
|
|
|
bluetooth_adapter_background(self);
|
2019-09-14 15:40:24 -04:00
|
|
|
} else {
|
2021-05-19 19:22:07 -04:00
|
|
|
ble_drv_remove_event_handler(adapter_on_ble_evt, self);
|
|
|
|
if (self->current_advertising_data != NULL) {
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_drv_reset();
|
|
|
|
self->scan_results = NULL;
|
|
|
|
self->current_advertising_data = NULL;
|
|
|
|
self->advertising_data = NULL;
|
|
|
|
self->scan_response_data = NULL;
|
2018-07-19 17:51:34 -04:00
|
|
|
}
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
2018-07-19 17:51:34 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
|
2018-07-22 10:06:19 -04:00
|
|
|
uint8_t is_enabled;
|
|
|
|
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(sd_softdevice_is_enabled(&is_enabled));
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
return is_enabled;
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self) {
|
|
|
|
common_hal_bleio_adapter_set_enabled(self, true);
|
|
|
|
|
|
|
|
ble_gap_addr_t local_address;
|
|
|
|
get_address(self, &local_address);
|
|
|
|
|
|
|
|
bleio_address_obj_t *address = m_new_obj(bleio_address_obj_t);
|
|
|
|
address->base.type = &bleio_address_type;
|
|
|
|
|
|
|
|
common_hal_bleio_address_construct(address, local_address.addr, local_address.addr_type);
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2020-08-14 05:48:15 -04:00
|
|
|
bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) {
|
2020-08-07 11:01:49 -04:00
|
|
|
ble_gap_addr_t local_address;
|
2020-08-14 05:48:15 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
if (!mp_get_buffer(address->bytes, &bufinfo, MP_BUFFER_READ)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-07 11:01:49 -04:00
|
|
|
local_address.addr_type = address->type;
|
2020-08-14 05:48:15 -04:00
|
|
|
memcpy(local_address.addr, bufinfo.buf, NUM_BLEIO_ADDRESS_BYTES);
|
|
|
|
return sd_ble_gap_addr_set(&local_address) == NRF_SUCCESS;
|
2020-08-07 11:01:49 -04:00
|
|
|
}
|
|
|
|
|
2022-08-03 17:35:28 -04:00
|
|
|
uint16_t bleio_adapter_get_name(char *buf, uint16_t len) {
|
|
|
|
uint16_t full_len = 0;
|
|
|
|
sd_ble_gap_device_name_get(NULL, &full_len);
|
|
|
|
|
|
|
|
uint32_t err_code = sd_ble_gap_device_name_get((uint8_t *)buf, &len);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return full_len;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_str_t *common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self) {
|
2019-09-14 15:40:24 -04:00
|
|
|
uint16_t len = 0;
|
|
|
|
sd_ble_gap_device_name_get(NULL, &len);
|
2022-08-03 17:35:28 -04:00
|
|
|
char buf[len];
|
|
|
|
bleio_adapter_get_name(buf, len);
|
|
|
|
return mp_obj_new_str(buf, len);
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
2016-12-13 14:27:26 -05:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *name) {
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_gap_conn_sec_mode_t sec;
|
|
|
|
sec.lv = 0;
|
|
|
|
sec.sm = 0;
|
2022-07-02 16:34:42 -04:00
|
|
|
uint16_t len = strlen(name);
|
|
|
|
if (len > BLE_GAP_DEVNAME_MAX_LEN) {
|
|
|
|
len = BLE_GAP_DEVNAME_MAX_LEN;
|
2022-07-02 15:47:14 -04:00
|
|
|
}
|
2022-07-02 16:34:42 -04:00
|
|
|
sd_ble_gap_device_name_set(&sec, (const uint8_t *)name, len);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
STATIC uint32_t _update_identities(bool is_central) {
|
|
|
|
const ble_gap_id_key_t *keys[BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT];
|
|
|
|
// TODO: Make sure we don't store more than BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT identities of
|
|
|
|
// each type. Right now, we'll silently ignore those keys.
|
|
|
|
size_t len = bonding_load_identities(is_central, keys, BLE_GAP_DEVICE_IDENTITIES_MAX_COUNT);
|
|
|
|
uint32_t status = NRF_SUCCESS;
|
|
|
|
if (len > 0) {
|
|
|
|
status = sd_ble_gap_device_identities_set(
|
|
|
|
keys,
|
|
|
|
NULL, // Don't set local IRK because we use our device IRK for private addresses.
|
|
|
|
len
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
};
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
STATIC bool scan_on_ble_evt(ble_evt_t *ble_evt, void *scan_results_in) {
|
2021-03-15 09:57:36 -04:00
|
|
|
bleio_scanresults_obj_t *scan_results = (bleio_scanresults_obj_t *)scan_results_in;
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
if (ble_evt->header.evt_id == BLE_GAP_EVT_TIMEOUT &&
|
|
|
|
ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SCAN) {
|
|
|
|
shared_module_bleio_scanresults_set_done(scan_results, true);
|
|
|
|
ble_drv_remove_event_handler(scan_on_ble_evt, scan_results);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
if (ble_evt->header.evt_id != BLE_GAP_EVT_ADV_REPORT) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ble_gap_evt_adv_report_t *report = &ble_evt->evt.gap_evt.params.adv_report;
|
|
|
|
|
|
|
|
shared_module_bleio_scanresults_append(scan_results,
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_ticks_ms64(),
|
|
|
|
report->type.connectable,
|
|
|
|
report->type.scan_response,
|
|
|
|
report->rssi,
|
|
|
|
report->peer_addr.addr,
|
|
|
|
report->peer_addr.addr_type,
|
|
|
|
report->data.p_data,
|
|
|
|
report->data.len);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
const uint32_t err_code = sd_ble_gap_scan_start(NULL, scan_results->common_hal_data);
|
2018-07-22 10:06:19 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2019-09-14 15:40:24 -04:00
|
|
|
// TODO: Pass the error into the scan results so it can throw an exception.
|
|
|
|
scan_results->done = true;
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_obj_t common_hal_bleio_adapter_start_scan(bleio_adapter_obj_t *self, uint8_t *prefixes, size_t prefix_length, bool extended, mp_int_t buffer_size, mp_float_t timeout, mp_float_t interval, mp_float_t window, mp_int_t minimum_rssi, bool active) {
|
2019-09-14 15:40:24 -04:00
|
|
|
if (self->scan_results != NULL) {
|
|
|
|
if (!shared_module_bleio_scanresults_get_done(self->scan_results)) {
|
2019-11-20 16:53:50 -05:00
|
|
|
mp_raise_bleio_BluetoothError(translate("Scan already in progess. Stop with stop_scan."));
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
self->scan_results = NULL;
|
|
|
|
}
|
2021-09-08 19:58:53 -04:00
|
|
|
// Check to see if advertising is going already.
|
|
|
|
if (self->current_advertising_data != NULL && self->current_advertising_data == self->advertising_data) {
|
|
|
|
check_nrf_error(NRF_ERROR_BUSY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the current advertising data isn't owned by the adapter then it must be an internal
|
|
|
|
// advertisement that we should stop.
|
|
|
|
if (self->current_advertising_data != NULL) {
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
self->scan_results = shared_module_bleio_new_scanresults(buffer_size, prefixes, prefix_length, minimum_rssi);
|
|
|
|
size_t max_packet_size = extended ? BLE_GAP_SCAN_BUFFER_EXTENDED_MAX_SUPPORTED : BLE_GAP_SCAN_BUFFER_MAX;
|
|
|
|
uint8_t *raw_data = m_malloc(sizeof(ble_data_t) + max_packet_size, false);
|
2021-03-15 09:57:36 -04:00
|
|
|
ble_data_t *sd_data = (ble_data_t *)raw_data;
|
2019-09-14 15:40:24 -04:00
|
|
|
self->scan_results->common_hal_data = sd_data;
|
|
|
|
sd_data->len = max_packet_size;
|
|
|
|
sd_data->p_data = raw_data + sizeof(ble_data_t);
|
2019-11-20 16:53:50 -05:00
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
|
|
|
|
// Update the identities of peripheral peers so they can use a private
|
|
|
|
// resolvable address in their advertisements.
|
|
|
|
check_nrf_error(_update_identities(true));
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_drv_add_event_handler(scan_on_ble_evt, self->scan_results);
|
|
|
|
|
2021-01-10 13:16:19 -05:00
|
|
|
uint32_t nrf_timeout = SEC_TO_UNITS(timeout, UNIT_10_MS) + 0.5f;
|
2020-12-21 22:58:17 -05:00
|
|
|
if (nrf_timeout > UINT16_MAX) {
|
|
|
|
// 0xffff / 100
|
|
|
|
mp_raise_ValueError(translate("timeout must be < 655.35 secs"));
|
|
|
|
}
|
|
|
|
if (nrf_timeout == 0 && timeout > 0.0f) {
|
|
|
|
// Make sure converted timeout is > 0 if original timeout is > 0.
|
|
|
|
mp_raise_ValueError(translate("non-zero timeout must be > 0.01"));
|
|
|
|
}
|
|
|
|
|
2021-01-10 13:16:19 -05:00
|
|
|
if (nrf_timeout == 0) {
|
2019-09-14 15:40:24 -04:00
|
|
|
nrf_timeout = BLE_GAP_SCAN_TIMEOUT_UNLIMITED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_gap_scan_params_t scan_params = {
|
|
|
|
.extended = extended,
|
2021-01-10 13:16:19 -05:00
|
|
|
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS) + 0.5f,
|
2019-09-14 15:40:24 -04:00
|
|
|
.timeout = nrf_timeout,
|
2021-01-10 13:16:19 -05:00
|
|
|
.window = SEC_TO_UNITS(window, UNIT_0_625_MS) + 0.5f,
|
2019-09-14 15:40:24 -04:00
|
|
|
.scan_phys = BLE_GAP_PHY_1MBPS,
|
|
|
|
.active = active
|
|
|
|
};
|
2021-05-19 19:22:07 -04:00
|
|
|
|
|
|
|
uint32_t err_code = sd_ble_gap_scan_start(&scan_params, sd_data);
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
ble_drv_remove_event_handler(scan_on_ble_evt, self->scan_results);
|
2020-08-15 14:39:53 -04:00
|
|
|
self->scan_results = NULL;
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(err_code);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self->scan_results);
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_bleio_adapter_stop_scan(bleio_adapter_obj_t *self) {
|
2021-05-19 19:22:07 -04:00
|
|
|
if (self->scan_results == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
sd_ble_gap_scan_stop();
|
|
|
|
shared_module_bleio_scanresults_set_done(self->scan_results, true);
|
|
|
|
ble_drv_remove_event_handler(scan_on_ble_evt, self->scan_results);
|
|
|
|
self->scan_results = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t conn_handle;
|
|
|
|
volatile bool done;
|
|
|
|
} connect_info_t;
|
|
|
|
|
|
|
|
STATIC bool connect_on_ble_evt(ble_evt_t *ble_evt, void *info_in) {
|
2021-03-15 09:57:36 -04:00
|
|
|
connect_info_t *info = (connect_info_t *)info_in;
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
switch (ble_evt->header.evt_id) {
|
|
|
|
case BLE_GAP_EVT_CONNECTED:
|
|
|
|
info->conn_handle = ble_evt->evt.gap_evt.conn_handle;
|
|
|
|
info->done = true;
|
2019-11-20 16:53:50 -05:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case BLE_GAP_EVT_TIMEOUT:
|
|
|
|
// Handle will be invalid.
|
|
|
|
info->done = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// For debugging.
|
|
|
|
// mp_printf(&mp_plat_print, "Unhandled central event: 0x%04x\n", ble_evt->header.evt_id);
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
STATIC void _convert_address(const bleio_address_obj_t *address, ble_gap_addr_t *sd_address) {
|
|
|
|
sd_address->addr_type = address->type;
|
2019-09-14 15:40:24 -04:00
|
|
|
mp_buffer_info_t address_buf_info;
|
|
|
|
mp_get_buffer_raise(address->bytes, &address_buf_info, MP_BUFFER_READ);
|
2021-05-19 19:22:07 -04:00
|
|
|
memcpy(sd_address->addr, (uint8_t *)address_buf_info.buf, NUM_BLEIO_ADDRESS_BYTES);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_address_obj_t *address, mp_float_t timeout) {
|
|
|
|
ble_gap_addr_t addr;
|
|
|
|
_convert_address(address, &addr);
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
ble_gap_scan_params_t scan_params = {
|
|
|
|
.interval = MSEC_TO_UNITS(100, UNIT_0_625_MS),
|
|
|
|
.window = MSEC_TO_UNITS(100, UNIT_0_625_MS),
|
|
|
|
.scan_phys = BLE_GAP_PHY_1MBPS,
|
|
|
|
// timeout of 0 means no timeout
|
2021-01-10 13:16:19 -05:00
|
|
|
.timeout = SEC_TO_UNITS(timeout, UNIT_10_MS) + 0.5f,
|
2019-09-14 15:40:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
ble_gap_conn_params_t conn_params = {
|
|
|
|
.conn_sup_timeout = MSEC_TO_UNITS(4000, UNIT_10_MS),
|
|
|
|
.min_conn_interval = MSEC_TO_UNITS(15, UNIT_1_25_MS),
|
|
|
|
.max_conn_interval = MSEC_TO_UNITS(300, UNIT_1_25_MS),
|
|
|
|
.slave_latency = 0, // number of conn events
|
|
|
|
};
|
|
|
|
|
|
|
|
connect_info_t event_info;
|
|
|
|
ble_drv_add_event_handler(connect_on_ble_evt, &event_info);
|
|
|
|
event_info.done = false;
|
|
|
|
|
|
|
|
uint32_t err_code = sd_ble_gap_connect(&addr, &scan_params, &conn_params, BLE_CONN_CFG_TAG_CUSTOM);
|
2018-07-22 10:06:19 -04:00
|
|
|
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_drv_remove_event_handler(connect_on_ble_evt, &event_info);
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(err_code);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (!event_info.done) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
ble_drv_remove_event_handler(connect_on_ble_evt, &event_info);
|
|
|
|
|
2019-12-06 15:27:46 -05:00
|
|
|
uint16_t conn_handle = event_info.conn_handle;
|
|
|
|
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
|
2019-11-20 16:53:50 -05:00
|
|
|
mp_raise_bleio_BluetoothError(translate("Failed to connect: timeout"));
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
2021-05-19 19:22:07 -04:00
|
|
|
// If we have keys, then try and encrypt the connection.
|
|
|
|
const ble_gap_enc_key_t *encryption_key = bonding_load_peer_encryption_key(true, &addr);
|
|
|
|
pair_status_t pair_status = PAIR_NOT_PAIRED;
|
|
|
|
if (encryption_key != NULL) {
|
|
|
|
err_code = sd_ble_gap_encrypt(conn_handle, &encryption_key->master_id, &encryption_key->enc_info);
|
|
|
|
pair_status = PAIR_WAITING;
|
|
|
|
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
pair_status = PAIR_NOT_PAIRED;
|
|
|
|
}
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
|
2019-12-06 15:27:46 -05:00
|
|
|
// Negotiate for better PHY, larger MTU and data lengths since we are the central. These are
|
|
|
|
// nice-to-haves so ignore any errors.
|
|
|
|
ble_gap_phys_t const phys = {
|
|
|
|
.rx_phys = BLE_GAP_PHY_AUTO,
|
|
|
|
.tx_phys = BLE_GAP_PHY_AUTO,
|
|
|
|
};
|
|
|
|
sd_ble_gap_phy_update(conn_handle, &phys);
|
|
|
|
sd_ble_gattc_exchange_mtu_request(conn_handle, BLE_GATTS_VAR_ATTR_LEN_MAX);
|
|
|
|
sd_ble_gap_data_length_update(conn_handle, NULL, NULL);
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
// Make the connection object and return it.
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_connection_internal_t *connection = &bleio_connections[i];
|
|
|
|
if (connection->conn_handle == conn_handle) {
|
2020-08-11 16:21:16 -04:00
|
|
|
connection->is_central = true;
|
2021-05-19 19:22:07 -04:00
|
|
|
connection->pair_status = pair_status;
|
2019-09-14 15:40:24 -04:00
|
|
|
return bleio_connection_new_from_internal(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 16:53:50 -05:00
|
|
|
mp_raise_bleio_BluetoothError(translate("Failed to connect: internal error"));
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
return mp_const_none;
|
2019-07-31 00:30:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-19 16:55:58 -05:00
|
|
|
STATIC void check_data_fit(size_t data_len, bool connectable) {
|
|
|
|
if (data_len > BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED ||
|
|
|
|
(connectable && data_len > BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED)) {
|
2019-09-14 15:40:24 -04:00
|
|
|
mp_raise_ValueError(translate("Data too large for advertisement packet"));
|
|
|
|
}
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:21:48 -04:00
|
|
|
// The nRF SD 6.1.0 can only do one concurrent advertisement so share the advertising handle.
|
|
|
|
uint8_t adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
|
|
|
|
|
2020-05-19 02:56:04 -04:00
|
|
|
STATIC bool advertising_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
|
2021-03-15 09:57:36 -04:00
|
|
|
bleio_adapter_obj_t *self = (bleio_adapter_obj_t *)self_in;
|
2020-05-19 02:56:04 -04:00
|
|
|
|
|
|
|
switch (ble_evt->header.evt_id) {
|
2021-05-19 19:22:07 -04:00
|
|
|
case BLE_GAP_EVT_CONNECTED: // Connecting also stops an advertisement.
|
|
|
|
// Set the tx_power for the connection higher than the advertisement.
|
|
|
|
sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN, ble_evt->evt.gap_evt.conn_handle, 0);
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
|
|
|
return false;
|
|
|
|
break;
|
2020-05-19 02:56:04 -04:00
|
|
|
case BLE_GAP_EVT_ADV_SET_TERMINATED:
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// For debugging.
|
2020-05-19 09:03:56 -04:00
|
|
|
// mp_printf(&mp_plat_print, "Unhandled advertising event: 0x%04x\n", ble_evt->header.evt_id);
|
2020-05-19 02:56:04 -04:00
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
|
|
|
|
bool connectable, bool anonymous, uint32_t timeout, float interval,
|
|
|
|
const uint8_t *advertising_data, uint16_t advertising_data_len,
|
|
|
|
const uint8_t *scan_response_data, uint16_t scan_response_data_len,
|
|
|
|
mp_int_t tx_power, const bleio_address_obj_t *directed_to) {
|
2019-09-14 15:40:24 -04:00
|
|
|
if (self->current_advertising_data != NULL && self->current_advertising_data == self->advertising_data) {
|
|
|
|
return NRF_ERROR_BUSY;
|
|
|
|
}
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
// If the current advertising data isn't owned by the adapter then it must be an internal
|
|
|
|
// advertisement that we should stop.
|
|
|
|
if (self->current_advertising_data != NULL) {
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
|
|
|
}
|
|
|
|
|
2020-05-17 10:06:21 -04:00
|
|
|
uint32_t err_code;
|
2019-11-19 16:55:58 -05:00
|
|
|
bool extended = advertising_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX ||
|
2021-03-15 09:57:36 -04:00
|
|
|
scan_response_data_len > BLE_GAP_ADV_SET_DATA_SIZE_MAX;
|
2019-11-19 16:55:58 -05:00
|
|
|
|
|
|
|
uint8_t adv_type;
|
2021-05-19 19:22:07 -04:00
|
|
|
ble_gap_addr_t *peer = NULL;
|
|
|
|
ble_gap_addr_t peer_address;
|
2019-11-19 16:55:58 -05:00
|
|
|
if (extended) {
|
|
|
|
if (connectable) {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_EXTENDED_CONNECTABLE_NONSCANNABLE_UNDIRECTED;
|
|
|
|
} else if (scan_response_data_len > 0) {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_EXTENDED_NONCONNECTABLE_SCANNABLE_UNDIRECTED;
|
|
|
|
} else {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_EXTENDED_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
|
|
|
|
}
|
|
|
|
} else if (connectable) {
|
2021-05-19 19:22:07 -04:00
|
|
|
if (directed_to == NULL) {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
|
|
|
|
} else if (interval <= 3.5 && timeout <= 1.3) {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED_HIGH_DUTY_CYCLE;
|
|
|
|
_convert_address(directed_to, &peer_address);
|
|
|
|
peer = &peer_address;
|
|
|
|
} else {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED;
|
|
|
|
_convert_address(directed_to, &peer_address);
|
|
|
|
peer = &peer_address;
|
|
|
|
}
|
2019-11-19 16:55:58 -05:00
|
|
|
} else if (scan_response_data_len > 0) {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_SCANNABLE_UNDIRECTED;
|
|
|
|
} else {
|
|
|
|
adv_type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
|
|
|
|
}
|
|
|
|
|
2020-05-17 10:06:21 -04:00
|
|
|
if (anonymous) {
|
|
|
|
ble_gap_privacy_params_t privacy = {
|
|
|
|
.privacy_mode = BLE_GAP_PRIVACY_MODE_DEVICE_PRIVACY,
|
|
|
|
.private_addr_type = BLE_GAP_ADDR_TYPE_RANDOM_PRIVATE_RESOLVABLE,
|
2020-05-19 02:56:04 -04:00
|
|
|
// Rotate the keys one second after we're scheduled to stop
|
|
|
|
// advertising. This prevents a potential race condition where we
|
|
|
|
// fire off a beacon with the same advertising data but a new MAC
|
|
|
|
// address just as we tear down the connection.
|
|
|
|
.private_addr_cycle_s = timeout + 1,
|
2020-05-17 10:06:21 -04:00
|
|
|
.p_device_irk = NULL,
|
|
|
|
};
|
|
|
|
err_code = sd_ble_gap_privacy_set(&privacy);
|
|
|
|
} else {
|
|
|
|
ble_gap_privacy_params_t privacy = {
|
|
|
|
.privacy_mode = BLE_GAP_PRIVACY_MODE_OFF,
|
|
|
|
.private_addr_type = BLE_GAP_ADDR_TYPE_PUBLIC,
|
|
|
|
.private_addr_cycle_s = 0,
|
|
|
|
.p_device_irk = NULL,
|
|
|
|
};
|
|
|
|
err_code = sd_ble_gap_privacy_set(&privacy);
|
|
|
|
}
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
ble_gap_adv_params_t adv_params = {
|
2021-01-10 13:16:19 -05:00
|
|
|
.interval = SEC_TO_UNITS(interval, UNIT_0_625_MS) + 0.5f,
|
2019-11-19 16:55:58 -05:00
|
|
|
.properties.type = adv_type,
|
2020-05-19 02:56:04 -04:00
|
|
|
.duration = SEC_TO_UNITS(timeout, UNIT_10_MS),
|
2019-09-14 15:40:24 -04:00
|
|
|
.filter_policy = BLE_GAP_ADV_FP_ANY,
|
|
|
|
.primary_phy = BLE_GAP_PHY_1MBPS,
|
2021-05-19 19:22:07 -04:00
|
|
|
.p_peer_addr = peer,
|
2019-09-14 15:40:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const ble_gap_adv_data_t ble_gap_adv_data = {
|
2021-05-19 19:22:07 -04:00
|
|
|
.adv_data.p_data = (uint8_t *)advertising_data,
|
2019-09-14 15:40:24 -04:00
|
|
|
.adv_data.len = advertising_data_len,
|
2021-05-19 19:22:07 -04:00
|
|
|
.scan_rsp_data.p_data = scan_response_data_len > 0 ? (uint8_t *)scan_response_data : NULL,
|
2019-09-14 15:40:24 -04:00
|
|
|
.scan_rsp_data.len = scan_response_data_len,
|
|
|
|
};
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
// Update the identities of central peers so they can use a private address
|
|
|
|
// in the scan and connection initiation.
|
|
|
|
err_code = _update_identities(false);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
err_code = sd_ble_gap_adv_set_configure(&adv_handle, &ble_gap_adv_data, &adv_params);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
ble_drv_add_event_handler_entry(&self->advertising_handler_entry, advertising_on_ble_evt, self);
|
|
|
|
|
2021-05-19 15:21:48 -04:00
|
|
|
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, adv_handle, tx_power);
|
2020-09-30 13:03:30 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
return err_code;
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
err_code = sd_ble_gap_adv_start(adv_handle, BLE_CONN_CFG_TAG_CUSTOM);
|
2018-07-22 10:06:19 -04:00
|
|
|
if (err_code != NRF_SUCCESS) {
|
2019-09-14 15:40:24 -04:00
|
|
|
return err_code;
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
self->current_advertising_data = advertising_data;
|
|
|
|
return NRF_SUCCESS;
|
2019-07-31 00:30:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable,
|
|
|
|
bool anonymous, uint32_t timeout, mp_float_t interval,
|
|
|
|
mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo,
|
|
|
|
mp_int_t tx_power, const bleio_address_obj_t *directed_to) {
|
|
|
|
if (self->user_advertising) {
|
2019-11-20 16:53:50 -05:00
|
|
|
mp_raise_bleio_BluetoothError(translate("Already advertising."));
|
2021-05-19 19:22:07 -04:00
|
|
|
} else if (self->current_advertising_data != NULL) {
|
|
|
|
// If the user isn't advertising, then the background is. So, stop the
|
|
|
|
// background advertising so the user can.
|
|
|
|
common_hal_bleio_adapter_stop_advertising(self);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
// interval value has already been validated.
|
|
|
|
|
2019-11-19 16:55:58 -05:00
|
|
|
check_data_fit(advertising_data_bufinfo->len, connectable);
|
|
|
|
check_data_fit(scan_response_data_bufinfo->len, connectable);
|
2020-02-05 18:54:31 -05:00
|
|
|
|
|
|
|
if (advertising_data_bufinfo->len > 31 && scan_response_data_bufinfo->len > 0) {
|
|
|
|
mp_raise_bleio_BluetoothError(translate("Extended advertisements with scan response not supported."));
|
|
|
|
}
|
2020-05-19 02:56:04 -04:00
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
|
|
|
|
if (advertising_data_bufinfo->len > 0 && directed_to != NULL) {
|
|
|
|
mp_raise_bleio_BluetoothError(translate("Data not supported with directed advertising"));
|
|
|
|
}
|
|
|
|
|
2020-05-19 02:56:04 -04:00
|
|
|
// Anonymous mode requires a timeout so that we don't continue to broadcast
|
|
|
|
// the same data while cycling the MAC address -- otherwise, what's the
|
|
|
|
// point of randomizing the MAC address?
|
|
|
|
if (!timeout) {
|
|
|
|
if (anonymous) {
|
|
|
|
// The Nordic macro is in units of 10ms. Convert to seconds.
|
2020-05-19 09:03:56 -04:00
|
|
|
uint32_t adv_timeout_max_secs = UNITS_TO_SEC(BLE_GAP_ADV_TIMEOUT_LIMITED_MAX, UNIT_10_MS);
|
2020-05-19 02:56:04 -04:00
|
|
|
uint32_t rotate_timeout_max_secs = BLE_GAP_DEFAULT_PRIVATE_ADDR_CYCLE_INTERVAL_S;
|
|
|
|
timeout = MIN(adv_timeout_max_secs, rotate_timeout_max_secs);
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
2020-05-19 02:56:04 -04:00
|
|
|
timeout = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (SEC_TO_UNITS(timeout, UNIT_10_MS) > BLE_GAP_ADV_TIMEOUT_LIMITED_MAX) {
|
|
|
|
mp_raise_bleio_BluetoothError(translate("Timeout is too long: Maximum timeout length is %d seconds"),
|
2021-03-15 09:57:36 -04:00
|
|
|
UNITS_TO_SEC(BLE_GAP_ADV_TIMEOUT_LIMITED_MAX, UNIT_10_MS));
|
2020-05-19 02:56:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
// The advertising data buffers must not move, because the SoftDevice depends on them.
|
|
|
|
// So make them long-lived and reuse them onwards.
|
|
|
|
if (self->advertising_data == NULL) {
|
2021-03-15 09:57:36 -04:00
|
|
|
self->advertising_data = (uint8_t *)gc_alloc(BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED * sizeof(uint8_t), false, true);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
if (self->scan_response_data == NULL) {
|
2021-03-15 09:57:36 -04:00
|
|
|
self->scan_response_data = (uint8_t *)gc_alloc(BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED * sizeof(uint8_t), false, true);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
memcpy(self->advertising_data, advertising_data_bufinfo->buf, advertising_data_bufinfo->len);
|
|
|
|
memcpy(self->scan_response_data, scan_response_data_bufinfo->buf, scan_response_data_bufinfo->len);
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2020-05-19 02:56:04 -04:00
|
|
|
check_nrf_error(_common_hal_bleio_adapter_start_advertising(self, connectable, anonymous, timeout, interval,
|
2021-03-15 09:57:36 -04:00
|
|
|
self->advertising_data,
|
|
|
|
advertising_data_bufinfo->len,
|
|
|
|
self->scan_response_data,
|
2021-05-19 15:21:48 -04:00
|
|
|
scan_response_data_bufinfo->len,
|
2021-05-19 19:22:07 -04:00
|
|
|
tx_power,
|
|
|
|
directed_to));
|
|
|
|
self->user_advertising = true;
|
2019-07-31 00:30:24 -04:00
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (adv_handle == BLE_GAP_ADV_SET_HANDLE_NOT_SET) {
|
2019-09-14 15:40:24 -04:00
|
|
|
return;
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
|
|
|
|
const uint32_t err_code = sd_ble_gap_adv_stop(adv_handle);
|
2021-05-19 19:22:07 -04:00
|
|
|
ble_drv_remove_event_handler(advertising_on_ble_evt, self);
|
2019-09-14 15:40:24 -04:00
|
|
|
self->current_advertising_data = NULL;
|
2021-05-19 19:22:07 -04:00
|
|
|
self->user_advertising = false;
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_INVALID_STATE)) {
|
2019-11-20 16:53:50 -05:00
|
|
|
check_nrf_error(err_code);
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 00:30:24 -04:00
|
|
|
|
2020-05-19 02:56:04 -04:00
|
|
|
bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self) {
|
|
|
|
return self->current_advertising_data != NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self) {
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_connection_internal_t *connection = &bleio_connections[i];
|
2019-09-14 15:40:24 -04:00
|
|
|
if (connection->conn_handle != BLE_CONN_HANDLE_INVALID) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-13 21:55:07 -04:00
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
mp_obj_t common_hal_bleio_adapter_get_connections(bleio_adapter_obj_t *self) {
|
|
|
|
if (self->connection_objs != NULL) {
|
|
|
|
return self->connection_objs;
|
|
|
|
}
|
|
|
|
size_t total_connected = 0;
|
|
|
|
mp_obj_t items[BLEIO_TOTAL_CONNECTION_COUNT];
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_connection_internal_t *connection = &bleio_connections[i];
|
2019-09-14 15:40:24 -04:00
|
|
|
if (connection->conn_handle != BLE_CONN_HANDLE_INVALID) {
|
|
|
|
if (connection->connection_obj == mp_const_none) {
|
|
|
|
connection->connection_obj = bleio_connection_new_from_internal(connection);
|
|
|
|
}
|
|
|
|
items[total_connected] = connection->connection_obj;
|
|
|
|
total_connected++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self->connection_objs = mp_obj_new_tuple(total_connected, items);
|
|
|
|
return self->connection_objs;
|
|
|
|
}
|
2018-07-22 10:06:19 -04:00
|
|
|
|
2020-01-10 23:55:45 -05:00
|
|
|
void common_hal_bleio_adapter_erase_bonding(bleio_adapter_obj_t *self) {
|
|
|
|
bonding_erase_storage();
|
|
|
|
}
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
bool common_hal_bleio_adapter_is_bonded_to_central(bleio_adapter_obj_t *self) {
|
|
|
|
return bonding_peripheral_bond_count() > 0;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void bleio_adapter_gc_collect(bleio_adapter_obj_t *adapter) {
|
2021-07-28 11:50:11 -04:00
|
|
|
// We divide by size_t so that we can scan each 32-bit aligned value to see
|
|
|
|
// if it is a pointer. This allows us to change the structs without worrying
|
|
|
|
// about collecting new pointers.
|
|
|
|
gc_collect_root((void **)adapter, sizeof(bleio_adapter_obj_t) / (sizeof(size_t)));
|
|
|
|
gc_collect_root((void **)bleio_connections, sizeof(bleio_connections) / (sizeof(size_t)));
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2019-06-13 21:55:07 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void bleio_adapter_reset(bleio_adapter_obj_t *adapter) {
|
2019-09-14 15:40:24 -04:00
|
|
|
common_hal_bleio_adapter_stop_scan(adapter);
|
2020-02-24 19:11:17 -05:00
|
|
|
if (adapter->current_advertising_data != NULL) {
|
|
|
|
common_hal_bleio_adapter_stop_advertising(adapter);
|
|
|
|
}
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
adapter->connection_objs = NULL;
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
2019-12-06 15:27:46 -05:00
|
|
|
bleio_connection_internal_t *connection = &bleio_connections[i];
|
2021-07-08 22:11:05 -04:00
|
|
|
// Disconnect all connections cleanly.
|
|
|
|
if (connection->conn_handle != BLE_CONN_HANDLE_INVALID) {
|
2020-01-30 09:52:06 -05:00
|
|
|
common_hal_bleio_connection_disconnect(connection);
|
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
connection->connection_obj = mp_const_none;
|
|
|
|
}
|
2021-07-08 22:11:05 -04:00
|
|
|
|
|
|
|
// Wait up to 125 ms (128 ticks) for disconnect to complete. This should be
|
|
|
|
// greater than most connection intervals.
|
|
|
|
bool any_connected = false;
|
|
|
|
uint64_t start_ticks = supervisor_ticks_ms64();
|
|
|
|
while (any_connected && supervisor_ticks_ms64() - start_ticks < 128) {
|
|
|
|
any_connected = false;
|
|
|
|
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
|
|
|
|
bleio_connection_internal_t *connection = &bleio_connections[i];
|
|
|
|
any_connected |= connection->conn_handle != BLE_CONN_HANDLE_INVALID;
|
|
|
|
}
|
|
|
|
}
|
2018-07-08 15:13:05 -04:00
|
|
|
}
|