diff --git a/apps/mosquitto_ctrl/ctrl_shell.c b/apps/mosquitto_ctrl/ctrl_shell.c index 5b888d73..7b9e3371 100644 --- a/apps/mosquitto_ctrl/ctrl_shell.c +++ b/apps/mosquitto_ctrl/ctrl_shell.c @@ -139,16 +139,22 @@ bool ctrl_shell_get_password(char *buf, size_t len) return true; } + static int response_wait(bool unlock) { struct timespec timeout; int rc = 0; + data.response_received = false; + clock_gettime(CLOCK_REALTIME, &timeout); timeout.tv_sec += 2; - if(pthread_cond_timedwait(&data.response_cond, &data.response_mutex, &timeout) == ETIMEDOUT){ - ctrl_shell_printf("Timed out with no response.\n"); - rc = 1; + while(data.response_received == false){ + if(pthread_cond_timedwait(&data.response_cond, &data.response_mutex, &timeout) == ETIMEDOUT){ + ctrl_shell_printf("Timed out with no response.\n"); + rc = 1; + break; + } } if(unlock){ pthread_mutex_unlock(&data.response_mutex); @@ -291,14 +297,7 @@ static int ctrl_shell__subscribe_blocking(const char *topic, void (*module_on_su mosquitto_subscribe(data.mosq, NULL, topic, 1); - struct timespec timeout; - clock_gettime(CLOCK_REALTIME, &timeout); - timeout.tv_sec += 2; - if(pthread_cond_timedwait(&data.response_cond, &data.response_mutex, &timeout) == ETIMEDOUT){ - ctrl_shell_printf("Subscribe timed out with no response.\n"); - rc = 1; - } - pthread_mutex_unlock(&data.response_mutex); + response_wait(true); if(data.subscribe_rc >= 128){ rc = 1; @@ -493,6 +492,7 @@ void ctrl_shell__on_connect(struct mosquitto *mosq, void *userdata, int rc) data.connect_rc = rc; + data.response_received = true; pthread_mutex_unlock(&data.response_mutex); pthread_cond_signal(&data.response_cond); } @@ -519,6 +519,7 @@ void ctrl_shell__on_message(struct mosquitto *mosq, void *userdata, const struct } cJSON_Delete(j_tree); + data.response_received = true; pthread_mutex_unlock(&data.response_mutex); pthread_cond_signal(&data.response_cond); } @@ -535,6 +536,7 @@ void ctrl_shell__on_publish(struct mosquitto *mosq, void *userdata, int mid, int data.publish_rc = reason_code; } + data.response_received = true; pthread_mutex_unlock(&data.response_mutex); pthread_cond_signal(&data.response_cond); } @@ -550,6 +552,7 @@ void ctrl_shell__on_subscribe(struct mosquitto *mosq, void *userdata, int mid, i data.subscribe_rc = granted_qos[0]; } + data.response_received = true; pthread_mutex_unlock(&data.response_mutex); pthread_cond_signal(&data.response_cond); } diff --git a/apps/mosquitto_ctrl/ctrl_shell_internal.h b/apps/mosquitto_ctrl/ctrl_shell_internal.h index e28d9b36..c59d1a1a 100644 --- a/apps/mosquitto_ctrl/ctrl_shell_internal.h +++ b/apps/mosquitto_ctrl/ctrl_shell_internal.h @@ -38,6 +38,7 @@ struct ctrl_shell{ rl_vcpfunc_t *line_callback; pthread_cond_t response_cond; pthread_mutex_t response_mutex; + bool response_received; const char *request_topic; struct completion_tree_root *commands; void (*response_callback)(const char *command, cJSON *data, const char *payload);