Prusa MINI Firmware overview
mqtt.h
Go to the documentation of this file.
1 /**
2  * @file
3  * MQTT client
4  */
5 
6 /*
7  * Copyright (c) 2016 Erik Andersson
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
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.
20  *
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
30  * OF SUCH DAMAGE.
31  *
32  * This file is part of the lwIP TCP/IP stack.
33  *
34  * Author: Erik Andersson
35  *
36  */
37 #ifndef LWIP_HDR_APPS_MQTT_CLIENT_H
38 #define LWIP_HDR_APPS_MQTT_CLIENT_H
39 
40 #include "lwip/apps/mqtt_opts.h"
41 #include "lwip/err.h"
42 #include "lwip/ip_addr.h"
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
49 
50 /** @ingroup mqtt
51  * Default MQTT port */
52 #define MQTT_PORT 1883
53 
54 /*---------------------------------------------------------------------------------------------- */
55 /* Connection with server */
56 
57 /**
58  * @ingroup mqtt
59  * Client information and connection parameters */
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;
71  const char* will_msg;
74 };
75 
76 /**
77  * @ingroup mqtt
78  * Connection status codes */
79 typedef enum
80 {
90 
91 /**
92  * @ingroup mqtt
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
96  *
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
100  *
101  */
103 
104 
105 /**
106  * @ingroup mqtt
107  * Data callback flags */
108 enum {
109  /** Flag set when last fragment of data arrives in data callback */
111 };
112 
113 /**
114  * @ingroup mqtt
115  * Function prototype for MQTT incoming publish data callback function. Called when data
116  * arrives to a subscribed topic @see mqtt_subscribe
117  *
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
123  *
124  */
125 typedef void (*mqtt_incoming_data_cb_t)(void *arg, const u8_t *data, u16_t len, u8_t flags);
126 
127 
128 /**
129  * @ingroup mqtt
130  * Function prototype for MQTT incoming publish function. Called when an incoming publish
131  * arrives to a subscribed topic @see mqtt_subscribe
132  *
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
136  */
137 typedef void (*mqtt_incoming_publish_cb_t)(void *arg, const char *topic, u32_t tot_len);
138 
139 
140 /**
141  * @ingroup mqtt
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
148  */
149 typedef void (*mqtt_request_cb_t)(void *arg, err_t err);
150 
151 
152 /**
153  * Pending request item, binds application callback to pending server requests
154  */
156 {
157  /** Next item in list, NULL means this is the last in chain,
158  next pointing at itself means request is unallocated */
160  /** Callback to upper layer */
162  void *arg;
163  /** MQTT packet identifier */
165  /** Expire time relative to element before this */
167 };
168 
169 /** Ring buffer */
174 };
175 
176 /** MQTT client */
178 {
179  /** Timers and timeouts */
183  /** Packet identifier generator*/
185  /** Packet identifier of pending incoming publish */
187  /** Connection state */
189  struct tcp_pcb *conn;
190  /** Connection callback */
191  void *connect_arg;
193  /** Pending requests to server */
196  void *inpub_arg;
197  /** Incoming data callback */
200  /** Input */
203  /** Output ring-buffer */
205 };
206 
207 
208 /** Connect to server */
210  const struct mqtt_connect_client_info_t *client_info);
211 
212 /** Disconnect from server */
213 void mqtt_disconnect(mqtt_client_t *client);
214 
215 /** Create new client */
217 
218 /** Check connection status */
220 
221 /** Set callback to call for incoming publish */
223  mqtt_incoming_data_cb_t data_cb, void *arg);
224 
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);
227 
228 /** @ingroup mqtt
229  *Subscribe to topic */
230 #define mqtt_subscribe(client, topic, qos, cb, arg) mqtt_sub_unsub(client, topic, qos, cb, arg, 1)
231 /** @ingroup mqtt
232  * Unsubscribe to topic */
233 #define mqtt_unsubscribe(client, topic, cb, arg) mqtt_sub_unsub(client, topic, 0, cb, arg, 0)
234 
235 
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);
239 
240 #ifdef __cplusplus
241 }
242 #endif
243 
244 #endif /* LWIP_HDR_APPS_MQTT_CLIENT_H */
MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_
Definition: mqtt.h:86
MQTT_CONNECT_REFUSED_IDENTIFIER
Definition: mqtt.h:83
MQTT_CONNECT_REFUSED_USERNAME_PASS
Definition: mqtt.h:85
mqtt_connect_client_info_t::client_user
const char * client_user
Definition: mqtt.h:64
mqtt_client_connect
err_t mqtt_client_connect(mqtt_client_t *client, const ip_addr_t *ipaddr, u16_t port, mqtt_connection_cb_t cb, void *arg, const struct mqtt_connect_client_info_t *client_info)
mqtt_connect_client_info_t::will_retain
u8_t will_retain
Definition: mqtt.h:73
ipaddr
ip4_addr_t ipaddr
Definition: lwip.c:73
mqtt_connect_client_info_t
Definition: mqtt.h:60
mqtt_connect_client_info_t::will_msg
const char * will_msg
Definition: mqtt.h:71
mqtt_client_t::output
struct mqtt_ringbuf_t output
Definition: mqtt.h:204
mqtt_client_t::conn_state
u8_t conn_state
Definition: mqtt.h:188
u16_t
uint16_t u16_t
Definition: arch.h:121
mqtt_set_inpub_callback
void mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t, mqtt_incoming_data_cb_t data_cb, void *arg)
mqtt_connection_cb_t
void(* mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
Definition: mqtt.h:102
mqtt_incoming_data_cb_t
void(* mqtt_incoming_data_cb_t)(void *arg, const u8_t *data, u16_t len, u8_t flags)
Definition: mqtt.h:125
mqtt_incoming_publish_cb_t
void(* mqtt_incoming_publish_cb_t)(void *arg, const char *topic, u32_t tot_len)
Definition: mqtt.h:137
MQTT_REQ_MAX_IN_FLIGHT
#define MQTT_REQ_MAX_IN_FLIGHT
Definition: mqtt_opts.h:71
MQTT_CONNECT_TIMEOUT
Definition: mqtt.h:88
data
uint8_t data[8]
Definition: masstorage.h:49
u32_t
uint32_t u32_t
Definition: arch.h:123
mqtt_request_t
Definition: mqtt.h:155
mqtt_client_t::pend_req_queue
struct mqtt_request_t * pend_req_queue
Definition: mqtt.h:194
MQTT_VAR_HEADER_BUFFER_LEN
#define MQTT_VAR_HEADER_BUFFER_LEN
Definition: mqtt_opts.h:64
mqtt_connect_client_info_t::keep_alive
u16_t keep_alive
Definition: mqtt.h:67
mqtt_connect_client_info_t::will_topic
const char * will_topic
Definition: mqtt.h:70
mqtt_client_t::pub_cb
mqtt_incoming_publish_cb_t pub_cb
Definition: mqtt.h:199
mqtt_publish
err_t mqtt_publish(mqtt_client_t *client, const char *topic, const void *payload, u16_t payload_length, u8_t qos, u8_t retain, mqtt_request_cb_t cb, void *arg)
mqtt_connect_client_info_t::will_qos
u8_t will_qos
Definition: mqtt.h:72
MQTT_CONNECT_REFUSED_PROTOCOL_VERSION
Definition: mqtt.h:82
MQTT_CONNECT_REFUSED_SERVER
Definition: mqtt.h:84
mqtt_opts.h
mqtt_request_t::pkt_id
u16_t pkt_id
Definition: mqtt.h:164
mqtt_client_t::msg_idx
u32_t msg_idx
Definition: mqtt.h:201
mqtt_client_t
Definition: mqtt.h:177
mqtt_connect_client_info_t::client_id
const char * client_id
Definition: mqtt.h:62
u8_t
uint8_t u8_t
Definition: arch.h:119
err.h
mqtt_client_t::data_cb
mqtt_incoming_data_cb_t data_cb
Definition: mqtt.h:198
ip_addr_t
ip6_addr_t ip_addr_t
Definition: ip_addr.h:290
mqtt_client_t::inpub_pkt_id
u16_t inpub_pkt_id
Definition: mqtt.h:186
mqtt_client_t::inpub_arg
void * inpub_arg
Definition: mqtt.h:196
void
void
Definition: png.h:1083
mqtt_disconnect
void mqtt_disconnect(mqtt_client_t *client)
mqtt_ringbuf_t
Definition: mqtt.h:170
mqtt_request_t::cb
mqtt_request_cb_t cb
Definition: mqtt.h:161
MQTT_CONNECT_ACCEPTED
Definition: mqtt.h:81
mqtt_request_t::timeout_diff
u16_t timeout_diff
Definition: mqtt.h:166
mqtt_client_t::keep_alive
u16_t keep_alive
Definition: mqtt.h:181
mqtt_connection_status_t
mqtt_connection_status_t
Definition: mqtt.h:79
err_t
s8_t err_t
Definition: err.h:57
mqtt_client_t::rx_buffer
u8_t rx_buffer[MQTT_VAR_HEADER_BUFFER_LEN]
Definition: mqtt.h:202
mqtt_client_new
mqtt_client_t * mqtt_client_new(void)
mqtt_client_t::req_list
struct mqtt_request_t req_list[MQTT_REQ_MAX_IN_FLIGHT]
Definition: mqtt.h:195
mqtt_ringbuf_t::put
u16_t put
Definition: mqtt.h:171
mqtt_ringbuf_t::buf
u8_t buf[MQTT_OUTPUT_RINGBUF_SIZE]
Definition: mqtt.h:173
mqtt_client_t::cyclic_tick
u16_t cyclic_tick
Definition: mqtt.h:180
mqtt_client_t::conn
struct tcp_pcb * conn
Definition: mqtt.h:189
mqtt_client_is_connected
u8_t mqtt_client_is_connected(mqtt_client_t *client)
MQTT_DATA_FLAG_LAST
Definition: mqtt.h:110
status
static status_t status
Definition: filament_sensor.c:37
mqtt_client_t::pkt_id_seq
u16_t pkt_id_seq
Definition: mqtt.h:184
mqtt_client_t::connect_cb
mqtt_connection_cb_t connect_cb
Definition: mqtt.h:192
mqtt_request_t::next
struct mqtt_request_t * next
Definition: mqtt.h:159
mqtt_client_t::connect_arg
void * connect_arg
Definition: mqtt.h:191
mqtt_connect_client_info_t::client_pass
const char * client_pass
Definition: mqtt.h:65
MQTT_CONNECT_DISCONNECTED
Definition: mqtt.h:87
mqtt_request_t::arg
void * arg
Definition: mqtt.h:162
mqtt_request_cb_t
void(* mqtt_request_cb_t)(void *arg, err_t err)
Definition: mqtt.h:149
mqtt_ringbuf_t::get
u16_t get
Definition: mqtt.h:172
ip_addr.h
MQTT_OUTPUT_RINGBUF_SIZE
#define MQTT_OUTPUT_RINGBUF_SIZE
Definition: mqtt_opts.h:56
mqtt_sub_unsub
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)
mqtt_client_t::server_watchdog
u16_t server_watchdog
Definition: mqtt.h:182