mqtt_msg.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * File: mqtt_msg.h
  3. * Author: Minh Tuan
  4. *
  5. * Created on July 12, 2014, 1:05 PM
  6. */
  7. #ifndef MQTT_MSG_H
  8. #define MQTT_MSG_H
  9. #include "c_types.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /*
  14. * Copyright (c) 2014, Stephen Robinson
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright
  24. * notice, this list of conditions and the following disclaimer in the
  25. * documentation and/or other materials provided with the distribution.
  26. * 3. Neither the name of the copyright holder nor the names of its
  27. * contributors may be used to endorse or promote products derived
  28. * from this software without specific prior written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  31. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  36. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  37. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  38. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  39. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. */
  43. /* 7 6 5 4 3 2 1 0*/
  44. /*| --- Message Type---- | DUP Flag | QoS Level | Retain |
  45. /* Remaining Length */
  46. enum mqtt_message_type
  47. {
  48. MQTT_MSG_TYPE_CONNECT = 1,
  49. MQTT_MSG_TYPE_CONNACK = 2,
  50. MQTT_MSG_TYPE_PUBLISH = 3,
  51. MQTT_MSG_TYPE_PUBACK = 4,
  52. MQTT_MSG_TYPE_PUBREC = 5,
  53. MQTT_MSG_TYPE_PUBREL = 6,
  54. MQTT_MSG_TYPE_PUBCOMP = 7,
  55. MQTT_MSG_TYPE_SUBSCRIBE = 8,
  56. MQTT_MSG_TYPE_SUBACK = 9,
  57. MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
  58. MQTT_MSG_TYPE_UNSUBACK = 11,
  59. MQTT_MSG_TYPE_PINGREQ = 12,
  60. MQTT_MSG_TYPE_PINGRESP = 13,
  61. MQTT_MSG_TYPE_DISCONNECT = 14
  62. };
  63. enum mqtt_connack_return_code
  64. {
  65. MQTT_CONN_FAIL_SERVER_NOT_FOUND = -5,
  66. MQTT_CONN_FAIL_NOT_A_CONNACK_MSG = -4,
  67. MQTT_CONN_FAIL_DNS = -3,
  68. MQTT_CONN_FAIL_TIMEOUT_RECEIVING = -2,
  69. MQTT_CONN_FAIL_TIMEOUT_SENDING = -1,
  70. MQTT_CONNACK_ACCEPTED = 0,
  71. MQTT_CONNACK_REFUSED_PROTOCOL_VER = 1,
  72. MQTT_CONNACK_REFUSED_ID_REJECTED = 2,
  73. MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE = 3,
  74. MQTT_CONNACK_REFUSED_BAD_USER_OR_PASS = 4,
  75. MQTT_CONNACK_REFUSED_NOT_AUTHORIZED = 5
  76. };
  77. typedef struct mqtt_message
  78. {
  79. uint8_t* data;
  80. uint16_t length;
  81. } mqtt_message_t;
  82. typedef struct mqtt_connection
  83. {
  84. mqtt_message_t message;
  85. uint16_t message_id;
  86. uint8_t* buffer;
  87. uint16_t buffer_length;
  88. } mqtt_connection_t;
  89. typedef struct mqtt_connect_info
  90. {
  91. char* client_id;
  92. char* username;
  93. char* password;
  94. char* will_topic;
  95. char* will_message;
  96. int keepalive;
  97. int will_qos;
  98. int will_retain;
  99. int clean_session;
  100. } mqtt_connect_info_t;
  101. static inline int mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
  102. static inline int mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
  103. static inline int mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
  104. static inline int mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
  105. static inline int mqtt_get_connect_ret_code(uint8_t* buffer) { return (buffer[3]); }
  106. void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length);
  107. int mqtt_get_total_length(uint8_t* buffer, uint16_t length);
  108. const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length);
  109. const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length);
  110. uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length);
  111. mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info);
  112. mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id);
  113. mqtt_message_t* mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id);
  114. mqtt_message_t* mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id);
  115. mqtt_message_t* mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id);
  116. mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id);
  117. mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id);
  118. mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id);
  119. mqtt_message_t* mqtt_msg_pingreq(mqtt_connection_t* connection);
  120. mqtt_message_t* mqtt_msg_pingresp(mqtt_connection_t* connection);
  121. mqtt_message_t* mqtt_msg_disconnect(mqtt_connection_t* connection);
  122. mqtt_message_t* mqtt_msg_subscribe_init(mqtt_connection_t* connection, uint16_t* message_id);
  123. mqtt_message_t* mqtt_msg_subscribe_topic(mqtt_connection_t* connection, const char* topic, int qos);
  124. mqtt_message_t* mqtt_msg_subscribe_fini(mqtt_connection_t* connection);
  125. mqtt_message_t* mqtt_msg_unsubscribe_init(mqtt_connection_t* connection, uint16_t* message_id);
  126. mqtt_message_t* mqtt_msg_unsubscribe_topic(mqtt_connection_t* connection, const char* topic);
  127. mqtt_message_t* mqtt_msg_unsubscribe_fini(mqtt_connection_t* connection);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif /* MQTT_MSG_H */