Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BT] Update NimBLE-Arduino to 2.2.0 #2140

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions environments.ini
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ build_flags =
custom_description = BLE gateway with external antenna and Ethernet/WiFi connectivity, [user guide](https://tbridge02.theengs.io/)
custom_hardware = Theengs Bridge gateway ethernet

; DISCLAIMER: This is the default environment for Theengs Plug.
; DISCLAIMER: This is the default environment for Theengs Plug.
; Any modifications to this configuration are not covered by warranty.
; Proceed with caution when making changes.
;
;
; SAFETY WARNING: Do not modify values related to current limits MAX_CURRENT_ACTUATOR or other
; safety-critical parameters. Doing so may lead to fire hazards or damage to the device.
[env:theengs-plug]
Expand All @@ -143,7 +143,7 @@ lib_deps =
${libraries.decoder}
${libraries.adafruit_neopixel}
${libraries.rn8209}
build_flags =
build_flags =
${env.build_flags}
${com-esp32.build_flags}
'-DZgatewayBT="BT"'
Expand Down Expand Up @@ -1811,7 +1811,7 @@ build_flags =
'-DCONFIG_ESP_CONSOLE_UART=ESP_CONSOLE_USB_SERIAL_JTAG'

[env:esp32c3-m5stamp]
; https://docs.m5stack.com/en/core/stamp_c3
; https://docs.m5stack.com/en/core/stamp_c3
platform = ${com.esp32_platform}
board = esp32-c3-devkitm-1
board_build.partitions = min_spiffs.csv
Expand All @@ -1834,7 +1834,7 @@ custom_description = BLE gateway on ESP32C3
custom_hardware = M5Stamp C3

[env:esp32c3u-m5stamp]
; https://docs.m5stack.com/en/core/stamp_c3u
; https://docs.m5stack.com/en/core/stamp_c3u
platform = ${com.esp32_platform}
board = esp32-c3-devkitm-1
board_build.partitions = min_spiffs.csv
Expand Down Expand Up @@ -1898,8 +1898,9 @@ extra_scripts = ${com-esp32.extra_scripts}
board_build.partitions = partitions/min_spiffs.csv
lib_deps =
${com-esp32.lib_deps}
${libraries.ble}
${libraries.decoder}
h2zero/[email protected]
h2zero/[email protected]
build_flags =
${com-esp32.build_flags}
'-DZgatewayBT="BT"'
Expand All @@ -1908,5 +1909,5 @@ build_flags =
'-DGateway_Name="OMG_ESP32_BLE"'
'-DUSE_BLUFI=1'
'-DenableMultiGTWSync=false'
'-DARDUINO_LOOP_STACK_SIZE=17500'
'-DCONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT=31'
custom_description = Regular BLE gateway based on esp-idf with adaptive scanning activated, automatically adapts the scan parameters depending on your devices
22 changes: 21 additions & 1 deletion main/Zblufi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
#if defined(ESP32) && defined(USE_BLUFI)
# include "NimBLEDevice.h"
# include "NimBLEOta.h"
# include "esp_blufi_api.h"
# include "esp_timer.h"

Expand All @@ -35,6 +36,9 @@ extern "C" {
}

static esp_timer_handle_t connection_timer = nullptr;
static NimBLEOta* pNimBLEOta;
static NimBLECharacteristic* pCommandCharacteristic;
static NimBLECharacteristic* pRecvFwCharacteristic;

struct pkt_info {
uint8_t* pkt;
Expand Down Expand Up @@ -183,6 +187,8 @@ void stop_connection_timer() {}
static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t* param) {
/* actually, should post to blufi_task handle the procedure,
* now, as a example, we do it more simply */
ble_gap_conn_desc desc;

switch (event) {
case ESP_BLUFI_EVENT_INIT_FINISH:
Log.trace(F("BLUFI init finish" CR));
Expand All @@ -201,6 +207,10 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
gatewayState = GatewayState::ONBOARDING;
omg_blufi_ble_connected = true;
restart_connection_timer();
ble_gap_conn_find(param->connect.conn_id, &desc);
// Blufi dosn't provide a callback for subscribe so we emulate it here.
pCommandCharacteristic->getCallbacks()->onSubscribe(pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 2);
pRecvFwCharacteristic->getCallbacks()->onSubscribe(pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 2);
esp_blufi_adv_stop();
blufi_security_init();
break;
Expand All @@ -217,6 +227,9 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
} else {
gatewayState = GatewayState::OFFLINE;
}
ble_gap_conn_find(param->connect.conn_id, &desc);
pCommandCharacteristic->getCallbacks()->onSubscribe(pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 0);
pRecvFwCharacteristic->getCallbacks()->onSubscribe(pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 0);
blufi_security_deinit();
esp_blufi_adv_start();
break;
Expand Down Expand Up @@ -386,7 +399,7 @@ bool startBlufi() {
return false;
}

if (NimBLEDevice::getInitialized()) {
if (NimBLEDevice::isInitialized()) {
NimBLEDevice::deinit(true);
delay(50);
}
Expand All @@ -401,7 +414,14 @@ bool startBlufi() {
}
snprintf(advName, sizeof(advName), Gateway_Short_Name "_%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
NimBLEDevice::init(advName);
NimBLEDevice::setMTU(517);
NimBLEDevice::createServer(); // this initializes the GATT server so we need to reset it for blufi to init
ble_gatts_reset();
esp_blufi_gatt_svr_init();
pNimBLEOta = new NimBLEOta();
NimBLEService* pNimBLEOtaSvc = pNimBLEOta->start();
pCommandCharacteristic = pNimBLEOtaSvc->getCharacteristic(NimBLEUUID((uint16_t)0x8022));
pRecvFwCharacteristic = pNimBLEOtaSvc->getCharacteristic(NimBLEUUID((uint16_t)0x8020));
ble_gatts_start();
Log.notice(F("BLUFI started" CR));
return esp_blufi_profile_init() == ESP_OK;
Expand Down
2 changes: 1 addition & 1 deletion main/ZgatewayBLEConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class zBLEConnect {
TaskHandle_t m_taskHandle = nullptr;
zBLEConnect(NimBLEAddress& addr) {
m_pClient = NimBLEDevice::createClient(addr);
m_pClient->setConnectTimeout(5);
m_pClient->setConnectTimeout(5000);
}
virtual ~zBLEConnect() { NimBLEDevice::deleteClient(m_pClient); }
virtual bool writeData(BLEAction* action);
Expand Down
56 changes: 24 additions & 32 deletions main/ZgatewayBLEConnect.ino
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ bool zBLEConnect::writeData(BLEAction* action) {
}

std::vector<uint8_t> buf;
buf.reserve(len / 2);
for (auto i = 0; i < len; i += 2) {
std::string temp = action->value.substr(i, 2);
buf.push_back((uint8_t)strtoul(temp.c_str(), nullptr, 16));
char sVal[3] = {action->value[i], action->value[i + 1], 0};
buf.push_back((uint8_t)strtoul(sVal, nullptr, 16));
}
return pChar->writeValue((const uint8_t*)&buf[0], buf.size(), !pChar->canWriteNoResponse());
return pChar->writeValue(&buf[0], buf.size(), !pChar->canWriteNoResponse());
}
case BLE_VAL_INT:
return pChar->writeValue(strtol(action->value.c_str(), nullptr, 0), !pChar->canWriteNoResponse());
Expand Down Expand Up @@ -80,23 +81,21 @@ bool zBLEConnect::processActions(std::vector<BLEAction>& actions) {
if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) {
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["id"] = m_pClient->getPeerAddress().toString().c_str();
BLEdata["service"] = it.service.toString();
BLEdata["characteristic"] = it.characteristic.toString();

if (it.write) {
Log.trace(F("processing BLE write" CR));
BLEdata["write"] = it.value;
BLEdata["write"] = std::string(it.value);
result = writeData(&it);
} else {
Log.trace(F("processing BLE read" CR));
result = readData(&it);
if (result) {
switch (it.value_type) {
case BLE_VAL_HEX: {
char* pHex = NimBLEUtils::buildHexData(nullptr, (uint8_t*)it.value.c_str(), it.value.length());
BLEdata["read"] = pHex;
free(pHex);
BLEdata["read"] = NimBLEUtils::dataToHexString(it.value.data(), it.value.size());
break;
}
case BLE_VAL_INT: {
Expand Down Expand Up @@ -141,19 +140,18 @@ void LYWSD03MMC_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pD
Log.trace(F("Device identified creating BLE buffer" CR));
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
auto mac_addr = m_pClient->getPeerAddress().toString().c_str();
for (std::vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it;
if ((strcmp(p->macAdr, (char*)mac_address.c_str()) == 0)) {
if ((strcmp(p->macAdr, mac_addr) == 0)) {
if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC)
BLEdata["model"] = "LYWSD03MMC";
else if (p->sensorModel_id == BLEconectable::id::MHO_C401)
BLEdata["model"] = "MHO-C401";
}
}
BLEdata["id"] = (char*)mac_address.c_str();
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_address.c_str());
BLEdata["id"] = mac_addr;
Log.trace(F("Device identified in CB: %s" CR), mac_addr);
BLEdata["tempc"] = (float)((pData[0] | (pData[1] << 8)) * 0.01);
BLEdata["tempf"] = (float)(convertTemp_CtoF((pData[0] | (pData[1] << 8)) * 0.01));
BLEdata["hum"] = (float)(pData[2]);
Expand Down Expand Up @@ -212,11 +210,10 @@ void DT24_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, s
Log.trace(F("Device identified creating BLE buffer" CR));
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
auto mac_address = m_pClient->getPeerAddress().toString().c_str();
BLEdata["model"] = "DT24";
BLEdata["id"] = (char*)mac_address.c_str();
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_address.c_str());
BLEdata["id"] = mac_address;
Log.trace(F("Device identified in CB: %s" CR), mac_address);
BLEdata["volt"] = (float)(((m_data[4] * 256 * 256) + (m_data[5] * 256) + m_data[6]) / 10.0);
BLEdata["current"] = (float)(((m_data[7] * 256 * 256) + (m_data[8] * 256) + m_data[9]) / 1000.0);
BLEdata["power"] = (float)(((m_data[10] * 256 * 256) + (m_data[11] * 256) + m_data[12]) / 10.0);
Expand Down Expand Up @@ -269,10 +266,8 @@ void BM2_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* pData, si
Log.trace(F("Device identified creating BLE buffer" CR));
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
BLEdata["model"] = "BM2 Battery Monitor";
BLEdata["id"] = (char*)mac_address.c_str();
BLEdata["id"] = m_pClient->getPeerAddress().toString().c_str();
mbedtls_aes_context aes;
mbedtls_aes_init(&aes);
unsigned char output[16];
Expand Down Expand Up @@ -356,10 +351,8 @@ void HHCCJCY01HHCC_connect::publishData() {
batteryValue = val2[0];
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
BLEdata["model"] = "HHCCJCY01HHCC";
BLEdata["id"] = (char*)mac_address.c_str();
BLEdata["id"] = m_pClient->getPeerAddress().toString().c_str();
BLEdata["batt"] = (int)batteryValue;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
enqueueJsonObject(BLEdata, QueueSemaphoreTimeOutTask);
Expand All @@ -383,11 +376,10 @@ void XMWSDJ04MMC_connect::notifyCB(NimBLERemoteCharacteristic* pChar, uint8_t* p
Log.trace(F("Device identified creating BLE buffer" CR));
DynamicJsonDocument BLEdataBuffer(JSON_MSG_BUFFER);
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
String mac_address = m_pClient->getPeerAddress().toString().c_str();
mac_address.toUpperCase();
auto mac_address = m_pClient->getPeerAddress().toString().c_str();
BLEdata["model"] = "XMWSDJ04MMC";
BLEdata["id"] = (char*)mac_address.c_str();
Log.trace(F("Device identified in CB: %s" CR), (char*)mac_address.c_str());
BLEdata["id"] = mac_address;
Log.trace(F("Device identified in CB: %s" CR), mac_address);
BLEdata["tempc"] = (float)((pData[0] | (pData[1] << 8)) * 0.1);
BLEdata["tempf"] = (float)(convertTemp_CtoF((pData[0] | (pData[1] << 8)) * 0.1));
BLEdata["hum"] = (float)((pData[2] | (pData[3] << 8)) * 0.1);
Expand Down Expand Up @@ -460,7 +452,7 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
bool result = false;
if (actions.size() > 0) {
for (auto& it : actions) {
if (NimBLEAddress(it.addr) == m_pClient->getPeerAddress()) {
if (it.addr == m_pClient->getPeerAddress()) {
NimBLERemoteCharacteristic* pChar = getCharacteristic(serviceUUID, charUUID);
NimBLERemoteCharacteristic* pNotifyChar = getCharacteristic(serviceUUID, notifyCharUUID);

Expand Down Expand Up @@ -497,8 +489,8 @@ bool SBS1_connect::processActions(std::vector<BLEAction>& actions) {
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["state"] = it.value;
BLEdata["id"] = m_pClient->getPeerAddress().toString().c_str();
BLEdata["state"] = std::string(it.value);
buildTopicFromId(BLEdata, subjectBTtoMQTT);
enqueueJsonObject(BLEdata, QueueSemaphoreTimeOutTask);
}
Expand Down Expand Up @@ -592,7 +584,7 @@ bool SBBT_connect::processActions(std::vector<BLEAction>& actions) {
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["id"] = it.addr.toString().c_str();
if (value != -99 || value != -1)
BLEdata["tilt"] = value;
if (value == 50) {
Expand Down Expand Up @@ -694,7 +686,7 @@ bool SBCU_connect::processActions(std::vector<BLEAction>& actions) {
if (result || it.ttl <= 1) {
StaticJsonDocument<JSON_MSG_BUFFER> BLEdataBuffer;
JsonObject BLEdata = BLEdataBuffer.to<JsonObject>();
BLEdata["id"] = it.addr;
BLEdata["id"] = it.addr.toString().c_str();
if (value != -99 || value != -1)
BLEdata["position"] = value;
buildTopicFromId(BLEdata, subjectBTtoMQTT);
Expand Down
Loading
Loading