mqtt_msg.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2014, Stephen Robinson
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holder nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef MQTT_MSG_H
  32. #define MQTT_MSG_H
  33. #define PROTOCOL_NAMEv311
  34. enum mqtt_message_type {
  35. MQTT_MSG_TYPE_CONNECT = 1,
  36. MQTT_MSG_TYPE_CONNACK = 2,
  37. MQTT_MSG_TYPE_PUBLISH = 3,
  38. MQTT_MSG_TYPE_PUBACK = 4,
  39. MQTT_MSG_TYPE_PUBREC = 5,
  40. MQTT_MSG_TYPE_PUBREL = 6,
  41. MQTT_MSG_TYPE_PUBCOMP = 7,
  42. MQTT_MSG_TYPE_SUBSCRIBE = 8,
  43. MQTT_MSG_TYPE_SUBACK = 9,
  44. MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
  45. MQTT_MSG_TYPE_UNSUBACK = 11,
  46. MQTT_MSG_TYPE_PINGREQ = 12,
  47. MQTT_MSG_TYPE_PINGRESP = 13,
  48. MQTT_MSG_TYPE_DISCONNECT = 14
  49. };
  50. // Descriptor for a serialized MQTT message, this is returned by functions that compose a message
  51. // (It's really an MQTT packet in v3.1.1 terminology)
  52. typedef struct mqtt_message {
  53. uint8_t* data;
  54. uint16_t length;
  55. } mqtt_message_t;
  56. // Descriptor for a connection with message assembly storage
  57. typedef struct mqtt_connection {
  58. mqtt_message_t message; // resulting message
  59. uint16_t message_id; // id of assembled message and memo to calculate next message id
  60. uint8_t* buffer; // buffer for assembling messages
  61. uint16_t buffer_length; // buffer length
  62. } mqtt_connection_t;
  63. // Descriptor for a connect request
  64. typedef struct mqtt_connect_info {
  65. char* client_id;
  66. char* username;
  67. char* password;
  68. char* will_topic;
  69. char* will_message;
  70. uint8_t keepalive;
  71. uint8_t will_qos;
  72. uint8_t will_retain;
  73. uint8_t clean_session;
  74. } mqtt_connect_info_t;
  75. static inline int ICACHE_FLASH_ATTR mqtt_get_type(const uint8_t* buffer) {
  76. return (buffer[0] & 0xf0) >> 4;
  77. }
  78. static inline int ICACHE_FLASH_ATTR mqtt_get_dup(const uint8_t* buffer) {
  79. return (buffer[0] & 0x08) >> 3;
  80. }
  81. static inline int ICACHE_FLASH_ATTR mqtt_get_qos(const uint8_t* buffer) {
  82. return (buffer[0] & 0x06) >> 1;
  83. }
  84. static inline int ICACHE_FLASH_ATTR mqtt_get_retain(const uint8_t* buffer) {
  85. return (buffer[0] & 0x01);
  86. }
  87. // Init a connection descriptor
  88. void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length);
  89. // Returns the total length of a message including MQTT fixed header
  90. int mqtt_get_total_length(const uint8_t* buffer, uint16_t length);
  91. // Return pointer to topic, length in in/out param: in=length of buffer, out=length of topic
  92. const char* mqtt_get_publish_topic(const uint8_t* buffer, uint16_t* length);
  93. // Return pointer to data, length in in/out param: in=length of buffer, out=length of data
  94. const char* mqtt_get_publish_data(const uint8_t* buffer, uint16_t* length);
  95. // Return message id
  96. uint16_t mqtt_get_id(const uint8_t* buffer, uint16_t length);
  97. // The following functions construct an outgoing message
  98. mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info);
  99. 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);
  100. mqtt_message_t* mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id);
  101. mqtt_message_t* mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id);
  102. mqtt_message_t* mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id);
  103. mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id);
  104. mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id);
  105. mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id);
  106. mqtt_message_t* mqtt_msg_pingreq(mqtt_connection_t* connection);
  107. mqtt_message_t* mqtt_msg_pingresp(mqtt_connection_t* connection);
  108. mqtt_message_t* mqtt_msg_disconnect(mqtt_connection_t* connection);
  109. #endif // MQTT_MSG_H