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
|
|
|
|
|
|
|
|
|
|
|
|
// Maximum text payload size per message in bytes (fixed).
|
2025-10-14 15:17:11 -04:00
|
|
|
|
// All messages use the same size to simplify memory handling and avoid dynamic allocations.
|
2025-10-12 22:36:15 -04:00
|
|
|
|
#define MAX_MESSAGE_SIZE 220
|
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-10-13 03:21:37 -04:00
|
|
|
|
// A single stored message in RAM and/or flash
|
2025-09-21 17:40:26 -04:00
|
|
|
|
struct StoredMessage {
|
2025-10-14 15:17:11 -04:00
|
|
|
|
uint32_t timestamp; // When message was created (secs since boot or RTC)
|
2025-10-14 14:14:28 -04:00
|
|
|
|
uint32_t sender; // NodeNum of sender
|
|
|
|
|
|
uint8_t channelIndex; // Channel index used
|
2025-10-14 15:17:11 -04:00
|
|
|
|
char text[MAX_MESSAGE_SIZE]; // Fixed-size buffer for message text (null-terminated)
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
uint32_t dest; // Destination node (broadcast or direct)
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
MessageType type; // Derived from dest (explicit classification)
|
2025-09-23 01:05:22 -04:00
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
bool isBootRelative; // true = millis()/1000 fallback; false = epoch/RTC absolute
|
2025-09-23 01:05:22 -04:00
|
|
|
|
|
2025-10-14 15:17:11 -04:00
|
|
|
|
AckStatus ackStatus; // Delivery status (only meaningful for our own sent messages)
|
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()
|
|
|
|
|
|
: timestamp(0), sender(0), channelIndex(0), text(""), dest(0xffffffff), type(MessageType::BROADCAST),
|
2025-09-28 00:58:48 -04:00
|
|
|
|
isBootRelative(false), ackStatus(AckStatus::NONE) // start as NONE (waiting, no symbol)
|
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-09-26 00:28:25 -04:00
|
|
|
|
// Clear all messages (RAM + persisted queue)
|
2025-09-21 17:40:26 -04:00
|
|
|
|
void clearAllMessages();
|
|
|
|
|
|
|
2025-09-26 00:28:25 -04:00
|
|
|
|
// Dismiss helpers
|
2025-10-14 15:17:11 -04:00
|
|
|
|
void dismissOldestMessage(); // remove oldest from RAM (and flash on save)
|
2025-09-21 17:40:26 -04:00
|
|
|
|
|
2025-09-28 01:31:32 -04:00
|
|
|
|
// New targeted dismiss helpers
|
|
|
|
|
|
void dismissOldestMessageInChannel(uint8_t channel);
|
|
|
|
|
|
void dismissOldestMessageWithPeer(uint32_t peer);
|
|
|
|
|
|
|
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-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
|