MQTTPacket.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*******************************************************************************
  2. * Copyright (c) 2014 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
  15. *documentation Xiang Rong - 442039 Add makefile to Embedded C client
  16. *******************************************************************************/
  17. #ifndef MQTTPACKET_H_
  18. #define MQTTPACKET_H_
  19. #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
  20. extern "C" {
  21. #endif
  22. #if defined(WIN32_DLL) || defined(WIN64_DLL)
  23. #define DLLImport __declspec(dllimport)
  24. #define DLLExport __declspec(dllexport)
  25. #elif defined(LINUX_SO)
  26. #define DLLImport extern
  27. #define DLLExport __attribute__((visibility("default")))
  28. #else
  29. #define DLLImport
  30. #define DLLExport
  31. #endif
  32. enum errors {
  33. MQTTPACKET_BUFFER_TOO_SHORT = -2,
  34. MQTTPACKET_READ_ERROR = -1,
  35. MQTTPACKET_READ_COMPLETE
  36. };
  37. enum msgTypes {
  38. CONNECT = 1,
  39. CONNACK,
  40. PUBLISH,
  41. PUBACK,
  42. PUBREC,
  43. PUBREL,
  44. PUBCOMP,
  45. SUBSCRIBE,
  46. SUBACK,
  47. UNSUBSCRIBE,
  48. UNSUBACK,
  49. PINGREQ,
  50. PINGRESP,
  51. DISCONNECT
  52. };
  53. /**
  54. * Bitfields for the MQTT header byte.
  55. */
  56. typedef union {
  57. unsigned char byte; /**< the whole byte */
  58. #if defined(REVERSED)
  59. struct {
  60. unsigned int type : 4; /**< message type nibble */
  61. unsigned int dup : 1; /**< DUP flag bit */
  62. unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
  63. unsigned int retain : 1; /**< retained flag bit */
  64. } bits;
  65. #else
  66. struct {
  67. unsigned int retain : 1; /**< retained flag bit */
  68. unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
  69. unsigned int dup : 1; /**< DUP flag bit */
  70. unsigned int type : 4; /**< message type nibble */
  71. } bits;
  72. #endif
  73. } MQTTHeader;
  74. typedef struct {
  75. int len;
  76. char* data;
  77. } MQTTLenString;
  78. typedef struct {
  79. char* cstring;
  80. MQTTLenString lenstring;
  81. } MQTTString;
  82. #define MQTTString_initializer \
  83. { \
  84. NULL, { \
  85. 0, NULL \
  86. } \
  87. }
  88. int MQTTstrlen(MQTTString mqttstring);
  89. #include "MQTTConnect.h"
  90. #include "MQTTFormat.h"
  91. #include "MQTTPublish.h"
  92. #include "MQTTSubscribe.h"
  93. #include "MQTTUnsubscribe.h"
  94. DLLExport int MQTTSerialize_ack(unsigned char* buf,
  95. int buflen,
  96. unsigned char type,
  97. unsigned char dup,
  98. unsigned short packetid);
  99. DLLExport int MQTTDeserialize_ack(unsigned char* packettype,
  100. unsigned char* dup,
  101. unsigned short* packetid,
  102. unsigned char* buf,
  103. int buflen);
  104. int MQTTPacket_len(int rem_len);
  105. DLLExport int MQTTPacket_equals(MQTTString* a, char* b);
  106. DLLExport int MQTTPacket_encode(unsigned char* buf, int length);
  107. int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
  108. int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
  109. int readInt(unsigned char** pptr);
  110. char readChar(unsigned char** pptr);
  111. void writeChar(unsigned char** pptr, char c);
  112. void writeInt(unsigned char** pptr, int anInt);
  113. int readMQTTLenString(MQTTString* mqttstring,
  114. unsigned char** pptr,
  115. unsigned char* enddata);
  116. void writeCString(unsigned char** pptr, const char* string);
  117. void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
  118. DLLExport int MQTTPacket_read(unsigned char* buf,
  119. int buflen,
  120. int (*getfn)(unsigned char*, int));
  121. typedef struct {
  122. int (*getfn)(void*,
  123. unsigned char*,
  124. int); /* must return -1 for error, 0 for call again, or the
  125. number of bytes read */
  126. void* sck; /* pointer to whatever the system may use to identify the
  127. transport */
  128. int multiplier;
  129. int rem_len;
  130. int len;
  131. char state;
  132. } MQTTTransport;
  133. int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport* trp);
  134. #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
  135. }
  136. #endif
  137. #endif /* MQTTPACKET_H_ */