From a280d7f7969a6362cdbb2412c551d96b64587223 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 10:00:19 +0100 Subject: [PATCH 1/7] Guard simulator handling with HAS_RADIO flag --- src/main.cpp | 26 +++++++++++++------------- src/mesh/MeshService.cpp | 2 +- src/mesh/MeshService.h | 2 +- src/platform/portduino/architecture.h | 4 ---- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 89148c22bc..f2681bc192 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -394,6 +394,19 @@ void setup() // radio init MUST BE AFTER service.init, so we have our radio config settings (from nodedb init) +#if !HAS_RADIO && defined(ARCH_PORTDUINO) + if (!rIf) { + rIf = new SimRadio; + if (!rIf->init()) { + LOG_WARN("Failed to find simulated radio\n"); + delete rIf; + rIf = NULL; + } else { + LOG_INFO("Using SIMULATED radio!\n"); + } + } +#endif + #if defined(RF95_IRQ) if (!rIf) { rIf = new RF95Interface(RF95_NSS, RF95_IRQ, RF95_RESET, SPI); @@ -459,19 +472,6 @@ void setup() } #endif -#ifdef ARCH_PORTDUINO - if (!rIf) { - rIf = new SimRadio; - if (!rIf->init()) { - LOG_WARN("Failed to find simulated radio\n"); - delete rIf; - rIf = NULL; - } else { - LOG_INFO("Using SIMULATED radio!\n"); - } - } -#endif - // check if the radio chip matches the selected region if ((config.lora.region == meshtastic_Config_LoRaConfig_RegionCode_LORA_24) && (!rIf->wideLora())) { diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index 85a6390c9e..a4d4114b63 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -132,7 +132,7 @@ void MeshService::reloadOwner(bool shouldSave) */ void MeshService::handleToRadio(meshtastic_MeshPacket &p) { -#ifdef ARCH_PORTDUINO +#if defined(ARCH_PORTDUINO) && !HAS_RADIO // Simulates device is receiving a packet via the LoRa chip if (p.decoded.portnum == meshtastic_PortNum_SIMULATOR_APP) { // Simulator packet (=Compressed packet) is encapsulated in a MeshPacket, so need to unwrap first diff --git a/src/mesh/MeshService.h b/src/mesh/MeshService.h index b8abac80d3..4314ea3621 100644 --- a/src/mesh/MeshService.h +++ b/src/mesh/MeshService.h @@ -10,7 +10,7 @@ #include "MeshTypes.h" #include "Observer.h" #include "PointerQueue.h" -#ifdef ARCH_PORTDUINO +#if defined(ARCH_PORTDUINO) && !HAS_RADIO #include "../platform/portduino/SimRadio.h" #endif diff --git a/src/platform/portduino/architecture.h b/src/platform/portduino/architecture.h index 54df672d80..c548957035 100644 --- a/src/platform/portduino/architecture.h +++ b/src/platform/portduino/architecture.h @@ -2,10 +2,6 @@ #define ARCH_PORTDUINO -// -// defaults for NRF52 architecture -// - // // set HW_VENDOR // From 680550b76ce5f9b329222dafbdfcf68dd597c793 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 10:08:25 +0100 Subject: [PATCH 2/7] Add HAS_SENSOR flag To separate DeviceTelemetry and EnvironmentTelemetry --- arch/portduino/portduino.ini | 4 +++- src/modules/Modules.cpp | 6 +++++- src/modules/Telemetry/EnvironmentTelemetry.cpp | 2 -- src/platform/esp32/architecture.h | 3 +++ src/platform/nrf52/architecture.h | 3 +++ 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/portduino/portduino.ini b/arch/portduino/portduino.ini index b6b17d391a..100f3538dd 100644 --- a/arch/portduino/portduino.ini +++ b/arch/portduino/portduino.ini @@ -12,7 +12,9 @@ build_src_filter = - - - - - + - + - + - +<../variants/portduino> lib_deps = ${env.lib_deps} diff --git a/src/modules/Modules.cpp b/src/modules/Modules.cpp index bc874a9a3d..4119cdc773 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -14,9 +14,11 @@ #include "modules/TraceRouteModule.h" #include "modules/WaypointModule.h" #if HAS_TELEMETRY -#include "modules/Telemetry/AirQualityTelemetry.h" #include "modules/Telemetry/DeviceTelemetry.h" +#endif +#if HAS_SENSOR #include "modules/Telemetry/EnvironmentTelemetry.h" +#include "modules/Telemetry/AirQualityTelemetry.h" #endif #ifdef ARCH_ESP32 #include "modules/esp32/AudioModule.h" @@ -63,6 +65,8 @@ void setupModules() #endif #if HAS_TELEMETRY new DeviceTelemetryModule(); +#endif +#if HAS_SENSOR new EnvironmentTelemetryModule(); if (nodeTelemetrySensorsMap[meshtastic_TelemetrySensorType_PMSA003I] > 0) { new AirQualityTelemetryModule(); diff --git a/src/modules/Telemetry/EnvironmentTelemetry.cpp b/src/modules/Telemetry/EnvironmentTelemetry.cpp index d9b129e70f..bd7ed539aa 100644 --- a/src/modules/Telemetry/EnvironmentTelemetry.cpp +++ b/src/modules/Telemetry/EnvironmentTelemetry.cpp @@ -52,7 +52,6 @@ SHT31Sensor sht31Sensor; int32_t EnvironmentTelemetryModule::runOnce() { -#ifndef ARCH_PORTDUINO int32_t result = INT32_MAX; /* Uncomment the preferences below if you want to use the module @@ -115,7 +114,6 @@ int32_t EnvironmentTelemetryModule::runOnce() } } return sendToPhoneIntervalMs; -#endif } bool EnvironmentTelemetryModule::wantUIFrame() diff --git a/src/platform/esp32/architecture.h b/src/platform/esp32/architecture.h index 09b754d0f8..90c016daeb 100644 --- a/src/platform/esp32/architecture.h +++ b/src/platform/esp32/architecture.h @@ -27,6 +27,9 @@ #ifndef HAS_TELEMETRY #define HAS_TELEMETRY 1 #endif +#ifndef HAS_SENSOR +#define HAS_SENSOR 1 +#endif #ifndef HAS_RADIO #define HAS_RADIO 1 #endif diff --git a/src/platform/nrf52/architecture.h b/src/platform/nrf52/architecture.h index 383a0da4ea..f21635560b 100644 --- a/src/platform/nrf52/architecture.h +++ b/src/platform/nrf52/architecture.h @@ -23,6 +23,9 @@ #ifndef HAS_TELEMETRY #define HAS_TELEMETRY 1 #endif +#ifndef HAS_SENSOR +#define HAS_SENSOR 1 +#endif #ifndef HAS_RADIO #define HAS_RADIO 1 #endif From 03f584a5ab9cfceaf9826b7ba851ac24cb34d4ad Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 10:09:48 +0100 Subject: [PATCH 3/7] Add HAS_TELEMETRY to portduino --- src/platform/portduino/architecture.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/platform/portduino/architecture.h b/src/platform/portduino/architecture.h index c548957035..9408ad19c7 100644 --- a/src/platform/portduino/architecture.h +++ b/src/platform/portduino/architecture.h @@ -8,5 +8,12 @@ #define HW_VENDOR meshtastic_HardwareModel_PORTDUINO -#define HAS_RTC 1 +#ifndef HAS_WIFI #define HAS_WIFI 1 +#endif +#ifndef HAS_RTC +#define HAS_RTC 1 +#endif +#ifndef HAS_TELEMETRY +#define HAS_TELEMETRY 1 +#endif From 97c1cf628aaed69400b30e9f99572a300bf3d163 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 10:34:08 +0100 Subject: [PATCH 4/7] SimRadio in separate thread To use notifyLater when transmitting, fixes packetPool issues --- src/platform/portduino/SimRadio.cpp | 23 +++++++++-------------- src/platform/portduino/SimRadio.h | 3 ++- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/platform/portduino/SimRadio.cpp b/src/platform/portduino/SimRadio.cpp index fa42126694..f71113ab44 100644 --- a/src/platform/portduino/SimRadio.cpp +++ b/src/platform/portduino/SimRadio.cpp @@ -2,7 +2,7 @@ #include "MeshService.h" #include "Router.h" -SimRadio::SimRadio() +SimRadio::SimRadio() : NotifiedWorkerThread("SimRadio") { instance = this; } @@ -53,10 +53,7 @@ void SimRadio::startTransmitTimer(bool withDelay) if (!txQueue.empty()) { uint32_t delayMsec = !withDelay ? 1 : getTxDelayMsec(); // LOG_DEBUG("xmit timer %d\n", delay); - delay(delayMsec); - onNotify(TRANSMIT_DELAY_COMPLETED); - } else { - LOG_DEBUG("TX QUEUE EMPTY!\n"); + notifyLater(delayMsec, TRANSMIT_DELAY_COMPLETED, false); } } @@ -66,8 +63,7 @@ void SimRadio::startTransmitTimerSNR(float snr) if (!txQueue.empty()) { uint32_t delayMsec = getTxDelayMsecWeighted(snr); // LOG_DEBUG("xmit timer %d\n", delay); - delay(delayMsec); - onNotify(TRANSMIT_DELAY_COMPLETED); + notifyLater(delayMsec, TRANSMIT_DELAY_COMPLETED, false); } } @@ -142,11 +138,12 @@ void SimRadio::onNotify(uint32_t notification) switch (notification) { case ISR_TX: handleTransmitInterrupt(); - LOG_DEBUG("tx complete - starting timer\n"); + // LOG_DEBUG("tx complete - starting timer\n"); startTransmitTimer(); break; case ISR_RX: - LOG_DEBUG("rx complete - starting timer\n"); + // LOG_DEBUG("rx complete - starting timer\n"); + startTransmitTimer(); break; case TRANSMIT_DELAY_COMPLETED: LOG_DEBUG("delay done\n"); @@ -170,8 +167,7 @@ void SimRadio::onNotify(uint32_t notification) uint32_t xmitMsec = getPacketTime(txp); airTime->logAirtime(TX_LOG, xmitMsec); - delay(xmitMsec); // Model the time it is busy sending - completeSending(); + notifyLater(xmitMsec, ISR_TX, false); // Model the time it is busy sending } } } else { @@ -242,8 +238,7 @@ void SimRadio::handleReceiveInterrupt(meshtastic_MeshPacket *p) xmitMsec = getPacketTime(length); // LOG_DEBUG("Payload size %d vs length (includes header) %d\n", p->decoded.payload.size, length); - meshtastic_MeshPacket *mp = packetPool.allocCopy(*p); // keep a copy in packtPool - mp->which_payload_variant = meshtastic_MeshPacket_decoded_tag; // Mark that the payload is already decoded + meshtastic_MeshPacket *mp = packetPool.allocCopy(*p); // keep a copy in packetPool printPacket("Lora RX", mp); @@ -268,4 +263,4 @@ int16_t SimRadio::readData(uint8_t *data, size_t len) } return state; -} \ No newline at end of file +} diff --git a/src/platform/portduino/SimRadio.h b/src/platform/portduino/SimRadio.h index b78beb70a9..1edb4963b4 100644 --- a/src/platform/portduino/SimRadio.h +++ b/src/platform/portduino/SimRadio.h @@ -3,10 +3,11 @@ #include "MeshPacketQueue.h" #include "RadioInterface.h" #include "api/WiFiServerAPI.h" +#include "concurrency/NotifiedWorkerThread.h" #include -class SimRadio : public RadioInterface +class SimRadio : public RadioInterface, protected concurrency::NotifiedWorkerThread { enum PendingISR { ISR_NONE = 0, ISR_RX, ISR_TX, TRANSMIT_DELAY_COMPLETED }; From 7063acdda6fea4a9f5874082ed88d559d00a8c21 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 11:32:10 +0100 Subject: [PATCH 5/7] Ignore syslog on portduino Breaks when running since mesh/http is not compiled --- src/RedirectablePrint.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index 04dbcdd491..39ea76c007 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -103,7 +103,7 @@ size_t RedirectablePrint::log(const char *logLevel, const char *format, ...) } r += vprintf(format, arg); -#if HAS_WIFI || HAS_ETHERNET +#if (HAS_WIFI || HAS_ETHERNET) && !defined(ARCH_PORTDUINO) // if syslog is in use, collect the log messages and send them to syslog if (syslog.isEnabled()) { int ll = 0; From abf3a5840b179c9f741a3cbe014f34dcb47ca8fe Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 11:46:54 +0100 Subject: [PATCH 6/7] trunk fmt --- 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 4119cdc773..3247d02c14 100644 --- a/src/modules/Modules.cpp +++ b/src/modules/Modules.cpp @@ -17,8 +17,8 @@ #include "modules/Telemetry/DeviceTelemetry.h" #endif #if HAS_SENSOR -#include "modules/Telemetry/EnvironmentTelemetry.h" #include "modules/Telemetry/AirQualityTelemetry.h" +#include "modules/Telemetry/EnvironmentTelemetry.h" #endif #ifdef ARCH_ESP32 #include "modules/esp32/AudioModule.h" From 406187084155f715cc591d58e96cc663aab45d07 Mon Sep 17 00:00:00 2001 From: GUVWAF Date: Sat, 11 Feb 2023 12:04:53 +0100 Subject: [PATCH 7/7] Don't need a Portduino guard clause here as it will not be compiled --- src/modules/Telemetry/AirQualityTelemetry.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/modules/Telemetry/AirQualityTelemetry.cpp b/src/modules/Telemetry/AirQualityTelemetry.cpp index 2b744c4891..966e2d0ff7 100644 --- a/src/modules/Telemetry/AirQualityTelemetry.cpp +++ b/src/modules/Telemetry/AirQualityTelemetry.cpp @@ -10,7 +10,6 @@ int32_t AirQualityTelemetryModule::runOnce() { -#ifndef ARCH_PORTDUINO int32_t result = INT32_MAX; /* Uncomment the preferences below if you want to use the module @@ -55,7 +54,6 @@ int32_t AirQualityTelemetryModule::runOnce() } } return sendToPhoneIntervalMs; -#endif } bool AirQualityTelemetryModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_Telemetry *t)