Files
firmware/src/modules/esp32/StoreForwardModule.h

108 lines
3.6 KiB
C
Raw Normal View History

#pragma once
#include "ProtobufModule.h"
#include "concurrency/OSThread.h"
2023-01-18 08:56:47 -06:00
#include "mesh/generated/meshtastic/storeforward.pb.h"
2021-11-28 19:34:35 -08:00
#include "configuration.h"
#include <Arduino.h>
#include <functional>
#include <unordered_map>
2021-02-21 20:15:31 -08:00
struct PacketHistoryStruct {
uint32_t time;
uint32_t to;
2021-11-20 21:21:11 -08:00
uint32_t from;
uint8_t channel;
uint8_t payload[meshtastic_Constants_DATA_PAYLOAD_LEN];
2021-11-20 21:21:11 -08:00
pb_size_t payload_size;
2021-02-21 20:15:31 -08:00
};
class StoreForwardModule : private concurrency::OSThread, public ProtobufModule<meshtastic_StoreAndForward>
{
bool busy = 0;
uint32_t busyTo = 0;
char routerMessage[meshtastic_Constants_DATA_PAYLOAD_LEN] = {0};
PacketHistoryStruct *packetHistory = 0;
uint32_t packetHistoryTotalCount = 0;
uint32_t last_time = 0;
uint32_t requestCount = 0;
2021-11-21 14:43:47 -08:00
uint32_t packetTimeMax = 5000; // Interval between sending history packets as a server.
bool is_client = false;
bool is_server = false;
2021-11-21 14:43:47 -08:00
// Unordered_map stores the last request for each nodeNum (`to` field)
std::unordered_map<NodeNum, uint32_t> lastRequest;
public:
2022-02-27 02:21:02 -08:00
StoreForwardModule();
2022-12-28 15:31:04 +01:00
unsigned long lastHeartbeat = 0;
uint32_t heartbeatInterval = 900;
2022-12-28 15:31:04 +01:00
2021-02-16 17:42:46 -08:00
/**
Update our local reference of when we last saw that node.
@return 0 if we have never seen that node before otherwise return the last time we saw the node.
*/
void historyAdd(const meshtastic_MeshPacket &mp);
2022-12-28 15:31:04 +01:00
void statsSend(uint32_t to);
void historySend(uint32_t secAgo, uint32_t to);
uint32_t getNumAvailablePackets(NodeNum dest, uint32_t last_time);
2021-03-17 21:03:11 -07:00
/**
* Send our payload into the mesh
*/
bool sendPayload(NodeNum dest = NODENUM_BROADCAST, uint32_t packetHistory_index = 0);
meshtastic_MeshPacket *preparePayload(NodeNum dest, uint32_t packetHistory_index, bool local = false);
void sendMessage(NodeNum dest, const meshtastic_StoreAndForward &payload);
void sendMessage(NodeNum dest, meshtastic_StoreAndForward_RequestResponse rr);
void sendErrorTextMessage(NodeNum dest, bool want_response);
meshtastic_MeshPacket *getForPhone();
// Returns true if we are configured as server AND we could allocate PSRAM.
bool isServer() { return is_server; }
2022-12-28 15:31:04 +01:00
2021-11-28 19:34:35 -08:00
/*
2022-12-28 15:31:04 +01:00
-Override the wantPacket method.
*/
virtual bool wantPacket(const meshtastic_MeshPacket *p) override
2022-12-28 15:31:04 +01:00
{
switch (p->decoded.portnum) {
case meshtastic_PortNum_TEXT_MESSAGE_APP:
case meshtastic_PortNum_STORE_FORWARD_APP:
return true;
default:
return false;
2022-12-28 15:31:04 +01:00
}
}
2021-03-17 21:03:11 -07:00
2021-02-16 17:42:46 -08:00
private:
2021-11-21 14:43:47 -08:00
void populatePSRAM();
2021-02-16 17:42:46 -08:00
// S&F Defaults
uint32_t historyReturnMax = 25; // Return maximum of 25 records by default.
uint32_t historyReturnWindow = 240; // Return history of last 4 hours by default.
uint32_t records = 0; // Calculated
bool heartbeat = false; // No heartbeat.
2022-12-28 15:31:04 +01:00
// stats
uint32_t requests = 0; // Number of times any client sent a request to the S&F.
uint32_t requests_history = 0; // Number of times the history was requested.
2022-12-28 15:31:04 +01:00
uint32_t retry_delay = 0; // If server is busy, retry after this delay (in ms).
2022-12-28 15:31:04 +01:00
protected:
2022-01-24 17:24:40 +00:00
virtual int32_t runOnce() override;
2021-03-17 21:03:11 -07:00
/** Called to handle a particular incoming message
2021-11-18 09:36:39 -08:00
@return ProcessMessage::STOP if you've guaranteed you've handled this message and no other handlers should be considered for
it
2021-03-17 21:03:11 -07:00
*/
virtual ProcessMessage handleReceived(const meshtastic_MeshPacket &mp) override;
virtual bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_StoreAndForward *p);
};
extern StoreForwardModule *storeForwardModule;