From e6535f55045f0b6345722ba6a3820ab68234d225 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 2 Feb 2020 20:54:40 -0800 Subject: [PATCH] we now BLE notify for the arrival of new messages --- TODO.md | 14 +++++++----- .../src/BluetoothSoftwareUpdate.cpp | 1 + platformio.ini | 1 - src/MeshBluetoothService.cpp | 8 +++++++ src/MeshBluetoothService.h | 6 +++++ src/MeshService.cpp | 22 ++++++++++++++++++- src/MeshService.h | 8 +++++-- 7 files changed, 50 insertions(+), 10 deletions(-) diff --git a/TODO.md b/TODO.md index c8493cb1f..5671b5540 100644 --- a/TODO.md +++ b/TODO.md @@ -1,20 +1,20 @@ # High priority -* make jtag work on second board -* notify phone when rx packets arrive, currently the phone polls at startup only -* when notified phone should download messages -* have phone use our local node number as its node number (instead of hardwired) * have MeshService keep a node DB by sniffing user messages * have meshservice send location data on mesh (if device has a GPS) +* have a state machine return the correct FromRadio packet to the phone, it isn't always going to be a MeshPacket. Do a notify on fromnum to force the radio to read our state machine generated packets +* send my_node_num when phone sends WantsNodes +* make jtag work on second board * make basic gui. different screens: debug, one page for each user in the user db, last received text message -* respond to the WantUsers message +* respond to the WantNodes message # Medium priority +* save our node db (and any rx packets waiting for phone) to flash +* send correct hw vendor in the bluetooth info * use https://lastminuteengineers.com/esp32-sleep-modes-power-consumption/ association sleep pattern to save power - but see https://github.com/espressif/esp-idf/issues/2070 * correctly map nodeids to nodenums, currently we just do a proof of concept by always doing a broadcast * add interrupt detach/sleep mode config to lora radio so we can enable deepsleep without panicing -* figure out if we can use PA_BOOST * scrub default radio config settings for bandwidth/range/speed * use a freertos thread to remain blocked reading from recvfromAckTimeout, so that we don't need to keep polling it from our main thread * override peekAtMessage so we can see any messages that pass through our node (even if not broadcast)? would that be useful? @@ -52,3 +52,5 @@ until the phone pulls those packets. Ever so often power on bluetooth just so w * make message send from android go to service, then to mesh radio * make message receive from radio go through to android * test loopback tx/rx path code without using radio +* notify phone when rx packets arrive, currently the phone polls at startup only +* figure out if we can use PA_BOOST - yes, it seems to be on both boards \ No newline at end of file diff --git a/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp b/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp index 60d17a60a..fc5d53a5f 100644 --- a/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp +++ b/lib/BluetoothOTA/src/BluetoothSoftwareUpdate.cpp @@ -69,6 +69,7 @@ class UpdateCallbacks : public BLECharacteristicCallbacks result = Update.getError(); } swUpdateResultCharacteristic.setValue(&result, 1); + swUpdateResultCharacteristic.notify(); } else { Serial.println("unexpected write"); diff --git a/platformio.ini b/platformio.ini index 2baeaec76..bd7be700a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -40,5 +40,4 @@ lib_deps = TinyGPSPlus ESP8266_SSD1306 AXP202X_Library - CRC32 SPI \ No newline at end of file diff --git a/src/MeshBluetoothService.cpp b/src/MeshBluetoothService.cpp index 8c45532ec..d8887600b 100644 --- a/src/MeshBluetoothService.cpp +++ b/src/MeshBluetoothService.cpp @@ -14,6 +14,14 @@ static BLECharacteristic meshFromRadioCharacteristic("8ba2bcc2-ee02-4a55-a531-c5 static BLECharacteristic meshToRadioCharacteristic("f75c76d2-129e-4dad-a1dd-7866124401e7", BLECharacteristic::PROPERTY_WRITE); static BLECharacteristic meshFromNumCharacteristic("ed9da18c-a800-4f66-a670-aa7547e34453", BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY); +/** + * Tell any bluetooth clients that the number of rx packets has changed + */ +void bluetoothNotifyFromNum(uint32_t newValue) { + meshFromNumCharacteristic.setValue(newValue); + meshFromNumCharacteristic.notify(); +} + class BluetoothMeshCallbacks : public BLECharacteristicCallbacks { void onRead(BLECharacteristic *c) diff --git a/src/MeshBluetoothService.h b/src/MeshBluetoothService.h index 117b1e9f4..d6f368e18 100644 --- a/src/MeshBluetoothService.h +++ b/src/MeshBluetoothService.h @@ -1,6 +1,12 @@ #pragma once +#include +#include #include BLEService *createMeshBluetoothService(BLEServer* server); +/** + * Tell any bluetooth clients that the number of rx packets has changed + */ +void bluetoothNotifyFromNum(uint32_t newValue); diff --git a/src/MeshService.cpp b/src/MeshService.cpp index 07771af18..02f741574 100644 --- a/src/MeshService.cpp +++ b/src/MeshService.cpp @@ -6,6 +6,7 @@ #include #include "mesh.pb.h" #include "MeshService.h" +#include "MeshBluetoothService.h" /* receivedPacketQueue - this is a queue of messages we've received from the mesh, which we are keeping to deliver to the phone. @@ -50,8 +51,14 @@ MeshService service; #define MAX_PACKETS 32 // max number of packets which can be in flight (either queued from reception or queued for sending) #define MAX_RX_TOPHONE 16 // max number of packets which can be waiting for delivery to android +#define MAX_RX_FROMRADIO 4 // max number of packets destined to our queue, we dispatch packets quickly so it doesn't need to be big -MeshService::MeshService() : packetPool(MAX_PACKETS), toPhoneQueue(MAX_RX_TOPHONE), radio(packetPool, toPhoneQueue) +MeshService::MeshService() + : packetPool(MAX_PACKETS), + toPhoneQueue(MAX_RX_TOPHONE), + fromRadioQueue(MAX_RX_FROMRADIO), + fromNum(0), + radio(packetPool, fromRadioQueue) { } @@ -65,6 +72,19 @@ void MeshService::init() void MeshService::loop() { radio.loop(); // FIXME, possibly move radio interaction to own thread + + MeshPacket *mp; + uint32_t oldFromNum = fromNum; + while((mp = fromRadioQueue.dequeuePtr(0)) != NULL) { + // FIXME, process the packet locally to update our node DB, update the LCD screen etc... + Serial.printf("FIXME, skipping local processing of fromRadio\n"); + + fromNum++; + assert(toPhoneQueue.enqueue(mp , 0) == pdTRUE); // FIXME, instead of failing for full queue, delete the oldest mssages + + } + if(oldFromNum != fromNum) // We don't want to generate extra notifies for multiple new packets + bluetoothNotifyFromNum(fromNum); } /// Given a ToRadio buffer parse it and properly handle it (setup radio, owner or send packet into the mesh) diff --git a/src/MeshService.h b/src/MeshService.h index 2889900e5..ab7d8661e 100644 --- a/src/MeshService.h +++ b/src/MeshService.h @@ -19,11 +19,15 @@ class MeshService /// received packets waiting for the phone to process them /// FIXME, change to a DropOldestQueue and keep a count of the number of dropped packets to ensure /// we never hang because android hasn't been there in a while + /// FIXME - save this to flash on deep sleep PointerQueue toPhoneQueue; /// Packets which have just arrived from the radio, ready to be processed by this service and possibly - /// forwarded to the phone. Note: not using yet - seeing if I can just handle everything asap in handleFromRadio - // PointerQueue fromRadioQueue; + /// forwarded to the phone. + PointerQueue fromRadioQueue; + + /// The current nonce for the newest packet which has been queued for the phone + uint32_t fromNum; public: