samples: net: mqtt_publisher: Add exit code and Kconfig options

Modify the mqtt_publisher sample app to return an exit code after
its tests have been run. Add Kconfig options to set the number of
test iterations per TCP connection as well as the number of TCP
connections to make to the server while keeping the default values
intact. Further add a config overlay file to lower the number of
TCP connections and test iterations used.

Signed-off-by: Patrik Flykt <patrik.flykt@intel.com>
This commit is contained in:
Patrik Flykt 2020-03-13 10:48:17 +02:00 committed by Jukka Rissanen
commit 984e8ac6bc
5 changed files with 54 additions and 16 deletions

View file

@ -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"

View file

@ -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

View file

@ -0,0 +1,3 @@
# Lower values used for testing
CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS=3
CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS=3

View file

@ -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"

View file

@ -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);
}