manual master merge

# Conflicts:
#	src/Power.cpp
This commit is contained in:
Thomas Göttgens 2022-12-06 16:56:38 +01:00
parent cea8393a7f
commit aec091e7aa
2 changed files with 146 additions and 141 deletions

View File

@ -16,16 +16,10 @@ StoreForwardModule *storeForwardModule;
int32_t StoreForwardModule::runOnce() int32_t StoreForwardModule::runOnce()
{ {
#ifdef ARCH_ESP32 #ifdef ARCH_ESP32
if (moduleConfig.store_forward.enabled && is_server) {
if (moduleConfig.store_forward.enabled) {
if (config.device.role == Config_DeviceConfig_Role_ROUTER) {
// Send out the message queue. // Send out the message queue.
if (this->busy) { if (this->busy) {
// Only send packets if the channel is less than 25% utilized. // Only send packets if the channel is less than 25% utilized.
if (airTime->channelUtilizationPercent() < polite_channel_util_percent) { if (airTime->channelUtilizationPercent() < polite_channel_util_percent) {
@ -44,24 +38,28 @@ int32_t StoreForwardModule::runOnce()
} }
} else { } else {
DEBUG_MSG("Channel utilization is too high. Skipping this opportunity to send and will retry later.\n"); DEBUG_MSG("Channel utilization is too high. Retrying later.\n");
} }
} DEBUG_MSG("SF bitrate = %f bytes / sec\n", myNodeInfo.bitrate);
DEBUG_MSG("SF myNodeInfo.bitrate = %f bytes / sec\n", myNodeInfo.bitrate);
} else if (millis() - lastHeartbeat > 300000) {
lastHeartbeat = millis();
DEBUG_MSG("Sending heartbeat\n");
StoreAndForward sf;
sf.rr = StoreAndForward_RequestResponse_ROUTER_HEARTBEAT;
sf.has_heartbeat = true;
sf.heartbeat.period = 300;
sf.heartbeat.secondary = 0; // TODO we always have one primary router for now
MeshPacket *p = allocDataProtobuf(sf);
p->to = NODENUM_BROADCAST;
p->decoded.want_response = false;
p->priority = MeshPacket_Priority_MIN;
service.sendToMesh(p);
}
return (this->packetTimeMax); return (this->packetTimeMax);
} else {
DEBUG_MSG("Store & Forward Module - Disabled (is_router = false)\n");
return (INT32_MAX);
} }
} else {
DEBUG_MSG("Store & Forward Module - Disabled\n");
return (INT32_MAX);
}
#endif #endif
return (INT32_MAX); return (INT32_MAX);
} }
@ -76,12 +74,7 @@ void StoreForwardModule::populatePSRAM()
https://learn.upesy.com/en/programmation/psram.html#psram-tab https://learn.upesy.com/en/programmation/psram.html#psram-tab
*/ */
DEBUG_MSG("Before PSRAM initilization:\n"); DEBUG_MSG("Before PSRAM initilization: heap %d/%d PSRAM %d/%d\n", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getFreePsram(), ESP.getPsramSize());
DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize());
DEBUG_MSG(" Free heap: %d\n", ESP.getFreeHeap());
DEBUG_MSG(" Total PSRAM: %d\n", ESP.getPsramSize());
DEBUG_MSG(" Free PSRAM: %d\n", ESP.getFreePsram());
this->packetHistoryTXQueue = this->packetHistoryTXQueue =
static_cast<PacketHistoryStruct *>(ps_calloc(this->historyReturnMax, sizeof(PacketHistoryStruct))); static_cast<PacketHistoryStruct *>(ps_calloc(this->historyReturnMax, sizeof(PacketHistoryStruct)));
@ -93,19 +86,12 @@ void StoreForwardModule::populatePSRAM()
this->packetHistory = static_cast<PacketHistoryStruct *>(ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct))); this->packetHistory = static_cast<PacketHistoryStruct *>(ps_calloc(numberOfPackets, sizeof(PacketHistoryStruct)));
DEBUG_MSG("After PSRAM initilization:\n"); DEBUG_MSG("After PSRAM initilization: heap %d/%d PSRAM %d/%d\n", ESP.getFreeHeap(), ESP.getHeapSize(), ESP.getFreePsram(), ESP.getPsramSize());
DEBUG_MSG("numberOfPackets for packetHistory - %u\n", numberOfPackets);
DEBUG_MSG(" Total heap: %d\n", ESP.getHeapSize());
DEBUG_MSG(" Free heap: %d\n", ESP.getFreeHeap());
DEBUG_MSG(" Total PSRAM: %d\n", ESP.getPsramSize());
DEBUG_MSG(" Free PSRAM: %d\n", ESP.getFreePsram());
DEBUG_MSG("Store and Forward Stats:\n");
DEBUG_MSG(" numberOfPackets for packetHistory - %u\n", numberOfPackets);
} }
void StoreForwardModule::historyReport() void StoreForwardModule::historyReport()
{ {
DEBUG_MSG("Iterating through the message history...\n");
DEBUG_MSG("Message history contains %u records\n", this->packetHistoryCurrent); DEBUG_MSG("Message history contains %u records\n", this->packetHistoryCurrent);
} }
@ -246,8 +232,8 @@ ProcessMessage StoreForwardModule::handleReceived(const MeshPacket &mp)
DEBUG_MSG("--- S&F Received something\n"); DEBUG_MSG("--- S&F Received something\n");
// The router node should not be sending messages as a client. // The router node should not be sending messages as a client. Unless he is a ROUTER_CLIENT
if (getFrom(&mp) != nodeDB.getNodeNum()) { if ((getFrom(&mp) != nodeDB.getNodeNum()) || (config.device.role == Config_DeviceConfig_Role_ROUTER_CLIENT)) {
if (mp.decoded.portnum == PortNum_TEXT_MESSAGE_APP) { if (mp.decoded.portnum == PortNum_TEXT_MESSAGE_APP) {
DEBUG_MSG("Packet came from - PortNum_TEXT_MESSAGE_APP\n"); DEBUG_MSG("Packet came from - PortNum_TEXT_MESSAGE_APP\n");
@ -264,6 +250,7 @@ ProcessMessage StoreForwardModule::handleReceived(const MeshPacket &mp)
} else { } else {
storeForwardModule->historySend(1000 * 60, getFrom(&mp)); storeForwardModule->historySend(1000 * 60, getFrom(&mp));
} }
} else if ((p.payload.bytes[0] == 'S') && (p.payload.bytes[1] == 'F') && (p.payload.bytes[2] == 'm') && } else if ((p.payload.bytes[0] == 'S') && (p.payload.bytes[1] == 'F') && (p.payload.bytes[2] == 'm') &&
(p.payload.bytes[3] == 0x00)) { (p.payload.bytes[3] == 0x00)) {
strlcpy(this->routerMessage, strlcpy(this->routerMessage,
@ -278,14 +265,12 @@ ProcessMessage StoreForwardModule::handleReceived(const MeshPacket &mp)
} }
} else if (mp.decoded.portnum == PortNum_STORE_FORWARD_APP) { } else if (mp.decoded.portnum == PortNum_STORE_FORWARD_APP) {
DEBUG_MSG("Packet came from an PortNum_STORE_FORWARD_APP port %u\n", mp.decoded.portnum);
} else { } else {
DEBUG_MSG("Packet came from an unknown port %u\n", mp.decoded.portnum); DEBUG_MSG("Packet came from an unknown port %u\n", mp.decoded.portnum);
} }
} }
} else {
DEBUG_MSG("Store & Forward Module - Disabled\n");
} }
#endif #endif
@ -293,33 +278,31 @@ ProcessMessage StoreForwardModule::handleReceived(const MeshPacket &mp)
return ProcessMessage::CONTINUE; // Let others look at this message also if they want return ProcessMessage::CONTINUE; // Let others look at this message also if they want
} }
ProcessMessage StoreForwardModule::handleReceivedProtobuf(const MeshPacket &mp, StoreAndForward *p) bool StoreForwardModule::handleReceivedProtobuf(const MeshPacket &mp, StoreAndForward *p)
{ {
if (!moduleConfig.store_forward.enabled) { if (!moduleConfig.store_forward.enabled) {
// If this module is not enabled in any capacity, don't handle the packet, and allow other modules to consume // If this module is not enabled in any capacity, don't handle the packet, and allow other modules to consume
return ProcessMessage::CONTINUE; return false;
} }
if (mp.decoded.portnum == PortNum_TEXT_MESSAGE_APP) { if (mp.decoded.portnum != PortNum_STORE_FORWARD_APP) {
DEBUG_MSG("Packet came from an PortNum_TEXT_MESSAGE_APP port %u\n", mp.decoded.portnum); DEBUG_MSG("Packet came from port %u\n", mp.decoded.portnum);
return ProcessMessage::CONTINUE; return false;
} else if (mp.decoded.portnum == PortNum_STORE_FORWARD_APP) {
DEBUG_MSG("Packet came from an PortNum_STORE_FORWARD_APP port %u\n", mp.decoded.portnum);
} else { } else {
DEBUG_MSG("Packet came from an UNKNOWN port %u\n", mp.decoded.portnum); DEBUG_MSG("Packet came from PortNum_STORE_FORWARD_APP port %u\n", mp.decoded.portnum);
return ProcessMessage::CONTINUE;
}
switch (p->rr) { switch (p->rr) {
case StoreAndForward_RequestResponse_CLIENT_ERROR: case StoreAndForward_RequestResponse_CLIENT_ERROR:
if(is_server) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_ERROR\n"); DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_ERROR\n");
}
break; break;
case StoreAndForward_RequestResponse_CLIENT_HISTORY: case StoreAndForward_RequestResponse_CLIENT_HISTORY:
if(is_server) {
DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_HISTORY\n"); DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_HISTORY\n");
// Send the last 60 minutes of messages. // Send the last 60 minutes of messages.
if (this->busy) { if (this->busy) {
strcpy(this->routerMessage, "** S&F - Busy. Try again shortly."); strcpy(this->routerMessage, "** S&F - Busy. Try again shortly.");
@ -327,58 +310,75 @@ ProcessMessage StoreForwardModule::handleReceivedProtobuf(const MeshPacket &mp,
} else { } else {
storeForwardModule->historySend(1000 * 60, getFrom(&mp)); storeForwardModule->historySend(1000 * 60, getFrom(&mp));
} }
}
break; break;
case StoreAndForward_RequestResponse_CLIENT_PING: case StoreAndForward_RequestResponse_CLIENT_PING:
if(is_server) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_PING\n"); DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_PING\n");
}
break; break;
case StoreAndForward_RequestResponse_CLIENT_PONG: case StoreAndForward_RequestResponse_CLIENT_PONG:
if(is_server) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_PONG\n"); DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_PONG\n");
}
break; break;
case StoreAndForward_RequestResponse_CLIENT_STATS: case StoreAndForward_RequestResponse_CLIENT_STATS:
if(is_server) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_STATS\n"); DEBUG_MSG("StoreAndForward_RequestResponse_CLIENT_STATS\n");
}
break; break;
case StoreAndForward_RequestResponse_ROUTER_BUSY: case StoreAndForward_RequestResponse_ROUTER_BUSY:
if(is_client) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_BUSY\n"); DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_BUSY\n");
}
break; break;
case StoreAndForward_RequestResponse_ROUTER_ERROR: case StoreAndForward_RequestResponse_ROUTER_ERROR:
if(is_client) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_ERROR\n"); DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_ERROR\n");
}
break; break;
case StoreAndForward_RequestResponse_ROUTER_HEARTBEAT: case StoreAndForward_RequestResponse_ROUTER_HEARTBEAT:
if(is_client) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_HEARTBEAT\n"); DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_HEARTBEAT\n");
}
break; break;
case StoreAndForward_RequestResponse_ROUTER_PING: case StoreAndForward_RequestResponse_ROUTER_PING:
if(is_client) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_PING\n"); DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_PING\n");
}
break; break;
case StoreAndForward_RequestResponse_ROUTER_PONG: case StoreAndForward_RequestResponse_ROUTER_PONG:
if(is_client) {
// Do nothing // Do nothing
DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_PONG\n"); DEBUG_MSG("StoreAndForward_RequestResponse_ROUTER_PONG\n");
}
break; break;
default: default:
assert(0); // unexpected state - FIXME, make an error code and reboot assert(0); // unexpected state - FIXME, make an error code and reboot
} }
}
return ProcessMessage::STOP; // There's no need for others to look at this message. return true; // There's no need for others to look at this message.
} }
StoreForwardModule::StoreForwardModule() StoreForwardModule::StoreForwardModule()
: SinglePortModule("StoreForwardModule", PortNum_TEXT_MESSAGE_APP), concurrency::OSThread("StoreForwardModule") : concurrency::OSThread("StoreForwardModule"), ProtobufModule("StoreForward", PortNum_STORE_FORWARD_APP, &StoreAndForward_msg)
{ {
#ifdef ARCH_ESP32 #ifdef ARCH_ESP32
@ -397,9 +397,9 @@ StoreForwardModule::StoreForwardModule()
if (moduleConfig.store_forward.enabled) { if (moduleConfig.store_forward.enabled) {
// Router // Router
if (config.device.role == Config_DeviceConfig_Role_ROUTER) { if ((config.device.role == Config_DeviceConfig_Role_ROUTER) || (config.device.role == Config_DeviceConfig_Role_ROUTER_CLIENT)) {
DEBUG_MSG("Initializing Store & Forward Module - Enabled as Router\n"); DEBUG_MSG("Initializing Store & Forward Module in Router mode\n");
if (ESP.getPsramSize()) { if (ESP.getPsramSize() > 0) {
if (ESP.getFreePsram() >= 1024 * 1024) { if (ESP.getFreePsram() >= 1024 * 1024) {
// Do the startup here // Do the startup here
@ -416,26 +416,27 @@ StoreForwardModule::StoreForwardModule()
if (moduleConfig.store_forward.records) if (moduleConfig.store_forward.records)
this->records = moduleConfig.store_forward.records; this->records = moduleConfig.store_forward.records;
// Maximum number of records to store in memory // send heartbeat advertising?
if (moduleConfig.store_forward.heartbeat) if (moduleConfig.store_forward.heartbeat)
this->heartbeat = moduleConfig.store_forward.heartbeat; this->heartbeat = moduleConfig.store_forward.heartbeat;
// Popupate PSRAM with our data structures. // Popupate PSRAM with our data structures.
this->populatePSRAM(); this->populatePSRAM();
is_server = true;
} else { } else {
DEBUG_MSG("Device has less than 1M of PSRAM free. Aborting startup.\n"); DEBUG_MSG("Device has less than 1M of PSRAM free.\n");
DEBUG_MSG("Store & Forward Module - Aborting Startup.\n"); DEBUG_MSG("Store & Forward Module - disabling server.\n");
} }
} else { } else {
DEBUG_MSG("Device doesn't have PSRAM.\n"); DEBUG_MSG("Device doesn't have PSRAM.\n");
DEBUG_MSG("Store & Forward Module - Aborting Startup.\n"); DEBUG_MSG("Store & Forward Module - disabling server.\n");
} }
// Client // Client
} else { }
DEBUG_MSG("Initializing Store & Forward Module - Enabled as Client\n"); if ((config.device.role == Config_DeviceConfig_Role_CLIENT) || (config.device.role == Config_DeviceConfig_Role_ROUTER_CLIENT)) {
is_client = true;
DEBUG_MSG("Initializing Store & Forward Module in Client mode\n");
} }
} }
#endif #endif

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "SinglePortModule.h" #include "ProtobufModule.h"
#include "concurrency/OSThread.h" #include "concurrency/OSThread.h"
#include "mesh/generated/storeforward.pb.h" #include "mesh/generated/storeforward.pb.h"
@ -18,9 +18,8 @@ struct PacketHistoryStruct {
pb_size_t payload_size; pb_size_t payload_size;
}; };
class StoreForwardModule : public SinglePortModule, private concurrency::OSThread class StoreForwardModule : private concurrency::OSThread, public ProtobufModule<StoreAndForward>
{ {
// bool firstTime = 1;
bool busy = 0; bool busy = 0;
uint32_t busyTo = 0; uint32_t busyTo = 0;
char routerMessage[Constants_DATA_PAYLOAD_LEN] = {0}; char routerMessage[Constants_DATA_PAYLOAD_LEN] = {0};
@ -34,7 +33,12 @@ class StoreForwardModule : public SinglePortModule, private concurrency::OSThrea
uint32_t packetHistoryTXQueue_size = 0; uint32_t packetHistoryTXQueue_size = 0;
uint32_t packetHistoryTXQueue_index = 0; uint32_t packetHistoryTXQueue_index = 0;
uint32_t packetTimeMax = 2000; uint32_t packetTimeMax = 5000;
unsigned long lastHeartbeat = 0;
bool is_client = false;
bool is_server = false;
public: public:
StoreForwardModule(); StoreForwardModule();
@ -78,7 +82,7 @@ class StoreForwardModule : public SinglePortModule, private concurrency::OSThrea
it it
*/ */
virtual ProcessMessage handleReceived(const MeshPacket &mp) override; virtual ProcessMessage handleReceived(const MeshPacket &mp) override;
virtual ProcessMessage handleReceivedProtobuf(const MeshPacket &mp, StoreAndForward *p); virtual bool handleReceivedProtobuf(const MeshPacket &mp, StoreAndForward *p);
}; };