Skip to content

Commit 589efee

Browse files
committed
Adding publish and subscribe atomic client examples
1 parent 65a88f9 commit 589efee

File tree

11 files changed

+1281
-14
lines changed

11 files changed

+1281
-14
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ examples/sn-client/sn-client_qos-1
7878
examples/sn-client/sn-multithread
7979
examples/multithread/multithread
8080
examples/wiot/wiot
81+
examples/pub-sub/pub
82+
examples/pub-sub/sub
8183

8284
# eclipse
8385
.cproject

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ if (WOLFMQTT_EXAMPLES)
147147
add_mqtt_example(azureiothub azure/azureiothub.c)
148148
add_mqtt_example(fwpush firmware/fwpush.c)
149149
add_mqtt_example(fwclient firmware/fwclient.c)
150+
add_mqtt_example(mqtt-pub pub-sub/mqtt-pub.c)
151+
add_mqtt_example(mqtt-pub pub-sub/mqtt-sub.c)
150152
endif()
151153

152154
####################################################

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ WolfGatewayQoS-1,wolfMQTT/example/testTopic, 1
213213
### Multithread Example
214214
This example exercises the multithreading capabilities of the client library. The client implements two tasks: one that publishes to the broker; and another that waits for messages from the broker. The publish thread is created `NUM_PUB_TASKS` times (10 by default) and sends unique messages to the broker. This feature is enabled using the `--enable-mt` configuration option. The example is located in `/examples/multithread/`.
215215

216+
### Atomic publish and subscribe examples
217+
In the `examples/pub-sub` folder, there are two simple client examples:
218+
* mqtt-pub - publishes to a topic
219+
* mqtt-sub - subscribes to a topic and waits for messages
220+
221+
These examples are useful for quickly testing or scripting.
222+
216223
## Example Options
217224
The command line examples can be executed with optional parameters. To see a list of the available parameters, add the `-?`
218225

examples/include.am

+25-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ noinst_PROGRAMS += examples/mqttclient/mqttclient \
1313
examples/multithread/multithread \
1414
examples/sn-client/sn-client \
1515
examples/sn-client/sn-client_qos-1 \
16-
examples/sn-client/sn-multithread
16+
examples/sn-client/sn-multithread \
17+
examples/pub-sub/mqtt-pub \
18+
examples/pub-sub/mqtt-sub
1719

1820
noinst_HEADERS += examples/mqttclient/mqttclient.h \
1921
examples/mqttsimple/mqttsimple.h \
@@ -28,7 +30,8 @@ noinst_HEADERS += examples/mqttclient/mqttclient.h \
2830
examples/mqttport.h \
2931
examples/nbclient/nbclient.h \
3032
examples/multithread/multithread.h \
31-
examples/sn-client/sn-client.h
33+
examples/sn-client/sn-client.h \
34+
examples/pub-sub/mqtt-pub-sub.h
3235

3336
# MQTT Client Example
3437
examples_mqttclient_mqttclient_SOURCES = examples/mqttclient/mqttclient.c \
@@ -127,6 +130,21 @@ examples_sn_client_sn_multithread_SOURCES = examples/sn-client/sn-multithr
127130
examples_sn_client_sn_multithread_LDADD = src/libwolfmqtt.la
128131
examples_sn_client_sn_multithread_DEPENDENCIES = src/libwolfmqtt.la
129132
examples_sn_client_sn_multithread_CPPFLAGS = -I$(top_srcdir)/examples $(AM_CPPFLAGS)
133+
134+
# MQTT pub and sub clients
135+
examples_pub_sub_mqtt_pub_SOURCES = examples/pub-sub/mqtt-pub.c \
136+
examples/mqttnet.c \
137+
examples/mqttexample.c
138+
examples_pub_sub_mqtt_pub_LDADD = src/libwolfmqtt.la
139+
examples_pub_sub_mqtt_pub_DEPENDENCIES = src/libwolfmqtt.la
140+
examples_pub_sub_mqtt_pub_CPPFLAGS = -I$(top_srcdir)/examples $(AM_CPPFLAGS)
141+
142+
examples_pub_sub_mqtt_sub_SOURCES = examples/pub-sub/mqtt-sub.c \
143+
examples/mqttnet.c \
144+
examples/mqttexample.c
145+
examples_pub_sub_mqtt_sub_LDADD = src/libwolfmqtt.la
146+
examples_pub_sub_mqtt_sub_DEPENDENCIES = src/libwolfmqtt.la
147+
examples_pub_sub_mqtt_sub_CPPFLAGS = -I$(top_srcdir)/examples $(AM_CPPFLAGS)
130148
endif
131149

132150

@@ -145,6 +163,8 @@ dist_example_DATA+= examples/multithread/multithread.c
145163
dist_example_DATA+= examples/sn-client/sn-client.c
146164
dist_example_DATA+= examples/sn-client/sn-client_qos-1.c
147165
dist_example_DATA+= examples/sn-client/sn-multithread.c
166+
dist_example_DATA+= examples/pub-sub/mqtt-pub.c
167+
dist_example_DATA+= examples/pub-sub/mqtt-sub.c
148168

149169
DISTCLEANFILES+= examples/mqttclient/.libs/mqttclient \
150170
examples/firmware/.libs/fwpush \
@@ -156,7 +176,9 @@ DISTCLEANFILES+= examples/mqttclient/.libs/mqttclient \
156176
examples/multithread/.libs/multithread \
157177
examples/sn-client/.libs/sn-client \
158178
examples/sn-client/.libs/sn-client_qos-1 \
159-
examples/sn-client/.libs/sn-multithread
179+
examples/sn-client/.libs/sn-multithread \
180+
examples/pub-sub/mqtt-pub \
181+
examples/pub-sub/mqtt-sub
160182

