mirror of
https://github.com/Laxilef/OTGateway.git
synced 2026-11-30 10:13:04 +05:00
refactor: move Bluetooth scan callbacks into bluetooth.hpp
This commit is contained in:
1 parent
4004927ecd
commit
ff07bdcdca
2 files changed
+322
-316
No files matched your search
@@ -0,0 +1,320 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <NimBLEAdvertisedDevice.h>
|
||||||
|
#include <NimBLEScan.h>
|
||||||
|
#include <esp_log.h>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include "Sensors.h"
|
||||||
|
#include "strings_shared.h"
|
||||||
|
|
||||||
|
class BluetoothScanCallbacks : public NimBLEScanCallbacks {
|
||||||
|
public:
|
||||||
|
void onDiscovered(const NimBLEAdvertisedDevice* device) override {
|
||||||
|
auto& deviceAddress = device->getAddress();
|
||||||
|
|
||||||
|
bool found = false;
|
||||||
|
uint8_t sensorId;
|
||||||
|
std::optional<Sensors::Settings> sSensor;
|
||||||
|
for (sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
|
||||||
|
sSensor = Sensors::getSettings(sensorId);
|
||||||
|
if (!sSensor) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!sSensor->enabled || sSensor->type != Sensors::Type::BLUETOOTH || sSensor->purpose == Sensors::Purpose::NOT_CONFIGURED) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto sensorAddress = NimBLEAddress(sSensor->address, deviceAddress.getType());
|
||||||
|
if (sensorAddress.isNull() || sensorAddress != deviceAddress) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto deviceName = device->getName();
|
||||||
|
auto deviceRssi = device->getRSSI();
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu '%s': discovered device %s, name: %s, RSSI: %hhd",
|
||||||
|
sensorId, sSensor->name,
|
||||||
|
deviceAddress.toString().c_str(), deviceName.c_str(), deviceRssi
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!device->haveServiceData()) {
|
||||||
|
ESP_LOGW(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu '%s': not found service data!",
|
||||||
|
sensorId, sSensor->name
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto serviceDataCount = device->getServiceDataCount();
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu '%s': found %hhu service data",
|
||||||
|
sensorId, sSensor->name, serviceDataCount
|
||||||
|
);
|
||||||
|
|
||||||
|
if (parseAtcData(device, sensorId) || parsePvvxData(device, sensorId) || parseBTHomeData(device, sensorId)) {
|
||||||
|
// update rssi
|
||||||
|
Sensors::setValueById(sensorId, deviceRssi, Sensors::ValueType::RSSI, false, false);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu '%s': unsupported data format!",
|
||||||
|
sensorId, sSensor->name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parseAtcData(const NimBLEAdvertisedDevice* device, uint8_t sensorId) {
|
||||||
|
NimBLEUUID serviceUuid(static_cast<uint16_t>(0x181A));
|
||||||
|
|
||||||
|
auto serviceData = device->getServiceData(serviceUuid);
|
||||||
|
if (!serviceData.size()) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: not found ATC1441 data",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
} else if (serviceData.size() != 13) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: not in ATC1441 format",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: found ATC1441 format",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Temperature (2 bytes, signed, big-endian)
|
||||||
|
float temperature = static_cast<int16_t>(
|
||||||
|
(static_cast<uint8_t>(serviceData[6]) << 8) |
|
||||||
|
static_cast<uint8_t>(serviceData[7])
|
||||||
|
) * 0.1f;
|
||||||
|
Sensors::setValueById(
|
||||||
|
sensorId, temperature,
|
||||||
|
Sensors::ValueType::TEMPERATURE, true, true
|
||||||
|
);
|
||||||
|
|
||||||
|
// Humidity (1 byte)
|
||||||
|
float humidity = static_cast<uint8_t>(serviceData[8]);
|
||||||
|
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
||||||
|
|
||||||
|
// Battery level (1 byte)
|
||||||
|
uint8_t batteryLevel = static_cast<uint8_t>(serviceData[9]);
|
||||||
|
Sensors::setValueById(sensorId, batteryLevel, Sensors::ValueType::BATTERY, true, true);
|
||||||
|
|
||||||
|
// Battery mV (2 bytes, big-endian)
|
||||||
|
uint16_t batteryMv = (static_cast<uint8_t>(serviceData[10]) << 8) | static_cast<uint8_t>(serviceData[11]);
|
||||||
|
|
||||||
|
// Log
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE,
|
||||||
|
"Sensor #%hhu, received temp: %.2f; humidity: %.2f, battery voltage: %hu, battery level: %hhu",
|
||||||
|
sensorId, temperature, humidity, batteryMv, batteryLevel
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parsePvvxData(const NimBLEAdvertisedDevice* device, uint8_t sensorId) {
|
||||||
|
NimBLEUUID serviceUuid(static_cast<uint16_t>(0x181A));
|
||||||
|
|
||||||
|
auto serviceData = device->getServiceData(serviceUuid);
|
||||||
|
if (!serviceData.size()) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: not found PVVX data",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
} else if (serviceData.size() != 14 && serviceData.size() != 15) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: not in PVVX format",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: found PVVX format",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Temperature (2 bytes, signed, little-endian)
|
||||||
|
float temperature = static_cast<int16_t>(
|
||||||
|
(static_cast<uint8_t>(serviceData[7]) << 8) |
|
||||||
|
static_cast<uint8_t>(serviceData[6])
|
||||||
|
) * 0.01f;
|
||||||
|
Sensors::setValueById(
|
||||||
|
sensorId, temperature,
|
||||||
|
Sensors::ValueType::TEMPERATURE, true, true
|
||||||
|
);
|
||||||
|
|
||||||
|
// Humidity (2 bytes, little-endian)
|
||||||
|
float humidity = (
|
||||||
|
(static_cast<uint8_t>(serviceData[9]) << 8) | static_cast<uint8_t>(serviceData[8])
|
||||||
|
) * 0.01f;
|
||||||
|
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
||||||
|
|
||||||
|
// Battery level (1 byte)
|
||||||
|
uint8_t batteryLevel = static_cast<uint8_t>(serviceData[12]);
|
||||||
|
Sensors::setValueById(sensorId, batteryLevel, Sensors::ValueType::BATTERY, true, true);
|
||||||
|
|
||||||
|
// Battery mV (2 bytes, little-endian)
|
||||||
|
uint16_t batteryMv = (static_cast<uint8_t>(serviceData[11]) << 8) | static_cast<uint8_t>(serviceData[10]);
|
||||||
|
|
||||||
|
// Log
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE,
|
||||||
|
"Sensor #%hhu, received temp: %.2f; humidity: %.2f, battery voltage: %hu, battery level: %hhu",
|
||||||
|
sensorId, temperature, humidity, batteryMv, batteryLevel
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parseBTHomeData(const NimBLEAdvertisedDevice* device, uint8_t sensorId) {
|
||||||
|
NimBLEUUID serviceUuid(static_cast<uint16_t>(0xFCD2));
|
||||||
|
|
||||||
|
auto serviceData = device->getServiceData(serviceUuid);
|
||||||
|
if (!serviceData.size()) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: not found BTHome data",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
} else if ((serviceData[0] & 0xE0) != 0x40) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: unsupported BTHome version",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
|
||||||
|
} else if ((serviceData[0] & 0x01) != 0) {
|
||||||
|
ESP_LOGD(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: unsupported BTHome encrypted data",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, service %s: found BTHome format",
|
||||||
|
sensorId, serviceUuid.toString().c_str()
|
||||||
|
);
|
||||||
|
|
||||||
|
bool foundData = false;
|
||||||
|
size_t serviceDataPos = 1;
|
||||||
|
while (serviceDataPos < serviceData.size()) {
|
||||||
|
uint8_t objectId = serviceData[serviceDataPos++];
|
||||||
|
|
||||||
|
switch (objectId) {
|
||||||
|
// Packet ID (1 byte)
|
||||||
|
case 0x00:
|
||||||
|
if (serviceDataPos + 1 > serviceData.size()) {
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
|
||||||
|
serviceDataPos += 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Battery (1 byte)
|
||||||
|
case 0x01: {
|
||||||
|
if (serviceDataPos + 1 > serviceData.size()) {
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t batteryLevel = static_cast<uint8_t>(serviceData[serviceDataPos]);
|
||||||
|
Sensors::setValueById(sensorId, batteryLevel, Sensors::ValueType::BATTERY, true, true);
|
||||||
|
serviceDataPos += 1;
|
||||||
|
foundData = true;
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, received battery level: %hhu",
|
||||||
|
sensorId, batteryLevel
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Temperature (2 bytes, signed, little-endian)
|
||||||
|
case 0x02: {
|
||||||
|
if (serviceDataPos + 2 > serviceData.size()) {
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
|
||||||
|
int16_t rawTemperature = (static_cast<uint8_t>(serviceData[serviceDataPos + 1]) << 8)
|
||||||
|
| static_cast<uint8_t>(serviceData[serviceDataPos]);
|
||||||
|
float temperature = static_cast<float>(rawTemperature) * 0.01f;
|
||||||
|
Sensors::setValueById(
|
||||||
|
sensorId, temperature,
|
||||||
|
Sensors::ValueType::TEMPERATURE, true, true
|
||||||
|
);
|
||||||
|
serviceDataPos += 2;
|
||||||
|
foundData = true;
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, received temp: %.2f",
|
||||||
|
sensorId, temperature
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Humidity (2 bytes, little-endian)
|
||||||
|
case 0x03: {
|
||||||
|
if (serviceDataPos + 2 > serviceData.size()) {
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t rawHumidity = (static_cast<uint16_t>(serviceData[serviceDataPos + 1]) << 8)
|
||||||
|
| static_cast<uint8_t>(serviceData[serviceDataPos]);
|
||||||
|
float humidity = static_cast<float>(rawHumidity) * 0.01f;
|
||||||
|
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
||||||
|
serviceDataPos += 2;
|
||||||
|
foundData = true;
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, received humidity: %.2f",
|
||||||
|
sensorId, humidity
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Voltage (2 bytes, little-endian)
|
||||||
|
case 0x0C: {
|
||||||
|
if (serviceDataPos + 2 > serviceData.size()) {
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t batteryMv = (static_cast<uint16_t>(serviceData[serviceDataPos + 1]) << 8)
|
||||||
|
| static_cast<uint8_t>(serviceData[serviceDataPos]);
|
||||||
|
serviceDataPos += 2;
|
||||||
|
foundData = true;
|
||||||
|
|
||||||
|
ESP_LOGI(
|
||||||
|
L_SENSORS_BLE, "Sensor #%hhu, received battery voltage: %hu",
|
||||||
|
sensorId, batteryMv
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return foundData;
|
||||||
|
}
|
||||||
|
};
|
||||||
+2
-316
@@ -9,12 +9,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <optional>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#if USE_BLE
|
#if USE_BLE
|
||||||
#include <NimBLEDevice.h>
|
#include <NimBLEDevice.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
#include "bluetooth.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if USE_BLE && defined(CONFIG_BT_NIMBLE_PINNED_TO_CORE)
|
#if USE_BLE && defined(CONFIG_BT_NIMBLE_PINNED_TO_CORE)
|
||||||
@@ -25,321 +25,6 @@ static constexpr BaseType_t SENSORS_TASK_CORE = tskNO_AFFINITY;
|
|||||||
|
|
||||||
TaskManager sensorsTaskManager("Sensors");
|
TaskManager sensorsTaskManager("Sensors");
|
||||||
|
|
||||||
#if USE_BLE
|
|
||||||
class BluetoothScanCallbacks : public NimBLEScanCallbacks {
|
|
||||||
public:
|
|
||||||
void onDiscovered(const NimBLEAdvertisedDevice* device) override {
|
|
||||||
auto& deviceAddress = device->getAddress();
|
|
||||||
|
|
||||||
bool found = false;
|
|
||||||
uint8_t sensorId;
|
|
||||||
std::optional<Sensors::Settings> sSensor;
|
|
||||||
for (sensorId = 0; sensorId <= Sensors::getMaxSensorId(); sensorId++) {
|
|
||||||
sSensor = Sensors::getSettings(sensorId);
|
|
||||||
if (!sSensor) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (!sSensor->enabled || sSensor->type != Sensors::Type::BLUETOOTH || sSensor->purpose == Sensors::Purpose::NOT_CONFIGURED) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const auto sensorAddress = NimBLEAddress(sSensor->address, deviceAddress.getType());
|
|
||||||
if (sensorAddress.isNull() || sensorAddress != deviceAddress) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto deviceName = device->getName();
|
|
||||||
auto deviceRssi = device->getRSSI();
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu '%s': discovered device %s, name: %s, RSSI: %hhd",
|
|
||||||
sensorId, sSensor->name,
|
|
||||||
deviceAddress.toString().c_str(), deviceName.c_str(), deviceRssi
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!device->haveServiceData()) {
|
|
||||||
ESP_LOGW(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu '%s': not found service data!",
|
|
||||||
sensorId, sSensor->name
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto serviceDataCount = device->getServiceDataCount();
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu '%s': found %hhu service data",
|
|
||||||
sensorId, sSensor->name, serviceDataCount
|
|
||||||
);
|
|
||||||
|
|
||||||
if (parseAtcData(device, sensorId) || parsePvvxData(device, sensorId) || parseBTHomeData(device, sensorId)) {
|
|
||||||
// update rssi
|
|
||||||
Sensors::setValueById(sensorId, deviceRssi, Sensors::ValueType::RSSI, false, false);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ESP_LOGW(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu '%s': unsupported data format!",
|
|
||||||
sensorId, sSensor->name
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool parseAtcData(const NimBLEAdvertisedDevice* device, uint8_t sensorId) {
|
|
||||||
NimBLEUUID serviceUuid(static_cast<uint16_t>(0x181A));
|
|
||||||
|
|
||||||
auto serviceData = device->getServiceData(serviceUuid);
|
|
||||||
if (!serviceData.size()) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: not found ATC1441 data",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
|
|
||||||
} else if (serviceData.size() != 13) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: not in ATC1441 format",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: found ATC1441 format",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Temperature (2 bytes, signed, big-endian)
|
|
||||||
float temperature = static_cast<int16_t>(
|
|
||||||
(static_cast<uint8_t>(serviceData[6]) << 8) |
|
|
||||||
static_cast<uint8_t>(serviceData[7])
|
|
||||||
) * 0.1f;
|
|
||||||
Sensors::setValueById(
|
|
||||||
sensorId, temperature,
|
|
||||||
Sensors::ValueType::TEMPERATURE, true, true
|
|
||||||
);
|
|
||||||
|
|
||||||
// Humidity (1 byte)
|
|
||||||
float humidity = static_cast<uint8_t>(serviceData[8]);
|
|
||||||
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
|
||||||
|
|
||||||
// Battery level (1 byte)
|
|
||||||
uint8_t batteryLevel = static_cast<uint8_t>(serviceData[9]);
|
|
||||||
Sensors::setValueById(sensorId, batteryLevel, Sensors::ValueType::BATTERY, true, true);
|
|
||||||
|
|
||||||
// Battery mV (2 bytes, big-endian)
|
|
||||||
uint16_t batteryMv = (static_cast<uint8_t>(serviceData[10]) << 8) | static_cast<uint8_t>(serviceData[11]);
|
|
||||||
|
|
||||||
// Log
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE,
|
|
||||||
"Sensor #%hhu, received temp: %.2f; humidity: %.2f, battery voltage: %hu, battery level: %hhu",
|
|
||||||
sensorId, temperature, humidity, batteryMv, batteryLevel
|
|
||||||
);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool parsePvvxData(const NimBLEAdvertisedDevice* device, uint8_t sensorId) {
|
|
||||||
NimBLEUUID serviceUuid(static_cast<uint16_t>(0x181A));
|
|
||||||
|
|
||||||
auto serviceData = device->getServiceData(serviceUuid);
|
|
||||||
if (!serviceData.size()) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: not found PVVX data",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
|
|
||||||
} else if (serviceData.size() != 14 && serviceData.size() != 15) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: not in PVVX format",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: found PVVX format",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Temperature (2 bytes, signed, little-endian)
|
|
||||||
float temperature = static_cast<int16_t>(
|
|
||||||
(static_cast<uint8_t>(serviceData[7]) << 8) |
|
|
||||||
static_cast<uint8_t>(serviceData[6])
|
|
||||||
) * 0.01f;
|
|
||||||
Sensors::setValueById(
|
|
||||||
sensorId, temperature,
|
|
||||||
Sensors::ValueType::TEMPERATURE, true, true
|
|
||||||
);
|
|
||||||
|
|
||||||
// Humidity (2 bytes, little-endian)
|
|
||||||
float humidity = (
|
|
||||||
(static_cast<uint8_t>(serviceData[9]) << 8) | static_cast<uint8_t>(serviceData[8])
|
|
||||||
) * 0.01f;
|
|
||||||
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
|
||||||
|
|
||||||
// Battery level (1 byte)
|
|
||||||
uint8_t batteryLevel = static_cast<uint8_t>(serviceData[12]);
|
|
||||||
Sensors::setValueById(sensorId, batteryLevel, Sensors::ValueType::BATTERY, true, true);
|
|
||||||
|
|
||||||
// Battery mV (2 bytes, little-endian)
|
|
||||||
uint16_t batteryMv = (static_cast<uint8_t>(serviceData[11]) << 8) | static_cast<uint8_t>(serviceData[10]);
|
|
||||||
|
|
||||||
// Log
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE,
|
|
||||||
"Sensor #%hhu, received temp: %.2f; humidity: %.2f, battery voltage: %hu, battery level: %hhu",
|
|
||||||
sensorId, temperature, humidity, batteryMv, batteryLevel
|
|
||||||
);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool parseBTHomeData(const NimBLEAdvertisedDevice* device, uint8_t sensorId) {
|
|
||||||
NimBLEUUID serviceUuid(static_cast<uint16_t>(0xFCD2));
|
|
||||||
|
|
||||||
auto serviceData = device->getServiceData(serviceUuid);
|
|
||||||
if (!serviceData.size()) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: not found BTHome data",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
|
|
||||||
} else if ((serviceData[0] & 0xE0) != 0x40) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: unsupported BTHome version",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
|
|
||||||
} else if ((serviceData[0] & 0x01) != 0) {
|
|
||||||
ESP_LOGD(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: unsupported BTHome encrypted data",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, service %s: found BTHome format",
|
|
||||||
sensorId, serviceUuid.toString().c_str()
|
|
||||||
);
|
|
||||||
|
|
||||||
bool foundData = false;
|
|
||||||
size_t serviceDataPos = 1;
|
|
||||||
while (serviceDataPos < serviceData.size()) {
|
|
||||||
uint8_t objectId = serviceData[serviceDataPos++];
|
|
||||||
|
|
||||||
switch (objectId) {
|
|
||||||
// Packet ID (1 byte)
|
|
||||||
case 0x00:
|
|
||||||
if (serviceDataPos + 1 > serviceData.size()) {
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
|
|
||||||
serviceDataPos += 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Battery (1 byte)
|
|
||||||
case 0x01: {
|
|
||||||
if (serviceDataPos + 1 > serviceData.size()) {
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t batteryLevel = static_cast<uint8_t>(serviceData[serviceDataPos]);
|
|
||||||
Sensors::setValueById(sensorId, batteryLevel, Sensors::ValueType::BATTERY, true, true);
|
|
||||||
serviceDataPos += 1;
|
|
||||||
foundData = true;
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, received battery level: %hhu",
|
|
||||||
sensorId, batteryLevel
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Temperature (2 bytes, signed, little-endian)
|
|
||||||
case 0x02: {
|
|
||||||
if (serviceDataPos + 2 > serviceData.size()) {
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t rawTemperature = (static_cast<uint8_t>(serviceData[serviceDataPos + 1]) << 8)
|
|
||||||
| static_cast<uint8_t>(serviceData[serviceDataPos]);
|
|
||||||
float temperature = static_cast<float>(rawTemperature) * 0.01f;
|
|
||||||
Sensors::setValueById(
|
|
||||||
sensorId, temperature,
|
|
||||||
Sensors::ValueType::TEMPERATURE, true, true
|
|
||||||
);
|
|
||||||
serviceDataPos += 2;
|
|
||||||
foundData = true;
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, received temp: %.2f",
|
|
||||||
sensorId, temperature
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Humidity (2 bytes, little-endian)
|
|
||||||
case 0x03: {
|
|
||||||
if (serviceDataPos + 2 > serviceData.size()) {
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t rawHumidity = (static_cast<uint16_t>(serviceData[serviceDataPos + 1]) << 8)
|
|
||||||
| static_cast<uint8_t>(serviceData[serviceDataPos]);
|
|
||||||
float humidity = static_cast<float>(rawHumidity) * 0.01f;
|
|
||||||
Sensors::setValueById(sensorId, humidity, Sensors::ValueType::HUMIDITY, true, true);
|
|
||||||
serviceDataPos += 2;
|
|
||||||
foundData = true;
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, received humidity: %.2f",
|
|
||||||
sensorId, humidity
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Voltage (2 bytes, little-endian)
|
|
||||||
case 0x0C: {
|
|
||||||
if (serviceDataPos + 2 > serviceData.size()) {
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t batteryMv = (static_cast<uint16_t>(serviceData[serviceDataPos + 1]) << 8)
|
|
||||||
| static_cast<uint8_t>(serviceData[serviceDataPos]);
|
|
||||||
serviceDataPos += 2;
|
|
||||||
foundData = true;
|
|
||||||
|
|
||||||
ESP_LOGI(
|
|
||||||
L_SENSORS_BLE, "Sensor #%hhu, received battery voltage: %hu",
|
|
||||||
sensorId, batteryMv
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return foundData;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static BluetoothScanCallbacks bluetoothScanCallbacks;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static const unsigned int wiredDisconnectTimeout = 180000u;
|
static const unsigned int wiredDisconnectTimeout = 180000u;
|
||||||
static const unsigned int wirelessDisconnectTimeout = 600000u;
|
static const unsigned int wirelessDisconnectTimeout = 600000u;
|
||||||
static const unsigned short dallasSearchInterval = 60000u;
|
static const unsigned short dallasSearchInterval = 60000u;
|
||||||
@@ -354,6 +39,7 @@ static std::unordered_map<uint8_t, unsigned long> dallasLastPollingTime;
|
|||||||
static std::unordered_map<uint8_t, bool> dallasPolling;
|
static std::unordered_map<uint8_t, bool> dallasPolling;
|
||||||
|
|
||||||
#if USE_BLE
|
#if USE_BLE
|
||||||
|
static BluetoothScanCallbacks bluetoothScanCallbacks;
|
||||||
static NimBLEScan* pBLEScan = nullptr;
|
static NimBLEScan* pBLEScan = nullptr;
|
||||||
static bool activeScanBle = false;
|
static bool activeScanBle = false;
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in new issue
Block a user