mirror of
https://github.com/meshtastic/firmware.git
synced 2025-08-02 11:55:56 +00:00
woot! using new BLE api approximately works for reading
This commit is contained in:
parent
c67b53b969
commit
3673f95fe5
@ -1,48 +1,52 @@
|
|||||||
#include "BluetoothUtil.h"
|
#include "BluetoothUtil.h"
|
||||||
#include "BluetoothSoftwareUpdate.h"
|
#include "BluetoothSoftwareUpdate.h"
|
||||||
#include <esp_gatt_defs.h>
|
|
||||||
#include <BLE2902.h>
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include <Update.h>
|
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <BLE2902.h>
|
||||||
|
#include <Update.h>
|
||||||
|
#include <esp_gatt_defs.h>
|
||||||
|
|
||||||
SimpleAllocator btPool;
|
SimpleAllocator btPool;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create standard device info service
|
* Create standard device info service
|
||||||
**/
|
**/
|
||||||
BLEService *createDeviceInfomationService(BLEServer *server, std::string hwVendor, std::string swVersion, std::string hwVersion = "")
|
BLEService *createDeviceInfomationService(BLEServer *server, std::string hwVendor, std::string swVersion,
|
||||||
|
std::string hwVersion = "")
|
||||||
{
|
{
|
||||||
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 BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_SW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
BLECharacteristic *swC =
|
||||||
|
new BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_SW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
||||||
BLECharacteristic *mfC = new BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_MANU_NAME), BLECharacteristic::PROPERTY_READ);
|
BLECharacteristic *mfC = new 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);
|
// 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?
|
||||||
|
|
||||||
BLECharacteristic *m_pnpCharacteristic = m_deviceInfoService->createCharacteristic(ESP_GATT_UUID_PNP_ID, BLECharacteristic::PROPERTY_READ);
|
BLECharacteristic *m_pnpCharacteristic = m_deviceInfoService->createCharacteristic(ESP_GATT_UUID_PNP_ID,
|
||||||
|
BLECharacteristic::PROPERTY_READ);
|
||||||
|
|
||||||
uint8_t sig, uint16_t vid, uint16_t pid, uint16_t version;
|
uint8_t sig, uint16_t vid, uint16_t pid, uint16_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 };
|
uint8_t pnp[] = { sig, (uint8_t) (vid >> 8), (uint8_t) vid, (uint8_t) (pid >> 8), (uint8_t) pid, (uint8_t) (version >>
|
||||||
m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
|
8), (uint8_t) version }; m_pnpCharacteristic->setValue(pnp, sizeof(pnp));
|
||||||
*/
|
*/
|
||||||
swC->setValue(swVersion);
|
swC->setValue(swVersion);
|
||||||
deviceInfoService->addCharacteristic(addBLECharacteristic(swC));
|
deviceInfoService->addCharacteristic(addBLECharacteristic(swC));
|
||||||
mfC->setValue(hwVendor);
|
mfC->setValue(hwVendor);
|
||||||
deviceInfoService->addCharacteristic(addBLECharacteristic(mfC));
|
deviceInfoService->addCharacteristic(addBLECharacteristic(mfC));
|
||||||
if (!hwVersion.empty())
|
if (!hwVersion.empty()) {
|
||||||
{
|
BLECharacteristic *hwvC =
|
||||||
BLECharacteristic *hwvC = new BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_HW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
new BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_HW_VERSION_STR), BLECharacteristic::PROPERTY_READ);
|
||||||
hwvC->setValue(hwVersion);
|
hwvC->setValue(hwVersion);
|
||||||
deviceInfoService->addCharacteristic(addBLECharacteristic(hwvC));
|
deviceInfoService->addCharacteristic(addBLECharacteristic(hwvC));
|
||||||
}
|
}
|
||||||
//SerialNumberCharacteristic.setValue("FIXME");
|
// SerialNumberCharacteristic.setValue("FIXME");
|
||||||
//deviceInfoService->addCharacteristic(&SerialNumberCharacteristic);
|
// deviceInfoService->addCharacteristic(&SerialNumberCharacteristic);
|
||||||
|
|
||||||
// m_manufacturerCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t) 0x2a29, BLECharacteristic::PROPERTY_READ);
|
// m_manufacturerCharacteristic = m_deviceInfoService->createCharacteristic((uint16_t) 0x2a29,
|
||||||
// m_manufacturerCharacteristic->setValue(name);
|
// BLECharacteristic::PROPERTY_READ); m_manufacturerCharacteristic->setValue(name);
|
||||||
|
|
||||||
/* add these later?
|
/* add these later?
|
||||||
ESP_GATT_UUID_SYSTEM_ID
|
ESP_GATT_UUID_SYSTEM_ID
|
||||||
@ -56,15 +60,9 @@ bool _BLEClientConnected = false;
|
|||||||
|
|
||||||
class MyServerCallbacks : public BLEServerCallbacks
|
class MyServerCallbacks : public BLEServerCallbacks
|
||||||
{
|
{
|
||||||
void onConnect(BLEServer *pServer)
|
void onConnect(BLEServer *pServer) { _BLEClientConnected = true; };
|
||||||
{
|
|
||||||
_BLEClientConnected = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
void onDisconnect(BLEServer *pServer)
|
void onDisconnect(BLEServer *pServer) { _BLEClientConnected = false; }
|
||||||
{
|
|
||||||
_BLEClientConnected = false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#define MAX_DESCRIPTORS 32
|
#define MAX_DESCRIPTORS 32
|
||||||
@ -118,7 +116,8 @@ 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));
|
||||||
|
|
||||||
batteryLevelC = new BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_BATTERY_LEVEL), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
batteryLevelC = new BLECharacteristic(BLEUUID((uint16_t)ESP_GATT_UUID_BATTERY_LEVEL),
|
||||||
|
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
|
||||||
|
|
||||||
addWithDesc(pBattery, batteryLevelC, "Percentage 0 - 100");
|
addWithDesc(pBattery, batteryLevelC, "Percentage 0 - 100");
|
||||||
batteryLevelC->addDescriptor(addBLEDescriptor(new BLE2902())); // Needed so clients can request notification
|
batteryLevelC->addDescriptor(addBLEDescriptor(new BLE2902())); // Needed so clients can request notification
|
||||||
@ -137,8 +136,7 @@ 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
|
||||||
if (batteryLevelC)
|
if (batteryLevelC) {
|
||||||
{
|
|
||||||
batteryLevelC->setValue(&level, 1);
|
batteryLevelC->setValue(&level, 1);
|
||||||
batteryLevelC->notify();
|
batteryLevelC->notify();
|
||||||
}
|
}
|
||||||
@ -148,8 +146,7 @@ void dumpCharacteristic(BLECharacteristic *c)
|
|||||||
{
|
{
|
||||||
std::string value = c->getValue();
|
std::string value = c->getValue();
|
||||||
|
|
||||||
if (value.length() > 0)
|
if (value.length() > 0) {
|
||||||
{
|
|
||||||
DEBUG_MSG("New value: ");
|
DEBUG_MSG("New value: ");
|
||||||
for (int i = 0; i < value.length(); i++)
|
for (int i = 0; i < value.length(); i++)
|
||||||
DEBUG_MSG("%c", value[i]);
|
DEBUG_MSG("%c", value[i]);
|
||||||
@ -199,15 +196,12 @@ class MySecurity : public BLESecurityCallbacks
|
|||||||
|
|
||||||
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl)
|
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl)
|
||||||
{
|
{
|
||||||
if (cmpl.success)
|
if (cmpl.success) {
|
||||||
{
|
|
||||||
uint16_t length;
|
uint16_t length;
|
||||||
esp_ble_gap_get_whitelist_size(&length);
|
esp_ble_gap_get_whitelist_size(&length);
|
||||||
Serial.printf(" onAuthenticationComplete -> success size: %d\n", length);
|
Serial.printf(" authenticated and connected to phone\n");
|
||||||
}
|
} else {
|
||||||
else
|
Serial.printf("phone authenticate failed %d\n", cmpl.fail_reason);
|
||||||
{
|
|
||||||
Serial.printf("onAuthenticationComplete -> fail %d\n", cmpl.fail_reason);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove our custom PIN request screen.
|
// Remove our custom PIN request screen.
|
||||||
@ -258,9 +252,7 @@ void deinitBLE()
|
|||||||
btPool.reset();
|
btPool.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
BLEServer *initBLE(
|
BLEServer *initBLE(StartBluetoothPinScreenCallback startBtPinScreen, StopBluetoothPinScreenCallback stopBtPinScreen,
|
||||||
StartBluetoothPinScreenCallback startBtPinScreen,
|
|
||||||
StopBluetoothPinScreenCallback stopBtPinScreen,
|
|
||||||
std::string deviceName, std::string hwVendor, std::string swVersion, std::string hwVersion)
|
std::string deviceName, std::string hwVendor, std::string swVersion, std::string hwVersion)
|
||||||
{
|
{
|
||||||
BLEDevice::init(deviceName);
|
BLEDevice::init(deviceName);
|
||||||
@ -284,7 +276,8 @@ BLEServer *initBLE(
|
|||||||
// We now let users create the battery service only if they really want (not all devices have a battery)
|
// We now let users create the battery service only if they really want (not all devices have a battery)
|
||||||
// BLEService *pBattery = createBatteryService(pServer);
|
// BLEService *pBattery = createBatteryService(pServer);
|
||||||
|
|
||||||
pUpdate = createUpdateService(pServer, hwVendor, swVersion, hwVersion); // We need to advertise this so our android ble scan operation can see it
|
pUpdate = createUpdateService(pServer, hwVendor, swVersion,
|
||||||
|
hwVersion); // We need to advertise this so our android ble scan operation can see it
|
||||||
|
|
||||||
// It seems only one service can be advertised - so for now don't advertise our updater
|
// It seems only one service can be advertised - so for now don't advertise our updater
|
||||||
// pServer->getAdvertising()->addServiceUUID(pUpdate->getUUID());
|
// pServer->getAdvertising()->addServiceUUID(pUpdate->getUUID());
|
||||||
@ -294,7 +287,8 @@ BLEServer *initBLE(
|
|||||||
pUpdate->start();
|
pUpdate->start();
|
||||||
|
|
||||||
// FIXME turn on this restriction only after the device is paired with a phone
|
// FIXME turn on this restriction only after the device is paired with a phone
|
||||||
// advert->setScanFilter(false, true); // We let anyone scan for us (FIXME, perhaps only allow that until we are paired with a phone and configured) but only let whitelist phones connect
|
// advert->setScanFilter(false, true); // We let anyone scan for us (FIXME, perhaps only allow that until we are paired with a
|
||||||
|
// phone and configured) but only let whitelist phones connect
|
||||||
|
|
||||||
static BLESecurity security; // static to avoid allocs
|
static BLESecurity security; // static to avoid allocs
|
||||||
BLESecurity *pSecurity = &security;
|
BLESecurity *pSecurity = &security;
|
||||||
|
@ -97,6 +97,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf)
|
|||||||
|
|
||||||
case STATE_SEND_NODEINFO: {
|
case STATE_SEND_NODEINFO: {
|
||||||
const NodeInfo *info = nodeInfoForPhone;
|
const NodeInfo *info = nodeInfoForPhone;
|
||||||
|
nodeInfoForPhone = NULL; // We just consumed a nodeinfo, will need a new one next time
|
||||||
|
|
||||||
if (info) {
|
if (info) {
|
||||||
DEBUG_MSG("Sending nodeinfo: num=0x%x, lastseen=%u, id=%s, name=%s\n", info->num, info->position.time, info->user.id,
|
DEBUG_MSG("Sending nodeinfo: num=0x%x, lastseen=%u, id=%s, name=%s\n", info->num, info->position.time, info->user.id,
|
||||||
@ -140,8 +141,9 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf)
|
|||||||
// Do we have a message from the mesh?
|
// Do we have a message from the mesh?
|
||||||
if (fromRadioScratch.which_variant != 0) {
|
if (fromRadioScratch.which_variant != 0) {
|
||||||
// Encapsulate as a FromRadio packet
|
// Encapsulate as a FromRadio packet
|
||||||
size_t numbytes = pb_encode_to_bytes(buf, sizeof(FromRadio_size), FromRadio_fields, &fromRadioScratch);
|
DEBUG_MSG("encoding toPhone packet to phone variant=%d", fromRadioScratch.which_variant);
|
||||||
DEBUG_MSG("delivering toPhone packet to phone variant=%d, %d bytes\n", fromRadioScratch.which_variant, numbytes);
|
size_t numbytes = pb_encode_to_bytes(buf, FromRadio_size, FromRadio_fields, &fromRadioScratch);
|
||||||
|
DEBUG_MSG(", %d bytes\n", numbytes);
|
||||||
return numbytes;
|
return numbytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,6 +192,8 @@ class FromRadioCharacteristic : public CallbackCharacteristic
|
|||||||
// or make empty if the queue is empty
|
// or make empty if the queue is empty
|
||||||
if (numBytes) {
|
if (numBytes) {
|
||||||
c->setValue(trBytes, numBytes);
|
c->setValue(trBytes, numBytes);
|
||||||
|
} else {
|
||||||
|
c->setValue((uint8_t *)"", 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user