diff --git a/samples/net/mqtt_publisher/Kconfig b/samples/net/mqtt_publisher/Kconfig new file mode 100644 index 00000000000..912987e3ae5 --- /dev/null +++ b/samples/net/mqtt_publisher/Kconfig @@ -0,0 +1,24 @@ +# Config options for mqtt_publisher sample application + +# Copyright (c) 2020 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +mainmenu "Networking mqtt_publisher sample application" + +config NET_SAMPLE_APP_MAX_ITERATIONS + int "Number of times to Publish sample MQTT messages" + default 500 + help + Send sample MQTT messages this many times in a MQTT connection + before exiting. A value of zero means that the messages are sent + forever. + +config NET_SAMPLE_APP_MAX_CONNECTIONS + int "Number of times to connect to the MQTT server" + default 0 + help + Number of times to connect to the MQTT server. With each connection + send NET_SAMPLE_APP_MAX_ITERATIONS amount of MQTT sample messages. + A value of zero means to continue forever. + +source "Kconfig.zephyr" diff --git a/samples/net/mqtt_publisher/README.rst b/samples/net/mqtt_publisher/README.rst index dc8e4ea5be5..a789197e93e 100644 --- a/samples/net/mqtt_publisher/README.rst +++ b/samples/net/mqtt_publisher/README.rst @@ -63,12 +63,6 @@ Max number of connection tries: #define APP_CONNECT_TRIES 10 -Max number of MQTT PUBLISH iterations: - -.. code-block:: c - - #define APP_MAX_ITERATIONS 5 - MQTT Client Identifier: .. code-block:: c @@ -93,6 +87,12 @@ following macros to specify those values: #define BLUEMIX_EVENT "status" #define BLUEMIX_FORMAT "json" +Max number of MQTT PUBLISH iterations is defined in Kconfig: + +.. code-block:: c + + CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS 5 + On your Linux host computer, open a terminal window, locate the source code of this sample application (i.e., :zephyr_file:`samples/net/mqtt_publisher`) and type: @@ -209,7 +209,7 @@ This is the output from the FRDM UART console, with: .. code-block:: c - #define APP_MAX_ITERATIONS 5 + CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS 5 .. code-block:: console diff --git a/samples/net/mqtt_publisher/overlay-sample.conf b/samples/net/mqtt_publisher/overlay-sample.conf new file mode 100644 index 00000000000..b1b0d868355 --- /dev/null +++ b/samples/net/mqtt_publisher/overlay-sample.conf @@ -0,0 +1,3 @@ +# Lower values used for testing +CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS=3 +CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS=3 diff --git a/samples/net/mqtt_publisher/src/config.h b/samples/net/mqtt_publisher/src/config.h index 29134a5b383..eb30398a9cc 100644 --- a/samples/net/mqtt_publisher/src/config.h +++ b/samples/net/mqtt_publisher/src/config.h @@ -50,8 +50,6 @@ #define APP_CONNECT_TRIES 10 -#define APP_MAX_ITERATIONS 100 - #define APP_MQTT_BUFFER_SIZE 128 #define MQTT_CLIENTID "zephyr_publisher" diff --git a/samples/net/mqtt_publisher/src/main.c b/samples/net/mqtt_publisher/src/main.c index ea54b4ef41b..59d5282c596 100644 --- a/samples/net/mqtt_publisher/src/main.c +++ b/samples/net/mqtt_publisher/src/main.c @@ -415,12 +415,12 @@ static int process_mqtt_and_sleep(struct mqtt_client *client, int timeout) return 0; } -#define SUCCESS_OR_EXIT(rc) { if (rc != 0) { return; } } +#define SUCCESS_OR_EXIT(rc) { if (rc != 0) { return 1; } } #define SUCCESS_OR_BREAK(rc) { if (rc != 0) { break; } } -static void publisher(void) +static int publisher(void) { - int i, rc; + int i, rc, r = 0; LOG_INF("attempting to connect: "); rc = try_to_connect(&client_ctx); @@ -428,7 +428,9 @@ static void publisher(void) SUCCESS_OR_EXIT(rc); i = 0; - while (i++ < APP_MAX_ITERATIONS && connected) { + while (i++ < CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS && connected) { + r = -1; + rc = mqtt_ping(&client_ctx); PRINT_RESULT("mqtt_ping", rc); SUCCESS_OR_BREAK(rc); @@ -456,16 +458,21 @@ static void publisher(void) rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); SUCCESS_OR_BREAK(rc); + + r = 0; } rc = mqtt_disconnect(&client_ctx); PRINT_RESULT("mqtt_disconnect", rc); LOG_INF("Bye!"); + + return r; } void main(void) { + int r = 0, i = 0; #if defined(CONFIG_MQTT_LIB_TLS) int rc; @@ -473,8 +480,14 @@ void main(void) PRINT_RESULT("tls_init", rc); #endif - while (1) { - publisher(); - k_sleep(K_MSEC(5000)); + while (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS || + i++ < CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) { + r = publisher(); + + if (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) { + k_sleep(K_MSEC(5000)); + } } + + exit(r); }