Paho MQTT Embedded C client: Use submodule pointing to upstream repo

Due to code organisation of upstream repo, MQTTClient still needs
copying into our repo.
This commit is contained in:
Angus Gratton 2016-05-28 16:23:13 +10:00
parent 9b21c54fc5
commit 42880fded5
23 changed files with 240 additions and 1828 deletions

View file

@ -13,26 +13,25 @@
* 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"
void NewMessageData(MessageData* md, MQTTString* aTopicName, MQTTMessage* aMessgage) {
md->topic = aTopicName;
void NewMessageData(MessageData* md, MQTTString* aTopicName, MQTTMessage* aMessgage) {
md->topicName = aTopicName;
md->message = aMessgage;
}
int getNextPacketId(MQTTClient *c) {
int getNextPacketId(Client *c) {
return c->next_packetid = (c->next_packetid == MAX_PACKET_ID) ? 1 : c->next_packetid + 1;
}
int sendPacket(MQTTClient* c, int length, Timer* timer)
int sendPacket(Client* c, int length, Timer* timer)
{
int rc = FAILURE,
int rc = FAILURE,
sent = 0;
while (sent < length && !expired(timer))
{
rc = c->ipstack->mqttwrite(c->ipstack, &c->buf[sent], length, left_ms(timer));
@ -42,7 +41,7 @@ int sendPacket(MQTTClient* c, int length, Timer* timer)
}
if (sent == length)
{
countdown(&(c->ping_timer), c->keepAliveInterval); // record the fact that we have successfully sent the packet
countdown(&c->ping_timer, c->keepAliveInterval); // record the fact that we have successfully sent the packet
rc = SUCCESS;
}
else
@ -51,7 +50,26 @@ int sendPacket(MQTTClient* c, int length, Timer* timer)
}
int decodePacket(MQTTClient* c, int* value, int timeout)
void MQTTClient(Client* c, Network* network, unsigned int command_timeout_ms, unsigned char* buf, size_t buf_size, unsigned char* readbuf, size_t readbuf_size)
{
int i;
c->ipstack = network;
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
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->defaultMessageHandler = NULL;
InitTimer(&c->ping_timer);
}
int decodePacket(Client* c, int* value, int timeout)
{
unsigned char i;
int multiplier = 1;
@ -79,7 +97,7 @@ exit:
}
int readPacket(MQTTClient* c, Timer* timer)
int readPacket(Client* c, Timer* timer)
{
int rc = FAILURE;
MQTTHeader header = {0};
@ -102,7 +120,6 @@ int readPacket(MQTTClient* c, Timer* timer)
header.byte = c->readbuf[0];
rc = header.bits.type;
exit:
//dmsg_printf("readPacket=%d\r\n", rc);
return rc;
}
@ -110,12 +127,12 @@ exit:
// assume topic filter and name is in correct format
// # can only be at end
// + and # can only be next to separator
char isTopicMatched(char* topicFilter, MQTTString* topicName)
char isTopicMatched(char* topicFilter, MQTTString* topicName)
{
char* curf = topicFilter;
char* curn = topicName->lenstring.data;
char* curn_end = curn + topicName->lenstring.len;
while (*curf && curn < curn_end)
{
if (*curn == '/' && *curf != '/')
@ -133,12 +150,12 @@ char isTopicMatched(char* topicFilter, MQTTString* topicName)
curf++;
curn++;
};
return (curn == curn_end) && (*curf == '\0');
}
int deliverMessage(MQTTClient* c, MQTTString* topicName, MQTTMessage* message)
int deliverMessage(Client* c, MQTTString* topicName, MQTTMessage* message)
{
int i;
int rc = FAILURE;
@ -158,22 +175,22 @@ int deliverMessage(MQTTClient* c, MQTTString* topicName, MQTTMessage* message)
}
}
}
if (rc == FAILURE && c->defaultMessageHandler != NULL)
if (rc == FAILURE && c->defaultMessageHandler != NULL)
{
MessageData md;
NewMessageData(&md, topicName, message);
c->defaultMessageHandler(&md);
rc = SUCCESS;
}
}
return rc;
}
int keepalive(MQTTClient* c)
int keepalive(Client* c)
{
int rc = SUCCESS;
int rc = FAILURE;
if (c->keepAliveInterval == 0)
{
@ -181,30 +198,17 @@ int keepalive(MQTTClient* c)
goto exit;
}
if (expired(&(c->ping_timer)))
if (expired(&c->ping_timer))
{
if (c->ping_outstanding)
{
// if ping failure accumulated above MAX_FAIL_ALLOWED, the connection is broken
++(c->fail_count);
if (c->fail_count >= MAX_FAIL_ALLOWED)
{
rc = DISCONNECTED;
goto exit;
}
}
else
if (!c->ping_outstanding)
{
Timer timer;
InitTimer(&timer);
countdown_ms(&timer, 1000);
c->ping_outstanding = 1;
int len = MQTTSerialize_pingreq(c->buf, c->buf_size);
if (len > 0)
sendPacket(c, len, &timer);
if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) // send the ping packet
c->ping_outstanding = 1;
}
// re-arm ping counter
countdown(&(c->ping_timer), c->keepAliveInterval);
}
exit:
@ -212,11 +216,11 @@ exit:
}
int cycle(MQTTClient* c, Timer* timer)
int cycle(Client* c, Timer* timer)
{
// read the socket, see what work is due
unsigned short packet_type = readPacket(c, timer);
int len = 0,
rc = SUCCESS;
@ -242,8 +246,8 @@ int cycle(MQTTClient* c, Timer* timer)
len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREC, 0, msg.id);
if (len <= 0)
rc = FAILURE;
else
rc = sendPacket(c, len, timer);
else
rc = sendPacket(c, len, timer);
if (rc == FAILURE)
goto exit; // there was a problem
}
@ -266,14 +270,10 @@ int cycle(MQTTClient* c, Timer* timer)
case PUBCOMP:
break;
case PINGRESP:
{
c->ping_outstanding = 0;
c->fail_count = 0;
}
c->ping_outstanding = 0;
break;
}
if (c->isconnected)
rc = keepalive(c);
keepalive(c);
exit:
if (rc == SUCCESS)
rc = packet_type;
@ -281,69 +281,49 @@ exit:
}
void NewMQTTClient(MQTTClient* c, Network* network, unsigned int command_timeout_ms, unsigned char* buf, size_t buf_size, unsigned char* readbuf, size_t readbuf_size)
{
int i;
c->ipstack = network;
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
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;
InitTimer(&(c->ping_timer));
}
int MQTTYield(MQTTClient* c, int timeout_ms)
int MQTTYield(Client* c, int timeout_ms)
{
int rc = SUCCESS;
Timer timer;
InitTimer(&timer);
InitTimer(&timer);
countdown_ms(&timer, timeout_ms);
while (!expired(&timer))
{
rc = cycle(c, &timer);
// cycle could return 0 or packet_type or 65535 if nothing is read
// cycle returns DISCONNECTED only if keepalive() fails.
if (rc == DISCONNECTED)
if (cycle(c, &timer) == FAILURE)
{
rc = FAILURE;
break;
rc = SUCCESS;
}
}
return rc;
}
// only used in single-threaded mode where one command at a time is in process
int waitfor(MQTTClient* c, int packet_type, Timer* timer)
int waitfor(Client* c, int packet_type, Timer* timer)
{
int rc = FAILURE;
do
{
if (expired(timer))
if (expired(timer))
break; // we timed out
}
while ((rc = cycle(c, timer)) != packet_type);
while ((rc = cycle(c, timer)) != packet_type);
return rc;
}
int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
int MQTTConnect(Client* c, MQTTPacket_connectData* options)
{
Timer connect_timer;
int rc = FAILURE;
MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
int len = 0;
InitTimer(&connect_timer);
countdown_ms(&connect_timer, c->command_timeout_ms);
@ -352,15 +332,14 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
if (options == 0)
options = &default_options; // set default options if none were supplied
c->keepAliveInterval = options->keepAliveInterval;
countdown(&(c->ping_timer), c->keepAliveInterval);
countdown(&c->ping_timer, c->keepAliveInterval);
if ((len = MQTTSerialize_connect(c->buf, c->buf_size, options)) <= 0)
goto exit;
if ((rc = sendPacket(c, len, &connect_timer)) != SUCCESS) // send the connect packet
goto exit; // there was a problem
// this will be a blocking call, wait for the connack
if (waitfor(c, CONNACK, &connect_timer) == CONNACK)
{
@ -373,7 +352,7 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
}
else
rc = FAILURE;
exit:
if (rc == SUCCESS)
c->isconnected = 1;
@ -381,33 +360,32 @@ exit:
}
int MQTTSubscribe(MQTTClient* c, const char* topic, enum QoS qos, messageHandler handler)
{
int rc = FAILURE;
int MQTTSubscribe(Client* c, const char* topicFilter, enum QoS qos, messageHandler messageHandler)
{
int rc = FAILURE;
Timer timer;
int len = 0;
MQTTString topicStr = MQTTString_initializer;
topicStr.cstring = (char *)topic;
MQTTString topic = MQTTString_initializer;
topic.cstring = (char *)topicFilter;
InitTimer(&timer);
countdown_ms(&timer, c->command_timeout_ms);
if (!c->isconnected)
goto exit;
len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topicStr, (int*)&qos);
len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int*)&qos);
if (len <= 0)
goto exit;
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
{
goto exit; // there was a problem
}
if (waitfor(c, SUBACK, &timer) == SUBACK) // wait for suback
if (waitfor(c, SUBACK, &timer) == SUBACK) // wait for suback
{
int count = 0, grantedQoS = -1;
unsigned short mypacketid;
if (MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size) == 1)
rc = grantedQoS; // 0, 1, 2 or 0x80
rc = grantedQoS; // 0, 1, 2 or 0x80
if (rc != 0x80)
{
int i;
@ -415,126 +393,111 @@ int MQTTSubscribe(MQTTClient* c, const char* topic, enum QoS qos, messageHandle
{
if (c->messageHandlers[i].topicFilter == 0)
{
c->messageHandlers[i].topicFilter = topic;
c->messageHandlers[i].fp = handler;
c->messageHandlers[i].topicFilter = topicFilter;
c->messageHandlers[i].fp = messageHandler;
rc = 0;
break;
}
}
}
}
else
else
rc = FAILURE;
exit:
return rc;
}
int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
{
int MQTTUnsubscribe(Client* c, const char* topicFilter)
{
int rc = FAILURE;
Timer timer;
Timer timer;
MQTTString topic = MQTTString_initializer;
topic.cstring = (char *)topicFilter;
int len = 0;
InitTimer(&timer);
countdown_ms(&timer, c->command_timeout_ms);
if (!c->isconnected)
goto exit;
if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
goto exit;
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
goto exit; // there was a problem
if (waitfor(c, UNSUBACK, &timer) == UNSUBACK)
{
unsigned short mypacketid; // should be the same as the packetid above
if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1)
rc = 0;
rc = 0;
}
else
rc = FAILURE;
exit:
return rc;
}
int MQTTPublish(MQTTClient* c, const char* topic, MQTTMessage* message)
int MQTTPublish(Client* c, const char* topicName, MQTTMessage* message)
{
int rc = FAILURE;
Timer timer;
MQTTString topicStr = MQTTString_initializer;
topicStr.cstring = (char *)topic;
Timer timer;
MQTTString topic = MQTTString_initializer;
topic.cstring = (char *)topicName;
int len = 0;
InitTimer(&timer);
countdown_ms(&timer, c->command_timeout_ms);
if (!c->isconnected)
goto exit;
if (message->qos == QOS1 || message->qos == QOS2)
message->id = getNextPacketId(c);
len = MQTTSerialize_publish(c->buf, c->buf_size, 0, message->qos, message->retained, message->id,
topicStr, (unsigned char*)message->payload, message->payloadlen);
topic, (unsigned char*)message->payload, message->payloadlen);
if (len <= 0)
goto exit;
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
{
goto exit; // there was a problem
}
if (message->qos == QOS1)
{
if (waitfor(c, PUBACK, &timer) == PUBACK)
{
// We still can receive from broker, treat as recoverable
c->fail_count = 0;
unsigned short mypacketid;
unsigned char dup, type;
if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
rc = FAILURE;
else
rc = SUCCESS;
}
else
{
rc = FAILURE;
}
}
else if (message->qos == QOS2)
{
if (waitfor(c, PUBCOMP, &timer) == PUBCOMP)
{
// We still can receive from broker, treat as recoverable
c->fail_count = 0;
unsigned short mypacketid;
unsigned char dup, type;
if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
rc = FAILURE;
else
rc = SUCCESS;
}
else
{
rc = FAILURE;
}
}
exit:
return rc;
}
int MQTTDisconnect(MQTTClient* c)
{
int MQTTDisconnect(Client* c)
{
int rc = FAILURE;
Timer timer; // we might wait for incomplete incoming publishes to complete
int len = MQTTSerialize_disconnect(c->buf, c->buf_size);
@ -544,7 +507,7 @@ int MQTTDisconnect(MQTTClient* c)
if (len > 0)
rc = sendPacket(c, len, &timer); // send the disconnect packet
c->isconnected = 0;
return rc;
}