mirror of
https://github.com/meshtastic/firmware.git
synced 2025-07-31 02:45:41 +00:00
WIP - all BT characteristics must be dynamically alloced
This commit is contained in:
parent
356902d552
commit
a678dd3ee8
@ -9,11 +9,6 @@
|
|||||||
|
|
||||||
SimpleAllocator btPool;
|
SimpleAllocator btPool;
|
||||||
|
|
||||||
static BLECharacteristic SWVersionCharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_SW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
|
||||||
static BLECharacteristic ManufacturerCharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_MANU_NAME), BLECharacteristic::PROPERTY_READ);
|
|
||||||
static BLECharacteristic HardwareVersionCharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_HW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
|
||||||
//static BLECharacteristic SerialNumberCharacteristic(BLEUUID((uint16_t) ESP_GATT_UUID_SERIAL_NUMBER_STR), BLECharacteristic::PROPERTY_READ);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create standard device info service
|
* Create standard device info service
|
||||||
**/
|
**/
|
||||||
@ -21,6 +16,10 @@ BLEService *createDeviceInfomationService(BLEServer *server, std::string hwVendo
|
|||||||
{
|
{
|
||||||
BLEService *deviceInfoService = server->createService(BLEUUID((uint16_t)ESP_GATT_UUID_DEVICE_INFO_SVC));
|
BLEService *deviceInfoService = server->createService(BLEUUID((uint16_t)ESP_GATT_UUID_DEVICE_INFO_SVC));
|
||||||
|
|
||||||
|
BLECharacteristic *swC = new (btPool) BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_SW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
||||||
|
BLECharacteristic *mfC = new (btPool) BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_MANU_NAME), BLECharacteristic::PROPERTY_READ);
|
||||||
|
// BLECharacteristic SerialNumberCharacteristic(BLEUUID((uint16_t) ESP_GATT_UUID_SERIAL_NUMBER_STR), BLECharacteristic::PROPERTY_READ);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Mandatory characteristic for device info service?
|
* Mandatory characteristic for device info service?
|
||||||
|
|
||||||
@ -30,14 +29,15 @@ BLEService *createDeviceInfomationService(BLEServer *server, std::string hwVendo
|
|||||||
uint8_t pnp[] = { sig, (uint8_t) (vid >> 8), (uint8_t) vid, (uint8_t) (pid >> 8), (uint8_t) pid, (uint8_t) (version >> 8), (uint8_t) version };
|
uint8_t pnp[] = { sig, (uint8_t) (vid >> 8), (uint8_t) vid, (uint8_t) (pid >> 8), (uint8_t) pid, (uint8_t) (version >> 8), (uint8_t) version };
|
||||||
m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
|
m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
|
||||||
*/
|
*/
|
||||||
SWVersionCharacteristic.setValue(swVersion);
|
swC->setValue(swVersion);
|
||||||
deviceInfoService->addCharacteristic(&SWVersionCharacteristic);
|
deviceInfoService->addCharacteristic(swC);
|
||||||
ManufacturerCharacteristic.setValue(hwVendor);
|
mfC->setValue(hwVendor);
|
||||||
deviceInfoService->addCharacteristic(&ManufacturerCharacteristic);
|
deviceInfoService->addCharacteristic(mfC);
|
||||||
if (!hwVersion.empty())
|
if (!hwVersion.empty())
|
||||||
{
|
{
|
||||||
HardwareVersionCharacteristic.setValue(hwVersion);
|
BLECharacteristic *hwvC = new (btPool) BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_HW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
||||||
deviceInfoService->addCharacteristic(&HardwareVersionCharacteristic);
|
hwvC->setValue(hwVersion);
|
||||||
|
deviceInfoService->addCharacteristic(hwvC);
|
||||||
}
|
}
|
||||||
//SerialNumberCharacteristic.setValue("FIXME");
|
//SerialNumberCharacteristic.setValue("FIXME");
|
||||||
//deviceInfoService->addCharacteristic(&SerialNumberCharacteristic);
|
//deviceInfoService->addCharacteristic(&SerialNumberCharacteristic);
|
||||||
@ -68,17 +68,13 @@ class MyServerCallbacks : public BLEServerCallbacks
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Help routine to add a description to any BLECharacteristic and add it to the service
|
// Help routine to add a description to any BLECharacteristic and add it to the service
|
||||||
// We default to require an encrypted BOND for all these these characterstics
|
// We default to require an encrypted BOND for all these these characterstics
|
||||||
void addWithDesc(BLEService *service, BLECharacteristic *c, const char *description)
|
void addWithDesc(BLEService *service, BLECharacteristic *c, const char *description)
|
||||||
{
|
{
|
||||||
c->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
c->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
||||||
|
|
||||||
BLEDescriptor *desc = new(btPool) BLEDescriptor(BLEUUID((uint16_t)ESP_GATT_UUID_CHAR_DESCRIPTION), strlen(description) + 1);
|
BLEDescriptor *desc = new (btPool) BLEDescriptor(BLEUUID((uint16_t)ESP_GATT_UUID_CHAR_DESCRIPTION), strlen(description) + 1);
|
||||||
assert(desc);
|
assert(desc);
|
||||||
desc->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
desc->setAccessPermissions(ESP_GATT_PERM_READ_ENCRYPTED | ESP_GATT_PERM_WRITE_ENCRYPTED);
|
||||||
desc->setValue(description);
|
desc->setValue(description);
|
||||||
@ -86,8 +82,7 @@ void addWithDesc(BLEService *service, BLECharacteristic *c, const char *descript
|
|||||||
service->addCharacteristic(c);
|
service->addCharacteristic(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
static BLECharacteristic BatteryLevelCharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_BATTERY_LEVEL), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
static BLECharacteristic *batteryLevelC;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a battery level service
|
* Create a battery level service
|
||||||
@ -97,8 +92,10 @@ BLEService *createBatteryService(BLEServer *server)
|
|||||||
// Create the BLE Service
|
// Create the BLE Service
|
||||||
BLEService *pBattery = server->createService(BLEUUID((uint16_t)0x180F));
|
BLEService *pBattery = server->createService(BLEUUID((uint16_t)0x180F));
|
||||||
|
|
||||||
addWithDesc(pBattery, &BatteryLevelCharacteristic, "Percentage 0 - 100");
|
batteryLevelC = new (btPool) BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_BATTERY_LEVEL), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||||
BatteryLevelCharacteristic.addDescriptor(new(btPool) BLE2902()); // Needed so clients can request notification
|
|
||||||
|
addWithDesc(pBattery, batteryLevelC, "Percentage 0 - 100");
|
||||||
|
batteryLevelC->addDescriptor(new (btPool) BLE2902()); // Needed so clients can request notification
|
||||||
|
|
||||||
// I don't think we need to advertise this
|
// I don't think we need to advertise this
|
||||||
// server->getAdvertising()->addServiceUUID(pBattery->getUUID());
|
// server->getAdvertising()->addServiceUUID(pBattery->getUUID());
|
||||||
@ -114,8 +111,11 @@ BLEService *createBatteryService(BLEServer *server)
|
|||||||
void updateBatteryLevel(uint8_t level)
|
void updateBatteryLevel(uint8_t level)
|
||||||
{
|
{
|
||||||
// Pretend to update battery levels - fixme do elsewhere
|
// Pretend to update battery levels - fixme do elsewhere
|
||||||
BatteryLevelCharacteristic.setValue(&level, 1);
|
if (batteryLevelC)
|
||||||
BatteryLevelCharacteristic.notify();
|
{
|
||||||
|
batteryLevelC->setValue(&level, 1);
|
||||||
|
batteryLevelC->notify();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dumpCharacteristic(BLECharacteristic *c)
|
void dumpCharacteristic(BLECharacteristic *c)
|
||||||
@ -189,6 +189,14 @@ class MySecurity : public BLESecurityCallbacks
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void deinitBLE()
|
||||||
|
{
|
||||||
|
batteryLevelC = NULL; // Don't let anyone generate bogus notifies
|
||||||
|
|
||||||
|
BLEDevice::deinit(false);
|
||||||
|
btPool.reset();
|
||||||
|
}
|
||||||
|
|
||||||
BLEServer *initBLE(std::string deviceName, std::string hwVendor, std::string swVersion, std::string hwVersion)
|
BLEServer *initBLE(std::string deviceName, std::string hwVendor, std::string swVersion, std::string hwVersion)
|
||||||
{
|
{
|
||||||
BLEDevice::init(deviceName);
|
BLEDevice::init(deviceName);
|
||||||
|
@ -19,6 +19,7 @@ uint32_t getValue32(BLECharacteristic *c, uint32_t defaultValue);
|
|||||||
|
|
||||||
void loopBLE();
|
void loopBLE();
|
||||||
BLEServer *initBLE(std::string devName, std::string hwVendor, std::string swVersion, std::string hwVersion = "");
|
BLEServer *initBLE(std::string devName, std::string hwVendor, std::string swVersion, std::string hwVersion = "");
|
||||||
|
void deinitBLE();
|
||||||
|
|
||||||
/// Any bluetooth objects you allocate _must_ come from this pool if you want to be able to call destroyBLE()
|
/// Any bluetooth objects you allocate _must_ come from this pool if you want to be able to call deinitBLE()
|
||||||
extern SimpleAllocator btPool;
|
extern SimpleAllocator btPool;
|
||||||
|
@ -285,8 +285,7 @@ void setBluetoothEnable(bool on)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// FIXME - we are leaking like crazy
|
// FIXME - we are leaking like crazy
|
||||||
BLEDevice::deinit(false);
|
deinitBLE();
|
||||||
btPool.reset();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user