-
Notifications
You must be signed in to change notification settings - Fork 13
/
otau_file.cpp
268 lines (222 loc) · 6.77 KB
/
otau_file.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
#include <QDataStream>
#include <deconz.h>
#include "otau_file.h"
#define MANDATORY_HEADER_LENGTH 56
#define SEGMENT_HEADER_LENGTH 6
/*! The constructor.
*/
OtauFile::OtauFile()
{
upgradeFileId = 0x0BEEF11E;
headerVersion = 0x0100;
headerLength = 0;
headerFieldControl = 0x0000;
manufacturerCode = 0x0000;
imageType = 0;
fileVersion = 0;
zigBeeStackVersion = 0x0002; // ZigBee PRO
totalImageSize = 0;
securityCredentialVersion = 0;
upgradeFileDestination = 0;
minHardwareVersion = 0;
maxHardwareVersion = 0;
}
/*! Packs the file in a byte array.
\return the array holding the otau file
*/
QByteArray OtauFile::toArray()
{
headerLength = MANDATORY_HEADER_LENGTH; // fixed length
if (headerFieldControl & OF_FC_SECURITY_CREDENTIAL_VERSION)
{
headerLength += 1;
}
if (headerFieldControl & OF_FC_DEVICE_SPECIFIC)
{
headerLength += 8; // mac address
}
if (headerFieldControl & OF_FC_HARDWARE_VERSION)
{
headerLength += 2; // min hw version
headerLength += 2; // max hw version
}
totalImageSize = headerLength;
{
auto it = subElements.begin();
auto end = subElements.end();
for (;it != end; ++it)
{
totalImageSize += (2 + 4); // fixed tag size
totalImageSize += (uint32_t)it->data.size();
}
}
QByteArray arr;
QDataStream stream(&arr, QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << upgradeFileId;
stream << headerVersion;
stream << headerLength;
stream << headerFieldControl;
stream << manufacturerCode;
stream << imageType;
stream << fileVersion;
stream << zigBeeStackVersion;
for (uint i = 0; i < 32; i++)
{
stream << headerString[i];
}
stream << totalImageSize;
if (headerFieldControl & OF_FC_SECURITY_CREDENTIAL_VERSION)
{
stream << securityCredentialVersion;
}
if (headerFieldControl & OF_FC_DEVICE_SPECIFIC)
{
stream << upgradeFileDestination;
}
if (headerFieldControl & OF_FC_HARDWARE_VERSION)
{
stream << minHardwareVersion;
stream << maxHardwareVersion;
}
DBG_Printf(DBG_OTA, "OTAU: %s: %d bytes\n", qPrintable(path), totalImageSize);
DBG_Printf(DBG_OTA, "OTAU: ota header (%u bytes)\n", headerLength);
// append tags
{
auto it = subElements.begin();
auto end = subElements.end();
for (;it != end; ++it)
{
stream << it->tag;
stream << it->length;
DBG_Printf(DBG_OTA, "OTAU: tag 0x%04X, length 0x%08X (%d bytes)\n", it->tag, it->length, it->data.size() + SEGMENT_HEADER_LENGTH);
for (int i = 0; i < it->data.size(); i++)
{
stream << (uint8_t)it->data[i];
}
}
}
DBG_Printf(DBG_OTA, "OTAU: packed %d bytes\n", arr.length());
return arr;
}
/*! Reads the otau file from a byte array.
\return true on success or false false if no valid data was found
*/
bool OtauFile::fromArray(const QByteArray &arr)
{
DBG_Printf(DBG_OTA, "OTAU: %s: %d bytes\n", qPrintable(path), arr.length());
if (arr.size() < MANDATORY_HEADER_LENGTH)
{
DBG_Printf(DBG_OTA, "OTAU: %s: not an ota file (too small)\n", qPrintable(path));
return false;
}
const char *hdr = "\x1e\xf1\xee\x0b";
int offset = arr.indexOf(hdr);
if (offset < 0)
{
DBG_Printf(DBG_OTA, "OTAU: %s: not an ota file (header not found)\n", qPrintable(path));
return false;
}
uint processedLength = 0;
QDataStream stream(arr);
stream.setByteOrder(QDataStream::LittleEndian);
for (int i = 0; i < offset; i++)
{
quint8 dummy;
stream >> dummy;
}
stream >> upgradeFileId;
stream >> headerVersion;
stream >> headerLength;
if (headerLength < MANDATORY_HEADER_LENGTH)
{
DBG_Printf(DBG_OTA, "OTAU: %s: not an ota file (invalid header length)\n", qPrintable(path));
return false;
}
stream >> headerFieldControl;
stream >> manufacturerCode;
stream >> imageType;
stream >> fileVersion;
stream >> zigBeeStackVersion;
for (uint i = 0; i < sizeof(headerString); i++)
{
stream >> headerString[i];
}
stream >> totalImageSize;
processedLength = MANDATORY_HEADER_LENGTH;
if (headerFieldControl & OF_FC_SECURITY_CREDENTIAL_VERSION)
{
stream >> securityCredentialVersion;
processedLength += 1;
}
if (headerFieldControl & OF_FC_DEVICE_SPECIFIC)
{
stream >> upgradeFileDestination;
processedLength += 8;
}
if (headerFieldControl & OF_FC_HARDWARE_VERSION)
{
stream >> minHardwareVersion;
stream >> maxHardwareVersion;
processedLength += 4;
}
// discard optional fields of header
for (quint16 i = 0; headerLength > processedLength; i++)
{
quint8 dummy;
stream >> dummy;
processedLength++;
}
DBG_Printf(DBG_OTA, "OTAU: offset %6d: ota header (%u bytes)\n", offset, processedLength);
// read tags
subElements.clear();
while (!stream.atEnd())
{
SubElement sub;
int size = 0;
int start = offset + processedLength;
stream >> sub.tag;
processedLength += 2;
stream >> sub.length;
processedLength += 4;
if (stream.atEnd())
{
break;
}
if (sub.length > arr.size() - offset - processedLength)
{
size = arr.size() - offset - processedLength;
}
else
{
size = sub.length;
}
sub.data.resize(size);
if (stream.readRawData(sub.data.data(), size) == size)
{
processedLength += size;
}
if (sub.data.size() == size)
{
subElements.push_back(sub);
DBG_Printf(DBG_OTA, "OTAU: offset %6u: tag 0x%04X, length 0x%08X (%d bytes)\n", start, sub.tag, sub.length, size + SEGMENT_HEADER_LENGTH);
}
else
{
DBG_Printf(DBG_OTA, "OTAU: offset %6u: ignore tag 0x%04X with invalid length\n", start, sub.tag);
break; // unkown what current data is
}
// Total data process = totalImageSize, skip next segments, used only for legrand ATM
if ((manufacturerCode == 0x1021) && (processedLength == totalImageSize))
{
DBG_Printf(DBG_OTA, "OTAU: Total Image size reached, skip next segments\n");
break;
}
}
if (!stream.atEnd())
{
DBG_Printf(DBG_OTA, "OTAU: offset %6u: ignore trailing %d bytes\n", offset + processedLength, arr.size() - offset - processedLength);
}
raw = arr.mid(offset, totalImageSize);
return !subElements.empty();
}