1*d6eebaa4SHervé Poussineau /**
2*d6eebaa4SHervé Poussineau * @file
3*d6eebaa4SHervé Poussineau * MQTT client
4*d6eebaa4SHervé Poussineau *
5*d6eebaa4SHervé Poussineau * @defgroup mqtt MQTT client
6*d6eebaa4SHervé Poussineau * @ingroup apps
7*d6eebaa4SHervé Poussineau * @verbinclude mqtt_client.txt
8*d6eebaa4SHervé Poussineau */
9*d6eebaa4SHervé Poussineau
10*d6eebaa4SHervé Poussineau /*
11*d6eebaa4SHervé Poussineau * Copyright (c) 2016 Erik Andersson <erian747@gmail.com>
12*d6eebaa4SHervé Poussineau * All rights reserved.
13*d6eebaa4SHervé Poussineau *
14*d6eebaa4SHervé Poussineau * Redistribution and use in source and binary forms, with or without modification,
15*d6eebaa4SHervé Poussineau * are permitted provided that the following conditions are met:
16*d6eebaa4SHervé Poussineau *
17*d6eebaa4SHervé Poussineau * 1. Redistributions of source code must retain the above copyright notice,
18*d6eebaa4SHervé Poussineau * this list of conditions and the following disclaimer.
19*d6eebaa4SHervé Poussineau * 2. Redistributions in binary form must reproduce the above copyright notice,
20*d6eebaa4SHervé Poussineau * this list of conditions and the following disclaimer in the documentation
21*d6eebaa4SHervé Poussineau * and/or other materials provided with the distribution.
22*d6eebaa4SHervé Poussineau * 3. The name of the author may not be used to endorse or promote products
23*d6eebaa4SHervé Poussineau * derived from this software without specific prior written permission.
24*d6eebaa4SHervé Poussineau *
25*d6eebaa4SHervé Poussineau * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
26*d6eebaa4SHervé Poussineau * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27*d6eebaa4SHervé Poussineau * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
28*d6eebaa4SHervé Poussineau * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29*d6eebaa4SHervé Poussineau * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
30*d6eebaa4SHervé Poussineau * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31*d6eebaa4SHervé Poussineau * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32*d6eebaa4SHervé Poussineau * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33*d6eebaa4SHervé Poussineau * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34*d6eebaa4SHervé Poussineau * OF SUCH DAMAGE.
35*d6eebaa4SHervé Poussineau *
36*d6eebaa4SHervé Poussineau * This file is part of the lwIP TCP/IP stack
37*d6eebaa4SHervé Poussineau *
38*d6eebaa4SHervé Poussineau * Author: Erik Andersson <erian747@gmail.com>
39*d6eebaa4SHervé Poussineau *
40*d6eebaa4SHervé Poussineau *
41*d6eebaa4SHervé Poussineau * @todo:
42*d6eebaa4SHervé Poussineau * - Handle large outgoing payloads for PUBLISH messages
43*d6eebaa4SHervé Poussineau * - Fix restriction of a single topic in each (UN)SUBSCRIBE message (protocol has support for multiple topics)
44*d6eebaa4SHervé Poussineau * - Add support for legacy MQTT protocol version
45*d6eebaa4SHervé Poussineau *
46*d6eebaa4SHervé Poussineau * Please coordinate changes and requests with Erik Andersson
47*d6eebaa4SHervé Poussineau * Erik Andersson <erian747@gmail.com>
48*d6eebaa4SHervé Poussineau *
49*d6eebaa4SHervé Poussineau */
50*d6eebaa4SHervé Poussineau #include "lwip/apps/mqtt.h"
51*d6eebaa4SHervé Poussineau #include "lwip/apps/mqtt_priv.h"
52*d6eebaa4SHervé Poussineau #include "lwip/timeouts.h"
53*d6eebaa4SHervé Poussineau #include "lwip/ip_addr.h"
54*d6eebaa4SHervé Poussineau #include "lwip/mem.h"
55*d6eebaa4SHervé Poussineau #include "lwip/err.h"
56*d6eebaa4SHervé Poussineau #include "lwip/pbuf.h"
57*d6eebaa4SHervé Poussineau #include "lwip/altcp.h"
58*d6eebaa4SHervé Poussineau #include "lwip/altcp_tcp.h"
59*d6eebaa4SHervé Poussineau #include "lwip/altcp_tls.h"
60*d6eebaa4SHervé Poussineau #include <string.h>
61*d6eebaa4SHervé Poussineau
62*d6eebaa4SHervé Poussineau #if LWIP_TCP && LWIP_CALLBACK_API
63*d6eebaa4SHervé Poussineau
64*d6eebaa4SHervé Poussineau /**
65*d6eebaa4SHervé Poussineau * MQTT_DEBUG: Default is off.
66*d6eebaa4SHervé Poussineau */
67*d6eebaa4SHervé Poussineau #if !defined MQTT_DEBUG || defined __DOXYGEN__
68*d6eebaa4SHervé Poussineau #define MQTT_DEBUG LWIP_DBG_OFF
69*d6eebaa4SHervé Poussineau #endif
70*d6eebaa4SHervé Poussineau
71*d6eebaa4SHervé Poussineau #define MQTT_DEBUG_TRACE (MQTT_DEBUG | LWIP_DBG_TRACE)
72*d6eebaa4SHervé Poussineau #define MQTT_DEBUG_STATE (MQTT_DEBUG | LWIP_DBG_STATE)
73*d6eebaa4SHervé Poussineau #define MQTT_DEBUG_WARN (MQTT_DEBUG | LWIP_DBG_LEVEL_WARNING)
74*d6eebaa4SHervé Poussineau #define MQTT_DEBUG_WARN_STATE (MQTT_DEBUG | LWIP_DBG_LEVEL_WARNING | LWIP_DBG_STATE)
75*d6eebaa4SHervé Poussineau #define MQTT_DEBUG_SERIOUS (MQTT_DEBUG | LWIP_DBG_LEVEL_SERIOUS)
76*d6eebaa4SHervé Poussineau
77*d6eebaa4SHervé Poussineau
78*d6eebaa4SHervé Poussineau
79*d6eebaa4SHervé Poussineau /**
80*d6eebaa4SHervé Poussineau * MQTT client connection states
81*d6eebaa4SHervé Poussineau */
82*d6eebaa4SHervé Poussineau enum {
83*d6eebaa4SHervé Poussineau TCP_DISCONNECTED,
84*d6eebaa4SHervé Poussineau TCP_CONNECTING,
85*d6eebaa4SHervé Poussineau MQTT_CONNECTING,
86*d6eebaa4SHervé Poussineau MQTT_CONNECTED
87*d6eebaa4SHervé Poussineau };
88*d6eebaa4SHervé Poussineau
89*d6eebaa4SHervé Poussineau /**
90*d6eebaa4SHervé Poussineau * MQTT control message types
91*d6eebaa4SHervé Poussineau */
92*d6eebaa4SHervé Poussineau enum mqtt_message_type {
93*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_CONNECT = 1,
94*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_CONNACK = 2,
95*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PUBLISH = 3,
96*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PUBACK = 4,
97*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PUBREC = 5,
98*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PUBREL = 6,
99*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PUBCOMP = 7,
100*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_SUBSCRIBE = 8,
101*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_SUBACK = 9,
102*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
103*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_UNSUBACK = 11,
104*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PINGREQ = 12,
105*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_PINGRESP = 13,
106*d6eebaa4SHervé Poussineau MQTT_MSG_TYPE_DISCONNECT = 14
107*d6eebaa4SHervé Poussineau };
108*d6eebaa4SHervé Poussineau
109*d6eebaa4SHervé Poussineau /** Helpers to extract control packet type and qos from first byte in fixed header */
110*d6eebaa4SHervé Poussineau #define MQTT_CTL_PACKET_TYPE(fixed_hdr_byte0) ((fixed_hdr_byte0 & 0xf0) >> 4)
111*d6eebaa4SHervé Poussineau #define MQTT_CTL_PACKET_QOS(fixed_hdr_byte0) ((fixed_hdr_byte0 & 0x6) >> 1)
112*d6eebaa4SHervé Poussineau
113*d6eebaa4SHervé Poussineau /**
114*d6eebaa4SHervé Poussineau * MQTT connect flags, only used in CONNECT message
115*d6eebaa4SHervé Poussineau */
116*d6eebaa4SHervé Poussineau enum mqtt_connect_flag {
117*d6eebaa4SHervé Poussineau MQTT_CONNECT_FLAG_USERNAME = 1 << 7,
118*d6eebaa4SHervé Poussineau MQTT_CONNECT_FLAG_PASSWORD = 1 << 6,
119*d6eebaa4SHervé Poussineau MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5,
120*d6eebaa4SHervé Poussineau MQTT_CONNECT_FLAG_WILL = 1 << 2,
121*d6eebaa4SHervé Poussineau MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1
122*d6eebaa4SHervé Poussineau };
123*d6eebaa4SHervé Poussineau
124*d6eebaa4SHervé Poussineau
125*d6eebaa4SHervé Poussineau static void mqtt_cyclic_timer(void *arg);
126*d6eebaa4SHervé Poussineau
127*d6eebaa4SHervé Poussineau #if defined(LWIP_DEBUG)
128*d6eebaa4SHervé Poussineau static const char *const mqtt_message_type_str[15] = {
129*d6eebaa4SHervé Poussineau "UNDEFINED",
130*d6eebaa4SHervé Poussineau "CONNECT",
131*d6eebaa4SHervé Poussineau "CONNACK",
132*d6eebaa4SHervé Poussineau "PUBLISH",
133*d6eebaa4SHervé Poussineau "PUBACK",
134*d6eebaa4SHervé Poussineau "PUBREC",
135*d6eebaa4SHervé Poussineau "PUBREL",
136*d6eebaa4SHervé Poussineau "PUBCOMP",
137*d6eebaa4SHervé Poussineau "SUBSCRIBE",
138*d6eebaa4SHervé Poussineau "SUBACK",
139*d6eebaa4SHervé Poussineau "UNSUBSCRIBE",
140*d6eebaa4SHervé Poussineau "UNSUBACK",
141*d6eebaa4SHervé Poussineau "PINGREQ",
142*d6eebaa4SHervé Poussineau "PINGRESP",
143*d6eebaa4SHervé Poussineau "DISCONNECT"
144*d6eebaa4SHervé Poussineau };
145*d6eebaa4SHervé Poussineau
146*d6eebaa4SHervé Poussineau /**
147*d6eebaa4SHervé Poussineau * Message type value to string
148*d6eebaa4SHervé Poussineau * @param msg_type see enum mqtt_message_type
149*d6eebaa4SHervé Poussineau *
150*d6eebaa4SHervé Poussineau * @return Control message type text string
151*d6eebaa4SHervé Poussineau */
152*d6eebaa4SHervé Poussineau static const char *
mqtt_msg_type_to_str(u8_t msg_type)153*d6eebaa4SHervé Poussineau mqtt_msg_type_to_str(u8_t msg_type)
154*d6eebaa4SHervé Poussineau {
155*d6eebaa4SHervé Poussineau if (msg_type >= LWIP_ARRAYSIZE(mqtt_message_type_str)) {
156*d6eebaa4SHervé Poussineau msg_type = 0;
157*d6eebaa4SHervé Poussineau }
158*d6eebaa4SHervé Poussineau return mqtt_message_type_str[msg_type];
159*d6eebaa4SHervé Poussineau }
160*d6eebaa4SHervé Poussineau
161*d6eebaa4SHervé Poussineau #endif
162*d6eebaa4SHervé Poussineau
163*d6eebaa4SHervé Poussineau
164*d6eebaa4SHervé Poussineau /**
165*d6eebaa4SHervé Poussineau * Generate MQTT packet identifier
166*d6eebaa4SHervé Poussineau * @param client MQTT client
167*d6eebaa4SHervé Poussineau * @return New packet identifier, range 1 to 65535
168*d6eebaa4SHervé Poussineau */
169*d6eebaa4SHervé Poussineau static u16_t
msg_generate_packet_id(mqtt_client_t * client)170*d6eebaa4SHervé Poussineau msg_generate_packet_id(mqtt_client_t *client)
171*d6eebaa4SHervé Poussineau {
172*d6eebaa4SHervé Poussineau client->pkt_id_seq++;
173*d6eebaa4SHervé Poussineau if (client->pkt_id_seq == 0) {
174*d6eebaa4SHervé Poussineau client->pkt_id_seq++;
175*d6eebaa4SHervé Poussineau }
176*d6eebaa4SHervé Poussineau return client->pkt_id_seq;
177*d6eebaa4SHervé Poussineau }
178*d6eebaa4SHervé Poussineau
179*d6eebaa4SHervé Poussineau /*--------------------------------------------------------------------------------------------------------------------- */
180*d6eebaa4SHervé Poussineau /* Output ring buffer */
181*d6eebaa4SHervé Poussineau
182*d6eebaa4SHervé Poussineau /** Add single item to ring buffer */
183*d6eebaa4SHervé Poussineau static void
mqtt_ringbuf_put(struct mqtt_ringbuf_t * rb,u8_t item)184*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(struct mqtt_ringbuf_t *rb, u8_t item)
185*d6eebaa4SHervé Poussineau {
186*d6eebaa4SHervé Poussineau rb->buf[rb->put] = item;
187*d6eebaa4SHervé Poussineau rb->put++;
188*d6eebaa4SHervé Poussineau if (rb->put >= MQTT_OUTPUT_RINGBUF_SIZE) {
189*d6eebaa4SHervé Poussineau rb->put = 0;
190*d6eebaa4SHervé Poussineau }
191*d6eebaa4SHervé Poussineau }
192*d6eebaa4SHervé Poussineau
193*d6eebaa4SHervé Poussineau /** Return pointer to ring buffer get position */
194*d6eebaa4SHervé Poussineau static u8_t *
mqtt_ringbuf_get_ptr(struct mqtt_ringbuf_t * rb)195*d6eebaa4SHervé Poussineau mqtt_ringbuf_get_ptr(struct mqtt_ringbuf_t *rb)
196*d6eebaa4SHervé Poussineau {
197*d6eebaa4SHervé Poussineau return &rb->buf[rb->get];
198*d6eebaa4SHervé Poussineau }
199*d6eebaa4SHervé Poussineau
200*d6eebaa4SHervé Poussineau static void
mqtt_ringbuf_advance_get_idx(struct mqtt_ringbuf_t * rb,u16_t len)201*d6eebaa4SHervé Poussineau mqtt_ringbuf_advance_get_idx(struct mqtt_ringbuf_t *rb, u16_t len)
202*d6eebaa4SHervé Poussineau {
203*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_ringbuf_advance_get_idx: len < MQTT_OUTPUT_RINGBUF_SIZE", len < MQTT_OUTPUT_RINGBUF_SIZE);
204*d6eebaa4SHervé Poussineau
205*d6eebaa4SHervé Poussineau rb->get += len;
206*d6eebaa4SHervé Poussineau if (rb->get >= MQTT_OUTPUT_RINGBUF_SIZE) {
207*d6eebaa4SHervé Poussineau rb->get = rb->get - MQTT_OUTPUT_RINGBUF_SIZE;
208*d6eebaa4SHervé Poussineau }
209*d6eebaa4SHervé Poussineau }
210*d6eebaa4SHervé Poussineau
211*d6eebaa4SHervé Poussineau /** Return number of bytes in ring buffer */
212*d6eebaa4SHervé Poussineau static u16_t
mqtt_ringbuf_len(struct mqtt_ringbuf_t * rb)213*d6eebaa4SHervé Poussineau mqtt_ringbuf_len(struct mqtt_ringbuf_t *rb)
214*d6eebaa4SHervé Poussineau {
215*d6eebaa4SHervé Poussineau u32_t len = rb->put - rb->get;
216*d6eebaa4SHervé Poussineau if (len > 0xFFFF) {
217*d6eebaa4SHervé Poussineau len += MQTT_OUTPUT_RINGBUF_SIZE;
218*d6eebaa4SHervé Poussineau }
219*d6eebaa4SHervé Poussineau return (u16_t)len;
220*d6eebaa4SHervé Poussineau }
221*d6eebaa4SHervé Poussineau
222*d6eebaa4SHervé Poussineau /** Return number of bytes free in ring buffer */
223*d6eebaa4SHervé Poussineau #define mqtt_ringbuf_free(rb) (MQTT_OUTPUT_RINGBUF_SIZE - mqtt_ringbuf_len(rb))
224*d6eebaa4SHervé Poussineau
225*d6eebaa4SHervé Poussineau /** Return number of bytes possible to read without wrapping around */
226*d6eebaa4SHervé Poussineau #define mqtt_ringbuf_linear_read_length(rb) LWIP_MIN(mqtt_ringbuf_len(rb), (MQTT_OUTPUT_RINGBUF_SIZE - (rb)->get))
227*d6eebaa4SHervé Poussineau
228*d6eebaa4SHervé Poussineau /**
229*d6eebaa4SHervé Poussineau * Try send as many bytes as possible from output ring buffer
230*d6eebaa4SHervé Poussineau * @param rb Output ring buffer
231*d6eebaa4SHervé Poussineau * @param tpcb TCP connection handle
232*d6eebaa4SHervé Poussineau */
233*d6eebaa4SHervé Poussineau static void
mqtt_output_send(struct mqtt_ringbuf_t * rb,struct altcp_pcb * tpcb)234*d6eebaa4SHervé Poussineau mqtt_output_send(struct mqtt_ringbuf_t *rb, struct altcp_pcb *tpcb)
235*d6eebaa4SHervé Poussineau {
236*d6eebaa4SHervé Poussineau err_t err;
237*d6eebaa4SHervé Poussineau u8_t wrap = 0;
238*d6eebaa4SHervé Poussineau u16_t ringbuf_lin_len = mqtt_ringbuf_linear_read_length(rb);
239*d6eebaa4SHervé Poussineau u16_t send_len = altcp_sndbuf(tpcb);
240*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_output_send: tpcb != NULL", tpcb != NULL);
241*d6eebaa4SHervé Poussineau
242*d6eebaa4SHervé Poussineau if (send_len == 0 || ringbuf_lin_len == 0) {
243*d6eebaa4SHervé Poussineau return;
244*d6eebaa4SHervé Poussineau }
245*d6eebaa4SHervé Poussineau
246*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_output_send: tcp_sndbuf: %d bytes, ringbuf_linear_available: %d, get %d, put %d\n",
247*d6eebaa4SHervé Poussineau send_len, ringbuf_lin_len, rb->get, rb->put));
248*d6eebaa4SHervé Poussineau
249*d6eebaa4SHervé Poussineau if (send_len > ringbuf_lin_len) {
250*d6eebaa4SHervé Poussineau /* Space in TCP output buffer is larger than available in ring buffer linear portion */
251*d6eebaa4SHervé Poussineau send_len = ringbuf_lin_len;
252*d6eebaa4SHervé Poussineau /* Wrap around if more data in ring buffer after linear portion */
253*d6eebaa4SHervé Poussineau wrap = (mqtt_ringbuf_len(rb) > ringbuf_lin_len);
254*d6eebaa4SHervé Poussineau }
255*d6eebaa4SHervé Poussineau err = altcp_write(tpcb, mqtt_ringbuf_get_ptr(rb), send_len, TCP_WRITE_FLAG_COPY | (wrap ? TCP_WRITE_FLAG_MORE : 0));
256*d6eebaa4SHervé Poussineau if ((err == ERR_OK) && wrap) {
257*d6eebaa4SHervé Poussineau mqtt_ringbuf_advance_get_idx(rb, send_len);
258*d6eebaa4SHervé Poussineau /* Use the lesser one of ring buffer linear length and TCP send buffer size */
259*d6eebaa4SHervé Poussineau send_len = LWIP_MIN(altcp_sndbuf(tpcb), mqtt_ringbuf_linear_read_length(rb));
260*d6eebaa4SHervé Poussineau err = altcp_write(tpcb, mqtt_ringbuf_get_ptr(rb), send_len, TCP_WRITE_FLAG_COPY);
261*d6eebaa4SHervé Poussineau }
262*d6eebaa4SHervé Poussineau
263*d6eebaa4SHervé Poussineau if (err == ERR_OK) {
264*d6eebaa4SHervé Poussineau mqtt_ringbuf_advance_get_idx(rb, send_len);
265*d6eebaa4SHervé Poussineau /* Flush */
266*d6eebaa4SHervé Poussineau altcp_output(tpcb);
267*d6eebaa4SHervé Poussineau } else {
268*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_output_send: Send failed with err %d (\"%s\")\n", err, lwip_strerr(err)));
269*d6eebaa4SHervé Poussineau }
270*d6eebaa4SHervé Poussineau }
271*d6eebaa4SHervé Poussineau
272*d6eebaa4SHervé Poussineau
273*d6eebaa4SHervé Poussineau
274*d6eebaa4SHervé Poussineau /*--------------------------------------------------------------------------------------------------------------------- */
275*d6eebaa4SHervé Poussineau /* Request queue */
276*d6eebaa4SHervé Poussineau
277*d6eebaa4SHervé Poussineau /**
278*d6eebaa4SHervé Poussineau * Create request item
279*d6eebaa4SHervé Poussineau * @param r_objs Pointer to request objects
280*d6eebaa4SHervé Poussineau * @param r_objs_len Number of array entries
281*d6eebaa4SHervé Poussineau * @param pkt_id Packet identifier of request
282*d6eebaa4SHervé Poussineau * @param cb Packet callback to call when requests lifetime ends
283*d6eebaa4SHervé Poussineau * @param arg Parameter following callback
284*d6eebaa4SHervé Poussineau * @return Request or NULL if failed to create
285*d6eebaa4SHervé Poussineau */
286*d6eebaa4SHervé Poussineau static struct mqtt_request_t *
mqtt_create_request(struct mqtt_request_t * r_objs,size_t r_objs_len,u16_t pkt_id,mqtt_request_cb_t cb,void * arg)287*d6eebaa4SHervé Poussineau mqtt_create_request(struct mqtt_request_t *r_objs, size_t r_objs_len, u16_t pkt_id, mqtt_request_cb_t cb, void *arg)
288*d6eebaa4SHervé Poussineau {
289*d6eebaa4SHervé Poussineau struct mqtt_request_t *r = NULL;
290*d6eebaa4SHervé Poussineau u8_t n;
291*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_create_request: r_objs != NULL", r_objs != NULL);
292*d6eebaa4SHervé Poussineau for (n = 0; n < r_objs_len; n++) {
293*d6eebaa4SHervé Poussineau /* Item point to itself if not in use */
294*d6eebaa4SHervé Poussineau if (r_objs[n].next == &r_objs[n]) {
295*d6eebaa4SHervé Poussineau r = &r_objs[n];
296*d6eebaa4SHervé Poussineau r->next = NULL;
297*d6eebaa4SHervé Poussineau r->cb = cb;
298*d6eebaa4SHervé Poussineau r->arg = arg;
299*d6eebaa4SHervé Poussineau r->pkt_id = pkt_id;
300*d6eebaa4SHervé Poussineau break;
301*d6eebaa4SHervé Poussineau }
302*d6eebaa4SHervé Poussineau }
303*d6eebaa4SHervé Poussineau return r;
304*d6eebaa4SHervé Poussineau }
305*d6eebaa4SHervé Poussineau
306*d6eebaa4SHervé Poussineau
307*d6eebaa4SHervé Poussineau /**
308*d6eebaa4SHervé Poussineau * Append request to pending request queue
309*d6eebaa4SHervé Poussineau * @param tail Pointer to request queue tail pointer
310*d6eebaa4SHervé Poussineau * @param r Request to append
311*d6eebaa4SHervé Poussineau */
312*d6eebaa4SHervé Poussineau static void
mqtt_append_request(struct mqtt_request_t ** tail,struct mqtt_request_t * r)313*d6eebaa4SHervé Poussineau mqtt_append_request(struct mqtt_request_t **tail, struct mqtt_request_t *r)
314*d6eebaa4SHervé Poussineau {
315*d6eebaa4SHervé Poussineau struct mqtt_request_t *head = NULL;
316*d6eebaa4SHervé Poussineau s16_t time_before = 0;
317*d6eebaa4SHervé Poussineau struct mqtt_request_t *iter;
318*d6eebaa4SHervé Poussineau
319*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_append_request: tail != NULL", tail != NULL);
320*d6eebaa4SHervé Poussineau
321*d6eebaa4SHervé Poussineau /* Iterate trough queue to find head, and count total timeout time */
322*d6eebaa4SHervé Poussineau for (iter = *tail; iter != NULL; iter = iter->next) {
323*d6eebaa4SHervé Poussineau time_before += iter->timeout_diff;
324*d6eebaa4SHervé Poussineau head = iter;
325*d6eebaa4SHervé Poussineau }
326*d6eebaa4SHervé Poussineau
327*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_append_request: time_before <= MQTT_REQ_TIMEOUT", time_before <= MQTT_REQ_TIMEOUT);
328*d6eebaa4SHervé Poussineau r->timeout_diff = MQTT_REQ_TIMEOUT - time_before;
329*d6eebaa4SHervé Poussineau if (head == NULL) {
330*d6eebaa4SHervé Poussineau *tail = r;
331*d6eebaa4SHervé Poussineau } else {
332*d6eebaa4SHervé Poussineau head->next = r;
333*d6eebaa4SHervé Poussineau }
334*d6eebaa4SHervé Poussineau }
335*d6eebaa4SHervé Poussineau
336*d6eebaa4SHervé Poussineau
337*d6eebaa4SHervé Poussineau /**
338*d6eebaa4SHervé Poussineau * Delete request item
339*d6eebaa4SHervé Poussineau * @param r Request item to delete
340*d6eebaa4SHervé Poussineau */
341*d6eebaa4SHervé Poussineau static void
mqtt_delete_request(struct mqtt_request_t * r)342*d6eebaa4SHervé Poussineau mqtt_delete_request(struct mqtt_request_t *r)
343*d6eebaa4SHervé Poussineau {
344*d6eebaa4SHervé Poussineau if (r != NULL) {
345*d6eebaa4SHervé Poussineau r->next = r;
346*d6eebaa4SHervé Poussineau }
347*d6eebaa4SHervé Poussineau }
348*d6eebaa4SHervé Poussineau
349*d6eebaa4SHervé Poussineau /**
350*d6eebaa4SHervé Poussineau * Remove a request item with a specific packet identifier from request queue
351*d6eebaa4SHervé Poussineau * @param tail Pointer to request queue tail pointer
352*d6eebaa4SHervé Poussineau * @param pkt_id Packet identifier of request to take
353*d6eebaa4SHervé Poussineau * @return Request item if found, NULL if not
354*d6eebaa4SHervé Poussineau */
355*d6eebaa4SHervé Poussineau static struct mqtt_request_t *
mqtt_take_request(struct mqtt_request_t ** tail,u16_t pkt_id)356*d6eebaa4SHervé Poussineau mqtt_take_request(struct mqtt_request_t **tail, u16_t pkt_id)
357*d6eebaa4SHervé Poussineau {
358*d6eebaa4SHervé Poussineau struct mqtt_request_t *iter = NULL, *prev = NULL;
359*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_take_request: tail != NULL", tail != NULL);
360*d6eebaa4SHervé Poussineau /* Search all request for pkt_id */
361*d6eebaa4SHervé Poussineau for (iter = *tail; iter != NULL; iter = iter->next) {
362*d6eebaa4SHervé Poussineau if (iter->pkt_id == pkt_id) {
363*d6eebaa4SHervé Poussineau break;
364*d6eebaa4SHervé Poussineau }
365*d6eebaa4SHervé Poussineau prev = iter;
366*d6eebaa4SHervé Poussineau }
367*d6eebaa4SHervé Poussineau
368*d6eebaa4SHervé Poussineau /* If request was found */
369*d6eebaa4SHervé Poussineau if (iter != NULL) {
370*d6eebaa4SHervé Poussineau /* unchain */
371*d6eebaa4SHervé Poussineau if (prev == NULL) {
372*d6eebaa4SHervé Poussineau *tail = iter->next;
373*d6eebaa4SHervé Poussineau } else {
374*d6eebaa4SHervé Poussineau prev->next = iter->next;
375*d6eebaa4SHervé Poussineau }
376*d6eebaa4SHervé Poussineau /* If exists, add remaining timeout time for the request to next */
377*d6eebaa4SHervé Poussineau if (iter->next != NULL) {
378*d6eebaa4SHervé Poussineau iter->next->timeout_diff += iter->timeout_diff;
379*d6eebaa4SHervé Poussineau }
380*d6eebaa4SHervé Poussineau iter->next = NULL;
381*d6eebaa4SHervé Poussineau }
382*d6eebaa4SHervé Poussineau return iter;
383*d6eebaa4SHervé Poussineau }
384*d6eebaa4SHervé Poussineau
385*d6eebaa4SHervé Poussineau /**
386*d6eebaa4SHervé Poussineau * Handle requests timeout
387*d6eebaa4SHervé Poussineau * @param tail Pointer to request queue tail pointer
388*d6eebaa4SHervé Poussineau * @param t Time since last call in seconds
389*d6eebaa4SHervé Poussineau */
390*d6eebaa4SHervé Poussineau static void
mqtt_request_time_elapsed(struct mqtt_request_t ** tail,u8_t t)391*d6eebaa4SHervé Poussineau mqtt_request_time_elapsed(struct mqtt_request_t **tail, u8_t t)
392*d6eebaa4SHervé Poussineau {
393*d6eebaa4SHervé Poussineau struct mqtt_request_t *r;
394*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_request_time_elapsed: tail != NULL", tail != NULL);
395*d6eebaa4SHervé Poussineau r = *tail;
396*d6eebaa4SHervé Poussineau while (t > 0 && r != NULL) {
397*d6eebaa4SHervé Poussineau if (t >= r->timeout_diff) {
398*d6eebaa4SHervé Poussineau t -= (u8_t)r->timeout_diff;
399*d6eebaa4SHervé Poussineau /* Unchain */
400*d6eebaa4SHervé Poussineau *tail = r->next;
401*d6eebaa4SHervé Poussineau /* Notify upper layer about timeout */
402*d6eebaa4SHervé Poussineau if (r->cb != NULL) {
403*d6eebaa4SHervé Poussineau r->cb(r->arg, ERR_TIMEOUT);
404*d6eebaa4SHervé Poussineau }
405*d6eebaa4SHervé Poussineau mqtt_delete_request(r);
406*d6eebaa4SHervé Poussineau /* Tail might be be modified in callback, so re-read it in every iteration */
407*d6eebaa4SHervé Poussineau r = *(struct mqtt_request_t *const volatile *)tail;
408*d6eebaa4SHervé Poussineau } else {
409*d6eebaa4SHervé Poussineau r->timeout_diff -= t;
410*d6eebaa4SHervé Poussineau t = 0;
411*d6eebaa4SHervé Poussineau }
412*d6eebaa4SHervé Poussineau }
413*d6eebaa4SHervé Poussineau }
414*d6eebaa4SHervé Poussineau
415*d6eebaa4SHervé Poussineau /**
416*d6eebaa4SHervé Poussineau * Free all request items
417*d6eebaa4SHervé Poussineau * @param tail Pointer to request queue tail pointer
418*d6eebaa4SHervé Poussineau */
419*d6eebaa4SHervé Poussineau static void
mqtt_clear_requests(struct mqtt_request_t ** tail)420*d6eebaa4SHervé Poussineau mqtt_clear_requests(struct mqtt_request_t **tail)
421*d6eebaa4SHervé Poussineau {
422*d6eebaa4SHervé Poussineau struct mqtt_request_t *iter, *next;
423*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_clear_requests: tail != NULL", tail != NULL);
424*d6eebaa4SHervé Poussineau for (iter = *tail; iter != NULL; iter = next) {
425*d6eebaa4SHervé Poussineau next = iter->next;
426*d6eebaa4SHervé Poussineau mqtt_delete_request(iter);
427*d6eebaa4SHervé Poussineau }
428*d6eebaa4SHervé Poussineau *tail = NULL;
429*d6eebaa4SHervé Poussineau }
430*d6eebaa4SHervé Poussineau /**
431*d6eebaa4SHervé Poussineau * Initialize all request items
432*d6eebaa4SHervé Poussineau * @param r_objs Pointer to request objects
433*d6eebaa4SHervé Poussineau * @param r_objs_len Number of array entries
434*d6eebaa4SHervé Poussineau */
435*d6eebaa4SHervé Poussineau static void
mqtt_init_requests(struct mqtt_request_t * r_objs,size_t r_objs_len)436*d6eebaa4SHervé Poussineau mqtt_init_requests(struct mqtt_request_t *r_objs, size_t r_objs_len)
437*d6eebaa4SHervé Poussineau {
438*d6eebaa4SHervé Poussineau u8_t n;
439*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_init_requests: r_objs != NULL", r_objs != NULL);
440*d6eebaa4SHervé Poussineau for (n = 0; n < r_objs_len; n++) {
441*d6eebaa4SHervé Poussineau /* Item pointing to itself indicates unused */
442*d6eebaa4SHervé Poussineau r_objs[n].next = &r_objs[n];
443*d6eebaa4SHervé Poussineau }
444*d6eebaa4SHervé Poussineau }
445*d6eebaa4SHervé Poussineau
446*d6eebaa4SHervé Poussineau /*--------------------------------------------------------------------------------------------------------------------- */
447*d6eebaa4SHervé Poussineau /* Output message build helpers */
448*d6eebaa4SHervé Poussineau
449*d6eebaa4SHervé Poussineau
450*d6eebaa4SHervé Poussineau static void
mqtt_output_append_u8(struct mqtt_ringbuf_t * rb,u8_t value)451*d6eebaa4SHervé Poussineau mqtt_output_append_u8(struct mqtt_ringbuf_t *rb, u8_t value)
452*d6eebaa4SHervé Poussineau {
453*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, value);
454*d6eebaa4SHervé Poussineau }
455*d6eebaa4SHervé Poussineau
456*d6eebaa4SHervé Poussineau static
mqtt_output_append_u16(struct mqtt_ringbuf_t * rb,u16_t value)457*d6eebaa4SHervé Poussineau void mqtt_output_append_u16(struct mqtt_ringbuf_t *rb, u16_t value)
458*d6eebaa4SHervé Poussineau {
459*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, value >> 8);
460*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, value & 0xff);
461*d6eebaa4SHervé Poussineau }
462*d6eebaa4SHervé Poussineau
463*d6eebaa4SHervé Poussineau static void
mqtt_output_append_buf(struct mqtt_ringbuf_t * rb,const void * data,u16_t length)464*d6eebaa4SHervé Poussineau mqtt_output_append_buf(struct mqtt_ringbuf_t *rb, const void *data, u16_t length)
465*d6eebaa4SHervé Poussineau {
466*d6eebaa4SHervé Poussineau u16_t n;
467*d6eebaa4SHervé Poussineau for (n = 0; n < length; n++) {
468*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, ((const u8_t *)data)[n]);
469*d6eebaa4SHervé Poussineau }
470*d6eebaa4SHervé Poussineau }
471*d6eebaa4SHervé Poussineau
472*d6eebaa4SHervé Poussineau static void
mqtt_output_append_string(struct mqtt_ringbuf_t * rb,const char * str,u16_t length)473*d6eebaa4SHervé Poussineau mqtt_output_append_string(struct mqtt_ringbuf_t *rb, const char *str, u16_t length)
474*d6eebaa4SHervé Poussineau {
475*d6eebaa4SHervé Poussineau u16_t n;
476*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, length >> 8);
477*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, length & 0xff);
478*d6eebaa4SHervé Poussineau for (n = 0; n < length; n++) {
479*d6eebaa4SHervé Poussineau mqtt_ringbuf_put(rb, str[n]);
480*d6eebaa4SHervé Poussineau }
481*d6eebaa4SHervé Poussineau }
482*d6eebaa4SHervé Poussineau
483*d6eebaa4SHervé Poussineau /**
484*d6eebaa4SHervé Poussineau * Append fixed header
485*d6eebaa4SHervé Poussineau * @param rb Output ring buffer
486*d6eebaa4SHervé Poussineau * @param msg_type see enum mqtt_message_type
487*d6eebaa4SHervé Poussineau * @param fdup MQTT DUP flag
488*d6eebaa4SHervé Poussineau * @param fqos MQTT QoS field
489*d6eebaa4SHervé Poussineau * @param fretain MQTT retain flag
490*d6eebaa4SHervé Poussineau * @param r_length Remaining length after fixed header
491*d6eebaa4SHervé Poussineau */
492*d6eebaa4SHervé Poussineau
493*d6eebaa4SHervé Poussineau static void
mqtt_output_append_fixed_header(struct mqtt_ringbuf_t * rb,u8_t msg_type,u8_t fdup,u8_t fqos,u8_t fretain,u16_t r_length)494*d6eebaa4SHervé Poussineau mqtt_output_append_fixed_header(struct mqtt_ringbuf_t *rb, u8_t msg_type, u8_t fdup,
495*d6eebaa4SHervé Poussineau u8_t fqos, u8_t fretain, u16_t r_length)
496*d6eebaa4SHervé Poussineau {
497*d6eebaa4SHervé Poussineau /* Start with control byte */
498*d6eebaa4SHervé Poussineau mqtt_output_append_u8(rb, (((msg_type & 0x0f) << 4) | ((fdup & 1) << 3) | ((fqos & 3) << 1) | (fretain & 1)));
499*d6eebaa4SHervé Poussineau /* Encode remaining length field */
500*d6eebaa4SHervé Poussineau do {
501*d6eebaa4SHervé Poussineau mqtt_output_append_u8(rb, (r_length & 0x7f) | (r_length >= 128 ? 0x80 : 0));
502*d6eebaa4SHervé Poussineau r_length >>= 7;
503*d6eebaa4SHervé Poussineau } while (r_length > 0);
504*d6eebaa4SHervé Poussineau }
505*d6eebaa4SHervé Poussineau
506*d6eebaa4SHervé Poussineau
507*d6eebaa4SHervé Poussineau /**
508*d6eebaa4SHervé Poussineau * Check output buffer space
509*d6eebaa4SHervé Poussineau * @param rb Output ring buffer
510*d6eebaa4SHervé Poussineau * @param r_length Remaining length after fixed header
511*d6eebaa4SHervé Poussineau * @return 1 if message will fit, 0 if not enough buffer space
512*d6eebaa4SHervé Poussineau */
513*d6eebaa4SHervé Poussineau static u8_t
mqtt_output_check_space(struct mqtt_ringbuf_t * rb,u16_t r_length)514*d6eebaa4SHervé Poussineau mqtt_output_check_space(struct mqtt_ringbuf_t *rb, u16_t r_length)
515*d6eebaa4SHervé Poussineau {
516*d6eebaa4SHervé Poussineau /* Start with length of type byte + remaining length */
517*d6eebaa4SHervé Poussineau u16_t total_len = 1 + r_length;
518*d6eebaa4SHervé Poussineau
519*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_output_check_space: rb != NULL", rb != NULL);
520*d6eebaa4SHervé Poussineau
521*d6eebaa4SHervé Poussineau /* Calculate number of required bytes to contain the remaining bytes field and add to total*/
522*d6eebaa4SHervé Poussineau do {
523*d6eebaa4SHervé Poussineau total_len++;
524*d6eebaa4SHervé Poussineau r_length >>= 7;
525*d6eebaa4SHervé Poussineau } while (r_length > 0);
526*d6eebaa4SHervé Poussineau
527*d6eebaa4SHervé Poussineau return (total_len <= mqtt_ringbuf_free(rb));
528*d6eebaa4SHervé Poussineau }
529*d6eebaa4SHervé Poussineau
530*d6eebaa4SHervé Poussineau
531*d6eebaa4SHervé Poussineau /**
532*d6eebaa4SHervé Poussineau * Close connection to server
533*d6eebaa4SHervé Poussineau * @param client MQTT client
534*d6eebaa4SHervé Poussineau * @param reason Reason for disconnection
535*d6eebaa4SHervé Poussineau */
536*d6eebaa4SHervé Poussineau static void
mqtt_close(mqtt_client_t * client,mqtt_connection_status_t reason)537*d6eebaa4SHervé Poussineau mqtt_close(mqtt_client_t *client, mqtt_connection_status_t reason)
538*d6eebaa4SHervé Poussineau {
539*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_close: client != NULL", client != NULL);
540*d6eebaa4SHervé Poussineau
541*d6eebaa4SHervé Poussineau /* Bring down TCP connection if not already done */
542*d6eebaa4SHervé Poussineau if (client->conn != NULL) {
543*d6eebaa4SHervé Poussineau err_t res;
544*d6eebaa4SHervé Poussineau altcp_recv(client->conn, NULL);
545*d6eebaa4SHervé Poussineau altcp_err(client->conn, NULL);
546*d6eebaa4SHervé Poussineau altcp_sent(client->conn, NULL);
547*d6eebaa4SHervé Poussineau res = altcp_close(client->conn);
548*d6eebaa4SHervé Poussineau if (res != ERR_OK) {
549*d6eebaa4SHervé Poussineau altcp_abort(client->conn);
550*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_close: Close err=%s\n", lwip_strerr(res)));
551*d6eebaa4SHervé Poussineau }
552*d6eebaa4SHervé Poussineau client->conn = NULL;
553*d6eebaa4SHervé Poussineau }
554*d6eebaa4SHervé Poussineau
555*d6eebaa4SHervé Poussineau /* Remove all pending requests */
556*d6eebaa4SHervé Poussineau mqtt_clear_requests(&client->pend_req_queue);
557*d6eebaa4SHervé Poussineau /* Stop cyclic timer */
558*d6eebaa4SHervé Poussineau sys_untimeout(mqtt_cyclic_timer, client);
559*d6eebaa4SHervé Poussineau
560*d6eebaa4SHervé Poussineau /* Notify upper layer of disconnection if changed state */
561*d6eebaa4SHervé Poussineau if (client->conn_state != TCP_DISCONNECTED) {
562*d6eebaa4SHervé Poussineau
563*d6eebaa4SHervé Poussineau client->conn_state = TCP_DISCONNECTED;
564*d6eebaa4SHervé Poussineau if (client->connect_cb != NULL) {
565*d6eebaa4SHervé Poussineau client->connect_cb(client, client->connect_arg, reason);
566*d6eebaa4SHervé Poussineau }
567*d6eebaa4SHervé Poussineau }
568*d6eebaa4SHervé Poussineau }
569*d6eebaa4SHervé Poussineau
570*d6eebaa4SHervé Poussineau
571*d6eebaa4SHervé Poussineau /**
572*d6eebaa4SHervé Poussineau * Interval timer, called every MQTT_CYCLIC_TIMER_INTERVAL seconds in MQTT_CONNECTING and MQTT_CONNECTED states
573*d6eebaa4SHervé Poussineau * @param arg MQTT client
574*d6eebaa4SHervé Poussineau */
575*d6eebaa4SHervé Poussineau static void
mqtt_cyclic_timer(void * arg)576*d6eebaa4SHervé Poussineau mqtt_cyclic_timer(void *arg)
577*d6eebaa4SHervé Poussineau {
578*d6eebaa4SHervé Poussineau u8_t restart_timer = 1;
579*d6eebaa4SHervé Poussineau mqtt_client_t *client = (mqtt_client_t *)arg;
580*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_cyclic_timer: client != NULL", client != NULL);
581*d6eebaa4SHervé Poussineau
582*d6eebaa4SHervé Poussineau if (client->conn_state == MQTT_CONNECTING) {
583*d6eebaa4SHervé Poussineau client->cyclic_tick++;
584*d6eebaa4SHervé Poussineau if ((client->cyclic_tick * MQTT_CYCLIC_TIMER_INTERVAL) >= MQTT_CONNECT_TIMOUT) {
585*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_cyclic_timer: CONNECT attempt to server timed out\n"));
586*d6eebaa4SHervé Poussineau /* Disconnect TCP */
587*d6eebaa4SHervé Poussineau mqtt_close(client, MQTT_CONNECT_TIMEOUT);
588*d6eebaa4SHervé Poussineau restart_timer = 0;
589*d6eebaa4SHervé Poussineau }
590*d6eebaa4SHervé Poussineau } else if (client->conn_state == MQTT_CONNECTED) {
591*d6eebaa4SHervé Poussineau /* Handle timeout for pending requests */
592*d6eebaa4SHervé Poussineau mqtt_request_time_elapsed(&client->pend_req_queue, MQTT_CYCLIC_TIMER_INTERVAL);
593*d6eebaa4SHervé Poussineau
594*d6eebaa4SHervé Poussineau /* keep_alive > 0 means keep alive functionality shall be used */
595*d6eebaa4SHervé Poussineau if (client->keep_alive > 0) {
596*d6eebaa4SHervé Poussineau
597*d6eebaa4SHervé Poussineau client->server_watchdog++;
598*d6eebaa4SHervé Poussineau /* If reception from server has been idle for 1.5*keep_alive time, server is considered unresponsive */
599*d6eebaa4SHervé Poussineau if ((client->server_watchdog * MQTT_CYCLIC_TIMER_INTERVAL) > (client->keep_alive + client->keep_alive / 2)) {
600*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_cyclic_timer: Server incoming keep-alive timeout\n"));
601*d6eebaa4SHervé Poussineau mqtt_close(client, MQTT_CONNECT_TIMEOUT);
602*d6eebaa4SHervé Poussineau restart_timer = 0;
603*d6eebaa4SHervé Poussineau }
604*d6eebaa4SHervé Poussineau
605*d6eebaa4SHervé Poussineau /* If time for a keep alive message to be sent, transmission has been idle for keep_alive time */
606*d6eebaa4SHervé Poussineau client->cyclic_tick++;
607*d6eebaa4SHervé Poussineau if ((client->cyclic_tick * MQTT_CYCLIC_TIMER_INTERVAL) >= client->keep_alive) {
608*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_cyclic_timer: Sending keep-alive message to server\n"));
609*d6eebaa4SHervé Poussineau if (mqtt_output_check_space(&client->output, 0) != 0) {
610*d6eebaa4SHervé Poussineau mqtt_output_append_fixed_header(&client->output, MQTT_MSG_TYPE_PINGREQ, 0, 0, 0, 0);
611*d6eebaa4SHervé Poussineau client->cyclic_tick = 0;
612*d6eebaa4SHervé Poussineau }
613*d6eebaa4SHervé Poussineau }
614*d6eebaa4SHervé Poussineau }
615*d6eebaa4SHervé Poussineau } else {
616*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_cyclic_timer: Timer should not be running in state %d\n", client->conn_state));
617*d6eebaa4SHervé Poussineau restart_timer = 0;
618*d6eebaa4SHervé Poussineau }
619*d6eebaa4SHervé Poussineau if (restart_timer) {
620*d6eebaa4SHervé Poussineau sys_timeout(MQTT_CYCLIC_TIMER_INTERVAL * 1000, mqtt_cyclic_timer, arg);
621*d6eebaa4SHervé Poussineau }
622*d6eebaa4SHervé Poussineau }
623*d6eebaa4SHervé Poussineau
624*d6eebaa4SHervé Poussineau
625*d6eebaa4SHervé Poussineau /**
626*d6eebaa4SHervé Poussineau * Send PUBACK, PUBREC or PUBREL response message
627*d6eebaa4SHervé Poussineau * @param client MQTT client
628*d6eebaa4SHervé Poussineau * @param msg PUBACK, PUBREC or PUBREL
629*d6eebaa4SHervé Poussineau * @param pkt_id Packet identifier
630*d6eebaa4SHervé Poussineau * @param qos QoS value
631*d6eebaa4SHervé Poussineau * @return ERR_OK if successful, ERR_MEM if out of memory
632*d6eebaa4SHervé Poussineau */
633*d6eebaa4SHervé Poussineau static err_t
pub_ack_rec_rel_response(mqtt_client_t * client,u8_t msg,u16_t pkt_id,u8_t qos)634*d6eebaa4SHervé Poussineau pub_ack_rec_rel_response(mqtt_client_t *client, u8_t msg, u16_t pkt_id, u8_t qos)
635*d6eebaa4SHervé Poussineau {
636*d6eebaa4SHervé Poussineau err_t err = ERR_OK;
637*d6eebaa4SHervé Poussineau if (mqtt_output_check_space(&client->output, 2)) {
638*d6eebaa4SHervé Poussineau mqtt_output_append_fixed_header(&client->output, msg, 0, qos, 0, 2);
639*d6eebaa4SHervé Poussineau mqtt_output_append_u16(&client->output, pkt_id);
640*d6eebaa4SHervé Poussineau mqtt_output_send(&client->output, client->conn);
641*d6eebaa4SHervé Poussineau } else {
642*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("pub_ack_rec_rel_response: OOM creating response: %s with pkt_id: %d\n",
643*d6eebaa4SHervé Poussineau mqtt_msg_type_to_str(msg), pkt_id));
644*d6eebaa4SHervé Poussineau err = ERR_MEM;
645*d6eebaa4SHervé Poussineau }
646*d6eebaa4SHervé Poussineau return err;
647*d6eebaa4SHervé Poussineau }
648*d6eebaa4SHervé Poussineau
649*d6eebaa4SHervé Poussineau /**
650*d6eebaa4SHervé Poussineau * Subscribe response from server
651*d6eebaa4SHervé Poussineau * @param r Matching request
652*d6eebaa4SHervé Poussineau * @param result Result code from server
653*d6eebaa4SHervé Poussineau */
654*d6eebaa4SHervé Poussineau static void
mqtt_incoming_suback(struct mqtt_request_t * r,u8_t result)655*d6eebaa4SHervé Poussineau mqtt_incoming_suback(struct mqtt_request_t *r, u8_t result)
656*d6eebaa4SHervé Poussineau {
657*d6eebaa4SHervé Poussineau if (r->cb != NULL) {
658*d6eebaa4SHervé Poussineau r->cb(r->arg, result < 3 ? ERR_OK : ERR_ABRT);
659*d6eebaa4SHervé Poussineau }
660*d6eebaa4SHervé Poussineau }
661*d6eebaa4SHervé Poussineau
662*d6eebaa4SHervé Poussineau
663*d6eebaa4SHervé Poussineau /**
664*d6eebaa4SHervé Poussineau * Complete MQTT message received or buffer full
665*d6eebaa4SHervé Poussineau * @param client MQTT client
666*d6eebaa4SHervé Poussineau * @param fixed_hdr_len length of fixed header
667*d6eebaa4SHervé Poussineau * @param length length received part
668*d6eebaa4SHervé Poussineau * @param remaining_length Remaining length of complete message
669*d6eebaa4SHervé Poussineau */
670*d6eebaa4SHervé Poussineau static mqtt_connection_status_t
mqtt_message_received(mqtt_client_t * client,u8_t fixed_hdr_len,u16_t length,u32_t remaining_length,u8_t * var_hdr_payload)671*d6eebaa4SHervé Poussineau mqtt_message_received(mqtt_client_t *client, u8_t fixed_hdr_len, u16_t length, u32_t remaining_length,
672*d6eebaa4SHervé Poussineau u8_t *var_hdr_payload)
673*d6eebaa4SHervé Poussineau {
674*d6eebaa4SHervé Poussineau mqtt_connection_status_t res = MQTT_CONNECT_ACCEPTED;
675*d6eebaa4SHervé Poussineau
676*d6eebaa4SHervé Poussineau /* Control packet type */
677*d6eebaa4SHervé Poussineau u8_t pkt_type = MQTT_CTL_PACKET_TYPE(client->rx_buffer[0]);
678*d6eebaa4SHervé Poussineau u16_t pkt_id = 0;
679*d6eebaa4SHervé Poussineau
680*d6eebaa4SHervé Poussineau LWIP_ASSERT("fixed_hdr_len <= client->msg_idx", fixed_hdr_len <= client->msg_idx);
681*d6eebaa4SHervé Poussineau LWIP_ERROR("buffer length mismatch", fixed_hdr_len + length <= MQTT_VAR_HEADER_BUFFER_LEN,
682*d6eebaa4SHervé Poussineau return MQTT_CONNECT_DISCONNECTED);
683*d6eebaa4SHervé Poussineau
684*d6eebaa4SHervé Poussineau if (pkt_type == MQTT_MSG_TYPE_CONNACK) {
685*d6eebaa4SHervé Poussineau if (client->conn_state == MQTT_CONNECTING) {
686*d6eebaa4SHervé Poussineau if (length < 2) {
687*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN,( "mqtt_message_received: Received short CONNACK message\n"));
688*d6eebaa4SHervé Poussineau goto out_disconnect;
689*d6eebaa4SHervé Poussineau }
690*d6eebaa4SHervé Poussineau /* Get result code from CONNACK */
691*d6eebaa4SHervé Poussineau res = (mqtt_connection_status_t)var_hdr_payload[1];
692*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_message_received: Connect response code %d\n", res));
693*d6eebaa4SHervé Poussineau if (res == MQTT_CONNECT_ACCEPTED) {
694*d6eebaa4SHervé Poussineau /* Reset cyclic_tick when changing to connected state */
695*d6eebaa4SHervé Poussineau client->cyclic_tick = 0;
696*d6eebaa4SHervé Poussineau client->conn_state = MQTT_CONNECTED;
697*d6eebaa4SHervé Poussineau /* Notify upper layer */
698*d6eebaa4SHervé Poussineau if (client->connect_cb != NULL) {
699*d6eebaa4SHervé Poussineau client->connect_cb(client, client->connect_arg, res);
700*d6eebaa4SHervé Poussineau }
701*d6eebaa4SHervé Poussineau }
702*d6eebaa4SHervé Poussineau } else {
703*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_message_received: Received CONNACK in connected state\n"));
704*d6eebaa4SHervé Poussineau }
705*d6eebaa4SHervé Poussineau } else if (pkt_type == MQTT_MSG_TYPE_PINGRESP) {
706*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ( "mqtt_message_received: Received PINGRESP from server\n"));
707*d6eebaa4SHervé Poussineau
708*d6eebaa4SHervé Poussineau } else if (pkt_type == MQTT_MSG_TYPE_PUBLISH) {
709*d6eebaa4SHervé Poussineau u16_t payload_offset = 0;
710*d6eebaa4SHervé Poussineau u16_t payload_length = length;
711*d6eebaa4SHervé Poussineau u8_t qos = MQTT_CTL_PACKET_QOS(client->rx_buffer[0]);
712*d6eebaa4SHervé Poussineau
713*d6eebaa4SHervé Poussineau if (client->msg_idx == (u32_t)(fixed_hdr_len + length)) {
714*d6eebaa4SHervé Poussineau /* First publish message frame. Should have topic and pkt id*/
715*d6eebaa4SHervé Poussineau size_t var_hdr_payload_bufsize = sizeof(client->rx_buffer) - fixed_hdr_len;
716*d6eebaa4SHervé Poussineau u8_t *topic;
717*d6eebaa4SHervé Poussineau u16_t after_topic;
718*d6eebaa4SHervé Poussineau u8_t bkp;
719*d6eebaa4SHervé Poussineau u16_t topic_len;
720*d6eebaa4SHervé Poussineau u16_t qos_len = (qos ? 2U : 0U);
721*d6eebaa4SHervé Poussineau if (length < 2 + qos_len) {
722*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN,( "mqtt_message_received: Received short PUBLISH packet\n"));
723*d6eebaa4SHervé Poussineau goto out_disconnect;
724*d6eebaa4SHervé Poussineau }
725*d6eebaa4SHervé Poussineau topic_len = var_hdr_payload[0];
726*d6eebaa4SHervé Poussineau topic_len = (topic_len << 8) + (u16_t)(var_hdr_payload[1]);
727*d6eebaa4SHervé Poussineau if ((topic_len > length - (2 + qos_len)) ||
728*d6eebaa4SHervé Poussineau (topic_len > var_hdr_payload_bufsize - (2 + qos_len))) {
729*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN,( "mqtt_message_received: Received short PUBLISH packet (topic)\n"));
730*d6eebaa4SHervé Poussineau goto out_disconnect;
731*d6eebaa4SHervé Poussineau }
732*d6eebaa4SHervé Poussineau
733*d6eebaa4SHervé Poussineau topic = var_hdr_payload + 2;
734*d6eebaa4SHervé Poussineau after_topic = 2 + topic_len;
735*d6eebaa4SHervé Poussineau /* Check buffer length, add one byte even for QoS 0 so that zero termination will fit */
736*d6eebaa4SHervé Poussineau if ((after_topic + (qos ? 2U : 1U)) > var_hdr_payload_bufsize) {
737*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_message_received: Receive buffer can not fit topic + pkt_id\n"));
738*d6eebaa4SHervé Poussineau goto out_disconnect;
739*d6eebaa4SHervé Poussineau }
740*d6eebaa4SHervé Poussineau
741*d6eebaa4SHervé Poussineau /* id for QoS 1 and 2 */
742*d6eebaa4SHervé Poussineau if (qos > 0) {
743*d6eebaa4SHervé Poussineau if (length < after_topic + 2U) {
744*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN,( "mqtt_message_received: Received short PUBLISH packet (after_topic)\n"));
745*d6eebaa4SHervé Poussineau goto out_disconnect;
746*d6eebaa4SHervé Poussineau }
747*d6eebaa4SHervé Poussineau client->inpub_pkt_id = ((u16_t)var_hdr_payload[after_topic] << 8) + (u16_t)var_hdr_payload[after_topic + 1];
748*d6eebaa4SHervé Poussineau after_topic += 2;
749*d6eebaa4SHervé Poussineau } else {
750*d6eebaa4SHervé Poussineau client->inpub_pkt_id = 0;
751*d6eebaa4SHervé Poussineau }
752*d6eebaa4SHervé Poussineau /* Take backup of byte after topic */
753*d6eebaa4SHervé Poussineau bkp = topic[topic_len];
754*d6eebaa4SHervé Poussineau /* Zero terminate string */
755*d6eebaa4SHervé Poussineau topic[topic_len] = 0;
756*d6eebaa4SHervé Poussineau /* Payload data remaining in receive buffer */
757*d6eebaa4SHervé Poussineau payload_length = length - after_topic;
758*d6eebaa4SHervé Poussineau payload_offset = after_topic;
759*d6eebaa4SHervé Poussineau
760*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_incoming_publish: Received message with QoS %d at topic: %s, payload length %"U32_F"\n",
761*d6eebaa4SHervé Poussineau qos, topic, remaining_length + payload_length));
762*d6eebaa4SHervé Poussineau if (client->pub_cb != NULL) {
763*d6eebaa4SHervé Poussineau client->pub_cb(client->inpub_arg, (const char *)topic, remaining_length + payload_length);
764*d6eebaa4SHervé Poussineau }
765*d6eebaa4SHervé Poussineau /* Restore byte after topic */
766*d6eebaa4SHervé Poussineau topic[topic_len] = bkp;
767*d6eebaa4SHervé Poussineau }
768*d6eebaa4SHervé Poussineau if (payload_length > 0 || remaining_length == 0) {
769*d6eebaa4SHervé Poussineau if (length < (size_t)(payload_offset + payload_length)) {
770*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN,( "mqtt_message_received: Received short packet (payload)\n"));
771*d6eebaa4SHervé Poussineau goto out_disconnect;
772*d6eebaa4SHervé Poussineau }
773*d6eebaa4SHervé Poussineau if (client->data_cb != NULL) {
774*d6eebaa4SHervé Poussineau client->data_cb(client->inpub_arg, var_hdr_payload + payload_offset, payload_length, remaining_length == 0 ? MQTT_DATA_FLAG_LAST : 0);
775*d6eebaa4SHervé Poussineau }
776*d6eebaa4SHervé Poussineau /* Reply if QoS > 0 */
777*d6eebaa4SHervé Poussineau if (remaining_length == 0 && qos > 0) {
778*d6eebaa4SHervé Poussineau /* Send PUBACK for QoS 1 or PUBREC for QoS 2 */
779*d6eebaa4SHervé Poussineau u8_t resp_msg = (qos == 1) ? MQTT_MSG_TYPE_PUBACK : MQTT_MSG_TYPE_PUBREC;
780*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_incoming_publish: Sending publish response: %s with pkt_id: %d\n",
781*d6eebaa4SHervé Poussineau mqtt_msg_type_to_str(resp_msg), client->inpub_pkt_id));
782*d6eebaa4SHervé Poussineau pub_ack_rec_rel_response(client, resp_msg, client->inpub_pkt_id, 0);
783*d6eebaa4SHervé Poussineau }
784*d6eebaa4SHervé Poussineau }
785*d6eebaa4SHervé Poussineau } else {
786*d6eebaa4SHervé Poussineau if (length < 2) {
787*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN,( "mqtt_message_received: Received short message\n"));
788*d6eebaa4SHervé Poussineau goto out_disconnect;
789*d6eebaa4SHervé Poussineau }
790*d6eebaa4SHervé Poussineau /* Get packet identifier */
791*d6eebaa4SHervé Poussineau pkt_id = (u16_t)var_hdr_payload[0] << 8;
792*d6eebaa4SHervé Poussineau pkt_id |= (u16_t)var_hdr_payload[1];
793*d6eebaa4SHervé Poussineau if (pkt_id == 0) {
794*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_message_received: Got message with illegal packet identifier: 0\n"));
795*d6eebaa4SHervé Poussineau goto out_disconnect;
796*d6eebaa4SHervé Poussineau }
797*d6eebaa4SHervé Poussineau if (pkt_type == MQTT_MSG_TYPE_PUBREC) {
798*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_message_received: PUBREC, sending PUBREL with pkt_id: %d\n", pkt_id));
799*d6eebaa4SHervé Poussineau pub_ack_rec_rel_response(client, MQTT_MSG_TYPE_PUBREL, pkt_id, 1);
800*d6eebaa4SHervé Poussineau
801*d6eebaa4SHervé Poussineau } else if (pkt_type == MQTT_MSG_TYPE_PUBREL) {
802*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_message_received: PUBREL, sending PUBCOMP response with pkt_id: %d\n", pkt_id));
803*d6eebaa4SHervé Poussineau pub_ack_rec_rel_response(client, MQTT_MSG_TYPE_PUBCOMP, pkt_id, 0);
804*d6eebaa4SHervé Poussineau
805*d6eebaa4SHervé Poussineau } else if (pkt_type == MQTT_MSG_TYPE_SUBACK || pkt_type == MQTT_MSG_TYPE_UNSUBACK ||
806*d6eebaa4SHervé Poussineau pkt_type == MQTT_MSG_TYPE_PUBCOMP || pkt_type == MQTT_MSG_TYPE_PUBACK) {
807*d6eebaa4SHervé Poussineau struct mqtt_request_t *r = mqtt_take_request(&client->pend_req_queue, pkt_id);
808*d6eebaa4SHervé Poussineau if (r != NULL) {
809*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_message_received: %s response with id %d\n", mqtt_msg_type_to_str(pkt_type), pkt_id));
810*d6eebaa4SHervé Poussineau if (pkt_type == MQTT_MSG_TYPE_SUBACK) {
811*d6eebaa4SHervé Poussineau if (length < 3) {
812*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_message_received: To small SUBACK packet\n"));
813*d6eebaa4SHervé Poussineau goto out_disconnect;
814*d6eebaa4SHervé Poussineau } else {
815*d6eebaa4SHervé Poussineau mqtt_incoming_suback(r, var_hdr_payload[2]);
816*d6eebaa4SHervé Poussineau }
817*d6eebaa4SHervé Poussineau } else if (r->cb != NULL) {
818*d6eebaa4SHervé Poussineau r->cb(r->arg, ERR_OK);
819*d6eebaa4SHervé Poussineau }
820*d6eebaa4SHervé Poussineau mqtt_delete_request(r);
821*d6eebaa4SHervé Poussineau } else {
822*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ( "mqtt_message_received: Received %s reply, with wrong pkt_id: %d\n", mqtt_msg_type_to_str(pkt_type), pkt_id));
823*d6eebaa4SHervé Poussineau }
824*d6eebaa4SHervé Poussineau } else {
825*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ( "mqtt_message_received: Received unknown message type: %d\n", pkt_type));
826*d6eebaa4SHervé Poussineau goto out_disconnect;
827*d6eebaa4SHervé Poussineau }
828*d6eebaa4SHervé Poussineau }
829*d6eebaa4SHervé Poussineau return res;
830*d6eebaa4SHervé Poussineau out_disconnect:
831*d6eebaa4SHervé Poussineau return MQTT_CONNECT_DISCONNECTED;
832*d6eebaa4SHervé Poussineau }
833*d6eebaa4SHervé Poussineau
834*d6eebaa4SHervé Poussineau
835*d6eebaa4SHervé Poussineau /**
836*d6eebaa4SHervé Poussineau * MQTT incoming message parser
837*d6eebaa4SHervé Poussineau * @param client MQTT client
838*d6eebaa4SHervé Poussineau * @param p PBUF chain of received data
839*d6eebaa4SHervé Poussineau * @return Connection status
840*d6eebaa4SHervé Poussineau */
841*d6eebaa4SHervé Poussineau static mqtt_connection_status_t
mqtt_parse_incoming(mqtt_client_t * client,struct pbuf * p)842*d6eebaa4SHervé Poussineau mqtt_parse_incoming(mqtt_client_t *client, struct pbuf *p)
843*d6eebaa4SHervé Poussineau {
844*d6eebaa4SHervé Poussineau u16_t in_offset = 0;
845*d6eebaa4SHervé Poussineau u32_t msg_rem_len = 0;
846*d6eebaa4SHervé Poussineau u8_t fixed_hdr_len = 0;
847*d6eebaa4SHervé Poussineau u8_t b = 0;
848*d6eebaa4SHervé Poussineau
849*d6eebaa4SHervé Poussineau while (p->tot_len > in_offset) {
850*d6eebaa4SHervé Poussineau /* We ALWAYS parse the header here first. Even if the header was not
851*d6eebaa4SHervé Poussineau included in this segment, we re-parse it here by buffering it in
852*d6eebaa4SHervé Poussineau client->rx_buffer. client->msg_idx keeps track of this. */
853*d6eebaa4SHervé Poussineau if ((fixed_hdr_len < 2) || ((b & 0x80) != 0)) {
854*d6eebaa4SHervé Poussineau
855*d6eebaa4SHervé Poussineau if (fixed_hdr_len < client->msg_idx) {
856*d6eebaa4SHervé Poussineau /* parse header from old pbuf (buffered in client->rx_buffer) */
857*d6eebaa4SHervé Poussineau b = client->rx_buffer[fixed_hdr_len];
858*d6eebaa4SHervé Poussineau } else {
859*d6eebaa4SHervé Poussineau /* parse header from this pbuf and save it in client->rx_buffer in case
860*d6eebaa4SHervé Poussineau it comes in segmented */
861*d6eebaa4SHervé Poussineau b = pbuf_get_at(p, in_offset++);
862*d6eebaa4SHervé Poussineau client->rx_buffer[client->msg_idx++] = b;
863*d6eebaa4SHervé Poussineau }
864*d6eebaa4SHervé Poussineau fixed_hdr_len++;
865*d6eebaa4SHervé Poussineau
866*d6eebaa4SHervé Poussineau if (fixed_hdr_len >= 2) {
867*d6eebaa4SHervé Poussineau /* fixed header contains at least 2 bytes but can contain more, depending on
868*d6eebaa4SHervé Poussineau 'remaining length'. All bytes but the last of this have 0x80 set to
869*d6eebaa4SHervé Poussineau indicate more bytes are coming. */
870*d6eebaa4SHervé Poussineau msg_rem_len |= (u32_t)(b & 0x7f) << ((fixed_hdr_len - 2) * 7);
871*d6eebaa4SHervé Poussineau if ((b & 0x80) == 0) {
872*d6eebaa4SHervé Poussineau /* fixed header is done */
873*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_parse_incoming: Remaining length after fixed header: %"U32_F"\n", msg_rem_len));
874*d6eebaa4SHervé Poussineau if (msg_rem_len == 0) {
875*d6eebaa4SHervé Poussineau /* Complete message with no extra headers of payload received */
876*d6eebaa4SHervé Poussineau mqtt_message_received(client, fixed_hdr_len, 0, 0, NULL);
877*d6eebaa4SHervé Poussineau client->msg_idx = 0;
878*d6eebaa4SHervé Poussineau fixed_hdr_len = 0;
879*d6eebaa4SHervé Poussineau } else {
880*d6eebaa4SHervé Poussineau /* Bytes remaining in message (changes remaining length if this is
881*d6eebaa4SHervé Poussineau not the first segment of this message) */
882*d6eebaa4SHervé Poussineau msg_rem_len = (msg_rem_len + fixed_hdr_len) - client->msg_idx;
883*d6eebaa4SHervé Poussineau }
884*d6eebaa4SHervé Poussineau }
885*d6eebaa4SHervé Poussineau }
886*d6eebaa4SHervé Poussineau } else {
887*d6eebaa4SHervé Poussineau /* Fixed header has been parsed, parse variable header */
888*d6eebaa4SHervé Poussineau u16_t cpy_len, buffer_space;
889*d6eebaa4SHervé Poussineau u8_t *var_hdr_payload;
890*d6eebaa4SHervé Poussineau mqtt_connection_status_t res;
891*d6eebaa4SHervé Poussineau
892*d6eebaa4SHervé Poussineau /* Allow to copy the lesser one of available length in input data or bytes remaining in message */
893*d6eebaa4SHervé Poussineau cpy_len = (u16_t)LWIP_MIN((u16_t)(p->tot_len - in_offset), msg_rem_len);
894*d6eebaa4SHervé Poussineau
895*d6eebaa4SHervé Poussineau /* Limit to available space in buffer */
896*d6eebaa4SHervé Poussineau buffer_space = MQTT_VAR_HEADER_BUFFER_LEN - fixed_hdr_len;
897*d6eebaa4SHervé Poussineau if (cpy_len > buffer_space) {
898*d6eebaa4SHervé Poussineau cpy_len = buffer_space;
899*d6eebaa4SHervé Poussineau }
900*d6eebaa4SHervé Poussineau /* Adjust cpy_len to ensure zero-copy operation for remaining parts of current message */
901*d6eebaa4SHervé Poussineau if (client->msg_idx >= MQTT_VAR_HEADER_BUFFER_LEN) {
902*d6eebaa4SHervé Poussineau if (cpy_len > (p->len - in_offset))
903*d6eebaa4SHervé Poussineau cpy_len = p->len - in_offset;
904*d6eebaa4SHervé Poussineau }
905*d6eebaa4SHervé Poussineau var_hdr_payload = (u8_t*)pbuf_get_contiguous(p, client->rx_buffer + fixed_hdr_len,
906*d6eebaa4SHervé Poussineau buffer_space, cpy_len, in_offset);
907*d6eebaa4SHervé Poussineau
908*d6eebaa4SHervé Poussineau /* Advance get and put indexes */
909*d6eebaa4SHervé Poussineau client->msg_idx += cpy_len;
910*d6eebaa4SHervé Poussineau in_offset += cpy_len;
911*d6eebaa4SHervé Poussineau msg_rem_len -= cpy_len;
912*d6eebaa4SHervé Poussineau
913*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_parse_incoming: msg_idx: %"U32_F", cpy_len: %"U16_F", remaining %"U32_F"\n", client->msg_idx, cpy_len, msg_rem_len));
914*d6eebaa4SHervé Poussineau /* Whole or partial message received */
915*d6eebaa4SHervé Poussineau res = mqtt_message_received(client, fixed_hdr_len, cpy_len, msg_rem_len, var_hdr_payload);
916*d6eebaa4SHervé Poussineau if (res != MQTT_CONNECT_ACCEPTED) {
917*d6eebaa4SHervé Poussineau return res;
918*d6eebaa4SHervé Poussineau }
919*d6eebaa4SHervé Poussineau if (msg_rem_len == 0) {
920*d6eebaa4SHervé Poussineau /* Reset parser state */
921*d6eebaa4SHervé Poussineau client->msg_idx = 0;
922*d6eebaa4SHervé Poussineau /* msg_tot_len = 0; */
923*d6eebaa4SHervé Poussineau fixed_hdr_len = 0;
924*d6eebaa4SHervé Poussineau }
925*d6eebaa4SHervé Poussineau }
926*d6eebaa4SHervé Poussineau }
927*d6eebaa4SHervé Poussineau return MQTT_CONNECT_ACCEPTED;
928*d6eebaa4SHervé Poussineau }
929*d6eebaa4SHervé Poussineau
930*d6eebaa4SHervé Poussineau
931*d6eebaa4SHervé Poussineau /**
932*d6eebaa4SHervé Poussineau * TCP received callback function. @see tcp_recv_fn
933*d6eebaa4SHervé Poussineau * @param arg MQTT client
934*d6eebaa4SHervé Poussineau * @param p PBUF chain of received data
935*d6eebaa4SHervé Poussineau * @param err Passed as return value if not ERR_OK
936*d6eebaa4SHervé Poussineau * @return ERR_OK or err passed into callback
937*d6eebaa4SHervé Poussineau */
938*d6eebaa4SHervé Poussineau static err_t
mqtt_tcp_recv_cb(void * arg,struct altcp_pcb * pcb,struct pbuf * p,err_t err)939*d6eebaa4SHervé Poussineau mqtt_tcp_recv_cb(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
940*d6eebaa4SHervé Poussineau {
941*d6eebaa4SHervé Poussineau mqtt_client_t *client = (mqtt_client_t *)arg;
942*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_tcp_recv_cb: client != NULL", client != NULL);
943*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_tcp_recv_cb: client->conn == pcb", client->conn == pcb);
944*d6eebaa4SHervé Poussineau
945*d6eebaa4SHervé Poussineau if (p == NULL) {
946*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_tcp_recv_cb: Recv pbuf=NULL, remote has closed connection\n"));
947*d6eebaa4SHervé Poussineau mqtt_close(client, MQTT_CONNECT_DISCONNECTED);
948*d6eebaa4SHervé Poussineau } else {
949*d6eebaa4SHervé Poussineau mqtt_connection_status_t res;
950*d6eebaa4SHervé Poussineau if (err != ERR_OK) {
951*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_tcp_recv_cb: Recv err=%d\n", err));
952*d6eebaa4SHervé Poussineau pbuf_free(p);
953*d6eebaa4SHervé Poussineau return err;
954*d6eebaa4SHervé Poussineau }
955*d6eebaa4SHervé Poussineau
956*d6eebaa4SHervé Poussineau /* Tell remote that data has been received */
957*d6eebaa4SHervé Poussineau altcp_recved(pcb, p->tot_len);
958*d6eebaa4SHervé Poussineau res = mqtt_parse_incoming(client, p);
959*d6eebaa4SHervé Poussineau pbuf_free(p);
960*d6eebaa4SHervé Poussineau
961*d6eebaa4SHervé Poussineau if (res != MQTT_CONNECT_ACCEPTED) {
962*d6eebaa4SHervé Poussineau mqtt_close(client, res);
963*d6eebaa4SHervé Poussineau }
964*d6eebaa4SHervé Poussineau /* If keep alive functionality is used */
965*d6eebaa4SHervé Poussineau if (client->keep_alive != 0) {
966*d6eebaa4SHervé Poussineau /* Reset server alive watchdog */
967*d6eebaa4SHervé Poussineau client->server_watchdog = 0;
968*d6eebaa4SHervé Poussineau }
969*d6eebaa4SHervé Poussineau
970*d6eebaa4SHervé Poussineau }
971*d6eebaa4SHervé Poussineau return ERR_OK;
972*d6eebaa4SHervé Poussineau }
973*d6eebaa4SHervé Poussineau
974*d6eebaa4SHervé Poussineau
975*d6eebaa4SHervé Poussineau /**
976*d6eebaa4SHervé Poussineau * TCP data sent callback function. @see tcp_sent_fn
977*d6eebaa4SHervé Poussineau * @param arg MQTT client
978*d6eebaa4SHervé Poussineau * @param tpcb TCP connection handle
979*d6eebaa4SHervé Poussineau * @param len Number of bytes sent
980*d6eebaa4SHervé Poussineau * @return ERR_OK
981*d6eebaa4SHervé Poussineau */
982*d6eebaa4SHervé Poussineau static err_t
mqtt_tcp_sent_cb(void * arg,struct altcp_pcb * tpcb,u16_t len)983*d6eebaa4SHervé Poussineau mqtt_tcp_sent_cb(void *arg, struct altcp_pcb *tpcb, u16_t len)
984*d6eebaa4SHervé Poussineau {
985*d6eebaa4SHervé Poussineau mqtt_client_t *client = (mqtt_client_t *)arg;
986*d6eebaa4SHervé Poussineau
987*d6eebaa4SHervé Poussineau LWIP_UNUSED_ARG(tpcb);
988*d6eebaa4SHervé Poussineau LWIP_UNUSED_ARG(len);
989*d6eebaa4SHervé Poussineau
990*d6eebaa4SHervé Poussineau if (client->conn_state == MQTT_CONNECTED) {
991*d6eebaa4SHervé Poussineau struct mqtt_request_t *r;
992*d6eebaa4SHervé Poussineau
993*d6eebaa4SHervé Poussineau /* Reset keep-alive send timer and server watchdog */
994*d6eebaa4SHervé Poussineau client->cyclic_tick = 0;
995*d6eebaa4SHervé Poussineau client->server_watchdog = 0;
996*d6eebaa4SHervé Poussineau /* QoS 0 publish has no response from server, so call its callbacks here */
997*d6eebaa4SHervé Poussineau while ((r = mqtt_take_request(&client->pend_req_queue, 0)) != NULL) {
998*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_tcp_sent_cb: Calling QoS 0 publish complete callback\n"));
999*d6eebaa4SHervé Poussineau if (r->cb != NULL) {
1000*d6eebaa4SHervé Poussineau r->cb(r->arg, ERR_OK);
1001*d6eebaa4SHervé Poussineau }
1002*d6eebaa4SHervé Poussineau mqtt_delete_request(r);
1003*d6eebaa4SHervé Poussineau }
1004*d6eebaa4SHervé Poussineau /* Try send any remaining buffers from output queue */
1005*d6eebaa4SHervé Poussineau mqtt_output_send(&client->output, client->conn);
1006*d6eebaa4SHervé Poussineau }
1007*d6eebaa4SHervé Poussineau return ERR_OK;
1008*d6eebaa4SHervé Poussineau }
1009*d6eebaa4SHervé Poussineau
1010*d6eebaa4SHervé Poussineau /**
1011*d6eebaa4SHervé Poussineau * TCP error callback function. @see tcp_err_fn
1012*d6eebaa4SHervé Poussineau * @param arg MQTT client
1013*d6eebaa4SHervé Poussineau * @param err Error encountered
1014*d6eebaa4SHervé Poussineau */
1015*d6eebaa4SHervé Poussineau static void
mqtt_tcp_err_cb(void * arg,err_t err)1016*d6eebaa4SHervé Poussineau mqtt_tcp_err_cb(void *arg, err_t err)
1017*d6eebaa4SHervé Poussineau {
1018*d6eebaa4SHervé Poussineau mqtt_client_t *client = (mqtt_client_t *)arg;
1019*d6eebaa4SHervé Poussineau LWIP_UNUSED_ARG(err); /* only used for debug output */
1020*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_tcp_err_cb: TCP error callback: error %d, arg: %p\n", err, arg));
1021*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_tcp_err_cb: client != NULL", client != NULL);
1022*d6eebaa4SHervé Poussineau /* Set conn to null before calling close as pcb is already deallocated*/
1023*d6eebaa4SHervé Poussineau client->conn = NULL;
1024*d6eebaa4SHervé Poussineau mqtt_close(client, MQTT_CONNECT_DISCONNECTED);
1025*d6eebaa4SHervé Poussineau }
1026*d6eebaa4SHervé Poussineau
1027*d6eebaa4SHervé Poussineau /**
1028*d6eebaa4SHervé Poussineau * TCP poll callback function. @see tcp_poll_fn
1029*d6eebaa4SHervé Poussineau * @param arg MQTT client
1030*d6eebaa4SHervé Poussineau * @param tpcb TCP connection handle
1031*d6eebaa4SHervé Poussineau * @return err ERR_OK
1032*d6eebaa4SHervé Poussineau */
1033*d6eebaa4SHervé Poussineau static err_t
mqtt_tcp_poll_cb(void * arg,struct altcp_pcb * tpcb)1034*d6eebaa4SHervé Poussineau mqtt_tcp_poll_cb(void *arg, struct altcp_pcb *tpcb)
1035*d6eebaa4SHervé Poussineau {
1036*d6eebaa4SHervé Poussineau mqtt_client_t *client = (mqtt_client_t *)arg;
1037*d6eebaa4SHervé Poussineau if (client->conn_state == MQTT_CONNECTED) {
1038*d6eebaa4SHervé Poussineau /* Try send any remaining buffers from output queue */
1039*d6eebaa4SHervé Poussineau mqtt_output_send(&client->output, tpcb);
1040*d6eebaa4SHervé Poussineau }
1041*d6eebaa4SHervé Poussineau return ERR_OK;
1042*d6eebaa4SHervé Poussineau }
1043*d6eebaa4SHervé Poussineau
1044*d6eebaa4SHervé Poussineau /**
1045*d6eebaa4SHervé Poussineau * TCP connect callback function. @see tcp_connected_fn
1046*d6eebaa4SHervé Poussineau * @param arg MQTT client
1047*d6eebaa4SHervé Poussineau * @param err Always ERR_OK, mqtt_tcp_err_cb is called in case of error
1048*d6eebaa4SHervé Poussineau * @return ERR_OK
1049*d6eebaa4SHervé Poussineau */
1050*d6eebaa4SHervé Poussineau static err_t
mqtt_tcp_connect_cb(void * arg,struct altcp_pcb * tpcb,err_t err)1051*d6eebaa4SHervé Poussineau mqtt_tcp_connect_cb(void *arg, struct altcp_pcb *tpcb, err_t err)
1052*d6eebaa4SHervé Poussineau {
1053*d6eebaa4SHervé Poussineau mqtt_client_t *client = (mqtt_client_t *)arg;
1054*d6eebaa4SHervé Poussineau
1055*d6eebaa4SHervé Poussineau if (err != ERR_OK) {
1056*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_tcp_connect_cb: TCP connect error %d\n", err));
1057*d6eebaa4SHervé Poussineau return err;
1058*d6eebaa4SHervé Poussineau }
1059*d6eebaa4SHervé Poussineau
1060*d6eebaa4SHervé Poussineau /* Initiate receiver state */
1061*d6eebaa4SHervé Poussineau client->msg_idx = 0;
1062*d6eebaa4SHervé Poussineau
1063*d6eebaa4SHervé Poussineau /* Setup TCP callbacks */
1064*d6eebaa4SHervé Poussineau altcp_recv(tpcb, mqtt_tcp_recv_cb);
1065*d6eebaa4SHervé Poussineau altcp_sent(tpcb, mqtt_tcp_sent_cb);
1066*d6eebaa4SHervé Poussineau altcp_poll(tpcb, mqtt_tcp_poll_cb, 2);
1067*d6eebaa4SHervé Poussineau
1068*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_tcp_connect_cb: TCP connection established to server\n"));
1069*d6eebaa4SHervé Poussineau /* Enter MQTT connect state */
1070*d6eebaa4SHervé Poussineau client->conn_state = MQTT_CONNECTING;
1071*d6eebaa4SHervé Poussineau
1072*d6eebaa4SHervé Poussineau /* Start cyclic timer */
1073*d6eebaa4SHervé Poussineau sys_timeout(MQTT_CYCLIC_TIMER_INTERVAL * 1000, mqtt_cyclic_timer, client);
1074*d6eebaa4SHervé Poussineau client->cyclic_tick = 0;
1075*d6eebaa4SHervé Poussineau
1076*d6eebaa4SHervé Poussineau /* Start transmission from output queue, connect message is the first one out*/
1077*d6eebaa4SHervé Poussineau mqtt_output_send(&client->output, client->conn);
1078*d6eebaa4SHervé Poussineau
1079*d6eebaa4SHervé Poussineau return ERR_OK;
1080*d6eebaa4SHervé Poussineau }
1081*d6eebaa4SHervé Poussineau
1082*d6eebaa4SHervé Poussineau
1083*d6eebaa4SHervé Poussineau
1084*d6eebaa4SHervé Poussineau /*---------------------------------------------------------------------------------------------------- */
1085*d6eebaa4SHervé Poussineau /* Public API */
1086*d6eebaa4SHervé Poussineau
1087*d6eebaa4SHervé Poussineau
1088*d6eebaa4SHervé Poussineau /**
1089*d6eebaa4SHervé Poussineau * @ingroup mqtt
1090*d6eebaa4SHervé Poussineau * MQTT publish function.
1091*d6eebaa4SHervé Poussineau * @param client MQTT client
1092*d6eebaa4SHervé Poussineau * @param topic Publish topic string
1093*d6eebaa4SHervé Poussineau * @param payload Data to publish (NULL is allowed)
1094*d6eebaa4SHervé Poussineau * @param payload_length Length of payload (0 is allowed)
1095*d6eebaa4SHervé Poussineau * @param qos Quality of service, 0 1 or 2
1096*d6eebaa4SHervé Poussineau * @param retain MQTT retain flag
1097*d6eebaa4SHervé Poussineau * @param cb Callback to call when publish is complete or has timed out
1098*d6eebaa4SHervé Poussineau * @param arg User supplied argument to publish callback
1099*d6eebaa4SHervé Poussineau * @return ERR_OK if successful
1100*d6eebaa4SHervé Poussineau * ERR_CONN if client is disconnected
1101*d6eebaa4SHervé Poussineau * ERR_MEM if short on memory
1102*d6eebaa4SHervé Poussineau */
1103*d6eebaa4SHervé Poussineau 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)1104*d6eebaa4SHervé Poussineau mqtt_publish(mqtt_client_t *client, const char *topic, const void *payload, u16_t payload_length, u8_t qos, u8_t retain,
1105*d6eebaa4SHervé Poussineau mqtt_request_cb_t cb, void *arg)
1106*d6eebaa4SHervé Poussineau {
1107*d6eebaa4SHervé Poussineau struct mqtt_request_t *r;
1108*d6eebaa4SHervé Poussineau u16_t pkt_id;
1109*d6eebaa4SHervé Poussineau size_t topic_strlen;
1110*d6eebaa4SHervé Poussineau size_t total_len;
1111*d6eebaa4SHervé Poussineau u16_t topic_len;
1112*d6eebaa4SHervé Poussineau u16_t remaining_length;
1113*d6eebaa4SHervé Poussineau
1114*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1115*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_publish: client != NULL", client);
1116*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_publish: topic != NULL", topic);
1117*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_publish: TCP disconnected", (client->conn_state != TCP_DISCONNECTED), return ERR_CONN);
1118*d6eebaa4SHervé Poussineau
1119*d6eebaa4SHervé Poussineau topic_strlen = strlen(topic);
1120*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_publish: topic length overflow", (topic_strlen <= (0xFFFF - 2)), return ERR_ARG);
1121*d6eebaa4SHervé Poussineau topic_len = (u16_t)topic_strlen;
1122*d6eebaa4SHervé Poussineau total_len = 2 + topic_len + payload_length;
1123*d6eebaa4SHervé Poussineau
1124*d6eebaa4SHervé Poussineau if (qos > 0) {
1125*d6eebaa4SHervé Poussineau total_len += 2;
1126*d6eebaa4SHervé Poussineau /* Generate pkt_id id for QoS1 and 2 */
1127*d6eebaa4SHervé Poussineau pkt_id = msg_generate_packet_id(client);
1128*d6eebaa4SHervé Poussineau } else {
1129*d6eebaa4SHervé Poussineau /* Use reserved value pkt_id 0 for QoS 0 in request handle */
1130*d6eebaa4SHervé Poussineau pkt_id = 0;
1131*d6eebaa4SHervé Poussineau }
1132*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_publish: total length overflow", (total_len <= 0xFFFF), return ERR_ARG);
1133*d6eebaa4SHervé Poussineau remaining_length = (u16_t)total_len;
1134*d6eebaa4SHervé Poussineau
1135*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_publish: Publish with payload length %d to topic \"%s\"\n", payload_length, topic));
1136*d6eebaa4SHervé Poussineau
1137*d6eebaa4SHervé Poussineau r = mqtt_create_request(client->req_list, LWIP_ARRAYSIZE(client->req_list), pkt_id, cb, arg);
1138*d6eebaa4SHervé Poussineau if (r == NULL) {
1139*d6eebaa4SHervé Poussineau return ERR_MEM;
1140*d6eebaa4SHervé Poussineau }
1141*d6eebaa4SHervé Poussineau
1142*d6eebaa4SHervé Poussineau if (mqtt_output_check_space(&client->output, remaining_length) == 0) {
1143*d6eebaa4SHervé Poussineau mqtt_delete_request(r);
1144*d6eebaa4SHervé Poussineau return ERR_MEM;
1145*d6eebaa4SHervé Poussineau }
1146*d6eebaa4SHervé Poussineau /* Append fixed header */
1147*d6eebaa4SHervé Poussineau mqtt_output_append_fixed_header(&client->output, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain, remaining_length);
1148*d6eebaa4SHervé Poussineau
1149*d6eebaa4SHervé Poussineau /* Append Topic */
1150*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, topic, topic_len);
1151*d6eebaa4SHervé Poussineau
1152*d6eebaa4SHervé Poussineau /* Append packet if for QoS 1 and 2*/
1153*d6eebaa4SHervé Poussineau if (qos > 0) {
1154*d6eebaa4SHervé Poussineau mqtt_output_append_u16(&client->output, pkt_id);
1155*d6eebaa4SHervé Poussineau }
1156*d6eebaa4SHervé Poussineau
1157*d6eebaa4SHervé Poussineau /* Append optional publish payload */
1158*d6eebaa4SHervé Poussineau if ((payload != NULL) && (payload_length > 0)) {
1159*d6eebaa4SHervé Poussineau mqtt_output_append_buf(&client->output, payload, payload_length);
1160*d6eebaa4SHervé Poussineau }
1161*d6eebaa4SHervé Poussineau
1162*d6eebaa4SHervé Poussineau mqtt_append_request(&client->pend_req_queue, r);
1163*d6eebaa4SHervé Poussineau mqtt_output_send(&client->output, client->conn);
1164*d6eebaa4SHervé Poussineau return ERR_OK;
1165*d6eebaa4SHervé Poussineau }
1166*d6eebaa4SHervé Poussineau
1167*d6eebaa4SHervé Poussineau
1168*d6eebaa4SHervé Poussineau /**
1169*d6eebaa4SHervé Poussineau * @ingroup mqtt
1170*d6eebaa4SHervé Poussineau * MQTT subscribe/unsubscribe function.
1171*d6eebaa4SHervé Poussineau * @param client MQTT client
1172*d6eebaa4SHervé Poussineau * @param topic topic to subscribe to
1173*d6eebaa4SHervé Poussineau * @param qos Quality of service, 0 1 or 2 (only used for subscribe)
1174*d6eebaa4SHervé Poussineau * @param cb Callback to call when subscribe/unsubscribe response is received
1175*d6eebaa4SHervé Poussineau * @param arg User supplied argument to publish callback
1176*d6eebaa4SHervé Poussineau * @param sub 1 for subscribe, 0 for unsubscribe
1177*d6eebaa4SHervé Poussineau * @return ERR_OK if successful, @see err_t enum for other results
1178*d6eebaa4SHervé Poussineau */
1179*d6eebaa4SHervé Poussineau 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)1180*d6eebaa4SHervé Poussineau mqtt_sub_unsub(mqtt_client_t *client, const char *topic, u8_t qos, mqtt_request_cb_t cb, void *arg, u8_t sub)
1181*d6eebaa4SHervé Poussineau {
1182*d6eebaa4SHervé Poussineau size_t topic_strlen;
1183*d6eebaa4SHervé Poussineau size_t total_len;
1184*d6eebaa4SHervé Poussineau u16_t topic_len;
1185*d6eebaa4SHervé Poussineau u16_t remaining_length;
1186*d6eebaa4SHervé Poussineau u16_t pkt_id;
1187*d6eebaa4SHervé Poussineau struct mqtt_request_t *r;
1188*d6eebaa4SHervé Poussineau
1189*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1190*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_sub_unsub: client != NULL", client);
1191*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_sub_unsub: topic != NULL", topic);
1192*d6eebaa4SHervé Poussineau
1193*d6eebaa4SHervé Poussineau topic_strlen = strlen(topic);
1194*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_sub_unsub: topic length overflow", (topic_strlen <= (0xFFFF - 2)), return ERR_ARG);
1195*d6eebaa4SHervé Poussineau topic_len = (u16_t)topic_strlen;
1196*d6eebaa4SHervé Poussineau /* Topic string, pkt_id, qos for subscribe */
1197*d6eebaa4SHervé Poussineau total_len = topic_len + 2 + 2 + (sub != 0);
1198*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_sub_unsub: total length overflow", (total_len <= 0xFFFF), return ERR_ARG);
1199*d6eebaa4SHervé Poussineau remaining_length = (u16_t)total_len;
1200*d6eebaa4SHervé Poussineau
1201*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_sub_unsub: qos < 3", qos < 3);
1202*d6eebaa4SHervé Poussineau if (client->conn_state == TCP_DISCONNECTED) {
1203*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_sub_unsub: Can not (un)subscribe in disconnected state\n"));
1204*d6eebaa4SHervé Poussineau return ERR_CONN;
1205*d6eebaa4SHervé Poussineau }
1206*d6eebaa4SHervé Poussineau
1207*d6eebaa4SHervé Poussineau pkt_id = msg_generate_packet_id(client);
1208*d6eebaa4SHervé Poussineau r = mqtt_create_request(client->req_list, LWIP_ARRAYSIZE(client->req_list), pkt_id, cb, arg);
1209*d6eebaa4SHervé Poussineau if (r == NULL) {
1210*d6eebaa4SHervé Poussineau return ERR_MEM;
1211*d6eebaa4SHervé Poussineau }
1212*d6eebaa4SHervé Poussineau
1213*d6eebaa4SHervé Poussineau if (mqtt_output_check_space(&client->output, remaining_length) == 0) {
1214*d6eebaa4SHervé Poussineau mqtt_delete_request(r);
1215*d6eebaa4SHervé Poussineau return ERR_MEM;
1216*d6eebaa4SHervé Poussineau }
1217*d6eebaa4SHervé Poussineau
1218*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_sub_unsub: Client (un)subscribe to topic \"%s\", id: %d\n", topic, pkt_id));
1219*d6eebaa4SHervé Poussineau
1220*d6eebaa4SHervé Poussineau mqtt_output_append_fixed_header(&client->output, sub ? MQTT_MSG_TYPE_SUBSCRIBE : MQTT_MSG_TYPE_UNSUBSCRIBE, 0, 1, 0, remaining_length);
1221*d6eebaa4SHervé Poussineau /* Packet id */
1222*d6eebaa4SHervé Poussineau mqtt_output_append_u16(&client->output, pkt_id);
1223*d6eebaa4SHervé Poussineau /* Topic */
1224*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, topic, topic_len);
1225*d6eebaa4SHervé Poussineau /* QoS */
1226*d6eebaa4SHervé Poussineau if (sub != 0) {
1227*d6eebaa4SHervé Poussineau mqtt_output_append_u8(&client->output, LWIP_MIN(qos, 2));
1228*d6eebaa4SHervé Poussineau }
1229*d6eebaa4SHervé Poussineau
1230*d6eebaa4SHervé Poussineau mqtt_append_request(&client->pend_req_queue, r);
1231*d6eebaa4SHervé Poussineau mqtt_output_send(&client->output, client->conn);
1232*d6eebaa4SHervé Poussineau return ERR_OK;
1233*d6eebaa4SHervé Poussineau }
1234*d6eebaa4SHervé Poussineau
1235*d6eebaa4SHervé Poussineau
1236*d6eebaa4SHervé Poussineau /**
1237*d6eebaa4SHervé Poussineau * @ingroup mqtt
1238*d6eebaa4SHervé Poussineau * Set callback to handle incoming publish requests from server
1239*d6eebaa4SHervé Poussineau * @param client MQTT client
1240*d6eebaa4SHervé Poussineau * @param pub_cb Callback invoked when publish starts, contain topic and total length of payload
1241*d6eebaa4SHervé Poussineau * @param data_cb Callback for each fragment of payload that arrives
1242*d6eebaa4SHervé Poussineau * @param arg User supplied argument to both callbacks
1243*d6eebaa4SHervé Poussineau */
1244*d6eebaa4SHervé Poussineau void
mqtt_set_inpub_callback(mqtt_client_t * client,mqtt_incoming_publish_cb_t pub_cb,mqtt_incoming_data_cb_t data_cb,void * arg)1245*d6eebaa4SHervé Poussineau mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t pub_cb,
1246*d6eebaa4SHervé Poussineau mqtt_incoming_data_cb_t data_cb, void *arg)
1247*d6eebaa4SHervé Poussineau {
1248*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1249*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_set_inpub_callback: client != NULL", client != NULL);
1250*d6eebaa4SHervé Poussineau client->data_cb = data_cb;
1251*d6eebaa4SHervé Poussineau client->pub_cb = pub_cb;
1252*d6eebaa4SHervé Poussineau client->inpub_arg = arg;
1253*d6eebaa4SHervé Poussineau }
1254*d6eebaa4SHervé Poussineau
1255*d6eebaa4SHervé Poussineau /**
1256*d6eebaa4SHervé Poussineau * @ingroup mqtt
1257*d6eebaa4SHervé Poussineau * Create a new MQTT client instance
1258*d6eebaa4SHervé Poussineau * @return Pointer to instance on success, NULL otherwise
1259*d6eebaa4SHervé Poussineau */
1260*d6eebaa4SHervé Poussineau mqtt_client_t *
mqtt_client_new(void)1261*d6eebaa4SHervé Poussineau mqtt_client_new(void)
1262*d6eebaa4SHervé Poussineau {
1263*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1264*d6eebaa4SHervé Poussineau return (mqtt_client_t *)mem_calloc(1, sizeof(mqtt_client_t));
1265*d6eebaa4SHervé Poussineau }
1266*d6eebaa4SHervé Poussineau
1267*d6eebaa4SHervé Poussineau /**
1268*d6eebaa4SHervé Poussineau * @ingroup mqtt
1269*d6eebaa4SHervé Poussineau * Free MQTT client instance
1270*d6eebaa4SHervé Poussineau * @param client Pointer to instance to be freed
1271*d6eebaa4SHervé Poussineau */
1272*d6eebaa4SHervé Poussineau void
mqtt_client_free(mqtt_client_t * client)1273*d6eebaa4SHervé Poussineau mqtt_client_free(mqtt_client_t *client)
1274*d6eebaa4SHervé Poussineau {
1275*d6eebaa4SHervé Poussineau mem_free(client);
1276*d6eebaa4SHervé Poussineau }
1277*d6eebaa4SHervé Poussineau
1278*d6eebaa4SHervé Poussineau /**
1279*d6eebaa4SHervé Poussineau * @ingroup mqtt
1280*d6eebaa4SHervé Poussineau * Connect to MQTT server
1281*d6eebaa4SHervé Poussineau * @param client MQTT client
1282*d6eebaa4SHervé Poussineau * @param ip_addr Server IP
1283*d6eebaa4SHervé Poussineau * @param port Server port
1284*d6eebaa4SHervé Poussineau * @param cb Connection state change callback
1285*d6eebaa4SHervé Poussineau * @param arg User supplied argument to connection callback
1286*d6eebaa4SHervé Poussineau * @param client_info Client identification and connection options
1287*d6eebaa4SHervé Poussineau * @return ERR_OK if successful, @see err_t enum for other results
1288*d6eebaa4SHervé Poussineau */
1289*d6eebaa4SHervé Poussineau err_t
mqtt_client_connect(mqtt_client_t * client,const ip_addr_t * ip_addr,u16_t port,mqtt_connection_cb_t cb,void * arg,const struct mqtt_connect_client_info_t * client_info)1290*d6eebaa4SHervé Poussineau mqtt_client_connect(mqtt_client_t *client, const ip_addr_t *ip_addr, u16_t port, mqtt_connection_cb_t cb, void *arg,
1291*d6eebaa4SHervé Poussineau const struct mqtt_connect_client_info_t *client_info)
1292*d6eebaa4SHervé Poussineau {
1293*d6eebaa4SHervé Poussineau err_t err;
1294*d6eebaa4SHervé Poussineau size_t len;
1295*d6eebaa4SHervé Poussineau u16_t client_id_length;
1296*d6eebaa4SHervé Poussineau /* Length is the sum of 2+"MQTT", protocol level, flags and keep alive */
1297*d6eebaa4SHervé Poussineau u16_t remaining_length = 2 + 4 + 1 + 1 + 2;
1298*d6eebaa4SHervé Poussineau u8_t flags = 0, will_topic_len = 0, will_msg_len = 0;
1299*d6eebaa4SHervé Poussineau u16_t client_user_len = 0, client_pass_len = 0;
1300*d6eebaa4SHervé Poussineau mqtt_incoming_data_cb_t data_cb;
1301*d6eebaa4SHervé Poussineau mqtt_incoming_publish_cb_t pub_cb;
1302*d6eebaa4SHervé Poussineau void *inpub_arg;
1303*d6eebaa4SHervé Poussineau
1304*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1305*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_client_connect: client != NULL", client != NULL);
1306*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_client_connect: ip_addr != NULL", ip_addr != NULL);
1307*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_client_connect: client_info != NULL", client_info != NULL);
1308*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_client_connect: client_info->client_id != NULL", client_info->client_id != NULL);
1309*d6eebaa4SHervé Poussineau
1310*d6eebaa4SHervé Poussineau if (client->conn_state != TCP_DISCONNECTED) {
1311*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_client_connect: Already connected\n"));
1312*d6eebaa4SHervé Poussineau return ERR_ISCONN;
1313*d6eebaa4SHervé Poussineau }
1314*d6eebaa4SHervé Poussineau
1315*d6eebaa4SHervé Poussineau /* Wipe clean, but keep callbacks */
1316*d6eebaa4SHervé Poussineau data_cb = client->data_cb;
1317*d6eebaa4SHervé Poussineau pub_cb = client->pub_cb;
1318*d6eebaa4SHervé Poussineau inpub_arg = client->inpub_arg;
1319*d6eebaa4SHervé Poussineau memset(client, 0, sizeof(mqtt_client_t));
1320*d6eebaa4SHervé Poussineau client->data_cb = data_cb;
1321*d6eebaa4SHervé Poussineau client->pub_cb = pub_cb;
1322*d6eebaa4SHervé Poussineau client->inpub_arg = inpub_arg;
1323*d6eebaa4SHervé Poussineau
1324*d6eebaa4SHervé Poussineau client->connect_arg = arg;
1325*d6eebaa4SHervé Poussineau client->connect_cb = cb;
1326*d6eebaa4SHervé Poussineau client->keep_alive = client_info->keep_alive;
1327*d6eebaa4SHervé Poussineau mqtt_init_requests(client->req_list, LWIP_ARRAYSIZE(client->req_list));
1328*d6eebaa4SHervé Poussineau
1329*d6eebaa4SHervé Poussineau /* Build connect message */
1330*d6eebaa4SHervé Poussineau if (client_info->will_topic != NULL && client_info->will_msg != NULL) {
1331*d6eebaa4SHervé Poussineau flags |= MQTT_CONNECT_FLAG_WILL;
1332*d6eebaa4SHervé Poussineau flags |= (client_info->will_qos & 3) << 3;
1333*d6eebaa4SHervé Poussineau if (client_info->will_retain) {
1334*d6eebaa4SHervé Poussineau flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
1335*d6eebaa4SHervé Poussineau }
1336*d6eebaa4SHervé Poussineau len = strlen(client_info->will_topic);
1337*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->will_topic length overflow", len <= 0xFF, return ERR_VAL);
1338*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->will_topic length must be > 0", len > 0, return ERR_VAL);
1339*d6eebaa4SHervé Poussineau will_topic_len = (u8_t)len;
1340*d6eebaa4SHervé Poussineau len = strlen(client_info->will_msg);
1341*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->will_msg length overflow", len <= 0xFF, return ERR_VAL);
1342*d6eebaa4SHervé Poussineau will_msg_len = (u8_t)len;
1343*d6eebaa4SHervé Poussineau len = remaining_length + 2 + will_topic_len + 2 + will_msg_len;
1344*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: remaining_length overflow", len <= 0xFFFF, return ERR_VAL);
1345*d6eebaa4SHervé Poussineau remaining_length = (u16_t)len;
1346*d6eebaa4SHervé Poussineau }
1347*d6eebaa4SHervé Poussineau if (client_info->client_user != NULL) {
1348*d6eebaa4SHervé Poussineau flags |= MQTT_CONNECT_FLAG_USERNAME;
1349*d6eebaa4SHervé Poussineau len = strlen(client_info->client_user);
1350*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->client_user length overflow", len <= 0xFFFF, return ERR_VAL);
1351*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->client_user length must be > 0", len > 0, return ERR_VAL);
1352*d6eebaa4SHervé Poussineau client_user_len = (u16_t)len;
1353*d6eebaa4SHervé Poussineau len = remaining_length + 2 + client_user_len;
1354*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: remaining_length overflow", len <= 0xFFFF, return ERR_VAL);
1355*d6eebaa4SHervé Poussineau remaining_length = (u16_t)len;
1356*d6eebaa4SHervé Poussineau }
1357*d6eebaa4SHervé Poussineau if (client_info->client_pass != NULL) {
1358*d6eebaa4SHervé Poussineau flags |= MQTT_CONNECT_FLAG_PASSWORD;
1359*d6eebaa4SHervé Poussineau len = strlen(client_info->client_pass);
1360*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->client_pass length overflow", len <= 0xFFFF, return ERR_VAL);
1361*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->client_pass length must be > 0", len > 0, return ERR_VAL);
1362*d6eebaa4SHervé Poussineau client_pass_len = (u16_t)len;
1363*d6eebaa4SHervé Poussineau len = remaining_length + 2 + client_pass_len;
1364*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: remaining_length overflow", len <= 0xFFFF, return ERR_VAL);
1365*d6eebaa4SHervé Poussineau remaining_length = (u16_t)len;
1366*d6eebaa4SHervé Poussineau }
1367*d6eebaa4SHervé Poussineau
1368*d6eebaa4SHervé Poussineau /* Don't complicate things, always connect using clean session */
1369*d6eebaa4SHervé Poussineau flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
1370*d6eebaa4SHervé Poussineau
1371*d6eebaa4SHervé Poussineau len = strlen(client_info->client_id);
1372*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: client_info->client_id length overflow", len <= 0xFFFF, return ERR_VAL);
1373*d6eebaa4SHervé Poussineau client_id_length = (u16_t)len;
1374*d6eebaa4SHervé Poussineau len = remaining_length + 2 + client_id_length;
1375*d6eebaa4SHervé Poussineau LWIP_ERROR("mqtt_client_connect: remaining_length overflow", len <= 0xFFFF, return ERR_VAL);
1376*d6eebaa4SHervé Poussineau remaining_length = (u16_t)len;
1377*d6eebaa4SHervé Poussineau
1378*d6eebaa4SHervé Poussineau if (mqtt_output_check_space(&client->output, remaining_length) == 0) {
1379*d6eebaa4SHervé Poussineau return ERR_MEM;
1380*d6eebaa4SHervé Poussineau }
1381*d6eebaa4SHervé Poussineau
1382*d6eebaa4SHervé Poussineau #if LWIP_ALTCP && LWIP_ALTCP_TLS
1383*d6eebaa4SHervé Poussineau if (client_info->tls_config) {
1384*d6eebaa4SHervé Poussineau client->conn = altcp_tls_new(client_info->tls_config, IP_GET_TYPE(ip_addr));
1385*d6eebaa4SHervé Poussineau } else
1386*d6eebaa4SHervé Poussineau #endif
1387*d6eebaa4SHervé Poussineau {
1388*d6eebaa4SHervé Poussineau client->conn = altcp_tcp_new_ip_type(IP_GET_TYPE(ip_addr));
1389*d6eebaa4SHervé Poussineau }
1390*d6eebaa4SHervé Poussineau if (client->conn == NULL) {
1391*d6eebaa4SHervé Poussineau return ERR_MEM;
1392*d6eebaa4SHervé Poussineau }
1393*d6eebaa4SHervé Poussineau
1394*d6eebaa4SHervé Poussineau /* Set arg pointer for callbacks */
1395*d6eebaa4SHervé Poussineau altcp_arg(client->conn, client);
1396*d6eebaa4SHervé Poussineau /* Any local address, pick random local port number */
1397*d6eebaa4SHervé Poussineau err = altcp_bind(client->conn, IP_ADDR_ANY, 0);
1398*d6eebaa4SHervé Poussineau if (err != ERR_OK) {
1399*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_WARN, ("mqtt_client_connect: Error binding to local ip/port, %d\n", err));
1400*d6eebaa4SHervé Poussineau goto tcp_fail;
1401*d6eebaa4SHervé Poussineau }
1402*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_client_connect: Connecting to host: %s at port:%"U16_F"\n", ipaddr_ntoa(ip_addr), port));
1403*d6eebaa4SHervé Poussineau
1404*d6eebaa4SHervé Poussineau /* Connect to server */
1405*d6eebaa4SHervé Poussineau err = altcp_connect(client->conn, ip_addr, port, mqtt_tcp_connect_cb);
1406*d6eebaa4SHervé Poussineau if (err != ERR_OK) {
1407*d6eebaa4SHervé Poussineau LWIP_DEBUGF(MQTT_DEBUG_TRACE, ("mqtt_client_connect: Error connecting to remote ip/port, %d\n", err));
1408*d6eebaa4SHervé Poussineau goto tcp_fail;
1409*d6eebaa4SHervé Poussineau }
1410*d6eebaa4SHervé Poussineau /* Set error callback */
1411*d6eebaa4SHervé Poussineau altcp_err(client->conn, mqtt_tcp_err_cb);
1412*d6eebaa4SHervé Poussineau client->conn_state = TCP_CONNECTING;
1413*d6eebaa4SHervé Poussineau
1414*d6eebaa4SHervé Poussineau /* Append fixed header */
1415*d6eebaa4SHervé Poussineau mqtt_output_append_fixed_header(&client->output, MQTT_MSG_TYPE_CONNECT, 0, 0, 0, remaining_length);
1416*d6eebaa4SHervé Poussineau /* Append Protocol string */
1417*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, "MQTT", 4);
1418*d6eebaa4SHervé Poussineau /* Append Protocol level */
1419*d6eebaa4SHervé Poussineau mqtt_output_append_u8(&client->output, 4);
1420*d6eebaa4SHervé Poussineau /* Append connect flags */
1421*d6eebaa4SHervé Poussineau mqtt_output_append_u8(&client->output, flags);
1422*d6eebaa4SHervé Poussineau /* Append keep-alive */
1423*d6eebaa4SHervé Poussineau mqtt_output_append_u16(&client->output, client_info->keep_alive);
1424*d6eebaa4SHervé Poussineau /* Append client id */
1425*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, client_info->client_id, client_id_length);
1426*d6eebaa4SHervé Poussineau /* Append will message if used */
1427*d6eebaa4SHervé Poussineau if ((flags & MQTT_CONNECT_FLAG_WILL) != 0) {
1428*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, client_info->will_topic, will_topic_len);
1429*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, client_info->will_msg, will_msg_len);
1430*d6eebaa4SHervé Poussineau }
1431*d6eebaa4SHervé Poussineau /* Append user name if given */
1432*d6eebaa4SHervé Poussineau if ((flags & MQTT_CONNECT_FLAG_USERNAME) != 0) {
1433*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, client_info->client_user, client_user_len);
1434*d6eebaa4SHervé Poussineau }
1435*d6eebaa4SHervé Poussineau /* Append password if given */
1436*d6eebaa4SHervé Poussineau if ((flags & MQTT_CONNECT_FLAG_PASSWORD) != 0) {
1437*d6eebaa4SHervé Poussineau mqtt_output_append_string(&client->output, client_info->client_pass, client_pass_len);
1438*d6eebaa4SHervé Poussineau }
1439*d6eebaa4SHervé Poussineau return ERR_OK;
1440*d6eebaa4SHervé Poussineau
1441*d6eebaa4SHervé Poussineau tcp_fail:
1442*d6eebaa4SHervé Poussineau altcp_abort(client->conn);
1443*d6eebaa4SHervé Poussineau client->conn = NULL;
1444*d6eebaa4SHervé Poussineau return err;
1445*d6eebaa4SHervé Poussineau }
1446*d6eebaa4SHervé Poussineau
1447*d6eebaa4SHervé Poussineau
1448*d6eebaa4SHervé Poussineau /**
1449*d6eebaa4SHervé Poussineau * @ingroup mqtt
1450*d6eebaa4SHervé Poussineau * Disconnect from MQTT server
1451*d6eebaa4SHervé Poussineau * @param client MQTT client
1452*d6eebaa4SHervé Poussineau */
1453*d6eebaa4SHervé Poussineau void
mqtt_disconnect(mqtt_client_t * client)1454*d6eebaa4SHervé Poussineau mqtt_disconnect(mqtt_client_t *client)
1455*d6eebaa4SHervé Poussineau {
1456*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1457*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_disconnect: client != NULL", client);
1458*d6eebaa4SHervé Poussineau /* If connection in not already closed */
1459*d6eebaa4SHervé Poussineau if (client->conn_state != TCP_DISCONNECTED) {
1460*d6eebaa4SHervé Poussineau /* Set conn_state before calling mqtt_close to prevent callback from being called */
1461*d6eebaa4SHervé Poussineau client->conn_state = TCP_DISCONNECTED;
1462*d6eebaa4SHervé Poussineau mqtt_close(client, (mqtt_connection_status_t)0);
1463*d6eebaa4SHervé Poussineau }
1464*d6eebaa4SHervé Poussineau }
1465*d6eebaa4SHervé Poussineau
1466*d6eebaa4SHervé Poussineau /**
1467*d6eebaa4SHervé Poussineau * @ingroup mqtt
1468*d6eebaa4SHervé Poussineau * Check connection with server
1469*d6eebaa4SHervé Poussineau * @param client MQTT client
1470*d6eebaa4SHervé Poussineau * @return 1 if connected to server, 0 otherwise
1471*d6eebaa4SHervé Poussineau */
1472*d6eebaa4SHervé Poussineau u8_t
mqtt_client_is_connected(mqtt_client_t * client)1473*d6eebaa4SHervé Poussineau mqtt_client_is_connected(mqtt_client_t *client)
1474*d6eebaa4SHervé Poussineau {
1475*d6eebaa4SHervé Poussineau LWIP_ASSERT_CORE_LOCKED();
1476*d6eebaa4SHervé Poussineau LWIP_ASSERT("mqtt_client_is_connected: client != NULL", client);
1477*d6eebaa4SHervé Poussineau return client->conn_state == MQTT_CONNECTED;
1478*d6eebaa4SHervé Poussineau }
1479*d6eebaa4SHervé Poussineau
1480*d6eebaa4SHervé Poussineau #endif /* LWIP_TCP && LWIP_CALLBACK_API */
1481