123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /****************************************************************************
- **
- ** Copyright (C) 2013 Jarek Pelczar <jpelczar@gmail.com>
- ** Copyright (C) 2014 Tomasz Olszak <olszak.tomasz@gmail.com>
- **
- ** This file is part of Qt Creator.
- **
- ** Commercial License Usage
- ** Licensees holding valid commercial Qt licenses may use this file in
- ** accordance with the commercial license agreement provided with the
- ** Software or, alternatively, in accordance with the terms contained in
- ** a written agreement between you and Digia. For licensing terms and
- ** conditions see http://qt.digia.com/licensing. For further information
- ** use the contact form at http://qt.digia.com/contact-us.
- **
- ** GNU Lesser General Public License Usage
- ** Alternatively, this file may be used under the terms of the GNU Lesser
- ** General Public License version 2.1 as published by the Free Software
- ** Foundation and appearing in the file LICENSE.LGPL included in the
- ** packaging of this file. Please review the following information to
- ** ensure the GNU Lesser General Public License version 2.1 requirements
- ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- **
- ** In addition, as a special exception, Digia gives you certain additional
- ** rights. These rights are described in the Digia Qt LGPL Exception
- ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
- **
- ****************************************************************************/
- #include "tizendebug.h"
- #include "sdbpacketutils.h"
- #include <QString>
- #include <QByteArray>
- #include <QIODevice>
- #include <stdio.h>
- namespace Tizen {
- namespace Internal {
- const QLatin1String ForwardCreateMessage("host-serial:%1:forward:tcp:%2;tcp:%3");
- const QLatin1String ForwardDeleteMessage("host-serial:%1:killforward:tcp:%2;tcp:%3");
- const QLatin1String InitializeDeviceMessage("host:transport:%1");
- QByteArray SdbPacketUtils::makeRequest(const QString& string)
- {
- QByteArray result;
- QByteArray utf8 = string.toUtf8();
- result.reserve(4 + utf8.length());
- char length_code[16];
- qsnprintf(length_code, sizeof(length_code) - 1, "%04X", utf8.length());
- result.reserve(4 + string.length());
- result.append(length_code);
- result.append(utf8);
- return result;
- }
- QByteArray SdbPacketUtils::makeRequest(const QByteArray& string)
- {
- QByteArray result;
- result.reserve(4 + string.length());
- char length_code[16];
- qsnprintf(length_code, sizeof(length_code) - 1, "%04X", string.length());
- result.reserve(4 + string.length());
- result.append(length_code);
- result.append(string);
- return result;
- }
- bool SdbPacketUtils::parseResponse(const QByteArray& source, DiagnosticOutputHandling readDiagnosticString, SdbResponse& response)
- {
- if(source.length() < 4)
- return false;
- if(source[0] == 'O' && source[1] == 'K' && source[2] == 'A' && source[3] == 'Y') {
- response.okay = true;
- } else {
- response.okay = false;
- readDiagnosticString = ReadDiagnosticString;
- }
- response.length_read = 4;
- if(readDiagnosticString == ReadDiagnosticString) {
- if(source.length() < 8)
- return false;
- char packet_length[5];
- memcpy(packet_length, source.constData() + 4, 4);
- packet_length[4] = 0;
- int message_length = 0;
- sscanf(packet_length, "%X", &message_length);
- if((message_length + 8) < source.length())
- return false;
- response.message = QString::fromUtf8(source.constData() + 8, message_length);
- response.length_read += 4 + message_length;
- }
- return true;
- }
- bool SdbPacketUtils::parseResponse(QIODevice * source, DiagnosticOutputHandling readDiagnosticString, SdbResponse& response)
- {
- qCDebug(QTC_TIZEN_SDBCONNECTOR) << "source->bytesAvailable():" << source->bytesAvailable();
- if(source->bytesAvailable() < 4)
- return false;
- char hdr[4];
- source->peek(hdr, 4);
- if(hdr[0] == 'O' && hdr[1] == 'K' && hdr[2] == 'A' && hdr[3] == 'Y') {
- response.okay = true;
- } else {
- response.okay = false;
- readDiagnosticString = ReadDiagnosticString;
- }
- response.length_read = 4;
- if(readDiagnosticString == ReadDiagnosticString) {
- if(source->bytesAvailable() < 8)
- return false;
- char packet_length[9];
- source->peek(packet_length, 8);
- packet_length[8] = 0;
- int message_length = 0;
- sscanf(packet_length, "%X", &message_length);
- if((message_length + 8) < source->bytesAvailable())
- return false;
- // Discard
- source->read(packet_length, 8);
- QByteArray message = source->read(message_length);
- response.message = QString::fromUtf8(message.constData(), message.length());
- response.length_read += 4 + message_length;
- } else {
- // Discard
- source->read(hdr, 4);
- }
- return true;
- }
- QByteArray SdbPacketUtils::makeCreateForwardMessage(QString deviceSerial, int localPort, int remotePort)
- {
- return makeRequest(QString(ForwardCreateMessage).arg(deviceSerial).arg(localPort).arg(remotePort));
- }
- QByteArray SdbPacketUtils::makeDeleteForwardMessage(QString deviceSerial, int localPort, int remotePort)
- {
- return makeRequest(QString(ForwardDeleteMessage).arg(deviceSerial).arg(localPort).arg(remotePort));
- }
- QByteArray SdbPacketUtils::makeInitializeDeviceMessage(QString deviceSerial)
- {
- return makeRequest(QString(InitializeDeviceMessage).arg(deviceSerial));
- }
- } // namespace Internal
- } // namespace Tizen
|