UbixOS V2 2.0

mqtt.h
Go to the documentation of this file.
1
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
48 typedef struct mqtt_client_t mqtt_client_t;
49
52 #define MQTT_PORT 1883
53
54 /*---------------------------------------------------------------------------------------------- */
55 /* Connection with server */
56
60 struct mqtt_connect_client_info_t {
62 const char *client_id;
64 const char* client_user;
65 const char* client_pass;
67 u16_t keep_alive;
70 const char* will_topic;
71 const char* will_msg;
72 u8_t will_qos;
73 u8_t will_retain;
74 };
75
79 typedef enum
80 {
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;
90
102 typedef void (*mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_t status);
103
104
108 enum {
110 MQTT_DATA_FLAG_LAST = 1
111 };
112
125 typedef void (*mqtt_incoming_data_cb_t)(void *arg, const u8_t *data, u16_t len, u8_t flags);
126
127
137 typedef void (*mqtt_incoming_publish_cb_t)(void *arg, const char *topic, u32_t tot_len);
138
139
149 typedef void (*mqtt_request_cb_t)(void *arg, err_t err);
150
151
155 struct mqtt_request_t
156 {
159 struct mqtt_request_t *next;
161 mqtt_request_cb_t cb;
162 void *arg;
164 u16_t pkt_id;
166 u16_t timeout_diff;
167 };
168
170 struct mqtt_ringbuf_t {
171 u16_t put;
172 u16_t get;
173 u8_t buf[MQTT_OUTPUT_RINGBUF_SIZE];
174 };
175
177 struct mqtt_client_t
178 {
180 u16_t cyclic_tick;
181 u16_t keep_alive;
182 u16_t server_watchdog;
184 u16_t pkt_id_seq;
186 u16_t inpub_pkt_id;
188 u8_t conn_state;
189 struct tcp_pcb *conn;
191 void *connect_arg;
192 mqtt_connection_cb_t connect_cb;
194 struct mqtt_request_t *pend_req_queue;
195 struct mqtt_request_t req_list[MQTT_REQ_MAX_IN_FLIGHT];
196 void *inpub_arg;
198 mqtt_incoming_data_cb_t data_cb;
199 mqtt_incoming_publish_cb_t pub_cb;
201 u32_t msg_idx;
202 u8_t rx_buffer[MQTT_VAR_HEADER_BUFFER_LEN];
204 struct mqtt_ringbuf_t output;
205 };
206
207
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);
211
213 void mqtt_disconnect(mqtt_client_t *client);
214
216 mqtt_client_t *mqtt_client_new(void);
217
219 u8_t mqtt_client_is_connected(mqtt_client_t *client);
220
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);
224
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
230 #define mqtt_subscribe(client, topic, qos, cb, arg) mqtt_sub_unsub(client, topic, qos, cb, arg, 1)
231
233 #define mqtt_unsubscribe(client, topic, cb, arg) mqtt_sub_unsub(client, topic, 0, cb, arg, 0)
234
235
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_client_info_t::client_user
const char * client_user
Definition: mqtt.h:64
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_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_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
MQTT_DATA_FLAG_LAST
Definition: mqtt.h:110
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:124
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_REQ_MAX_IN_FLIGHT
#define MQTT_REQ_MAX_IN_FLIGHT
Definition: mqtt_opts.h:71
mqtt_connection_status_t
mqtt_connection_status_t
Definition: mqtt.h:79
u32_t
uint32_t u32_t
Definition: arch.h:126
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_CONNECT_REFUSED_IDENTIFIER
Definition: mqtt.h:83
MQTT_CONNECT_REFUSED_SERVER
Definition: mqtt.h:84
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_TIMEOUT
Definition: mqtt.h:88
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:122
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
buf
Definition: buf.h:35
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
MQTT_CONNECT_REFUSED_USERNAME_PASS
Definition: mqtt.h:85
mqtt_disconnect
void mqtt_disconnect(mqtt_client_t *client)
mqtt_ringbuf_t
Definition: mqtt.h:170
MQTT_CONNECT_DISCONNECTED
Definition: mqtt.h:87
mqtt_request_cb_t
void(* mqtt_request_cb_t)(void *arg, err_t err)
Definition: mqtt.h:149
mqtt_request_t::cb
mqtt_request_cb_t cb
Definition: mqtt.h:161
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_CONNECT_REFUSED_PROTOCOL_VERSION
Definition: mqtt.h:82
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_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_CONNECT_ACCEPTED
Definition: mqtt.h:81
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_connection_cb_t
void(* mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
Definition: mqtt.h:102
mqtt_request_t::arg
void * arg
Definition: mqtt.h:162
mqtt_ringbuf_t::get
u16_t get
Definition: mqtt.h:172
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
MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_
Definition: mqtt.h:86