From 5476e0b365ac05c5355e10558fb2d2b30f7be179 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Mon, 27 Jan 2020 12:46:53 -0800 Subject: [PATCH] samples: net: mqtt_publisher: add return value to wait() In order to better handle incoming data, wait() should return the # of sockets with data returned by poll(). Based on this new return value, we can call mqtt_input() in a smarter way. Signed-off-by: Michael Scott --- samples/net/mqtt_publisher/src/main.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/samples/net/mqtt_publisher/src/main.c b/samples/net/mqtt_publisher/src/main.c index 5357b1a3844..7bd5ff21283 100644 --- a/samples/net/mqtt_publisher/src/main.c +++ b/samples/net/mqtt_publisher/src/main.c @@ -113,13 +113,18 @@ static void clear_fds(void) nfds = 0; } -static void wait(int timeout) +static int wait(int timeout) { + int ret = 0; + if (nfds > 0) { - if (poll(fds, nfds, timeout) < 0) { + ret = poll(fds, nfds, timeout); + if (ret < 0) { LOG_ERR("poll error: %d", errno); } } + + return ret; } void mqtt_evt_handler(struct mqtt_client *const client, @@ -357,8 +362,9 @@ static int try_to_connect(struct mqtt_client *client) prepare_fds(client); - wait(APP_SLEEP_MSECS); - mqtt_input(client); + if (wait(APP_SLEEP_MSECS)) { + mqtt_input(client); + } if (!connected) { mqtt_abort(client); @@ -379,7 +385,13 @@ static int process_mqtt_and_sleep(struct mqtt_client *client, int timeout) int rc; while (remaining > 0 && connected) { - wait(remaining); + if (wait(remaining)) { + rc = mqtt_input(client); + if (rc != 0) { + PRINT_RESULT("mqtt_input", rc); + return rc; + } + } rc = mqtt_live(client); if (rc != 0 && rc != -EAGAIN) {