Fix Adafruit_MQTT:subscribe()

This method contained a loop that checked for an existing subscription,
but it only printed a debug message if so. Now, it immediately returns
true if the subscription is already registered.

When this method succesfully adds a subscription, it was documented to
return true, but the actual code did not return anything in this case,
resulting in a compiler warning. In practice, on AVR, the value of the
first argument would be returned, which likely evaluates as true, so
it is likely it actually seemed to work fine.
This commit is contained in:
Matthijs Kooijman 2015-07-02 13:28:38 +02:00
parent 564e87ff36
commit b996b096b3
2 changed files with 6 additions and 7 deletions

View File

@ -176,7 +176,7 @@ bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
for (i=0; i<MAXSUBSCRIPTIONS; i++) { for (i=0; i<MAXSUBSCRIPTIONS; i++) {
if (subscriptions[i] == sub) { if (subscriptions[i] == sub) {
DEBUG_PRINTLN(F("Already subscribed")); DEBUG_PRINTLN(F("Already subscribed"));
break; return true;
} }
} }
if (i==MAXSUBSCRIPTIONS) { // add to subscriptionlist if (i==MAXSUBSCRIPTIONS) { // add to subscriptionlist
@ -184,14 +184,13 @@ bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
if (subscriptions[i] == 0) { if (subscriptions[i] == 0) {
DEBUG_PRINT(F("Added sub ")); DEBUG_PRINTLN(i); DEBUG_PRINT(F("Added sub ")); DEBUG_PRINTLN(i);
subscriptions[i] = sub; subscriptions[i] = sub;
break; return true;
} }
} }
} }
if (i==MAXSUBSCRIPTIONS) {
DEBUG_PRINTLN(F("no more subscription space :(")); DEBUG_PRINTLN(F("no more subscription space :("));
return false; return false;
}
} }
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) { Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {

View File

@ -127,7 +127,7 @@ class Adafruit_MQTT {
bool publish(const char *topic, const char *payload, uint8_t qos); bool publish(const char *topic, const char *payload, uint8_t qos);
// Add a subscription to receive messages for a topic. Returns true if the // Add a subscription to receive messages for a topic. Returns true if the
// subscription could be added, false otherwise. // subscription could be added or was already present, false otherwise.
bool subscribe(Adafruit_MQTT_Subscribe *sub); bool subscribe(Adafruit_MQTT_Subscribe *sub);
// Check if any subscriptions have new messages. Will return a reference to // Check if any subscriptions have new messages. Will return a reference to