From 97598c9178a0ced6d8fa24f5bb2efeba68099557 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 3 Feb 2020 11:53:38 -0800 Subject: [PATCH] new ble charatristics work --- TODO.md | 1 + src/MeshBluetoothService.cpp | 54 ++++++++++++++++++++++++++++++------ src/MeshRadio.cpp | 2 +- src/NodeDB.cpp | 3 +- src/NodeDB.h | 1 + 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/TODO.md b/TODO.md index f4a4a8308..fb96b90b2 100644 --- a/TODO.md +++ b/TODO.md @@ -2,6 +2,7 @@ * implement new ble characteristics * have MeshService keep a node DB by sniffing user messages +* figure out why protobuf reads of Owner fail * have meshservice send location data on mesh (if device has a GPS) * implement getCurrentTime() - set based off gps but then updated locally * confirm second device receives that gps message and updates device db diff --git a/src/MeshBluetoothService.cpp b/src/MeshBluetoothService.cpp index 63deaeffd..f71d2543f 100644 --- a/src/MeshBluetoothService.cpp +++ b/src/MeshBluetoothService.cpp @@ -5,19 +5,56 @@ #include #include - #include "mesh.pb.h" #include "MeshService.h" #include "mesh-pb-constants.h" +#include "NodeDB.h" + +// This scratch buffer is used for various bluetooth reads/writes - but it is safe because only one bt operation can be in proccess at once +static uint8_t trBytes[_max(_max(_max(_max(ToRadio_size, RadioConfig_size), User_size), MyNodeInfo_size), FromRadio_size)]; + +class ProtobufCharacteristic : public BLECharacteristic, public BLECharacteristicCallbacks +{ + const pb_msgdesc_t *fields; + void *my_struct; + +public: + ProtobufCharacteristic(const char *uuid, uint32_t btprops, const pb_msgdesc_t *_fields, void *_my_struct) + : BLECharacteristic(uuid, btprops), + fields(_fields), + my_struct(_my_struct) + { + setCallbacks(this); + } + + void onRead(BLECharacteristic *c) + { + Serial.println("Got proto read"); + size_t numbytes = pb_encode_to_bytes(trBytes, sizeof(trBytes), fields, my_struct); + c->setValue(trBytes, numbytes); + } + + void onWrite(BLECharacteristic *c) + { + // dumpCharacteristic(pCharacteristic); + Serial.println("Got on proto write"); + std::string src = c->getValue(); + if (pb_decode_from_bytes((const uint8_t *)src.c_str(), src.length(), fields, my_struct)) { + // Success, the bytes are now in our struct - do nothing else + } + } +}; static BLECharacteristic meshFromRadioCharacteristic("8ba2bcc2-ee02-4a55-a531-c525c5e454d5", BLECharacteristic::PROPERTY_READ), meshToRadioCharacteristic("f75c76d2-129e-4dad-a1dd-7866124401e7", BLECharacteristic::PROPERTY_WRITE), meshFromNumCharacteristic("ed9da18c-a800-4f66-a670-aa7547e34453", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY), - meshMyNodeCharacteristic("ea9f3f82-8dc4-4733-9452-1f6da28892a2", BLECharacteristic::PROPERTY_READ), - meshNodeInfoCharacteristic("d31e02e0-c8ab-4d3f-9cc9-0b8466bdabe8", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ), - meshRadioCharacteristic("b56786c8-839a-44a1-b98e-a1724c4a0262", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ), - meshOwnerCharacteristic("6ff1d8b6-e2de-41e3-8c0b-8fa384f64eb6", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ); + meshNodeInfoCharacteristic("d31e02e0-c8ab-4d3f-9cc9-0b8466bdabe8", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ); + +static ProtobufCharacteristic + meshMyNodeCharacteristic("ea9f3f82-8dc4-4733-9452-1f6da28892a2", BLECharacteristic::PROPERTY_READ, MyNodeInfo_fields, &myNodeInfo), + meshRadioCharacteristic("b56786c8-839a-44a1-b98e-a1724c4a0262", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ, RadioConfig_fields, &radioConfig), + meshOwnerCharacteristic("6ff1d8b6-e2de-41e3-8c0b-8fa384f64eb6", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ, User_fields, &owner); /** * Tell any bluetooth clients that the number of rx packets has changed @@ -28,7 +65,6 @@ void bluetoothNotifyFromNum(uint32_t newValue) meshFromNumCharacteristic.notify(); } - class BluetoothMeshCallbacks : public BLECharacteristicCallbacks { void onRead(BLECharacteristic *c) @@ -59,8 +95,6 @@ class BluetoothMeshCallbacks : public BLECharacteristicCallbacks service.releaseToPool(mp); // we just copied the bytes, so don't need this buffer anymore - static uint8_t trBytes[ToRadio_size]; - size_t numbytes = pb_encode_to_bytes(trBytes, sizeof(trBytes), FromRadio_fields, &fradio); c->setValue(trBytes, numbytes); } @@ -158,6 +192,10 @@ BLEService *createMeshBluetoothService(BLEServer *server) meshToRadioCharacteristic.setCallbacks(&btMeshCb); meshFromNumCharacteristic.setCallbacks(&btMeshCb); + addWithDesc(service, &meshMyNodeCharacteristic, "myNode"); + addWithDesc(service, &meshRadioCharacteristic, "radio"); + addWithDesc(service, &meshOwnerCharacteristic, "owner"); + meshFromNumCharacteristic.addDescriptor(new BLE2902()); // Needed so clients can request notification service->start(); diff --git a/src/MeshRadio.cpp b/src/MeshRadio.cpp index 6975ef201..5409d422e 100644 --- a/src/MeshRadio.cpp +++ b/src/MeshRadio.cpp @@ -12,7 +12,7 @@ // Change to 434.0 or other frequency, must match RX's freq! FIXME, choose a better default value #define RF95_FREQ_US 915.0f -RadioConfig radioConfig; +RadioConfig radioConfig = RadioConfig_init_zero; MeshRadio::MeshRadio(MemoryPool &_pool, PointerQueue &_rxDest) : rf95(NSS_GPIO, DIO0_GPIO), diff --git a/src/NodeDB.cpp b/src/NodeDB.cpp index 825d217bd..5f5852a41 100644 --- a/src/NodeDB.cpp +++ b/src/NodeDB.cpp @@ -7,8 +7,9 @@ #include "mesh-pb-constants.h" #include "NodeDB.h" -MyNodeInfo myNodeInfo; +MyNodeInfo myNodeInfo = MyNodeInfo_init_zero; NodeDB nodeDB; +User owner = User_init_zero; /** * get our starting (provisional) nodenum from flash. But check first if anyone else is using it, by trying to send a message to it (arping) diff --git a/src/NodeDB.h b/src/NodeDB.h index efcb43a67..ea0902ffa 100644 --- a/src/NodeDB.h +++ b/src/NodeDB.h @@ -51,3 +51,4 @@ private: extern NodeDB nodeDB; extern MyNodeInfo myNodeInfo; +extern User owner;