MQTTConnect.h 4.7 KB

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