mqtt_msg.h 5.2 KB

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