diff --git a/README.md b/README.md index 04dd217..d818c42 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,14 @@ Install JAVA Runtime Environment ## Revision History +### v1.2.1 + +- Fixed bugs + - Fixed a bug that stops when the module switches to data mode and at-command mode. + - The MQTT socket has been modified to open as a nonblock. + - Modified the memory used for MQTT publishing from static to dynamic allocation. + - Fixed to update connection LED and network state when switching to at-command mode. + ### v1.2.0 - Added features diff --git a/WIZ550S2E_App/src/Configuration/ConfigData.h b/WIZ550S2E_App/src/Configuration/ConfigData.h index 3d761b6..dbd5669 100644 --- a/WIZ550S2E_App/src/Configuration/ConfigData.h +++ b/WIZ550S2E_App/src/Configuration/ConfigData.h @@ -21,7 +21,6 @@ #define NAME_LEN 25 #define MQTT_ACCOUNT_LEN 10 -#define MQTT_PASS_LEN 10 #define MQTT_TOPIC_LEN 25 enum baud { diff --git a/WIZ550S2E_App/src/Internet/MQTT/mqtt_interface.c b/WIZ550S2E_App/src/Internet/MQTT/mqtt_interface.c index ccf0b3a..b1ed5fd 100644 --- a/WIZ550S2E_App/src/Internet/MQTT/mqtt_interface.c +++ b/WIZ550S2E_App/src/Internet/MQTT/mqtt_interface.c @@ -42,6 +42,7 @@ #include "mqtt_interface.h" #include "wizchip_conf.h" +#include "socket.h" unsigned long MilliTimer; @@ -160,7 +161,7 @@ int InitNetwork(Network* n) { int ret = 0; - ret = socket(n->my_socket, Sn_MR_TCP, 0, 0x00); + ret = socket(n->my_socket, Sn_MR_TCP, 0, SF_IO_NONBLOCK); return ret; } diff --git a/WIZ550S2E_App/src/S2E/S2E.c b/WIZ550S2E_App/src/S2E/S2E.c index dbda3cf..09f1c64 100644 --- a/WIZ550S2E_App/src/S2E/S2E.c +++ b/WIZ550S2E_App/src/S2E/S2E.c @@ -30,29 +30,36 @@ uint32_t uart_recv_cnt = 0; uint32_t ether_send_cnt = 0; uint32_t ether_recv_cnt = 0; -extern Network mqttNetwork; -extern MQTTClient mqttClient; -extern MQTTPacket_connectData mqttConnectData; -unsigned char mqtt_buf[WORK_BUF_SIZE] = {'\0',}; +Network mqttNetwork; +MQTTClient mqttClient; + +static uint8_t mqttInit = 0; static void messageArrived(MessageData* md) { - uint16_t len = 0; + uint16_t subMessagelen = 0; + int subTopiclen = 0; MQTTMessage* mqttSubMessage = md->message; + MQTTString* mqttSubTopic = md->topicName; + + subMessagelen = (int)mqttSubMessage->payloadlen; + subTopiclen = mqttSubTopic->lenstring.len; - len = (int)mqttSubMessage->payloadlen; +#ifdef __MQTT_DEBUG__ + printf("messageArrived : %s, %d\r\n", (char*)mqttSubTopic->lenstring.data, subTopiclen); +#endif - if(len > UART_SRB_SIZE) - len = UART_SRB_SIZE; + if(subMessagelen > UART_SRB_SIZE) + subMessagelen = UART_SRB_SIZE; - if((len > 0) && len <= RingBuffer_GetFree(&txring)) - ether_recv_cnt += len; + if((subMessagelen > 0) && subMessagelen <= RingBuffer_GetFree(&txring)) + ether_recv_cnt += subMessagelen; else - len = 0; + subMessagelen = 0; - if(len) { + if(subMessagelen) { Chip_UART_SendRB(LPC_USART, &txring, (char*)mqttSubMessage->payload, (int)mqttSubMessage->payloadlen); - uart_send_cnt += len; + uart_send_cnt += subMessagelen; } } @@ -203,6 +210,8 @@ static void uart_to_ether(uint8_t sock) uint16_t len = 0; int ret, uart_read_len = sizeof(g_send_buf); + char *mqtt_buf_ptr; + if(RingBuffer_IsEmpty(&rxring)) return; @@ -260,8 +269,16 @@ static void uart_to_ether(uint8_t sock) /* Data Transfer */ if(net->working_mode == MQTT) { - memset(mqtt_buf, '\0', sizeof(mqtt_buf)); - len = Chip_UART_ReadRB(LPC_USART, &rxring, &mqtt_buf, uart_read_len); + mqtt_buf_ptr = (char *)malloc(sizeof(char)*uart_read_len); + + if(mqtt_buf_ptr == NULL) { +#ifdef __MQTT_DEBUG__ + printf("MQTT Out of memory for publish\r\n"); +#endif + return; + } + + len = Chip_UART_ReadRB(LPC_USART, &rxring, mqtt_buf_ptr, uart_read_len); } else len = Chip_UART_ReadRB(LPC_USART, &rxring, &g_send_buf, uart_read_len); @@ -287,12 +304,13 @@ static void uart_to_ether(uint8_t sock) else if(sock_state == SOCK_ESTABLISHED) { if(net->working_mode == MQTT) { - mqttPubMessage.payload = mqtt_buf; + mqttPubMessage.payload = mqtt_buf_ptr; mqttPubMessage.payloadlen = len; mqttPubMessage.qos = QOS0; mqttPubMessage.retained = 0; mqttPubMessage.dup = 0; ret = MQTTPublish(&mqttClient, option->mqtt_publish_topic, &mqttPubMessage); + free(mqtt_buf_ptr); if(ret != SUCCESSS) { #ifdef __MQTT_DEBUG__ printf("MQTT publish error - ret : %d\r\n", ret); @@ -466,6 +484,10 @@ static void trigger_state3_process(uint8_t sock) Chip_UART_SendRB(LPC_USART, &txring, "[S,0]\r\n", 7); op_mode = OP_COMMAND; + Board_LED_Set(1, false); + + net->state = net_disconnect; + close(sock); return; } @@ -493,6 +515,11 @@ static void s2e_sockclose_process(uint8_t sock) switch(net->working_mode) { case MQTT: + if(mqttInit == 0) { + mqttInit = 1; + NewNetwork(&mqttNetwork, sock); + } + MQTTClientInit(&mqttClient, &mqttNetwork, 1000, g_send_buf, WORK_BUF_SIZE, g_recv_buf, WORK_BUF_SIZE); InitNetwork(&mqttNetwork); break; case TCP_CLIENT_MODE: @@ -597,6 +624,8 @@ static void s2e_sockestablished_process(uint8_t sock) int ret; S2E_Packet *value = get_S2E_Packet_pointer(); + MQTTPacket_connectData mqttConnectData = MQTTPacket_connectData_initializer; + if(reconn_flag) reconn_flag = reconn_time = 0; @@ -608,8 +637,8 @@ static void s2e_sockestablished_process(uint8_t sock) return; } case MQTT: - if(getSn_IR(SOCK_MQTT) & Sn_IR_CON) { - setSn_IR(SOCK_MQTT,Sn_IR_CON); + if(getSn_IR(sock) & Sn_IR_CON) { + setSn_IR(sock,Sn_IR_CON); mqttConnectData.MQTTVersion = 4; mqttConnectData.clientID.cstring = value->module_name; @@ -626,7 +655,7 @@ static void s2e_sockestablished_process(uint8_t sock) #endif } - ret = MQTTSubscribe(&mqttClient, value->options.mqtt_subscribe_topic, QOS0, messageArrived); + ret = MQTTSubscribe(&mqttClient, value->options.mqtt_subscribe_topic, QOS0, (volatile)messageArrived); if(ret != SUCCESSS) { #ifdef __MQTT_DEBUG__ printf("MQTT subscribe error - ret : %d\r\n", ret); @@ -758,9 +787,6 @@ void s2e_run(uint8_t sock) struct __options *option = (struct __options *)&(get_S2E_Packet_pointer()->options); uint8_t sock_state; - if(net->working_mode == MQTT) - sock = SOCK_MQTT; - getsockopt(sock, SO_STATUS, &sock_state); #ifdef __TRIG_DEBUG__ diff --git a/WIZ550S2E_App/src/common.h b/WIZ550S2E_App/src/common.h index 0f7a132..92f1c4f 100644 --- a/WIZ550S2E_App/src/common.h +++ b/WIZ550S2E_App/src/common.h @@ -9,14 +9,13 @@ #define MAJOR_VER 1 #define MINOR_VER 2 -#define MAINTENANCE_VER 0 +#define MAINTENANCE_VER 1 #define SOCK_DATA 0 #define SOCK_CONFIG 1 #define SOCK_TFTP 2 #define SOCK_DHCP 3 #define SOCK_DNS 4 -#define SOCK_MQTT 5 #define OP_COMMAND 0 #define OP_DATA 1 diff --git a/WIZ550S2E_App/src/main.c b/WIZ550S2E_App/src/main.c index 34cdc80..0a799a8 100644 --- a/WIZ550S2E_App/src/main.c +++ b/WIZ550S2E_App/src/main.c @@ -49,10 +49,6 @@ uint8_t g_recv_buf[WORK_BUF_SIZE] = {'\0',}; uint8_t run_dns = 1; uint8_t op_mode; - -Network mqttNetwork; -MQTTClient mqttClient; -MQTTPacket_connectData mqttConnectData = MQTTPacket_connectData_initializer; /***************************************************************************** * Private functions ****************************************************************************/ @@ -147,9 +143,6 @@ int main(void) } } - NewNetwork(&mqttNetwork, SOCK_MQTT); - MQTTClientInit(&mqttClient, &mqttNetwork, 1000, g_send_buf, WORK_BUF_SIZE, g_recv_buf, WORK_BUF_SIZE); - atc_init(); op_mode = OP_DATA;