mqtt.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @file
  3. * MQTT client
  4. */
  5. /*
  6. * Copyright (c) 2016 Erik Andersson
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Erik Andersson
  34. *
  35. */
  36. #ifndef LWIP_HDR_APPS_MQTT_CLIENT_H
  37. #define LWIP_HDR_APPS_MQTT_CLIENT_H
  38. #include "lwip/apps/mqtt_opts.h"
  39. #include "lwip/err.h"
  40. #include "lwip/ip_addr.h"
  41. #include "lwip/prot/iana.h"
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. typedef struct mqtt_client_s mqtt_client_t;
  46. #if LWIP_ALTCP && LWIP_ALTCP_TLS
  47. struct altcp_tls_config;
  48. #endif
  49. /** @ingroup mqtt
  50. * Default MQTT port (non-TLS) */
  51. #define MQTT_PORT LWIP_IANA_PORT_MQTT
  52. /** @ingroup mqtt
  53. * Default MQTT TLS port */
  54. #define MQTT_TLS_PORT LWIP_IANA_PORT_SECURE_MQTT
  55. /*---------------------------------------------------------------------------------------------- */
  56. /* Connection with server */
  57. /**
  58. * @ingroup mqtt
  59. * Client information and connection parameters */
  60. struct mqtt_connect_client_info_t {
  61. /** Client identifier, must be set by caller */
  62. const char *client_id;
  63. /** User name, set to NULL if not used */
  64. const char* client_user;
  65. /** Password, set to NULL if not used */
  66. const char* client_pass;
  67. /** keep alive time in seconds, 0 to disable keep alive functionality*/
  68. u16_t keep_alive;
  69. /** will topic, set to NULL if will is not to be used,
  70. will_msg, will_qos and will retain are then ignored */
  71. const char* will_topic;
  72. /** will_msg, see will_topic */
  73. const char* will_msg;
  74. /** will_msg length, 0 to compute length from will_msg string */
  75. u8_t will_msg_len;
  76. /** will_qos, see will_topic */
  77. u8_t will_qos;
  78. /** will_retain, see will_topic */
  79. u8_t will_retain;
  80. #if LWIP_ALTCP && LWIP_ALTCP_TLS
  81. /** TLS configuration for secure connections */
  82. struct altcp_tls_config *tls_config;
  83. #endif
  84. };
  85. /**
  86. * @ingroup mqtt
  87. * Connection status codes */
  88. typedef enum
  89. {
  90. /** Accepted */
  91. MQTT_CONNECT_ACCEPTED = 0,
  92. /** Refused protocol version */
  93. MQTT_CONNECT_REFUSED_PROTOCOL_VERSION = 1,
  94. /** Refused identifier */
  95. MQTT_CONNECT_REFUSED_IDENTIFIER = 2,
  96. /** Refused server */
  97. MQTT_CONNECT_REFUSED_SERVER = 3,
  98. /** Refused user credentials */
  99. MQTT_CONNECT_REFUSED_USERNAME_PASS = 4,
  100. /** Refused not authorized */
  101. MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_ = 5,
  102. /** Disconnected */
  103. MQTT_CONNECT_DISCONNECTED = 256,
  104. /** Timeout */
  105. MQTT_CONNECT_TIMEOUT = 257
  106. } mqtt_connection_status_t;
  107. /**
  108. * @ingroup mqtt
  109. * Function prototype for mqtt connection status callback. Called when
  110. * client has connected to the server after initiating a mqtt connection attempt by
  111. * calling mqtt_client_connect() or when connection is closed by server or an error
  112. *
  113. * @param client MQTT client itself
  114. * @param arg Additional argument to pass to the callback function
  115. * @param status Connect result code or disconnection notification @see mqtt_connection_status_t
  116. *
  117. */
  118. typedef void (*mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_t status);
  119. /**
  120. * @ingroup mqtt
  121. * Data callback flags */
  122. enum {
  123. /** Flag set when last fragment of data arrives in data callback */
  124. MQTT_DATA_FLAG_LAST = 1
  125. };
  126. /**
  127. * @ingroup mqtt
  128. * Function prototype for MQTT incoming publish data callback function. Called when data
  129. * arrives to a subscribed topic @see mqtt_subscribe
  130. *
  131. * @param arg Additional argument to pass to the callback function
  132. * @param data User data, pointed object, data may not be referenced after callback return,
  133. NULL is passed when all publish data are delivered
  134. * @param len Length of publish data fragment
  135. * @param flags MQTT_DATA_FLAG_LAST set when this call contains the last part of data from publish message
  136. *
  137. */
  138. typedef void (*mqtt_incoming_data_cb_t)(void *arg, const u8_t *data, u16_t len, u8_t flags);
  139. /**
  140. * @ingroup mqtt
  141. * Function prototype for MQTT incoming publish function. Called when an incoming publish
  142. * arrives to a subscribed topic @see mqtt_subscribe
  143. *
  144. * @param arg Additional argument to pass to the callback function
  145. * @param topic Zero terminated Topic text string, topic may not be referenced after callback return
  146. * @param tot_len Total length of publish data, if set to 0 (no publish payload) data callback will not be invoked
  147. */
  148. typedef void (*mqtt_incoming_publish_cb_t)(void *arg, const char *topic, u32_t tot_len);
  149. /**
  150. * @ingroup mqtt
  151. * Function prototype for mqtt request callback. Called when a subscribe, unsubscribe
  152. * or publish request has completed
  153. * @param arg Pointer to user data supplied when invoking request
  154. * @param err ERR_OK on success
  155. * ERR_TIMEOUT if no response was received within timeout,
  156. * ERR_ABRT if (un)subscribe was denied
  157. */
  158. typedef void (*mqtt_request_cb_t)(void *arg, err_t err);
  159. err_t mqtt_client_connect(mqtt_client_t *client, const ip_addr_t *ipaddr, u16_t port, mqtt_connection_cb_t cb, void *arg,
  160. const struct mqtt_connect_client_info_t *client_info);
  161. void mqtt_disconnect(mqtt_client_t *client);
  162. mqtt_client_t *mqtt_client_new(void);
  163. void mqtt_client_free(mqtt_client_t* client);
  164. u8_t mqtt_client_is_connected(mqtt_client_t *client);
  165. void mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t pub_cb,
  166. mqtt_incoming_data_cb_t data_cb, void *arg);
  167. err_t mqtt_sub_unsub(mqtt_client_t *client, const char *topic, u8_t qos, mqtt_request_cb_t cb, void *arg, u8_t sub);
  168. /** @ingroup mqtt
  169. *Subscribe to topic */
  170. #define mqtt_subscribe(client, topic, qos, cb, arg) mqtt_sub_unsub(client, topic, qos, cb, arg, 1)
  171. /** @ingroup mqtt
  172. * Unsubscribe to topic */
  173. #define mqtt_unsubscribe(client, topic, cb, arg) mqtt_sub_unsub(client, topic, 0, cb, arg, 0)
  174. err_t mqtt_publish(mqtt_client_t *client, const char *topic, const void *payload, u16_t payload_length, u8_t qos, u8_t retain,
  175. mqtt_request_cb_t cb, void *arg);
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* LWIP_HDR_APPS_MQTT_CLIENT_H */