-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlumpparser.cpp
More file actions
754 lines (687 loc) · 23.6 KB
/
lumpparser.cpp
File metadata and controls
754 lines (687 loc) · 23.6 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
#include "lumpparser.h"
#include "format.h"
#include "legodevice.h"
#include "logging.h"
#include <cctype>
#include <cstring>
#include <memory>
// ---------------------------------------------------------------------------
// LUMP protocol constants (kept local — replaces protocolstate.h)
// ---------------------------------------------------------------------------
static constexpr uint8_t LUMP_MSG_TYPE_SYS = 0x00;
static constexpr uint8_t LUMP_MSG_TYPE_CMD = 0x40;
static constexpr uint8_t LUMP_MSG_TYPE_INFO = 0x80;
static constexpr uint8_t LUMP_MSG_TYPE_DATA = 0xC0;
static constexpr uint8_t LUMP_CMD_TYPE = 0x0;
static constexpr uint8_t LUMP_CMD_MODES = 0x1;
static constexpr uint8_t LUMP_CMD_SPEED = 0x2;
static constexpr uint8_t LUMP_CMD_SELECT = 0x3;
static constexpr uint8_t LUMP_CMD_WRITE = 0x4;
static constexpr uint8_t LUMP_CMD_EXT_MODE = 0x6;
static constexpr uint8_t LUMP_CMD_VERSION = 0x7;
static constexpr uint8_t LUMP_SYS_SYNC = 0x00;
static constexpr uint8_t LUMP_SYS_NACK = 0x02;
static constexpr uint8_t LUMP_SYS_ACK = 0x04;
static constexpr uint8_t LUMP_INFO_NAME = 0x00;
static constexpr uint8_t LUMP_INFO_RAW = 0x01;
static constexpr uint8_t LUMP_INFO_PCT = 0x02;
static constexpr uint8_t LUMP_INFO_SI = 0x03;
static constexpr uint8_t LUMP_INFO_UNITS = 0x04;
static constexpr uint8_t LUMP_INFO_MAPPING = 0x05;
static constexpr uint8_t LUMP_INFO_MODE_COMBOS = 0x06;
static constexpr uint8_t LUMP_INFO_MODE_PLUS_8 = 0x20;
static constexpr uint8_t LUMP_INFO_FORMAT = 0x80;
// ---------------------------------------------------------------------------
// BCD helper — converts a BCD byte to a two-character decimal string
// e.g. 0x31 -> "31" (note: uses nibble swap like the original parsecommandstate)
// ---------------------------------------------------------------------------
static void bcdByteToStr(uint8_t bcd, char out[3]) {
// The original code swaps nibbles before extracting digits
uint8_t swapped = (uint8_t) (((bcd & 0x0F) << 4) | ((bcd >> 4) & 0x0F));
out[0] = '0' + ((swapped >> 4) & 0x0F);
out[1] = '0' + (swapped & 0x0F);
out[2] = '\0';
}
// ---------------------------------------------------------------------------
// Constructor
// ---------------------------------------------------------------------------
LumpParser::LumpParser(LegoDevice* device)
: head_(0), count_(0), extModeOffset_(0), inSyncLoss_(false), consecutiveErrors_(0), syncLossDiscardStart_(0),
discardCapCount_(0), device_(device) {
memset(buf_, 0, sizeof(buf_));
memset(&stats_, 0, sizeof(stats_));
memset(discardCap_, 0, sizeof(discardCap_));
}
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
void LumpParser::feedByte(uint8_t byte) {
if (count_ >= ringBufSize) {
// Overflow: discard the oldest byte to make room
stats_.bufferOverflows++;
head_ = (head_ + 1) % ringBufSize;
count_--;
}
uint16_t tail = (head_ + count_) % ringBufSize;
buf_[tail] = byte;
count_++;
processBuffer();
}
void LumpParser::feedBytes(const uint8_t* data, int len) {
// Add all bytes to the ring buffer first, then process once.
// This ensures onDataFrameDispatched() fires only after the last
// frame in the batch (count_ == 0), not after each individual frame.
for (int i = 0; i < len; i++) {
if (count_ >= ringBufSize) {
stats_.bufferOverflows++;
head_ = (head_ + 1) % ringBufSize;
count_--;
}
uint16_t tail = (head_ + count_) % ringBufSize;
buf_[tail] = data[i];
count_++;
}
processBuffer();
}
auto LumpParser::stats() const -> const LumpParserStats& {
return stats_;
}
void LumpParser::resetStats() {
memset(&stats_, 0, sizeof(stats_));
}
void LumpParser::reset() {
head_ = 0;
count_ = 0;
extModeOffset_ = 0;
inSyncLoss_ = false;
consecutiveErrors_ = 0;
syncLossDiscardStart_ = 0;
discardCapCount_ = 0;
memset(&stats_, 0, sizeof(stats_));
}
void LumpParser::clearBuffer() {
head_ = 0;
count_ = 0;
extModeOffset_ = 0;
inSyncLoss_ = false;
consecutiveErrors_ = 0;
syncLossDiscardStart_ = 0;
discardCapCount_ = 0;
// stats_ intentionally preserved — they cover the pre-switch phase
}
// ---------------------------------------------------------------------------
// Static helpers
// ---------------------------------------------------------------------------
auto LumpParser::decodePayloadSize(uint8_t header) -> int {
uint8_t sizeEnc = (header >> 3) & 0x07;
switch (sizeEnc) {
case 0:
return 1;
case 1:
return 2;
case 2:
return 4;
case 3:
return 8;
case 4:
return 16;
case 5:
return 32;
default:
return -1; // 6 and 7 are reserved/invalid
}
}
// ---------------------------------------------------------------------------
// processBuffer — inner sliding-window loop
// ---------------------------------------------------------------------------
void LumpParser::processBuffer() {
while (count_ > 0) {
uint8_t header = buf_[head_];
uint8_t type = header & 0xC0;
// ---------------------------------------------------------------
// System byte: type bits 7-6 == 0b00
// ---------------------------------------------------------------
if (type == LUMP_MSG_TYPE_SYS) {
// Consume exactly 1 byte
head_ = (head_ + 1) % ringBufSize;
count_--;
if (header == LUMP_SYS_SYNC || header == LUMP_SYS_NACK || header == LUMP_SYS_ACK) {
dispatchSystemByte(header);
} else {
stats_.unknownSysBytes++;
}
continue;
}
// ---------------------------------------------------------------
// Decode payload size from header
// ---------------------------------------------------------------
int payloadSize = decodePayloadSize(header);
if (payloadSize < 0) {
// Invalid/reserved size encoding — discard this byte
stats_.invalidSizeBytes++;
stats_.bytesDiscarded++;
head_ = (head_ + 1) % ringBufSize;
count_--;
continue;
}
int frameSize = 1 + payloadSize + 1; // header + payload + checksum
// ---------------------------------------------------------------
// INFO messages have an extra byte for INFO type (not in payload size)
// ---------------------------------------------------------------
if (type == LUMP_MSG_TYPE_INFO) {
frameSize += 1; // Add 1 byte for INFO type byte
}
// ---------------------------------------------------------------
// Wait for more bytes if buffer doesn't have a complete frame
// ---------------------------------------------------------------
if (count_ < (uint16_t) frameSize) {
break;
}
// ---------------------------------------------------------------
// Compute expected checksum over header + payload bytes
// ---------------------------------------------------------------
uint8_t expected = 0xFF;
for (int i = 0; i < frameSize - 1; i++) {
expected ^= buf_[(head_ + i) % ringBufSize];
}
uint8_t actual = buf_[(head_ + frameSize - 1) % ringBufSize];
if (expected == actual) {
// -----------------------------------------------------------
// Valid frame: copy payload to stack buffer and dispatch
// For INFO messages, payload includes the INFO type byte
// -----------------------------------------------------------
int bytesToExtract = payloadSize;
if (type == LUMP_MSG_TYPE_INFO) {
bytesToExtract += 1; // Include INFO type byte in payload
}
uint8_t payload[33]; // Max 32 data bytes + 1 INFO type byte
for (int i = 0; i < bytesToExtract; i++) {
payload[i] = buf_[(head_ + 1 + i) % ringBufSize];
}
// Advance ring buffer past this frame
head_ = (head_ + frameSize) % ringBufSize;
count_ -= frameSize;
// Sync recovery tracking
if (inSyncLoss_) {
uint32_t episodeBytes = stats_.bytesDiscarded - syncLossDiscardStart_;
DEBUG("LUMP sync recovered after discarding %lu bytes", episodeBytes);
// Hex-dump the captured discard bytes
if (discardCapCount_ > 0) {
// Build hex string: "xx xx xx ..."
char hexbuf[discardCapSize * 3 + 1];
int pos = 0;
const char hex[] = "0123456789ABCDEF";
for (uint8_t i = 0; i < discardCapCount_; i++) {
hexbuf[pos++] = hex[(discardCap_[i] >> 4) & 0x0F];
hexbuf[pos++] = hex[discardCap_[i] & 0x0F];
hexbuf[pos++] = ' ';
}
if (pos > 0) {
hexbuf[pos - 1] = '\0';
} else {
hexbuf[0] = '\0';
}
bool truncated = (discardCapCount_ == discardCapSize && episodeBytes > discardCapSize);
DEBUG(" Discarded bytes (first %u%s): %s", discardCapCount_, truncated ? ", truncated" : "",
hexbuf);
}
stats_.syncRecoveries++;
inSyncLoss_ = false;
discardCapCount_ = 0;
}
consecutiveErrors_ = 0;
stats_.framesOk++;
dispatchFrame(header, payload, bytesToExtract);
} else {
// -----------------------------------------------------------
// Bad checksum: slide by 1 byte
// -----------------------------------------------------------
if (!inSyncLoss_) {
DEBUG("LUMP sync lost after %lu good frames (checksum expected=0x%02X actual=0x%02X, header=0x%02X, "
"type=0x%02X, payloadSize=0x%02X)",
stats_.framesOk, expected, actual, header, type, payloadSize);
inSyncLoss_ = true;
syncLossDiscardStart_ = stats_.bytesDiscarded;
discardCapCount_ = 0;
}
consecutiveErrors_++;
stats_.checksumErrors++;
stats_.bytesDiscarded++;
if (discardCapCount_ < discardCapSize) {
discardCap_[discardCapCount_++] = buf_[head_];
}
head_ = (head_ + 1) % ringBufSize;
count_--;
if (consecutiveErrors_ > LumpParser::syncLossResetThreshold) {
WARN("Too many consecutive checksum errors (%lu), triggering device reset", consecutiveErrors_);
device_->reset();
return;
}
}
}
}
// ---------------------------------------------------------------------------
// dispatchSystemByte
// ---------------------------------------------------------------------------
void LumpParser::dispatchSystemByte(uint8_t sysByte) {
if (sysByte == LUMP_SYS_SYNC) {
// Silently ignore SYNC bytes — they are just keep-alive markers
return;
}
if (sysByte == LUMP_SYS_ACK) {
if (!device_->isHandshakeComplete() && device_->fullyInitialized()) {
device_->markAsHandshakeComplete();
}
return;
}
if (sysByte == LUMP_SYS_NACK) {
DEBUG("Received unexpected NACK from device");
return;
}
}
// ---------------------------------------------------------------------------
// dispatchFrame — routes validated frames to the appropriate handler
// ---------------------------------------------------------------------------
void LumpParser::dispatchFrame(uint8_t header, const uint8_t* payload, int payloadSize) {
uint8_t type = header & 0xC0;
// -----------------------------------------------------------------------
// CMD frames (0x40)
// -----------------------------------------------------------------------
if (type == LUMP_MSG_TYPE_CMD) {
uint8_t cmd = header & 0x07;
switch (cmd) {
case LUMP_CMD_TYPE: {
INFO("Parsing LUMP_CMD_TYPE");
// payload[0] = device type ID
int deviceId = payload[0];
std::string deviceName;
switch (deviceId) {
case DEVICEID_EV3_COLOR_SENSOR:
deviceName = "MINDSTORMS EV3 Color Sensor";
break;
case DEVICEID_EV3_ULTRASONIC_SENSOR:
deviceName = "MINDSTORMS EV3 Ultrasonic Sensor";
break;
case DEVICEID_EV3_GYRO_SENSOR:
deviceName = "MINDSTORMS EV3 Gyro Sensor";
break;
case DEVICEID_EV3_INFRARED_SENSOR:
deviceName = "MINDSTORMS EV3 Infrared Sensor";
break;
case DEVICEID_WEDO20_TILT:
deviceName = "WeDo 2.0 Tilt Sensor";
break;
case DEVICEID_WEDO20_MOTION:
deviceName = "WeDo 2.0 Motion Sensor";
break;
case DEVICEID_BOOST_COLOR_DISTANCE_SENSOR:
deviceName = "BOOST Color and Distance Sensor";
break;
case DEVICEID_BOOST_INTERACTIVE_MOTOR:
deviceName = "BOOST Interactive Motor";
break;
case DEVICEID_TECHNIC_XL_MOTOR:
deviceName = "Technic XL Motor";
break;
case DEVICEID_TECHNIC_MEDIUM_MOTOR:
deviceName = "Technic Medium Motor";
break;
case DEVICEID_SPIKE_MEDIUM_MOTOR:
deviceName = "SPIKE Medium Motor";
break;
case DEVUCEID_SPIKE_LARGE_MOTOR:
deviceName = "SPIKE Large Motor";
break;
case DEVICEID_SPIKE_COLOR_SENSOR:
deviceName = "SPIKE Color Sensor";
break;
case DEVICEID_SPIKE_ULTRASONIC_SENSOR:
deviceName = "SPIKE Ultrasonic Sensor";
break;
case DEVICEID_SPIKE_PRIME_FORCE_SENSOR:
deviceName = "SPIKE Prime Force Sensor";
break;
case DEVICEID_TECHNIC_COLOR_LIGHT_MATRIX:
deviceName = "Technic Color Light Matrix";
break;
case DEVICEID_SPIKE_SMALL_MOTOR:
deviceName = "SPIKE Small Motor";
break;
case DEVICEID_TECHNIC_MEDIUM_ANGULAR_MOTOR:
deviceName = "Technic Medium Angular Motor";
break;
case DEVICEID_TECHNIC_LARGE_ANGULAR_MOTOR:
deviceName = "Technic Large Angular Motor";
break;
default:
deviceName = "Unknown Device";
break;
}
device_->setDeviceIdAndName(deviceId, deviceName);
break;
}
case LUMP_CMD_MODES: {
INFO("Parsing LUMP_CMD_MODES");
// payload size determines the format:
// 1 byte: numModes = payload[0]+1
// 2 bytes: numModes = payload[0]+1, views = payload[1]+1
// 4 bytes: numModes = payload[2]+1, views = payload[3]+1
if (payloadSize == 1) {
int numModes = payload[0] + 1;
device_->initNumberOfModes(numModes);
INFO("Number of supported modes is %d (1 byte payload)", numModes);
} else if (payloadSize == 2) {
int numModes = payload[0] + 1;
device_->initNumberOfModes(numModes);
INFO("Number of supported modes is %d (2 bytes payload)", numModes);
} else if (payloadSize == 4) {
int numModes = payload[0] + 1;
device_->initNumberOfModes(numModes);
INFO("Number of supported modes is %d (4 bytes payload)", numModes);
} else {
WARN("Unsupported payload length for CMD_MODES: %d", payloadSize);
}
break;
}
case LUMP_CMD_SPEED: {
INFO("Parsing LUMP_CMD_SPEED");
// 4-byte little-endian uint32 baud rate
if (payloadSize >= 4) {
long speed = ((long) (payload[0] & 0xFF)) | ((long) (payload[1] & 0xFF) << 8) |
((long) (payload[2] & 0xFF) << 16) | ((long) (payload[3] & 0xFF) << 24);
device_->setSerialSpeed(speed);
} else {
WARN("CMD_SPEED payload too short: %d bytes", payloadSize);
}
break;
}
case LUMP_CMD_SELECT: {
// Hub -> device command; ignore if received from device
WARN("CMD_SELECT received (hub->device, ignoring)");
break;
}
case LUMP_CMD_WRITE: {
// Hub -> device command; unexpected from device side
WARN("CMD_WRITE received from device (unexpected, ignoring)");
break;
}
case LUMP_CMD_EXT_MODE: {
DEBUG("Parsing LUMP_CMD_EXT_MODE");
// payload[0] = 0x00 (modes 0-7) or 0x08 (modes 8-15)
if (payloadSize >= 1) {
extModeOffset_ = payload[0];
DEBUG("CMD_EXT_MODE: extModeOffset set to %d", (int) extModeOffset_);
} else {
WARN("CMD_EXT_MODE payload too short: %d bytes", payloadSize);
}
break;
}
case LUMP_CMD_VERSION: {
INFO("Parsing LUMP_CMD_VERSION");
// 8 bytes: FW version (bytes 0-3 BCD LE) + HW version (bytes 4-7 BCD LE)
if (payloadSize >= 8) {
char tmp[3];
std::string fwVersion;
fwVersion.reserve(12);
bcdByteToStr(payload[3], tmp);
fwVersion += tmp;
fwVersion += '.';
bcdByteToStr(payload[2], tmp);
fwVersion += tmp;
fwVersion += '.';
bcdByteToStr(payload[1], tmp);
fwVersion += tmp;
fwVersion += '.';
bcdByteToStr(payload[0], tmp);
fwVersion += tmp;
std::string hwVersion;
hwVersion.reserve(12);
bcdByteToStr(payload[7], tmp);
hwVersion += tmp;
hwVersion += '.';
bcdByteToStr(payload[6], tmp);
hwVersion += tmp;
hwVersion += '.';
bcdByteToStr(payload[5], tmp);
hwVersion += tmp;
hwVersion += '.';
bcdByteToStr(payload[4], tmp);
hwVersion += tmp;
device_->setVersions(fwVersion, hwVersion);
} else {
WARN("CMD_VERSION payload too short: %d bytes", payloadSize);
}
break;
}
default:
WARN("Unknown CMD command: 0x%02X (header=0x%02X)", cmd, header);
break;
}
return;
}
// -----------------------------------------------------------------------
// INFO frames (0x80)
// -----------------------------------------------------------------------
if (type == LUMP_MSG_TYPE_INFO) {
if (payloadSize < 1) {
WARN("INFO frame with empty payload, skipping");
return;
}
int mode = header & 0x07;
uint8_t infoByte = payload[0];
// Check for INFO_MODE_PLUS_8 flag in payload[0]
if (infoByte & LUMP_INFO_MODE_PLUS_8) {
mode += 8;
}
uint8_t infoType = infoByte & (uint8_t) (~LUMP_INFO_MODE_PLUS_8); // strip bit 5
// Validate mode index
if (mode < 0 || mode >= 16) {
WARN("INFO frame with invalid mode index %d, skipping", mode);
return;
}
Mode* modeObj = device_->getMode(mode);
if (modeObj == nullptr) {
WARN("INFO frame: getMode(%d) returned nullptr, skipping", mode);
return;
}
switch (infoType) {
case LUMP_INFO_NAME: {
INFO("Parsing LUMP_INFO_NAME");
// payload[1..] = null-terminated ASCII name
if (payloadSize < 2) {
WARN("INFO_NAME payload too short: %d bytes", payloadSize);
break;
}
char name[12]; // max 11 chars + null
int nameLen = 0;
for (int i = 1; i < payloadSize && nameLen < 11; i++) {
uint8_t ch = payload[i];
if (ch == 0) {
break;
}
name[nameLen++] = (char) ch;
}
name[nameLen] = '\0';
if (nameLen > 0 && isupper((unsigned char) name[0])) {
INFO("Mode %d name is '%s'", mode, name);
std::string nameStr(name, nameLen);
modeObj->setName(nameStr);
} else {
WARN("Ignoring Name for mode %d: invalid (len=%d or not uppercase)", mode, nameLen);
}
break;
}
case LUMP_INFO_RAW: {
INFO("Parsing LUMP_INFO_RAW");
// payload[1..4] = float min, payload[5..8] = float max (little-endian IEEE 754)
if (payloadSize < 9) {
WARN("INFO_RAW payload too short: %d bytes", payloadSize);
break;
}
union {
uint8_t b[4];
float f;
} uMin, uMax;
memcpy(uMin.b, &payload[1], 4);
memcpy(uMax.b, &payload[5], 4);
// RAW min/max — Mode does not have setRawMinMax, but has no direct API.
// Store via available method if present; otherwise silently skip.
// (The original parseinfostate.cpp also skipped RAW in the INFO switch.)
DEBUG("INFO_RAW for mode %d: min=%f max=%f (skipped)", mode, uMin.f, uMax.f);
break;
}
case LUMP_INFO_PCT: {
INFO("Parsing LUMP_INFO_PCT");
// payload[1..4] = float min, payload[5..8] = float max
if (payloadSize < 9) {
WARN("INFO_PCT payload too short: %d bytes", payloadSize);
break;
}
union {
uint8_t b[4];
float f;
} uMin, uMax;
memcpy(uMin.b, &payload[1], 4);
memcpy(uMax.b, &payload[5], 4);
modeObj->setPctMinMax(uMin.f, uMax.f);
break;
}
case LUMP_INFO_SI: {
INFO("Parsing LUMP_INFO_SI");
// payload[1..4] = float min, payload[5..8] = float max
if (payloadSize < 9) {
WARN("INFO_SI payload too short: %d bytes", payloadSize);
break;
}
union {
uint8_t b[4];
float f;
} uMin, uMax;
memcpy(uMin.b, &payload[1], 4);
memcpy(uMax.b, &payload[5], 4);
modeObj->setSiMinMax(uMin.f, uMax.f);
break;
}
case LUMP_INFO_UNITS: {
INFO("Parsing LUMP_INFO_UNITS");
// payload[1..] = null-terminated ASCII units string, max 4 chars
if (payloadSize < 2) {
WARN("INFO_UNITS payload too short: %d bytes", payloadSize);
break;
}
char units[5]; // max 4 chars + null
int unitsLen = 0;
for (int i = 1; i < payloadSize && unitsLen < 4; i++) {
uint8_t ch = payload[i];
if (ch == 0) {
break;
}
units[unitsLen++] = (char) ch;
}
units[unitsLen] = '\0';
std::string unitsStr(units, unitsLen);
modeObj->setUnits(unitsStr);
break;
}
case LUMP_INFO_MAPPING: {
INFO("Parsing LUMP_INFO_MAPPING");
// payload[1] = input flags, payload[2] = output flags
if (payloadSize < 3) {
WARN("INFO_MAPPING payload too short: %d bytes", payloadSize);
break;
}
uint8_t inputFlags = payload[1];
uint8_t outputFlags = payload[2];
if (inputFlags & 128) {
modeObj->registerInputType(Mode::InputOutputType::SUPPORTS_NULL);
}
if (inputFlags & 64) {
modeObj->registerInputType(Mode::InputOutputType::SUPPORTS_FUNCTIONAL_MAPPING_20);
}
if (inputFlags & 16) {
modeObj->registerInputType(Mode::InputOutputType::ABS);
}
if (inputFlags & 8) {
modeObj->registerInputType(Mode::InputOutputType::REL);
}
if (inputFlags & 4) {
modeObj->registerInputType(Mode::InputOutputType::DIS);
}
if (outputFlags & 128) {
modeObj->registerOutputType(Mode::InputOutputType::SUPPORTS_NULL);
}
if (outputFlags & 64) {
modeObj->registerOutputType(Mode::InputOutputType::SUPPORTS_FUNCTIONAL_MAPPING_20);
}
if (outputFlags & 16) {
modeObj->registerOutputType(Mode::InputOutputType::ABS);
}
if (outputFlags & 8) {
modeObj->registerOutputType(Mode::InputOutputType::REL);
}
if (outputFlags & 4) {
modeObj->registerOutputType(Mode::InputOutputType::DIS);
}
break;
}
case LUMP_INFO_MODE_COMBOS: {
INFO("Parsing LUMP_INFO_MODE_COMBOS");
INFO("Got Info Mode Combos for mode %d, payload size %d", mode, payloadSize);
for (int i = 0; i < payloadSize; i++) {
DEBUG(" Byte %d = 0x%02X", i, payload[i]);
}
// No action yet — combi-mode support is a future extension
break;
}
case LUMP_INFO_FORMAT: {
INFO("Parsing LUMP_INFO_FORMAT");
// payload[1]=datasets, payload[2]=format type, payload[3]=figures, payload[4]=decimals
if (payloadSize < 5) {
WARN("INFO_FORMAT payload too short: %d bytes", payloadSize);
break;
}
int datasets = payload[1];
int fmtType = payload[2];
int figures = payload[3];
int decimals = payload[4];
INFO("Mode %d format: datasets=%d, format=%d, figures=%d, decimals=%d", mode, datasets, fmtType,
figures, decimals);
modeObj->setFormat(std::make_unique<Format>(datasets, Format::forId(fmtType), figures, decimals));
break;
}
default: {
// Unknown info types 0x07-0x0C and anything else: log at DEBUG, do not discard
WARN("Unknown INFO type 0x%02X for mode %d, ignoring", infoType, mode);
break;
}
}
// INFO frames resolve their extended-mode index from LUMP_INFO_MODE_PLUS_8 in
// the info byte, not from extModeOffset_. Consume the offset here so it does
// not bleed into subsequent DATA frames after enumeration of modes 8+.
extModeOffset_ = 0;
return;
}
// -----------------------------------------------------------------------
// DATA frames (0xC0)
// -----------------------------------------------------------------------
if (type == LUMP_MSG_TYPE_DATA) {
int mode = (header & 0x07) + extModeOffset_;
extModeOffset_ = 0; // consume the offset
if (mode < 0 || mode >= 16) {
WARN("DATA frame with invalid mode index %d, skipping", mode);
return;
}
device_->onDataFrame(mode, payload, payloadSize);
// Signal the inter-frame gap only when the ring buffer is empty.
// If more frames are batched (count_ > 0), the device is already
// transmitting subsequent frames; sending a NACK now would collide.
// When count_ == 0 we have fully caught up — the wire is idle.
if (count_ == 0) {
device_->onDataFrameDispatched();
}
return;
}
// SYS frames (type == 0x00) are handled before dispatchFrame() is called;
// reaching here should never happen.
WARN("dispatchFrame called with unexpected type 0x%02X (header=0x%02X)", type, header);
}