adds packet ids for publish QoS > 0

also adds packet id counter so packet ids aren't
static values. this is a step in the direction
of QoS 1 support
This commit is contained in:
Todd Treece 2015-10-05 11:21:05 -04:00
parent 765a2feaff
commit b9c9a28883
2 changed files with 24 additions and 8 deletions

View File

@ -225,7 +225,7 @@ bool Adafruit_MQTT::unsubscribe(Adafruit_MQTT_Subscribe *sub) {
// if QoS for this subscription is 1 or 2, we need
// to wait for the unsuback to confirm unsubscription
if(subscriptions[i]->qos > 0) {
if(subscriptions[i]->qos > 0 && MQTT_PROTOCOL_LEVEL > 3) {
// wait for UNSUBACK
len = readPacket(buffer, 5, CONNECT_TIMEOUT_MS);
@ -404,6 +404,16 @@ uint8_t Adafruit_MQTT::publishPacket(uint8_t *packet, const char *topic,
// fill in packet[1] last
p+=2;
// add packet identifier. used for checking PUBACK in QOS > 0
if(qos > 0) {
p[0] = (packet_id_counter >> 8) & 0xFF;
p[1] = packet_id_counter & 0xFF;
p+=2;
// increment the packet id
packet_id_counter++;
}
p = stringprint_P(p, topic);
memcpy(p, data, strlen(data));
@ -424,11 +434,14 @@ uint8_t Adafruit_MQTT::subscribePacket(uint8_t *packet, const char *topic,
// fill in packet[1] last
p+=2;
// put in a message id,
p[0] = 0xAD;
p[1] = 0xAF;
// packet identifier. used for checking SUBACK
p[0] = (packet_id_counter >> 8) & 0xFF;
p[1] = packet_id_counter & 0xFF;
p+=2;
// increment the packet id
packet_id_counter++;
p = stringprint_P(p, topic);
p[0] = qos;
@ -450,12 +463,14 @@ uint8_t Adafruit_MQTT::unsubscribePacket(uint8_t *packet, const char *topic) {
// fill in packet[1] last
p+=2;
// packet identifier. used for QoS > 1
// TODO: this shouldn't be a static value
p[0] = 0xAD;
p[1] = 0xAF;
// packet identifier. used for checking UNSUBACK
p[0] = (packet_id_counter >> 8) & 0xFF;
p[1] = packet_id_counter & 0xFF;
p+=2;
// increment the packet id
packet_id_counter++;
p = stringprint_P(p, topic);
len = p - packet;

View File

@ -193,6 +193,7 @@ class Adafruit_MQTT {
uint8_t will_qos;
uint8_t will_retain;
uint8_t buffer[MAXBUFFERSIZE]; // one buffer, used for all incoming/outgoing
uint16_t packet_id_counter = 0;
private:
Adafruit_MQTT_Subscribe *subscriptions[MAXSUBSCRIPTIONS];