mqtt.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 MQTT_H_
  31. #define MQTT_H_
  32. #include "mqtt_msg.h"
  33. #include "pktbuf.h"
  34. // in rest.c
  35. uint8_t UTILS_StrToIP(const char* str, void *ip);
  36. // State of MQTT connection
  37. typedef enum {
  38. MQTT_DISCONNECTED, // we're in disconnected state
  39. TCP_RECONNECT_REQ, // connect failed, needs reconnecting
  40. TCP_CONNECTING, // in TCP connection process
  41. MQTT_CONNECTED, // conneted (or connecting)
  42. } tConnState;
  43. typedef struct MQTT_Client MQTT_Client; // forward definition
  44. // Simple notification callback
  45. typedef void (*MqttCallback)(MQTT_Client *client);
  46. // Callback with data messge
  47. typedef void (*MqttDataCallback)(MQTT_Client *client, const char* topic, uint32_t topic_len,
  48. const char* data, uint32_t data_len);
  49. // MQTTY client data structure
  50. struct MQTT_Client {
  51. struct espconn* pCon; // socket
  52. // connection information
  53. char* host; // MQTT server
  54. uint16_t port;
  55. uint8_t security; // 0=tcp, 1=ssl
  56. ip_addr_t ip; // MQTT server IP address
  57. mqtt_connect_info_t connect_info; // info to connect/reconnect
  58. // protocol state and message assembly
  59. tConnState connState; // connection state
  60. bool sending; // espconn_send is pending
  61. mqtt_connection_t mqtt_connection; // message assembly descriptor
  62. PktBuf* msgQueue; // queued outbound messages
  63. // TCP input buffer
  64. uint8_t* in_buffer;
  65. int in_buffer_size; // length allocated
  66. int in_buffer_filled; // number of bytes held
  67. // outstanding message when we expect an ACK
  68. PktBuf* pending_buffer; // buffer sent and awaiting ACK
  69. PktBuf* sending_buffer; // buffer sent not awaiting ACK
  70. // timer and associated timeout counters
  71. ETSTimer mqttTimer; // timer for this connection
  72. uint8_t keepAliveTick; // seconds 'til keep-alive is required (0=no k-a)
  73. uint8_t keepAliveAckTick; // seconds 'til keep-alive ack is overdue (0=no k-a)
  74. uint8_t timeoutTick; // seconds 'til other timeout
  75. uint8_t sendTimeout; // value of send timeout setting
  76. uint8_t reconTimeout; // timeout to reconnect (back-off)
  77. // callbacks
  78. MqttCallback connectedCb;
  79. MqttCallback cmdConnectedCb;
  80. MqttCallback disconnectedCb;
  81. MqttCallback cmdDisconnectedCb;
  82. MqttCallback publishedCb;
  83. MqttCallback cmdPublishedCb;
  84. MqttDataCallback dataCb;
  85. MqttDataCallback cmdDataCb;
  86. // misc
  87. void* user_data;
  88. };
  89. // Initialize client data structure
  90. void MQTT_Init(MQTT_Client* mqttClient, char* host, uint32 port,
  91. uint8_t security, uint8_t sendTimeout,
  92. char* client_id, char* client_user, char* client_pass,
  93. uint8_t keepAliveTime);
  94. // Completely free buffers associated with client data structure
  95. // This does not free the mqttClient struct itself, it just readies the struct so
  96. // it can be freed or MQTT_Init can be called on it again
  97. void MQTT_Free(MQTT_Client* mqttClient);
  98. // Set Last Will Topic on client, must be called before MQTT_InitConnection
  99. void MQTT_InitLWT(MQTT_Client* mqttClient, char* will_topic, char* will_msg,
  100. uint8_t will_qos, uint8_t will_retain);
  101. // Disconnect and reconnect in order to change params (such as LWT)
  102. void MQTT_Reconnect(MQTT_Client* mqttClient);
  103. // Kick of a persistent connection to the broker, will reconnect anytime conn breaks
  104. void MQTT_Connect(MQTT_Client* mqttClient);
  105. // Kill persistent connection
  106. void MQTT_Disconnect(MQTT_Client* mqttClient);
  107. // Subscribe to a topic
  108. bool MQTT_Subscribe(MQTT_Client* client, char* topic, uint8_t qos);
  109. // Publish a message
  110. bool MQTT_Publish(MQTT_Client* client, const char* topic, const char* data, uint16_t data_len,
  111. uint8_t qos, uint8_t retain);
  112. // Callback when connected
  113. void MQTT_OnConnected(MQTT_Client* mqttClient, MqttCallback connectedCb);
  114. // Callback when disconnected
  115. void MQTT_OnDisconnected(MQTT_Client* mqttClient, MqttCallback disconnectedCb);
  116. // Callback when publish succeeded
  117. void MQTT_OnPublished(MQTT_Client* mqttClient, MqttCallback publishedCb);
  118. // Callback when data arrives for subscription
  119. void MQTT_OnData(MQTT_Client* mqttClient, MqttDataCallback dataCb);
  120. #endif /* USER_AT_MQTT_H_ */