2015-06-11 00:38:34 +02:00
|
|
|
// The MIT License (MIT)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2015 Adafruit Industries
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2020-06-25 15:56:46 +02:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
2015-06-11 00:38:34 +02:00
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
// SOFTWARE.
|
2015-06-01 00:38:33 +02:00
|
|
|
#include "Adafruit_MQTT.h"
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
#if defined(ARDUINO_SAMD_ZERO) || defined(ARDUINO_SAMD_MKR1000) || \
|
2020-09-23 11:27:06 +02:00
|
|
|
defined(ARDUINO_SAMD_MKR1010) || defined(ARDUINO_ARCH_SAMD)
|
2020-06-25 15:56:46 +02:00
|
|
|
static char *dtostrf(double val, signed char width, unsigned char prec,
|
|
|
|
char *sout) {
|
2015-11-11 16:22:26 +01:00
|
|
|
char fmt[20];
|
|
|
|
sprintf(fmt, "%%%d.%df", width, prec);
|
|
|
|
sprintf(sout, fmt, val);
|
|
|
|
return sout;
|
|
|
|
}
|
|
|
|
#endif
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2016-08-10 03:37:07 +02:00
|
|
|
#if defined(ESP8266)
|
2020-06-25 15:56:46 +02:00
|
|
|
int strncasecmp(const char *str1, const char *str2, int len) {
|
|
|
|
int d = 0;
|
|
|
|
while (len--) {
|
|
|
|
int c1 = tolower(*str1++);
|
|
|
|
int c2 = tolower(*str2++);
|
|
|
|
if (((d = c1 - c2) != 0) || (c2 == '\0')) {
|
|
|
|
return d;
|
2016-08-10 03:37:07 +02:00
|
|
|
}
|
2020-06-25 15:56:46 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2016-08-10 03:37:07 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-07-06 23:20:02 +02:00
|
|
|
void printBuffer(uint8_t *buffer, uint16_t len) {
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTER.print('\t');
|
2020-06-25 15:56:46 +02:00
|
|
|
for (uint16_t i = 0; i < len; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
if (isprint(buffer[i]))
|
|
|
|
DEBUG_PRINTER.write(buffer[i]);
|
|
|
|
else
|
|
|
|
DEBUG_PRINTER.print(" ");
|
|
|
|
DEBUG_PRINTER.print(F(" [0x"));
|
|
|
|
if (buffer[i] < 0x10)
|
|
|
|
DEBUG_PRINTER.print("0");
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINTER.print(buffer[i], HEX);
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTER.print("], ");
|
|
|
|
if (i % 8 == 7) {
|
|
|
|
DEBUG_PRINTER.print("\n\t");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG_PRINTER.println();
|
2015-06-05 06:16:08 +02:00
|
|
|
}
|
|
|
|
|
2015-07-02 13:39:33 +02:00
|
|
|
/* Not used now, but might be useful in the future
|
2015-06-05 06:16:08 +02:00
|
|
|
static uint8_t *stringprint(uint8_t *p, char *s) {
|
|
|
|
uint16_t len = strlen(s);
|
|
|
|
p[0] = len >> 8; p++;
|
|
|
|
p[0] = len & 0xFF; p++;
|
2016-01-18 02:27:20 +01:00
|
|
|
memmove(p, s, len);
|
2015-06-05 06:16:08 +02:00
|
|
|
return p+len;
|
|
|
|
}
|
2015-07-02 13:39:33 +02:00
|
|
|
*/
|
2015-06-05 06:16:08 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
static uint8_t *stringprint(uint8_t *p, const char *s, uint16_t maxlen = 0) {
|
2020-02-24 07:28:48 +01:00
|
|
|
// If maxlen is specified (has a non-zero value) then use it as the maximum
|
|
|
|
// length of the source string to write to the buffer. Otherwise write
|
|
|
|
// the entire source string.
|
|
|
|
uint16_t len = strlen(s);
|
|
|
|
if (maxlen > 0 && len > maxlen) {
|
|
|
|
len = maxlen;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
for (uint8_t i=0; i<len; i++) {
|
|
|
|
Serial.write(pgm_read_byte(s+i));
|
|
|
|
}
|
|
|
|
*/
|
2020-06-25 15:56:46 +02:00
|
|
|
p[0] = len >> 8;
|
|
|
|
p++;
|
|
|
|
p[0] = len & 0xFF;
|
|
|
|
p++;
|
2020-02-24 07:28:48 +01:00
|
|
|
strncpy((char *)p, s, len);
|
2020-06-25 15:56:46 +02:00
|
|
|
return p + len;
|
2015-06-05 06:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adafruit_MQTT Definition ////////////////////////////////////////////////////
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port, const char *cid,
|
|
|
|
const char *user, const char *pass) {
|
2020-02-24 07:28:48 +01:00
|
|
|
servername = server;
|
|
|
|
portnum = port;
|
|
|
|
clientid = cid;
|
|
|
|
username = user;
|
|
|
|
password = pass;
|
2015-10-05 20:54:37 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// reset subscriptions
|
2020-06-25 15:56:46 +02:00
|
|
|
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
subscriptions[i] = 0;
|
|
|
|
}
|
2016-05-20 20:41:16 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
will_topic = 0;
|
|
|
|
will_payload = 0;
|
|
|
|
will_qos = 0;
|
|
|
|
will_retain = 0;
|
|
|
|
|
|
|
|
packet_id_counter = 0;
|
2015-06-02 21:23:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
Adafruit_MQTT::Adafruit_MQTT(const char *server, uint16_t port,
|
|
|
|
const char *user, const char *pass) {
|
2020-02-24 07:28:48 +01:00
|
|
|
servername = server;
|
|
|
|
portnum = port;
|
|
|
|
clientid = "";
|
|
|
|
username = user;
|
|
|
|
password = pass;
|
2015-10-22 16:09:16 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// reset subscriptions
|
2020-06-25 15:56:46 +02:00
|
|
|
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
subscriptions[i] = 0;
|
|
|
|
}
|
2016-05-20 20:41:16 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
will_topic = 0;
|
|
|
|
will_payload = 0;
|
|
|
|
will_qos = 0;
|
|
|
|
will_retain = 0;
|
2015-10-22 16:09:16 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
packet_id_counter = 0;
|
2020-02-24 04:42:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
int8_t Adafruit_MQTT::connect() {
|
|
|
|
// Connect to the server.
|
|
|
|
if (!connectServer())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Construct and send connect packet.
|
|
|
|
uint8_t len = connectPacket(buffer);
|
|
|
|
if (!sendPacket(buffer, len))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Read connect response packet and verify it
|
|
|
|
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
|
2020-11-06 16:20:52 +01:00
|
|
|
if (len != 4) {
|
2020-02-24 07:28:48 +01:00
|
|
|
return -1;
|
2020-11-06 16:20:52 +01:00
|
|
|
}
|
|
|
|
if ((buffer[0] != (MQTT_CTRL_CONNECTACK << 4)) || (buffer[1] != 2)) {
|
2020-02-24 07:28:48 +01:00
|
|
|
return -1;
|
2020-11-06 16:20:52 +01:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
if (buffer[3] != 0)
|
|
|
|
return buffer[3];
|
|
|
|
|
|
|
|
// Setup subscriptions once connected.
|
2020-06-25 15:56:46 +02:00
|
|
|
for (uint8_t i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
// Ignore subscriptions that aren't defined.
|
2020-06-25 15:56:46 +02:00
|
|
|
if (subscriptions[i] == 0)
|
|
|
|
continue;
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
boolean success = false;
|
2020-11-06 16:20:52 +01:00
|
|
|
for (uint8_t retry = 0; (retry < 3) && !success;
|
|
|
|
retry++) { // retry until we get a suback
|
2020-02-24 07:28:48 +01:00
|
|
|
// Construct and send subscription packet.
|
2020-06-25 15:56:46 +02:00
|
|
|
uint8_t len = subscribePacket(buffer, subscriptions[i]->topic,
|
|
|
|
subscriptions[i]->qos);
|
2020-02-24 07:28:48 +01:00
|
|
|
if (!sendPacket(buffer, len))
|
2020-06-25 15:56:46 +02:00
|
|
|
return -1;
|
2020-02-24 07:28:48 +01:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
if (MQTT_PROTOCOL_LEVEL < 3) // older versions didn't suback
|
|
|
|
break;
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
// Check for SUBACK if using MQTT 3.1.1 or higher
|
2020-06-25 15:56:46 +02:00
|
|
|
// TODO: The Server is permitted to start sending PUBLISH packets matching
|
|
|
|
// the Subscription before the Server sends the SUBACK Packet. (will
|
|
|
|
// really need to use callbacks - ada)
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
if (processPacketsUntil(buffer, MQTT_CTRL_SUBACK, SUBACK_TIMEOUT_MS)) {
|
2020-06-25 15:56:46 +02:00
|
|
|
success = true;
|
|
|
|
break;
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:56:46 +02:00
|
|
|
if (!success)
|
|
|
|
return -2; // failed to sub for some reason
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
int8_t Adafruit_MQTT::connect(const char *user, const char *pass) {
|
2020-02-24 07:28:48 +01:00
|
|
|
username = user;
|
|
|
|
password = pass;
|
|
|
|
return connect();
|
2016-07-13 21:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer,
|
|
|
|
uint8_t waitforpackettype,
|
|
|
|
uint16_t timeout) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint16_t len;
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
while (true) {
|
2020-02-24 07:28:48 +01:00
|
|
|
len = readFullPacket(buffer, MAXBUFFERSIZE, timeout);
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
if (len == 0) {
|
2020-02-24 07:28:48 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-08-18 00:01:52 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t packetType = (buffer[0] >> 4);
|
|
|
|
if (packetType == waitforpackettype) {
|
2020-11-06 16:20:52 +01:00
|
|
|
return len;
|
2020-02-24 07:28:48 +01:00
|
|
|
} else {
|
2020-11-06 16:20:52 +01:00
|
|
|
if (packetType == MQTT_CTRL_PUBLISH) {
|
|
|
|
handleSubscriptionPacket(len);
|
|
|
|
} else {
|
|
|
|
ERROR_PRINTLN(F("Dropped a packet"));
|
|
|
|
}
|
2016-02-09 05:36:25 +01:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2016-02-09 05:36:25 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize,
|
|
|
|
uint16_t timeout) {
|
2020-02-24 07:28:48 +01:00
|
|
|
// will read a packet and Do The Right Thing with length
|
|
|
|
uint8_t *pbuff = buffer;
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2021-05-13 06:31:22 +02:00
|
|
|
uint16_t rlen;
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// read the packet type:
|
|
|
|
rlen = readPacket(pbuff, 1, timeout);
|
2020-06-25 15:56:46 +02:00
|
|
|
if (rlen != 1)
|
|
|
|
return 0;
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Packet Type:\t"));
|
|
|
|
DEBUG_PRINTBUFFER(pbuff, rlen);
|
2020-02-24 07:28:48 +01:00
|
|
|
pbuff++;
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
uint32_t value = 0;
|
|
|
|
uint32_t multiplier = 1;
|
|
|
|
uint8_t encodedByte;
|
2016-02-09 05:36:25 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
do {
|
|
|
|
rlen = readPacket(pbuff, 1, timeout);
|
2020-06-25 15:56:46 +02:00
|
|
|
if (rlen != 1)
|
|
|
|
return 0;
|
2020-02-24 07:28:48 +01:00
|
|
|
encodedByte = pbuff[0]; // save the last read val
|
2020-06-25 15:56:46 +02:00
|
|
|
pbuff++; // get ready for reading the next byte
|
2020-02-24 07:28:48 +01:00
|
|
|
uint32_t intermediate = encodedByte & 0x7F;
|
|
|
|
intermediate *= multiplier;
|
|
|
|
value += intermediate;
|
|
|
|
multiplier *= 128;
|
2020-06-25 15:56:46 +02:00
|
|
|
if (multiplier > (128UL * 128UL * 128UL)) {
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINT(F("Malformed packet len\n"));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} while (encodedByte & 0x80);
|
|
|
|
|
2020-11-06 16:20:52 +01:00
|
|
|
DEBUG_PRINT(F("Packet Length:\t"));
|
|
|
|
DEBUG_PRINTLN(value);
|
2020-02-24 07:28:48 +01:00
|
|
|
|
2020-11-06 16:20:52 +01:00
|
|
|
if (value > (maxsize - (pbuff - buffer) - 1)) {
|
|
|
|
DEBUG_PRINTLN(F("Packet too big for buffer"));
|
|
|
|
rlen = readPacket(pbuff, (maxsize - (pbuff - buffer) - 1), timeout);
|
2020-02-24 07:28:48 +01:00
|
|
|
} else {
|
|
|
|
rlen = readPacket(pbuff, value, timeout);
|
|
|
|
}
|
2020-06-25 15:56:46 +02:00
|
|
|
// DEBUG_PRINT(F("Remaining packet:\t")); DEBUG_PRINTBUFFER(pbuff, rlen);
|
|
|
|
|
|
|
|
return ((pbuff - buffer) + rlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
const __FlashStringHelper *Adafruit_MQTT::connectErrorString(int8_t code) {
|
|
|
|
switch (code) {
|
|
|
|
case 1:
|
|
|
|
return F(
|
|
|
|
"The Server does not support the level of the MQTT protocol requested");
|
|
|
|
case 2:
|
|
|
|
return F(
|
|
|
|
"The Client identifier is correct UTF-8 but not allowed by the Server");
|
|
|
|
case 3:
|
|
|
|
return F("The MQTT service is unavailable");
|
|
|
|
case 4:
|
|
|
|
return F("The data in the user name or password is malformed");
|
|
|
|
case 5:
|
|
|
|
return F("Not authorized to connect");
|
|
|
|
case 6:
|
|
|
|
return F("Exceeded reconnect rate limit. Please try again later.");
|
|
|
|
case 7:
|
|
|
|
return F("You have been banned from connecting. Please contact the MQTT "
|
|
|
|
"server administrator for more details.");
|
|
|
|
case -1:
|
|
|
|
return F("Connection failed");
|
|
|
|
case -2:
|
|
|
|
return F("Failed to subscribe");
|
|
|
|
default:
|
|
|
|
return F("Unknown error");
|
|
|
|
}
|
2015-07-02 13:01:05 +02:00
|
|
|
}
|
|
|
|
|
2015-10-05 20:18:31 +02:00
|
|
|
bool Adafruit_MQTT::disconnect() {
|
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// Construct and send disconnect packet.
|
|
|
|
uint8_t len = disconnectPacket(buffer);
|
2020-06-25 15:56:46 +02:00
|
|
|
if (!sendPacket(buffer, len))
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTLN(F("Unable to send disconnect packet"));
|
2015-10-05 20:18:31 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
return disconnectServer();
|
2015-10-05 20:18:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
bool Adafruit_MQTT::publish(const char *topic, const char *data, uint8_t qos) {
|
2020-06-25 15:56:46 +02:00
|
|
|
return publish(topic, (uint8_t *)(data), strlen(data), qos);
|
2016-01-18 02:27:20 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
bool Adafruit_MQTT::publish(const char *topic, uint8_t *data, uint16_t bLen,
|
|
|
|
uint8_t qos) {
|
2020-02-24 07:28:48 +01:00
|
|
|
// Construct and send publish packet.
|
|
|
|
uint16_t len = publishPacket(buffer, topic, data, bLen, qos);
|
|
|
|
if (!sendPacket(buffer, len))
|
|
|
|
return false;
|
2016-01-18 02:27:20 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// If QOS level is high enough verify the response packet.
|
|
|
|
if (qos > 0) {
|
|
|
|
len = readFullPacket(buffer, MAXBUFFERSIZE, PUBLISH_TIMEOUT_MS);
|
|
|
|
DEBUG_PRINT(F("Publish QOS1+ reply:\t"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
if (len != 4)
|
|
|
|
return false;
|
|
|
|
if ((buffer[0] >> 4) != MQTT_CTRL_PUBACK)
|
|
|
|
return false;
|
|
|
|
uint16_t packnum = buffer[2];
|
|
|
|
packnum <<= 8;
|
|
|
|
packnum |= buffer[3];
|
2015-06-05 06:16:08 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
// we increment the packet_id_counter right after publishing so inc here too
|
|
|
|
// to match
|
2020-02-24 07:28:48 +01:00
|
|
|
packnum++;
|
|
|
|
if (packnum != packet_id_counter)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2015-06-01 05:56:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
bool Adafruit_MQTT::will(const char *topic, const char *payload, uint8_t qos,
|
|
|
|
uint8_t retain) {
|
2015-10-05 15:50:57 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
if (connected()) {
|
|
|
|
DEBUG_PRINT(F("Will defined after connect"));
|
|
|
|
return false;
|
|
|
|
}
|
2015-10-05 15:50:57 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
will_topic = topic;
|
|
|
|
will_payload = payload;
|
|
|
|
will_qos = qos;
|
|
|
|
will_retain = retain;
|
2015-10-05 15:50:57 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
return true;
|
2015-10-05 15:50:57 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
bool Adafruit_MQTT::subscribe(Adafruit_MQTT_Subscribe *sub) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t i;
|
|
|
|
// see if we are already subscribed
|
2020-06-25 15:56:46 +02:00
|
|
|
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
if (subscriptions[i] == sub) {
|
|
|
|
DEBUG_PRINTLN(F("Already subscribed"));
|
|
|
|
return true;
|
2015-06-05 06:16:08 +02:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
2020-06-25 15:56:46 +02:00
|
|
|
if (i == MAXSUBSCRIPTIONS) { // add to subscriptionlist
|
|
|
|
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
if (subscriptions[i] == 0) {
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Added sub "));
|
|
|
|
DEBUG_PRINTLN(i);
|
2020-02-24 07:28:48 +01:00
|
|
|
subscriptions[i] = sub;
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-02 13:26:56 +02:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
2015-07-02 13:28:38 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTLN(F("no more subscription space :("));
|
|
|
|
return false;
|
2015-06-02 21:23:42 +02:00
|
|
|
}
|
|
|
|
|
2015-10-05 16:33:33 +02:00
|
|
|
bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t i;
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// see if we are already subscribed
|
2020-06-25 15:56:46 +02:00
|
|
|
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
if (subscriptions[i] == sub) {
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINTLN(
|
|
|
|
F("Found matching subscription and attempting to unsubscribe."));
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// Construct and send unsubscribe packet.
|
|
|
|
uint8_t len = unsubscribePacket(buffer, subscriptions[i]->topic);
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// sending unsubscribe failed
|
2020-06-25 15:56:46 +02:00
|
|
|
if (!sendPacket(buffer, len))
|
2020-02-24 07:28:48 +01:00
|
|
|
return false;
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// if QoS for this subscription is 1 or 2, we need
|
|
|
|
// to wait for the unsuback to confirm unsubscription
|
2020-06-25 15:56:46 +02:00
|
|
|
if (subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
|
2020-02-24 04:42:43 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// wait for UNSUBACK
|
|
|
|
len = readFullPacket(buffer, MAXBUFFERSIZE, CONNECT_TIMEOUT_MS);
|
|
|
|
DEBUG_PRINT(F("UNSUBACK:\t"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
if ((len != 5) || (buffer[0] != (MQTT_CTRL_UNSUBACK << 4))) {
|
2020-06-25 15:56:46 +02:00
|
|
|
return false; // failure to unsubscribe
|
2015-10-05 16:33:33 +02:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
subscriptions[i] = 0;
|
|
|
|
return true;
|
2015-10-05 16:33:33 +02:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// subscription not found, so we are unsubscribed
|
|
|
|
return true;
|
2015-10-05 16:33:33 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 23:01:40 +02:00
|
|
|
void Adafruit_MQTT::processPackets(int16_t timeout) {
|
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
uint32_t elapsed = 0, endtime, starttime = millis();
|
|
|
|
|
|
|
|
while (elapsed < (uint32_t)timeout) {
|
|
|
|
Adafruit_MQTT_Subscribe *sub = readSubscription(timeout - elapsed);
|
|
|
|
if (sub) {
|
|
|
|
if (sub->callback_uint32t != NULL) {
|
2020-11-06 16:20:52 +01:00
|
|
|
// huh lets do the callback in integer mode
|
|
|
|
uint32_t data = 0;
|
|
|
|
data = atoi((char *)sub->lastread);
|
|
|
|
sub->callback_uint32t(data);
|
|
|
|
} else if (sub->callback_double != NULL) {
|
|
|
|
// huh lets do the callback in doublefloat mode
|
|
|
|
double data = 0;
|
|
|
|
data = atof((char *)sub->lastread);
|
|
|
|
sub->callback_double(data);
|
|
|
|
} else if (sub->callback_buffer != NULL) {
|
|
|
|
// huh lets do the callback in buffer mode
|
|
|
|
sub->callback_buffer((char *)sub->lastread, sub->datalen);
|
|
|
|
} else if (sub->callback_io != NULL) {
|
2020-02-24 07:28:48 +01:00
|
|
|
// huh lets do the callback in io mode
|
2020-06-25 15:56:46 +02:00
|
|
|
((sub->io_mqtt)->*(sub->callback_io))((char *)sub->lastread,
|
|
|
|
sub->datalen);
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep track over elapsed time
|
|
|
|
endtime = millis();
|
|
|
|
if (endtime < starttime) {
|
|
|
|
starttime = endtime; // looped around!")
|
|
|
|
}
|
|
|
|
elapsed += (endtime - starttime);
|
|
|
|
}
|
2016-07-08 23:01:40 +02:00
|
|
|
}
|
2015-06-05 06:16:08 +02:00
|
|
|
Adafruit_MQTT_Subscribe *Adafruit_MQTT::readSubscription(int16_t timeout) {
|
2020-11-06 16:20:52 +01:00
|
|
|
// Check if data is available to read.
|
|
|
|
uint16_t len =
|
|
|
|
readFullPacket(buffer, MAXBUFFERSIZE, timeout); // return one full packet
|
|
|
|
return handleSubscriptionPacket(len);
|
2020-02-24 07:41:00 +01:00
|
|
|
}
|
|
|
|
|
2021-05-13 06:31:22 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
uint16_t topicOffsetFromLenth(uint16_t const len)
|
|
|
|
{
|
|
|
|
if (len < 128) { // 7 bits (+1 continuation bit)
|
|
|
|
return 0;
|
|
|
|
} else if (len < 16384) { // 14 bits (+2 continuation bits)
|
|
|
|
return 1;
|
|
|
|
} else if (len < 2097152) { // 21 bits
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon
|
|
|
|
|
2020-02-24 07:41:00 +01:00
|
|
|
Adafruit_MQTT_Subscribe *Adafruit_MQTT::handleSubscriptionPacket(uint16_t len) {
|
2020-11-06 16:20:52 +01:00
|
|
|
uint16_t i, topiclen, datalen;
|
2020-02-24 07:28:48 +01:00
|
|
|
|
2020-11-06 16:20:52 +01:00
|
|
|
if (!len) {
|
|
|
|
return NULL; // No data available, just quit.
|
|
|
|
}
|
|
|
|
DEBUG_PRINT("Packet len: ");
|
|
|
|
DEBUG_PRINTLN(len);
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
|
2020-11-06 16:20:52 +01:00
|
|
|
if (len < 3) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((buffer[0] & 0xF0) != (MQTT_CTRL_PUBLISH) << 4) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
// Parse out length of packet.
|
2021-05-13 06:31:22 +02:00
|
|
|
uint16_t const topicoffset = topicOffsetFromLength(len);
|
|
|
|
uint16_t const topicstart = topicoffset + 4;
|
|
|
|
topiclen = buffer[3+topicoffset];
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Looking for subscription len "));
|
|
|
|
DEBUG_PRINTLN(topiclen);
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
// Find subscription associated with this packet.
|
2020-06-25 15:56:46 +02:00
|
|
|
for (i = 0; i < MAXSUBSCRIPTIONS; i++) {
|
2020-02-24 07:28:48 +01:00
|
|
|
if (subscriptions[i]) {
|
|
|
|
// Skip this subscription if its name length isn't the same as the
|
|
|
|
// received topic name.
|
|
|
|
if (strlen(subscriptions[i]->topic) != topiclen)
|
|
|
|
continue;
|
|
|
|
// Stop if the subscription topic matches the received topic. Be careful
|
|
|
|
// to make comparison case insensitive.
|
2021-05-13 06:31:22 +02:00
|
|
|
if (strncasecmp((char *)buffer + topicstart, subscriptions[i]->topic, topiclen) ==
|
2020-06-25 15:56:46 +02:00
|
|
|
0) {
|
|
|
|
DEBUG_PRINT(F("Found sub #"));
|
|
|
|
DEBUG_PRINTLN(i);
|
2020-02-24 07:28:48 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-25 15:56:46 +02:00
|
|
|
if (i == MAXSUBSCRIPTIONS)
|
|
|
|
return NULL; // matching sub not found ???
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
uint8_t packet_id_len = 0;
|
|
|
|
uint16_t packetid = 0;
|
|
|
|
// Check if it is QoS 1, TODO: we dont support QoS 2
|
|
|
|
if ((buffer[0] & 0x6) == 0x2) {
|
|
|
|
packet_id_len = 2;
|
2021-05-13 06:31:22 +02:00
|
|
|
packetid = buffer[topiclen + topicstart];
|
2020-02-24 07:28:48 +01:00
|
|
|
packetid <<= 8;
|
2021-05-13 06:31:22 +02:00
|
|
|
packetid |= buffer[topiclen + topicstart + 1];
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// zero out the old data
|
|
|
|
memset(subscriptions[i]->lastread, 0, SUBSCRIPTIONDATALEN);
|
|
|
|
|
2021-05-13 06:31:22 +02:00
|
|
|
datalen = len - topiclen - packet_id_len - topicstart;
|
2020-02-24 07:28:48 +01:00
|
|
|
if (datalen > SUBSCRIPTIONDATALEN) {
|
2020-06-25 15:56:46 +02:00
|
|
|
datalen = SUBSCRIPTIONDATALEN - 1; // cut it off
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
|
|
|
// extract out just the data, into the subscription object itself
|
2021-05-13 06:31:22 +02:00
|
|
|
memmove(subscriptions[i]->lastread, buffer + topicstart + topiclen + packet_id_len,
|
2020-06-25 15:56:46 +02:00
|
|
|
datalen);
|
2020-02-24 07:28:48 +01:00
|
|
|
subscriptions[i]->datalen = datalen;
|
2020-06-25 15:56:46 +02:00
|
|
|
DEBUG_PRINT(F("Data len: "));
|
|
|
|
DEBUG_PRINTLN(datalen);
|
|
|
|
DEBUG_PRINT(F("Data: "));
|
|
|
|
DEBUG_PRINTLN((char *)subscriptions[i]->lastread);
|
2020-02-24 07:28:48 +01:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
if ((MQTT_PROTOCOL_LEVEL > 3) && (buffer[0] & 0x6) == 0x2) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t ackpacket[4];
|
|
|
|
|
|
|
|
// Construct and send puback packet.
|
|
|
|
uint8_t len = pubackPacket(ackpacket, packetid);
|
|
|
|
if (!sendPacket(ackpacket, len))
|
|
|
|
DEBUG_PRINT(F("Failed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the valid matching subscription
|
|
|
|
return subscriptions[i];
|
2015-06-05 06:16:08 +02:00
|
|
|
}
|
|
|
|
|
2015-10-22 19:03:55 +02:00
|
|
|
void Adafruit_MQTT::flushIncoming(uint16_t timeout) {
|
2020-02-24 07:28:48 +01:00
|
|
|
// flush input!
|
|
|
|
DEBUG_PRINTLN(F("Flushing input buffer"));
|
2020-06-25 15:56:46 +02:00
|
|
|
while (readPacket(buffer, MAXBUFFERSIZE, timeout))
|
|
|
|
;
|
2015-10-22 19:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Adafruit_MQTT::ping(uint8_t num) {
|
2020-06-25 15:56:46 +02:00
|
|
|
// flushIncoming(100);
|
2016-01-18 02:27:20 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
while (num--) {
|
|
|
|
// Construct and send ping packet.
|
|
|
|
uint8_t len = pingPacket(buffer);
|
|
|
|
if (!sendPacket(buffer, len))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Process ping reply.
|
|
|
|
len = processPacketsUntil(buffer, MQTT_CTRL_PINGRESP, PING_TIMEOUT_MS);
|
|
|
|
if (buffer[0] == (MQTT_CTRL_PINGRESP << 4))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-06-05 06:16:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Packet Generation Functions /////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// The current MQTT spec is 3.1.1 and available here:
|
|
|
|
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718028
|
|
|
|
// However this connect packet and code follows the MQTT 3.1 spec here (some
|
|
|
|
// small differences in the protocol):
|
|
|
|
// http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#connect
|
2015-06-01 00:38:33 +02:00
|
|
|
uint8_t Adafruit_MQTT::connectPacket(uint8_t *packet) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t *p = packet;
|
|
|
|
uint16_t len;
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// fixed header, connection messsage no flags
|
|
|
|
p[0] = (MQTT_CTRL_CONNECT << 4) | 0x0;
|
2020-06-25 15:56:46 +02:00
|
|
|
p += 2;
|
2020-02-24 07:28:48 +01:00
|
|
|
// fill in packet[1] last
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2015-10-21 06:05:46 +02:00
|
|
|
#if MQTT_PROTOCOL_LEVEL == 3
|
2020-06-25 15:56:46 +02:00
|
|
|
p = stringprint(p, "MQIsdp");
|
2015-10-21 06:05:46 +02:00
|
|
|
#elif MQTT_PROTOCOL_LEVEL == 4
|
2020-06-25 15:56:46 +02:00
|
|
|
p = stringprint(p, "MQTT");
|
2015-10-21 06:05:46 +02:00
|
|
|
#else
|
2020-06-25 15:56:46 +02:00
|
|
|
#error "MQTT level not supported"
|
2015-10-21 06:05:46 +02:00
|
|
|
#endif
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = MQTT_PROTOCOL_LEVEL;
|
|
|
|
p++;
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// always clean the session
|
|
|
|
p[0] = MQTT_CONN_CLEANSESSION;
|
2015-10-05 15:45:27 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// set the will flags if needed
|
|
|
|
if (will_topic && pgm_read_byte(will_topic) != 0) {
|
2015-10-05 15:45:27 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] |= MQTT_CONN_WILLFLAG;
|
2015-10-05 15:45:27 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
if (will_qos == 1)
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] |= MQTT_CONN_WILLQOS_1;
|
2020-06-25 15:56:46 +02:00
|
|
|
else if (will_qos == 2)
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] |= MQTT_CONN_WILLQOS_2;
|
2015-10-05 15:45:27 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
if (will_retain == 1)
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] |= MQTT_CONN_WILLRETAIN;
|
|
|
|
}
|
2015-10-05 15:45:27 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
if (pgm_read_byte(username) != 0)
|
|
|
|
p[0] |= MQTT_CONN_USERNAMEFLAG;
|
|
|
|
if (pgm_read_byte(password) != 0)
|
|
|
|
p[0] |= MQTT_CONN_PASSWORDFLAG;
|
|
|
|
p++;
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = MQTT_CONN_KEEPALIVE >> 8;
|
|
|
|
p++;
|
|
|
|
p[0] = MQTT_CONN_KEEPALIVE & 0xFF;
|
|
|
|
p++;
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
if (MQTT_PROTOCOL_LEVEL == 3) {
|
|
|
|
p = stringprint(p, clientid, 23); // Limit client ID to first 23 characters.
|
2020-02-24 07:28:48 +01:00
|
|
|
} else {
|
|
|
|
if (pgm_read_byte(clientid) != 0) {
|
|
|
|
p = stringprint(p, clientid);
|
2015-10-02 23:10:16 +02:00
|
|
|
} else {
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = 0x0;
|
|
|
|
p++;
|
|
|
|
p[0] = 0x0;
|
|
|
|
p++;
|
|
|
|
DEBUG_PRINTLN(F("SERVER GENERATING CLIENT ID"));
|
2015-10-02 16:45:00 +02:00
|
|
|
}
|
2020-02-24 07:28:48 +01:00
|
|
|
}
|
2015-06-01 03:07:17 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
if (will_topic && pgm_read_byte(will_topic) != 0) {
|
|
|
|
p = stringprint(p, will_topic);
|
|
|
|
p = stringprint(p, will_payload);
|
|
|
|
}
|
2015-10-05 19:41:14 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
if (pgm_read_byte(username) != 0) {
|
|
|
|
p = stringprint(p, username);
|
|
|
|
}
|
|
|
|
if (pgm_read_byte(password) != 0) {
|
|
|
|
p = stringprint(p, password);
|
|
|
|
}
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
len = p - packet;
|
2015-06-01 00:38:33 +02:00
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
packet[1] = len - 2; // don't include the 2 bytes of fixed header data
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTLN(F("MQTT connect packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
// as per
|
|
|
|
// http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718040
|
2016-07-07 00:32:21 +02:00
|
|
|
uint16_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
|
2020-06-25 15:56:46 +02:00
|
|
|
uint8_t *data, uint16_t bLen,
|
|
|
|
uint8_t qos) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t *p = packet;
|
2020-06-25 15:56:46 +02:00
|
|
|
uint16_t len = 0;
|
2020-02-24 07:28:48 +01:00
|
|
|
|
|
|
|
// calc length of non-header data
|
2020-06-25 15:56:46 +02:00
|
|
|
len += 2; // two bytes to set the topic size
|
2020-02-24 07:28:48 +01:00
|
|
|
len += strlen(topic); // topic length
|
2020-11-06 16:20:52 +01:00
|
|
|
if (qos > 0) {
|
2020-02-24 07:28:48 +01:00
|
|
|
len += 2; // qos packet id
|
|
|
|
}
|
|
|
|
len += bLen; // payload length
|
|
|
|
|
|
|
|
// Now you can start generating the packet!
|
|
|
|
p[0] = MQTT_CTRL_PUBLISH << 4 | qos << 1;
|
|
|
|
p++;
|
|
|
|
|
|
|
|
// fill in packet[1] last
|
|
|
|
do {
|
|
|
|
uint8_t encodedByte = len % 128;
|
|
|
|
len /= 128;
|
|
|
|
// if there are more data to encode, set the top bit of this byte
|
2020-06-25 15:56:46 +02:00
|
|
|
if (len > 0) {
|
2020-02-24 07:28:48 +01:00
|
|
|
encodedByte |= 0x80;
|
|
|
|
}
|
|
|
|
p[0] = encodedByte;
|
2016-07-07 00:32:21 +02:00
|
|
|
p++;
|
2020-06-25 15:56:46 +02:00
|
|
|
} while (len > 0);
|
2016-01-18 02:27:20 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// topic comes before packet identifier
|
|
|
|
p = stringprint(p, topic);
|
2016-01-18 02:27:20 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// add packet identifier. used for checking PUBACK in QOS > 0
|
2020-06-25 15:56:46 +02:00
|
|
|
if (qos > 0) {
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = (packet_id_counter >> 8) & 0xFF;
|
|
|
|
p[1] = packet_id_counter & 0xFF;
|
2020-06-25 15:56:46 +02:00
|
|
|
p += 2;
|
2016-01-18 02:27:20 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// increment the packet id
|
|
|
|
packet_id_counter++;
|
|
|
|
}
|
2020-02-24 04:42:43 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
memmove(p, data, bLen);
|
2020-06-25 15:56:46 +02:00
|
|
|
p += bLen;
|
2020-02-24 07:28:48 +01:00
|
|
|
len = p - packet;
|
|
|
|
DEBUG_PRINTLN(F("MQTT publish packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
2016-01-18 02:27:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 07:22:58 +01:00
|
|
|
uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
|
|
|
|
uint8_t qos) {
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t *p = packet;
|
|
|
|
uint16_t len;
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = MQTT_CTRL_SUBSCRIBE << 4 | MQTT_QOS_1 << 1;
|
|
|
|
// fill in packet[1] last
|
2020-06-25 15:56:46 +02:00
|
|
|
p += 2;
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// packet identifier. used for checking SUBACK
|
|
|
|
p[0] = (packet_id_counter >> 8) & 0xFF;
|
|
|
|
p[1] = packet_id_counter & 0xFF;
|
2020-06-25 15:56:46 +02:00
|
|
|
p += 2;
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// increment the packet id
|
|
|
|
packet_id_counter++;
|
2015-10-05 17:21:05 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p = stringprint(p, topic);
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = qos;
|
|
|
|
p++;
|
2015-06-03 00:33:03 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
len = p - packet;
|
2020-06-25 15:56:46 +02:00
|
|
|
packet[1] = len - 2; // don't include the 2 bytes of fixed header data
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTLN(F("MQTT subscription packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
2015-06-03 00:33:03 +02:00
|
|
|
}
|
|
|
|
|
2015-10-05 16:33:33 +02:00
|
|
|
uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
|
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
uint8_t *p = packet;
|
|
|
|
uint16_t len;
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p[0] = MQTT_CTRL_UNSUBSCRIBE << 4 | 0x1;
|
|
|
|
// fill in packet[1] last
|
2020-06-25 15:56:46 +02:00
|
|
|
p += 2;
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// packet identifier. used for checking UNSUBACK
|
|
|
|
p[0] = (packet_id_counter >> 8) & 0xFF;
|
|
|
|
p[1] = packet_id_counter & 0xFF;
|
2020-06-25 15:56:46 +02:00
|
|
|
p += 2;
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
// increment the packet id
|
|
|
|
packet_id_counter++;
|
2015-10-05 17:21:05 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
p = stringprint(p, topic);
|
2015-10-05 16:33:33 +02:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
len = p - packet;
|
2020-06-25 15:56:46 +02:00
|
|
|
packet[1] = len - 2; // don't include the 2 bytes of fixed header data
|
2020-02-24 07:28:48 +01:00
|
|
|
DEBUG_PRINTLN(F("MQTT unsubscription packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, len);
|
|
|
|
return len;
|
2015-10-05 16:33:33 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
uint8_t Adafruit_MQTT::pingPacket(uint8_t *packet) {
|
2020-02-24 07:28:48 +01:00
|
|
|
packet[0] = MQTT_CTRL_PINGREQ << 4;
|
|
|
|
packet[1] = 0;
|
|
|
|
DEBUG_PRINTLN(F("MQTT ping packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, 2);
|
|
|
|
return 2;
|
2015-06-05 06:16:08 +02:00
|
|
|
}
|
|
|
|
|
2016-02-02 00:42:28 +01:00
|
|
|
uint8_t Adafruit_MQTT::pubackPacket(uint8_t *packet, uint16_t packetid) {
|
2020-02-24 07:28:48 +01:00
|
|
|
packet[0] = MQTT_CTRL_PUBACK << 4;
|
|
|
|
packet[1] = 2;
|
|
|
|
packet[2] = packetid >> 8;
|
|
|
|
packet[3] = packetid;
|
|
|
|
DEBUG_PRINTLN(F("MQTT puback packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, 4);
|
|
|
|
return 4;
|
2016-02-02 00:42:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-05 20:18:31 +02:00
|
|
|
uint8_t Adafruit_MQTT::disconnectPacket(uint8_t *packet) {
|
2020-02-24 07:28:48 +01:00
|
|
|
packet[0] = MQTT_CTRL_DISCONNECT << 4;
|
|
|
|
packet[1] = 0;
|
|
|
|
DEBUG_PRINTLN(F("MQTT disconnect packet:"));
|
|
|
|
DEBUG_PRINTBUFFER(buffer, 2);
|
|
|
|
return 2;
|
2015-10-05 20:18:31 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Adafruit_MQTT_Publish Definition ////////////////////////////////////////////
|
2015-06-01 05:19:13 +02:00
|
|
|
|
2016-01-18 02:27:20 +01:00
|
|
|
Adafruit_MQTT_Publish::Adafruit_MQTT_Publish(Adafruit_MQTT *mqttserver,
|
2020-02-24 07:28:48 +01:00
|
|
|
const char *feed, uint8_t q) {
|
|
|
|
mqtt = mqttserver;
|
|
|
|
topic = feed;
|
|
|
|
qos = q;
|
2015-06-01 00:38:33 +02:00
|
|
|
}
|
2015-06-01 05:19:13 +02:00
|
|
|
bool Adafruit_MQTT_Publish::publish(int32_t i) {
|
2020-02-24 07:28:48 +01:00
|
|
|
char payload[12];
|
|
|
|
ltoa(i, payload, 10);
|
|
|
|
return mqtt->publish(topic, payload, qos);
|
2015-06-01 05:19:13 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 19:52:23 +02:00
|
|
|
bool Adafruit_MQTT_Publish::publish(uint32_t i) {
|
2020-02-24 07:28:48 +01:00
|
|
|
char payload[11];
|
|
|
|
ultoa(i, payload, 10);
|
|
|
|
return mqtt->publish(topic, payload, qos);
|
2015-06-11 00:38:34 +02:00
|
|
|
}
|
|
|
|
|
2016-07-13 19:52:23 +02:00
|
|
|
bool Adafruit_MQTT_Publish::publish(double f, uint8_t precision) {
|
2020-06-25 15:56:46 +02:00
|
|
|
char payload[41]; // Need to technically hold float max, 39 digits and minus
|
|
|
|
// sign.
|
2020-02-24 07:28:48 +01:00
|
|
|
dtostrf(f, 0, precision, payload);
|
|
|
|
return mqtt->publish(topic, payload, qos);
|
2015-06-01 05:19:13 +02:00
|
|
|
}
|
2015-06-05 06:16:08 +02:00
|
|
|
|
2015-07-02 12:59:46 +02:00
|
|
|
bool Adafruit_MQTT_Publish::publish(const char *payload) {
|
2020-02-24 07:28:48 +01:00
|
|
|
return mqtt->publish(topic, payload, qos);
|
2015-06-02 21:23:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
// publish buffer of arbitrary length
|
2020-02-24 07:28:48 +01:00
|
|
|
bool Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen) {
|
2016-01-19 23:10:03 +01:00
|
|
|
|
2020-02-24 07:28:48 +01:00
|
|
|
return mqtt->publish(topic, payload, bLen, qos);
|
2016-01-18 02:27:20 +01:00
|
|
|
}
|
|
|
|
|
2015-06-05 06:16:08 +02:00
|
|
|
// Adafruit_MQTT_Subscribe Definition //////////////////////////////////////////
|
|
|
|
|
2016-01-18 02:27:20 +01:00
|
|
|
Adafruit_MQTT_Subscribe::Adafruit_MQTT_Subscribe(Adafruit_MQTT *mqttserver,
|
2020-02-24 07:28:48 +01:00
|
|
|
const char *feed, uint8_t q) {
|
|
|
|
mqtt = mqttserver;
|
|
|
|
topic = feed;
|
|
|
|
qos = q;
|
|
|
|
datalen = 0;
|
|
|
|
callback_uint32t = 0;
|
|
|
|
callback_buffer = 0;
|
|
|
|
callback_double = 0;
|
|
|
|
callback_io = 0;
|
|
|
|
io_mqtt = 0;
|
2015-06-03 00:33:03 +02:00
|
|
|
}
|
2015-07-24 11:29:22 +02:00
|
|
|
|
2016-07-08 23:01:40 +02:00
|
|
|
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackUInt32Type cb) {
|
2020-02-24 07:28:48 +01:00
|
|
|
callback_uint32t = cb;
|
2016-07-08 23:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackDoubleType cb) {
|
2020-02-24 07:28:48 +01:00
|
|
|
callback_double = cb;
|
2016-02-09 05:36:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-08 23:01:40 +02:00
|
|
|
void Adafruit_MQTT_Subscribe::setCallback(SubscribeCallbackBufferType cb) {
|
2020-02-24 07:28:48 +01:00
|
|
|
callback_buffer = cb;
|
2016-02-09 05:36:25 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:56:46 +02:00
|
|
|
void Adafruit_MQTT_Subscribe::setCallback(AdafruitIO_MQTT *io,
|
|
|
|
SubscribeCallbackIOType cb) {
|
2020-02-24 07:28:48 +01:00
|
|
|
callback_io = cb;
|
2020-06-25 15:56:46 +02:00
|
|
|
io_mqtt = io;
|
2016-07-29 15:27:00 +02:00
|
|
|
}
|
|
|
|
|
2016-02-09 05:36:25 +01:00
|
|
|
void Adafruit_MQTT_Subscribe::removeCallback(void) {
|
2020-02-24 07:28:48 +01:00
|
|
|
callback_uint32t = 0;
|
|
|
|
callback_buffer = 0;
|
|
|
|
callback_double = 0;
|
|
|
|
callback_io = 0;
|
|
|
|
io_mqtt = 0;
|
2015-07-24 11:29:22 +02:00
|
|
|
}
|