circuitpython/supervisor/shared/bluetooth/bluetooth.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

375 lines
13 KiB
C
Raw Normal View History

2021-06-25 17:58:32 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <string.h>
#include "supervisor/shared/bluetooth/bluetooth.h"
#include "shared-bindings/_bleio/__init__.h"
#include "shared-bindings/_bleio/Adapter.h"
#if defined(CIRCUITPY_BOOT_BUTTON)
#include "shared-bindings/digitalio/DigitalInOut.h"
#endif
#include "shared-bindings/microcontroller/Processor.h"
#include "shared-bindings/microcontroller/ResetReason.h"
#include "shared-module/storage/__init__.h"
#include "common-hal/_bleio/__init__.h"
2022-08-03 17:35:28 -04:00
#include "supervisor/serial.h"
2021-06-25 17:58:32 -04:00
#include "supervisor/shared/status_leds.h"
#include "supervisor/shared/tick.h"
2022-08-03 17:35:28 -04:00
#include "supervisor/shared/translate/translate.h"
2021-06-25 17:58:32 -04:00
#include "py/mpstate.h"
#if CIRCUITPY_BLE_FILE_SERVICE
#include "supervisor/shared/bluetooth/file_transfer.h"
2021-06-25 19:40:23 -04:00
#include "bluetooth/ble_drv.h"
2021-06-25 17:58:32 -04:00
#endif
#if CIRCUITPY_SERIAL_BLE
#include "supervisor/shared/bluetooth/serial.h"
2021-06-25 19:40:23 -04:00
#include "bluetooth/ble_drv.h"
2021-06-25 17:58:32 -04:00
#endif
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
#include "supervisor/shared/status_bar.h"
#endif
2021-06-25 17:58:32 -04:00
// This standard advertisement advertises the CircuitPython editing service and a CIRCUITPY short name.
const uint8_t public_advertising_data[] = { 0x02, 0x01, 0x06, // 0-2 Flags
0x02, 0x0a, 0xec, // 3-5 TX power level -20
2021-06-25 17:58:32 -04:00
#if CIRCUITPY_BLE_FILE_SERVICE
0x03, 0x02, 0xbb, 0xfe, // 6 - 9 Incomplete service list (File Transfer service)
#endif
0x0e, 0xff, 0x22, 0x08, // 10 - 13 Adafruit Manufacturer Data
0x0a, 0x04, 0x00, // 14 - 16 Creator ID / Creation ID
CIRCUITPY_CREATOR_ID & 0xff, // 17 - 20 Creator ID
(CIRCUITPY_CREATOR_ID >> 8) & 0xff,
(CIRCUITPY_CREATOR_ID >> 16) & 0xff,
(CIRCUITPY_CREATOR_ID >> 24) & 0xff,
CIRCUITPY_CREATION_ID & 0xff, // 21 - 24 Creation ID
(CIRCUITPY_CREATION_ID >> 8) & 0xff,
(CIRCUITPY_CREATION_ID >> 16) & 0xff,
(CIRCUITPY_CREATION_ID >> 24) & 0xff,
0x05, 0x08, 0x43, 0x49, 0x52, 0x43 // 25 - 31 - Short name
};
const uint8_t private_advertising_data[] = { 0x02, 0x01, 0x06, // 0-2 Flags
0x02, 0x0a, 0x00 // 3-5 TX power level 0
};
2022-08-03 17:35:28 -04:00
// This scan response advertises the full device name (if it fits.)
uint8_t circuitpython_scan_response_data[31];
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
STATIC bool boot_in_discovery_mode = false;
STATIC bool advertising = false;
2022-08-03 17:35:28 -04:00
STATIC bool _private_advertising = false;
STATIC bool ble_started = false;
#define WORKFLOW_UNSET 0
#define WORKFLOW_ENABLED 1
#define WORKFLOW_DISABLED 2
STATIC uint8_t workflow_state = WORKFLOW_UNSET;
STATIC bool was_connected = false;
2021-06-25 17:58:32 -04:00
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
2022-08-03 17:35:28 -04:00
// To detect when the title bar changes.
STATIC bool _last_connected = false;
STATIC bool _last_advertising = false;
2022-08-28 21:55:13 -04:00
#endif
2022-08-03 17:35:28 -04:00
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
2022-08-03 17:35:28 -04:00
// Title bar status
bool supervisor_bluetooth_status_dirty(void) {
return _last_advertising != advertising ||
_last_connected != was_connected;
}
2022-08-28 21:55:13 -04:00
#endif
2022-08-03 17:35:28 -04:00
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
2022-08-03 17:35:28 -04:00
void supervisor_bluetooth_status(void) {
serial_write("BLE:");
if (advertising) {
if (_private_advertising) {
serial_write_compressed(MP_ERROR_TEXT("Reconnecting"));
2022-08-03 17:35:28 -04:00
} else {
const char *name = (char *)circuitpython_scan_response_data + 2;
int len = MIN(strlen(name), sizeof(circuitpython_scan_response_data) - 2);
serial_write_substring(name, len);
}
} else if (was_connected) {
serial_write_compressed(MP_ERROR_TEXT("Ok"));
2022-08-03 17:35:28 -04:00
} else {
serial_write_compressed(MP_ERROR_TEXT("Off"));
2022-08-03 17:35:28 -04:00
}
_last_connected = was_connected;
_last_advertising = advertising;
}
2022-08-28 21:55:13 -04:00
#endif
2022-08-03 17:35:28 -04:00
2021-06-25 17:58:32 -04:00
STATIC void supervisor_bluetooth_start_advertising(void) {
if (workflow_state != WORKFLOW_ENABLED) {
return;
}
2021-06-25 17:58:32 -04:00
bool is_connected = common_hal_bleio_adapter_get_connected(&common_hal_bleio_adapter_obj);
if (is_connected) {
return;
}
bool bonded = common_hal_bleio_adapter_is_bonded_to_central(&common_hal_bleio_adapter_obj);
#if CIRCUITPY_USB
// Don't advertise when we have USB instead of BLE.
if (!bonded && !boot_in_discovery_mode) {
return;
}
#endif
uint32_t timeout = 0;
float interval = 0.1f;
int tx_power = 0;
const uint8_t *adv = private_advertising_data;
size_t adv_len = sizeof(private_advertising_data);
const uint8_t *scan_response = NULL;
size_t scan_response_len = 0;
2022-08-03 17:35:28 -04:00
_private_advertising = true;
2021-06-25 17:58:32 -04:00
// Advertise with less power when doing so publicly to reduce who can hear us. This will make it
// harder for someone with bad intentions to pair from a distance.
if (!bonded || boot_in_discovery_mode) {
tx_power = -20;
2021-06-25 17:58:32 -04:00
adv = public_advertising_data;
adv_len = sizeof(public_advertising_data);
scan_response = circuitpython_scan_response_data;
scan_response_len = sizeof(circuitpython_scan_response_data);
2022-08-03 17:35:28 -04:00
uint16_t max_name_len = sizeof(circuitpython_scan_response_data) - 2;
uint16_t name_len = bleio_adapter_get_name((char *)circuitpython_scan_response_data + 2,
max_name_len);
if (name_len > max_name_len) {
circuitpython_scan_response_data[0] = max_name_len + 1;
circuitpython_scan_response_data[1] = 0x8;
} else {
circuitpython_scan_response_data[0] = name_len + 1;
circuitpython_scan_response_data[1] = 0x9;
}
scan_response_len = circuitpython_scan_response_data[0] + 1;
_private_advertising = false;
2021-06-25 17:58:32 -04:00
}
uint32_t status = _common_hal_bleio_adapter_start_advertising(&common_hal_bleio_adapter_obj,
true,
_private_advertising, // Advertise anonymously if we are privately advertising
2021-06-25 17:58:32 -04:00
timeout,
interval,
adv,
adv_len,
scan_response,
scan_response_len,
tx_power,
NULL);
// This may fail if we are already advertising.
advertising = status == NRF_SUCCESS;
}
2021-08-03 17:36:41 -04:00
#endif // CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
2021-06-25 17:58:32 -04:00
#define BLE_DISCOVERY_DATA_GUARD 0xbb0000bb
#define BLE_DISCOVERY_DATA_GUARD_MASK 0xff0000ff
void supervisor_bluetooth_init(void) {
2021-08-03 17:36:41 -04:00
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
2021-06-25 17:58:32 -04:00
uint32_t reset_state = port_get_saved_word();
uint32_t ble_mode = 0;
if ((reset_state & BLE_DISCOVERY_DATA_GUARD_MASK) == BLE_DISCOVERY_DATA_GUARD) {
ble_mode = (reset_state & ~BLE_DISCOVERY_DATA_GUARD_MASK) >> 8;
}
2021-06-29 14:54:15 -04:00
const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason();
2021-06-25 17:58:32 -04:00
boot_in_discovery_mode = false;
2021-06-29 14:54:15 -04:00
if (reset_reason != RESET_REASON_POWER_ON &&
reset_reason != RESET_REASON_RESET_PIN &&
reset_reason != RESET_REASON_UNKNOWN &&
reset_reason != RESET_REASON_SOFTWARE) {
return;
}
2021-06-25 17:58:32 -04:00
if (ble_mode == 0) {
port_set_saved_word(BLE_DISCOVERY_DATA_GUARD | (0x01 << 8));
}
// Wait for a while to allow for reset.
#ifdef CIRCUITPY_BOOT_BUTTON
digitalio_digitalinout_obj_t boot_button;
common_hal_digitalio_digitalinout_construct(&boot_button, CIRCUITPY_BOOT_BUTTON);
common_hal_digitalio_digitalinout_switch_to_input(&boot_button, PULL_UP);
#endif
#if CIRCUITPY_STATUS_LED
status_led_init();
#endif
2021-06-25 17:58:32 -04:00
uint64_t start_ticks = supervisor_ticks_ms64();
uint64_t diff = 0;
if (ble_mode != 0) {
#ifdef CIRCUITPY_STATUS_LED
new_status_color(0x0000ff);
#endif
common_hal_bleio_adapter_erase_bonding(&common_hal_bleio_adapter_obj);
boot_in_discovery_mode = true;
reset_state = 0x0;
}
#if !CIRCUITPY_USB
// Boot into discovery if USB isn't available and we aren't bonded already.
// Checking here allows us to have the status LED solidly on even if no button was
// pressed.
bool bonded = common_hal_bleio_adapter_is_bonded_to_central(&common_hal_bleio_adapter_obj);
if (!bonded) {
boot_in_discovery_mode = true;
}
#endif
2021-06-25 17:58:32 -04:00
while (diff < 1000) {
#ifdef CIRCUITPY_STATUS_LED
// Blink on for 50 and off for 100
bool led_on = boot_in_discovery_mode || (diff % 150) <= 50;
2021-06-25 17:58:32 -04:00
if (led_on) {
new_status_color(0x0000ff);
} else {
new_status_color(BLACK);
}
#endif
#ifdef CIRCUITPY_BOOT_BUTTON
if (!common_hal_digitalio_digitalinout_get_value(&boot_button)) {
boot_in_discovery_mode = true;
break;
}
#endif
diff = supervisor_ticks_ms64() - start_ticks;
}
#if CIRCUITPY_STATUS_LED
new_status_color(BLACK);
status_led_deinit();
#endif
port_set_saved_word(reset_state);
2021-08-03 17:36:41 -04:00
#endif
2021-06-25 17:58:32 -04:00
}
void supervisor_bluetooth_background(void) {
2021-08-03 17:36:41 -04:00
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
if (!ble_started) {
return;
}
2021-06-25 17:58:32 -04:00
bool is_connected = common_hal_bleio_adapter_get_connected(&common_hal_bleio_adapter_obj);
if (was_connected && !is_connected) {
#if CIRCUITPY_BLE_FILE_SERVICE
supervisor_bluetooth_file_transfer_disconnected();
#endif
}
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
2022-08-03 17:35:28 -04:00
if (was_connected != is_connected) {
2022-08-28 21:55:13 -04:00
supervisor_status_bar_request_update(false);
2022-08-03 17:35:28 -04:00
}
2022-08-28 21:55:13 -04:00
#endif
2021-06-25 17:58:32 -04:00
was_connected = is_connected;
if (!is_connected) {
supervisor_bluetooth_start_advertising();
return;
} else {
advertising = false;
2021-06-25 17:58:32 -04:00
}
#if CIRCUITPY_BLE_FILE_SERVICE
supervisor_bluetooth_file_transfer_background();
#endif
2021-08-03 17:36:41 -04:00
#endif
2021-06-25 17:58:32 -04:00
}
void supervisor_start_bluetooth(void) {
2021-08-03 17:36:41 -04:00
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
2021-06-25 17:58:32 -04:00
if (workflow_state != WORKFLOW_ENABLED || ble_started) {
return;
}
2021-06-25 17:58:32 -04:00
common_hal_bleio_adapter_set_enabled(&common_hal_bleio_adapter_obj, true);
#if CIRCUITPY_BLE_FILE_SERVICE
supervisor_start_bluetooth_file_transfer();
#endif
#if CIRCUITPY_SERIAL_BLE
supervisor_start_bluetooth_serial();
#endif
// Mark as started so that the background call does something.
ble_started = true;
// Kick off advertisements
2021-06-25 17:58:32 -04:00
supervisor_bluetooth_background();
2022-08-28 21:55:13 -04:00
#if CIRCUITPY_STATUS_BAR
supervisor_status_bar_request_update(false);
#endif
2021-08-03 17:36:41 -04:00
#endif
2021-06-25 17:58:32 -04:00
}
void supervisor_stop_bluetooth(void) {
2021-08-03 17:36:41 -04:00
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
if (!ble_started && workflow_state != WORKFLOW_ENABLED) {
return;
}
ble_started = false;
#if CIRCUITPY_SERIAL_BLE
supervisor_stop_bluetooth_serial();
#endif
#endif
2021-08-03 17:36:41 -04:00
}
2021-08-03 17:36:41 -04:00
void supervisor_bluetooth_enable_workflow(void) {
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
if (workflow_state == WORKFLOW_DISABLED) {
return;
}
workflow_state = WORKFLOW_ENABLED;
2021-08-03 17:36:41 -04:00
#endif
}
void supervisor_bluetooth_disable_workflow(void) {
2021-08-03 17:36:41 -04:00
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
workflow_state = WORKFLOW_DISABLED;
2021-08-03 17:36:41 -04:00
#endif
}
bool supervisor_bluetooth_workflow_is_enabled(void) {
#if CIRCUITPY_BLE_FILE_SERVICE || CIRCUITPY_SERIAL_BLE
if (workflow_state == 1) {
return true;
}
#endif
return false;
}