sdbpacketutils.cpp 5.1 KB

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