161183
EXTRA_DIST+= examples/mqttuart.c \
162184
examples/publish.dat \

examples/mqttclient/mqttclient.c

-3
Original file line numberDiff line numberDiff line change
@@ -648,9 +648,6 @@ int mqttclient_test(MQTTCtx *mqttCtx)
648648

649649
PRINTF("MQTT Disconnect: %s (%d)",
650650
MqttClient_ReturnCodeToString(rc), rc);
651-
if (rc != MQTT_CODE_SUCCESS) {
652-
goto disconn;
653-
}
654651

655652
rc = MqttClient_NetDisconnect(&mqttCtx->client);
656653

examples/mqttexample.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ void mqtt_show_usage(MQTTCtx* mqttCtx)
247247
#endif
248248
PRINTF("-T Test mode");
249249
PRINTF("-f <file> Use file contents for publish");
250+
if (!mqttCtx->debug_on) {
251+
PRINTF("-d Enable example debug messages");
252+
}
250253
}
251254

252255
void mqtt_init_ctx(MQTTCtx* mqttCtx)
@@ -259,6 +262,7 @@ void mqtt_init_ctx(MQTTCtx* mqttCtx)
259262
mqttCtx->client_id = kDefClientId;
260263
mqttCtx->topic_name = kDefTopicName;
261264
mqttCtx->cmd_timeout_ms = DEFAULT_CMD_TIMEOUT_MS;
265+
mqttCtx->debug_on = 1;
262266
#ifdef WOLFMQTT_V5
263267
mqttCtx->max_packet_size = DEFAULT_MAX_PKT_SZ;
264268
mqttCtx->topic_alias = 1;
@@ -286,7 +290,7 @@ int mqtt_parse_args(MQTTCtx* mqttCtx, int argc, char** argv)
286290
#define MQTT_V5_ARGS ""
287291
#endif
288292

289-
while ((rc = mygetopt(argc, argv, "?h:p:q:sk:i:lu:w:m:n:C:Tf:rt" \
293+
while ((rc = mygetopt(argc, argv, "?h:p:q:sk:i:lu:w:m:n:C:Tf:rtd" \
290294
MQTT_TLS_ARGS MQTT_V5_ARGS)) != -1) {
291295
switch ((char)rc) {
292296
case '?' :
@@ -364,6 +368,10 @@ int mqtt_parse_args(MQTTCtx* mqttCtx, int argc, char** argv)
364368
mqttCtx->use_tls = 1;
365369
break;
366370

371+
case 'd':
372+
mqttCtx->debug_on = 1;
373+
break;
374+
367375
#ifdef ENABLE_MQTT_TLS
368376
case 'A':
369377
mTlsCaFile = myoptarg;

examples/mqttexample.h

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ typedef struct _MQTTCtx {
168168
#endif
169169
byte clean_session;
170170
byte test_mode;
171+
byte debug_on:1; /* enable debug messages in example */
171172
#ifdef WOLFMQTT_V5
172173
byte subId_not_avail; /* Server property */
173174
byte enable_eauth; /* Enhanced authentication */

examples/mqttnet.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ static int NetConnect(void *context, const char* host, word16 port,
7676

7777
switch (sock->stat) {
7878
case SOCK_BEGIN:
79-
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
80-
host, port, timeout_ms, mqttCtx->use_tls);
79+
if (mqttCtx->debug_on) {
80+
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
81+
host, port, timeout_ms, mqttCtx->use_tls);
82+
}
8183

8284
hostIp = FreeRTOS_gethostbyname_a(host, NULL, 0, 0);
8385
if (hostIp == 0)
@@ -255,9 +257,10 @@ static int NetConnect(void *context, const char* host, word16 port,
255257
switch(sock->stat) {
256258
case SOCK_BEGIN:
257259
{
258-
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
259-
host, port, timeout_ms, mqttCtx->use_tls);
260-
260+
if (mqttCtx->debug_on) {
261+
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, "
262+
"Use TLS %d", host, port, timeout_ms, mqttCtx->use_tls);
263+
}
261264
XMEMSET(&hints, 0, sizeof(hints));
262265
hints.ai_family = AF_INET;
263266
hints.ai_socktype = SOCK_STREAM;
@@ -446,8 +449,10 @@ static int NetConnect(void *context, const char* host, word16 port,
446449
switch(sock->stat) {
447450
case SOCK_BEGIN:
448451
{
449-
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
450-
host, port, timeout_ms, mqttCtx->use_tls);
452+
if (mqttCtx->debug_on) {
453+
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, "
454+
"Use TLS %d", host, port, timeout_ms, mqttCtx->use_tls);
455+
}
451456

452457
XMEMSET(&hints, 0, sizeof(hints));
453458
hints.ai_family = AF_INET;

examples/pub-sub/mqtt-pub-sub.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* mqtt-pub-sub
2+
*
3+
* Copyright (C) 2006-2023 wolfSSL Inc.
4+
*
5+
* This file is part of wolfMQTT.
6+
*
7+
* wolfMQTT is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfMQTT is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
#ifndef WOLFMQTT_PUB_SUB_H
23+
#define WOLFMQTT_PUB_SUB_H
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
30+
/* Exposed functions */
31+
int pub_client(MQTTCtx *mqttCtx);
32+
int sub_client(MQTTCtx *mqttCtx);
33+
34+
#ifdef __cplusplus
35+
}
36+
#endif
37+
38+
#endif /* WOLFMQTT_PUB_SUB_H */

0 commit comments

Comments
 (0)