-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathespnow_protocol.h
More file actions
171 lines (147 loc) · 5.07 KB
/
espnow_protocol.h
File metadata and controls
171 lines (147 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#ifndef ESPNOW_PROTOCOL_H
#define ESPNOW_PROTOCOL_H
#include <cstdint>
#include <cstring>
#include <string>
// ESP-NOW message size limit is 250 bytes
#define ESPNOW_MAX_DATA_LEN 250
#define ESPNOW_UUID_LEN 37
#define ESPNOW_NAME_LEN 32
#define ESPNOW_INBOX_LEN 64
#define ESPNOW_TEXT_LEN 150
namespace espnow {
// Message types
enum MessageType : uint8_t {
kMessageTypePairingRequest = 0x01,
kMessageTypePairingResponse = 0x02,
kMessageTypeTextMessage = 0x03,
kMessageTypeMetadataUpdate = 0x04,
kMessageTypeHeartbeat = 0x05,
kMessageTypeUnpair = 0x06
};
// Base message header (all messages start with this)
struct MessageHeader {
uint8_t version; // Protocol version (currently 1)
uint8_t type; // MessageType
uint16_t payload_size; // Size of the payload following this header
uint32_t timestamp; // Timestamp in milliseconds
uint8_t sender_mac[6]; // Sender MAC address
} __attribute__((packed));
// Pairing Request Message
// Sent when device wants to pair after sustained proximity
struct PairingRequestPayload {
char device_uuid[ESPNOW_UUID_LEN]; // Device UUID
char device_name[ESPNOW_NAME_LEN]; // Device name (e.g., "PlaiPin-abc123")
char agentmail_inbox[ESPNOW_INBOX_LEN]; // AgentMail inbox ID
int8_t rssi; // Current RSSI value
uint8_t reserved[2]; // Reserved for future use
} __attribute__((packed));
// Pairing Response Message
// Sent in response to pairing request
struct PairingResponsePayload {
char device_uuid[ESPNOW_UUID_LEN];
char device_name[ESPNOW_NAME_LEN];
char agentmail_inbox[ESPNOW_INBOX_LEN];
uint8_t accepted; // 1 = accepted, 0 = rejected
uint8_t reserved[2]; // Reserved for future use
} __attribute__((packed));
// Text Message
// Plain text message to be processed by LLM
struct TextMessagePayload {
char sender_uuid[ESPNOW_UUID_LEN];
char sender_name[ESPNOW_NAME_LEN];
char message_text[ESPNOW_TEXT_LEN];
uint8_t reserved[2];
} __attribute__((packed));
// Metadata Update Message
// Update device information for already paired devices
struct MetadataUpdatePayload {
char device_uuid[ESPNOW_UUID_LEN];
char device_name[ESPNOW_NAME_LEN];
char agentmail_inbox[ESPNOW_INBOX_LEN];
uint8_t reserved[2];
} __attribute__((packed));
// Heartbeat Message
// Periodic message to maintain connection
struct HeartbeatPayload {
uint32_t uptime_seconds;
int8_t rssi;
uint8_t battery_level; // 0-100
uint8_t reserved[2];
} __attribute__((packed));
// Unpair Message
// Request to remove pairing
struct UnpairPayload {
char device_uuid[ESPNOW_UUID_LEN];
uint8_t reason; // Optional reason code
uint8_t reserved[2];
} __attribute__((packed));
// Complete message structure
struct Message {
MessageHeader header;
union {
PairingRequestPayload pairing_request;
PairingResponsePayload pairing_response;
TextMessagePayload text_message;
MetadataUpdatePayload metadata_update;
HeartbeatPayload heartbeat;
UnpairPayload unpair;
uint8_t raw[ESPNOW_MAX_DATA_LEN - sizeof(MessageHeader)];
} payload;
} __attribute__((packed));
// Helper class for message serialization/deserialization
class MessageBuilder {
public:
// Create pairing request message
static bool BuildPairingRequest(
Message& msg,
const uint8_t* sender_mac,
const std::string& device_uuid,
const std::string& device_name,
const std::string& agentmail_inbox,
int8_t rssi);
// Create pairing response message
static bool BuildPairingResponse(
Message& msg,
const uint8_t* sender_mac,
const std::string& device_uuid,
const std::string& device_name,
const std::string& agentmail_inbox,
bool accepted);
// Create text message
static bool BuildTextMessage(
Message& msg,
const uint8_t* sender_mac,
const std::string& sender_uuid,
const std::string& sender_name,
const std::string& text);
// Create metadata update message
static bool BuildMetadataUpdate(
Message& msg,
const uint8_t* sender_mac,
const std::string& device_uuid,
const std::string& device_name,
const std::string& agentmail_inbox);
// Create heartbeat message
static bool BuildHeartbeat(
Message& msg,
const uint8_t* sender_mac,
uint32_t uptime_seconds,
int8_t rssi,
uint8_t battery_level);
// Create unpair message
static bool BuildUnpair(
Message& msg,
const uint8_t* sender_mac,
const std::string& device_uuid,
uint8_t reason);
// Validate message integrity
static bool ValidateMessage(const Message& msg, size_t recv_len);
// Get message type as string (for logging)
static const char* MessageTypeToString(MessageType type);
private:
// Helper to initialize header
static void InitHeader(MessageHeader& header, MessageType type, const uint8_t* sender_mac);
};
} // namespace espnow
#endif // ESPNOW_PROTOCOL_H