Skip to content

Commit

Permalink
Adding publish and subscribe atomic client examples
Browse files Browse the repository at this point in the history
  • Loading branch information
embhorn committed Aug 15, 2023
1 parent 65a88f9 commit 8032ff2
Show file tree
Hide file tree
Showing 11 changed files with 1,286 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ examples/sn-client/sn-client_qos-1
examples/sn-client/sn-multithread
examples/multithread/multithread
examples/wiot/wiot
examples/pub-sub/pub
examples/pub-sub/sub

# eclipse
.cproject
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ if (WOLFMQTT_EXAMPLES)
add_mqtt_example(azureiothub azure/azureiothub.c)
add_mqtt_example(fwpush firmware/fwpush.c)
add_mqtt_example(fwclient firmware/fwclient.c)
add_mqtt_example(mqtt-pub pub-sub/mqtt-pub.c)
add_mqtt_example(mqtt-pub pub-sub/mqtt-sub.c)
endif()

####################################################
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ WolfGatewayQoS-1,wolfMQTT/example/testTopic, 1
### Multithread Example
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/`.

### Atomic publish and subscribe examples
In the `examples/pub-sub` folder, there are two simple client examples:
* mqtt-pub - publishes to a topic
* mqtt-sub - subscribes to a topic and waits for messages

These examples are useful for quickly testing or scripting.

## Example Options
The command line examples can be executed with optional parameters. To see a list of the available parameters, add the `-?`

Expand Down
28 changes: 25 additions & 3 deletions examples/include.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ noinst_PROGRAMS += examples/mqttclient/mqttclient \
examples/multithread/multithread \
examples/sn-client/sn-client \
examples/sn-client/sn-client_qos-1 \
examples/sn-client/sn-multithread
examples/sn-client/sn-multithread \
examples/pub-sub/mqtt-pub \
examples/pub-sub/mqtt-sub

noinst_HEADERS += examples/mqttclient/mqttclient.h \
examples/mqttsimple/mqttsimple.h \
Expand All @@ -28,7 +30,8 @@ noinst_HEADERS += examples/mqttclient/mqttclient.h \
examples/mqttport.h \
examples/nbclient/nbclient.h \
examples/multithread/multithread.h \
examples/sn-client/sn-client.h
examples/sn-client/sn-client.h \
examples/pub-sub/mqtt-pub-sub.h

# MQTT Client Example
examples_mqttclient_mqttclient_SOURCES = examples/mqttclient/mqttclient.c \
Expand Down Expand Up @@ -127,6 +130,21 @@ examples_sn_client_sn_multithread_SOURCES = examples/sn-client/sn-multithr
examples_sn_client_sn_multithread_LDADD = src/libwolfmqtt.la
examples_sn_client_sn_multithread_DEPENDENCIES = src/libwolfmqtt.la
examples_sn_client_sn_multithread_CPPFLAGS = -I$(top_srcdir)/examples $(AM_CPPFLAGS)

# MQTT pub and sub clients
examples_pub_sub_mqtt_pub_SOURCES = examples/pub-sub/mqtt-pub.c \
examples/mqttnet.c \
examples/mqttexample.c
examples_pub_sub_mqtt_pub_LDADD = src/libwolfmqtt.la
examples_pub_sub_mqtt_pub_DEPENDENCIES = src/libwolfmqtt.la
examples_pub_sub_mqtt_pub_CPPFLAGS = -I$(top_srcdir)/examples $(AM_CPPFLAGS)

examples_pub_sub_mqtt_sub_SOURCES = examples/pub-sub/mqtt-sub.c \
examples/mqttnet.c \
examples/mqttexample.c
examples_pub_sub_mqtt_sub_LDADD = src/libwolfmqtt.la
examples_pub_sub_mqtt_sub_DEPENDENCIES = src/libwolfmqtt.la
examples_pub_sub_mqtt_sub_CPPFLAGS = -I$(top_srcdir)/examples $(AM_CPPFLAGS)
endif


Expand All @@ -145,6 +163,8 @@ dist_example_DATA+= examples/multithread/multithread.c
dist_example_DATA+= examples/sn-client/sn-client.c
dist_example_DATA+= examples/sn-client/sn-client_qos-1.c
dist_example_DATA+= examples/sn-client/sn-multithread.c
dist_example_DATA+= examples/pub-sub/mqtt-pub.c
dist_example_DATA+= examples/pub-sub/mqtt-sub.c

DISTCLEANFILES+= examples/mqttclient/.libs/mqttclient \
examples/firmware/.libs/fwpush \
Expand All @@ -156,7 +176,9 @@ DISTCLEANFILES+= examples/mqttclient/.libs/mqttclient \
examples/multithread/.libs/multithread \
examples/sn-client/.libs/sn-client \
examples/sn-client/.libs/sn-client_qos-1 \
examples/sn-client/.libs/sn-multithread
examples/sn-client/.libs/sn-multithread \
examples/pub-sub/mqtt-pub \
examples/pub-sub/mqtt-sub

EXTRA_DIST+= examples/mqttuart.c \
examples/publish.dat \
Expand Down
3 changes: 0 additions & 3 deletions examples/mqttclient/mqttclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,6 @@ int mqttclient_test(MQTTCtx *mqttCtx)

PRINTF("MQTT Disconnect: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto disconn;
}

rc = MqttClient_NetDisconnect(&mqttCtx->client);

Expand Down
10 changes: 9 additions & 1 deletion examples/mqttexample.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ void mqtt_show_usage(MQTTCtx* mqttCtx)
#endif
PRINTF("-T Test mode");
PRINTF("-f <file> Use file contents for publish");
if (!mqttCtx->debug_on) {
PRINTF("-d Enable example debug messages");
}
}

void mqtt_init_ctx(MQTTCtx* mqttCtx)
Expand All @@ -259,6 +262,7 @@ void mqtt_init_ctx(MQTTCtx* mqttCtx)
mqttCtx->client_id = kDefClientId;
mqttCtx->topic_name = kDefTopicName;
mqttCtx->cmd_timeout_ms = DEFAULT_CMD_TIMEOUT_MS;
mqttCtx->debug_on = 1;
#ifdef WOLFMQTT_V5
mqttCtx->max_packet_size = DEFAULT_MAX_PKT_SZ;
mqttCtx->topic_alias = 1;
Expand Down Expand Up @@ -286,7 +290,7 @@ int mqtt_parse_args(MQTTCtx* mqttCtx, int argc, char** argv)
#define MQTT_V5_ARGS ""
#endif

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

case 'd':
mqttCtx->debug_on = 1;
break;

#ifdef ENABLE_MQTT_TLS
case 'A':
mTlsCaFile = myoptarg;
Expand Down
1 change: 1 addition & 0 deletions examples/mqttexample.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ typedef struct _MQTTCtx {
#endif
byte clean_session;
byte test_mode;
byte debug_on:1; /* enable debug messages in example */
#ifdef WOLFMQTT_V5
byte subId_not_avail; /* Server property */
byte enable_eauth; /* Enhanced authentication */
Expand Down
19 changes: 12 additions & 7 deletions examples/mqttnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ static int NetConnect(void *context, const char* host, word16 port,

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

hostIp = FreeRTOS_gethostbyname_a(host, NULL, 0, 0);
if (hostIp == 0)
Expand Down Expand Up @@ -255,9 +257,10 @@ static int NetConnect(void *context, const char* host, word16 port,
switch(sock->stat) {
case SOCK_BEGIN:
{
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
host, port, timeout_ms, mqttCtx->use_tls);

if (mqttCtx->debug_on) {
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, "
"Use TLS %d", host, port, timeout_ms, mqttCtx->use_tls);
}
XMEMSET(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
Expand Down Expand Up @@ -446,8 +449,10 @@ static int NetConnect(void *context, const char* host, word16 port,
switch(sock->stat) {
case SOCK_BEGIN:
{
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, Use TLS %d",
host, port, timeout_ms, mqttCtx->use_tls);
if (mqttCtx->debug_on) {
PRINTF("NetConnect: Host %s, Port %u, Timeout %d ms, "
"Use TLS %d", host, port, timeout_ms, mqttCtx->use_tls);
}

XMEMSET(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
Expand Down
43 changes: 43 additions & 0 deletions examples/pub-sub/mqtt-pub-sub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* mqtt-pub-sub
*
* Copyright (C) 2006-2023 wolfSSL Inc.
*
* This file is part of wolfMQTT.
*
* wolfMQTT is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfMQTT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/

#ifndef WOLFMQTT_PUB_SUB_H
#define WOLFMQTT_PUB_SUB_H

#ifdef __cplusplus
extern "C" {
#endif


/* Exposed functions */
int pub_client(MQTTCtx *mqttCtx);
int sub_client(MQTTCtx *mqttCtx);

#if defined(NO_MAIN_DRIVER)
int mqttPub_main(int argc, char** argv);
int mqttSub_main(int argc, char** argv);
#endif

#ifdef __cplusplus
}
#endif

#endif /* WOLFMQTT_PUB_SUB_H */
Loading

0 comments on commit 8032ff2

Please sign in to comment.