shell: mqtt: cancel disconnect work on connect event
If the disconnect work has not yet run when a connect event is received the
connect event is dropped.
Prevent this behaviour by reorganizing `network_evt_handler()`.
Signed-off-by: Jeppe Odgaard <jeppe.odgaard@prevas.dk>
diff --git a/subsys/shell/backends/shell_mqtt.c b/subsys/shell/backends/shell_mqtt.c
index 5b9b452..211989f 100644
--- a/subsys/shell/backends/shell_mqtt.c
+++ b/subsys/shell/backends/shell_mqtt.c
@@ -469,13 +469,12 @@
sh_mqtt_context_unlock();
}
-static void cancel_dworks_and_cleanup(struct shell_mqtt *sh)
+static void cancel_dworks(struct shell_mqtt *sh)
{
(void)k_work_cancel_delayable(&sh->connect_dwork);
(void)k_work_cancel_delayable(&sh->subscribe_dwork);
(void)k_work_cancel_delayable(&sh->process_dwork);
(void)k_work_cancel_delayable(&sh->publish_dwork);
- sh_mqtt_close_and_cleanup(sh);
}
static void net_disconnect_handler(struct k_work *work)
@@ -484,11 +483,10 @@
struct shell_mqtt *sh = sh_mqtt;
LOG_WRN("Network %s", "disconnected");
- sh->network_state = SHELL_MQTT_NETWORK_DISCONNECTED;
/* Stop all possible work */
(void)sh_mqtt_context_lock(K_FOREVER);
- cancel_dworks_and_cleanup(sh);
+ sh_mqtt_close_and_cleanup(sh);
sh_mqtt_context_unlock();
/* If the transport was requested, the connect work will be rescheduled
* when internet is connected again
@@ -501,13 +499,17 @@
{
struct shell_mqtt *sh = sh_mqtt;
- if ((mgmt_event == NET_EVENT_L4_CONNECTED) &&
- (sh->network_state == SHELL_MQTT_NETWORK_DISCONNECTED)) {
- LOG_WRN("Network %s", "connected");
- sh->network_state = SHELL_MQTT_NETWORK_CONNECTED;
- (void)sh_mqtt_work_reschedule(&sh->connect_dwork, PROCESS_INTERVAL);
+ if (mgmt_event == NET_EVENT_L4_CONNECTED) {
+ (void)k_work_cancel(&sh->net_disconnected_work);
+ if (sh->network_state == SHELL_MQTT_NETWORK_DISCONNECTED) {
+ LOG_WRN("Network %s", "connected");
+ sh->network_state = SHELL_MQTT_NETWORK_CONNECTED;
+ (void)sh_mqtt_work_reschedule(&sh->connect_dwork, PROCESS_INTERVAL);
+ }
} else if ((mgmt_event == NET_EVENT_L4_DISCONNECTED) &&
(sh->network_state == SHELL_MQTT_NETWORK_CONNECTED)) {
+ sh->network_state = SHELL_MQTT_NETWORK_DISCONNECTED;
+ cancel_dworks(sh);
(void)sh_mqtt_work_submit(&sh->net_disconnected_work);
}
}