|
@@ -1,16 +1,23 @@
|
|
|
+#include "esp_gap_ble_api.h"
|
|
|
#include "BLEUUID.h"
|
|
|
#include "BLEAdvertising.h"
|
|
|
#include "WS_Bluetooth.h"
|
|
|
+#include "WS_GPIO.h"
|
|
|
#include <ArduinoJson.h>
|
|
|
+#include <MacroDebugger.h>
|
|
|
|
|
|
BLEServer* pServer;
|
|
|
|
|
|
+
|
|
|
BLEService* pConfigService;
|
|
|
BLECharacteristic* pConfigCharacteristic;
|
|
|
|
|
|
BLEService* pControlService;
|
|
|
BLECharacteristic* pControlCharacteristic;
|
|
|
|
|
|
+BLEAdvertising *pAdvertising;
|
|
|
+
|
|
|
+
|
|
|
bool requiresrestart = false;
|
|
|
bool isBluetoothConnected = false;
|
|
|
|
|
@@ -18,35 +25,25 @@ bool isBluetoothConnected = false;
|
|
|
|
|
|
class ServerCallbacks : public BLEServerCallbacks {
|
|
|
|
|
|
- void onConnect(BLEServer* pServer) {
|
|
|
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
|
|
- pAdvertising->stop();
|
|
|
- isBluetoothConnected = true;
|
|
|
+ void onConnect(BLEServer* pServer) {
|
|
|
+ DEBUGLN("BLE::Connect()");
|
|
|
+ //pAdvertising->stop();
|
|
|
+ isBluetoothConnected = true;
|
|
|
}
|
|
|
|
|
|
void onDisconnect(BLEServer* pServer) {
|
|
|
|
|
|
- isBluetoothConnected = false;
|
|
|
-
|
|
|
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
|
|
+ DEBUGLN("BLE::Disconnect()");
|
|
|
|
|
|
- // String devicename = GetDeviceName().substring(0,30);
|
|
|
- // BLEAdvertisementData data;
|
|
|
- // data.setName(devicename);
|
|
|
+ isBluetoothConnected = false;
|
|
|
|
|
|
- // pAdvertising->addServiceUUID(CONFIG_SERVICE_UUID);
|
|
|
+ // BLEDevice::startAdvertising();
|
|
|
+ // pConfigCharacteristic->notify();
|
|
|
// if (pControlService)
|
|
|
- // pAdvertising->addServiceUUID(pControlService->getUUID());
|
|
|
-
|
|
|
- // pAdvertising->setScanResponse(true);
|
|
|
- // pAdvertising->setMinPreferred(0x06);
|
|
|
- // pAdvertising->setMinPreferred(0x12);
|
|
|
+ // pControlCharacteristic->notify();
|
|
|
+ // pAdvertising->start();
|
|
|
|
|
|
- BLEDevice::startAdvertising();
|
|
|
- pConfigCharacteristic->notify();
|
|
|
- if (pControlService)
|
|
|
- pControlCharacteristic->notify();
|
|
|
- pAdvertising->start();
|
|
|
+ UpdateAdvertising();
|
|
|
|
|
|
if (requiresrestart)
|
|
|
Restart();
|
|
@@ -57,29 +54,24 @@ class ServerCallbacks : public BLEServerCallbacks {
|
|
|
class ConfigCharacteristicCallback : public BLECharacteristicCallbacks
|
|
|
{
|
|
|
|
|
|
- // char json[] = "{ \"warning\" : "", \"contact\" : "" }"
|
|
|
void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param)
|
|
|
{
|
|
|
- Serial.println("into writeConfig");
|
|
|
+
|
|
|
+ DEBUGLN("Config::Read()");
|
|
|
|
|
|
JsonDocument doc;
|
|
|
doc["warning"] = GetWarningNotice();
|
|
|
doc["contact"] = GetContactInfo();
|
|
|
String json = "";
|
|
|
serializeJson(doc,json);
|
|
|
-
|
|
|
- Serial.println("serialised");
|
|
|
- Serial.println(json);
|
|
|
pCharacteristic->setValue(json);
|
|
|
- Serial.println("done");
|
|
|
}
|
|
|
|
|
|
-// char json[] = "{ \"name\" : "PRS Digital Key", \"warning\" : "", \"contact\" : "" \"id\" : "00000000-0000-0000-0000-000000000000" }"
|
|
|
void onWrite(BLECharacteristic* pCharacteristic)
|
|
|
{
|
|
|
String json = pCharacteristic->getValue();
|
|
|
|
|
|
- Serial.println(json);
|
|
|
+ DEBUGLN(json);
|
|
|
|
|
|
JsonDocument doc;
|
|
|
deserializeJson(doc, json);
|
|
@@ -92,6 +84,7 @@ class ConfigCharacteristicCallback : public BLECharacteristicCallbacks
|
|
|
|
|
|
pCharacteristic->setValue("");
|
|
|
requiresrestart = true;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
};
|
|
@@ -99,8 +92,8 @@ class ConfigCharacteristicCallback : public BLECharacteristicCallbacks
|
|
|
// char json[] = "{ \"Relay00\" : 15000, \"Relay01\" : 0, \"Relay02\" : 0, \"Relay03\" : 0, \"Relay04\" : 0, \"Relay05\" : 0 }"
|
|
|
class ControlCharacteristicCallback : public BLECharacteristicCallbacks
|
|
|
{
|
|
|
- // char json[] = "{\"Relay01\"="15000"
|
|
|
- void onWrite(BLECharacteristic* pCharacteristic)
|
|
|
+
|
|
|
+ void onWrite(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t *param)
|
|
|
{
|
|
|
String json = pCharacteristic->getValue();
|
|
|
|
|
@@ -114,13 +107,13 @@ class ControlCharacteristicCallback : public BLECharacteristicCallbacks
|
|
|
SetRelay(i,time);
|
|
|
}
|
|
|
|
|
|
- pCharacteristic->setValue("");
|
|
|
+ pCharacteristic->setValue("");
|
|
|
}
|
|
|
|
|
|
// char json[] = "{ \"Relay00\" : true, \"Relay01\" : false, \"Relay02\" : false, \"Relay03\" : false, \"Relay04\" : false, \"Relay05\" : false }"
|
|
|
void onRead(BLECharacteristic* pCharacteristic, esp_ble_gatts_cb_param_t* param)
|
|
|
{
|
|
|
-
|
|
|
+ DEBUGLN("Control::Read()");
|
|
|
JsonDocument doc;
|
|
|
char name[10];
|
|
|
for (int i=0; i<6; i++)
|
|
@@ -140,13 +133,90 @@ bool IsConnected()
|
|
|
return isBluetoothConnected;
|
|
|
}
|
|
|
|
|
|
+void UpdateAdvertising()
|
|
|
+{
|
|
|
+ DEBUGLN("UpdateAdvertising()");
|
|
|
+ pAdvertising->stop();
|
|
|
+ BLEAdvertisementData advData;
|
|
|
+ advData.addData(UUID32ToAdvertisingData(0xce6c0b18));
|
|
|
+ if (pControlService)
|
|
|
+ advData.addData(UUID128ToAdvertisingData(pControlService->getUUID()));
|
|
|
+ advData.setManufacturerData(DeviceStatus());
|
|
|
+ pAdvertising->setAdvertisementData(advData);
|
|
|
+ pAdvertising->start();
|
|
|
+}
|
|
|
+
|
|
|
+String DeviceStatus()
|
|
|
+{
|
|
|
+ String result;
|
|
|
+ uint16_t companyID = 0x02E5; // Espressif's Bluetooth SIG company ID
|
|
|
+ //uint16_t companyID = 0xFFFF; // Deliberately invalid for identification purposes
|
|
|
+ result += (char)(companyID & 0xFF); // LSB
|
|
|
+ result += (char)((companyID >> 8) & 0xFF); // MSB
|
|
|
+
|
|
|
+ byte status = 0;
|
|
|
+ if (RelayStatus(0))
|
|
|
+ status += 1;
|
|
|
+ if (RelayStatus(1))
|
|
|
+ status += 2;
|
|
|
+ if (RelayStatus(2))
|
|
|
+ status += 4;
|
|
|
+ if (RelayStatus(3))
|
|
|
+ status += 8;
|
|
|
+ if (RelayStatus(4))
|
|
|
+ status += 16;
|
|
|
+ if (RelayStatus(5))
|
|
|
+ status += 32;
|
|
|
+ result += (char)status;
|
|
|
+
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+String UUID128ToAdvertisingData(BLEUUID uuid128) {
|
|
|
+ String adField;
|
|
|
+
|
|
|
+ // Get raw 16 bytes of the 128-bit UUID
|
|
|
+ const uint8_t* uuidBytes = uuid128.getNative()->uuid.uuid128;
|
|
|
+
|
|
|
+ // First byte: length (16 bytes data + 1 byte type)
|
|
|
+ adField += (char)(16 + 1);
|
|
|
+
|
|
|
+ // Second byte: type = 0x07 (complete list of 128-bit UUIDs)
|
|
|
+ adField += (char)0x07;
|
|
|
+
|
|
|
+ // Append 16 UUID bytes
|
|
|
+ for (int i=0; i<16; i++) {
|
|
|
+ adField += (char)(uuidBytes[i] & 0xFF);
|
|
|
+ }
|
|
|
+
|
|
|
+ return adField;
|
|
|
+}
|
|
|
+
|
|
|
+String UUID32ToAdvertisingData(uint32_t uuid32) {
|
|
|
+ String adField;
|
|
|
+
|
|
|
+ // 4 bytes of UUID + 1 byte type = 5 bytes total payload length
|
|
|
+ adField += (char)(4 + 1); // total length
|
|
|
+ adField += (char)0x05; // 0x05 = Complete List of 32-bit Service Class UUIDs
|
|
|
+
|
|
|
+ // Append UUID in little-endian byte order
|
|
|
+ adField += (char)(uuid32 & 0xFF);
|
|
|
+ adField += (char)((uuid32 >> 8) & 0xFF);
|
|
|
+ adField += (char)((uuid32 >> 16) & 0xFF);
|
|
|
+ adField += (char)((uuid32 >> 24) & 0xFF);
|
|
|
+
|
|
|
+ return adField;
|
|
|
+}
|
|
|
+
|
|
|
void Bluetooth_Init()
|
|
|
{
|
|
|
/*************************************************************************
|
|
|
Bluetooth
|
|
|
*************************************************************************/
|
|
|
|
|
|
- String devicename = GetDeviceName().substring(0,30);
|
|
|
+ DEBUGLN("Initialising BLE...");
|
|
|
+ String devicename = GetDeviceName().substring(0,24);
|
|
|
|
|
|
// Initialise the Device
|
|
|
BLEDevice::init(devicename);
|
|
@@ -154,31 +224,31 @@ void Bluetooth_Init()
|
|
|
pServer = BLEDevice::createServer();
|
|
|
pServer->setCallbacks(new ServerCallbacks());
|
|
|
|
|
|
- BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
|
|
|
-
|
|
|
+ pAdvertising = BLEDevice::getAdvertising();
|
|
|
+
|
|
|
// Set up the Configuration interface (always)
|
|
|
pConfigService = pServer->createService(CONFIG_SERVICE_UUID);
|
|
|
+
|
|
|
pConfigCharacteristic = pConfigService->createCharacteristic(CONFIG_CHARACTERISTIC_UUID,
|
|
|
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
|
|
|
pConfigCharacteristic->setCallbacks(new ConfigCharacteristicCallback());
|
|
|
pConfigService->start();
|
|
|
- pAdvertising->addServiceUUID(CONFIG_SERVICE_UUID);
|
|
|
-
|
|
|
+ pAdvertising->addServiceUUID(CONFIG_SERVICE_UUID);
|
|
|
+
|
|
|
String equipmentid = GetEquipmentId();
|
|
|
if (!equipmentid.isEmpty() && !equipmentid.equalsIgnoreCase("00000000-0000-0000-0000-000000000000"))
|
|
|
{
|
|
|
- BLEService* pControlService = pServer->createService(equipmentid);
|
|
|
+ pControlService = pServer->createService(equipmentid);
|
|
|
pControlCharacteristic = pControlService->createCharacteristic(CONTROL_CHARACTERISTIC_UUID,
|
|
|
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
|
|
|
pControlCharacteristic->setCallbacks(new ControlCharacteristicCallback());
|
|
|
pControlService->start();
|
|
|
- pAdvertising->addServiceUUID(equipmentid);
|
|
|
+ pAdvertising->addServiceUUID(equipmentid);
|
|
|
}
|
|
|
-
|
|
|
|
|
|
- pAdvertising->setScanResponse(true);
|
|
|
+ //pAdvertising->setScanResponse(true);
|
|
|
pAdvertising->setMinPreferred(0x06);
|
|
|
- pAdvertising->setMinPreferred(0x12);
|
|
|
+ pAdvertising->setMinPreferred(0x12);
|
|
|
|
|
|
BLEDevice::startAdvertising();
|
|
|
|
|
@@ -187,6 +257,9 @@ void Bluetooth_Init()
|
|
|
pControlCharacteristic->notify();
|
|
|
|
|
|
pAdvertising->start();
|
|
|
-
|
|
|
+
|
|
|
+ UpdateAdvertising();
|
|
|
+ DEBUGLN("BLE Initialisation complete.");
|
|
|
+
|
|
|
|
|
|
}
|