mqtt_client_t *mqtt_client = (mqtt_client_t *)data;
if (mqtt_client) {
- mqtt_client->state = MQTT_CLIENT_STATE_CONNECTED;
if (mqtt_client->config && mqtt_client->config->on_publish) {
mqtt_client->config->on_publish(mqtt_client, msg_id);
}
mqtt_client_t *mqtt_client = (mqtt_client_t *)data;
if (mqtt_client) {
- mqtt_client->state = MQTT_CLIENT_STATE_CONNECTED;
if (mqtt_client->config && mqtt_client->config->on_subscribe) {
mqtt_client->config->on_subscribe(mqtt_client, msg_id, qos_count, granted_qos);
}
mqtt_client_t *mqtt_client = (mqtt_client_t *)data;
if (mqtt_client) {
- mqtt_client->state = MQTT_CLIENT_STATE_CONNECTED;
if (mqtt_client->config && mqtt_client->config->on_unsubscribe) {
mqtt_client->config->on_unsubscribe(mqtt_client, msg_id);
}
case MQTT_CLIENT_STATE_CONNECT_REQUEST:
snprintf(result_str, 20, "CONNECT_REQUEST");
break;
- case MQTT_CLIENT_STATE_SUBSCRIBE_REQUEST:
- snprintf(result_str, 20, "SUBSCRIBE_REQUEST");
- break;
- case MQTT_CLIENT_STATE_UNSUBSCRIBE_REQUEST:
- snprintf(result_str, 20, "UNSUBSCRIBE_REQUEST");
- break;
- case MQTT_CLIENT_STATE_PUBLISH_REQUEST:
- snprintf(result_str, 20, "PUBLISH_REQUEST");
- break;
case MQTT_CLIENT_STATE_DISCONNECT_REQUEST:
snprintf(result_str, 20, "DISCONNECT_REQUEST");
break;
int ret = 0;
struct mosquitto *mosq = NULL;
mqtt_client_config_t *mqtt_config = NULL;
+ int prev_state = 0;
if (handle == NULL) {
ndbg("ERROR: mqtt_client handle is null.\n");
port = MQTT_DEFAULT_BROKER_PORT;
}
+ prev_state = handle->state;
handle->state = MQTT_CLIENT_STATE_CONNECT_REQUEST;
ret = mosquitto_connect(mosq, addr, port, keep_alive);
if (ret != 0) {
ndbg("ERROR: mosquitto_connect() failed. (ret: %d)\n", ret);
- handle->state = MQTT_CLIENT_STATE_NOT_CONNECTED;
+ handle->state = prev_state;
goto done;
}
int result = -1;
int ret = 0;
struct mosquitto *mosq = NULL;
+ int prev_state = 0;
if (handle == NULL) {
ndbg("ERROR: mqtt_client handle is null.\n");
goto done;
}
- if (handle->state > MQTT_CLIENT_STATE_CONNECTED) {
+ if (handle->state == MQTT_CLIENT_STATE_DISCONNECT_REQUEST) {
char state_str[20];
get_mqtt_client_state_string(handle->state, state_str);
ndbg("ERROR: mqtt_client is busy. (current state: %s)\n", state_str);
goto done;
}
+ prev_state = handle->state;
handle->state = MQTT_CLIENT_STATE_DISCONNECT_REQUEST;
ret = mosquitto_disconnect(mosq);
if (ret != 0) {
ndbg("ERROR: mosquitto_disconnect() failed.\n");
- handle->state = MQTT_CLIENT_STATE_CONNECTED;
+ handle->state = prev_state;
goto done;
}
goto done;
}
- handle->state = MQTT_CLIENT_STATE_PUBLISH_REQUEST;
ret = mosquitto_publish(mosq, NULL, (const char *)topic, data_len, data, qos, retain != 0 ? true : false);
if (ret != 0) {
ndbg("ERROR: mosquitto_publish() failed. (ret: %d)\n", ret);
goto done;
}
- handle->state = MQTT_CLIENT_STATE_SUBSCRIBE_REQUEST;
ret = mosquitto_subscribe(mosq, NULL, (const char *)topic, qos);
if (ret != 0) {
ndbg("ERROR: mqtt_subscribe() failed. (ret: %d)\n", ret);
goto done;
}
- handle->state = MQTT_CLIENT_STATE_UNSUBSCRIBE_REQUEST;
ret = mosquitto_unsubscribe(mosq, NULL, (const char *)topic);
if (ret != 0) {
ndbg("ERROR: mosquitto_unsubscribe() failed. (ret: %d)\n");