2021-01-31 09:12:01 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
2022-12-06 16:56:38 +01:00
|
|
|
#include "ProtobufModule.h"
|
2021-01-31 09:12:01 -08:00
|
|
|
#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
|
|
|
|
2021-01-31 09:12:01 -08:00
|
|
|
#include "configuration.h"
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <functional>
|
2024-02-18 21:27:44 +01:00
|
|
|
#include <unordered_map>
|
2021-01-31 09:12:01 -08:00
|
|
|
|
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;
|
2021-12-21 19:28:47 -05:00
|
|
|
uint8_t channel;
|
2023-01-21 18:22:19 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
class StoreForwardModule : private concurrency::OSThread, public ProtobufModule<meshtastic_StoreAndForward>
|
2021-01-31 09:12:01 -08:00
|
|
|
{
|
2021-11-22 17:47:42 -08:00
|
|
|
bool busy = 0;
|
2022-01-24 19:58:07 +00:00
|
|
|
uint32_t busyTo = 0;
|
2023-01-21 18:22:19 +01:00
|
|
|
char routerMessage[meshtastic_Constants_DATA_PAYLOAD_LEN] = {0};
|
2021-01-31 09:12:01 -08:00
|
|
|
|
2022-01-24 19:58:07 +00:00
|
|
|
PacketHistoryStruct *packetHistory = 0;
|
2024-07-13 19:36:44 +02:00
|
|
|
uint32_t packetHistoryTotalCount = 0;
|
|
|
|
|
uint32_t last_time = 0;
|
|
|
|
|
uint32_t requestCount = 0;
|
2021-11-21 14:43:47 -08:00
|
|
|
|
2024-02-14 14:07:20 +01:00
|
|
|
uint32_t packetTimeMax = 5000; // Interval between sending history packets as a server.
|
2022-12-06 16:56:38 +01:00
|
|
|
|
|
|
|
|
bool is_client = false;
|
|
|
|
|
bool is_server = false;
|
2021-11-21 14:43:47 -08:00
|
|
|
|
2024-02-18 21:27:44 +01:00
|
|
|
// Unordered_map stores the last request for each nodeNum (`to` field)
|
|
|
|
|
std::unordered_map<NodeNum, uint32_t> lastRequest;
|
|
|
|
|
|
2021-01-31 09:12:01 -08:00
|
|
|
public:
|
2022-02-27 02:21:02 -08:00
|
|
|
StoreForwardModule();
|
2021-01-31 09:12:01 -08:00
|
|
|
|
2022-12-28 15:31:04 +01:00
|
|
|
unsigned long lastHeartbeat = 0;
|
2024-02-25 14:29:34 +01:00
|
|
|
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.
|
|
|
|
|
*/
|
2023-01-21 18:22:19 +01:00
|
|
|
void historyAdd(const meshtastic_MeshPacket &mp);
|
2022-12-28 15:31:04 +01:00
|
|
|
void statsSend(uint32_t to);
|
2024-07-13 19:36:44 +02:00
|
|
|
void historySend(uint32_t secAgo, uint32_t to);
|
|
|
|
|
uint32_t getNumAvailablePackets(NodeNum dest, uint32_t last_time);
|
2021-11-22 17:47:42 -08:00
|
|
|
|
2021-03-17 21:03:11 -07:00
|
|
|
/**
|
|
|
|
|
* Send our payload into the mesh
|
|
|
|
|
*/
|
2024-07-13 19:36:44 +02:00
|
|
|
bool sendPayload(NodeNum dest = NODENUM_BROADCAST, uint32_t packetHistory_index = 0);
|
|
|
|
|
meshtastic_MeshPacket *preparePayload(NodeNum dest, uint32_t packetHistory_index, bool local = false);
|
2023-08-12 09:29:19 -05:00
|
|
|
void sendMessage(NodeNum dest, const meshtastic_StoreAndForward &payload);
|
2023-01-21 18:22:19 +01:00
|
|
|
void sendMessage(NodeNum dest, meshtastic_StoreAndForward_RequestResponse rr);
|
2024-07-13 19:36:44 +02:00
|
|
|
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.
|
|
|
|
|
*/
|
2023-01-21 18:22:19 +01:00
|
|
|
virtual bool wantPacket(const meshtastic_MeshPacket *p) override
|
2022-12-28 15:31:04 +01:00
|
|
|
{
|
2023-01-18 14:51:48 -06:00
|
|
|
switch (p->decoded.portnum) {
|
2023-01-21 18:22:19 +01:00
|
|
|
case meshtastic_PortNum_TEXT_MESSAGE_APP:
|
|
|
|
|
case meshtastic_PortNum_STORE_FORWARD_APP:
|
2023-01-18 14:51:48 -06:00
|
|
|
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
|
|
|
|
2021-12-03 22:20:15 -08:00
|
|
|
// S&F Defaults
|
2024-02-14 14:07:20 +01:00
|
|
|
uint32_t historyReturnMax = 25; // Return maximum of 25 records by default.
|
|
|
|
|
uint32_t historyReturnWindow = 240; // Return history of last 4 hours by default.
|
2023-01-18 14:51:48 -06:00
|
|
|
uint32_t records = 0; // Calculated
|
|
|
|
|
bool heartbeat = false; // No heartbeat.
|
2021-12-03 22:20:15 -08:00
|
|
|
|
2022-12-28 15:31:04 +01:00
|
|
|
// stats
|
2024-02-14 14:07:20 +01:00
|
|
|
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
|
|
|
|
2024-02-14 14:07:20 +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
|
|
|
|
2021-01-31 09:12:01 -08: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
|
|
|
*/
|
2023-01-21 18:22:19 +01:00
|
|
|
virtual ProcessMessage handleReceived(const meshtastic_MeshPacket &mp) override;
|
|
|
|
|
virtual bool handleReceivedProtobuf(const meshtastic_MeshPacket &mp, meshtastic_StoreAndForward *p);
|
2021-01-31 09:12:01 -08:00
|
|
|
};
|
|
|
|
|
|
2024-02-14 14:07:20 +01:00
|
|
|
extern StoreForwardModule *storeForwardModule;
|