Comments (7)
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,
Pieter
https://piconomix.com
from mqtt-c.
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.
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...
from mqtt-c.
+1 on having a better way to detect CONNACK has been received.
from mqtt-c.
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()
from mqtt-c.
Hi @pieterconradie I think your solution is more elegant than mine:
--- a/include/mqtt.h 2021-03-29 14:53:52.000000000 -0300
+++ b/include/mqtt.h 2022-11-10 20:24:11.581983160 -0300
@@ -234,7 +234,8 @@
enum MQTTErrors {
MQTT_ERROR_UNKNOWN=INT_MIN,
__ALL_MQTT_ERRORS(GENERATE_ENUM)
- MQTT_OK = 1
+ MQTT_OK = 1,
+ MQTT_ACK
};
/**
diff -Naur a/src/mqtt.c b/src/mqtt.c
--- a/src/mqtt.c 2021-03-29 14:53:52.000000000 -0300
+++ b/src/mqtt.c 2022-11-10 20:23:41.997057844 -0300
@@ -723,7 +723,11 @@
/* initialize typical response time */
client->typical_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_ACCEPTED) {
+ client->error = MQTT_ACK;
+ }
+ else {
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;
MQTT-C has an example usage for NuttX RTOS, I will submit a patch to them based on your solution, ok?
from mqtt-c.
Hi @acassis You are most welcome. Go ahead. Best regards, Pieter
from mqtt-c.
@pieterconradie finally I got time to submit the PR to NuttX mainline:
apache/nuttx-apps@e90960d
Thank you very much!
from mqtt-c.
Related Issues (20)
- Possible undefined behaviour/bad memory access after reconnect HOT 2
- MQTT_ERROR_SEND_BUFFER_IS_FULL due to transient MQTT_ERROR_SOCKET_ERROR
- FPU-Trap when calculating client->typical_response_time
- mqtt_publish return value causes mqtt_error_str when not connected
- mqtt_publish seems to only send QoS 0 HOT 1
- Possible bug in MQTT_CLIENT_TRY_PACK macro
- MQTT-C Security Issue Report (mqtt_unpack_publish_response) HOT 2
- swap use of double variables to float HOT 1
- how to check network connection state in inspector callback
- Signed integer overflow in `mqtt_error_str()` HOT 2
- We cannot pass the context of the program to mqtt publish_callback HOT 2
- Mingw compile error
- Memory alignment in Keil ARM Compiler (thumb instruction set)
- mqtt_connect does not generate a client.error if wrong username and/or password is provided HOT 2
- Unable to connect openssl_publisher example to HiveMQ HOT 4
- Windows MSYS2 MINGW64: Examples fail with "MQTT_ERROR_SOCKET_ERROR" or "Failed to open socket: : No error"
- MQTT-C endless loop if sendbuffer is too large
- Can't connect to EMQX Broker Free
- `mqtt_connect` with Unvalidated SSL-connection returns `MQTT_ERROR_SOCKET_ERROR`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqtt-c.