-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIDotMatrixBulkTransfer.cpp
More file actions
200 lines (180 loc) · 5.63 KB
/
Copy pathIDotMatrixBulkTransfer.cpp
File metadata and controls
200 lines (180 loc) · 5.63 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
#include "IDotMatrixBulkTransfer.h"
#include <cstring>
#include <cstdlib>
#if defined(ARDUINO_ARCH_ESP32)
#include <esp_heap_caps.h>
#include <esp32-hal-psram.h>
#endif
namespace {
constexpr uint8_t TEXT_TYPE = 0x03;
constexpr uint8_t RAW_TYPE = 0x02;
constexpr uint8_t GIF_TYPE = 0x01;
constexpr uint8_t ACK_CONTINUE = 0x01;
constexpr uint8_t ACK_COMPLETE = 0x03;
uint32_t readLE32(const uint8_t* data) {
return uint32_t(data[0]) |
(uint32_t(data[1]) << 8) |
(uint32_t(data[2]) << 16) |
(uint32_t(data[3]) << 24);
}
}
IDotMatrixBulkTransfer::~IDotMatrixBulkTransfer() {
clearTextBuffer();
}
uint8_t* IDotMatrixBulkTransfer::allocateTextBuffer(size_t bytes) {
if (bytes == 0 || bytes > MAX_TEXT_PAYLOAD) return nullptr;
#if defined(ARDUINO_ARCH_ESP32)
if (psramFound()) {
void* external = heap_caps_malloc(bytes, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
if (external != nullptr) return static_cast<uint8_t*>(external);
}
return static_cast<uint8_t*>(heap_caps_malloc(bytes, MALLOC_CAP_8BIT));
#else
return static_cast<uint8_t*>(std::malloc(bytes));
#endif
}
void IDotMatrixBulkTransfer::freeTextBuffer(uint8_t* buffer) {
if (buffer == nullptr) return;
#if defined(ARDUINO_ARCH_ESP32)
heap_caps_free(buffer);
#else
std::free(buffer);
#endif
}
void IDotMatrixBulkTransfer::clearTextBuffer() {
freeTextBuffer(textPayload_);
textPayload_ = nullptr;
textPayloadLength_ = 0;
textReady_ = false;
}
bool IDotMatrixBulkTransfer::processPacket(
const uint8_t* data,
size_t length,
IDotMatrixBulkResult& result
) {
result = IDotMatrixBulkResult{};
if (data == nullptr || length < HEADER_SIZE) return false;
const uint16_t declaredLength = uint16_t(data[0]) | (uint16_t(data[1]) << 8);
const uint8_t type = data[2];
if (declaredLength != length || data[3] != 0x00 ||
(type != TEXT_TYPE && type != RAW_TYPE && type != GIF_TYPE)) {
return false;
}
result.handled = true;
result.type = type;
const uint32_t totalSize = readLE32(data + 5);
const uint32_t expectedCRC = readLE32(data + 9);
const uint8_t option = data[4];
const uint16_t timeSign = uint16_t(data[13]) | (uint16_t(data[14]) << 8);
const uint8_t imageIndex = data[15];
result.totalLength = totalSize;
result.expectedCRC = expectedCRC;
result.option = option;
result.timeSign = timeSign;
result.imageIndex = imageIndex;
const uint32_t maximumSize = type == TEXT_TYPE ? MAX_TEXT_PAYLOAD :
type == RAW_TYPE ? MAX_RAW_PAYLOAD : MAX_GIF_PAYLOAD;
if (totalSize == 0 || totalSize > maximumSize) {
if (activeType_ == TEXT_TYPE || textPayload_ != nullptr) clearTextBuffer();
resetActive();
result.replyAvailable = true;
result.status = ACK_COMPLETE;
result.completed = true;
return true;
}
if (!active_) {
active_ = true;
activeType_ = type;
expectedSize_ = totalSize;
expectedCRC_ = expectedCRC;
runningCRC_ = 0xFFFFFFFFu;
receivedSize_ = 0;
option_ = option;
timeSign_ = timeSign;
imageIndex_ = imageIndex;
if (type == TEXT_TYPE) {
clearTextBuffer();
textPayload_ = allocateTextBuffer(expectedSize_);
if (textPayload_ == nullptr) {
resetActive();
result.replyAvailable = true;
result.status = ACK_COMPLETE;
result.completed = true;
return true;
}
}
result.began = true;
} else if (type != activeType_ || totalSize != expectedSize_ ||
expectedCRC != expectedCRC_) {
// Only the transfer identity (type/size/CRC) is required to remain stable
// across chunks. The app does not guarantee that Device Assets metadata
// bytes are repeated verbatim in every continuation packet. Latch option,
// dwell and slot from the first packet and expose those latched values for
// the whole transaction instead of aborting a valid multi-chunk asset.
if (activeType_ == TEXT_TYPE) clearTextBuffer();
resetActive();
result.aborted = true;
return true;
}
result.option = option_;
result.timeSign = timeSign_;
result.imageIndex = imageIndex_;
const size_t payloadLength = length - HEADER_SIZE;
const uint32_t remaining = expectedSize_ - receivedSize_;
const size_t useful = payloadLength < remaining ? payloadLength : remaining;
result.chunkData = data + HEADER_SIZE;
result.chunkOffset = receivedSize_;
result.chunkLength = useful;
if (useful > 0) {
if (type == TEXT_TYPE) {
memcpy(textPayload_ + receivedSize_, data + HEADER_SIZE, useful);
}
runningCRC_ = updateCRC32(runningCRC_, data + HEADER_SIZE, useful);
receivedSize_ += useful;
if (type == TEXT_TYPE) textPayloadLength_ = receivedSize_;
}
result.replyAvailable = true;
if (receivedSize_ < expectedSize_) {
result.status = ACK_CONTINUE;
return true;
}
result.status = ACK_COMPLETE;
result.completed = true;
result.calculatedCRC = runningCRC_ ^ 0xFFFFFFFFu;
result.crcValid = result.calculatedCRC == expectedCRC_;
if (result.crcValid) {
if (type == TEXT_TYPE) textReady_ = true;
} else {
if (type == TEXT_TYPE) clearTextBuffer();
}
resetActive();
return true;
}
uint32_t IDotMatrixBulkTransfer::updateCRC32(
uint32_t crc,
const uint8_t* data,
size_t length
) {
for (size_t index = 0; index < length; ++index) {
crc ^= data[index];
for (uint8_t bit = 0; bit < 8; ++bit) {
crc = (crc >> 1) ^ ((crc & 1u) ? 0xEDB88320u : 0u);
}
}
return crc;
}
void IDotMatrixBulkTransfer::resetActive() {
active_ = false;
activeType_ = 0;
expectedSize_ = 0;
expectedCRC_ = 0;
runningCRC_ = 0xFFFFFFFFu;
receivedSize_ = 0;
option_ = 0;
timeSign_ = 0;
imageIndex_ = 12;
}
void IDotMatrixBulkTransfer::reset() {
resetActive();
clearTextBuffer();
}