mirror of
https://github.com/pelgraine/Meck.git
synced 2026-05-13 12:55:53 +02:00
595f0073f9
SerialBLEInterface.cpp — added esp_bt.h include and three esp_ble_tx_power_set calls at +9 dBm after BLEDevice::init(), covering default, advertising, and scan power types. MyMesh.h — FIRMWARE_VER_CODE bumped from 10 → 11. MyMesh.cpp — The RESP_CODE_DEVICE_INFO frame construction now: Byte 2: sends 0xFF (sentinel) when MAX_CONTACTS > 510, otherwise the normal MAX_CONTACTS / 2. Older apps interpret 0xFF as 510 contacts — completely harmless. Bytes 80-81 (new, appended after the version string): uint16_t little-endian with the true MAX_CONTACTS value. Apps that understand v11+ read it here. Apps < v11 ignore trailing bytes — the BLE/serial frame protocol is length-delimited, so extra bytes at the tail are safe. platformio.ini — Both BLE builds (meck_audio_ble, meck_4g_ble) bumped from 510 → 2000. mymesh.cpp: writeContactRespFrame return type change (return _serial->writeFrame() result) checkSerialInterface() batch-fill loop.
280 lines
9.0 KiB
C++
280 lines
9.0 KiB
C++
#include "SerialBLEInterface.h"
|
||
#include "esp_bt.h"
|
||
#include "esp_gap_ble_api.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 SerialBLEInterface::begin(const char* prefix, char* name, uint32_t pin_code) {
|
||
_pin_code = pin_code;
|
||
|
||
if (strcmp(name, "@@MAC") == 0) {
|
||
uint8_t addr[8];
|
||
memset(addr, 0, sizeof(addr));
|
||
esp_efuse_mac_get_default(addr);
|
||
sprintf(name, "%02X%02X%02X%02X%02X%02X", // modify (IN-OUT param)
|
||
addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
|
||
}
|
||
char dev_name[32+16];
|
||
sprintf(dev_name, "%s%s", prefix, name);
|
||
|
||
// Create the BLE Device
|
||
BLEDevice::init(dev_name);
|
||
BLEDevice::setSecurityCallbacks(this);
|
||
BLEDevice::setMTU(MAX_FRAME_SIZE);
|
||
|
||
// Boost BLE TX power for improved range (+9 dBm, up from default +3 dBm)
|
||
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_DEFAULT, ESP_PWR_LVL_P9);
|
||
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, ESP_PWR_LVL_P9);
|
||
esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_SCAN, ESP_PWR_LVL_P9);
|
||
|
||
BLESecurity sec;
|
||
sec.setStaticPIN(pin_code);
|
||
sec.setAuthenticationMode(ESP_LE_AUTH_REQ_SC_MITM_BOND);
|
||
|
||
//BLEDevice::setPower(ESP_PWR_LVL_N8);
|
||
|
||
// Create the BLE Server
|
||
pServer = BLEDevice::createServer();
|
||
pServer->setCallbacks(this);
|
||
|
||
// Create the BLE Service
|
||
pService = pServer->createService(SERVICE_UUID);
|
||
|
||
// Create a BLE Characteristic
|
||
pTxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||
pTxCharacteristic->setAccessPermissions(ESP_GATT_PERM_READ_ENC_MITM);
|
||
pTxCharacteristic->addDescriptor(new BLE2902());
|
||
|
||
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE);
|
||
pRxCharacteristic->setAccessPermissions(ESP_GATT_PERM_WRITE_ENC_MITM);
|
||
pRxCharacteristic->setCallbacks(this);
|
||
|
||
pServer->getAdvertising()->addServiceUUID(SERVICE_UUID);
|
||
}
|
||
|
||
// -------- BLESecurityCallbacks methods
|
||
|
||
uint32_t SerialBLEInterface::onPassKeyRequest() {
|
||
BLE_DEBUG_PRINTLN("onPassKeyRequest()");
|
||
return _pin_code;
|
||
}
|
||
|
||
void SerialBLEInterface::onPassKeyNotify(uint32_t pass_key) {
|
||
BLE_DEBUG_PRINTLN("onPassKeyNotify(%u)", pass_key);
|
||
}
|
||
|
||
bool SerialBLEInterface::onConfirmPIN(uint32_t pass_key) {
|
||
BLE_DEBUG_PRINTLN("onConfirmPIN(%u)", pass_key);
|
||
return true;
|
||
}
|
||
|
||
bool SerialBLEInterface::onSecurityRequest() {
|
||
BLE_DEBUG_PRINTLN("onSecurityRequest()");
|
||
return true; // allow
|
||
}
|
||
|
||
void SerialBLEInterface::onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl) {
|
||
if (cmpl.success) {
|
||
BLE_DEBUG_PRINTLN(" - SecurityCallback - Authentication Success");
|
||
deviceConnected = true;
|
||
|
||
// Request fast connection interval (15ms) for faster contact sync.
|
||
// Phone may negotiate higher, but most modern phones accept 15ms.
|
||
// Units are 1.25ms, so 12 = 15ms, 16 = 20ms.
|
||
esp_ble_conn_update_params_t conn_params;
|
||
memcpy(conn_params.bda, _remote_bda, 6);
|
||
conn_params.min_int = 12; // 15ms (12 × 1.25ms)
|
||
conn_params.max_int = 16; // 20ms (16 × 1.25ms)
|
||
conn_params.latency = 0; // no skipped intervals
|
||
conn_params.timeout = 400; // 4 seconds supervision timeout
|
||
esp_ble_gap_update_conn_params(&conn_params);
|
||
BLE_DEBUG_PRINTLN(" - Requested fast connection interval (15-20ms)");
|
||
} else {
|
||
BLE_DEBUG_PRINTLN(" - SecurityCallback - Authentication Failure*");
|
||
|
||
//pServer->removePeerDevice(pServer->getConnId(), true);
|
||
pServer->disconnect(pServer->getConnId());
|
||
adv_restart_time = millis() + ADVERT_RESTART_DELAY;
|
||
}
|
||
}
|
||
|
||
// -------- BLEServerCallbacks methods
|
||
|
||
void SerialBLEInterface::onConnect(BLEServer* pServer) {
|
||
}
|
||
|
||
void SerialBLEInterface::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) {
|
||
BLE_DEBUG_PRINTLN("onConnect(), conn_id=%d, mtu=%d", param->connect.conn_id, pServer->getPeerMTU(param->connect.conn_id));
|
||
last_conn_id = param->connect.conn_id;
|
||
memcpy(_remote_bda, param->connect.remote_bda, 6);
|
||
}
|
||
|
||
void SerialBLEInterface::onMtuChanged(BLEServer* pServer, esp_ble_gatts_cb_param_t* param) {
|
||
BLE_DEBUG_PRINTLN("onMtuChanged(), mtu=%d", pServer->getPeerMTU(param->mtu.conn_id));
|
||
}
|
||
|
||
void SerialBLEInterface::onDisconnect(BLEServer* pServer) {
|
||
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
|
||
}
|
||
}
|
||
|
||
// -------- BLECharacteristicCallbacks methods
|
||
|
||
void SerialBLEInterface::onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param) {
|
||
uint8_t* rxValue = pCharacteristic->getData();
|
||
int len = pCharacteristic->getLength();
|
||
|
||
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, len);
|
||
recv_queue_len++;
|
||
}
|
||
}
|
||
|
||
// ---------- public methods
|
||
|
||
void SerialBLEInterface::enable() {
|
||
if (_isEnabled) return;
|
||
|
||
_isEnabled = true;
|
||
clearBuffers();
|
||
|
||
// Start the service
|
||
pService->start();
|
||
|
||
// Start advertising
|
||
|
||
//pServer->getAdvertising()->setMinInterval(500);
|
||
//pServer->getAdvertising()->setMaxInterval(1000);
|
||
|
||
pServer->getAdvertising()->start();
|
||
adv_restart_time = 0;
|
||
}
|
||
|
||
void SerialBLEInterface::disable() {
|
||
bool wasEnabled = _isEnabled;
|
||
_isEnabled = false;
|
||
|
||
BLE_DEBUG_PRINTLN("SerialBLEInterface::disable");
|
||
|
||
// Only try BLE operations if we were previously enabled
|
||
// (avoids accessing dead BLE objects after btStop/mem_release)
|
||
if (wasEnabled && pServer) {
|
||
pServer->getAdvertising()->stop();
|
||
pServer->disconnect(last_conn_id);
|
||
pService->stop();
|
||
}
|
||
oldDeviceConnected = deviceConnected = false;
|
||
adv_restart_time = 0;
|
||
clearBuffers();
|
||
}
|
||
|
||
size_t SerialBLEInterface::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 15
|
||
|
||
bool SerialBLEInterface::isWriteBusy() const {
|
||
return millis() < _last_write + BLE_WRITE_MIN_INTERVAL; // still too soon to start another write?
|
||
}
|
||
|
||
size_t SerialBLEInterface::checkRecvFrame(uint8_t dest[]) {
|
||
if (!_isEnabled) return 0; // BLE disabled — skip all BLE operations
|
||
|
||
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("SerialBLEInterface -> disconnecting...");
|
||
|
||
//pServer->getAdvertising()->setMinInterval(500);
|
||
//pServer->getAdvertising()->setMaxInterval(1000);
|
||
|
||
adv_restart_time = millis() + ADVERT_RESTART_DELAY;
|
||
} else {
|
||
BLE_DEBUG_PRINTLN("SerialBLEInterface -> stopping advertising");
|
||
BLE_DEBUG_PRINTLN("SerialBLEInterface -> 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("SerialBLEInterface -> re-starting advertising");
|
||
pServer->getAdvertising()->start(); // re-Start advertising
|
||
}
|
||
adv_restart_time = 0;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
bool SerialBLEInterface::isConnected() const {
|
||
return deviceConnected; //pServer != NULL && pServer->getConnectedCount() > 0;
|
||
} |