7 * Copyright (c) 2016 Erik Andersson
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * This file is part of the lwIP TCP/IP stack.
34 * Author: Erik Andersson
37 #ifndef LWIP_HDR_APPS_MQTT_CLIENT_H
38 #define LWIP_HDR_APPS_MQTT_CLIENT_H
40 #include "lwip/apps/mqtt_opts.h"
42 #include "lwip/ip_addr.h"
48 typedef struct mqtt_client_t mqtt_client_t;
51 * Default MQTT port */
52 #define MQTT_PORT 1883
54 /*---------------------------------------------------------------------------------------------- */
55 /* Connection with server */
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 and password, set to NULL if not used */
64 const char* client_user;
65 const char* client_pass;
66 /** keep alive time in seconds, 0 to disable keep alive functionality*/
68 /** will topic, set to NULL if will is not to be used,
69 will_msg, will_qos and will retain are then ignored */
70 const char* will_topic;
78 * Connection status codes */
81 MQTT_CONNECT_ACCEPTED = 0,
82 MQTT_CONNECT_REFUSED_PROTOCOL_VERSION = 1,
83 MQTT_CONNECT_REFUSED_IDENTIFIER = 2,
84 MQTT_CONNECT_REFUSED_SERVER = 3,
85 MQTT_CONNECT_REFUSED_USERNAME_PASS = 4,
86 MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_ = 5,
87 MQTT_CONNECT_DISCONNECTED = 256,
88 MQTT_CONNECT_TIMEOUT = 257
89 } mqtt_connection_status_t;
93 * Function prototype for mqtt connection status callback. Called when
94 * client has connected to the server after initiating a mqtt connection attempt by
95 * calling mqtt_connect() or when connection is closed by server or an error
97 * @param client MQTT client itself
98 * @param arg Additional argument to pass to the callback function
99 * @param status Connect result code or disconnection notification @see mqtt_connection_status_t
102 typedef void (*mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_t status);
107 * Data callback flags */
109 /** Flag set when last fragment of data arrives in data callback */
110 MQTT_DATA_FLAG_LAST = 1
115 * Function prototype for MQTT incoming publish data callback function. Called when data
116 * arrives to a subscribed topic @see mqtt_subscribe
118 * @param arg Additional argument to pass to the callback function
119 * @param data User data, pointed object, data may not be referenced after callback return,
120 NULL is passed when all publish data are delivered
121 * @param len Length of publish data fragment
122 * @param flags MQTT_DATA_FLAG_LAST set when this call contains the last part of data from publish message
125 typedef void (*mqtt_incoming_data_cb_t)(void *arg, const u8_t *data, u16_t len, u8_t flags);
130 * Function prototype for MQTT incoming publish function. Called when an incoming publish
131 * arrives to a subscribed topic @see mqtt_subscribe
133 * @param arg Additional argument to pass to the callback function
134 * @param topic Zero terminated Topic text string, topic may not be referenced after callback return
135 * @param tot_len Total length of publish data, if set to 0 (no publish payload) data callback will not be invoked
137 typedef void (*mqtt_incoming_publish_cb_t)(void *arg, const char *topic, u32_t tot_len);
142 * Function prototype for mqtt request callback. Called when a subscribe, unsubscribe
143 * or publish request has completed
144 * @param arg Pointer to user data supplied when invoking request
145 * @param err ERR_OK on success
146 * ERR_TIMEOUT if no response was received within timeout,
147 * ERR_ABRT if (un)subscribe was denied
149 typedef void (*mqtt_request_cb_t)(void *arg, err_t err);
153 * Pending request item, binds application callback to pending server requests
155 struct mqtt_request_t
157 /** Next item in list, NULL means this is the last in chain,
158 next pointing at itself means request is unallocated */
159 struct mqtt_request_t *next;
160 /** Callback to upper layer */
161 mqtt_request_cb_t cb;
163 /** MQTT packet identifier */
165 /** Expire time relative to element before this */
170 struct mqtt_ringbuf_t {
173 u8_t buf[MQTT_OUTPUT_RINGBUF_SIZE];
179 /** Timers and timeouts */
182 u16_t server_watchdog;
183 /** Packet identifier generator*/
185 /** Packet identifier of pending incoming publish */
187 /** Connection state */
189 struct tcp_pcb *conn;
190 /** Connection callback */
192 mqtt_connection_cb_t connect_cb;
193 /** Pending requests to server */
194 struct mqtt_request_t *pend_req_queue;
195 struct mqtt_request_t req_list[MQTT_REQ_MAX_IN_FLIGHT];
197 /** Incoming data callback */
198 mqtt_incoming_data_cb_t data_cb;
199 mqtt_incoming_publish_cb_t pub_cb;
202 u8_t rx_buffer[MQTT_VAR_HEADER_BUFFER_LEN];
203 /** Output ring-buffer */
204 struct mqtt_ringbuf_t output;
208 /** Connect to server */
209 err_t mqtt_client_connect(mqtt_client_t *client, const ip_addr_t *ipaddr, u16_t port, mqtt_connection_cb_t cb, void *arg,
210 const struct mqtt_connect_client_info_t *client_info);
212 /** Disconnect from server */
213 void mqtt_disconnect(mqtt_client_t *client);
215 /** Create new client */
216 mqtt_client_t *mqtt_client_new(void);
218 /** Check connection status */
219 u8_t mqtt_client_is_connected(mqtt_client_t *client);
221 /** Set callback to call for incoming publish */
222 void mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t,
223 mqtt_incoming_data_cb_t data_cb, void *arg);
225 /** Common function for subscribe and unsubscribe */
226 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);
229 *Subscribe to topic */
230 #define mqtt_subscribe(client, topic, qos, cb, arg) mqtt_sub_unsub(client, topic, qos, cb, arg, 1)
232 * Unsubscribe to topic */
233 #define mqtt_unsubscribe(client, topic, cb, arg) mqtt_sub_unsub(client, topic, 0, cb, arg, 0)
236 /** Publish data to topic */
237 err_t mqtt_publish(mqtt_client_t *client, const char *topic, const void *payload, u16_t payload_length, u8_t qos, u8_t retain,
238 mqtt_request_cb_t cb, void *arg);
244 #endif /* LWIP_HDR_APPS_MQTT_CLIENT_H */