diff --git a/README.md b/README.md index 023e70a..fb305c8 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,15 @@ bool subscribe(const char topic[], int qos); - The functions return a boolean that indicates if the subscription has been successful (true). +Subscribe to multiple topics: + +```c++ +bool subscribeMultiple(int count, const char *topics[], int qos_levels[]); +``` + +- The functions allow subscribing to multiple topics at once. +- The functions return a boolean that indicates if the subscription has been successful (true). + Unsubscribe from a topic: ```c++ @@ -253,6 +262,15 @@ bool unsubscribe(const char topic[]); - The functions return a boolean that indicates if the unsubscription has been successful (true). +Unsubscribe from multiple topics: + +```c++ +bool unsubscribeMultiple(int count, const char *topics[]); +``` + +- The functions allow unsubscribing from multiple topics at once. +- The functions return a boolean that indicates if the unsubscription has been successful (true). + Sends and receives packets: ```c++ diff --git a/src/MQTTClient.cpp b/src/MQTTClient.cpp index 5925ec2..627fcf9 100644 --- a/src/MQTTClient.cpp +++ b/src/MQTTClient.cpp @@ -441,6 +441,30 @@ bool MQTTClient::subscribe(const char topic[], int qos) { return true; } +bool MQTTClient::subscribeMultiple(int count, const char *topics[], int qos_levels[]) { + // return immediately if not connected + if (!this->connected()) { + return false; + } + + lwmqtt_string_t topic_filters[count]; + lwmqtt_qos_t qos[count]; + + for (int i = 0; i < count; i++) { + topic_filters[i] = lwmqtt_string(topics[i]); + qos[i] = (lwmqtt_qos_t)qos_levels[i]; + } + + this->_lastError = lwmqtt_subscribe(&this->client, count, topic_filters, qos, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + // close connection + this->close(); + return false; + } + + return true; +} + bool MQTTClient::unsubscribe(const char topic[]) { // return immediately if not connected if (!this->connected()) { @@ -459,6 +483,28 @@ bool MQTTClient::unsubscribe(const char topic[]) { return true; } +bool MQTTClient::unsubscribeMultiple(int count, const char *topics[]) { + // return immediately if not connected + if (!this->connected()) { + return false; + } + + lwmqtt_string_t topic_filters[count]; + + for (int i = 0; i < count; i++) { + topic_filters[i] = lwmqtt_string(topics[i]); + } + + this->_lastError = lwmqtt_unsubscribe(&this->client, count, topic_filters, this->timeout); + if (this->_lastError != LWMQTT_SUCCESS) { + // close connection + this->close(); + return false; + } + + return true; +} + bool MQTTClient::loop() { // return immediately if not connected if (!this->connected()) { diff --git a/src/MQTTClient.h b/src/MQTTClient.h index 55071e2..5083c58 100644 --- a/src/MQTTClient.h +++ b/src/MQTTClient.h @@ -178,9 +178,11 @@ class MQTTClient { bool subscribe(const String &topic, int qos) { return this->subscribe(topic.c_str(), qos); } bool subscribe(const char topic[]) { return this->subscribe(topic, 0); } bool subscribe(const char topic[], int qos); + bool subscribeMultiple(int count, const char *topics[], int qos_levels[]); bool unsubscribe(const String &topic) { return this->unsubscribe(topic.c_str()); } bool unsubscribe(const char topic[]); + bool unsubscribeMultiple(int count, const char *topics[]); bool loop(); bool connected();