Add mosquitto_subscribe_multiple.

pull/937/merge
Roger A. Light 7 years ago
parent ebf4d3a920
commit cf9a72d8db

@ -105,7 +105,24 @@ int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int q
if(mosquitto_sub_topic_check(sub)) return MOSQ_ERR_INVAL;
if(mosquitto_validate_utf8(sub, strlen(sub))) return MOSQ_ERR_MALFORMED_UTF8;
return send__subscribe(mosq, mid, sub, qos);
return send__subscribe(mosq, mid, 1, &sub, qos);
}
int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count, const char **sub, int qos)
{
int i;
if(!mosq || !sub_count || !sub) return MOSQ_ERR_INVAL;
if(qos < 0 || qos > 2) return MOSQ_ERR_INVAL;
if(mosq->sock == INVALID_SOCKET) return MOSQ_ERR_NO_CONN;
for(i=0; i<sub_count; i++){
if(mosquitto_sub_topic_check(sub[i])) return MOSQ_ERR_INVAL;
if(mosquitto_validate_utf8(sub[i], strlen(sub[i]))) return MOSQ_ERR_MALFORMED_UTF8;
}
return send__subscribe(mosq, mid, sub_count, sub, qos);
}

@ -91,3 +91,8 @@ MOSQ_1.5 {
mosquitto_topic_matches_sub2;
mosquitto_connect_with_flags_callback_set;
} MOSQ_1.4;
MOSQ_1.6 {
global:
mosquitto_subscribe_multiple;
} MOSQ_1.5;

@ -630,6 +630,30 @@ libmosq_EXPORT int mosquitto_publish(struct mosquitto *mosq, int *mid, const cha
*/
libmosq_EXPORT int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int qos);
/*
* Function: mosquitto_subscribe_multiple
*
* Subscribe to multiple topics.
*
* Parameters:
* mosq - a valid mosquitto instance.
* mid - a pointer to an int. If not NULL, the function will set this to
* the message id of this particular message. This can be then used
* with the subscribe callback to determine when the message has been
* sent.
* sub_count - the count of subscriptions to be made
* sub - array of sub_count pointers, each pointing to a subscription string.
* qos - the requested Quality of Service for each subscription.
*
* Returns:
* MOSQ_ERR_SUCCESS - on success.
* MOSQ_ERR_INVAL - if the input parameters were invalid.
* MOSQ_ERR_NOMEM - if an out of memory condition occurred.
* MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
* MOSQ_ERR_MALFORMED_UTF8 - if a topic is not valid UTF-8
*/
int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count, const char **sub, int qos);
/*
* Function: mosquitto_unsubscribe
*

@ -31,7 +31,7 @@ int send__pubcomp(struct mosquitto *mosq, uint16_t mid);
int send__publish(struct mosquitto *mosq, uint16_t mid, const char *topic, uint32_t payloadlen, const void *payload, int qos, bool retain, bool dup);
int send__pubrec(struct mosquitto *mosq, uint16_t mid);
int send__pubrel(struct mosquitto *mosq, uint16_t mid);
int send__subscribe(struct mosquitto *mosq, int *mid, const char *topic, uint8_t topic_qos);
int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, const char **topic, int topic_qos);
int send__unsubscribe(struct mosquitto *mosq, int *mid, const char *topic);
#endif

@ -30,13 +30,13 @@ Contributors:
#include "util_mosq.h"
int send__subscribe(struct mosquitto *mosq, int *mid, const char *topic, uint8_t topic_qos)
int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, const char **topic, int topic_qos)
{
/* FIXME - only deals with a single topic */
struct mosquitto__packet *packet = NULL;
uint32_t packetlen;
uint16_t local_mid;
int rc;
int i;
assert(mosq);
assert(topic);
@ -44,7 +44,10 @@ int send__subscribe(struct mosquitto *mosq, int *mid, const char *topic, uint8_t
packet = mosquitto__calloc(1, sizeof(struct mosquitto__packet));
if(!packet) return MOSQ_ERR_NOMEM;
packetlen = 2 + 2+strlen(topic) + 1;
packetlen = 2;
for(i=0; i<topic_count; i++){
packetlen += 2+strlen(topic[i]) + 1;
}
packet->command = SUBSCRIBE | (1<<1);
packet->remaining_length = packetlen;
@ -60,15 +63,19 @@ int send__subscribe(struct mosquitto *mosq, int *mid, const char *topic, uint8_t
packet__write_uint16(packet, local_mid);
/* Payload */
packet__write_string(packet, topic, strlen(topic));
packet__write_byte(packet, topic_qos);
for(i=0; i<topic_count; i++){
packet__write_string(packet, topic[i], strlen(topic[i]));
packet__write_byte(packet, topic_qos);
}
#ifdef WITH_BROKER
# ifdef WITH_BRIDGE
log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d)", mosq->id, local_mid, topic, topic_qos);
log__printf(mosq, MOSQ_LOG_DEBUG, "Bridge %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d)", mosq->id, local_mid, topic[0], topic_qos);
# endif
#else
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d)", mosq->id, local_mid, topic, topic_qos);
for(i=0; i<topic_count; i++){
log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s sending SUBSCRIBE (Mid: %d, Topic: %s, QoS: %d)", mosq->id, local_mid, topic[i], topic_qos);
}
#endif
return packet__queue(mosq, packet);

@ -75,7 +75,7 @@ int handle__connack(struct mosquitto_db *db, struct mosquitto *context)
}
for(i=0; i<context->bridge->topic_count; i++){
if(context->bridge->topics[i].direction == bd_in || context->bridge->topics[i].direction == bd_both){
if(send__subscribe(context, NULL, context->bridge->topics[i].remote_topic, context->bridge->topics[i].qos)){
if(send__subscribe(context, NULL, 1, &context->bridge->topics[i].remote_topic, &context->bridge->topics[i].qos)){
return 1;
}
}else{

Loading…
Cancel
Save