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(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_qos */
|
---|
58 | 0 /* will_retain */
|
---|
59 | #if LWIP_ALTCP && LWIP_ALTCP_TLS
|
---|
60 | , NULL
|
---|
61 | #endif
|
---|
62 | };
|
---|
63 |
|
---|
64 | static void
|
---|
65 | mqtt_incoming_data_cb(void *arg, const u8_t *data, u16_t len, u8_t flags)
|
---|
66 | {
|
---|
67 | const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
|
---|
68 | LWIP_UNUSED_ARG(data);
|
---|
69 |
|
---|
70 | printf("MQTT client \"%s\" data cb: len %d, flags %d\n", client_info->client_id,
|
---|
71 | (int)len, (int)flags);
|
---|
72 | }
|
---|
73 |
|
---|
74 | static void
|
---|
75 | mqtt_incoming_publish_cb(void *arg, const char *topic, u32_t tot_len)
|
---|
76 | {
|
---|
77 | const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
|
---|
78 |
|
---|
79 | printf("MQTT client \"%s\" publish cb: topic %s, len %d\n", client_info->client_id,
|
---|
80 | topic, (int)tot_len);
|
---|
81 | }
|
---|
82 |
|
---|
83 | static void
|
---|
84 | mqtt_request_cb(void *arg, err_t err)
|
---|
85 | {
|
---|
86 | const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
|
---|
87 |
|
---|
88 | printf("MQTT client \"%s\" request cb: err %d\n", client_info->client_id, (int)err);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void
|
---|
92 | mqtt_connection_cb(mqtt_client_t *client, void *arg, mqtt_connection_status_t status)
|
---|
93 | {
|
---|
94 | const struct mqtt_connect_client_info_t* client_info = (const struct mqtt_connect_client_info_t*)arg;
|
---|
95 | LWIP_UNUSED_ARG(client);
|
---|
96 |
|
---|
97 | printf("MQTT client \"%s\" connection cb: status %d\n", client_info->client_id, (int)status);
|
---|
98 |
|
---|
99 | if (status == MQTT_CONNECT_ACCEPTED) {
|
---|
100 | mqtt_sub_unsub(client,
|
---|
101 | "topic_qos1", 1,
|
---|
102 | mqtt_request_cb, LWIP_CONST_CAST(void*, client_info),
|
---|
103 | 1);
|
---|
104 | mqtt_sub_unsub(client,
|
---|
105 | "topic_qos0", 0,
|
---|
106 | mqtt_request_cb, LWIP_CONST_CAST(void*, client_info),
|
---|
107 | 1);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | #endif /* LWIP_TCP */
|
---|
111 |
|
---|
112 | void
|
---|
113 | mqtt_example_init(void)
|
---|
114 | {
|
---|
115 | #if LWIP_TCP
|
---|
116 | mqtt_client = mqtt_client_new();
|
---|
117 |
|
---|
118 | mqtt_set_inpub_callback(mqtt_client,
|
---|
119 | mqtt_incoming_publish_cb,
|
---|
120 | mqtt_incoming_data_cb,
|
---|
121 | LWIP_CONST_CAST(void*, &mqtt_client_info));
|
---|
122 |
|
---|
123 | mqtt_client_connect(mqtt_client,
|
---|
124 | &mqtt_ip, MQTT_PORT,
|
---|
125 | mqtt_connection_cb, LWIP_CONST_CAST(void*, &mqtt_client_info),
|
---|
126 | &mqtt_client_info);
|
---|
127 | #endif /* LWIP_TCP */
|
---|
128 | }
|
---|