/
packet.cpp
436 lines (373 loc) · 10.8 KB
/
packet.cpp
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
#include <QDebug>
#include <QTextStream>
#include "packet.h"
#include "byteswap.h"
static QList<QString> packetTypes;
enum control_pins {
NAND_ALE = 2,
NAND_CLE = 1,
NAND_WE = 4,
NAND_RE = 8,
NAND_CS = 16,
NAND_RB = 32,
};
static uint8_t order[] = {
4, // Known
5, // Known
6, // Known
7, // Known
3, // Known
2, // Known
1, // Known
0, // Known
};
static uint8_t nand_unscramble_byte(uint8_t byte) {
return (
( (!!(byte&(1<<order[0]))) << 0)
| ( (!!(byte&(1<<order[1]))) << 1)
| ( (!!(byte&(1<<order[2]))) << 2)
| ( (!!(byte&(1<<order[3]))) << 3)
| ( (!!(byte&(1<<order[4]))) << 4)
| ( (!!(byte&(1<<order[5]))) << 5)
| ( (!!(byte&(1<<order[6]))) << 6)
| ( (!!(byte&(1<<order[7]))) << 7)
);
}
QList<QString> &PacketTypes()
{
if (packetTypes.length() <= 0) {
packetTypes.append("PACKET_UNKNOWN");
packetTypes.append("PACKET_ERROR");
packetTypes.append("PACKET_NAND_CYCLE");
packetTypes.append("PACKET_SD_DATA");
packetTypes.append("PACKET_SD_CMD_ARG");
packetTypes.append("PACKET_SD_RESPONSE");
packetTypes.append("PACKET_SD_CID");
packetTypes.append("PACKET_SD_CSD");
packetTypes.append("PACKET_BUFFER_OFFSET");
packetTypes.append("PACKET_BUFFER_CONTENTS");
packetTypes.append("PACKET_COMMAND");
packetTypes.append("PACKET_RESET");
packetTypes.append("PACKET_BUFFER_DRAIN");
packetTypes.append("PACKET_HELLO");
}
return packetTypes;
}
static qint64 streamReadData(QIODevice &source, char *data, qint64 bytesTotal) {
qint64 bytesRead = 0;
qint64 bytesLeft = bytesTotal;
int result;
while (bytesLeft > 0) {
result = source.read(data, bytesLeft);
if (result < 0) {
qDebug() << "Source had error:" << source.errorString();
return result;
}
if (result == 0) {
if (!source.waitForReadyRead(-1)) {
qDebug() << "Source is done, closing";
break;
}
}
if (result > bytesTotal) {
qDebug() << "Very bad. Wanted " << bytesTotal << ", but got " << result;
}
bytesRead += result;
data += result;
if (bytesLeft - result < 0) {
qDebug() << "That was a really strange read";
}
bytesLeft -= result;
}
if (bytesLeft < 0) {
qDebug() << "How could bytesLeft (" << bytesLeft << ") be less than zero?";
}
return bytesRead;
}
Packet::Packet(QObject *parent) :
QObject(parent)
{
memset(&packet, 0, sizeof(packet));
}
Packet::Packet(QIODevice &source, QObject *parent) :
QObject(parent)
{
qint64 bytesRead;
memset(&packet, 0, sizeof(packet));
bytesRead = streamReadData(source, (char *)(&packet.header), sizeof(packet.header));
if (bytesRead != sizeof(packet.header)) {
qDebug() << "Read an unexpected number of bytes:" << bytesRead << "vs" << sizeof(packet.header);
}
if (_ntohs(packet.header.size) > sizeof(packet)) {
qDebug() << "Header size is VERY wrong:" << _ntohs(packet.header.size);
}
bytesRead = streamReadData(source, (char *)(&packet.data), _ntohs(packet.header.size) - sizeof(packet.header));
decodePacket();
}
Packet::Packet(QByteArray &data, QObject *parent) :
QObject(parent)
{
memset(&packet, 0, sizeof(packet));
size_t size = data.length();
if (size > sizeof(packet))
size = sizeof(packet);
memcpy(&packet, data, size);
decodePacket();
}
Packet::Packet(const Packet &other, QObject *parent) :
QObject(parent)
{
memcpy(&packet, &other.packet, sizeof(packet));
decodePacket();
setIndex(other.packetIndex);
}
Packet &Packet::operator=(const Packet &other)
{
memcpy(&packet, &other.packet, sizeof(packet));
decodePacket();
return *this;
}
bool Packet::operator<(const Packet &other) const
{
if (seconds() < other.seconds())
return true;
if (seconds() > other.seconds())
return false;
if (seconds() == other.seconds() && nanoSeconds() < other.nanoSeconds())
return true;
return false;
}
void Packet::decodePacket() {
if (packetType() == PACKET_ERROR) {
char message[sizeof(packet.data.error.message)+1];
memset(message, 0, sizeof(message));
strncpy(message, (const char *)packet.data.error.message, sizeof(message));
errorString = message;
errorString.remove("\n");
}
else
errorString = "";
if (packetType() == PACKET_COMMAND) {
char cmd[sizeof(packet.data.command.cmd)+1];
memset(cmd, 0, sizeof(cmd));
memcpy(cmd, packet.data.command.cmd, sizeof(packet.data.command.cmd));
commandNameString = cmd;
commandNameString.remove("\n");
}
else
commandNameString = "";
if (packetType() == PACKET_SD_CID) {
cardCID.sprintf("%02x %02x %02x %02x %02x %02x %02x %02x",
packet.data.cid.cid[0],
packet.data.cid.cid[1],
packet.data.cid.cid[2],
packet.data.cid.cid[3],
packet.data.cid.cid[4],
packet.data.cid.cid[5],
packet.data.cid.cid[6],
packet.data.cid.cid[7]
);
}
else
cardCID = "";
if (packetType() == PACKET_SD_CSD) {
cardCSD.sprintf("%02x %02x %02x %02x %02x %02x %02x %02x",
packet.data.csd.csd[0],
packet.data.csd.csd[1],
packet.data.csd.csd[2],
packet.data.csd.csd[3],
packet.data.csd.csd[4],
packet.data.csd.csd[5],
packet.data.csd.csd[6],
packet.data.csd.csd[7]
);
}
else
cardCSD = "";
}
uint32_t Packet::nanoSeconds() const {
return _ntohl(packet.header.nsec);
}
time_t Packet::seconds() const {
return _ntohl(packet.header.sec);
}
uint32_t Packet::packetSize() const {
return _ntohs(packet.header.size);
}
enum pkt_type Packet::packetType() const {
return (enum pkt_type)packet.header.type;
}
const QString &Packet::packetTypeStr() const {
if (packet.header.type > PacketTypes().count())
return PacketTypes()[0];
return PacketTypes()[packet.header.type];
}
int Packet::setIndex(int index)
{
int o = index;
this->packetIndex = index;
return o;
}
int Packet::index()
{
return packetIndex;
}
uint8_t Packet::nandControl() const {
if (packetType() != PACKET_NAND_CYCLE)
return 0;
return packet.data.nand_cycle.control;
}
uint8_t Packet::nandData() const {
if (packetType() != PACKET_NAND_CYCLE)
return 0;
return nand_unscramble_byte(packet.data.nand_cycle.data);
}
uint16_t Packet::nandUnknown() const {
if (packetType() != PACKET_NAND_CYCLE)
return 0;
return _ntohs(packet.data.nand_cycle.unknown);
}
const QString &Packet::errorStr() const {
return errorString;
}
uint32_t Packet::errorCode() const {
if (packetType() != PACKET_ERROR)
return 0;
return packet.data.error.code;
}
uint32_t Packet::errorArgument() const {
if (packetType() != PACKET_ERROR)
return 0;
return _ntohs(packet.data.error.arg);
}
uint32_t Packet::errorSubsystem() const {
if (packetType() != PACKET_ERROR)
return 0;
return packet.data.error.subsystem;
}
uint8_t Packet::sdRegister() const {
if (packetType() != PACKET_SD_CMD_ARG)
return 0;
return packet.data.sd_cmd_arg.reg;
}
uint8_t Packet::sdValue() const {
if (packetType() != PACKET_SD_CMD_ARG)
return 0;
return packet.data.sd_cmd_arg.val;
}
uint8_t Packet::commandState() const {
if (packetType() != PACKET_COMMAND)
return 0;
return packet.data.command.start_stop;
}
uint32_t Packet::commandArg() const {
if (packetType() != PACKET_COMMAND)
return 0;
return _ntohl(packet.data.command.arg);
}
const QString &Packet::commandName() const {
return commandNameString;
}
uint8_t Packet::helloVersion() const {
if (packetType() != PACKET_HELLO)
return 0;
return packet.data.hello.version;
}
uint8_t Packet::sdResponse() const {
if (packetType() != PACKET_SD_RESPONSE)
return 0;
return packet.data.response.byte;
}
uint8_t Packet::bufferDrainEvent() const {
if (packetType() != PACKET_BUFFER_DRAIN)
return 0;
return packet.data.buffer_drain.start_stop;
}
uint8_t Packet::resetVersion() const {
if (packetType() != PACKET_RESET)
return 0;
return packet.data.reset.version;
}
const QString &Packet::csd() const {
return cardCSD;
}
const QString &Packet::cid() const {
return cardCID;
}
QDebug operator<<(QDebug dbg, const Packet &p)
{
QString timestamp;
QString message;
timestamp.sprintf("<%d.%09d> - ", (int)p.seconds(), (int)p.nanoSeconds());
message = p.packetTypeStr() + " " + timestamp;
switch(p.packetType()) {
case PACKET_HELLO:
QTextStream(&message) << "Hello (v" << p.helloVersion() << ")";
break;
case PACKET_SD_CMD_ARG:
if (p.sdRegister() == 0) {
QTextStream(&message) << "CMD" << (p.sdValue()&0x3f);
}
else {
QTextStream(&message) << "R" << p.sdRegister() <<"=" << p.sdValue();
}
break;
case PACKET_COMMAND:
if (p.commandState() == CMD_START)
QTextStream(&message) << "Start " << p.commandName() << "(" << p.commandArg() << ")";
else if (p.commandState() == CMD_STOP)
QTextStream(&message) << "Finish " << p.commandName() << "(" << p.commandArg() << ")";
else
QTextStream(&message) << "Unknown state for " << p.commandName() << "(" << p.commandArg() << ")";
break;
case PACKET_SD_RESPONSE:
QTextStream(&message) << "First byte of card response: " << QString::number(p.sdResponse(), 16);
break;
case PACKET_NAND_CYCLE: {
QString ctrlString;
uint8_t ctrl = p.nandControl();
QTextStream(&message)
<< QString("%1").arg(p.nandData(), 2, 16, QChar('0'))
<< " " << (ctrl&NAND_ALE?"A":" ")
<< " " << (ctrl&NAND_CLE?"C":" ")
<< " " << (ctrl&NAND_WE?"W":" ")
<< " " << (ctrl&NAND_RE?"R":" ")
<< " " << (ctrl&NAND_CS?"S":" ")
<< " " << (ctrl&NAND_RB?"B":" ")
<< " (" << QString("%1").arg(p.nandUnknown(), 4, 16, QChar('0')) << ")";
}
break;
case PACKET_SD_DATA:
QTextStream(&message) << "512 bytes of data";
break;
case PACKET_ERROR:
QTextStream(&message) << "Subsystem " << p.errorSubsystem() << ", Code " << p.errorCode() << ", Arg " << p.errorArgument() << ", Message: " << p.errorStr();
break;
case PACKET_RESET:
QTextStream(&message) << "Reset (version v" << p.resetVersion() << ")";
break;
case PACKET_BUFFER_DRAIN:
if (p.bufferDrainEvent() == PKT_BUFFER_DRAIN_START)
QTextStream(&message) << "Buffer started draining";
else if (p.bufferDrainEvent() == PKT_BUFFER_DRAIN_STOP)
QTextStream(&message) << "Buffer finished draining";
else
QTextStream(&message) << "Buffer drain did something unknown: " << p.bufferDrainEvent();
break;
case PACKET_SD_CID:
QTextStream(&message) << "Card CID: " << p.cid();
break;
case PACKET_SD_CSD:
QTextStream(&message) << "Card CSD: " << p.csd();
break;
case PACKET_UNKNOWN:
default:
QTextStream(&message) << "Unknown packet";
break;
}
dbg.nospace() << message;
return dbg.space();
}
qint64 Packet::write(QIODevice &device) {
return device.write((const char *)&packet, packetSize());
}