-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparseRecord.cpp
274 lines (253 loc) · 9.72 KB
/
parseRecord.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
/**********
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**********/
/*
A C++ program to parse DJI's ".txt" log files (recorded by the "DJI Go 4" app).
Version 2019-02-08
Copyright (c) 2019 Live Networks, Inc. All rights reserved.
For the latest version of this program (and more information), visit http://djilogs.live555.com
Parsing records within DJI ".txt" files.
Implementation.
*/
#include "RecordAndDetailsParser.hh"
#include <stdio.h>
#include <string.h>
#define RECORD_TYPE_OSD 0x01
#define RECORD_TYPE_HOME 0x02
#define RECORD_TYPE_GIMBAL 0x03
#define RECORD_TYPE_RC 0x04
#define RECORD_TYPE_CUSTOM 0x05
#define RECORD_TYPE_DEFORM 0x06
#define RECORD_TYPE_CENTER_BATTERY 0x07
#define RECORD_TYPE_SMART_BATTERY 0x08
#define RECORD_TYPE_APP_TIP 0x09
#define RECORD_TYPE_APP_WARN 0x0A
#define RECORD_TYPE_RC_GPS 0x0B
#define RECORD_TYPE_RC_DEBUG 0x0C
#define RECORD_TYPE_RECOVER 0x0D
#define RECORD_TYPE_APP_GPS 0x0E
#define RECORD_TYPE_FIRMWARE 0x0F
#define RECORD_TYPE_OFDM_DEBUG 0x10
#define RECORD_TYPE_VISION_GROUP 0x11
#define RECORD_TYPE_VISION_WARN 0x12
#define RECORD_TYPE_MC_PARAM 0x13
#define RECORD_TYPE_APP_OPERATION 0x14
// What is record type 0x16? #####
#define RECORD_TYPE_APP_SER_WARN 0x18
// What is record type 0x19? #####
// What is record type 0x1a? #####
// What is record type 0x1e? #####
// What is record type 0x28? #####
#define RECORD_TYPE_JPEG 0x39
#define RECORD_TYPE_OTHER 0xFE
#define JPEG_SOI_BYTE 0xD8
int RecordAndDetailsParser::parseRecord(u_int8_t const*& ptr, u_int8_t const* limit, int isScrambled) {
// Attempt to parse a record; Returns 1 iff it succeeds.
try {
// The first two bytes are the 'record type' and the 'record length':
u_int8_t recordType = getByte(ptr, limit);
u_int8_t recordLength = getByte(ptr, limit);
// Record statistics about the record type and length:
++fNumRecords;
RecordTypeStat& stat = fRecordTypeStats[recordType]; // alias
++stat.count;
if (stat.count > fMaxNumRecordsForOneType) fMaxNumRecordsForOneType = stat.count;
if (recordLength < stat.minLength) stat.minLength = recordLength;
if (recordLength > stat.maxLength) stat.maxLength = recordLength;
#ifdef DEBUG_RECORD_PARSING
char const* recordTypeName = fRecordTypeName[recordType];
if (recordTypeName == NULL) recordTypeName = "???";
fprintf(stderr, "[%d]\trecordType %d[%s], recordLength %d\n", fRecordTypeStats[RECORD_TYPE_OSD].count, recordType, recordTypeName, recordLength);
#endif
if (recordType == RECORD_TYPE_JPEG) {
// This record contains one or more JPEG images, and needs to be handled especially.
// (In particular, the 'recordLength' seems to be irrelevant in this case)
return parseRecord_JPEG(ptr, limit);
} else if (recordType == 0xFF && recordLength == JPEG_SOI_BYTE) {
// Some old log formats start JPEG images this way. Back up 4 bytes; handle them the same way:
ptr -=4;
return parseRecord_JPEG(ptr, limit);
}
// Check the record length, and whether there's a 0xFF byte at the end:
if (ptr + recordLength + 1 > limit) throw END_OF_DATA;
if (ptr[recordLength] != 0xFF) {
fprintf(stderr, "'End of record' byte not seen\n");
return 0;
}
u_int8_t const* recordStart = ptr;
u_int8_t const* recordLimit = ptr + recordLength; // position of the 0xFF 'End of record' byte
ptr += recordLength + 1; // advance to the next record, if any
u_int8_t unscrambledRecord[recordLength-1]; // used only if "isScrambled"
if (isScrambled) {
// We need to unscramble the record data before we can parse it.
// The next byte (along with the 'record type') is an index into the 'scramble table':
u_int8_t keyIndexLowByte = getByte(recordStart, limit);
u_int16_t scrambleTableIndex = ((recordType-1)<<8)|keyIndexLowByte;
--recordLength;
if (scrambleTableIndex >= 0x1000) {
// Our current 'scramble table' is not large enough to handle this record type. #####
fprintf(stderr, "WARNING: for record type 0x%02x", recordType);
#ifdef DEBUG_RECORD_PARSING
fprintf(stderr, "[%s]", recordTypeName);
#endif
fprintf(stderr, ", scrambleTableIndex 0x%x is too large (>0x1000) for our current 'scramble table'; we can't unscramble this data!\n", scrambleTableIndex);
} else {
// Normal case: We know how to unscramble this record's data:
extern u_int8_t const scrambleTable[0x1000][8];
u_int8_t const* scrambleBytes = scrambleTable[scrambleTableIndex]; // an array of 8 bytes
// Unscramble each byte in the record by XORing it with the 'scrambleBytes':
for (unsigned i = 0; i < recordLength; ++i) {
unscrambledRecord[i] = recordStart[i] ^ scrambleBytes[i%8];
}
recordStart = unscrambledRecord;
recordLimit = unscrambledRecord + recordLength;
}
}
switch (recordType) {
case RECORD_TYPE_OSD: {
// Because an 'OSD' record effectively starts a new row of data, output a row of data
// before we parse it (except for the very first 'OSD' record, where we output
// the column labels instead):
outputOneRow(fRecordTypeStats[RECORD_TYPE_OSD].count == 1);
parseRecord_OSD(recordStart, recordLimit);
break;
}
case RECORD_TYPE_HOME: {
parseRecord_HOME(recordStart, recordLimit);
break;
}
case RECORD_TYPE_GIMBAL: {
parseRecord_GIMBAL(recordStart, recordLimit);
break;
}
case RECORD_TYPE_RC: {
parseRecord_RC(recordStart, recordLimit);
break;
}
case RECORD_TYPE_CUSTOM: {
parseRecord_CUSTOM(recordStart, recordLimit);
break;
}
case RECORD_TYPE_DEFORM: {
parseRecord_DEFORM(recordStart, recordLimit);
break;
}
case RECORD_TYPE_CENTER_BATTERY: {
parseRecord_CENTER_BATTERY(recordStart, recordLimit);
break;
}
case RECORD_TYPE_SMART_BATTERY: {
parseRecord_SMART_BATTERY(recordStart, recordLimit);
break;
}
case RECORD_TYPE_APP_TIP: {
parseRecord_APP_TIP(recordStart, recordLimit);
break;
}
case RECORD_TYPE_APP_WARN: {
parseRecord_APP_WARN(recordStart, recordLimit);
break;
}
case RECORD_TYPE_RC_GPS: {
parseRecordUnknownFormat("RC_GPS", recordStart, recordLimit);
break;
}
case RECORD_TYPE_RC_DEBUG: {
parseRecordUnknownFormat("RC_DEBUG", recordStart, recordLimit);
break;
}
case RECORD_TYPE_RECOVER: {
parseRecord_RECOVER(recordStart, recordLimit);
break;
}
case RECORD_TYPE_APP_GPS: {
parseRecord_APP_GPS(recordStart, recordLimit);
break;
}
case RECORD_TYPE_FIRMWARE: {
parseRecord_FIRMWARE(recordStart, recordLimit);
break;
}
case RECORD_TYPE_OFDM_DEBUG: {
parseRecordUnknownFormat("OFDM_DEBUG", recordStart, recordLimit);
break;
}
case RECORD_TYPE_VISION_GROUP: {
parseRecordUnknownFormat("VISION_GROUP", recordStart, recordLimit);
break;
}
case RECORD_TYPE_VISION_WARN: {
parseRecordUnknownFormat("VISION_WARN", recordStart, recordLimit);
break;
}
case RECORD_TYPE_MC_PARAM: {
parseRecordUnknownFormat("MC_PARAM", recordStart, recordLimit);
break;
}
case RECORD_TYPE_APP_OPERATION: {
parseRecordUnknownFormat("APP_OPERATION", recordStart, recordLimit);
break;
}
case RECORD_TYPE_APP_SER_WARN: {
parseRecordUnknownFormat("APP_SER_WARN", recordStart, recordLimit);
break;
}
default: {
#ifdef DEBUG_RECORD_PARSING
char const* recordTypeName = fRecordTypeName[recordType];
if (recordTypeName == NULL) {
fprintf(stderr, "Unknown record type 0x%02x\n", recordType);
} else {
fprintf(stderr, "Unhandled record type 0x%02x [%s]\n", recordType, recordTypeName);
}
#else
fprintf(stderr, "Unhandled record type 0x%02x\n", recordType);
#endif
}
}
} catch (int /*e*/) {
fprintf(stderr, "Unexpected error in parsing\n");
return 0;
}
return 1;
}
void RecordAndDetailsParser::summarizeRecordParsing() {
#ifdef DEBUG_RECORD_PARSING
fprintf(stderr, "%d records parsed; max num records for one type: %d\n", fNumRecords, fMaxNumRecordsForOneType);
unsigned maxRecordTypeFieldLen = 0;
for (unsigned i = 0; i < 256; ++i) {
char const* recordTypeName = fRecordTypeName[i];
unsigned recordTypeNameLen = recordTypeName == NULL ? 3 : strlen(recordTypeName);
unsigned iLog10 = i<10 ? 0 : i<100 ? 1 : 2;
unsigned recordTypeFieldLen = iLog10 + 2 + recordTypeNameLen + 2;
if (recordTypeFieldLen > maxRecordTypeFieldLen) maxRecordTypeFieldLen = recordTypeFieldLen;
}
unsigned maxNumTabs = maxRecordTypeFieldLen/8 + 1;
for (unsigned i = 0; i < 255; ++i) {
if (fRecordTypeStats[i].count > 0) {
char const* recordTypeName = fRecordTypeName[i];
if (recordTypeName == NULL) recordTypeName = "???";
unsigned iLog10 = i<10 ? 0 : i<100 ? 1 : 2;
unsigned recordTypeFieldLen = iLog10 + 2 + strlen(recordTypeName) + 2;
unsigned numTabs = maxNumTabs - recordTypeFieldLen/8; // >0
fprintf(stderr, "%d[%s]:", i, recordTypeName);
for (unsigned j = 0; j < numTabs; ++j) fprintf(stderr, "\t");
fprintf(stderr, "%d\t", fRecordTypeStats[i].count);
if (fRecordTypeStats[i].minLength == fRecordTypeStats[i].maxLength) {
fprintf(stderr, "length:\t\t%d\n", fRecordTypeStats[i].minLength);
} else {
fprintf(stderr, "lengths:\t%d-%d\n", fRecordTypeStats[i].minLength, fRecordTypeStats[i].maxLength);
}
}
}
#endif
}