sdbpacketutils.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Jarek Pelczar <jpelczar@gmail.com>
  4. ** Copyright (C) 2014 Tomasz Olszak <olszak.tomasz@gmail.com>
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and Digia. For licensing terms and
  13. ** conditions see http://qt.digia.com/licensing. For further information
  14. ** use the contact form at http://qt.digia.com/contact-us.
  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, Digia gives you certain additional
  25. ** rights. These rights are described in the Digia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ****************************************************************************/
  29. #include "tizendebug.h"
  30. #include "sdbpacketutils.h"
  31. #include <QString>
  32. #include <QByteArray>
  33. #include <QIODevice>
  34. #include <stdio.h>
  35. namespace Tizen {
  36. namespace Internal {
  37. const QLatin1String ForwardCreateMessage("host-serial:%1:forward:tcp:%2;tcp:%3");
  38. const QLatin1String ForwardDeleteMessage("host-serial:%1:killforward:tcp:%2;tcp:%3");
  39. const QLatin1String InitializeDeviceMessage("host:transport:%1");
  40. QByteArray SdbPacketUtils::makeRequest(const QString& string)
  41. {
  42. QByteArray result;
  43. QByteArray utf8 = string.toUtf8();
  44. result.reserve(4 + utf8.length());
  45. char length_code[16];
  46. qsnprintf(length_code, sizeof(length_code) - 1, "%04X", utf8.length());
  47. result.reserve(4 + string.length());
  48. result.append(length_code);
  49. result.append(utf8);
  50. return result;
  51. }
  52. QByteArray SdbPacketUtils::makeRequest(const QByteArray& string)
  53. {
  54. QByteArray result;
  55. result.reserve(4 + string.length());
  56. char length_code[16];
  57. qsnprintf(length_code, sizeof(length_code) - 1, "%04X", string.length());
  58. result.reserve(4 + string.length());
  59. result.append(length_code);
  60. result.append(string);
  61. return result;
  62. }
  63. bool SdbPacketUtils::parseResponse(const QByteArray& source, DiagnosticOutputHandling readDiagnosticString, SdbResponse& response)
  64. {
  65. if(source.length() < 4)
  66. return false;
  67. if(source[0] == 'O' && source[1] == 'K' && source[2] == 'A' && source[3] == 'Y') {
  68. response.okay = true;
  69. } else {
  70. response.okay = false;
  71. readDiagnosticString = ReadDiagnosticString;
  72. }
  73. response.length_read = 4;
  74. if(readDiagnosticString == ReadDiagnosticString) {
  75. if(source.length() < 8)
  76. return false;
  77. char packet_length[5];
  78. memcpy(packet_length, source.constData() + 4, 4);
  79. packet_length[4] = 0;
  80. int message_length = 0;
  81. sscanf(packet_length, "%X", &message_length);
  82. if((message_length + 8) < source.length())
  83. return false;
  84. response.message = QString::fromUtf8(source.constData() + 8, message_length);
  85. response.length_read += 4 + message_length;
  86. }
  87. return true;
  88. }
  89. bool SdbPacketUtils::parseResponse(QIODevice * source, DiagnosticOutputHandling readDiagnosticString, SdbResponse& response)
  90. {
  91. qCDebug(QTC_TIZEN_SDBCONNECTOR) << "source->bytesAvailable():" << source->bytesAvailable();
  92. if(source->bytesAvailable() < 4)
  93. return false;
  94. char hdr[4];
  95. source->peek(hdr, 4);
  96. if(hdr[0] == 'O' && hdr[1] == 'K' && hdr[2] == 'A' && hdr[3] == 'Y') {
  97. response.okay = true;
  98. } else {
  99. response.okay = false;
  100. readDiagnosticString = ReadDiagnosticString;
  101. }
  102. response.length_read = 4;
  103. if(readDiagnosticString == ReadDiagnosticString) {
  104. if(source->bytesAvailable() < 8)
  105. return false;
  106. char packet_length[9];
  107. source->peek(packet_length, 8);
  108. packet_length[8] = 0;
  109. int message_length = 0;
  110. sscanf(packet_length, "%X", &message_length);
  111. if((message_length + 8) < source->bytesAvailable())
  112. return false;
  113. // Discard
  114. source->read(packet_length, 8);
  115. QByteArray message = source->read(message_length);
  116. response.message = QString::fromUtf8(message.constData(), message.length());
  117. response.length_read += 4 + message_length;
  118. } else {
  119. // Discard
  120. source->read(hdr, 4);
  121. }
  122. return true;
  123. }
  124. QByteArray SdbPacketUtils::makeCreateForwardMessage(QString deviceSerial, int localPort, int remotePort)
  125. {
  126. return makeRequest(QString(ForwardCreateMessage).arg(deviceSerial).arg(localPort).arg(remotePort));
  127. }
  128. QByteArray SdbPacketUtils::makeDeleteForwardMessage(QString deviceSerial, int localPort, int remotePort)
  129. {
  130. return makeRequest(QString(ForwardDeleteMessage).arg(deviceSerial).arg(localPort).arg(remotePort));
  131. }
  132. QByteArray SdbPacketUtils::makeInitializeDeviceMessage(QString deviceSerial)
  133. {
  134. return makeRequest(QString(InitializeDeviceMessage).arg(deviceSerial));
  135. }
  136. } // namespace Internal
  137. } // namespace Tizen