From 490331f3cd6c8d4f880a152275452fffa68ae0e8 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 1 Feb 2020 16:05:12 -0800 Subject: [PATCH] wip --- TODO.md | 7 ++ lib/BluetoothOTA/src/MeshBluetoothService.cpp | 78 +++++++++++++++---- src/MeshRadio.cpp | 5 +- src/main.ino | 6 +- 4 files changed, 75 insertions(+), 21 deletions(-) diff --git a/TODO.md b/TODO.md index 5b17ac0f6..8b0e92396 100644 --- a/TODO.md +++ b/TODO.md @@ -15,6 +15,9 @@ * 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? +* sendToMesh can currently block for a long time, instead have it just queue a packet for a radio freertos thread + +* fix the logo # Pre-beta priority @@ -24,6 +27,10 @@ * triple check fcc compliance * allow setting full radio params from android +# Low priority + +* report esp32 device code bugs back to the mothership via android + # Done * change the partition table to take advantage of the 4MB flash on the wroom: http://docs.platformio.org/en/latest/platforms/espressif32.html#partition-tables diff --git a/lib/BluetoothOTA/src/MeshBluetoothService.cpp b/lib/BluetoothOTA/src/MeshBluetoothService.cpp index 15d7f4593..271499431 100644 --- a/lib/BluetoothOTA/src/MeshBluetoothService.cpp +++ b/lib/BluetoothOTA/src/MeshBluetoothService.cpp @@ -5,6 +5,10 @@ #include #include +#include +#include +#include "mesh.pb.h" +#include "MeshRadio.h" /* receivedPacketQueue - this is a queue of messages we've received from the mesh, which we are keeping to deliver to the phone. @@ -49,31 +53,73 @@ public: */ +/// A temporary buffer used for sending packets, sized to hold the biggest buffer we might need +static uint8_t outbuf[MeshPacket_size]; + /** * Top level app for this service. keeps the mesh, the radio config and the queue of received packets. * */ -class MeshService { +class MeshService +{ public: + /// Given a ToRadio buffer parse it and properly handle it (setup radio, owner or send packet into the mesh) + void handleToRadio(std::string s) + { + static ToRadio r; // new ToRadio(); FIXME dynamically allocate + + pb_istream_t stream = pb_istream_from_buffer((const uint8_t *)s.c_str(), s.length()); + if (!pb_decode(&stream, ToRadio_fields, &r)) + { + Serial.printf("Error: can't decode ToRadio %s\n", PB_GET_ERROR(&stream)); + } + else + { + switch (r.which_variant) + { + case ToRadio_packet_tag: + sendToMesh(r.variant.packet); + break; + + default: + Serial.println("Error: unexpected ToRadio variant"); + break; + } + } + } + +private: + /// Send a packet into the mesh + void sendToMesh(const MeshPacket &p) + { + assert(p.has_payload); + + pb_ostream_t stream = pb_ostream_from_buffer(outbuf, sizeof(outbuf)); + if (!pb_encode(&stream, MeshPacket_fields, &p)) + { + Serial.printf("Error: can't encode MeshPacket %s\n", PB_GET_ERROR(&stream)); + } + else + { + radio.sendTo(p.to, outbuf, stream.bytes_written); + } + } }; +MeshService service; static BLECharacteristic meshFromRadioCharacteristic("8ba2bcc2-ee02-4a55-a531-c525c5e454d5", BLECharacteristic::PROPERTY_READ); 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); - -/// Given a ToRadio buffer parse it and properly handle it (setup radio, owner or send packet into the mesh) -static void handleToRadio(std::string s) { - -} - class BluetoothMeshCallbacks : public BLECharacteristicCallbacks { - void onRead(BLECharacteristic *c) { + void onRead(BLECharacteristic *c) + { Serial.println("Got on read"); - if(c == &meshFromRadioCharacteristic) { + if (c == &meshFromRadioCharacteristic) + { // Someone is going to read our value as soon as this callback returns. So fill it with the next message in the queue // or make empty if the queue is empty // c->setValue(byteptr, len); @@ -85,10 +131,12 @@ class BluetoothMeshCallbacks : public BLECharacteristicCallbacks // dumpCharacteristic(pCharacteristic); Serial.println("Got on write"); - if(c == &meshToRadioCharacteristic) { - handleToRadio(c->getValue()); + if (c == &meshToRadioCharacteristic) + { + service.handleToRadio(c->getValue()); } - else { + else + { assert(0); // Not yet implemented } } @@ -140,7 +188,8 @@ Not all messages are kept in the fromradio queue (filtered based on SubPacket): A variable keepAllPackets, if set to true will suppress this behavior and instead keep everything for forwarding to the phone (for debugging) */ -BLEService *createMeshBluetoothService(BLEServer* server) { +BLEService *createMeshBluetoothService(BLEServer *server) +{ // Create the BLE Service BLEService *service = server->createService("6ba1b218-15a8-461f-9fa8-5dcae273eafd"); @@ -151,9 +200,8 @@ BLEService *createMeshBluetoothService(BLEServer* server) { meshFromRadioCharacteristic.setCallbacks(&btMeshCb); meshToRadioCharacteristic.setCallbacks(&btMeshCb); meshFromNumCharacteristic.setCallbacks(&btMeshCb); - + meshFromNumCharacteristic.addDescriptor(new BLE2902()); // Needed so clients can request notification return service; } - diff --git a/src/MeshRadio.cpp b/src/MeshRadio.cpp index 600a8abc5..27fbe6807 100644 --- a/src/MeshRadio.cpp +++ b/src/MeshRadio.cpp @@ -69,6 +69,7 @@ bool MeshRadio::init() { ErrorCode MeshRadio::sendTo(NodeNum dest, const uint8_t *buf, size_t len) { + Serial.println("Sending..."); return manager.sendtoWait((uint8_t *) buf, len, dest); } @@ -91,11 +92,9 @@ void mesh_loop() radio.loop(); delay(1000); // Wait 1 second between transmits, could also 'sleep' here! - Serial.println("Transmitting..."); // Send a message to rf95_server - + char radiopacket[20] = "Hello World # "; sprintf(radiopacket, "hello %d", packetnum++); - Serial.println("Sending..."); radio.sendTo(NODENUM_BROADCAST, (uint8_t *)radiopacket, sizeof(radiopacket)); } diff --git a/src/main.ino b/src/main.ino index 28442abd9..a573ebe13 100644 --- a/src/main.ino +++ b/src/main.ino @@ -257,7 +257,7 @@ void setup() { #endif #ifdef LED_PIN pinMode(LED_PIN, OUTPUT); - digitalWrite(LED_PIN, 0); // turn off for now + digitalWrite(LED_PIN, 1); // turn on for now #endif // Hello @@ -274,12 +274,12 @@ void setup() { gps_setup(); // Show logo on first boot after removing battery - if (bootCount == 0) { + //if (bootCount == 0) { screen_print(APP_NAME " " APP_VERSION, 0, 0); screen_show_logo(); screen_update(); delay(LOGO_DELAY); - } + //} initBLE("KHBT Test"); // FIXME, use a real name based on the macaddr