fix: yet more wiznet bugs, reconnects, led status indicators
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -289,19 +289,19 @@ uint8_t OneWireReset(void) {
|
||||
// wait until the wire is high... just in case
|
||||
do {
|
||||
if (--retries == 0) return 0;
|
||||
Delay_Us(2);
|
||||
Delay_Us(ONEWIRE_RESET_RETRY_TIME);
|
||||
} while (!DIRECT_READ());
|
||||
|
||||
DIRECT_WRITE_LOW();
|
||||
DIRECT_MODE_OUTPUT(); // drive output low
|
||||
|
||||
Delay_Us(480);
|
||||
Delay_Us(ONEWIRE_RESET_LOW_TIME);
|
||||
|
||||
DIRECT_MODE_INPUT(); // allow it to float
|
||||
Delay_Us(70);
|
||||
Delay_Us(ONEWIRE_RESET_SAMPLE_TIME);
|
||||
r = !DIRECT_READ();
|
||||
|
||||
Delay_Us(410);
|
||||
Delay_Us(ONEWIRE_RESET_POST_TIME);
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -313,17 +313,17 @@ void OneWireWriteBit(uint8_t v) {
|
||||
if (v & 1) {
|
||||
DIRECT_WRITE_LOW();
|
||||
DIRECT_MODE_OUTPUT(); // drive output low
|
||||
Delay_Us(10);
|
||||
Delay_Us(ONEWIRE_WRITE_1_LOW_TIME);
|
||||
DIRECT_WRITE_HIGH(); // drive output high
|
||||
|
||||
Delay_Us(55);
|
||||
Delay_Us(ONEWIRE_WRITE_1_TOTAL_TIME - ONEWIRE_WRITE_1_LOW_TIME);
|
||||
} else {
|
||||
DIRECT_WRITE_LOW();
|
||||
DIRECT_MODE_OUTPUT(); // drive output low
|
||||
Delay_Us(65);
|
||||
Delay_Us(ONEWIRE_WRITE_0_LOW_TIME);
|
||||
DIRECT_WRITE_HIGH(); // drive output high
|
||||
|
||||
Delay_Us(5);
|
||||
Delay_Us(ONEWIRE_WRITE_0_TOTAL_TIME - ONEWIRE_WRITE_0_LOW_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,12 +336,17 @@ uint8_t OneWireReadBit(void) {
|
||||
|
||||
DIRECT_MODE_OUTPUT();
|
||||
DIRECT_WRITE_LOW();
|
||||
Delay_Us(3);
|
||||
DIRECT_MODE_INPUT(); // let pin float, pull up will raise
|
||||
Delay_Us(10);
|
||||
Delay_Us(ONEWIRE_READ_INIT_LOW_TIME); // 6us initial low pulse
|
||||
|
||||
DIRECT_MODE_INPUT(); // let pin float, pull up will raise
|
||||
Delay_Us(ONEWIRE_READ_SAMPLE_TIME); // 8us until sample point
|
||||
r = DIRECT_READ();
|
||||
|
||||
Delay_Us(53);
|
||||
// Wait for remainder of the read timeslot
|
||||
// Total - init_low - sample = 64 - 6 - 8 = 50us
|
||||
Delay_Us(ONEWIRE_READ_TOTAL_TIME - ONEWIRE_READ_INIT_LOW_TIME -
|
||||
ONEWIRE_READ_SAMPLE_TIME);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,14 +14,14 @@ static inline __attribute__((always_inline)) uint8_t directRead() {
|
||||
return (GPIOB->INDR & (1 << 9)) ? 1 : 0;
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void directModeInput() {
|
||||
static inline void directModeInput() {
|
||||
GPIOB->CFGHR &= ~(0xF << (4 * (9 - 8)));
|
||||
GPIOB->CFGHR |= (0x4 << (4 * (9 - 8)));
|
||||
GPIOB->CFGHR |= (GPIO_CNF_IN_FLOATING << (4 * (9 - 8)));
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void directModeOutput() {
|
||||
static inline void directModeOutput() {
|
||||
GPIOB->CFGHR &= ~(0xF << (4 * (9 - 8)));
|
||||
GPIOB->CFGHR |= (0x3 << (4 * (9 - 8)));
|
||||
GPIOB->CFGHR |= ((GPIO_Speed_50MHz | GPIO_CNF_OUT_PP) << (4 * (9 - 8)));
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) void directWriteLow() {
|
||||
@@ -39,6 +39,27 @@ static inline __attribute__((always_inline)) void directWriteHigh() {
|
||||
#define DIRECT_MODE_OUTPUT() directModeOutput()
|
||||
#endif
|
||||
|
||||
// timing configuration
|
||||
// time between line check retries
|
||||
#define ONEWIRE_RESET_RETRY_TIME 2
|
||||
// reset cycle
|
||||
#define ONEWIRE_RESET_LOW_TIME 480
|
||||
#define ONEWIRE_RESET_SAMPLE_TIME 60
|
||||
#define ONEWIRE_RESET_POST_TIME 410
|
||||
|
||||
// write 1 bit
|
||||
#define ONEWIRE_WRITE_1_LOW_TIME 6
|
||||
#define ONEWIRE_WRITE_1_TOTAL_TIME 64
|
||||
|
||||
// write 0 bit
|
||||
#define ONEWIRE_WRITE_0_LOW_TIME 80
|
||||
#define ONEWIRE_WRITE_0_TOTAL_TIME 84
|
||||
|
||||
// read bit
|
||||
#define ONEWIRE_READ_INIT_LOW_TIME 6
|
||||
#define ONEWIRE_READ_SAMPLE_TIME 8
|
||||
#define ONEWIRE_READ_TOTAL_TIME 64
|
||||
|
||||
// OneWire Function Declarations
|
||||
|
||||
// Initialize the OneWire bus
|
||||
|
||||
Reference in New Issue
Block a user