-
Notifications
You must be signed in to change notification settings - Fork 279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to check the client has received a Connack from the Broker after connect() #163
Comments
Hi @schnedann I have the same problem, for example when wrong MQTT password is used then mqtt_connect() will return MQTT_OK but only a while later will the MQTT_ERROR_CONNECTION_REFUSED be returned. I can only offer a hint / workaround. Inspect __mqtt_recv() and search for MQTT_CONTROL_CONNACK. You will see that client->typical_response_time is updated. You can check if this field has been updated to detect the connected state. I think a better way would be to modify struct mqtt_client and add a state field that gets updated when MQTT_CONTROL_CONNACK is received. All of the other error cases would also need to be catered for. Best regards, |
Hi, thx for the reply. In fact, this is what I did for serving our needs. Its still not clear if I can provide these changes upstream... |
+1 on having a better way to detect CONNACK has been received. |
Here's my quick fix. I added a bool flag in struct mqtt_client: struct mqtt_client {
...
/** @brief Flag is set on connection event */
bool event_connect;
}; I set the flag in __mqtt_recv(): switch (response.fixed_header.control_type) {
case MQTT_CONTROL_CONNACK:
/* release associated CONNECT */
msg = mqtt_mq_find(&client->mq, MQTT_CONTROL_CONNECT, NULL);
if (msg == NULL) {
client->error = MQTT_ERROR_ACK_OF_UNKNOWN;
mqtt_recv_ret = MQTT_ERROR_ACK_OF_UNKNOWN;
break;
}
msg->state = MQTT_QUEUED_COMPLETE;
lat_log(LAT_VER, "Release msg type %u", response.fixed_header.control_type);
/* initialize typical response time */
client->typical_response_time = (double) (MQTT_PAL_TIME() - msg->time_sent);
client->last_response_time = (double) (MQTT_PAL_TIME() - msg->time_sent);
client->min_response_time = (double) (MQTT_PAL_TIME() - msg->time_sent);
client->max_response_time = (double) (MQTT_PAL_TIME() - msg->time_sent);
/* check that connection was successful */
if (response.decoded.connack.return_code != MQTT_CONNACK_ACCEPTED) {
if (response.decoded.connack.return_code == MQTT_CONNACK_REFUSED_IDENTIFIER_REJECTED) {
client->error = MQTT_ERROR_CONNECT_CLIENT_ID_REFUSED;
mqtt_recv_ret = MQTT_ERROR_CONNECT_CLIENT_ID_REFUSED;
} else {
client->error = MQTT_ERROR_CONNECTION_REFUSED;
mqtt_recv_ret = MQTT_ERROR_CONNECTION_REFUSED;
}
break;
}
else {
client->event_connect = true;
}
break; Remember to clear the flag in mqtt_init(), mqtt_init_reconnect() and mqtt_reinit() |
Hi @pieterconradie I think your solution is more elegant than mine:
MQTT-C has an example usage for NuttX RTOS, I will submit a patch to them based on your solution, ok? |
Hi @acassis You are most welcome. Go ahead. Best regards, Pieter |
@pieterconradie finally I got time to submit the PR to NuttX mainline: Thank you very much! |
This is a fix implemented by Pieter Conradie as described here: LiamBindle/MQTT-C#163
after reading the API Docu and reading the code,
I still have no clue how to check for an successful connect,
so how one can check if the broker sent a Connack, and the
Client is in a Connected state prior to executing a publish?
The text was updated successfully, but these errors were encountered: