fix: yet more wiznet bugs, reconnects, led status indicators

This commit is contained in:
2024-11-11 15:40:05 +06:00
parent 8fe50deeed
commit 5f9c966602
11 changed files with 208 additions and 63 deletions

View File

@@ -42,7 +42,7 @@ static int sendPacket(MQTTClient* c, int length, Timer* timer)
if (sent == length)
{
TimerCountdown(&c->ping_timer, c->keepAliveInterval); // record the fact that we have successfully sent the packet
rc = SUCCESSS;
rc = SUCCESS;
}
else
rc = FAILURE;
@@ -111,8 +111,13 @@ static int readPacket(MQTTClient* c, Timer* timer)
int rem_len = 0;
/* 1. read the header byte. This has the packet type in it */
if (c->ipstack->mqttread(c->ipstack, c->readbuf, 1, TimerLeftMS(timer)) != 1)
goto exit;
int read_len = c->ipstack->mqttread(c->ipstack, c->readbuf, 1, TimerLeftMS(timer));
if (read_len == 0) { // Timeout/no data
return SUCCESS;
}
if (read_len < 0) {
return FAILURE;
}
len = 1;
/* 2. read the remaining length. This is variable in itself */
@@ -177,7 +182,7 @@ int deliverMessage(MQTTClient* c, MQTTString* topicName, MQTTMessage* message)
MessageData md;
NewMessageData(&md, topicName, message);
c->messageHandlers[i].fp(&md);
rc = SUCCESSS;
rc = SUCCESS;
}
}
}
@@ -187,7 +192,7 @@ int deliverMessage(MQTTClient* c, MQTTString* topicName, MQTTMessage* message)
MessageData md;
NewMessageData(&md, topicName, message);
c->defaultMessageHandler(&md);
rc = SUCCESSS;
rc = SUCCESS;
}
return rc;
@@ -196,14 +201,16 @@ int deliverMessage(MQTTClient* c, MQTTString* topicName, MQTTMessage* message)
int keepalive(MQTTClient* c)
{
int rc = SUCCESSS;
int rc = SUCCESS;
if (c->keepAliveInterval == 0)
{
goto exit;
}
if (!c->isconnected)
{
rc = FAILURE;
// rc = FAILURE;
goto exit;
}
@@ -223,7 +230,7 @@ int keepalive(MQTTClient* c)
}
rc = sendPacket(c, len, &timer);
if (rc == SUCCESSS)
if (rc == SUCCESS)
{
c->ping_outstanding = 1;
TimerCountdown(&c->ping_timer, c->keepAliveInterval);
@@ -239,14 +246,16 @@ exit:
return rc;
}
int cycle(MQTTClient* c, Timer* timer)
{
// read the socket, see what work is due
unsigned short packet_type = readPacket(c, timer);
int packet_type = readPacket(c, timer);
if (packet_type == FAILURE) {
return FAILURE;
}
int len = 0,
rc = SUCCESSS;
rc = SUCCESS;
switch (packet_type)
{
@@ -287,7 +296,7 @@ int cycle(MQTTClient* c, Timer* timer)
rc = FAILURE;
else if ((len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREL, 0, mypacketid)) <= 0)
rc = FAILURE;
else if ((rc = sendPacket(c, len, timer)) != SUCCESSS) // send the PUBREL packet
else if ((rc = sendPacket(c, len, timer)) != SUCCESS) // send the PUBREL packet
rc = FAILURE; // there was a problem
if (rc == FAILURE)
goto exit; // there was a problem
@@ -299,9 +308,12 @@ int cycle(MQTTClient* c, Timer* timer)
c->ping_outstanding = 0;
break;
}
keepalive(c);
rc = keepalive(c); // Check keepalive return value
if (rc != SUCCESS) {
return rc;
}
exit:
if (rc == SUCCESSS)
if (rc == SUCCESS)
rc = packet_type;
return rc;
}
@@ -309,7 +321,7 @@ exit:
int MQTTYield(MQTTClient* c, int timeout_ms)
{
int rc = SUCCESSS;
int rc = SUCCESS;
Timer timer;
TimerInit(&timer);
@@ -391,7 +403,7 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
TimerCountdown(&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)) != SUCCESSS) // send the connect packet
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
@@ -408,7 +420,7 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
rc = FAILURE;
exit:
if (rc == SUCCESSS)
if (rc == SUCCESS)
c->isconnected = 1;
#if defined(MQTT_TASK)
@@ -442,7 +454,7 @@ int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos, messageH
len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, &charQos);
if (len <= 0)
goto exit;
if ((rc = sendPacket(c, len, &timer)) != SUCCESSS) // send the subscribe packet
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
@@ -497,7 +509,7 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
goto exit;
if ((rc = sendPacket(c, len, &timer)) != SUCCESSS) // send the subscribe packet
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
goto exit; // there was a problem
if (waitfor(c, UNSUBACK, &timer) == UNSUBACK)
@@ -541,7 +553,7 @@ int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
topic, (unsigned char*)message->payload, message->payloadlen);
if (len <= 0)
goto exit;
if ((rc = sendPacket(c, len, &timer)) != SUCCESSS) // send the subscribe packet
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
goto exit; // there was a problem
if (message->qos == QOS1)

View File

@@ -48,7 +48,14 @@
enum QoS { QOS0, QOS1, QOS2 };
/* all failure return codes must be negative */
enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESSS = 0 };
enum returnCode {
NO_DATA = -100, /* No data available (timeout) */
PROTOCOL_ERROR = -4, /* Invalid packet type or format */
DECODE_ERROR = -3, /* Failed to decode packet length */
BUFFER_OVERFLOW = -2, /* Buffer too small */
FAILURE = -1, /* Generic failure */
SUCCESS = 0
};
/* The Platform specific header must define the Network and Timer structures and functions
* which operate on them.