From 925829dc58565c340e5a1c5e3701c8351167af5e Mon Sep 17 00:00:00 2001 From: Jm Date: Fri, 1 Jan 2021 12:31:46 -0800 Subject: [PATCH] Partial work to migrate to OSThread model --- src/airtime.cpp | 49 +++++++++++++++++++++++----------- src/airtime.h | 22 ++++++++++++--- src/main.cpp | 2 -- src/mesh/RadioLibInterface.cpp | 6 ++--- src/meshwifi/meshhttp.cpp | 21 +++++++++++---- src/meshwifi/meshhttp.h | 21 +++++++++++++-- 6 files changed, 91 insertions(+), 30 deletions(-) diff --git a/src/airtime.cpp b/src/airtime.cpp index ec0bf8c8..9632181b 100644 --- a/src/airtime.cpp +++ b/src/airtime.cpp @@ -3,6 +3,8 @@ #define periodsToLog 48 +AirTime airTime; + // A reminder that there are 3600 seconds in an hour so I don't have // to keep googling it. // This can be changed to a smaller number to speed up testing. @@ -11,6 +13,8 @@ uint32_t secondsPerPeriod = 3600; uint32_t lastMillis = 0; uint32_t secSinceBoot = 0; +// AirTime at; + // Don't read out of this directly. Use the helper functions. struct airtimeStruct { uint16_t periodTX[periodsToLog]; @@ -19,14 +23,18 @@ struct airtimeStruct { uint8_t lastPeriodIndex; } airtimes; -void logAirtime(reportTypes reportType, uint32_t airtime_ms) +void AirTime::logAirtime(reportTypes reportType, uint32_t airtime_ms) { + DEBUG_MSG("Packet - logAirtime()\n"); if (reportType == TX_LOG) { + DEBUG_MSG("Packet transmitted = %u\n", (uint32_t)round(airtime_ms / 1000)); airtimes.periodTX[0] = airtimes.periodTX[0] + round(airtime_ms / 1000); } else if (reportType == RX_LOG) { + DEBUG_MSG("Packet received = %u\n", (uint32_t)round(airtime_ms / 1000)); airtimes.periodRX[0] = airtimes.periodRX[0] + round(airtime_ms / 1000); } else if (reportType == RX_ALL_LOG) { + DEBUG_MSG("Packet received (noise?) = %u\n", (uint32_t)round(airtime_ms / 1000)); airtimes.periodRX_ALL[0] = airtimes.periodRX_ALL[0] + round(airtime_ms / 1000); } else { // Unknown report type @@ -38,23 +46,22 @@ uint8_t currentPeriodIndex() return ((getSecondsSinceBoot() / secondsPerPeriod) % periodsToLog); } -void airtimeCalculator() +void airtimeRotatePeriod() { - if (millis() - lastMillis > 1000) { - lastMillis = millis(); - secSinceBoot++; - if (airtimes.lastPeriodIndex != currentPeriodIndex()) { - for (int i = periodsToLog - 2; i >= 0; --i) { - airtimes.periodTX[i + 1] = airtimes.periodTX[i]; - airtimes.periodRX[i + 1] = airtimes.periodRX[i]; - airtimes.periodRX_ALL[i + 1] = airtimes.periodRX_ALL[i]; - } - airtimes.periodTX[0] = 0; - airtimes.periodRX[0] = 0; - airtimes.periodRX_ALL[0] = 0; - airtimes.lastPeriodIndex = currentPeriodIndex(); + if (airtimes.lastPeriodIndex != currentPeriodIndex()) { + DEBUG_MSG("Rotating airtimes to a new period = %u\n", currentPeriodIndex()); + + for (int i = periodsToLog - 2; i >= 0; --i) { + airtimes.periodTX[i + 1] = airtimes.periodTX[i]; + airtimes.periodRX[i + 1] = airtimes.periodRX[i]; + airtimes.periodRX_ALL[i + 1] = airtimes.periodRX_ALL[i]; } + airtimes.periodTX[0] = 0; + airtimes.periodRX[0] = 0; + airtimes.periodRX_ALL[0] = 0; + + airtimes.lastPeriodIndex = currentPeriodIndex(); } } @@ -86,3 +93,15 @@ uint32_t getSecondsSinceBoot() { return secSinceBoot; } + +AirTime::AirTime() : concurrency::OSThread("AirTime") {} + +int32_t AirTime::runOnce() +{ + DEBUG_MSG("AirTime::runOnce()\n"); + + airtimeRotatePeriod(); + secSinceBoot++; + + return 1000; +} \ No newline at end of file diff --git a/src/airtime.h b/src/airtime.h index 4cfbb82c..d79e8829 100644 --- a/src/airtime.h +++ b/src/airtime.h @@ -1,5 +1,6 @@ #pragma once +#include "concurrency/OSThread.h" #include "configuration.h" #include #include @@ -18,7 +19,7 @@ TX_LOG + RX_LOG = Total air time for a perticular meshtastic channel. TX_LOG + RX_LOG = Total air time for a perticular meshtastic channel, including - other lora radios. + other lora radios. RX_ALL_LOG - RX_LOG = Other lora radios on our frequency channel. */ @@ -26,7 +27,7 @@ enum reportTypes { TX_LOG, RX_LOG, RX_ALL_LOG }; void logAirtime(reportTypes reportType, uint32_t airtime_ms); -void airtimeCalculator(); +void airtimeRotatePeriod(); uint8_t currentPeriodIndex(); uint8_t getPeriodsToLog(); @@ -35,4 +36,19 @@ uint32_t getSecondsSinceBoot(); uint16_t *airtimeReport(reportTypes reportType); -uint32_t getSecondsPerPeriod(); \ No newline at end of file +uint32_t getSecondsPerPeriod(); + +class AirTime : private concurrency::OSThread +{ + + public: + AirTime(); + + void logAirtime(reportTypes reportType, uint32_t airtime_ms); + + protected: + + virtual int32_t runOnce(); +}; + +extern AirTime airTime; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 029f753e..bf62e585 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -584,6 +584,4 @@ void loop() mainDelay.delay(delayMsec); // if (didWake) DEBUG_MSG("wake!\n"); - // Handles cleanup for the airtime calculator. - airtimeCalculator(); } diff --git a/src/mesh/RadioLibInterface.cpp b/src/mesh/RadioLibInterface.cpp index f4ceeded..b4c93ac9 100644 --- a/src/mesh/RadioLibInterface.cpp +++ b/src/mesh/RadioLibInterface.cpp @@ -97,7 +97,7 @@ ErrorCode RadioLibInterface::send(MeshPacket *p) // Count the packet toward our TX airtime utilization. // We only count it if it can be added to the TX queue. - logAirtime(TX_LOG, xmitMsec); + airTime.logAirtime(TX_LOG, xmitMsec); // We want all sending/receiving to be done by our daemon thread, We use a delay here because this packet might have been sent // in response to a packet we just received. So we want to make sure the other side has had a chance to reconfigure its radio @@ -216,7 +216,7 @@ void RadioLibInterface::handleReceiveInterrupt() size_t length = iface->getPacketLength(); xmitMsec = getPacketTime(length); - logAirtime(RX_ALL_LOG, xmitMsec); + airTime.logAirtime(RX_ALL_LOG, xmitMsec); int state = iface->readData(radiobuf, length); if (state != ERR_NONE) { @@ -258,7 +258,7 @@ void RadioLibInterface::handleReceiveInterrupt() printPacket("Lora RX", mp); xmitMsec = getPacketTime(mp); - logAirtime(RX_LOG, xmitMsec); + airTime.logAirtime(RX_LOG, xmitMsec); deliverToReceiver(mp); } diff --git a/src/meshwifi/meshhttp.cpp b/src/meshwifi/meshhttp.cpp index 319a70d7..46fb1809 100644 --- a/src/meshwifi/meshhttp.cpp +++ b/src/meshwifi/meshhttp.cpp @@ -808,10 +808,10 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) return; } - //if (readLength) { - file.write(buf, readLength); - fileLength += readLength; - DEBUG_MSG("File Length %i\n", fileLength); + // if (readLength) { + file.write(buf, readLength); + fileLength += readLength; + DEBUG_MSG("File Length %i\n", fileLength); //} } // enableLoopWDT(); @@ -988,7 +988,8 @@ void handleRoot(HTTPRequest *req, HTTPResponse *res) res->printf("

