Re-formatted code and made some small changes to behaviour.

At some point this repo might get updated to MQTT v5. I have to
carefully read the standard and see what has changed.
This commit is contained in:
A.M. Rowsell 2024-05-06 08:02:52 -04:00
commit a84326db7f
Signed by: amr
GPG key ID: 0B6E2D8375CF79A9
2 changed files with 194 additions and 188 deletions

18
mqtt.c
View file

@ -188,8 +188,9 @@ void ICACHE_FLASH_ATTR reconnected_callback(void *arg, sint8 err) {
void ICACHE_FLASH_ATTR disconnected_callback(void *arg) {
os_printf("Disconnected\n");
os_timer_disarm(&pubTimer);
os_timer_disarm(&pingTimer);
/* These timers should be disarmed by the userspace code */
/* os_timer_disarm(&pubTimer); */
/* os_timer_disarm(&pingTimer); */
}
uint8_t ICACHE_FLASH_ATTR tcpConnect(void *arg) {
@ -373,7 +374,6 @@ uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint3
// prepare for subscribe
pPacket->fixedHeader = (uint8_t *)os_zalloc(sizeof(uint8_t) * 5);
pPacket->fixedHeader[0] = ((msgType << 4) & 0xF0) | 0x02;
pPacket->varHeader_len = 2;
pPacket->varHeader = (uint8_t *)os_zalloc(sizeof(uint8_t) * pPacket->varHeader_len);
os_memset(pPacket->varHeader, 0, 2); // set packet ID to 0
@ -400,10 +400,9 @@ uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint3
os_memcpy(fullPacket + pPacket->fixedHeader_len + pPacket->varHeader_len, pPacket->payload, pPacket->payload_len);
break;
case MQTT_MSG_TYPE_PINGREQ:
case MQTT_MSG_TYPE_DISCONNECT:
// PINGREQ has no varHeader or payload, it's just two bytes
// 0xC0 0x00
case MQTT_MSG_TYPE_PINGREQ:
// PINGREQ and DISCONNECT have no varHeader or payload, it's just two bytes
pPacket->fixedHeader = (uint8_t *)os_zalloc(sizeof(uint8_t) * 2);
pPacket->fixedHeader[0] = (msgType << 4) & 0xF0; // bottom nibble must be clear
pPacket->fixedHeader[1] = 0; // remaining length is zero
@ -419,6 +418,7 @@ uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint3
os_memcpy(fullPacket, pPacket->fixedHeader, pPacket->fixedHeader_len);
break;
default:
// something has gone wrong
os_printf("Attempt to send incorrect packet type: %d", (uint8_t)msgType);
return -1;
@ -435,6 +435,12 @@ uint8_t ICACHE_FLASH_ATTR mqttSend(mqtt_session_t *session, uint8_t *data, uint3
if(msgType != MQTT_MSG_TYPE_DISCONNECT) {
os_timer_setfn(&MQTT_KeepAliveTimer, (os_timer_func_t *)pingAlive, session);
os_timer_arm(&MQTT_KeepAliveTimer, 30000, 0);
} else {
/* If we send a disconnect packet, we must close the connection ourselves,
per MQTT standard 3.14.4 */
espconn_disconnect(session->activeConnection);
os_timer_disarm(&MQTT_KeepAliveTimer); // ensure the keepalive timer is disarmed
return 0;
}
} else {
os_printf("No wifi! Narf!\n");

0
strtoarr.py Executable file → Normal file
View file