MQTTPacket.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2017 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs, Allan Stockdill-Mander - SSL updates
  16. * Ian Craggs - MQTT 3.1.1 support
  17. * Ian Craggs - big endian Linux reversed definition
  18. *******************************************************************************/
  19. #if !defined(MQTTPACKET_H)
  20. #define MQTTPACKET_H
  21. #include "Socket.h"
  22. #if defined(OPENSSL)
  23. #include "SSLSocket.h"
  24. #endif
  25. #include "LinkedList.h"
  26. #include "Clients.h"
  27. /*BE
  28. include "Socket"
  29. include "LinkedList"
  30. include "Clients"
  31. BE*/
  32. typedef unsigned int bool;
  33. typedef void* (*pf)(unsigned char, char*, size_t);
  34. #define BAD_MQTT_PACKET -4
  35. enum msgTypes
  36. {
  37. CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
  38. PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
  39. PINGREQ, PINGRESP, DISCONNECT
  40. };
  41. #if defined(__linux__)
  42. #include <endian.h>
  43. #if __BYTE_ORDER == __BIG_ENDIAN
  44. #define REVERSED 1
  45. #endif
  46. #endif
  47. /**
  48. * Bitfields for the MQTT header byte.
  49. */
  50. typedef union
  51. {
  52. /*unsigned*/ char byte; /**< the whole byte */
  53. #if defined(REVERSED)
  54. struct
  55. {
  56. unsigned int type : 4; /**< message type nibble */
  57. bool dup : 1; /**< DUP flag bit */
  58. unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
  59. bool retain : 1; /**< retained flag bit */
  60. } bits;
  61. #else
  62. struct
  63. {
  64. bool retain : 1; /**< retained flag bit */
  65. unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
  66. bool dup : 1; /**< DUP flag bit */
  67. unsigned int type : 4; /**< message type nibble */
  68. } bits;
  69. #endif
  70. } Header;
  71. /**
  72. * Data for a connect packet.
  73. */
  74. typedef struct
  75. {
  76. Header header; /**< MQTT header byte */
  77. union
  78. {
  79. unsigned char all; /**< all connect flags */
  80. #if defined(REVERSED)
  81. struct
  82. {
  83. bool username : 1; /**< 3.1 user name */
  84. bool password : 1; /**< 3.1 password */
  85. bool willRetain : 1; /**< will retain setting */
  86. unsigned int willQoS : 2; /**< will QoS value */
  87. bool will : 1; /**< will flag */
  88. bool cleanstart : 1; /**< cleansession flag */
  89. int : 1; /**< unused */
  90. } bits;
  91. #else
  92. struct
  93. {
  94. int : 1; /**< unused */
  95. bool cleanstart : 1; /**< cleansession flag */
  96. bool will : 1; /**< will flag */
  97. unsigned int willQoS : 2; /**< will QoS value */
  98. bool willRetain : 1; /**< will retain setting */
  99. bool password : 1; /**< 3.1 password */
  100. bool username : 1; /**< 3.1 user name */
  101. } bits;
  102. #endif
  103. } flags; /**< connect flags byte */
  104. char *Protocol, /**< MQTT protocol name */
  105. *clientID, /**< string client id */
  106. *willTopic, /**< will topic */
  107. *willMsg; /**< will payload */
  108. int keepAliveTimer; /**< keepalive timeout value in seconds */
  109. unsigned char version; /**< MQTT version number */
  110. } Connect;
  111. /**
  112. * Data for a connack packet.
  113. */
  114. typedef struct
  115. {
  116. Header header; /**< MQTT header byte */
  117. union
  118. {
  119. unsigned char all; /**< all connack flags */
  120. #if defined(REVERSED)
  121. struct
  122. {
  123. unsigned int reserved : 7; /**< message type nibble */
  124. bool sessionPresent : 1; /**< was a session found on the server? */
  125. } bits;
  126. #else
  127. struct
  128. {
  129. bool sessionPresent : 1; /**< was a session found on the server? */
  130. unsigned int reserved : 7; /**< message type nibble */
  131. } bits;
  132. #endif
  133. } flags; /**< connack flags byte */
  134. char rc; /**< connack return code */
  135. } Connack;
  136. /**
  137. * Data for a packet with header only.
  138. */
  139. typedef struct
  140. {
  141. Header header; /**< MQTT header byte */
  142. } MQTTPacket;
  143. /**
  144. * Data for a subscribe packet.
  145. */
  146. typedef struct
  147. {
  148. Header header; /**< MQTT header byte */
  149. int msgId; /**< MQTT message id */
  150. List* topics; /**< list of topic strings */
  151. List* qoss; /**< list of corresponding QoSs */
  152. int noTopics; /**< topic and qos count */
  153. } Subscribe;
  154. /**
  155. * Data for a suback packet.
  156. */
  157. typedef struct
  158. {
  159. Header header; /**< MQTT header byte */
  160. int msgId; /**< MQTT message id */
  161. List* qoss; /**< list of granted QoSs */
  162. } Suback;
  163. /**
  164. * Data for an unsubscribe packet.
  165. */
  166. typedef struct
  167. {
  168. Header header; /**< MQTT header byte */
  169. int msgId; /**< MQTT message id */
  170. List* topics; /**< list of topic strings */
  171. int noTopics; /**< topic count */
  172. } Unsubscribe;
  173. /**
  174. * Data for a publish packet.
  175. */
  176. typedef struct
  177. {
  178. Header header; /**< MQTT header byte */
  179. char* topic; /**< topic string */
  180. int topiclen;
  181. int msgId; /**< MQTT message id */
  182. char* payload; /**< binary payload, length delimited */
  183. int payloadlen; /**< payload length */
  184. } Publish;
  185. /**
  186. * Data for one of the ack packets.
  187. */
  188. typedef struct
  189. {
  190. Header header; /**< MQTT header byte */
  191. int msgId; /**< MQTT message id */
  192. } Ack;
  193. typedef Ack Puback;
  194. typedef Ack Pubrec;
  195. typedef Ack Pubrel;
  196. typedef Ack Pubcomp;
  197. typedef Ack Unsuback;
  198. int MQTTPacket_encode(char* buf, size_t length);
  199. int MQTTPacket_decode(networkHandles* net, size_t* value);
  200. int readInt(char** pptr);
  201. char* readUTF(char** pptr, char* enddata);
  202. unsigned char readChar(char** pptr);
  203. void writeChar(char** pptr, char c);
  204. void writeInt(char** pptr, int anInt);
  205. void writeUTF(char** pptr, const char* string);
  206. void writeData(char** pptr, const void* data, int datalen);
  207. const char* MQTTPacket_name(int ptype);
  208. void* MQTTPacket_Factory(networkHandles* net, int* error);
  209. int MQTTPacket_send(networkHandles* net, Header header, char* buffer, size_t buflen, int free);
  210. int MQTTPacket_sends(networkHandles* net, Header header, int count, char** buffers, size_t* buflens, int* frees);
  211. void* MQTTPacket_header_only(unsigned char aHeader, char* data, size_t datalen);
  212. int MQTTPacket_send_disconnect(networkHandles* net, const char* clientID);
  213. void* MQTTPacket_publish(unsigned char aHeader, char* data, size_t datalen);
  214. void MQTTPacket_freePublish(Publish* pack);
  215. int MQTTPacket_send_publish(Publish* pack, int dup, int qos, int retained, networkHandles* net, const char* clientID);
  216. int MQTTPacket_send_puback(int msgid, networkHandles* net, const char* clientID);
  217. void* MQTTPacket_ack(unsigned char aHeader, char* data, size_t datalen);
  218. void MQTTPacket_freeSuback(Suback* pack);
  219. int MQTTPacket_send_pubrec(int msgid, networkHandles* net, const char* clientID);
  220. int MQTTPacket_send_pubrel(int msgid, int dup, networkHandles* net, const char* clientID);
  221. int MQTTPacket_send_pubcomp(int msgid, networkHandles* net, const char* clientID);
  222. void MQTTPacket_free_packet(MQTTPacket* pack);
  223. #if !defined(NO_BRIDGE)
  224. #include "MQTTPacketOut.h"
  225. #endif
  226. #endif /* MQTTPACKET_H */