qmqtt_frame.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * qmqtt_frame.h - qmqtt frame heaer
  3. *
  4. * Copyright (c) 2013 Ery Lee <ery.lee at gmail dot com>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of mqttc nor the names of its contributors may be used
  16. * to endorse or promote products derived from this software without
  17. * specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. */
  32. #ifndef QMQTT_FRAME_H
  33. #define QMQTT_FRAME_H
  34. #include <qmqtt_global.h>
  35. #include <QMetaType>
  36. #include <QByteArray>
  37. #include <QString>
  38. QT_FORWARD_DECLARE_CLASS(QDataStream)
  39. #define PROTOCOL_MAGIC_3_1_0 "MQIsdp"
  40. #define PROTOCOL_MAGIC_3_1_1 "MQTT"
  41. #define RANDOM_CLIENT_PREFIX "QMQTT-"
  42. #define CONNECT 0x10
  43. #define CONNACK 0x20
  44. #define PUBLISH 0x30
  45. #define PUBACK 0x40
  46. #define PUBREC 0x50
  47. #define PUBREL 0x60
  48. #define PUBCOMP 0x70
  49. #define SUBSCRIBE 0x80
  50. #define SUBACK 0x90
  51. #define UNSUBSCRIBE 0xA0
  52. #define UNSUBACK 0xB0
  53. #define PINGREQ 0xC0
  54. #define PINGRESP 0xD0
  55. #define DISCONNECT 0xE0
  56. #define LSB(A) quint8(A & 0x00FF)
  57. #define MSB(A) quint8((A & 0xFF00) >> 8)
  58. /*
  59. |--------------------------------------
  60. | 7 6 5 4 | 3 | 2 1 | 0 |
  61. | Type | DUP flag | QoS | RETAIN |
  62. |--------------------------------------
  63. */
  64. #define GETTYPE(HDR) (HDR & 0xF0)
  65. #define SETQOS(HDR, Q) (HDR | ((Q) << 1))
  66. #define GETQOS(HDR) ((HDR & 0x06) >> 1)
  67. #define SETDUP(HDR, D) (HDR | ((D) << 3))
  68. #define GETDUP(HDR) ((HDR & 0x08) >> 3)
  69. #define SETRETAIN(HDR, R) (HDR | (R))
  70. #define GETRETAIN(HDR) (HDR & 0x01)
  71. /*
  72. |----------------------------------------------------------------------------------
  73. | 7 | 6 | 5 | 4 3 | 2 | 1 | 0 |
  74. | username | password | willretain | willqos | willflag | cleansession | reserved |
  75. |----------------------------------------------------------------------------------
  76. */
  77. #define FLAG_CLEANSESS(F, C) (F | ((C) << 1))
  78. #define FLAG_WILL(F, W) (F | ((W) << 2))
  79. #define FLAG_WILLQOS(F, Q) (F | ((Q) << 3))
  80. #define FLAG_WILLRETAIN(F, R) (F | ((R) << 5))
  81. #define FLAG_PASSWD(F, P) (F | ((P) << 6))
  82. #define FLAG_USERNAME(F, U) (F | ((U) << 7))
  83. namespace QMQTT {
  84. class Q_MQTT_EXPORT Frame
  85. {
  86. public:
  87. explicit Frame();
  88. explicit Frame(const quint8 header);
  89. explicit Frame(const quint8 header, const QByteArray &data);
  90. virtual ~Frame();
  91. Frame(const Frame& other);
  92. Frame& operator=(const Frame& other);
  93. bool operator==(const Frame& other) const;
  94. inline bool operator!=(const Frame& other) const
  95. { return !operator==(other); }
  96. quint8 header() const;
  97. QByteArray data() const;
  98. quint16 readInt();
  99. quint8 readChar();
  100. QByteArray readByteArray();
  101. QString readString();
  102. void writeInt(const quint16 i);
  103. void writeChar(const quint8 c);
  104. void writeByteArray(const QByteArray &data);
  105. void writeString(const QString &string);
  106. void writeRawData(const QByteArray &data);
  107. //TODO: FIXME LATER
  108. void write(QDataStream &stream) const;
  109. bool encodeLength(QByteArray &lenbuf, int length) const;
  110. private:
  111. quint8 _header;
  112. QByteArray _data;
  113. };
  114. } // namespace QMQTT
  115. Q_DECLARE_METATYPE(QMQTT::Frame)
  116. #endif // QMQTT_FRAME_H