mqtt.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* mqtt.h
  2. *
  3. * Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * 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. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * 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 OWNER 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. #ifndef USER_AT_MQTT_H_
  31. #define USER_AT_MQTT_H_
  32. #include "user_config.h"
  33. #include "mqtt/mqtt_msg.h"
  34. #include "user_interface.h"
  35. #include "queue.h"
  36. typedef struct mqtt_event_data_t
  37. {
  38. uint8_t type;
  39. const char* topic;
  40. const char* data;
  41. uint16_t topic_length;
  42. uint16_t data_length;
  43. uint16_t data_offset;
  44. } mqtt_event_data_t;
  45. typedef struct mqtt_state_t
  46. {
  47. uint16_t port;
  48. int auto_reconnect;
  49. mqtt_connect_info_t* connect_info;
  50. uint8_t* in_buffer;
  51. uint8_t* out_buffer;
  52. int in_buffer_length;
  53. int out_buffer_length;
  54. uint16_t message_length;
  55. uint16_t message_length_read;
  56. mqtt_message_t* outbound_message;
  57. mqtt_connection_t mqtt_connection;
  58. uint16_t pending_msg_id;
  59. int pending_msg_type;
  60. int pending_publish_qos;
  61. } mqtt_state_t;
  62. typedef enum {
  63. WIFI_INIT,
  64. WIFI_CONNECTING,
  65. WIFI_CONNECTING_ERROR,
  66. WIFI_CONNECTED,
  67. DNS_RESOLVE,
  68. TCP_DISCONNECTING,
  69. TCP_DISCONNECTED,
  70. TCP_RECONNECT_DISCONNECTING,
  71. TCP_RECONNECT_REQ,
  72. TCP_RECONNECT,
  73. TCP_CONNECTING,
  74. TCP_CONNECTING_ERROR,
  75. TCP_CONNECTED,
  76. MQTT_CONNECT_SEND,
  77. MQTT_CONNECT_SENDING,
  78. MQTT_SUBSCIBE_SEND,
  79. MQTT_SUBSCIBE_SENDING,
  80. MQTT_DATA,
  81. MQTT_KEEPALIVE_SEND,
  82. MQTT_PUBLISH_RECV,
  83. MQTT_PUBLISHING,
  84. MQTT_DELETING,
  85. MQTT_DELETED,
  86. } tConnState;
  87. typedef void (*MqttCallback)(uint32_t *args);
  88. typedef void (*MqttDataCallback)(uint32_t *args, const char* topic, uint32_t topic_len, const char *data, uint32_t lengh);
  89. typedef struct {
  90. struct espconn *pCon;
  91. uint8_t security;
  92. uint8_t* host;
  93. uint32_t port;
  94. ip_addr_t ip;
  95. mqtt_state_t mqtt_state;
  96. mqtt_connect_info_t connect_info;
  97. MqttCallback connectedCb;
  98. MqttCallback disconnectedCb;
  99. MqttCallback publishedCb;
  100. MqttCallback timeoutCb;
  101. MqttDataCallback dataCb;
  102. ETSTimer mqttTimer;
  103. uint32_t keepAliveTick;
  104. uint32_t reconnectTick;
  105. uint32_t sendTimeout;
  106. tConnState connState;
  107. QUEUE msgQueue;
  108. void* user_data;
  109. } MQTT_Client;
  110. #define SEC_NONSSL 0
  111. #define SEC_SSL 1
  112. #define MQTT_FLAG_CONNECTED 1
  113. #define MQTT_FLAG_READY 2
  114. #define MQTT_FLAG_EXIT 4
  115. #define MQTT_EVENT_TYPE_NONE 0
  116. #define MQTT_EVENT_TYPE_CONNECTED 1
  117. #define MQTT_EVENT_TYPE_DISCONNECTED 2
  118. #define MQTT_EVENT_TYPE_SUBSCRIBED 3
  119. #define MQTT_EVENT_TYPE_UNSUBSCRIBED 4
  120. #define MQTT_EVENT_TYPE_PUBLISH 5
  121. #define MQTT_EVENT_TYPE_PUBLISHED 6
  122. #define MQTT_EVENT_TYPE_EXITED 7
  123. #define MQTT_EVENT_TYPE_PUBLISH_CONTINUATION 8
  124. void ICACHE_FLASH_ATTR MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32_t port, uint8_t security);
  125. BOOL ICACHE_FLASH_ATTR MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession);
  126. void ICACHE_FLASH_ATTR MQTT_DeleteClient(MQTT_Client *mqttClient);
  127. void ICACHE_FLASH_ATTR MQTT_InitLWT(MQTT_Client *mqttClient, uint8_t* will_topic, uint8_t* will_msg, uint8_t will_qos, uint8_t will_retain);
  128. void ICACHE_FLASH_ATTR MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb);
  129. void ICACHE_FLASH_ATTR MQTT_OnDisconnected(MQTT_Client *mqttClient, MqttCallback disconnectedCb);
  130. void ICACHE_FLASH_ATTR MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb);
  131. void ICACHE_FLASH_ATTR MQTT_OnTimeout(MQTT_Client *mqttClient, MqttCallback timeoutCb);
  132. void ICACHE_FLASH_ATTR MQTT_OnData(MQTT_Client *mqttClient, MqttDataCallback dataCb);
  133. BOOL ICACHE_FLASH_ATTR MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos);
  134. BOOL ICACHE_FLASH_ATTR MQTT_UnSubscribe(MQTT_Client *client, char* topic);
  135. void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient);
  136. void ICACHE_FLASH_ATTR MQTT_Disconnect(MQTT_Client *mqttClient);
  137. BOOL ICACHE_FLASH_ATTR MQTT_Publish(MQTT_Client *client, const char* topic, const char* data, int data_length, int qos, int retain);
  138. #endif /* USER_AT_MQTT_H_ */