From 412fbea9782be7b22c17617fd91c620029bb0593 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Wed, 18 Jan 2023 19:24:18 -0600 Subject: [PATCH 1/5] WIP --- src/main.cpp | 114 +++++++++++++++++++++++++++--------------- src/mesh/PhoneAPI.cpp | 85 +++++++++++++++++++++---------- src/xmodem.h | 42 ++++++++-------- 3 files changed, 153 insertions(+), 88 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 76a6a6b6f..f3a199c38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -87,7 +87,7 @@ uint8_t rtc_found; // Keystore Chips uint8_t keystore_found; -#ifndef ARCH_PORTDUINO +#if !defined(ARCH_PORTDUINO) && !defined(ARCH_STM32WL) ATECCX08A atecc; #endif @@ -98,7 +98,7 @@ uint32_t serialSinceMsec; bool pmu_found; // Array map of sensor types (as array index) and i2c address as value we'll find in the i2c scan -uint8_t nodeTelemetrySensorsMap[_TelemetrySensorType_MAX + 1] = { 0 }; // one is enough, missing elements will be initialized to 0 anyway. +uint8_t nodeTelemetrySensorsMap[_TelemetrySensorType_MAX + 1] = {0}; // one is enough, missing elements will be initialized to 0 anyway. Router *router = NULL; // Users of router don't care what sort of subclass implements that API @@ -112,9 +112,12 @@ const char *getDeviceName() static char name[20]; snprintf(name, sizeof(name), "%02x%02x", dmac[4], dmac[5]); // if the shortname exists and is NOT the new default of ab3c, use it for BLE name. - if ((owner.short_name != NULL) && (strcmp(owner.short_name, name) != 0)) { + if ((owner.short_name != NULL) && (strcmp(owner.short_name, name) != 0)) + { snprintf(name, sizeof(name), "%s_%02x%02x", owner.short_name, dmac[4], dmac[5]); - } else { + } + else + { snprintf(name, sizeof(name), "Meshtastic_%02x%02x", dmac[4], dmac[5]); } return name; @@ -169,7 +172,7 @@ void setup() #endif #ifdef DEBUG_PORT - consoleInit(); // Set serial baud rate and init our mesh console + consoleInit(); // Set serial baud rate and init our mesh console #endif serialSinceMsec = millis(); @@ -249,12 +252,12 @@ void setup() powerStatus->observe(&power->newStatus); power->setup(); // Must be after status handler is installed, so that handler gets notified of the initial configuration - #ifdef LILYGO_TBEAM_S3_CORE // In T-Beam-S3-core, the I2C device cannot be scanned before power initialization, otherwise the device will be stuck // PCF8563 RTC in tbeam-s3 uses Wire1 to share I2C bus Wire1.beginTransmission(PCF8563_RTC); - if (Wire1.endTransmission() == 0){ + if (Wire1.endTransmission() == 0) + { rtc_found = PCF8563_RTC; LOG_INFO("PCF8563 RTC found\n"); } @@ -354,9 +357,11 @@ void setup() // We have now loaded our saved preferences from flash // ONCE we will factory reset the GPS for bug #327 - if (gps && !devicestate.did_gps_reset) { + if (gps && !devicestate.did_gps_reset) + { LOG_WARN("GPS FactoryReset requested\n"); - if (gps->factoryReset()) { // If we don't succeed try again next time + if (gps->factoryReset()) + { // If we don't succeed try again next time devicestate.did_gps_reset = true; nodeDB.saveToDisk(SEGMENT_DEVICESTATE); } @@ -371,95 +376,121 @@ void setup() // radio init MUST BE AFTER service.init, so we have our radio config settings (from nodedb init) #if defined(RF95_IRQ) - if (!rIf) { + if (!rIf) + { rIf = new RF95Interface(RF95_NSS, RF95_IRQ, RF95_RESET, SPI); - if (!rIf->init()) { + if (!rIf->init()) + { LOG_WARN("Failed to find RF95 radio\n"); delete rIf; rIf = NULL; - } else { + } + else + { LOG_INFO("RF95 Radio init succeeded, using RF95 radio\n"); } } #endif #if defined(USE_SX1280) - if (!rIf) { + if (!rIf) + { rIf = new SX1280Interface(SX128X_CS, SX128X_DIO1, SX128X_RESET, SX128X_BUSY, SPI); - if (!rIf->init()) { + if (!rIf->init()) + { LOG_WARN("Failed to find SX1280 radio\n"); delete rIf; rIf = NULL; - } else { + } + else + { LOG_INFO("SX1280 Radio init succeeded, using SX1280 radio\n"); } } #endif #if defined(USE_SX1262) - if (!rIf) { + if (!rIf) + { rIf = new SX1262Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI); - if (!rIf->init()) { + if (!rIf->init()) + { LOG_WARN("Failed to find SX1262 radio\n"); delete rIf; rIf = NULL; - } else { + } + else + { LOG_INFO("SX1262 Radio init succeeded, using SX1262 radio\n"); } } #endif #if defined(USE_SX1268) - if (!rIf) { + if (!rIf) + { rIf = new SX1268Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI); - if (!rIf->init()) { + if (!rIf->init()) + { LOG_WARN("Failed to find SX1268 radio\n"); delete rIf; rIf = NULL; - } else { + } + else + { LOG_INFO("SX1268 Radio init succeeded, using SX1268 radio\n"); } } #endif #if defined(USE_LLCC68) - if (!rIf) { + if (!rIf) + { rIf = new LLCC68Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI); - if (!rIf->init()) { + if (!rIf->init()) + { LOG_WARN("Failed to find LLCC68 radio\n"); delete rIf; rIf = NULL; - } else { + } + else + { LOG_INFO("LLCC68 Radio init succeeded, using LLCC68 radio\n"); } } #endif #ifdef ARCH_PORTDUINO - if (!rIf) { + if (!rIf) + { rIf = new SimRadio; - if (!rIf->init()) { + if (!rIf->init()) + { LOG_WARN("Failed to find simulated radio\n"); delete rIf; rIf = NULL; - } else { + } + else + { LOG_INFO("Using SIMULATED radio!\n"); } } #endif -// check if the radio chip matches the selected region + // check if the radio chip matches the selected region -if((config.lora.region == Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLora())){ - LOG_WARN("Radio chip does not support 2.4GHz LoRa. Reverting to unset.\n"); - config.lora.region = Config_LoRaConfig_RegionCode_UNSET; - nodeDB.saveToDisk(SEGMENT_CONFIG); - if(!rIf->reconfigure()) { - LOG_WARN("Reconfigure failed, rebooting\n"); - screen->startRebootScreen(); - rebootAtMsec = millis() + 5000; + if ((config.lora.region == Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLora())) + { + LOG_WARN("Radio chip does not support 2.4GHz LoRa. Reverting to unset.\n"); + config.lora.region = Config_LoRaConfig_RegionCode_UNSET; + nodeDB.saveToDisk(SEGMENT_CONFIG); + if (!rIf->reconfigure()) + { + LOG_WARN("Reconfigure failed, rebooting\n"); + screen->startRebootScreen(); + rebootAtMsec = millis() + 5000; + } } -} #if HAS_WIFI || HAS_ETHERNET mqttInit(); @@ -487,7 +518,8 @@ if((config.lora.region == Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLo if (!rIf) RECORD_CRITICALERROR(CriticalErrorCode_NO_RADIO); - else { + else + { router->addInterface(rIf); // Calculate and save the bit rate to myNodeInfo @@ -532,7 +564,8 @@ void loop() #ifdef DEBUG_STACK static uint32_t lastPrint = 0; - if (millis() - lastPrint > 10 * 1000L) { + if (millis() - lastPrint > 10 * 1000L) + { lastPrint = millis(); meshtastic::printThreadInfo("main"); } @@ -550,7 +583,8 @@ void loop() mainController.nextThread->tillRun(millis())); */ // We want to sleep as long as possible here - because it saves power - if (!runASAP && loopCanSleep()) { + if (!runASAP && loopCanSleep()) + { // if(delayMsec > 100) LOG_DEBUG("sleeping %ld\n", delayMsec); mainDelay.delay(delayMsec); } diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 7c5781879..76a5689e3 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -29,7 +29,8 @@ PhoneAPI::~PhoneAPI() void PhoneAPI::handleStartConfig() { // Must be before setting state (because state is how we know !connected) - if (!isConnected()) { + if (!isConnected()) + { onConnectionChanged(true); observe(&service.fromNumChanged); observe(&xModem.packetReady); @@ -46,7 +47,8 @@ void PhoneAPI::handleStartConfig() void PhoneAPI::close() { - if (state != STATE_SEND_NOTHING) { + if (state != STATE_SEND_NOTHING) + { state = STATE_SEND_NOTHING; unobserve(&service.fromNumChanged); @@ -60,9 +62,11 @@ void PhoneAPI::close() void PhoneAPI::checkConnectionTimeout() { - if (isConnected()) { + if (isConnected()) + { bool newContact = checkIsConnected(); - if (!newContact) { + if (!newContact) + { LOG_INFO("Lost phone connection\n"); close(); } @@ -80,8 +84,10 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) // return (lastContactMsec != 0) && memset(&toRadioScratch, 0, sizeof(toRadioScratch)); - if (pb_decode_from_bytes(buf, bufLength, &ToRadio_msg, &toRadioScratch)) { - switch (toRadioScratch.which_payload_variant) { + if (pb_decode_from_bytes(buf, bufLength, &ToRadio_msg, &toRadioScratch)) + { + switch (toRadioScratch.which_payload_variant) + { case ToRadio_packet_tag: return handleToRadioPacket(toRadioScratch.packet); case ToRadio_want_config_id_tag: @@ -102,7 +108,9 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) // LOG_DEBUG("Error: unexpected ToRadio variant\n"); break; } - } else { + } + else + { LOG_ERROR("Error: ignoring malformed toradio\n"); } @@ -125,7 +133,8 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) */ size_t PhoneAPI::getFromRadio(uint8_t *buf) { - if (!available()) { + if (!available()) + { // LOG_DEBUG("getFromRadio=not available\n"); return 0; } @@ -133,7 +142,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) memset(&fromRadioScratch, 0, sizeof(fromRadioScratch)); // Advance states as needed - switch (state) { + switch (state) + { case STATE_SEND_NOTHING: LOG_INFO("getFromRadio=STATE_SEND_NOTHING\n"); break; @@ -150,18 +160,22 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) service.refreshMyNodeInfo(); // Update my NodeInfo because the client will be asking for it soon. break; - case STATE_SEND_NODEINFO: { + case STATE_SEND_NODEINFO: + { LOG_INFO("getFromRadio=STATE_SEND_NODEINFO\n"); const NodeInfo *info = nodeInfoForPhone; nodeInfoForPhone = NULL; // We just consumed a nodeinfo, will need a new one next time - if (info) { + if (info) + { LOG_INFO("Sending nodeinfo: num=0x%x, lastseen=%u, id=%s, name=%s\n", info->num, info->last_heard, info->user.id, info->user.long_name); fromRadioScratch.which_payload_variant = FromRadio_node_info_tag; fromRadioScratch.node_info = *info; // Stay in current state until done sending nodeinfos - } else { + } + else + { LOG_INFO("Done sending nodeinfos\n"); state = STATE_SEND_CHANNELS; // Go ahead and send that ID right now @@ -176,7 +190,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) fromRadioScratch.channel = channels.getByIndex(config_state); config_state++; // Advance when we have sent all of our Channels - if (config_state >= MAX_NUM_CHANNELS) { + if (config_state >= MAX_NUM_CHANNELS) + { state = STATE_SEND_CONFIG; config_state = _AdminMessage_ConfigType_MIN + 1; } @@ -185,7 +200,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_CONFIG: LOG_INFO("getFromRadio=STATE_SEND_CONFIG\n"); fromRadioScratch.which_payload_variant = FromRadio_config_tag; - switch (config_state) { + switch (config_state) + { case Config_device_tag: fromRadioScratch.config.which_payload_variant = Config_device_tag; fromRadioScratch.config.payload_variant.device = config.device; @@ -224,7 +240,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) config_state++; // Advance when we have sent all of our config objects - if (config_state > (_AdminMessage_ConfigType_MAX + 1)) { + if (config_state > (_AdminMessage_ConfigType_MAX + 1)) + { state = STATE_SEND_MODULECONFIG; config_state = _AdminMessage_ModuleConfigType_MIN + 1; } @@ -233,7 +250,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_MODULECONFIG: LOG_INFO("getFromRadio=STATE_SEND_MODULECONFIG\n"); fromRadioScratch.which_payload_variant = FromRadio_moduleConfig_tag; - switch (config_state) { + switch (config_state) + { case ModuleConfig_mqtt_tag: fromRadioScratch.moduleConfig.which_payload_variant = ModuleConfig_mqtt_tag; fromRadioScratch.moduleConfig.payload_variant.mqtt = moduleConfig.mqtt; @@ -276,7 +294,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) config_state++; // Advance when we have sent all of our ModuleConfig objects - if (config_state > (_AdminMessage_ModuleConfigType_MAX + 1)) { + if (config_state > (_AdminMessage_ModuleConfigType_MAX + 1)) + { state = STATE_SEND_COMPLETE_ID; config_state = 0; } @@ -293,16 +312,21 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_PACKETS: // Do we have a message from the mesh? LOG_INFO("getFromRadio=STATE_SEND_PACKETS\n"); - if (queueStatusPacketForPhone) { + if (queueStatusPacketForPhone) + { fromRadioScratch.which_payload_variant = FromRadio_queueStatus_tag; fromRadioScratch.queueStatus = *queueStatusPacketForPhone; releaseQueueStatusPhonePacket(); - } else if (xmodemPacketForPhone) { + } + else if (xmodemPacketForPhone) + { fromRadioScratch.which_payload_variant = FromRadio_xmodemPacket_tag; fromRadioScratch.xmodemPacket = *xmodemPacketForPhone; free(xmodemPacketForPhone); xmodemPacketForPhone = NULL; - } else if (packetForPhone) { + } + else if (packetForPhone) + { printPacket("phone downloaded packet", packetForPhone); // Encapsulate as a FromRadio packet @@ -317,7 +341,8 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) } // Do we have a message from the mesh? - if (fromRadioScratch.which_payload_variant != 0) { + if (fromRadioScratch.which_payload_variant != 0) + { // Encapsulate as a FromRadio packet size_t numbytes = pb_encode_to_bytes(buf, FromRadio_size, &FromRadio_msg, &fromRadioScratch); @@ -336,7 +361,8 @@ void PhoneAPI::handleDisconnect() void PhoneAPI::releasePhonePacket() { - if (packetForPhone) { + if (packetForPhone) + { service.releaseToPool(packetForPhone); // we just copied the bytes, so don't need this buffer anymore packetForPhone = NULL; } @@ -344,7 +370,8 @@ void PhoneAPI::releasePhonePacket() void PhoneAPI::releaseQueueStatusPhonePacket() { - if (queueStatusPacketForPhone) { + if (queueStatusPacketForPhone) + { service.releaseQueueStatusToPool(queueStatusPacketForPhone); queueStatusPacketForPhone = NULL; } @@ -355,7 +382,8 @@ void PhoneAPI::releaseQueueStatusPhonePacket() */ bool PhoneAPI::available() { - switch (state) { + switch (state) + { case STATE_SEND_NOTHING: return false; case STATE_SEND_MY_INFO: @@ -370,7 +398,8 @@ bool PhoneAPI::available() nodeInfoForPhone = nodeDB.readNextInfo(); return true; // Always say we have something, because we might need to advance our state machine - case STATE_SEND_PACKETS: { + case STATE_SEND_PACKETS: + { if (!queueStatusPacketForPhone) queueStatusPacketForPhone = service.getQueueStatusForPhone(); bool hasPacket = !!queueStatusPacketForPhone; @@ -413,10 +442,12 @@ int PhoneAPI::onNotify(uint32_t newValue) checkConnectionTimeout(); // a handy place to check if we've heard from the phone (since the BLE version doesn't call this // from idle) - if (state == STATE_SEND_PACKETS) { + if (state == STATE_SEND_PACKETS) + { LOG_INFO("Telling client we have new packets %u\n", newValue); onNowHasData(newValue); - } else + } + else LOG_DEBUG("(Client not yet interested in packets)\n"); return 0; diff --git a/src/xmodem.h b/src/xmodem.h index 8a9c08db6..3d6e6b27d 100644 --- a/src/xmodem.h +++ b/src/xmodem.h @@ -40,37 +40,37 @@ class XModemAdapter { - public: - // Called when we put a fragment in the outgoing memory - Observable packetReady; +public: + // Called when we put a fragment in the outgoing memory + Observable packetReady; - XModemAdapter(); + XModemAdapter(); - void handlePacket(XModem xmodemPacket); - XModem *getForPhone(); + void handlePacket(XModem xmodemPacket); + XModem *getForPhone(); - private: - bool isReceiving = false; - bool isTransmitting = false; - bool isEOT = false; +private: + bool isReceiving = false; + bool isTransmitting = false; + bool isEOT = false; - int retrans = MAXRETRANS; + int retrans = MAXRETRANS; - uint16_t packetno = 0; + uint16_t packetno = 0; -#ifdef ARCH_NRF52 - File file = File(FSCom); +#if defined(ARCH_NRF52) + File file = File(FSCom); #else - File file; + File file; #endif - char filename[sizeof(XModem_buffer_t::bytes)] = {0}; + char filename[sizeof(XModem_buffer_t::bytes)] = {0}; - protected: - XModem *xmodemStore = NULL; - unsigned short crc16_ccitt(const pb_byte_t *buffer, int length); - int check(const pb_byte_t *buf, int sz, unsigned short tcrc); - void sendControl(XModem_Control c); +protected: + XModem *xmodemStore = NULL; + unsigned short crc16_ccitt(const pb_byte_t *buffer, int length); + int check(const pb_byte_t *buf, int sz, unsigned short tcrc); + void sendControl(XModem_Control c); }; extern XModemAdapter xModem; From 660a73d58caca5f462020a09c0a38bbe8e941bd2 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Wed, 18 Jan 2023 19:45:39 -0600 Subject: [PATCH 2/5] Stuff --- src/modules/Modules.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index cd873a466..eefdeab37 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -42,7 +42,7 @@ void setupModules() waypointModule = new WaypointModule(); textMessageModule = new TextMessageModule(); traceRouteModule = new TraceRouteModule(); - + // Note: if the rest of meshtastic doesn't need to explicitly use your module, you do not need to assign the instance // to a global variable. From 9d47f7a531e8a7b9560b69efc031008b6ebbf6d2 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Wed, 18 Jan 2023 20:03:10 -0600 Subject: [PATCH 3/5] Format --- src/main.cpp | 98 ++++++++++++++--------------------------- src/mesh/PhoneAPI.cpp | 85 ++++++++++++----------------------- src/modules/Modules.cpp | 2 +- src/xmodem.h | 40 ++++++++--------- 4 files changed, 80 insertions(+), 145 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f3a199c38..77becf435 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,12 +3,12 @@ #include "MeshService.h" #include "NodeDB.h" #include "PowerFSM.h" +#include "ReliableRouter.h" #include "airtime.h" #include "buzz.h" #include "configuration.h" #include "error.h" #include "power.h" -#include "ReliableRouter.h" // #include "debug.h" #include "FSCommon.h" #include "RTC.h" @@ -27,8 +27,8 @@ #include // #include -#include "mesh/http/WiFiAPClient.h" #include "mesh/eth/ethClient.h" +#include "mesh/http/WiFiAPClient.h" #ifdef ARCH_ESP32 #include "mesh/http/WebServer.h" @@ -98,7 +98,8 @@ uint32_t serialSinceMsec; bool pmu_found; // Array map of sensor types (as array index) and i2c address as value we'll find in the i2c scan -uint8_t nodeTelemetrySensorsMap[_TelemetrySensorType_MAX + 1] = {0}; // one is enough, missing elements will be initialized to 0 anyway. +uint8_t nodeTelemetrySensorsMap[_TelemetrySensorType_MAX + 1] = { + 0}; // one is enough, missing elements will be initialized to 0 anyway. Router *router = NULL; // Users of router don't care what sort of subclass implements that API @@ -112,12 +113,9 @@ const char *getDeviceName() static char name[20]; snprintf(name, sizeof(name), "%02x%02x", dmac[4], dmac[5]); // if the shortname exists and is NOT the new default of ab3c, use it for BLE name. - if ((owner.short_name != NULL) && (strcmp(owner.short_name, name) != 0)) - { + if ((owner.short_name != NULL) && (strcmp(owner.short_name, name) != 0)) { snprintf(name, sizeof(name), "%s_%02x%02x", owner.short_name, dmac[4], dmac[5]); - } - else - { + } else { snprintf(name, sizeof(name), "Meshtastic_%02x%02x", dmac[4], dmac[5]); } return name; @@ -256,8 +254,7 @@ void setup() // In T-Beam-S3-core, the I2C device cannot be scanned before power initialization, otherwise the device will be stuck // PCF8563 RTC in tbeam-s3 uses Wire1 to share I2C bus Wire1.beginTransmission(PCF8563_RTC); - if (Wire1.endTransmission() == 0) - { + if (Wire1.endTransmission() == 0) { rtc_found = PCF8563_RTC; LOG_INFO("PCF8563 RTC found\n"); } @@ -357,11 +354,9 @@ void setup() // We have now loaded our saved preferences from flash // ONCE we will factory reset the GPS for bug #327 - if (gps && !devicestate.did_gps_reset) - { + if (gps && !devicestate.did_gps_reset) { LOG_WARN("GPS FactoryReset requested\n"); - if (gps->factoryReset()) - { // If we don't succeed try again next time + if (gps->factoryReset()) { // If we don't succeed try again next time devicestate.did_gps_reset = true; nodeDB.saveToDisk(SEGMENT_DEVICESTATE); } @@ -376,102 +371,78 @@ void setup() // radio init MUST BE AFTER service.init, so we have our radio config settings (from nodedb init) #if defined(RF95_IRQ) - if (!rIf) - { + if (!rIf) { rIf = new RF95Interface(RF95_NSS, RF95_IRQ, RF95_RESET, SPI); - if (!rIf->init()) - { + if (!rIf->init()) { LOG_WARN("Failed to find RF95 radio\n"); delete rIf; rIf = NULL; - } - else - { + } else { LOG_INFO("RF95 Radio init succeeded, using RF95 radio\n"); } } #endif #if defined(USE_SX1280) - if (!rIf) - { + if (!rIf) { rIf = new SX1280Interface(SX128X_CS, SX128X_DIO1, SX128X_RESET, SX128X_BUSY, SPI); - if (!rIf->init()) - { + if (!rIf->init()) { LOG_WARN("Failed to find SX1280 radio\n"); delete rIf; rIf = NULL; - } - else - { + } else { LOG_INFO("SX1280 Radio init succeeded, using SX1280 radio\n"); } } #endif #if defined(USE_SX1262) - if (!rIf) - { + if (!rIf) { rIf = new SX1262Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI); - if (!rIf->init()) - { + if (!rIf->init()) { LOG_WARN("Failed to find SX1262 radio\n"); delete rIf; rIf = NULL; - } - else - { + } else { LOG_INFO("SX1262 Radio init succeeded, using SX1262 radio\n"); } } #endif #if defined(USE_SX1268) - if (!rIf) - { + if (!rIf) { rIf = new SX1268Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI); - if (!rIf->init()) - { + if (!rIf->init()) { LOG_WARN("Failed to find SX1268 radio\n"); delete rIf; rIf = NULL; - } - else - { + } else { LOG_INFO("SX1268 Radio init succeeded, using SX1268 radio\n"); } } #endif #if defined(USE_LLCC68) - if (!rIf) - { + if (!rIf) { rIf = new LLCC68Interface(SX126X_CS, SX126X_DIO1, SX126X_RESET, SX126X_BUSY, SPI); - if (!rIf->init()) - { + if (!rIf->init()) { LOG_WARN("Failed to find LLCC68 radio\n"); delete rIf; rIf = NULL; - } - else - { + } else { LOG_INFO("LLCC68 Radio init succeeded, using LLCC68 radio\n"); } } #endif #ifdef ARCH_PORTDUINO - if (!rIf) - { + if (!rIf) { rIf = new SimRadio; - if (!rIf->init()) - { + if (!rIf->init()) { LOG_WARN("Failed to find simulated radio\n"); delete rIf; rIf = NULL; - } - else - { + } else { LOG_INFO("Using SIMULATED radio!\n"); } } @@ -479,13 +450,11 @@ void setup() // check if the radio chip matches the selected region - if ((config.lora.region == Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLora())) - { + if ((config.lora.region == Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLora())) { LOG_WARN("Radio chip does not support 2.4GHz LoRa. Reverting to unset.\n"); config.lora.region = Config_LoRaConfig_RegionCode_UNSET; nodeDB.saveToDisk(SEGMENT_CONFIG); - if (!rIf->reconfigure()) - { + if (!rIf->reconfigure()) { LOG_WARN("Reconfigure failed, rebooting\n"); screen->startRebootScreen(); rebootAtMsec = millis() + 5000; @@ -518,8 +487,7 @@ void setup() if (!rIf) RECORD_CRITICALERROR(CriticalErrorCode_NO_RADIO); - else - { + else { router->addInterface(rIf); // Calculate and save the bit rate to myNodeInfo @@ -564,8 +532,7 @@ void loop() #ifdef DEBUG_STACK static uint32_t lastPrint = 0; - if (millis() - lastPrint > 10 * 1000L) - { + if (millis() - lastPrint > 10 * 1000L) { lastPrint = millis(); meshtastic::printThreadInfo("main"); } @@ -583,8 +550,7 @@ void loop() mainController.nextThread->tillRun(millis())); */ // We want to sleep as long as possible here - because it saves power - if (!runASAP && loopCanSleep()) - { + if (!runASAP && loopCanSleep()) { // if(delayMsec > 100) LOG_DEBUG("sleeping %ld\n", delayMsec); mainDelay.delay(delayMsec); } diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index 76a5689e3..7c5781879 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -29,8 +29,7 @@ PhoneAPI::~PhoneAPI() void PhoneAPI::handleStartConfig() { // Must be before setting state (because state is how we know !connected) - if (!isConnected()) - { + if (!isConnected()) { onConnectionChanged(true); observe(&service.fromNumChanged); observe(&xModem.packetReady); @@ -47,8 +46,7 @@ void PhoneAPI::handleStartConfig() void PhoneAPI::close() { - if (state != STATE_SEND_NOTHING) - { + if (state != STATE_SEND_NOTHING) { state = STATE_SEND_NOTHING; unobserve(&service.fromNumChanged); @@ -62,11 +60,9 @@ void PhoneAPI::close() void PhoneAPI::checkConnectionTimeout() { - if (isConnected()) - { + if (isConnected()) { bool newContact = checkIsConnected(); - if (!newContact) - { + if (!newContact) { LOG_INFO("Lost phone connection\n"); close(); } @@ -84,10 +80,8 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) // return (lastContactMsec != 0) && memset(&toRadioScratch, 0, sizeof(toRadioScratch)); - if (pb_decode_from_bytes(buf, bufLength, &ToRadio_msg, &toRadioScratch)) - { - switch (toRadioScratch.which_payload_variant) - { + if (pb_decode_from_bytes(buf, bufLength, &ToRadio_msg, &toRadioScratch)) { + switch (toRadioScratch.which_payload_variant) { case ToRadio_packet_tag: return handleToRadioPacket(toRadioScratch.packet); case ToRadio_want_config_id_tag: @@ -108,9 +102,7 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) // LOG_DEBUG("Error: unexpected ToRadio variant\n"); break; } - } - else - { + } else { LOG_ERROR("Error: ignoring malformed toradio\n"); } @@ -133,8 +125,7 @@ bool PhoneAPI::handleToRadio(const uint8_t *buf, size_t bufLength) */ size_t PhoneAPI::getFromRadio(uint8_t *buf) { - if (!available()) - { + if (!available()) { // LOG_DEBUG("getFromRadio=not available\n"); return 0; } @@ -142,8 +133,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) memset(&fromRadioScratch, 0, sizeof(fromRadioScratch)); // Advance states as needed - switch (state) - { + switch (state) { case STATE_SEND_NOTHING: LOG_INFO("getFromRadio=STATE_SEND_NOTHING\n"); break; @@ -160,22 +150,18 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) service.refreshMyNodeInfo(); // Update my NodeInfo because the client will be asking for it soon. break; - case STATE_SEND_NODEINFO: - { + case STATE_SEND_NODEINFO: { LOG_INFO("getFromRadio=STATE_SEND_NODEINFO\n"); const NodeInfo *info = nodeInfoForPhone; nodeInfoForPhone = NULL; // We just consumed a nodeinfo, will need a new one next time - if (info) - { + if (info) { LOG_INFO("Sending nodeinfo: num=0x%x, lastseen=%u, id=%s, name=%s\n", info->num, info->last_heard, info->user.id, info->user.long_name); fromRadioScratch.which_payload_variant = FromRadio_node_info_tag; fromRadioScratch.node_info = *info; // Stay in current state until done sending nodeinfos - } - else - { + } else { LOG_INFO("Done sending nodeinfos\n"); state = STATE_SEND_CHANNELS; // Go ahead and send that ID right now @@ -190,8 +176,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) fromRadioScratch.channel = channels.getByIndex(config_state); config_state++; // Advance when we have sent all of our Channels - if (config_state >= MAX_NUM_CHANNELS) - { + if (config_state >= MAX_NUM_CHANNELS) { state = STATE_SEND_CONFIG; config_state = _AdminMessage_ConfigType_MIN + 1; } @@ -200,8 +185,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_CONFIG: LOG_INFO("getFromRadio=STATE_SEND_CONFIG\n"); fromRadioScratch.which_payload_variant = FromRadio_config_tag; - switch (config_state) - { + switch (config_state) { case Config_device_tag: fromRadioScratch.config.which_payload_variant = Config_device_tag; fromRadioScratch.config.payload_variant.device = config.device; @@ -240,8 +224,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) config_state++; // Advance when we have sent all of our config objects - if (config_state > (_AdminMessage_ConfigType_MAX + 1)) - { + if (config_state > (_AdminMessage_ConfigType_MAX + 1)) { state = STATE_SEND_MODULECONFIG; config_state = _AdminMessage_ModuleConfigType_MIN + 1; } @@ -250,8 +233,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_MODULECONFIG: LOG_INFO("getFromRadio=STATE_SEND_MODULECONFIG\n"); fromRadioScratch.which_payload_variant = FromRadio_moduleConfig_tag; - switch (config_state) - { + switch (config_state) { case ModuleConfig_mqtt_tag: fromRadioScratch.moduleConfig.which_payload_variant = ModuleConfig_mqtt_tag; fromRadioScratch.moduleConfig.payload_variant.mqtt = moduleConfig.mqtt; @@ -294,8 +276,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) config_state++; // Advance when we have sent all of our ModuleConfig objects - if (config_state > (_AdminMessage_ModuleConfigType_MAX + 1)) - { + if (config_state > (_AdminMessage_ModuleConfigType_MAX + 1)) { state = STATE_SEND_COMPLETE_ID; config_state = 0; } @@ -312,21 +293,16 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) case STATE_SEND_PACKETS: // Do we have a message from the mesh? LOG_INFO("getFromRadio=STATE_SEND_PACKETS\n"); - if (queueStatusPacketForPhone) - { + if (queueStatusPacketForPhone) { fromRadioScratch.which_payload_variant = FromRadio_queueStatus_tag; fromRadioScratch.queueStatus = *queueStatusPacketForPhone; releaseQueueStatusPhonePacket(); - } - else if (xmodemPacketForPhone) - { + } else if (xmodemPacketForPhone) { fromRadioScratch.which_payload_variant = FromRadio_xmodemPacket_tag; fromRadioScratch.xmodemPacket = *xmodemPacketForPhone; free(xmodemPacketForPhone); xmodemPacketForPhone = NULL; - } - else if (packetForPhone) - { + } else if (packetForPhone) { printPacket("phone downloaded packet", packetForPhone); // Encapsulate as a FromRadio packet @@ -341,8 +317,7 @@ size_t PhoneAPI::getFromRadio(uint8_t *buf) } // Do we have a message from the mesh? - if (fromRadioScratch.which_payload_variant != 0) - { + if (fromRadioScratch.which_payload_variant != 0) { // Encapsulate as a FromRadio packet size_t numbytes = pb_encode_to_bytes(buf, FromRadio_size, &FromRadio_msg, &fromRadioScratch); @@ -361,8 +336,7 @@ void PhoneAPI::handleDisconnect() void PhoneAPI::releasePhonePacket() { - if (packetForPhone) - { + if (packetForPhone) { service.releaseToPool(packetForPhone); // we just copied the bytes, so don't need this buffer anymore packetForPhone = NULL; } @@ -370,8 +344,7 @@ void PhoneAPI::releasePhonePacket() void PhoneAPI::releaseQueueStatusPhonePacket() { - if (queueStatusPacketForPhone) - { + if (queueStatusPacketForPhone) { service.releaseQueueStatusToPool(queueStatusPacketForPhone); queueStatusPacketForPhone = NULL; } @@ -382,8 +355,7 @@ void PhoneAPI::releaseQueueStatusPhonePacket() */ bool PhoneAPI::available() { - switch (state) - { + switch (state) { case STATE_SEND_NOTHING: return false; case STATE_SEND_MY_INFO: @@ -398,8 +370,7 @@ bool PhoneAPI::available() nodeInfoForPhone = nodeDB.readNextInfo(); return true; // Always say we have something, because we might need to advance our state machine - case STATE_SEND_PACKETS: - { + case STATE_SEND_PACKETS: { if (!queueStatusPacketForPhone) queueStatusPacketForPhone = service.getQueueStatusForPhone(); bool hasPacket = !!queueStatusPacketForPhone; @@ -442,12 +413,10 @@ int PhoneAPI::onNotify(uint32_t newValue) checkConnectionTimeout(); // a handy place to check if we've heard from the phone (since the BLE version doesn't call this // from idle) - if (state == STATE_SEND_PACKETS) - { + if (state == STATE_SEND_PACKETS) { LOG_INFO("Telling client we have new packets %u\n", newValue); onNowHasData(newValue); - } - else + } else LOG_DEBUG("(Client not yet interested in packets)\n"); return 0; diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index eefdeab37..aacb77151 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -18,9 +18,9 @@ #include "modules/Telemetry/EnvironmentTelemetry.h" #endif #ifdef ARCH_ESP32 +#include "modules/esp32/AudioModule.h" #include "modules/esp32/RangeTestModule.h" #include "modules/esp32/StoreForwardModule.h" -#include "modules/esp32/AudioModule.h" #endif #if defined(ARCH_ESP32) || defined(ARCH_NRF52) #include "modules/ExternalNotificationModule.h" diff --git a/src/xmodem.h b/src/xmodem.h index 3d6e6b27d..4baa713e2 100644 --- a/src/xmodem.h +++ b/src/xmodem.h @@ -40,37 +40,37 @@ class XModemAdapter { -public: - // Called when we put a fragment in the outgoing memory - Observable packetReady; + public: + // Called when we put a fragment in the outgoing memory + Observable packetReady; - XModemAdapter(); + XModemAdapter(); - void handlePacket(XModem xmodemPacket); - XModem *getForPhone(); + void handlePacket(XModem xmodemPacket); + XModem *getForPhone(); -private: - bool isReceiving = false; - bool isTransmitting = false; - bool isEOT = false; + private: + bool isReceiving = false; + bool isTransmitting = false; + bool isEOT = false; - int retrans = MAXRETRANS; + int retrans = MAXRETRANS; - uint16_t packetno = 0; + uint16_t packetno = 0; #if defined(ARCH_NRF52) - File file = File(FSCom); + File file = File(FSCom); #else - File file; + File file; #endif - char filename[sizeof(XModem_buffer_t::bytes)] = {0}; + char filename[sizeof(XModem_buffer_t::bytes)] = {0}; -protected: - XModem *xmodemStore = NULL; - unsigned short crc16_ccitt(const pb_byte_t *buffer, int length); - int check(const pb_byte_t *buf, int sz, unsigned short tcrc); - void sendControl(XModem_Control c); + protected: + XModem *xmodemStore = NULL; + unsigned short crc16_ccitt(const pb_byte_t *buffer, int length); + int check(const pb_byte_t *buf, int sz, unsigned short tcrc); + void sendControl(XModem_Control c); }; extern XModemAdapter xModem; From e2e7658789892ef05dbc396f053826b2b567e7af Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sat, 21 Jan 2023 19:24:33 -0600 Subject: [PATCH 4/5] w/e --- variants/rak4631/variant.h | 43 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index a37e1e7de..8e0831e79 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -34,7 +34,8 @@ #include "WVariant.h" #ifdef __cplusplus -extern "C" { +extern "C" +{ #endif // __cplusplus // Number of pins defined in PinDescription array @@ -55,9 +56,9 @@ extern "C" { #define LED_STATE_ON 1 // State when LED is litted -/* - * Buttons - */ + /* + * Buttons + */ #define PIN_BUTTON1 9 // Pin for button on E-ink button module or IO expansion #define BUTTON_NEED_PULLUP @@ -77,14 +78,14 @@ extern "C" { #define PIN_A6 (0xff) #define PIN_A7 (0xff) -static const uint8_t A0 = PIN_A0; -static const uint8_t A1 = PIN_A1; -static const uint8_t A2 = PIN_A2; -static const uint8_t A3 = PIN_A3; -static const uint8_t A4 = PIN_A4; -static const uint8_t A5 = PIN_A5; -static const uint8_t A6 = PIN_A6; -static const uint8_t A7 = PIN_A7; + static const uint8_t A0 = PIN_A0; + static const uint8_t A1 = PIN_A1; + static const uint8_t A2 = PIN_A2; + static const uint8_t A3 = PIN_A3; + static const uint8_t A4 = PIN_A4; + static const uint8_t A5 = PIN_A5; + static const uint8_t A6 = PIN_A6; + static const uint8_t A7 = PIN_A7; #define ADC_RESOLUTION 14 // Other pins @@ -92,7 +93,7 @@ static const uint8_t A7 = PIN_A7; #define PIN_NFC1 (9) #define PIN_NFC2 (10) -static const uint8_t AREF = PIN_AREF; + static const uint8_t AREF = PIN_AREF; /* * Serial interfaces @@ -117,14 +118,14 @@ static const uint8_t AREF = PIN_AREF; #define PIN_SPI1_MOSI (30) // (0 + 30) #define PIN_SPI1_SCK (3) // (0 + 3) -static const uint8_t SS = 42; -static const uint8_t MOSI = PIN_SPI_MOSI; -static const uint8_t MISO = PIN_SPI_MISO; -static const uint8_t SCK = PIN_SPI_SCK; + static const uint8_t SS = 42; + static const uint8_t MOSI = PIN_SPI_MOSI; + static const uint8_t MISO = PIN_SPI_MISO; + static const uint8_t SCK = PIN_SPI_SCK; -/* - * eink display pins - */ + /* + * eink display pins + */ #define PIN_EINK_EN (32 + 2) // (0 + 2) Note: this is really just backlight power #define PIN_EINK_CS (0 + 26) @@ -136,7 +137,7 @@ static const uint8_t SCK = PIN_SPI_SCK; // Controls power for the eink display - Board power is enabled either by VBUS from USB or the CPU asserting PWR_ON // FIXME - I think this is actually just the board power enable - it enables power to the CPU also -//#define PIN_EINK_PWR_ON (-1) +// #define PIN_EINK_PWR_ON (-1) // #define USE_EINK From 266c61065dffb1469333c7272e1d60ca0c7ccdb6 Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Sat, 21 Jan 2023 19:51:24 -0600 Subject: [PATCH 5/5] I swear I did this before you butthole --- variants/rak4631/variant.h | 41 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/variants/rak4631/variant.h b/variants/rak4631/variant.h index 8e0831e79..1845a5783 100644 --- a/variants/rak4631/variant.h +++ b/variants/rak4631/variant.h @@ -34,8 +34,7 @@ #include "WVariant.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif // __cplusplus // Number of pins defined in PinDescription array @@ -56,9 +55,9 @@ extern "C" #define LED_STATE_ON 1 // State when LED is litted - /* - * Buttons - */ +/* + * Buttons + */ #define PIN_BUTTON1 9 // Pin for button on E-ink button module or IO expansion #define BUTTON_NEED_PULLUP @@ -78,14 +77,14 @@ extern "C" #define PIN_A6 (0xff) #define PIN_A7 (0xff) - static const uint8_t A0 = PIN_A0; - static const uint8_t A1 = PIN_A1; - static const uint8_t A2 = PIN_A2; - static const uint8_t A3 = PIN_A3; - static const uint8_t A4 = PIN_A4; - static const uint8_t A5 = PIN_A5; - static const uint8_t A6 = PIN_A6; - static const uint8_t A7 = PIN_A7; +static const uint8_t A0 = PIN_A0; +static const uint8_t A1 = PIN_A1; +static const uint8_t A2 = PIN_A2; +static const uint8_t A3 = PIN_A3; +static const uint8_t A4 = PIN_A4; +static const uint8_t A5 = PIN_A5; +static const uint8_t A6 = PIN_A6; +static const uint8_t A7 = PIN_A7; #define ADC_RESOLUTION 14 // Other pins @@ -93,7 +92,7 @@ extern "C" #define PIN_NFC1 (9) #define PIN_NFC2 (10) - static const uint8_t AREF = PIN_AREF; +static const uint8_t AREF = PIN_AREF; /* * Serial interfaces @@ -118,14 +117,14 @@ extern "C" #define PIN_SPI1_MOSI (30) // (0 + 30) #define PIN_SPI1_SCK (3) // (0 + 3) - static const uint8_t SS = 42; - static const uint8_t MOSI = PIN_SPI_MOSI; - static const uint8_t MISO = PIN_SPI_MISO; - static const uint8_t SCK = PIN_SPI_SCK; +static const uint8_t SS = 42; +static const uint8_t MOSI = PIN_SPI_MOSI; +static const uint8_t MISO = PIN_SPI_MISO; +static const uint8_t SCK = PIN_SPI_SCK; - /* - * eink display pins - */ +/* + * eink display pins + */ #define PIN_EINK_EN (32 + 2) // (0 + 2) Note: this is really just backlight power #define PIN_EINK_CS (0 + 26)