|
|
|
|
@@ -15,9 +15,7 @@
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
#include "NimBLEAdvertising.h"
|
|
|
|
|
#ifdef CONFIG_BT_NIMBLE_EXT_ADV
|
|
|
|
|
#include "NimBLEExtAdvertising.h"
|
|
|
|
|
#endif
|
|
|
|
|
#include "PowerStatus.h"
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_NIMBLE_CPP_IDF)
|
|
|
|
|
@@ -312,7 +310,9 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG_NIMBLE_NOTIFY
|
|
|
|
|
int currentNotifyCount = notifyCount.fetch_add(1);
|
|
|
|
|
|
|
|
|
|
uint8_t cc = bleServer->getConnectedCount();
|
|
|
|
|
|
|
|
|
|
// This logging slows things down when there are lots of packets going to the phone, like initial connection:
|
|
|
|
|
LOG_DEBUG("BLE notify(%d) fromNum: %d connections: %d", currentNotifyCount, fromRadioNum, cc);
|
|
|
|
|
#endif
|
|
|
|
|
@@ -386,8 +386,9 @@ static uint8_t lastToRadio[MAX_TO_FROM_RADIO_SIZE];
|
|
|
|
|
|
|
|
|
|
class NimbleBluetoothToRadioCallback : public NimBLECharacteristicCallbacks
|
|
|
|
|
{
|
|
|
|
|
void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &) override
|
|
|
|
|
virtual void onWrite(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
|
|
|
|
|
{
|
|
|
|
|
(void)connInfo;
|
|
|
|
|
// CAUTION: This callback runs in the NimBLE task!!! Don't do anything except communicate with the main task's runOnce.
|
|
|
|
|
// Assumption: onWrite is serialized by NimBLE, so we don't need to lock here against multiple concurrent onWrite calls.
|
|
|
|
|
|
|
|
|
|
@@ -433,8 +434,9 @@ class NimbleBluetoothToRadioCallback : public NimBLECharacteristicCallbacks
|
|
|
|
|
|
|
|
|
|
class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks
|
|
|
|
|
{
|
|
|
|
|
void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &) override
|
|
|
|
|
virtual void onRead(NimBLECharacteristic *pCharacteristic, NimBLEConnInfo &connInfo)
|
|
|
|
|
{
|
|
|
|
|
(void)connInfo;
|
|
|
|
|
// CAUTION: This callback runs in the NimBLE task!!! Don't do anything except communicate with the main task's runOnce.
|
|
|
|
|
|
|
|
|
|
int currentReadCount = bluetoothPhoneAPI->readCount.fetch_add(1);
|
|
|
|
|
@@ -553,15 +555,16 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
|
|
|
|
|
|
|
|
|
|
if (config.bluetooth.mode == meshtastic_Config_BluetoothConfig_PairingMode_RANDOM_PIN) {
|
|
|
|
|
LOG_INFO("Use random passkey");
|
|
|
|
|
// This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits
|
|
|
|
|
passkey = random(100000, 999999);
|
|
|
|
|
}
|
|
|
|
|
LOG_INFO("*** Enter passkey %06u on the peer side ***", passkey);
|
|
|
|
|
LOG_INFO("*** Enter passkey %d on the peer side ***", passkey);
|
|
|
|
|
|
|
|
|
|
powerFSM.trigger(EVENT_BLUETOOTH_PAIR);
|
|
|
|
|
meshtastic::BluetoothStatus newStatus(std::to_string(passkey));
|
|
|
|
|
bluetoothStatus->updateStatus(&newStatus);
|
|
|
|
|
|
|
|
|
|
#if HAS_SCREEN
|
|
|
|
|
#if HAS_SCREEN // Todo: migrate this display code back into Screen class, and observe bluetoothStatus
|
|
|
|
|
if (screen) {
|
|
|
|
|
screen->startAlert([passkey](OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) -> void {
|
|
|
|
|
char btPIN[16] = "888888";
|
|
|
|
|
@@ -590,8 +593,8 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
passkeyShowing = true;
|
|
|
|
|
|
|
|
|
|
return passkey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -602,13 +605,14 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
|
|
|
|
|
meshtastic::BluetoothStatus newStatus(meshtastic::BluetoothStatus::ConnectionState::CONNECTED);
|
|
|
|
|
bluetoothStatus->updateStatus(&newStatus);
|
|
|
|
|
|
|
|
|
|
// Todo: migrate this display code back into Screen class, and observe bluetoothStatus
|
|
|
|
|
if (passkeyShowing) {
|
|
|
|
|
passkeyShowing = false;
|
|
|
|
|
if (screen) {
|
|
|
|
|
if (screen)
|
|
|
|
|
screen->endAlert();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the connection handle for future use
|
|
|
|
|
nimbleBluetoothConnHandle = connInfo.getConnHandle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -637,9 +641,10 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
|
|
|
|
|
LOG_INFO("BLE conn %u initial MTU %u (target %u)", connHandle, connInfo.getMTU(), kPreferredBleMtu);
|
|
|
|
|
pServer->updateConnParams(connHandle, 6, 12, 0, 200);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onDisconnect(NimBLEServer *pServer, NimBLEConnInfo &connInfo, int reason) override
|
|
|
|
|
{
|
|
|
|
|
(void)pServer;
|
|
|
|
|
(void)connInfo;
|
|
|
|
|
LOG_INFO("BLE disconnect reason: %d", reason);
|
|
|
|
|
if (ble->isDeInit)
|
|
|
|
|
return;
|
|
|
|
|
@@ -666,10 +671,12 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
|
|
|
|
|
bluetoothPhoneAPI->writeCount = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear the last ToRadio packet buffer to avoid rejecting first packet from new connection
|
|
|
|
|
memset(lastToRadio, 0, sizeof(lastToRadio));
|
|
|
|
|
|
|
|
|
|
nimbleBluetoothConnHandle = BLE_HS_CONN_HANDLE_NONE;
|
|
|
|
|
nimbleBluetoothConnHandle = BLE_HS_CONN_HANDLE_NONE; // BLE_HS_CONN_HANDLE_NONE means "no connection"
|
|
|
|
|
|
|
|
|
|
// Restart Advertising
|
|
|
|
|
ble->startAdvertising();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
@@ -677,58 +684,11 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks
|
|
|
|
|
static NimbleBluetoothToRadioCallback *toRadioCallbacks;
|
|
|
|
|
static NimbleBluetoothFromRadioCallback *fromRadioCallbacks;
|
|
|
|
|
|
|
|
|
|
void NimbleBluetooth::startAdvertising()
|
|
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_BT_NIMBLE_EXT_ADV)
|
|
|
|
|
NimBLEExtAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
|
|
|
|
NimBLEExtAdvertisement legacyAdvertising;
|
|
|
|
|
|
|
|
|
|
legacyAdvertising.setLegacyAdvertising(true);
|
|
|
|
|
legacyAdvertising.setScannable(true);
|
|
|
|
|
legacyAdvertising.setConnectable(true);
|
|
|
|
|
legacyAdvertising.setFlags(BLE_HS_ADV_F_DISC_GEN);
|
|
|
|
|
if (powerStatus->getHasBattery() == 1) {
|
|
|
|
|
legacyAdvertising.setCompleteServices(NimBLEUUID((uint16_t)0x180f));
|
|
|
|
|
}
|
|
|
|
|
legacyAdvertising.setCompleteServices(NimBLEUUID(MESH_SERVICE_UUID));
|
|
|
|
|
legacyAdvertising.setMinInterval(500);
|
|
|
|
|
legacyAdvertising.setMaxInterval(1000);
|
|
|
|
|
|
|
|
|
|
NimBLEExtAdvertisement legacyScanResponse;
|
|
|
|
|
legacyScanResponse.setLegacyAdvertising(true);
|
|
|
|
|
legacyScanResponse.setConnectable(true);
|
|
|
|
|
legacyScanResponse.setName(getDeviceName());
|
|
|
|
|
|
|
|
|
|
if (!pAdvertising->setInstanceData(0, legacyAdvertising)) {
|
|
|
|
|
LOG_ERROR("BLE failed to set legacyAdvertising");
|
|
|
|
|
} else if (!pAdvertising->setScanResponseData(0, legacyScanResponse)) {
|
|
|
|
|
LOG_ERROR("BLE failed to set legacyScanResponse");
|
|
|
|
|
} else if (!pAdvertising->start(0, 0, 0)) {
|
|
|
|
|
LOG_ERROR("BLE failed to start legacyAdvertising");
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
|
|
|
|
pAdvertising->reset();
|
|
|
|
|
pAdvertising->addServiceUUID(MESH_SERVICE_UUID);
|
|
|
|
|
if (powerStatus->getHasBattery() == 1) {
|
|
|
|
|
pAdvertising->addServiceUUID(NimBLEUUID((uint16_t)0x180f));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NimBLEAdvertisementData scan;
|
|
|
|
|
scan.setName(getDeviceName());
|
|
|
|
|
pAdvertising->setScanResponseData(scan);
|
|
|
|
|
pAdvertising->enableScanResponse(true);
|
|
|
|
|
|
|
|
|
|
if (!pAdvertising->start(0)) {
|
|
|
|
|
LOG_ERROR("BLE failed to start advertising");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
LOG_DEBUG("BLE Advertising started");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NimbleBluetooth::shutdown()
|
|
|
|
|
{
|
|
|
|
|
// No measurable power saving for ESP32 during light-sleep(?)
|
|
|
|
|
#ifndef ARCH_ESP32
|
|
|
|
|
// Shutdown bluetooth for minimum power draw
|
|
|
|
|
LOG_INFO("Disable bluetooth");
|
|
|
|
|
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
|
|
|
|
pAdvertising->reset();
|
|
|
|
|
@@ -736,6 +696,7 @@ void NimbleBluetooth::shutdown()
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Proper shutdown for ESP32. Needs reboot to reverse.
|
|
|
|
|
void NimbleBluetooth::deinit()
|
|
|
|
|
{
|
|
|
|
|
#ifdef ARCH_ESP32
|
|
|
|
|
@@ -752,14 +713,15 @@ void NimbleBluetooth::deinit()
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Has initial setup been completed
|
|
|
|
|
bool NimbleBluetooth::isActive()
|
|
|
|
|
{
|
|
|
|
|
return bleServer != nullptr;
|
|
|
|
|
return bleServer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NimbleBluetooth::isConnected()
|
|
|
|
|
{
|
|
|
|
|
return bleServer && bleServer->getConnectedCount() > 0;
|
|
|
|
|
return bleServer->getConnectedCount() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NimbleBluetooth::getRssi()
|
|
|
|
|
@@ -803,7 +765,7 @@ void NimbleBluetooth::setup()
|
|
|
|
|
LOG_INFO("Init the NimBLE bluetooth module");
|
|
|
|
|
|
|
|
|
|
NimBLEDevice::init(getDeviceName());
|
|
|
|
|
NimBLEDevice::setPower(9);
|
|
|
|
|
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
|
|
|
|
|
|
|
|
|
|
#if NIMBLE_ENABLE_2M_PHY && (defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6))
|
|
|
|
|
int mtuResult = NimBLEDevice::setMTU(kPreferredBleMtu);
|
|
|
|
|
@@ -836,7 +798,7 @@ void NimbleBluetooth::setup()
|
|
|
|
|
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
|
|
|
|
|
}
|
|
|
|
|
bleServer = NimBLEDevice::createServer();
|
|
|
|
|
auto *serverCallbacks = new NimbleBluetoothServerCallback(this);
|
|
|
|
|
NimbleBluetoothServerCallback *serverCallbacks = new NimbleBluetoothServerCallback(this);
|
|
|
|
|
bleServer->setCallbacks(serverCallbacks, true);
|
|
|
|
|
setupService();
|
|
|
|
|
startAdvertising();
|
|
|
|
|
@@ -889,6 +851,51 @@ void NimbleBluetooth::setupService()
|
|
|
|
|
batteryService->start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NimbleBluetooth::startAdvertising()
|
|
|
|
|
{
|
|
|
|
|
#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C3) || defined(CONFIG_IDF_TARGET_ESP32C6)
|
|
|
|
|
NimBLEExtAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
|
|
|
|
NimBLEExtAdvertisement legacyAdvertising;
|
|
|
|
|
|
|
|
|
|
legacyAdvertising.setLegacyAdvertising(true);
|
|
|
|
|
legacyAdvertising.setScannable(true);
|
|
|
|
|
legacyAdvertising.setConnectable(true);
|
|
|
|
|
legacyAdvertising.setFlags(BLE_HS_ADV_F_DISC_GEN);
|
|
|
|
|
if (powerStatus->getHasBattery() == 1) {
|
|
|
|
|
legacyAdvertising.setCompleteServices(NimBLEUUID((uint16_t)0x180f));
|
|
|
|
|
}
|
|
|
|
|
legacyAdvertising.setCompleteServices(NimBLEUUID(MESH_SERVICE_UUID));
|
|
|
|
|
legacyAdvertising.setMinInterval(500);
|
|
|
|
|
legacyAdvertising.setMaxInterval(1000);
|
|
|
|
|
|
|
|
|
|
NimBLEExtAdvertisement legacyScanResponse;
|
|
|
|
|
legacyScanResponse.setLegacyAdvertising(true);
|
|
|
|
|
legacyScanResponse.setConnectable(true);
|
|
|
|
|
legacyScanResponse.setName(getDeviceName());
|
|
|
|
|
|
|
|
|
|
if (!pAdvertising->setInstanceData(0, legacyAdvertising)) {
|
|
|
|
|
LOG_ERROR("BLE failed to set legacyAdvertising");
|
|
|
|
|
} else if (!pAdvertising->setScanResponseData(0, legacyScanResponse)) {
|
|
|
|
|
LOG_ERROR("BLE failed to set legacyScanResponse");
|
|
|
|
|
} else if (!pAdvertising->start(0, 0, 0)) {
|
|
|
|
|
LOG_ERROR("BLE failed to start legacyAdvertising");
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
|
|
|
|
pAdvertising->addServiceUUID(NimBLEUUID(MESH_SERVICE_UUID));
|
|
|
|
|
if (powerStatus->getHasBattery() == 1) {
|
|
|
|
|
pAdvertising->addServiceUUID(NimBLEUUID((uint16_t)0x180f));
|
|
|
|
|
}
|
|
|
|
|
pAdvertising->setName(getDeviceName());
|
|
|
|
|
// Optionally, set scan response data (not required unless you want to advertise more info)
|
|
|
|
|
// pAdvertising->setScanResponseData(...); // if needed
|
|
|
|
|
|
|
|
|
|
if (!pAdvertising->start()) {
|
|
|
|
|
LOG_ERROR("BLE failed to start advertising");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Given a level between 0-100, update the BLE attribute
|
|
|
|
|
void updateBatteryLevel(uint8_t level)
|
|
|
|
|
{
|
|
|
|
|
|