\n"); res->printf("

You have gotten this error because the filesystem for the web server has not been loaded.

\n"); res->printf("

Please review the 'Common Problems' section of the web interface documentation.

\n"); + "href=https://github.com/meshtastic/Meshtastic-device/wiki/" + "How-to-use-the-Meshtastic-Web-Interface-over-WiFi>web interface documentation.

\n"); return; } @@ -1212,3 +1213,13 @@ void replaceAll(std::string &str, const std::string &from, const std::string &to start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' } } + +HttpServer::HttpServer() : concurrency::OSThread("HttpServer") { + DEBUG_MSG("22**********************************\n"); +} + +int32_t HttpServer::runOnce() +{ + DEBUG_MSG("11**********************************\n"); + return 200; // Poll our GPIOs every 200ms (FIXME, make adjustable via protobuf arg) +} diff --git a/src/meshwifi/meshhttp.h b/src/meshwifi/meshhttp.h index b7a84475..3bcf2511 100644 --- a/src/meshwifi/meshhttp.h +++ b/src/meshwifi/meshhttp.h @@ -1,6 +1,7 @@ #pragma once #include "PhoneAPI.h" +#include "concurrency/OSThread.h" #include #include @@ -22,7 +23,7 @@ void handleRoot(); void handleScriptsScriptJS(); void handleJSONChatHistoryDummy(); -void replaceAll(std::string& str, const std::string& from, const std::string& to); +void replaceAll(std::string &str, const std::string &from, const std::string &to); class HttpAPI : public PhoneAPI { @@ -35,4 +36,20 @@ class HttpAPI : public PhoneAPI protected: // Nothing here yet -}; \ No newline at end of file +}; + +/** + * A plugin that provides easy low-level remote access to device hardware. + */ +class HttpServer : public concurrency::OSThread +{ + public: + // Nothing here + // RemoteHardwarePlugin(); + HttpServer(); + + protected: + virtual int32_t runOnce(); +}; + +// extern HttpServer httpServer;