1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
---|
6 | **
|
---|
7 | ** This file is part of the tools applications of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at qt-info@nokia.com.
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "trkutils.h"
|
---|
43 | #include <ctype.h>
|
---|
44 |
|
---|
45 | #include <QtCore/QCoreApplication>
|
---|
46 | #include <QtCore/QDebug>
|
---|
47 | #include <QtCore/QDate>
|
---|
48 | #include <QtCore/QDateTime>
|
---|
49 | #include <QtCore/QTime>
|
---|
50 |
|
---|
51 | #define logMessage(s) do { qDebug() << "TRKCLIENT: " << s; } while (0)
|
---|
52 |
|
---|
53 | namespace trk {
|
---|
54 |
|
---|
55 | TrkAppVersion::TrkAppVersion()
|
---|
56 | {
|
---|
57 | reset();
|
---|
58 | }
|
---|
59 |
|
---|
60 | void TrkAppVersion::reset()
|
---|
61 | {
|
---|
62 | trkMajor = trkMinor= protocolMajor = protocolMinor = 0;
|
---|
63 | }
|
---|
64 |
|
---|
65 | Session::Session()
|
---|
66 | {
|
---|
67 | reset();
|
---|
68 | }
|
---|
69 |
|
---|
70 | void Session::reset()
|
---|
71 | {
|
---|
72 | cpuMajor = 0;
|
---|
73 | cpuMinor = 0;
|
---|
74 | bigEndian = 0;
|
---|
75 | defaultTypeSize = 0;
|
---|
76 | fpTypeSize = 0;
|
---|
77 | extended1TypeSize = 0;
|
---|
78 | extended2TypeSize = 0;
|
---|
79 | pid = 0;
|
---|
80 | tid = 0;
|
---|
81 | codeseg = 0;
|
---|
82 | dataseg = 0;
|
---|
83 |
|
---|
84 | currentThread = 0;
|
---|
85 | libraries.clear();
|
---|
86 | trkAppVersion.reset();
|
---|
87 | }
|
---|
88 |
|
---|
89 | static QString formatCpu(int major, int minor)
|
---|
90 | {
|
---|
91 | //: CPU description of an S60 device
|
---|
92 | //: %1 major verison, %2 minor version
|
---|
93 | //: %3 real name of major verison, %4 real name of minor version
|
---|
94 | const QString str = QCoreApplication::translate("trk::Session", "CPU: v%1.%2%3%4");
|
---|
95 | QString majorStr;
|
---|
96 | QString minorStr;
|
---|
97 | switch (major) {
|
---|
98 | case 0x04:
|
---|
99 | majorStr = " ARM";
|
---|
100 | break;
|
---|
101 | }
|
---|
102 | switch (minor) {
|
---|
103 | case 0x00:
|
---|
104 | minorStr = " 920T";
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | return str.arg(major).arg(minor).arg(majorStr).arg(minorStr);
|
---|
108 | }
|
---|
109 |
|
---|
110 | QString formatTrkVersion(const TrkAppVersion &version)
|
---|
111 | {
|
---|
112 | QString str = QCoreApplication::translate("trk::Session",
|
---|
113 | "App TRK: v%1.%2 TRK protocol: v%3.%4");
|
---|
114 | str = str.arg(version.trkMajor).arg(version.trkMinor);
|
---|
115 | return str.arg(version.protocolMajor).arg(version.protocolMinor);
|
---|
116 | }
|
---|
117 |
|
---|
118 | QString Session::deviceDescription(unsigned verbose) const
|
---|
119 | {
|
---|
120 | if (!cpuMajor)
|
---|
121 | return QString();
|
---|
122 |
|
---|
123 | //: s60description
|
---|
124 | //: description of an S60 device
|
---|
125 | //: %1 CPU description, %2 endianness
|
---|
126 | //: %3 default type size (if any), %4 float size (if any)
|
---|
127 | //: %5 TRK version
|
---|
128 | QString msg = QCoreApplication::translate("trk::Session", "%1, %2%3%4, %5");
|
---|
129 | QString endianness = bigEndian
|
---|
130 | ? QCoreApplication::translate("trk::Session", "big endian")
|
---|
131 | : QCoreApplication::translate("trk::Session", "little endian");
|
---|
132 | msg = msg.arg(formatCpu(cpuMajor, cpuMinor)).arg(endianness);
|
---|
133 | //: The separator in a list of strings
|
---|
134 | QString defaultTypeSizeStr;
|
---|
135 | QString fpTypeSizeStr;
|
---|
136 | if (verbose && defaultTypeSize)
|
---|
137 | //: will be inserted into s60description
|
---|
138 | defaultTypeSizeStr = QCoreApplication::translate("trk::Session", ", type size: %1").arg(defaultTypeSize);
|
---|
139 | if (verbose && fpTypeSize)
|
---|
140 | //: will be inserted into s60description
|
---|
141 | fpTypeSizeStr = QCoreApplication::translate("trk::Session", ", float size: %1").arg(fpTypeSize);
|
---|
142 | msg = msg.arg(defaultTypeSizeStr).arg(fpTypeSizeStr);
|
---|
143 | return msg.arg(formatTrkVersion(trkAppVersion));
|
---|
144 | }
|
---|
145 |
|
---|
146 | // --------------
|
---|
147 |
|
---|
148 | QByteArray decode7d(const QByteArray &ba)
|
---|
149 | {
|
---|
150 | QByteArray res;
|
---|
151 | res.reserve(ba.size());
|
---|
152 | for (int i = 0; i < ba.size(); ++i) {
|
---|
153 | byte c = byte(ba.at(i));
|
---|
154 | if (c == 0x7d) {
|
---|
155 | ++i;
|
---|
156 | c = 0x20 ^ byte(ba.at(i));
|
---|
157 | }
|
---|
158 | res.append(c);
|
---|
159 | }
|
---|
160 | return res;
|
---|
161 | }
|
---|
162 |
|
---|
163 | QByteArray encode7d(const QByteArray &ba)
|
---|
164 | {
|
---|
165 | QByteArray res;
|
---|
166 | res.reserve(ba.size() + 2);
|
---|
167 | for (int i = 0; i < ba.size(); ++i) {
|
---|
168 | byte c = byte(ba.at(i));
|
---|
169 | if (c == 0x7e || c == 0x7d) {
|
---|
170 | res.append(0x7d);
|
---|
171 | res.append(0x20 ^ c);
|
---|
172 | } else {
|
---|
173 | res.append(c);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | return res;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // FIXME: Use the QByteArray based version below?
|
---|
180 | static inline QString stringFromByte(byte c)
|
---|
181 | {
|
---|
182 | return QString::fromLatin1("%1").arg(c, 2, 16, QChar('0'));
|
---|
183 | }
|
---|
184 |
|
---|
185 | SYMBIANUTILS_EXPORT QString stringFromArray(const QByteArray &ba, int maxLen)
|
---|
186 | {
|
---|
187 | QString str;
|
---|
188 | QString ascii;
|
---|
189 | const int size = maxLen == -1 ? ba.size() : qMin(ba.size(), maxLen);
|
---|
190 | for (int i = 0; i < size; ++i) {
|
---|
191 | //if (i == 5 || i == ba.size() - 2)
|
---|
192 | // str += " ";
|
---|
193 | int c = byte(ba.at(i));
|
---|
194 | str += QString("%1 ").arg(c, 2, 16, QChar('0'));
|
---|
195 | if (i >= 8 && i < ba.size() - 2)
|
---|
196 | ascii += QChar(c).isPrint() ? QChar(c) : QChar('.');
|
---|
197 | }
|
---|
198 | if (size != ba.size()) {
|
---|
199 | str += "...";
|
---|
200 | ascii += "...";
|
---|
201 | }
|
---|
202 | return str + " " + ascii;
|
---|
203 | }
|
---|
204 |
|
---|
205 | SYMBIANUTILS_EXPORT QByteArray hexNumber(uint n, int digits)
|
---|
206 | {
|
---|
207 | QByteArray ba = QByteArray::number(n, 16);
|
---|
208 | if (digits == 0 || ba.size() == digits)
|
---|
209 | return ba;
|
---|
210 | return QByteArray(digits - ba.size(), '0') + ba;
|
---|
211 | }
|
---|
212 |
|
---|
213 | SYMBIANUTILS_EXPORT QByteArray hexxNumber(uint n, int digits)
|
---|
214 | {
|
---|
215 | return "0x" + hexNumber(n, digits);
|
---|
216 | }
|
---|
217 |
|
---|
218 | TrkResult::TrkResult() :
|
---|
219 | code(0),
|
---|
220 | token(0),
|
---|
221 | isDebugOutput(false)
|
---|
222 | {
|
---|
223 | }
|
---|
224 |
|
---|
225 | void TrkResult::clear()
|
---|
226 | {
|
---|
227 | code = token= 0;
|
---|
228 | isDebugOutput = false;
|
---|
229 | data.clear();
|
---|
230 | cookie = QVariant();
|
---|
231 | }
|
---|
232 |
|
---|
233 | QString TrkResult::toString() const
|
---|
234 | {
|
---|
235 | QString res = stringFromByte(code);
|
---|
236 | res += QLatin1String(" [");
|
---|
237 | res += stringFromByte(token);
|
---|
238 | res += QLatin1Char(']');
|
---|
239 | res += QLatin1Char(' ');
|
---|
240 | res += stringFromArray(data);
|
---|
241 | return res;
|
---|
242 | }
|
---|
243 |
|
---|
244 | QByteArray frameMessage(byte command, byte token, const QByteArray &data, bool serialFrame)
|
---|
245 | {
|
---|
246 | byte s = command + token;
|
---|
247 | for (int i = 0; i != data.size(); ++i)
|
---|
248 | s += data.at(i);
|
---|
249 | byte checksum = 255 - (s & 0xff);
|
---|
250 | //int x = s + ~s;
|
---|
251 | //logMessage("check: " << s << checksum << x;
|
---|
252 |
|
---|
253 | QByteArray response;
|
---|
254 | response.reserve(data.size() + 3);
|
---|
255 | response.append(char(command));
|
---|
256 | response.append(char(token));
|
---|
257 | response.append(data);
|
---|
258 | response.append(char(checksum));
|
---|
259 |
|
---|
260 | QByteArray encodedData = encode7d(response);
|
---|
261 |
|
---|
262 | QByteArray ba;
|
---|
263 | ba.reserve(encodedData.size() + 6);
|
---|
264 | if (serialFrame) {
|
---|
265 | ba.append(char(0x01));
|
---|
266 | ba.append(char(0x90));
|
---|
267 | const ushort encodedSize = encodedData.size() + 2; // 2 x 0x7e
|
---|
268 | appendShort(&ba, encodedSize, BigEndian);
|
---|
269 | }
|
---|
270 | ba.append(char(0x7e));
|
---|
271 | ba.append(encodedData);
|
---|
272 | ba.append(char(0x7e));
|
---|
273 |
|
---|
274 | return ba;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /* returns 0 if array doesn't represent a result,
|
---|
278 | otherwise returns the length of the result data */
|
---|
279 | ushort isValidTrkResult(const QByteArray &buffer, bool serialFrame)
|
---|
280 | {
|
---|
281 | if (serialFrame) {
|
---|
282 | // Serial protocol with length info
|
---|
283 | if (buffer.length() < 4)
|
---|
284 | return 0;
|
---|
285 | if (buffer.at(0) != 0x01 || byte(buffer.at(1)) != 0x90)
|
---|
286 | return 0;
|
---|
287 | const ushort len = extractShort(buffer.data() + 2);
|
---|
288 | return (buffer.size() >= len + 4) ? len : ushort(0);
|
---|
289 | }
|
---|
290 | // Frameless protocol without length info
|
---|
291 | const char delimiter = char(0x7e);
|
---|
292 | const int firstDelimiterPos = buffer.indexOf(delimiter);
|
---|
293 | // Regular message delimited by 0x7e..0x7e
|
---|
294 | if (firstDelimiterPos == 0) {
|
---|
295 | const int endPos = buffer.indexOf(delimiter, firstDelimiterPos + 1);
|
---|
296 | return endPos != -1 ? endPos + 1 - firstDelimiterPos : 0;
|
---|
297 | }
|
---|
298 | // Some ASCII log message up to first delimiter or all
|
---|
299 | return firstDelimiterPos != -1 ? firstDelimiterPos : buffer.size();
|
---|
300 | }
|
---|
301 |
|
---|
302 | bool extractResult(QByteArray *buffer, bool serialFrame, TrkResult *result, QByteArray *rawData)
|
---|
303 | {
|
---|
304 | result->clear();
|
---|
305 | if(rawData)
|
---|
306 | rawData->clear();
|
---|
307 | const ushort len = isValidTrkResult(*buffer, serialFrame);
|
---|
308 | if (!len)
|
---|
309 | return false;
|
---|
310 | // handle receiving application output, which is not a regular command
|
---|
311 | const int delimiterPos = serialFrame ? 4 : 0;
|
---|
312 | if (buffer->at(delimiterPos) != 0x7e) {
|
---|
313 | result->isDebugOutput = true;
|
---|
314 | result->data = buffer->mid(delimiterPos, len);
|
---|
315 | result->data.replace("\r\n", "\n");
|
---|
316 | *buffer->remove(0, delimiterPos + len);
|
---|
317 | return true;
|
---|
318 | }
|
---|
319 | // FIXME: what happens if the length contains 0xfe?
|
---|
320 | // Assume for now that it passes unencoded!
|
---|
321 | const QByteArray data = decode7d(buffer->mid(delimiterPos + 1, len - 2));
|
---|
322 | if(rawData)
|
---|
323 | *rawData = data;
|
---|
324 | *buffer->remove(0, delimiterPos + len);
|
---|
325 |
|
---|
326 | byte sum = 0;
|
---|
327 | for (int i = 0; i < data.size(); ++i) // 3 = 2 * 0xfe + sum
|
---|
328 | sum += byte(data.at(i));
|
---|
329 | if (sum != 0xff)
|
---|
330 | logMessage("*** CHECKSUM ERROR: " << byte(sum));
|
---|
331 |
|
---|
332 | result->code = data.at(0);
|
---|
333 | result->token = data.at(1);
|
---|
334 | result->data = data.mid(2, data.size() - 3);
|
---|
335 | //logMessage(" REST BUF: " << stringFromArray(*buffer));
|
---|
336 | //logMessage(" CURR DATA: " << stringFromArray(data));
|
---|
337 | //QByteArray prefix = "READ BUF: ";
|
---|
338 | //logMessage((prefix + "HEADER: " + stringFromArray(header).toLatin1()).data());
|
---|
339 | return true;
|
---|
340 | }
|
---|
341 |
|
---|
342 | SYMBIANUTILS_EXPORT ushort extractShort(const char *data)
|
---|
343 | {
|
---|
344 | return byte(data[0]) * 256 + byte(data[1]);
|
---|
345 | }
|
---|
346 |
|
---|
347 | SYMBIANUTILS_EXPORT uint extractInt(const char *data)
|
---|
348 | {
|
---|
349 | uint res = byte(data[0]);
|
---|
350 | res *= 256; res += byte(data[1]);
|
---|
351 | res *= 256; res += byte(data[2]);
|
---|
352 | res *= 256; res += byte(data[3]);
|
---|
353 | return res;
|
---|
354 | }
|
---|
355 |
|
---|
356 | SYMBIANUTILS_EXPORT QString quoteUnprintableLatin1(const QByteArray &ba)
|
---|
357 | {
|
---|
358 | QString res;
|
---|
359 | char buf[10];
|
---|
360 | for (int i = 0, n = ba.size(); i != n; ++i) {
|
---|
361 | const byte c = ba.at(i);
|
---|
362 | if (isprint(c)) {
|
---|
363 | res += c;
|
---|
364 | } else {
|
---|
365 | qsnprintf(buf, sizeof(buf) - 1, "\\%x", int(c));
|
---|
366 | res += buf;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | return res;
|
---|
370 | }
|
---|
371 |
|
---|
372 | SYMBIANUTILS_EXPORT void appendShort(QByteArray *ba, ushort s, Endianness endian)
|
---|
373 | {
|
---|
374 | if (endian == BigEndian) {
|
---|
375 | ba->append(s / 256);
|
---|
376 | ba->append(s % 256);
|
---|
377 | } else {
|
---|
378 | ba->append(s % 256);
|
---|
379 | ba->append(s / 256);
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | SYMBIANUTILS_EXPORT void appendInt(QByteArray *ba, uint i, Endianness endian)
|
---|
384 | {
|
---|
385 | const uchar b3 = i % 256; i /= 256;
|
---|
386 | const uchar b2 = i % 256; i /= 256;
|
---|
387 | const uchar b1 = i % 256; i /= 256;
|
---|
388 | const uchar b0 = i;
|
---|
389 | ba->reserve(ba->size() + 4);
|
---|
390 | if (endian == BigEndian) {
|
---|
391 | ba->append(b0);
|
---|
392 | ba->append(b1);
|
---|
393 | ba->append(b2);
|
---|
394 | ba->append(b3);
|
---|
395 | } else {
|
---|
396 | ba->append(b3);
|
---|
397 | ba->append(b2);
|
---|
398 | ba->append(b1);
|
---|
399 | ba->append(b0);
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | void appendString(QByteArray *ba, const QByteArray &str, Endianness endian, bool appendNullTerminator)
|
---|
404 | {
|
---|
405 | const int fullSize = str.size() + (appendNullTerminator ? 1 : 0);
|
---|
406 | appendShort(ba, fullSize, endian); // count the terminating \0
|
---|
407 | ba->append(str);
|
---|
408 | if (appendNullTerminator)
|
---|
409 | ba->append('\0');
|
---|
410 | }
|
---|
411 |
|
---|
412 | void appendDateTime(QByteArray *ba, QDateTime dateTime, Endianness endian)
|
---|
413 | {
|
---|
414 | // convert the QDateTime to UTC and append its representation to QByteArray
|
---|
415 | // format is the same as in FAT file system
|
---|
416 | dateTime = dateTime.toUTC();
|
---|
417 | const QTime utcTime = dateTime.time();
|
---|
418 | const QDate utcDate = dateTime.date();
|
---|
419 | uint fatDateTime = (utcTime.hour() << 11 | utcTime.minute() << 5 | utcTime.second()/2) << 16;
|
---|
420 | fatDateTime |= (utcDate.year()-1980) << 9 | utcDate.month() << 5 | utcDate.day();
|
---|
421 | appendInt(ba, fatDateTime, endian);
|
---|
422 | }
|
---|
423 |
|
---|
424 | QByteArray errorMessage(byte code)
|
---|
425 | {
|
---|
426 | switch (code) {
|
---|
427 | case 0x00: return "No error";
|
---|
428 | case 0x01: return "Generic error in CWDS message";
|
---|
429 | case 0x02: return "Unexpected packet size in send msg";
|
---|
430 | case 0x03: return "Internal error occurred in CWDS";
|
---|
431 | case 0x04: return "Escape followed by frame flag";
|
---|
432 | case 0x05: return "Bad FCS in packet";
|
---|
433 | case 0x06: return "Packet too long";
|
---|
434 | case 0x07: return "Sequence ID not expected (gap in sequence)";
|
---|
435 |
|
---|
436 | case 0x10: return "Command not supported";
|
---|
437 | case 0x11: return "Command param out of range";
|
---|
438 | case 0x12: return "An option was not supported";
|
---|
439 | case 0x13: return "Read/write to invalid memory";
|
---|
440 | case 0x14: return "Read/write invalid registers";
|
---|
441 | case 0x15: return "Exception occurred in CWDS";
|
---|
442 | case 0x16: return "Targeted system or thread is running";
|
---|
443 | case 0x17: return "Breakpoint resources (HW or SW) exhausted";
|
---|
444 | case 0x18: return "Requested breakpoint conflicts with existing one";
|
---|
445 |
|
---|
446 | case 0x20: return "General OS-related error";
|
---|
447 | case 0x21: return "Request specified invalid process";
|
---|
448 | case 0x22: return "Request specified invalid thread";
|
---|
449 | }
|
---|
450 | return "Unknown error";
|
---|
451 | }
|
---|
452 |
|
---|
453 | uint swapEndian(uint in)
|
---|
454 | {
|
---|
455 | return (in>>24) | ((in<<8) & 0x00FF0000) | ((in>>8) & 0x0000FF00) | (in<<24);
|
---|
456 | }
|
---|
457 |
|
---|
458 | int TrkResult::errorCode() const
|
---|
459 | {
|
---|
460 | // NAK means always error, else data sized 1 with a non-null element
|
---|
461 | const bool isNAK = code == 0xff;
|
---|
462 | if (data.size() != 1 && !isNAK)
|
---|
463 | return 0;
|
---|
464 | if (const int errorCode = data.at(0))
|
---|
465 | return errorCode;
|
---|
466 | return isNAK ? 0xff : 0;
|
---|
467 | }
|
---|
468 |
|
---|
469 | QString TrkResult::errorString() const
|
---|
470 | {
|
---|
471 | // NAK means always error, else data sized 1 with a non-null element
|
---|
472 | if (code == 0xff)
|
---|
473 | return "NAK";
|
---|
474 | if (data.size() < 1)
|
---|
475 | return "Unknown error packet";
|
---|
476 | return errorMessage(data.at(0));
|
---|
477 | }
|
---|
478 |
|
---|
479 | } // namespace trk
|
---|
480 |
|
---|