Repository navigation
Expand file tree
/
Copy pathprotocol.cpp
More file actions
290 lines (256 loc) · 7.85 KB
/
Copy pathprotocol.cpp
File metadata and controls
290 lines (256 loc) · 7.85 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// =============================================================================
// protocol.cpp — battleship inter-board packet framer
// =============================================================================
#include "protocol.h"
// ===========================================================================
// CRC-8 (polynomial 0x07, init 0x00)
// ===========================================================================
uint8_t crc8(const uint8_t* data, uint8_t len) {
uint8_t crc = 0;
while (len--) {
crc ^= *data++;
for (uint8_t i = 0; i < 8; i++)
crc = (crc & 0x80) ? (crc << 1) ^ 0x07 : (crc << 1);
}
return crc;
}
// ===========================================================================
// Packet building
// ===========================================================================
uint8_t protocol_build_packet(uint8_t cmd, const uint8_t* payload, uint8_t len,
uint8_t* out) {
out[0] = PKT_START;
out[1] = cmd;
out[2] = len;
for (uint8_t i = 0; i < len; i++) out[3 + i] = payload[i];
uint8_t crcBuf[2 + MAX_PAYLOAD];
crcBuf[0] = cmd;
crcBuf[1] = len;
for (uint8_t i = 0; i < len; i++) crcBuf[2 + i] = payload[i];
out[3 + len] = crc8(crcBuf, 2 + len);
out[4 + len] = PKT_END;
return 5 + len;
}
// ===========================================================================
// Parser
// ===========================================================================
void protocol_parser_init(Parser& p) {
p.state = PS_WAIT_START;
p.cmd = 0;
p.len = 0;
p.payloadIdx = 0;
p.expectedCrc = 0;
p.frameErrors = 0;
p.packetReady = false;
}
static void parserResetToWait(Parser& p) {
p.state = PS_WAIT_START;
p.payloadIdx = 0;
}
bool protocol_parse_byte(Parser& p, uint8_t b) {
if (p.packetReady) return false;
switch (p.state) {
case PS_WAIT_START:
if (b == PKT_START) p.state = PS_READ_CMD;
break;
case PS_READ_CMD:
p.cmd = b;
p.state = PS_READ_LEN;
break;
case PS_READ_LEN:
if (b > MAX_PAYLOAD) {
p.frameErrors++;
parserResetToWait(p);
break;
}
p.len = b;
p.payloadIdx = 0;
p.state = (p.len == 0) ? PS_READ_CRC : PS_READ_PAYLOAD;
break;
case PS_READ_PAYLOAD:
p.payload[p.payloadIdx++] = b;
if (p.payloadIdx >= p.len) {
p.state = PS_READ_CRC;
}
break;
case PS_READ_CRC: {
uint8_t crcBuf[2 + MAX_PAYLOAD];
crcBuf[0] = p.cmd;
crcBuf[1] = p.len;
for (uint8_t i = 0; i < p.len; i++) crcBuf[2 + i] = p.payload[i];
uint8_t computed = crc8(crcBuf, 2 + p.len);
if (computed != b) {
p.frameErrors++;
parserResetToWait(p);
} else {
p.state = PS_READ_END;
}
break;
}
case PS_READ_END:
if (b == PKT_END) {
p.packetReady = true;
p.state = PS_WAIT_START;
return true;
} else {
p.frameErrors++;
parserResetToWait(p);
}
break;
}
return false;
}
// ===========================================================================
// Stream-bound state
// ===========================================================================
static Stream* g_link = nullptr;
static uint8_t g_boardId = 0;
static Parser g_parser;
static volatile uint8_t g_awaiting = 0;
static volatile bool g_replyReceived = false;
static volatile uint8_t g_replyOutcome = 0;
static volatile uint8_t g_fireR = 0, g_fireC = 0;
void protocol_init(Stream& link, uint8_t board_id) {
g_link = &link;
g_boardId = board_id;
protocol_parser_init(g_parser);
g_awaiting = 0;
g_replyReceived = false;
}
static void sendBytes(const uint8_t* buf, uint8_t n) {
if (!g_link) return;
for (uint8_t i = 0; i < n; i++) g_link->write(buf[i]);
}
static bool sendPacket(uint8_t cmd, const uint8_t* payload, uint8_t len) {
uint8_t buf[5 + MAX_PAYLOAD];
uint8_t n = protocol_build_packet(cmd, payload, len, buf);
sendBytes(buf, n);
return true;
}
// Auto-ACK helper: send an ACK with the given cmd back.
static void autoAck(uint8_t cmd_acked) {
uint8_t payload = cmd_acked;
sendPacket(CMD_ACK, &payload, 1);
}
static void dispatchPacket(const Parser& p) {
// ---- Auto-ACK first, BEFORE dispatching to application -----------------
// This way the ACK goes out promptly, even if the application's callback
// does some heavier work (display redraws, etc).
switch (p.cmd) {
case CMD_HELLO:
case CMD_READY:
case CMD_BEGIN:
case CMD_RESET:
autoAck(p.cmd);
break;
default:
break;
}
// ---- Application callbacks ---------------------------------------------
switch (p.cmd) {
case CMD_HELLO:
onHello(p.len ? p.payload[0] : 0);
break;
case CMD_READY:
onReady();
break;
case CMD_BEGIN:
onBegin();
break;
case CMD_FIRE:
if (p.len >= 2) onFire(p.payload[0], p.payload[1]);
break;
case CMD_RESULT:
if (p.len >= 3) onResult(p.payload[0], p.payload[1], p.payload[2]);
break;
case CMD_RESET:
onReset();
break;
case CMD_ACK:
onAck(p.len ? p.payload[0] : 0);
break;
case CMD_NACK:
onNack(p.len ? p.payload[0] : 0);
break;
}
// ---- Reply matching for blocking sends ---------------------------------
if (g_awaiting == 0 || g_replyReceived) return;
if (g_awaiting == CMD_ACK && p.cmd == CMD_ACK) {
g_replyReceived = true;
} else if (g_awaiting == CMD_RESULT && p.cmd == CMD_RESULT && p.len >= 3) {
if (p.payload[0] == g_fireR && p.payload[1] == g_fireC) {
g_replyOutcome = p.payload[2];
g_replyReceived = true;
}
}
}
void protocol_poll() {
if (!g_link) return;
while (g_link->available() > 0) {
int b = g_link->read();
if (b < 0) break;
if (protocol_parse_byte(g_parser, (uint8_t)b)) {
dispatchPacket(g_parser);
g_parser.packetReady = false;
}
}
}
static bool waitForReply(uint8_t cmd, uint16_t timeoutMs) {
g_awaiting = cmd;
g_replyReceived = false;
uint32_t deadline = millis() + timeoutMs;
while (millis() < deadline) {
protocol_poll();
if (g_replyReceived) {
g_awaiting = 0;
return true;
}
}
g_awaiting = 0;
return false;
}
static bool sendBlocking(uint8_t cmd, const uint8_t* payload, uint8_t len,
uint8_t expectedReply, uint16_t timeoutMs,
uint8_t tries) {
for (uint8_t attempt = 0; attempt < tries; attempt++) {
sendPacket(cmd, payload, len);
if (waitForReply(expectedReply, timeoutMs)) return true;
}
return false;
}
// ===========================================================================
// Public send functions
// ===========================================================================
bool protocol_send_hello() {
uint8_t payload = g_boardId;
return sendBlocking(CMD_HELLO, &payload, 1, CMD_ACK, 2000, 3);
}
bool protocol_send_ready() {
return sendBlocking(CMD_READY, nullptr, 0, CMD_ACK, 5000, 3);
}
bool protocol_send_begin() {
return sendBlocking(CMD_BEGIN, nullptr, 0, CMD_ACK, 30000, 1);
// Long timeout, no retry: the partner might be reading the instructions.
// Single attempt: we don't want to spam BEGINs.
}
bool protocol_send_fire(uint8_t r, uint8_t c, uint8_t* outOutcome) {
uint8_t payload[2] = {r, c};
g_fireR = r;
g_fireC = c;
bool ok = sendBlocking(CMD_FIRE, payload, 2, CMD_RESULT, 3000, 3);
if (ok && outOutcome) *outOutcome = g_replyOutcome;
return ok;
}
bool protocol_send_reset() {
return sendBlocking(CMD_RESET, nullptr, 0, CMD_ACK, 2000, 2);
}
bool protocol_send_result(uint8_t r, uint8_t c, uint8_t outcome) {
uint8_t payload[3] = {r, c, outcome};
return sendPacket(CMD_RESULT, payload, 3);
}
bool protocol_send_ack(uint8_t cmd_acked) {
return sendPacket(CMD_ACK, &cmd_acked, 1);
}
bool protocol_send_nack(uint8_t reason) {
return sendPacket(CMD_NACK, &reason, 1);
}