MQTTPacket.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 documentation
  15. * 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. {
  34. MQTTPACKET_BUFFER_TOO_SHORT = -2,
  35. MQTTPACKET_READ_ERROR = -1,
  36. MQTTPACKET_READ_COMPLETE
  37. };
  38. enum msgTypes
  39. {
  40. CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
  41. PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
  42. PINGREQ, PINGRESP, DISCONNECT
  43. };
  44. /**
  45. * Bitfields for the MQTT header byte.
  46. */
  47. typedef union
  48. {
  49. unsigned char byte; /**< the whole byte */
  50. #if defined(REVERSED)
  51. struct
  52. {
  53. unsigned int type : 4; /**< message type nibble */
  54. unsigned int dup : 1; /**< DUP flag bit */
  55. unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
  56. unsigned int retain : 1; /**< retained flag bit */
  57. } bits;
  58. #else
  59. struct
  60. {
  61. unsigned int retain : 1; /**< retained flag bit */
  62. unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
  63. unsigned int dup : 1; /**< DUP flag bit */
  64. unsigned int type : 4; /**< message type nibble */
  65. } bits;
  66. #endif
  67. } MQTTHeader;
  68. typedef struct
  69. {
  70. int len;
  71. char* data;
  72. } MQTTLenString;
  73. typedef struct
  74. {
  75. char* cstring;
  76. MQTTLenString lenstring;
  77. } MQTTString;
  78. #define MQTTString_initializer {NULL, {0, NULL}}
  79. int MQTTstrlen(MQTTString mqttstring);
  80. #include "MQTTConnect.h"
  81. #include "MQTTPublish.h"
  82. #include "MQTTSubscribe.h"
  83. #include "MQTTUnsubscribe.h"
  84. #include "MQTTFormat.h"
  85. DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
  86. DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
  87. int MQTTPacket_len(int rem_len);
  88. DLLExport int MQTTPacket_equals(MQTTString* a, char* b);
  89. DLLExport int MQTTPacket_encode(unsigned char* buf, int length);
  90. int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
  91. int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
  92. int readInt(unsigned char** pptr);
  93. char readChar(unsigned char** pptr);
  94. void writeChar(unsigned char** pptr, char c);
  95. void writeInt(unsigned char** pptr, int anInt);
  96. int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
  97. void writeCString(unsigned char** pptr, const char* string);
  98. void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
  99. DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
  100. typedef struct {
  101. int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
  102. void *sck; /* pointer to whatever the system may use to identify the transport */
  103. int multiplier;
  104. int rem_len;
  105. int len;
  106. char state;
  107. }MQTTTransport;
  108. int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
  109. #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
  110. }
  111. #endif
  112. #endif /* MQTTPACKET_H_ */