diff --git a/ChangeLog.txt b/ChangeLog.txt index db3de63c..30260f4a 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -94,6 +94,9 @@ Client library features: - Add mosquitto_pub_topic_check2(), mosquitto_sub_topic_check2(), and mosquitto_topic_matches_sub2() which are identical to the similarly named functions but also take length arguments. +- Add mosquitto_connect_with_flags_callback_set(), which allows a second + connect callback to be used which also exposes the connect flags parameter. + Closes #738 and #128. Client library fixes: - Fix incorrect PSK key being used if it had leading zeroes. diff --git a/client/sub_client.c b/client/sub_client.c index 0ac0c8ce..a77440e3 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -73,7 +73,7 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit } } -void my_connect_callback(struct mosquitto *mosq, void *obj, int result) +void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags) { int i; struct mosq_config *cfg; @@ -261,7 +261,7 @@ int main(int argc, char *argv[]) mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); } - mosquitto_connect_callback_set(mosq, my_connect_callback); + mosquitto_connect_with_flags_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); rc = client_connect(mosq, &cfg); diff --git a/lib/handle_connack.c b/lib/handle_connack.c index e05ec4fa..5e05e918 100644 --- a/lib/handle_connack.c +++ b/lib/handle_connack.c @@ -26,12 +26,12 @@ Contributors: int handle__connack(struct mosquitto *mosq) { - uint8_t byte; + uint8_t connect_flags; uint8_t result; int rc; assert(mosq); - rc = packet__read_byte(&mosq->in_packet, &byte); // Reserved byte, not used + rc = packet__read_byte(&mosq->in_packet, &connect_flags); if(rc) return rc; rc = packet__read_byte(&mosq->in_packet, &result); if(rc) return rc; @@ -42,6 +42,11 @@ int handle__connack(struct mosquitto *mosq) mosq->on_connect(mosq, mosq->userdata, result); mosq->in_callback = false; } + if(mosq->on_connect_with_flags){ + mosq->in_callback = true; + mosq->on_connect_with_flags(mosq, mosq->userdata, result, connect_flags); + mosq->in_callback = false; + } pthread_mutex_unlock(&mosq->callback_mutex); switch(result){ case 0: diff --git a/lib/linker.version b/lib/linker.version index 3020d714..c592d17e 100644 --- a/lib/linker.version +++ b/lib/linker.version @@ -89,4 +89,5 @@ MOSQ_1.5 { mosquitto_pub_topic_check2; mosquitto_sub_topic_check2; mosquitto_topic_matches_sub2; + mosquitto_connect_with_flags_callback_set; } MOSQ_1.4; diff --git a/lib/mosquitto.c b/lib/mosquitto.c index 56a4fb1d..5954bc67 100644 --- a/lib/mosquitto.c +++ b/lib/mosquitto.c @@ -1225,6 +1225,13 @@ void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(s pthread_mutex_unlock(&mosq->callback_mutex); } +void mosquitto_connect_with_flags_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int)) +{ + pthread_mutex_lock(&mosq->callback_mutex); + mosq->on_connect_with_flags = on_connect; + pthread_mutex_unlock(&mosq->callback_mutex); +} + void mosquitto_disconnect_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int)) { pthread_mutex_lock(&mosq->callback_mutex); diff --git a/lib/mosquitto.h b/lib/mosquitto.h index 96b88133..e4691fec 100644 --- a/lib/mosquitto.h +++ b/lib/mosquitto.h @@ -1133,6 +1133,31 @@ libmosq_EXPORT int mosquitto_tls_psk_set(struct mosquitto *mosq, const char *psk */ libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int)); +/* + * Function: mosquitto_connect_with_flags_callback_set + * + * Set the connect callback. This is called when the broker sends a CONNACK + * message in response to a connection. + * + * Parameters: + * mosq - a valid mosquitto instance. + * on_connect - a callback function in the following form: + * void callback(struct mosquitto *mosq, void *obj, int rc) + * + * Callback Parameters: + * mosq - the mosquitto instance making the callback. + * obj - the user data provided in + * rc - the return code of the connection response, one of: + * flags - the connect flags. + * + * * 0 - success + * * 1 - connection refused (unacceptable protocol version) + * * 2 - connection refused (identifier rejected) + * * 3 - connection refused (broker unavailable) + * * 4-255 - reserved for future use + */ +libmosq_EXPORT void mosquitto_connect_with_flags_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int)); + /* * Function: mosquitto_disconnect_callback_set * diff --git a/lib/mosquitto_internal.h b/lib/mosquitto_internal.h index 254b4231..e28721ac 100644 --- a/lib/mosquitto_internal.h +++ b/lib/mosquitto_internal.h @@ -246,6 +246,7 @@ struct mosquitto { struct mosquitto_message_all *out_messages; struct mosquitto_message_all *out_messages_last; void (*on_connect)(struct mosquitto *, void *userdata, int rc); + void (*on_connect_with_flags)(struct mosquitto *, void *userdata, int rc, int flags); void (*on_disconnect)(struct mosquitto *, void *userdata, int rc); void (*on_publish)(struct mosquitto *, void *userdata, int mid); void (*on_message)(struct mosquitto *, void *userdata, const struct mosquitto_message *message);