2025-09-21 17:40:26 -04:00
|
|
|
|
#pragma once
|
2025-10-06 12:29:43 -04:00
|
|
|
|
|
|
|
|
|
|
#if HAS_SCREEN
|
|
|
|
|
|
|
2025-10-10 09:34:43 -05:00
|
|
|
|
// Disable debug logging entirely on release builds of HELTEC_MESH_SOLAR for space constraints
|
|
|
|
|
|
#if defined(HELTEC_MESH_SOLAR)
|
|
|
|
|
|
#define LOG_DEBUG(...)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-10-13 03:21:37 -04:00
|
|
|
|
// Enable or disable message persistence (flash storage)
|
|
|
|
|
|
// Define -DENABLE_MESSAGE_PERSISTENCE=0 in build_flags to disable it entirely
|
2025-10-06 12:29:43 -04:00
|
|
|
|
#ifndef ENABLE_MESSAGE_PERSISTENCE
|
|
|
|
|
|
#define ENABLE_MESSAGE_PERSISTENCE 1
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
#include "mesh/generated/meshtastic/mesh.pb.h"
|
2025-09-21 17:40:26 -04:00
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
#include <deque>
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
// How many messages are stored (RAM + flash).
|
2025-10-13 03:21:37 -04:00
|
|
|
|
// Define -DMESSAGE_HISTORY_LIMIT=N in build_flags to control memory usage.
|
|
|
|
|
|
#ifndef MESSAGE_HISTORY_LIMIT
|
|
|
|
|
|
#define MESSAGE_HISTORY_LIMIT 20
|
2025-10-12 16:24:40 -04:00
|
|
|
|
#endif
|
2025-10-12 22:36:15 -04:00
|
|
|
|
|
2025-10-13 03:21:37 -04:00
|
|
|
|
// Internal alias used everywhere in code – do NOT redefine elsewhere.
|
|
|
|
|
|
#define MAX_MESSAGES_SAVED MESSAGE_HISTORY_LIMIT
|
|
|
|
|
|
|
2025-10-15 01:57:51 -04:00
|
|
|
|
// Maximum text payload size per message in bytes.
|
|
|
|
|
|
// This still defines the max message length, but we no longer reserve this space per message.
|
2025-10-12 22:36:15 -04:00
|
|
|
|
#define MAX_MESSAGE_SIZE 220
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-10-15 01:57:51 -04:00
|
|
|
|
// Total shared text pool size for all messages combined.
|
|
|
|
|
|
// The text pool is RAM-only. Text is re-stored from flash into the pool on boot.
|
|
|
|
|
|
#ifndef MESSAGE_TEXT_POOL_SIZE
|
|
|
|
|
|
#define MESSAGE_TEXT_POOL_SIZE (MAX_MESSAGES_SAVED * MAX_MESSAGE_SIZE)
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-09-21 17:40:26 -04:00
|
|
|
|
// Explicit message classification
|
2025-10-14 15:17:11 -04:00
|
|
|
|
enum class MessageType : uint8_t {
|
|
|
|
|
|
BROADCAST = 0, // broadcast message
|
|
|
|
|
|
DM_TO_US = 1 // direct message addressed to this node
|
|
|
|
|
|
};
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-09-26 16:51:09 -04:00
|
|
|
|
// Delivery status for messages we sent
|
|
|
|
|
|
enum class AckStatus : uint8_t {
|
2025-09-28 00:58:48 -04:00
|
|
|
|
NONE = 0, // just sent, waiting (no symbol shown)
|
|
|
|
|
|
ACKED = 1, // got a valid ACK from destination
|
2025-09-26 16:51:09 -04:00
|
|
|
|
NACKED = 2, // explicitly failed
|
2025-09-28 00:58:48 -04:00
|
|
|
|
TIMEOUT = 3, // no ACK after retry window
|
|
|
|
|
|
RELAYED = 4 // got an ACK from relay, not destination
|
2025-09-26 16:51:09 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-21 17:40:26 -04:00
|
|
|
|
struct StoredMessage {
|
2025-10-15 01:57:51 -04:00
|
|
|
|
uint32_t timestamp; // When message was created (secs since boot or RTC)
|
|
|
|
|
|
uint32_t sender; // NodeNum of sender
|
|
|
|
|
|
uint8_t channelIndex; // Channel index used
|
|
|
|
|
|
uint32_t dest; // Destination node (broadcast or direct)
|
|
|
|
|
|
MessageType type; // Derived from dest (explicit classification)
|
|
|
|
|
|
bool isBootRelative; // true = millis()/1000 fallback; false = epoch/RTC absolute
|
|
|
|
|
|
AckStatus ackStatus; // Delivery status (only meaningful for our own sent messages)
|
|
|
|
|
|
|
|
|
|
|
|
// Text storage metadata — rebuilt from flash at boot
|
|
|
|
|
|
uint16_t textOffset; // Offset into global text pool (valid only after loadFromFlash())
|
|
|
|
|
|
uint16_t textLength; // Length of text in bytes
|
2025-09-26 16:51:09 -04:00
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
// Default constructor initializes all fields safely
|
2025-09-23 01:05:22 -04:00
|
|
|
|
StoredMessage()
|
2025-10-15 01:57:51 -04:00
|
|
|
|
: timestamp(0), sender(0), channelIndex(0), dest(0xffffffff), type(MessageType::BROADCAST), isBootRelative(false),
|
|
|
|
|
|
ackStatus(AckStatus::NONE), textOffset(0), textLength(0)
|
2025-09-23 01:05:22 -04:00
|
|
|
|
{
|
|
|
|
|
|
}
|
2025-09-21 17:40:26 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MessageStore
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
explicit MessageStore(const std::string &label);
|
|
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
// Live RAM methods (always current, used by UI and runtime)
|
2025-10-12 16:24:40 -04:00
|
|
|
|
void addLiveMessage(StoredMessage &&msg);
|
2025-10-12 22:36:15 -04:00
|
|
|
|
void addLiveMessage(const StoredMessage &msg); // convenience overload
|
2025-09-21 17:40:26 -04:00
|
|
|
|
const std::deque<StoredMessage> &getLiveMessages() const { return liveMessages; }
|
|
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
// Add new messages from packets or manual input
|
|
|
|
|
|
const StoredMessage &addFromPacket(const meshtastic_MeshPacket &mp); // Incoming/outgoing → RAM only
|
|
|
|
|
|
void addFromString(uint32_t sender, uint8_t channelIndex, const std::string &text); // Manual add
|
|
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
// Persistence methods (used only on boot/shutdown)
|
2025-10-14 15:17:11 -04:00
|
|
|
|
void saveToFlash(); // Save messages to flash
|
|
|
|
|
|
void loadFromFlash(); // Load messages from flash
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-10-15 01:57:51 -04:00
|
|
|
|
// Clear all messages (RAM + persisted queue + text pool)
|
2025-09-21 17:40:26 -04:00
|
|
|
|
void clearAllMessages();
|
|
|
|
|
|
|
2025-11-10 00:42:49 -05:00
|
|
|
|
// Delete helpers
|
|
|
|
|
|
void deleteOldestMessage(); // remove oldest from RAM (and flash on save)
|
|
|
|
|
|
void deleteOldestMessageInChannel(uint8_t channel);
|
|
|
|
|
|
void deleteOldestMessageWithPeer(uint32_t peer);
|
|
|
|
|
|
void deleteAllMessagesInChannel(uint8_t channel);
|
|
|
|
|
|
void deleteAllMessagesWithPeer(uint32_t peer);
|
2025-09-28 01:31:32 -04:00
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
// Unified accessor (for UI code, defaults to RAM buffer)
|
2025-09-21 17:40:26 -04:00
|
|
|
|
const std::deque<StoredMessage> &getMessages() const { return liveMessages; }
|
|
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
// Helper filters for future use
|
2025-10-14 15:17:11 -04:00
|
|
|
|
std::deque<StoredMessage> getChannelMessages(uint8_t channel) const; // Only broadcast messages on a channel
|
|
|
|
|
|
std::deque<StoredMessage> getDirectMessages() const; // Only direct messages
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-09-23 01:05:22 -04:00
|
|
|
|
// Upgrade boot-relative timestamps once RTC is valid
|
|
|
|
|
|
void upgradeBootRelativeTimestamps();
|
|
|
|
|
|
|
2025-10-15 01:57:51 -04:00
|
|
|
|
// Retrieve the C-string text for a stored message
|
|
|
|
|
|
static const char *getText(const StoredMessage &msg);
|
|
|
|
|
|
|
|
|
|
|
|
// Allocate text into pool (used by sender-side code)
|
|
|
|
|
|
static uint16_t storeText(const char *src, size_t len);
|
|
|
|
|
|
|
|
|
|
|
|
// Used when loading from flash to rebuild the text pool
|
|
|
|
|
|
static uint16_t rebuildTextFromFlash(const char *src, size_t len);
|
|
|
|
|
|
|
2025-09-21 17:40:26 -04:00
|
|
|
|
private:
|
2025-10-14 15:17:11 -04:00
|
|
|
|
std::deque<StoredMessage> liveMessages; // Single in-RAM message buffer (also used for persistence)
|
|
|
|
|
|
std::string filename; // Flash filename for persistence
|
2025-09-21 17:40:26 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
// Global instance (defined in MessageStore.cpp)
|
2025-09-28 00:58:48 -04:00
|
|
|
|
extern MessageStore messageStore;
|
2025-10-06 12:29:43 -04:00
|
|
|
|
|
|
|
|
|
|
#endif
|