From b83c58763da1a7207b94ce0b4654bfa42a021663 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Mon, 25 Jan 2016 22:55:31 +0000 Subject: [PATCH] Add mosquitto_subscribe_single()/multiple(). --- ChangeLog.txt | 3 ++ lib/Makefile | 4 ++ lib/linker.version | 6 +++ lib/messages_mosq.c | 10 ++++- lib/mosquitto.h | 92 +++++++++++++++++++++++++++++++++++++++++- man/libmosquitto.3.xml | 21 ++++++++++ src/bridge.c | 2 +- 7 files changed, 135 insertions(+), 3 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index a3cf20b2..600e6194 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -30,6 +30,9 @@ Client library: - Outgoing messages with QoS>1 are no longer retried after a timeout period. Messages will be retried when a client reconnects. - DNS-SRV support is now disabled by default. +- Add mosquitto_subscribe_single() and mosquitto_subscribe_multiple(). These + are two helper functions to make retrieving messages from a broker very + straightforward. Client: - Add -x to mosquitto_sub for printing the payload in hexadecimal format. diff --git a/lib/Makefile b/lib/Makefile index 9d3722eb..cd599fdd 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -3,6 +3,7 @@ include ../config.mk .PHONY : really clean install MOSQ_OBJS=mosquitto.o \ + helpers.o \ logging_mosq.o \ memory_mosq.o \ messages_mosq.o \ @@ -51,6 +52,9 @@ libmosquitto.a : ${MOSQ_OBJS} mosquitto.o : mosquitto.c mosquitto.h ${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@ +helpers.o : helpers.c + ${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@ + logging_mosq.o : logging_mosq.c logging_mosq.h ${CROSS_COMPILE}$(CC) $(LIB_CFLAGS) -c $< -o $@ diff --git a/lib/linker.version b/lib/linker.version index 59eed794..d04984d6 100644 --- a/lib/linker.version +++ b/lib/linker.version @@ -78,3 +78,9 @@ MOSQ_1.4 { mosquitto_sub_topic_check; mosquitto_socks5_set; } MOSQ_1.3; + +MOSQ_1.5 { + global: + mosquitto_subscribe_simple; + mosquitto_subscribe_multiple; +} MOSQ_1.4; diff --git a/lib/messages_mosq.c b/lib/messages_mosq.c index 38bdaea9..208e73b7 100644 --- a/lib/messages_mosq.c +++ b/lib/messages_mosq.c @@ -66,7 +66,7 @@ int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto dst->qos = src->qos; dst->retain = src->retain; if(src->payloadlen){ - dst->payload = mosquitto__malloc(src->payloadlen); + dst->payload = mosquitto__calloc(src->payloadlen+1, sizeof(uint8_t)); if(!dst->payload){ mosquitto__free(dst->topic); return MOSQ_ERR_NOMEM; @@ -106,6 +106,14 @@ void mosquitto_message_free(struct mosquitto_message **message) mosquitto__free(msg); } +void mosquitto_message_free_contents(struct mosquitto_message *message) +{ + if(!message) return; + + mosquitto__free(message->topic); + mosquitto__free(message->payload); +} + int message__queue(struct mosquitto *mosq, struct mosquitto_message_all *message, enum mosquitto_msg_direction dir) { int rc = 0; diff --git a/lib/mosquitto.h b/lib/mosquitto.h index 4be9d5f5..e7798861 100644 --- a/lib/mosquitto.h +++ b/lib/mosquitto.h @@ -672,10 +672,23 @@ libmosq_EXPORT int mosquitto_message_copy(struct mosquitto_message *dst, const s * message - pointer to a mosquitto_message pointer to free. * * See Also: - * + * , */ libmosq_EXPORT void mosquitto_message_free(struct mosquitto_message **message); +/* + * Function: mosquitto_message_free_contents + * + * Free a mosquitto_message struct contents, leaving the struct unaffected. + * + * Parameters: + * message - pointer to a mosquitto_message struct to free its contents. + * + * See Also: + * , + */ +libmosq_EXPORT void mosquitto_message_free_contents(struct mosquitto_message *message); + /* * Function: mosquitto_loop * @@ -1523,6 +1536,83 @@ libmosq_EXPORT int mosquitto_pub_topic_check(const char *topic); */ libmosq_EXPORT int mosquitto_sub_topic_check(const char *topic); + +struct libmosquitto_will { + char *topic; + void *payload; + int payloadlen; + int qos; + bool retain; +}; + +struct libmosquitto_auth { + char *username; + char *password; +}; + +struct libmosquitto_tls { + char *cafile; + char *capath; + char *certfile; + char *keyfile; + char *ciphers; + char *tls_version; + int (*pw_callback)(char *buf, int size, int rwflag, void *userdata); + int cert_reqs; +}; + +/* + * Function: mosquitto_subscribe_simple + * + * Helper function to make subscribing to a topic and retrieving some messages + * very straightforward. + * + * This connects to a broker, subscribes to a topic, waits for msg_count + * messages to be received, then returns after disconnecting cleanly. + * + * Parameters: + * messages - pointer to a "struct mosquitto_message *". The received + * messages will be returned here. On error, this will be set to + * NULL. + * msg_count - the number of messages to retrieve. + * topic - the subscription topic to use (wildcards are allowed). + * qos - the qos to use for the subscription. + * retained - if set to true, stale retained messages will be treated as + * normal messages with regards to msg_count. If set to false, + * they will be ignored. + * host - the broker to connect to. + * port - the network port the broker is listening on. + * client_id - the client id to use, or NULL if a random client id should be + * generated. + * keepalive - the MQTT keepalive value. + * clean_session - the MQTT clean session flag. + * username - the username string, or NULL for no username authentication. + * password - the password string, or NULL for an empty password. + * will - a libmosquitto_will struct containing will information, or NULL for + * no will. + * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL + * for no use of TLS. + * + * + * Returns: + * MOSQ_ERR_SUCCESS - on success + * >0 - on error. + */ +libmosq_EXPORT int mosquitto_subscribe_simple( + struct mosquitto_message **messages, + int msg_count, + const char *topic, + int qos, + bool retained, + const char *host, + int port, + const char *client_id, + int keepalive, + bool clean_session, + const char *username, + const char *password, + const struct libmosquitto_will *will, + const struct libmosquitto_tls *tls); #ifdef __cplusplus } #endif diff --git a/man/libmosquitto.3.xml b/man/libmosquitto.3.xml index ed122283..e051069c 100644 --- a/man/libmosquitto.3.xml +++ b/man/libmosquitto.3.xml @@ -368,6 +368,27 @@ bool *result + + + Helper functions + + int mosquitto_subscribe_simple + struct mosquitto_message **message + int msg_count + const char *topic + const char *qos + bool retained + const char *host + int port + const char *client_id + int keepalive + bool clean_session + const char *username + const char *password + const struct libmosquitto_will *will + const struct libmosquitto_tls *tls + + diff --git a/src/bridge.c b/src/bridge.c index c3787e6e..4cb92ce5 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -180,7 +180,7 @@ int bridge__connect(struct mosquitto_db *db, struct mosquitto *context) rc = net__socket_connect(context, context->bridge->addresses[context->bridge->cur_address].address, context->bridge->addresses[context->bridge->cur_address].port, NULL, false); if(rc > 0 ){ if(rc == MOSQ_ERR_TLS){ - _mosquitto_socket_close(db, context); + net__socket_close(db, context); return rc; /* Error already printed */ }else if(rc == MOSQ_ERR_ERRNO){ log__printf(NULL, MOSQ_LOG_ERR, "Error creating bridge: %s.", strerror(errno));