mirror of
https://github.com/meshtastic/firmware.git
synced 2025-05-01 19:46:42 +00:00
Update ESP32Bluetooth.cpp (#1625)
* Update ESP32Bluetooth.cpp * Update ESP32Bluetooth.h
This commit is contained in:
parent
20e43fcf34
commit
97712a9dc4
@ -10,56 +10,44 @@
|
|||||||
#include "mesh/mesh-pb-constants.h"
|
#include "mesh/mesh-pb-constants.h"
|
||||||
#include <NimBLEDevice.h>
|
#include <NimBLEDevice.h>
|
||||||
|
|
||||||
//static BLEService meshBleService = BLEService(BLEUuid(MESH_SERVICE_UUID_16));
|
NimBLECharacteristic *fromNumCharacteristic;
|
||||||
//static BLECharacteristic fromNum = BLECharacteristic(BLEUuid(FROMNUM_UUID_16));
|
|
||||||
//static BLECharacteristic fromRadio = BLECharacteristic(BLEUuid(FROMRADIO_UUID_16));
|
|
||||||
//static BLECharacteristic toRadio = BLECharacteristic(BLEUuid(TORADIO_UUID_16));
|
|
||||||
|
|
||||||
//static BLEDis bledis; // DIS (Device Information Service) helper class instance
|
|
||||||
//static BLEBas blebas; // BAS (Battery Service) helper class instance
|
|
||||||
//static BLEDfu bledfu; // DFU software update helper service
|
|
||||||
|
|
||||||
// This scratch buffer is used for various bluetooth reads/writes - but it is safe because only one bt operation can be in
|
|
||||||
// proccess at once
|
|
||||||
// static uint8_t trBytes[_max(_max(_max(_max(ToRadio_size, RadioConfig_size), User_size), MyNodeInfo_size), FromRadio_size)];
|
|
||||||
static uint8_t fromRadioBytes[FromRadio_size];
|
|
||||||
|
|
||||||
NimBLECharacteristic *FromNumCharacteristic;
|
|
||||||
NimBLEServer *bleServer;
|
NimBLEServer *bleServer;
|
||||||
|
|
||||||
static bool passkeyShowing;
|
static bool passkeyShowing;
|
||||||
static uint32_t doublepressed;
|
static uint32_t doublepressed;
|
||||||
|
|
||||||
/**
|
class BluetoothPhoneAPI : public PhoneAPI
|
||||||
|
{
|
||||||
|
/**
|
||||||
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
||||||
*/
|
*/
|
||||||
void BluetoothPhoneAPI::onNowHasData(uint32_t fromRadioNum)
|
virtual void onNowHasData(uint32_t fromRadioNum)
|
||||||
{
|
{
|
||||||
PhoneAPI::onNowHasData(fromRadioNum);
|
PhoneAPI::onNowHasData(fromRadioNum);
|
||||||
|
|
||||||
DEBUG_MSG("BLE notify fromNum\n");
|
DEBUG_MSG("BLE notify fromNum\n");
|
||||||
//fromNum.notify32(fromRadioNum);
|
|
||||||
|
|
||||||
uint8_t val[4];
|
uint8_t val[4];
|
||||||
put_le32(val, fromRadioNum);
|
put_le32(val, fromRadioNum);
|
||||||
|
|
||||||
std::string fromNumByteString(&val[0], &val[0] + sizeof(val));
|
fromNumCharacteristic->setValue(val, sizeof(val));
|
||||||
|
fromNumCharacteristic->notify();
|
||||||
FromNumCharacteristic->setValue(fromNumByteString);
|
|
||||||
FromNumCharacteristic->notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check the current underlying physical link to see if the client is currently connected
|
|
||||||
bool BluetoothPhoneAPI::checkIsConnected() {
|
|
||||||
if (bleServer && bleServer->getConnectedCount() > 0) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
PhoneAPI *bluetoothPhoneAPI;
|
/// Check the current underlying physical link to see if the client is currently connected
|
||||||
|
virtual bool checkIsConnected()
|
||||||
|
{
|
||||||
|
return bleServer && bleServer->getConnectedCount() > 0;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class ESP32BluetoothToRadioCallback : public NimBLECharacteristicCallbacks {
|
static BluetoothPhoneAPI *bluetoothPhoneAPI;
|
||||||
|
/**
|
||||||
|
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
||||||
|
*/
|
||||||
|
|
||||||
|
class ESP32BluetoothToRadioCallback : public NimBLECharacteristicCallbacks
|
||||||
|
{
|
||||||
virtual void onWrite(NimBLECharacteristic *pCharacteristic) {
|
virtual void onWrite(NimBLECharacteristic *pCharacteristic) {
|
||||||
DEBUG_MSG("To Radio onwrite\n");
|
DEBUG_MSG("To Radio onwrite\n");
|
||||||
auto val = pCharacteristic->getValue();
|
auto val = pCharacteristic->getValue();
|
||||||
@ -71,6 +59,7 @@ class ESP32BluetoothToRadioCallback : public NimBLECharacteristicCallbacks {
|
|||||||
class ESP32BluetoothFromRadioCallback : public NimBLECharacteristicCallbacks {
|
class ESP32BluetoothFromRadioCallback : public NimBLECharacteristicCallbacks {
|
||||||
virtual void onRead(NimBLECharacteristic *pCharacteristic) {
|
virtual void onRead(NimBLECharacteristic *pCharacteristic) {
|
||||||
DEBUG_MSG("From Radio onread\n");
|
DEBUG_MSG("From Radio onread\n");
|
||||||
|
uint8_t fromRadioBytes[FromRadio_size];
|
||||||
size_t numBytes = bluetoothPhoneAPI->getFromRadio(fromRadioBytes);
|
size_t numBytes = bluetoothPhoneAPI->getFromRadio(fromRadioBytes);
|
||||||
|
|
||||||
std::string fromRadioByteString(fromRadioBytes, fromRadioBytes + numBytes);
|
std::string fromRadioByteString(fromRadioBytes, fromRadioBytes + numBytes);
|
||||||
@ -79,7 +68,8 @@ class ESP32BluetoothFromRadioCallback : public NimBLECharacteristicCallbacks {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ESP32BluetoothServerCallback : public NimBLEServerCallbacks {
|
class ESP32BluetoothServerCallback : public NimBLEServerCallbacks
|
||||||
|
{
|
||||||
virtual uint32_t onPassKeyRequest() {
|
virtual uint32_t onPassKeyRequest() {
|
||||||
|
|
||||||
uint32_t passkey = 0;
|
uint32_t passkey = 0;
|
||||||
@ -89,8 +79,8 @@ class ESP32BluetoothServerCallback : public NimBLEServerCallbacks {
|
|||||||
passkey = defaultBLEPin;
|
passkey = defaultBLEPin;
|
||||||
} else {
|
} else {
|
||||||
DEBUG_MSG("Using random passkey\n");
|
DEBUG_MSG("Using random passkey\n");
|
||||||
passkey = random(
|
// This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits
|
||||||
100000, 999999); // This is the passkey to be entered on peer - we pick a number >100,000 to ensure 6 digits
|
passkey = random(100000, 999999);
|
||||||
}
|
}
|
||||||
DEBUG_MSG("*** Enter passkey %d on the peer side ***\n", passkey);
|
DEBUG_MSG("*** Enter passkey %d on the peer side ***\n", passkey);
|
||||||
|
|
||||||
@ -109,6 +99,7 @@ class ESP32BluetoothServerCallback : public NimBLEServerCallbacks {
|
|||||||
screen->stopBluetoothPinScreen();
|
screen->stopBluetoothPinScreen();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc *desc) {
|
virtual void onDisconnect(NimBLEServer* pServer, ble_gap_conn_desc *desc) {
|
||||||
DEBUG_MSG("BLE disconnect\n");
|
DEBUG_MSG("BLE disconnect\n");
|
||||||
}
|
}
|
||||||
@ -129,48 +120,7 @@ void ESP32Bluetooth::shutdown()
|
|||||||
|
|
||||||
void ESP32Bluetooth::setup()
|
void ESP32Bluetooth::setup()
|
||||||
{
|
{
|
||||||
// Initialise the Bluefruit module
|
|
||||||
DEBUG_MSG("Initialise the ESP32 bluetooth module\n");
|
DEBUG_MSG("Initialise the ESP32 bluetooth module\n");
|
||||||
//Bluefruit.autoConnLed(false);
|
|
||||||
//Bluefruit.begin();
|
|
||||||
|
|
||||||
// Set the advertised device name (keep it short!)
|
|
||||||
//Bluefruit.setName(getDeviceName());
|
|
||||||
|
|
||||||
// Set the connect/disconnect callback handlers
|
|
||||||
//Bluefruit.Periph.setConnectCallback(connect_callback);
|
|
||||||
//Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
|
|
||||||
|
|
||||||
// Configure and Start the Device Information Service
|
|
||||||
DEBUG_MSG("Configuring the Device Information Service\n");
|
|
||||||
// FIXME, we should set a mfg string based on our HW_VENDOR enum
|
|
||||||
// bledis.setManufacturer(HW_VENDOR);
|
|
||||||
//bledis.setModel(optstr(HW_VERSION));
|
|
||||||
//bledis.setFirmwareRev(optstr(APP_VERSION));
|
|
||||||
//bledis.begin();
|
|
||||||
|
|
||||||
// Start the BLE Battery Service and set it to 100%
|
|
||||||
//DEBUG_MSG("Configuring the Battery Service\n");
|
|
||||||
//blebas.begin();
|
|
||||||
//blebas.write(0); // Unknown battery level for now
|
|
||||||
|
|
||||||
//bledfu.begin(); // Install the DFU helper
|
|
||||||
|
|
||||||
// Setup the Heart Rate Monitor service using
|
|
||||||
// BLEService and BLECharacteristic classes
|
|
||||||
DEBUG_MSG("Configuring the Mesh bluetooth service\n");
|
|
||||||
//setupMeshService();
|
|
||||||
|
|
||||||
// Supposedly debugging works with soft device if you disable advertising
|
|
||||||
//if (isSoftDeviceAllowed) {
|
|
||||||
// Setup the advertising packet(s)
|
|
||||||
// DEBUG_MSG("Setting up the advertising payload(s)\n");
|
|
||||||
// startAdv();
|
|
||||||
|
|
||||||
// DEBUG_MSG("Advertising\n");
|
|
||||||
//}
|
|
||||||
|
|
||||||
//NimBLEDevice::deleteAllBonds();
|
|
||||||
|
|
||||||
NimBLEDevice::init(getDeviceName());
|
NimBLEDevice::init(getDeviceName());
|
||||||
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
|
NimBLEDevice::setPower(ESP_PWR_LVL_P9);
|
||||||
@ -182,14 +132,18 @@ void ESP32Bluetooth::setup()
|
|||||||
ESP32BluetoothServerCallback *serverCallbacks = new ESP32BluetoothServerCallback();
|
ESP32BluetoothServerCallback *serverCallbacks = new ESP32BluetoothServerCallback();
|
||||||
bleServer->setCallbacks(serverCallbacks, true);
|
bleServer->setCallbacks(serverCallbacks, true);
|
||||||
|
|
||||||
|
setupService();
|
||||||
|
startAdvertising();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ESP32Bluetooth::setupService()
|
||||||
|
{
|
||||||
NimBLEService *bleService = bleServer->createService(MESH_SERVICE_UUID);
|
NimBLEService *bleService = bleServer->createService(MESH_SERVICE_UUID);
|
||||||
//NimBLECharacteristic *pNonSecureCharacteristic = bleService->createCharacteristic("1234", NIMBLE_PROPERTY::READ );
|
|
||||||
//NimBLECharacteristic *pSecureCharacteristic = bleService->createCharacteristic("1235", NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN);
|
|
||||||
|
|
||||||
//define the characteristics that the app is looking for
|
//define the characteristics that the app is looking for
|
||||||
NimBLECharacteristic *ToRadioCharacteristic = bleService->createCharacteristic(TORADIO_UUID, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_AUTHEN | NIMBLE_PROPERTY::WRITE_ENC);
|
NimBLECharacteristic *ToRadioCharacteristic = bleService->createCharacteristic(TORADIO_UUID, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_AUTHEN | NIMBLE_PROPERTY::WRITE_ENC);
|
||||||
NimBLECharacteristic *FromRadioCharacteristic = bleService->createCharacteristic(FROMRADIO_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC);
|
NimBLECharacteristic *FromRadioCharacteristic = bleService->createCharacteristic(FROMRADIO_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC);
|
||||||
FromNumCharacteristic = bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC);
|
fromNumCharacteristic = bleService->createCharacteristic(FROMNUM_UUID, NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_AUTHEN | NIMBLE_PROPERTY::READ_ENC);
|
||||||
|
|
||||||
bluetoothPhoneAPI = new BluetoothPhoneAPI();
|
bluetoothPhoneAPI = new BluetoothPhoneAPI();
|
||||||
|
|
||||||
@ -199,26 +153,17 @@ void ESP32Bluetooth::setup()
|
|||||||
fromRadioCallbacks = new ESP32BluetoothFromRadioCallback();
|
fromRadioCallbacks = new ESP32BluetoothFromRadioCallback();
|
||||||
FromRadioCharacteristic->setCallbacks(fromRadioCallbacks);
|
FromRadioCharacteristic->setCallbacks(fromRadioCallbacks);
|
||||||
|
|
||||||
//uint8_t val[4];
|
|
||||||
//uint32_t zero = 0;
|
|
||||||
//put_le32(val, zero);
|
|
||||||
//std::string fromNumByteString(&val[0], &val[0] + sizeof(val));
|
|
||||||
//FromNumCharacteristic->setValue(fromNumByteString);
|
|
||||||
|
|
||||||
bleService->start();
|
bleService->start();
|
||||||
//pNonSecureCharacteristic->setValue("Hello Non Secure BLE");
|
}
|
||||||
//pSecureCharacteristic->setValue("Hello Secure BLE");
|
|
||||||
|
|
||||||
//FromRadioCharacteristic->setValue("FromRadioString");
|
|
||||||
//ToRadioCharacteristic->setCallbacks()
|
|
||||||
|
|
||||||
|
void ESP32Bluetooth::startAdvertising()
|
||||||
|
{
|
||||||
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
NimBLEAdvertising *pAdvertising = NimBLEDevice::getAdvertising();
|
||||||
pAdvertising->reset();
|
pAdvertising->reset();
|
||||||
pAdvertising->addServiceUUID(MESH_SERVICE_UUID);
|
pAdvertising->addServiceUUID(MESH_SERVICE_UUID);
|
||||||
pAdvertising->start(0);
|
pAdvertising->start(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Given a level between 0-100, update the BLE attribute
|
/// Given a level between 0-100, update the BLE attribute
|
||||||
void updateBatteryLevel(uint8_t level)
|
void updateBatteryLevel(uint8_t level)
|
||||||
{
|
{
|
||||||
@ -228,20 +173,17 @@ void updateBatteryLevel(uint8_t level)
|
|||||||
void ESP32Bluetooth::clearBonds()
|
void ESP32Bluetooth::clearBonds()
|
||||||
{
|
{
|
||||||
DEBUG_MSG("Clearing bluetooth bonds!\n");
|
DEBUG_MSG("Clearing bluetooth bonds!\n");
|
||||||
//bond_print_list(BLE_GAP_ROLE_PERIPH);
|
|
||||||
//bond_print_list(BLE_GAP_ROLE_CENTRAL);
|
|
||||||
|
|
||||||
//Bluefruit.Periph.clearBonds();
|
|
||||||
//Bluefruit.Central.clearBonds();
|
|
||||||
NimBLEDevice::deleteAllBonds();
|
NimBLEDevice::deleteAllBonds();
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearNVS() {
|
void clearNVS()
|
||||||
|
{
|
||||||
NimBLEDevice::deleteAllBonds();
|
NimBLEDevice::deleteAllBonds();
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
void disablePin() {
|
void disablePin()
|
||||||
|
{
|
||||||
DEBUG_MSG("User Override, disabling bluetooth pin requirement\n");
|
DEBUG_MSG("User Override, disabling bluetooth pin requirement\n");
|
||||||
// keep track of when it was pressed, so we know it was within X seconds
|
// keep track of when it was pressed, so we know it was within X seconds
|
||||||
|
|
||||||
|
@ -2,28 +2,16 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
extern uint16_t fromNumValHandle;
|
|
||||||
|
|
||||||
class BluetoothPhoneAPI : public PhoneAPI
|
|
||||||
{
|
|
||||||
protected:
|
|
||||||
/**
|
|
||||||
* Subclasses can use this as a hook to provide custom notifications for their transport (i.e. bluetooth notifies)
|
|
||||||
*/
|
|
||||||
virtual void onNowHasData(uint32_t fromRadioNum) override;
|
|
||||||
|
|
||||||
/// Check the current underlying physical link to see if the client is currently connected
|
|
||||||
virtual bool checkIsConnected() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
extern PhoneAPI *bluetoothPhoneAPI;
|
|
||||||
|
|
||||||
class ESP32Bluetooth
|
class ESP32Bluetooth
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void setup();
|
void setup();
|
||||||
void shutdown();
|
void shutdown();
|
||||||
void clearBonds();
|
void clearBonds();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void setupService();
|
||||||
|
void startAdvertising();
|
||||||
};
|
};
|
||||||
|
|
||||||
void setBluetoothEnable(bool on);
|
void setBluetoothEnable(bool on);
|
||||||
|
Loading…
Reference in New Issue
Block a user