From 2a93c963455ea7b3d2b9a3df7510428ce85138d7 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Fri, 13 Nov 2020 15:37:32 +0100 Subject: [PATCH] refactor loop handling --- cube/User/Src/mqttTest.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/cube/User/Src/mqttTest.c b/cube/User/Src/mqttTest.c index ccf3f84..9555b63 100644 --- a/cube/User/Src/mqttTest.c +++ b/cube/User/Src/mqttTest.c @@ -25,11 +25,6 @@ static void mqttCallback(char *topic, uint8_t *payload, uint16_t payloadLength) logMsg("mcb: %s : %*s", topic, payloadLength, payload); } - -void mqttLoopHandler(void *handle) { - mqttLoop(&mqttClient); -} - void mqttTestHandler(void *handle) { static uint8_t state = 0; static uint8_t message[] = "Hello world\n\r"; @@ -59,33 +54,36 @@ void mqttTestHandler(void *handle) { break; case 2: - coloredMsg(LOG_YELLOW, "mth, start mqtt loop"); - schAdd(mqttLoopHandler, NULL, 0, 100); + coloredMsg(LOG_YELLOW, "mth, publish something"); + res = publish(&mqttClient, "wiznet/hello", message, strlen(message), false); + coloredMsg(LOG_YELLOW, "mth, publish returned %d", res); state = 3; break; case 3: - coloredMsg(LOG_YELLOW, "mth, publish something"); - res = publish(&mqttClient, "wiznet/hello", message, strlen(message), false); - coloredMsg(LOG_YELLOW, "mth, publish returned %d", res); + coloredMsg(LOG_YELLOW, "mth, subscribe something"); + res = subscribe(&mqttClient, "wiznet/helloback", MQTTQOS0); + coloredMsg(LOG_YELLOW, "mth, subscribe returned %d", res); state = 4; break; case 4: - coloredMsg(LOG_YELLOW, "mth, subscribe something"); - res = subscribe(&mqttClient, "wiznet/helloback", MQTTQOS0); - coloredMsg(LOG_YELLOW, "mth, subscribe returned %d", res); + coloredMsg(LOG_YELLOW, "mth, now entering the loop"); state = 5; break; case 5: - // coloredMsg(LOG_YELLOW, "mth, waiting"); + // coloredMsg(LOG_YELLOW, "mth, looping"); + if (! mqttLoop(&mqttClient)) { + state = 0; + } break; case 255: coloredMsg(LOG_YELLOW, "mth, error state, will stop here"); schDel(mqttTestHandler, NULL); - schDel(mqttLoopHandler, NULL); + coloredMsg(LOG_YELLOW, "mth, trying again in one minute"); + schAdd(mqttTestHandler, NULL, 60000, 100); break; } } else {