1 /*
2 * Redistribution and use in source and binary forms, with or without modification,
3 * are permitted provided that the following conditions are met:
4 *
5 * 1. Redistributions of source code must retain the above copyright notice,
6 * this list of conditions and the following disclaimer.
7 * 2. Redistributions in binary form must reproduce the above copyright notice,
8 * this list of conditions and the following disclaimer in the documentation
9 * and/or other materials provided with the distribution.
10 * 3. The name of the author may not be used to endorse or promote products
11 * derived from this software without specific prior written permission.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
16 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
17 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
18 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
19 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
21 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
22 * OF SUCH DAMAGE.
23 *
24 * This file is part of the lwIP TCP/IP stack.
25 *
26 * Author: Dirk Ziegelmeier <dziegel@gmx.de>
27 *
28 */
29
30 #include "lwip/apps/mqtt.h"
31 #include "mqtt_example.h"
32
33 #if LWIP_TCP
34
35 /** Define this to a compile-time IP address initialization
36 * to connect anything else than IPv4 loopback
37 */
38 #ifndef LWIP_MQTT_EXAMPLE_IPADDR_INIT
39 #if LWIP_IPV4
40 #define LWIP_MQTT_EXAMPLE_IPADDR_INIT = IPADDR4_INIT(PP_HTONL(IPADDR_LOOPBACK))
41 #else
42 #define LWIP_MQTT_EXAMPLE_IPADDR_INIT
43 #endif
44 #endif
45
46 static ip_addr_t mqtt_ip LWIP_MQTT_EXAMPLE_IPADDR_INIT;
47 static mqtt_client_t* mqtt_client;
48
49 static const struct mqtt_connect_client_info_t mqtt_client_info =
50 {
51 "test",
52 NULL, /* user */
53 NULL, /* pass */
54 100, /* keep alive */
55 NULL, /* will_topic */
56 NULL, /* will_msg */
57 0, /* will_msg_len */
58 0, /* will_qos */
59 0 /* will_retain */
60 #if LWIP_ALTCP && LWIP_ALTCP_TLS
61 , NULL
62 #endif
63 };
64
65 static void
mqtt_incoming_data_cb(void * arg,const u8_t * data,u16_t len,u8_t flags)66 mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
67 {
68 const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
69 LWIP_UNUSED_ARG(data);
70
71 LWIP_PLATFORM_DIAG(("MQTT client \"%s\" data cb: len %d, flags %d\n", client_info->client_id,
72 (int)len, (int)flags));
73 }
74
75 static void
mqtt_incoming_publish_cb(void * arg,const char * topic,u32_t tot_len)76 mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
77 {
78 const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
79
80 LWIP_PLATFORM_DIAG(("MQTT client \"%s\" publish cb: topic %s, len %d\n", client_info->client_id,
81 topic, (int)tot_len));
82 }
83
84 static void
mqtt_request_cb(void * arg,err_t err)85 mqtt_request_cb(void *arg, err_t err)
86 {
87 const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
88
89 LWIP_PLATFORM_DIAG(("MQTT client \"%s\" request cb: err %d\n", client_info->client_id, (int)err));
90 }
91
92 static void
mqtt_connection_cb(mqtt_client_t * client,void * arg,mqtt_connection_status_t status)93 mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
94 {
95 const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
96 LWIP_UNUSED_ARG(client);
97
98 LWIP_PLATFORM_DIAG(("MQTT client \"%s\" connection cb: status %d\n", client_info->client_id, (int)status));
99
100 if (status == MQTT_CONNECT_ACCEPTED) {
101 mqtt_sub_unsub(client,
102 "topic_qos1", 1,
103 mqtt_request_cb, LWIP_CONST_CAST(void*, client_info),
104 1);
105 mqtt_sub_unsub(client,
106 "topic_qos0", 0,
107 mqtt_request_cb, LWIP_CONST_CAST(void*, client_info),
108 1);
109 }
110 }
111 #endif /* LWIP_TCP */
112
113 void
mqtt_example_init(void)114 mqtt_example_init(void)
115 {
116 #if LWIP_TCP
117 mqtt_client = mqtt_client_new();
118
119 mqtt_set_inpub_callback(mqtt_client,
120 mqtt_incoming_publish_cb,
121 mqtt_incoming_data_cb,
122 LWIP_CONST_CAST(void*, &mqtt_client_info));
123
124 mqtt_client_connect(mqtt_client,
125 &mqtt_ip, MQTT_PORT,
126 mqtt_connection_cb, LWIP_CONST_CAST(void*, &mqtt_client_info),
127 &mqtt_client_info);
128 #endif /* LWIP_TCP */
129 }
130