MQTTConnect.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*******************************************************************************
  2. * Copyright (c) 2014, 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 - add connack return code definitions
  16. * Xiang Rong - 442039 Add makefile to Embedded C client
  17. * Ian Craggs - fix for issue #64, bit order in connack response
  18. *******************************************************************************/
  19. #ifndef MQTTCONNECT_H_
  20. #define MQTTCONNECT_H_
  21. enum connack_return_codes
  22. {
  23. MQTT_CONNECTION_ACCEPTED = 0,
  24. MQTT_UNNACCEPTABLE_PROTOCOL = 1,
  25. MQTT_CLIENTID_REJECTED = 2,
  26. MQTT_SERVER_UNAVAILABLE = 3,
  27. MQTT_BAD_USERNAME_OR_PASSWORD = 4,
  28. MQTT_NOT_AUTHORIZED = 5,
  29. };
  30. #if !defined(DLLImport)
  31. #define DLLImport
  32. #endif
  33. #if !defined(DLLExport)
  34. #define DLLExport
  35. #endif
  36. typedef union
  37. {
  38. unsigned char all; /**< all connect flags */
  39. #if defined(REVERSED)
  40. struct
  41. {
  42. unsigned int username : 1; /**< 3.1 user name */
  43. unsigned int password : 1; /**< 3.1 password */
  44. unsigned int willRetain : 1; /**< will retain setting */
  45. unsigned int willQoS : 2; /**< will QoS value */
  46. unsigned int will : 1; /**< will flag */
  47. unsigned int cleansession : 1; /**< clean session flag */
  48. unsigned int : 1; /**< unused */
  49. } bits;
  50. #else
  51. struct
  52. {
  53. unsigned int : 1; /**< unused */
  54. unsigned int cleansession : 1; /**< cleansession flag */
  55. unsigned int will : 1; /**< will flag */
  56. unsigned int willQoS : 2; /**< will QoS value */
  57. unsigned int willRetain : 1; /**< will retain setting */
  58. unsigned int password : 1; /**< 3.1 password */
  59. unsigned int username : 1; /**< 3.1 user name */
  60. } bits;
  61. #endif
  62. } MQTTConnectFlags; /**< connect flags byte */
  63. /**
  64. * Defines the MQTT "Last Will and Testament" (LWT) settings for
  65. * the connect packet.
  66. */
  67. typedef struct
  68. {
  69. /** The eyecatcher for this structure. must be MQTW. */
  70. char struct_id[4];
  71. /** The version number of this structure. Must be 0 */
  72. int struct_version;
  73. /** The LWT topic to which the LWT message will be published. */
  74. MQTTString topicName;
  75. /** The LWT payload. */
  76. MQTTString message;
  77. /**
  78. * The retained flag for the LWT message (see MQTTAsync_message.retained).
  79. */
  80. unsigned char retained;
  81. /**
  82. * The quality of service setting for the LWT message (see
  83. * MQTTAsync_message.qos and @ref qos).
  84. */
  85. char qos;
  86. } MQTTPacket_willOptions;
  87. #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
  88. typedef struct
  89. {
  90. /** The eyecatcher for this structure. must be MQTC. */
  91. char struct_id[4];
  92. /** The version number of this structure. Must be 0 */
  93. int struct_version;
  94. /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
  95. */
  96. unsigned char MQTTVersion;
  97. MQTTString clientID;
  98. unsigned short keepAliveInterval;
  99. unsigned char cleansession;
  100. unsigned char willFlag;
  101. MQTTPacket_willOptions will;
  102. MQTTString username;
  103. MQTTString password;
  104. } MQTTPacket_connectData;
  105. typedef union
  106. {
  107. unsigned char all; /**< all connack flags */
  108. #if defined(REVERSED)
  109. struct
  110. {
  111. unsigned int reserved : 7; /**< unused */
  112. unsigned int sessionpresent : 1; /**< session present flag */
  113. } bits;
  114. #else
  115. struct
  116. {
  117. unsigned int sessionpresent : 1; /**< session present flag */
  118. unsigned int reserved: 7; /**< unused */
  119. } bits;
  120. #endif
  121. } MQTTConnackFlags; /**< connack flags byte */
  122. #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
  123. MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
  124. DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
  125. DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
  126. DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
  127. DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
  128. DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
  129. DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
  130. #endif /* MQTTCONNECT_H_ */