2016-01-02 07:41:28 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2014 IBM Corp.
|
|
|
|
*
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
|
|
|
*
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
* and the Eclipse Distribution License is available at
|
|
|
|
* http://www.eclipse.org/org/documents/edl-v10.php.
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
|
|
|
|
*******************************************************************************/
|
|
|
|
#include <espressif/esp_common.h>
|
|
|
|
#include <lwip/arch.h>
|
|
|
|
#include "MQTTClient.h"
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static void new_message_data(mqtt_message_data_t* md, mqtt_string_t* aTopicName, mqtt_message_t* aMessgage) {
|
2016-01-02 07:41:28 +00:00
|
|
|
md->topic = aTopicName;
|
|
|
|
md->message = aMessgage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static int get_next_packet_id(mqtt_client_t *c) {
|
|
|
|
return c->next_packetid = (c->next_packetid == MQTT_MAX_PACKET_ID) ? 1 : c->next_packetid + 1;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static int send_packet(mqtt_client_t* c, int length, mqtt_timer_t* timer)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE,
|
2016-01-02 07:41:28 +00:00
|
|
|
sent = 0;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
while (sent < length && !mqtt_timer_expired(timer))
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = c->ipstack->mqttwrite(c->ipstack, &c->buf[sent], length - sent, mqtt_timer_left_ms(timer));
|
2016-01-02 07:41:28 +00:00
|
|
|
if (rc < 0) // there was an error writing the data
|
|
|
|
break;
|
|
|
|
sent += rc;
|
|
|
|
}
|
|
|
|
if (sent == length)
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_countdown(&(c->ping_timer), c->keepAliveInterval); // record the fact that we have successfully sent the packet
|
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static int decode_packet(mqtt_client_t* c, int* value, int timeout)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
unsigned char i;
|
|
|
|
int multiplier = 1;
|
|
|
|
int len = 0;
|
|
|
|
const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
|
|
|
|
|
|
|
|
*value = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int rc = MQTTPACKET_READ_ERROR;
|
|
|
|
|
|
|
|
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
|
|
|
{
|
|
|
|
rc = MQTTPACKET_READ_ERROR; /* bad data */
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
rc = c->ipstack->mqttread(c->ipstack, &i, 1, timeout);
|
|
|
|
if (rc != 1)
|
2016-07-05 13:37:47 +00:00
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
2016-01-02 07:41:28 +00:00
|
|
|
*value += (i & 127) * multiplier;
|
|
|
|
multiplier *= 128;
|
|
|
|
} while ((i & 128) != 0);
|
|
|
|
exit:
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-05 13:37:47 +00:00
|
|
|
// Return packet type. If no packet avilable, return FAILURE, or READ_ERROR if timeout
|
2016-09-15 17:52:57 +00:00
|
|
|
static int read_packet(mqtt_client_t* c, mqtt_timer_t* timer)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
|
|
|
mqtt_header_t header = {0};
|
2016-01-02 07:41:28 +00:00
|
|
|
int len = 0;
|
|
|
|
int rem_len = 0;
|
|
|
|
|
|
|
|
/* 1. read the header byte. This has the packet type in it */
|
2016-09-15 17:52:57 +00:00
|
|
|
if (c->ipstack->mqttread(c->ipstack, c->readbuf, 1, mqtt_timer_left_ms(timer)) != 1)
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
|
|
|
len = 1;
|
|
|
|
/* 2. read the remaining length. This is variable in itself */
|
2016-09-15 17:52:57 +00:00
|
|
|
len += decode_packet(c, &rem_len, mqtt_timer_left_ms(timer));
|
2016-08-29 17:55:32 +00:00
|
|
|
if (len <= 1 || len + rem_len > c->readbuf_size) /* if packet is too big to fit in our readbuf, abort */
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_READ_ERROR;
|
2016-08-29 17:55:32 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_packet_encode(c->readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
|
2016-01-02 07:41:28 +00:00
|
|
|
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
|
2016-09-15 17:52:57 +00:00
|
|
|
if (rem_len > 0 && (c->ipstack->mqttread(c->ipstack, c->readbuf + len, rem_len, mqtt_timer_left_ms(timer)) != rem_len))
|
2016-07-05 13:37:47 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_READ_ERROR;
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
2016-07-05 13:37:47 +00:00
|
|
|
}
|
2016-01-02 07:41:28 +00:00
|
|
|
header.byte = c->readbuf[0];
|
|
|
|
rc = header.bits.type;
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// assume topic filter and name is in correct format
|
|
|
|
// # can only be at end
|
|
|
|
// + and # can only be next to separator
|
2016-09-15 17:52:57 +00:00
|
|
|
static char is_topic_matched(char* topicFilter, mqtt_string_t* topicName)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
char* curf = topicFilter;
|
|
|
|
char* curn = topicName->lenstring.data;
|
|
|
|
char* curn_end = curn + topicName->lenstring.len;
|
|
|
|
|
|
|
|
while (*curf && curn < curn_end)
|
|
|
|
{
|
|
|
|
if (*curn == '/' && *curf != '/')
|
|
|
|
break;
|
|
|
|
if (*curf != '+' && *curf != '#' && *curf != *curn)
|
|
|
|
break;
|
|
|
|
if (*curf == '+')
|
|
|
|
{ // skip until we meet the next separator, or end of string
|
|
|
|
char* nextpos = curn + 1;
|
|
|
|
while (nextpos < curn_end && *nextpos != '/')
|
|
|
|
nextpos = ++curn + 1;
|
|
|
|
}
|
|
|
|
else if (*curf == '#')
|
|
|
|
curn = curn_end - 1; // skip until end of string
|
|
|
|
curf++;
|
|
|
|
curn++;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (curn == curn_end) && (*curf == '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static int deliver_message(mqtt_client_t* c, mqtt_string_t* topicName, mqtt_message_t* message)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
int i;
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
// we have to find the right message handler - indexed by topic
|
2016-09-15 17:52:57 +00:00
|
|
|
for (i = 0; i < MQTT_MAX_MESSAGE_HANDLERS; ++i)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
if (c->messageHandlers[i].topicFilter != 0 && (mqtt_packet_equals(topicName, (char*)c->messageHandlers[i].topicFilter) ||
|
|
|
|
is_topic_matched((char*)c->messageHandlers[i].topicFilter, topicName)))
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
if (c->messageHandlers[i].fp != NULL)
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_message_data_t md;
|
|
|
|
new_message_data(&md, topicName, message);
|
2016-01-02 07:41:28 +00:00
|
|
|
c->messageHandlers[i].fp(&md);
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (rc == MQTT_FAILURE && c->defaultMessageHandler != NULL)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_message_data_t md;
|
|
|
|
new_message_data(&md, topicName, message);
|
2016-01-02 07:41:28 +00:00
|
|
|
c->defaultMessageHandler(&md);
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static int keepalive(mqtt_client_t* c)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
if (c->keepAliveInterval == 0)
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_timer_expired(&(c->ping_timer)))
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
if (c->ping_outstanding)
|
|
|
|
{
|
|
|
|
// if ping failure accumulated above MAX_FAIL_ALLOWED, the connection is broken
|
|
|
|
++(c->fail_count);
|
2016-09-15 17:52:57 +00:00
|
|
|
if (c->fail_count >= MQTT_MAX_FAIL_ALLOWED)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_DISCONNECTED;
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_t timer;
|
|
|
|
mqtt_timer_init(&timer);
|
|
|
|
mqtt_timer_countdown_ms(&timer, 1000);
|
2016-01-02 07:41:28 +00:00
|
|
|
c->ping_outstanding = 1;
|
2016-09-15 17:52:57 +00:00
|
|
|
int len = mqtt_serialize_pingreq(c->buf, c->buf_size);
|
2016-01-02 07:41:28 +00:00
|
|
|
if (len > 0)
|
2016-09-15 17:52:57 +00:00
|
|
|
send_packet(c, len, &timer);
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
// re-arm ping counter
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_countdown(&(c->ping_timer), c->keepAliveInterval);
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
static int cycle(mqtt_client_t* c, mqtt_timer_t* timer)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
// read the socket, see what work is due
|
2016-09-15 17:52:57 +00:00
|
|
|
int packet_type = read_packet(c, timer);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
int len = 0,
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
switch (packet_type)
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
case MQTTPACKET_CONNACK:
|
|
|
|
case MQTTPACKET_PUBACK:
|
|
|
|
case MQTTPACKET_SUBACK:
|
2016-01-02 07:41:28 +00:00
|
|
|
break;
|
2016-09-15 17:52:57 +00:00
|
|
|
case MQTTPACKET_PUBLISH:
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_string_t topicName;
|
|
|
|
mqtt_message_t msg;
|
|
|
|
if (mqtt_deserialize_publish((unsigned char*)&msg.dup, (int*)&msg.qos, (unsigned char*)&msg.retained, (unsigned short*)&msg.id, &topicName,
|
2016-01-02 07:41:28 +00:00
|
|
|
(unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->readbuf, c->readbuf_size) != 1)
|
|
|
|
goto exit;
|
2016-09-15 17:52:57 +00:00
|
|
|
deliver_message(c, &topicName, &msg);
|
|
|
|
if (msg.qos != MQTT_QOS0)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
if (msg.qos == MQTT_QOS1)
|
|
|
|
len = mqtt_serialize_ack(c->buf, c->buf_size, MQTTPACKET_PUBACK, 0, msg.id);
|
|
|
|
else if (msg.qos == MQTT_QOS2)
|
|
|
|
len = mqtt_serialize_ack(c->buf, c->buf_size, MQTTPACKET_PUBREC, 0, msg.id);
|
2016-01-02 07:41:28 +00:00
|
|
|
if (len <= 0)
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = send_packet(c, len, timer);
|
|
|
|
if (rc == MQTT_FAILURE)
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit; // there was a problem
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-09-15 17:52:57 +00:00
|
|
|
case MQTTPACKET_PUBREC:
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
unsigned short mypacketid;
|
|
|
|
unsigned char dup, type;
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_deserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
|
|
|
|
rc = MQTT_FAILURE;
|
|
|
|
else if ((len = mqtt_serialize_ack(c->buf, c->buf_size, MQTTPACKET_PUBREL, 0, mypacketid)) <= 0)
|
|
|
|
rc = MQTT_FAILURE;
|
|
|
|
else if ((rc = send_packet(c, len, timer)) != MQTT_SUCCESS) // send the PUBREL packet
|
|
|
|
rc = MQTT_FAILURE; // there was a problem
|
|
|
|
if (rc == MQTT_FAILURE)
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit; // there was a problem
|
|
|
|
break;
|
|
|
|
}
|
2016-09-15 17:52:57 +00:00
|
|
|
case MQTTPACKET_PUBCOMP:
|
2016-01-02 07:41:28 +00:00
|
|
|
break;
|
2016-09-15 17:52:57 +00:00
|
|
|
case MQTTPACKET_PINGRESP:
|
2016-07-05 13:37:47 +00:00
|
|
|
{
|
|
|
|
c->ping_outstanding = 0;
|
|
|
|
c->fail_count = 0;
|
2016-01-02 07:41:28 +00:00
|
|
|
break;
|
2016-07-05 13:37:47 +00:00
|
|
|
}
|
2016-09-15 17:52:57 +00:00
|
|
|
case MQTT_READ_ERROR:
|
2016-07-05 13:37:47 +00:00
|
|
|
{
|
|
|
|
c->isconnected = 0; // we simulate a disconnect if reading error
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_DISCONNECTED; // so that the outer layer will reconnect and recover
|
2016-07-05 13:37:47 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
if (c->isconnected)
|
|
|
|
rc = keepalive(c);
|
|
|
|
exit:
|
2016-09-15 17:52:57 +00:00
|
|
|
if (rc == MQTT_SUCCESS)
|
2016-01-02 07:41:28 +00:00
|
|
|
rc = packet_type;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
void mqtt_client_new(mqtt_client_t* c, mqtt_network_t* network, unsigned int command_timeout_ms, unsigned char* buf, size_t buf_size, unsigned char* readbuf, size_t readbuf_size)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
c->ipstack = network;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
for (i = 0; i < MQTT_MAX_MESSAGE_HANDLERS; ++i)
|
2016-01-02 07:41:28 +00:00
|
|
|
c->messageHandlers[i].topicFilter = 0;
|
|
|
|
c->command_timeout_ms = command_timeout_ms;
|
|
|
|
c->buf = buf;
|
|
|
|
c->buf_size = buf_size;
|
|
|
|
c->readbuf = readbuf;
|
|
|
|
c->readbuf_size = readbuf_size;
|
|
|
|
c->isconnected = 0;
|
|
|
|
c->ping_outstanding = 0;
|
|
|
|
c->fail_count = 0;
|
|
|
|
c->defaultMessageHandler = NULL;
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&(c->ping_timer));
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_yield(mqtt_client_t* c, int timeout_ms)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_SUCCESS;
|
|
|
|
mqtt_timer_t timer;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&timer);
|
|
|
|
mqtt_timer_countdown_ms(&timer, timeout_ms);
|
|
|
|
while (!mqtt_timer_expired(&timer))
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
rc = cycle(c, &timer);
|
|
|
|
// cycle could return 0 or packet_type or 65535 if nothing is read
|
|
|
|
// cycle returns DISCONNECTED only if keepalive() fails.
|
2016-09-15 17:52:57 +00:00
|
|
|
if (rc == MQTT_DISCONNECTED)
|
2016-01-02 07:41:28 +00:00
|
|
|
break;
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// only used in single-threaded mode where one command at a time is in process
|
2016-09-15 17:52:57 +00:00
|
|
|
static int waitfor(mqtt_client_t* c, int packet_type, mqtt_timer_t* timer)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_timer_expired(timer))
|
2016-01-02 07:41:28 +00:00
|
|
|
break; // we timed out
|
|
|
|
}
|
|
|
|
while ((rc = cycle(c, timer)) != packet_type);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_connect(mqtt_client_t* c, mqtt_packet_connect_data_t* options)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_t connect_timer;
|
|
|
|
int rc = MQTT_FAILURE;
|
|
|
|
mqtt_packet_connect_data_t default_options = mqtt_packet_connect_data_initializer;
|
2016-01-02 07:41:28 +00:00
|
|
|
int len = 0;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&connect_timer);
|
|
|
|
mqtt_timer_countdown_ms(&connect_timer, c->command_timeout_ms);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
if (c->isconnected) // don't send connect packet again if we are already connected
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (options == 0)
|
|
|
|
options = &default_options; // set default options if none were supplied
|
|
|
|
|
|
|
|
c->keepAliveInterval = options->keepAliveInterval;
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_countdown(&(c->ping_timer), c->keepAliveInterval);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if ((len = mqtt_serialize_connect(c->buf, c->buf_size, options)) <= 0)
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
2016-09-15 17:52:57 +00:00
|
|
|
if ((rc = send_packet(c, len, &connect_timer)) != MQTT_SUCCESS) // send the connect packet
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit; // there was a problem
|
|
|
|
|
|
|
|
// this will be a blocking call, wait for the connack
|
2016-09-15 17:52:57 +00:00
|
|
|
if (waitfor(c, MQTTPACKET_CONNACK, &connect_timer) == MQTTPACKET_CONNACK)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
unsigned char connack_rc = 255;
|
|
|
|
char sessionPresent = 0;
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_deserialize_connack((unsigned char*)&sessionPresent, &connack_rc, c->readbuf, c->readbuf_size) == 1)
|
2016-01-02 07:41:28 +00:00
|
|
|
rc = connack_rc;
|
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
exit:
|
2016-09-15 17:52:57 +00:00
|
|
|
if (rc == MQTT_SUCCESS)
|
2016-01-02 07:41:28 +00:00
|
|
|
c->isconnected = 1;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_subscribe(mqtt_client_t* c, const char* topic, enum mqtt_qos qos, mqtt_message_handler_t handler)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
|
|
|
mqtt_timer_t timer;
|
2016-01-02 07:41:28 +00:00
|
|
|
int len = 0;
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_string_t topicStr = mqtt_string_initializer;
|
2016-01-02 07:41:28 +00:00
|
|
|
topicStr.cstring = (char *)topic;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&timer);
|
|
|
|
mqtt_timer_countdown_ms(&timer, c->command_timeout_ms);
|
2016-01-02 07:41:28 +00:00
|
|
|
if (!c->isconnected)
|
|
|
|
goto exit;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
len = mqtt_serialize_subscribe(c->buf, c->buf_size, 0, get_next_packet_id(c), 1, &topicStr, (int*)&qos);
|
2016-01-02 07:41:28 +00:00
|
|
|
if (len <= 0)
|
|
|
|
goto exit;
|
2016-09-15 17:52:57 +00:00
|
|
|
if ((rc = send_packet(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
goto exit; // there was a problem
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (waitfor(c, MQTTPACKET_SUBACK, &timer) == MQTTPACKET_SUBACK) // wait for suback
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
int count = 0, grantedQoS = -1;
|
|
|
|
unsigned short mypacketid;
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_deserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1)
|
2016-01-02 07:41:28 +00:00
|
|
|
rc = grantedQoS; // 0, 1, 2 or 0x80
|
|
|
|
if (rc != 0x80)
|
|
|
|
{
|
|
|
|
int i;
|
2016-09-15 17:52:57 +00:00
|
|
|
for (i = 0; i < MQTT_MAX_MESSAGE_HANDLERS; ++i)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
if (c->messageHandlers[i].topicFilter == 0)
|
|
|
|
{
|
|
|
|
c->messageHandlers[i].topicFilter = topic;
|
|
|
|
c->messageHandlers[i].fp = handler;
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_unsubscribe(mqtt_client_t* c, const char* topicFilter)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
|
|
|
mqtt_timer_t timer;
|
|
|
|
mqtt_string_t topic = mqtt_string_initializer;
|
2016-01-02 07:41:28 +00:00
|
|
|
topic.cstring = (char *)topicFilter;
|
|
|
|
int len = 0;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&timer);
|
|
|
|
mqtt_timer_countdown_ms(&timer, c->command_timeout_ms);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
if (!c->isconnected)
|
|
|
|
goto exit;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if ((len = mqtt_serialize_unsubscribe(c->buf, c->buf_size, 0, get_next_packet_id(c), 1, &topic)) <= 0)
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit;
|
2016-09-15 17:52:57 +00:00
|
|
|
if ((rc = send_packet(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet
|
2016-01-02 07:41:28 +00:00
|
|
|
goto exit; // there was a problem
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (waitfor(c, MQTTPACKET_UNSUBACK, &timer) == MQTTPACKET_UNSUBACK)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
unsigned short mypacketid; // should be the same as the packetid above
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_deserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1)
|
2016-01-02 07:41:28 +00:00
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_publish(mqtt_client_t* c, const char* topic, mqtt_message_t* message)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
|
|
|
mqtt_timer_t timer;
|
|
|
|
mqtt_string_t topicStr = mqtt_string_initializer;
|
2016-01-02 07:41:28 +00:00
|
|
|
topicStr.cstring = (char *)topic;
|
|
|
|
int len = 0;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&timer);
|
|
|
|
mqtt_timer_countdown_ms(&timer, c->command_timeout_ms);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
if (!c->isconnected)
|
|
|
|
goto exit;
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (message->qos == MQTT_QOS1 || message->qos == MQTT_QOS2)
|
|
|
|
message->id = get_next_packet_id(c);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
len = mqtt_serialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id,
|
2016-01-02 07:41:28 +00:00
|
|
|
topicStr, (unsigned char*)message->payload, message->payloadlen);
|
|
|
|
if (len <= 0)
|
|
|
|
goto exit;
|
2016-09-15 17:52:57 +00:00
|
|
|
if ((rc = send_packet(c, len, &timer)) != MQTT_SUCCESS) // send the subscribe packet
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
goto exit; // there was a problem
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
if (message->qos == MQTT_QOS1)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
if (waitfor(c, MQTTPACKET_PUBACK, &timer) == MQTTPACKET_PUBACK)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
// We still can receive from broker, treat as recoverable
|
|
|
|
c->fail_count = 0;
|
|
|
|
unsigned short mypacketid;
|
|
|
|
unsigned char dup, type;
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_deserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
|
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-09-15 17:52:57 +00:00
|
|
|
else if (message->qos == MQTT_QOS2)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
if (waitfor(c, MQTTPACKET_PUBCOMP, &timer) == MQTTPACKET_PUBCOMP)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
|
|
|
// We still can receive from broker, treat as recoverable
|
|
|
|
c->fail_count = 0;
|
|
|
|
unsigned short mypacketid;
|
|
|
|
unsigned char dup, type;
|
2016-09-15 17:52:57 +00:00
|
|
|
if (mqtt_deserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
|
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
else
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_SUCCESS;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = MQTT_FAILURE;
|
2016-01-02 07:41:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
int mqtt_disconnect(mqtt_client_t* c)
|
2016-01-02 07:41:28 +00:00
|
|
|
{
|
2016-09-15 17:52:57 +00:00
|
|
|
int rc = MQTT_FAILURE;
|
|
|
|
mqtt_timer_t timer; // we might wait for incomplete incoming publishes to complete
|
|
|
|
int len = mqtt_serialize_disconnect(c->buf, c->buf_size);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
2016-09-15 17:52:57 +00:00
|
|
|
mqtt_timer_init(&timer);
|
|
|
|
mqtt_timer_countdown_ms(&timer, c->command_timeout_ms);
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
if (len > 0)
|
2016-09-15 17:52:57 +00:00
|
|
|
rc = send_packet(c, len, &timer); // send the disconnect packet
|
2016-01-02 07:41:28 +00:00
|
|
|
|
|
|
|
c->isconnected = 0;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|