mirror of https://github.com/meshcore-dev/MeshCore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
7.4 KiB
236 lines
7.4 KiB
#include "SerialNimBLEInterface.h"
|
|
|
|
// See the following for generating UUIDs:
|
|
// https://www.uuidgenerator.net/
|
|
|
|
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
|
|
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
|
|
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
|
|
|
|
#define ADVERT_RESTART_DELAY 1000 // millis
|
|
|
|
void SerialNimBLEInterface::begin(const char* device_name, uint32_t pin_code) {
|
|
_pin_code = pin_code;
|
|
|
|
// Create the BLE Device
|
|
NimBLEDevice::init(device_name);
|
|
NimBLEDevice::setMTU(MAX_FRAME_SIZE);
|
|
NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY);
|
|
NimBLEDevice::setSecurityAuth(BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM | BLE_SM_PAIR_AUTHREQ_SC);
|
|
|
|
//NimBLEDevice::setPower(ESP_PWR_LVL_N8);
|
|
|
|
// Create the BLE Server
|
|
pServer = NimBLEDevice::createServer();
|
|
pServer->setCallbacks(this);
|
|
|
|
// Create the BLE Service
|
|
pService = pServer->createService(SERVICE_UUID);
|
|
|
|
// Create a BLE Characteristic
|
|
pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::READ_ENC);
|
|
|
|
NimBLE2904* pTx2904 = pTxCharacteristic->create2904();
|
|
pTx2904->setFormat(NimBLE2904::FORMAT_UTF8);
|
|
|
|
NimBLECharacteristic * pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::WRITE_ENC);
|
|
pRxCharacteristic->setCallbacks(this);
|
|
|
|
pService->start();
|
|
|
|
NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
|
|
pAdvertising->setName(device_name);
|
|
pAdvertising->addServiceUUID(pService->getUUID());
|
|
pAdvertising->enableScanResponse(true);
|
|
pAdvertising->start();
|
|
}
|
|
|
|
// -------- NimBLEServerCallbacks methods
|
|
uint32_t SerialNimBLEInterface::onPassKeyDisplay() {
|
|
BLE_DEBUG_PRINTLN("onPassKeyDisplay()");
|
|
return _pin_code;
|
|
}
|
|
|
|
void SerialNimBLEInterface::onConfirmPassKey(NimBLEConnInfo& connInfo, uint32_t pass_key) {
|
|
BLE_DEBUG_PRINTLN("onConfirmPassKey(%u)", pass_key);
|
|
NimBLEDevice::injectConfirmPasskey(connInfo, true);
|
|
}
|
|
|
|
void SerialNimBLEInterface::onAuthenticationComplete(NimBLEConnInfo& connInfo) {
|
|
if (connInfo.isEncrypted()) {
|
|
BLE_DEBUG_PRINTLN(" - SecurityCallback - Authentication Success");
|
|
deviceConnected = true;
|
|
} else {
|
|
BLE_DEBUG_PRINTLN(" - SecurityCallback - Authentication Failure*");
|
|
|
|
//pServer->removePeerDevice(connInfo.getConnHandle(), true);
|
|
pServer->disconnect(connInfo.getConnHandle());
|
|
adv_restart_time = millis() + ADVERT_RESTART_DELAY;
|
|
}
|
|
}
|
|
|
|
void SerialNimBLEInterface::onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo) {
|
|
uint16_t conn_id = connInfo.getConnHandle();
|
|
uint16_t mtu = pServer->getPeerMTU(conn_id);
|
|
|
|
BLE_DEBUG_PRINTLN("onConnect(), conn_id=%d, mtu=%d", conn_id, mtu);
|
|
|
|
last_conn_id = conn_id;
|
|
}
|
|
|
|
void SerialNimBLEInterface::onMTUChange(uint16_t mtu, NimBLEConnInfo& connInfo) {
|
|
BLE_DEBUG_PRINTLN("onMtuChanged(), mtu=%d", mtu);
|
|
}
|
|
|
|
void SerialNimBLEInterface::onDisconnect(NimBLEServer* pServer, NimBLEConnInfo &connInfo, int reason) {
|
|
BLE_DEBUG_PRINTLN("onDisconnect()");
|
|
if (_isEnabled) {
|
|
adv_restart_time = millis() + ADVERT_RESTART_DELAY;
|
|
|
|
// loop() will detect this on next loop, and set deviceConnected to false
|
|
}
|
|
}
|
|
|
|
// -------- NimBLECharacteristicCallbacks methods
|
|
|
|
void SerialNimBLEInterface::onWrite(NimBLECharacteristic* pCharacteristic, NimBLEConnInfo &connInfo) {
|
|
std::string rxValue = pCharacteristic->getValue();
|
|
size_t len = rxValue.length();
|
|
|
|
if (len > MAX_FRAME_SIZE) {
|
|
BLE_DEBUG_PRINTLN("ERROR: onWrite(), frame too big, len=%d", len);
|
|
} else if (recv_queue_len >= FRAME_QUEUE_SIZE) {
|
|
BLE_DEBUG_PRINTLN("ERROR: onWrite(), recv_queue is full!");
|
|
} else {
|
|
recv_queue[recv_queue_len].len = len;
|
|
memcpy(recv_queue[recv_queue_len].buf, rxValue.c_str(), len);
|
|
recv_queue_len++;
|
|
}
|
|
}
|
|
|
|
// ---------- public methods
|
|
|
|
void SerialNimBLEInterface::enable() {
|
|
if (_isEnabled) return;
|
|
|
|
_isEnabled = true;
|
|
clearBuffers();
|
|
|
|
// Start the service
|
|
pService->start();
|
|
|
|
// Start advertising
|
|
|
|
//pServer->getAdvertising()->setMinInterval(500);
|
|
//pServer->getAdvertising()->setMaxInterval(1000);
|
|
|
|
NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
|
|
pAdvertising->start();
|
|
|
|
adv_restart_time = 0;
|
|
}
|
|
|
|
void SerialNimBLEInterface::disable() {
|
|
_isEnabled = false;
|
|
|
|
BLE_DEBUG_PRINTLN("SerialNimBLEInterface::disable");
|
|
|
|
pServer->getAdvertising()->stop();
|
|
pServer->disconnect(last_conn_id);
|
|
NimBLEDevice::stopAdvertising();
|
|
oldDeviceConnected = deviceConnected = false;
|
|
adv_restart_time = 0;
|
|
}
|
|
|
|
size_t SerialNimBLEInterface::writeFrame(const uint8_t src[], size_t len) {
|
|
if (len > MAX_FRAME_SIZE) {
|
|
BLE_DEBUG_PRINTLN("writeFrame(), frame too big, len=%d", len);
|
|
return 0;
|
|
}
|
|
|
|
if (deviceConnected && len > 0) {
|
|
if (send_queue_len >= FRAME_QUEUE_SIZE) {
|
|
BLE_DEBUG_PRINTLN("writeFrame(), send_queue is full!");
|
|
return 0;
|
|
}
|
|
|
|
send_queue[send_queue_len].len = len; // add to send queue
|
|
memcpy(send_queue[send_queue_len].buf, src, len);
|
|
send_queue_len++;
|
|
|
|
return len;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
#define BLE_WRITE_MIN_INTERVAL 60
|
|
|
|
bool SerialNimBLEInterface::isWriteBusy() const {
|
|
return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write?
|
|
}
|
|
|
|
size_t SerialNimBLEInterface::checkRecvFrame(uint8_t dest[]) {
|
|
if (send_queue_len > 0 // first, check send queue
|
|
&& millis() >= _last_write + BLE_WRITE_MIN_INTERVAL // space the writes apart
|
|
) {
|
|
_last_write = millis();
|
|
pTxCharacteristic->setValue(send_queue[0].buf, send_queue[0].len);
|
|
pTxCharacteristic->notify();
|
|
|
|
BLE_DEBUG_PRINTLN("writeBytes: sz=%d, hdr=%d", (uint32_t)send_queue[0].len, (uint32_t) send_queue[0].buf[0]);
|
|
|
|
send_queue_len--;
|
|
for (int i = 0; i < send_queue_len; i++) { // delete top item from queue
|
|
send_queue[i] = send_queue[i + 1];
|
|
}
|
|
}
|
|
|
|
if (recv_queue_len > 0) { // check recv queue
|
|
size_t len = recv_queue[0].len; // take from top of queue
|
|
memcpy(dest, recv_queue[0].buf, len);
|
|
|
|
BLE_DEBUG_PRINTLN("readBytes: sz=%d, hdr=%d", len, (uint32_t) dest[0]);
|
|
|
|
recv_queue_len--;
|
|
for (int i = 0; i < recv_queue_len; i++) { // delete top item from queue
|
|
recv_queue[i] = recv_queue[i + 1];
|
|
}
|
|
return len;
|
|
}
|
|
|
|
if (pServer->getConnectedCount() == 0) deviceConnected = false;
|
|
|
|
if (deviceConnected != oldDeviceConnected) {
|
|
if (!deviceConnected) { // disconnecting
|
|
clearBuffers();
|
|
|
|
BLE_DEBUG_PRINTLN("SerialNimBLEInterface -> disconnecting...");
|
|
|
|
//pServer->getAdvertising()->setMinInterval(500);
|
|
//pServer->getAdvertising()->setMaxInterval(1000);
|
|
|
|
adv_restart_time = millis() + ADVERT_RESTART_DELAY;
|
|
} else {
|
|
BLE_DEBUG_PRINTLN("SerialNimBLEInterface -> stopping advertising");
|
|
BLE_DEBUG_PRINTLN("SerialNimBLEInterface -> connecting...");
|
|
// connecting
|
|
// do stuff here on connecting
|
|
pServer->getAdvertising()->stop();
|
|
adv_restart_time = 0;
|
|
}
|
|
oldDeviceConnected = deviceConnected;
|
|
}
|
|
|
|
if (adv_restart_time && millis() >= adv_restart_time) {
|
|
if (pServer->getConnectedCount() == 0) {
|
|
BLE_DEBUG_PRINTLN("SerialNimBLEInterface -> re-starting advertising");
|
|
pServer->getAdvertising()->start(); // re-Start advertising
|
|
}
|
|
adv_restart_time = 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool SerialNimBLEInterface::isConnected() const {
|
|
return deviceConnected; //pServer != NULL && pServer->getConnectedCount() > 0;
|
|
}
|
|
|