{"action":"created","comment":{"url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/comments/48232665","id":48232665,"diff_hunk":"@@ -0,0 +1,715 @@\n+/* mqtt.c\n+*  Protocol: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html\n+*\n+* Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com>\n+* All rights reserved.\n+*\n+* Redistribution and use in source and binary forms, with or without\n+* modification, are permitted provided that the following conditions are met:\n+*\n+* * Redistributions of source code must retain the above copyright notice,\n+* this list of conditions and the following disclaimer.\n+* * Redistributions in binary form must reproduce the above copyright\n+* notice, this list of conditions and the following disclaimer in the\n+* documentation and/or other materials provided with the distribution.\n+* * Neither the name of Redis nor the names of its contributors may be used\n+* to endorse or promote products derived from this software without\n+* specific prior written permission.\n+*\n+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\n+* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n+* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n+* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\n+* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\n+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\n+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\n+* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n+* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n+* POSSIBILITY OF SUCH DAMAGE.\n+*/\n+\n+#include \"mqtt_msg.h\"\n+#include \"debug.h\"\n+#include \"mqtt.h\"\n+#include \"queue.h\"\n+#include \"../../platform/include/pando_sys.h\"\n+#include \"../../platform/include/pando_types.h\"\n+#include \"../../platform/include/pando_timer.h\"\n+#include \"../../platform/include/pando_net_tcp.h\"\n+#include \"utils.h\"\n+\n+#define MQTT_BUF_SIZE\t\t1024\n+#define MQTT_RECONNECT_TIMEOUT 5\n+\n+#define MQTT_TASK_PRIO        \t\t0\n+#define MQTT_TASK_QUEUE_SIZE    \t1\n+#define MQTT_SEND_TIMOUT\t\t\t5\n+\n+#ifndef QUEUE_BUFFER_SIZE\n+#define QUEUE_BUFFER_SIZE\t\t \t2048\n+#endif\n+\n+void MQTT_Task(MQTT_Client * arg);\n+\n+\n+static void FUNCTION_ATTRIBUTE\n+deliver_publish(MQTT_Client* client, uint8_t* message, int length)\n+{\n+\tmqtt_event_data_t event_data;\n+\n+\tevent_data.topic_length = length;\n+\tevent_data.topic = mqtt_get_publish_topic(message, &event_data.topic_length);\n+\tevent_data.data_length = length;\n+\tevent_data.data = mqtt_get_publish_data(message, &event_data.data_length);\n+\n+\tif(client->dataCb)\n+\t\tclient->dataCb((uint32_t*)client, event_data.topic, event_data.topic_length, event_data.data, event_data.data_length);\n+\n+}\n+\n+\n+/**\n+  * @brief  Client received callback function.\n+  * @param  arg: contain the ip link information\n+  * @param  pdata: received data\n+  * @param  len: the lenght of received data\n+  * @retval None\n+  */\n+void FUNCTION_ATTRIBUTE\n+mqtt_tcpclient_recv(void *arg, struct data_buf *buffer)\n+{\n+\tuint8_t msg_type;\n+\tuint8_t msg_qos;\n+\tuint16_t msg_id;\n+\n+\tstruct pando_tcp_conn *pCon = (struct pando_tcp_conn*)arg;\n+\tMQTT_Client *client = (MQTT_Client *)pCon->reverse;\n+\n+READPACKET:\n+\tINFO(\"TCP: data received %d bytes\\r\\n\", buffer->length);\n+\tshow_package(buffer->data, buffer->length);\n+\tif(buffer->length < MQTT_BUF_SIZE && buffer->length > 0){\n+\t\tpd_memcpy(client->mqtt_state.in_buffer, buffer->data, buffer->length);\n+\n+\t\tmsg_type = mqtt_get_type(client->mqtt_state.in_buffer);\n+\t\tmsg_qos = mqtt_get_qos(client->mqtt_state.in_buffer);\n+\t\tmsg_id = mqtt_get_id(client->mqtt_state.in_buffer, client->mqtt_state.in_buffer_length);\n+\t\tPRINTF(\"client->connstate:%d, type:%d, Qos:%d, id:%d\\n\", client->connState, msg_type, msg_qos, msg_id);\n+\t\tswitch(client->connState){\n+\t\tcase MQTT_CONNECT_SENDING:\n+\t\t\tif(msg_type == MQTT_MSG_TYPE_CONNACK){\n+\t\t\t\tif(client->mqtt_state.pending_msg_type != MQTT_MSG_TYPE_CONNECT){\n+\t\t\t\t\tINFO(\"MQTT: Invalid packet\\r\\n\");\n+\t\t\t\t\tif(client->security){\n+\t\t\t\t\t\t//espconn_secure_disconnect(client->pCon);\n+                        net_tcp_disconnect(client->pCon);\n+\t\t\t\t\t}\n+\t\t\t\t\telse {\n+\t\t\t\t\t\t//espconn_disconnect(client->pCon);\n+\t\t\t\t\t\tnet_tcp_disconnect(client->pCon);\n+\t\t\t\t\t}\n+\t\t\t\t} else {\n+\t\t\t\t\tINFO(\"MQTT: Connected to %s:%d\\r\\n\", client->host, client->port);\n+\t\t\t\t\tclient->connState = MQTT_DATA;\n+\t\t\t\t\tif(client->connectedCb)\n+\t\t\t\t\t\tclient->connectedCb((uint32_t*)client);\n+\t\t\t\t}\n+\n+\t\t\t}\n+\t\t\tbreak;\n+\t\tcase MQTT_DATA:\n+\t\t\tclient->mqtt_state.message_length_read = buffer->length;\n+\t\t\tclient->mqtt_state.message_length = mqtt_get_total_length(client->mqtt_state.in_buffer, client->mqtt_state.message_length_read);\n+\t\t\tINFO(\"mqtt actual length:%d\\n\", client->mqtt_state.message_length);\n+\n+\t\t\tswitch(msg_type)\n+\t\t\t{\n+\n+\t\t\t  case MQTT_MSG_TYPE_SUBACK:\n+\t\t\t\tif(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_SUBSCRIBE && client->mqtt_state.pending_msg_id == msg_id)\n+\t\t\t\t  INFO(\"MQTT: Subscribe successful\\r\\n\");\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_UNSUBACK:\n+\t\t\t\tif(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && client->mqtt_state.pending_msg_id == msg_id)\n+\t\t\t\t  INFO(\"MQTT: UnSubscribe successful\\r\\n\");\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PUBLISH:\n+\t\t\t\tif(msg_qos == 1)\n+\t\t\t\t\tclient->mqtt_state.outbound_message = mqtt_msg_puback(&client->mqtt_state.mqtt_connection, msg_id);\n+\t\t\t\telse if(msg_qos == 2)\n+\t\t\t\t\tclient->mqtt_state.outbound_message = mqtt_msg_pubrec(&client->mqtt_state.mqtt_connection, msg_id);\n+\t\t\t\tif(msg_qos == 1 || msg_qos == 2){\n+\t\t\t\t\tINFO(\"MQTT: Queue response QoS: %d\\r\\n\", msg_qos);\n+\t\t\t\t\tif(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){\n+\t\t\t\t\t\tINFO(\"MQTT: Queue full\\r\\n\");\n+\t\t\t\t\t}\n+\t\t\t\t}\n+\n+\t\t\t\tdeliver_publish(client, client->mqtt_state.in_buffer, client->mqtt_state.message_length_read);\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PUBACK:\n+\t\t\t\tif(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_msg_id == msg_id){\n+\t\t\t\t  INFO(\"MQTT: received MQTT_MSG_TYPE_PUBACK, finish QoS1 publish\\r\\n\");\n+\t\t\t\t}\n+\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PUBREC:\n+\t\t\t\t  client->mqtt_state.outbound_message = mqtt_msg_pubrel(&client->mqtt_state.mqtt_connection, msg_id);\n+\t\t\t\t  if(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){\n+\t\t\t\t  \tINFO(\"MQTT: Queue full\\r\\n\");\n+\t\t\t\t  }\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PUBREL:\n+\t\t\t\t  client->mqtt_state.outbound_message = mqtt_msg_pubcomp(&client->mqtt_state.mqtt_connection, msg_id);\n+\t\t\t\t  if(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){\n+\t\t\t\t\tINFO(\"MQTT: Queue full\\r\\n\");\n+\t\t\t\t  }\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PUBCOMP:\n+\t\t\t\tif(client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH && client->mqtt_state.pending_msg_id == msg_id){\n+\t\t\t\t  INFO(\"MQTT: receive MQTT_MSG_TYPE_PUBCOMP, finish QoS2 publish\\r\\n\");\n+\t\t\t\t}\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PINGREQ:\n+\t\t\t\t  client->mqtt_state.outbound_message = mqtt_msg_pingresp(&client->mqtt_state.mqtt_connection);\n+\t\t\t\t  if(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){\n+\t\t\t\t\tINFO(\"MQTT: Queue full\\r\\n\");\n+\t\t\t\t  }\n+\t\t\t\tbreak;\n+\t\t\t  case MQTT_MSG_TYPE_PINGRESP:\n+\t\t\t\t// Ignore\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\t// NOTE: this is done down here and not in the switch case above\n+\t\t\t// because the PSOCK_READBUF_LEN() won't work inside a switch\n+\t\t\t// statement due to the way protothreads resume.\n+\t\t\tif(msg_type == MQTT_MSG_TYPE_PUBLISH)\n+\t\t\t{\n+\t\t\t  INFO(\"PUBLISH MESSAGE,receive length:%d, actual length:%d\\n\",client->mqtt_state.message_length, client->mqtt_state.message_length_read);\n+\t\t\t  buffer->length = client->mqtt_state.message_length_read;\n+\n+\t\t\t  if(client->mqtt_state.message_length < client->mqtt_state.message_length_read)\n+\t\t\t  {\n+\t\t\t\t  //client->connState = MQTT_PUBLISH_RECV;\n+\t\t\t\t  //Not Implement yet\n+\t\t\t\t  buffer->length -= client->mqtt_state.message_length;\n+\t\t\t\t  buffer->data += client->mqtt_state.message_length;\n+\n+\t\t\t\t  INFO(\"Get another published message\\r\\n\");\n+\t\t\t\t  goto READPACKET;\n+\t\t\t  }\n+\n+\t\t\t}\n+\t\t\tbreak;\n+\t\t}\n+\t} else {\n+\t\tINFO(\"ERROR: Message too long\\r\\n\");\n+\t}\n+\tMQTT_Task(client);\n+}\n+\n+/**\n+  * @brief  Client send over callback function.\n+  * @param  arg: contain the ip link information\n+  * @retval None\n+  */\n+void FUNCTION_ATTRIBUTE\n+mqtt_tcpclient_sent_cb(void *arg, int8_t errorno)\n+{\n+\tstruct pando_tcp_conn *pCon = (struct pando_tcp_conn *)arg;\n+\tMQTT_Client* client = (MQTT_Client *)pCon->reverse;\n+\tINFO(\"TCP: Sent\\r\\n\");\n+\tclient->sendTimeout = 0;\n+\tif(client->connState == MQTT_DATA && client->mqtt_state.pending_msg_type == MQTT_MSG_TYPE_PUBLISH){\n+\t\tif(client->publishedCb)\n+\t\t\tclient->publishedCb((uint32_t*)client);\n+\t}\n+\tMQTT_Task(client);\n+}\n+\n+void FUNCTION_ATTRIBUTE mqtt_timer(void *arg)\n+{\n+\tpd_printf(\"enter into mqtt timer!!!\\n\");\n+\tMQTT_Client* client = (MQTT_Client*)arg;\n+    struct data_buf buffer;\n+\tif(client->connState == MQTT_DATA)\n+\t{\n+\t\tclient->keepAliveTick ++;\n+\t\tif(client->keepAliveTick > client->mqtt_state.connect_info->keepalive){\n+\t\t\tINFO(\"\\r\\nMQTT: Send keepalive packet to %s:%d!\\r\\n\", client->host, client->port);\n+\t\t\tclient->mqtt_state.outbound_message = mqtt_msg_pingreq(&client->mqtt_state.mqtt_connection);\n+\t\t\tclient->mqtt_state.pending_msg_type = MQTT_MSG_TYPE_PINGREQ;\n+\t\t\tclient->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data);\n+\t\t\tclient->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);\n+\n+\n+\t\t\tclient->sendTimeout = MQTT_SEND_TIMOUT;\n+            buffer.length = client->mqtt_state.outbound_message->length;\n+            buffer.data = client->mqtt_state.outbound_message->data;\n+\t\t\tINFO(\"MQTT: Sending, type: %d, id: %04X\\r\\n\",client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);\n+\t\t\tif(client->security){\n+\t\t\t\t//espconn_secure_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);\n+                net_tcp_send(client->pCon, buffer, client->sendTimeout);\n+            }\n+\t\t\telse{\n+\t\t\t\tnet_tcp_send(client->pCon, buffer, client->sendTimeout);\n+\t\t\t}\n+\n+\t\t\tclient->mqtt_state.outbound_message = NULL;\n+\n+\t\t\tclient->keepAliveTick = 0;\n+\t\t\tMQTT_Task(client);\n+\t\t}\n+\n+\t} else if(client->connState == TCP_RECONNECT_REQ)\n+\t{\n+\t\tclient->reconnectTick ++;\n+\t\tif(client->reconnectTick > MQTT_RECONNECT_TIMEOUT) {\n+\t\t\tclient->reconnectTick = 0;\n+\t\t\tclient->connState = TCP_RECONNECT;\n+\t\t\tMQTT_Task(client);\n+\t\t}\n+\t}else if(client->connState == TCP_CONNECTING){\n+\t\tclient->connState = TCP_CONNECTING_ERROR;\n+\t\tMQTT_Task(client);\n+\t}\n+\n+\tif(client->sendTimeout > 0)\n+\t\tclient->sendTimeout --;\n+}\n+\n+void FUNCTION_ATTRIBUTE\n+mqtt_tcpclient_discon_cb(void *arg, int8_t errno)\n+{\n+\n+\tstruct pando_tcp_conn *pespconn = (struct pando_tcp_conn *)arg;\n+\tMQTT_Client* client = (MQTT_Client *)pespconn->reverse;\n+\tINFO(\"TCP: Disconnected callback\\r\\n\");\n+\tclient->connState = TCP_RECONNECT_REQ;\n+\tif(client->disconnectedCb)\n+\t\tclient->disconnectedCb((uint32_t*)client);\n+\n+\tMQTT_Task(client);\n+}\n+\n+\n+\n+/**\n+  * @brief  Tcp client connect success callback function.\n+  * @param  arg: contain the ip link information\n+  * @retval None\n+  */\n+void FUNCTION_ATTRIBUTE\n+mqtt_tcpclient_connect_cb(void *arg , int8_t errno)\n+{\n+\tpd_printf(\"enter into mqtt_tcpclient_connect_cb\\n\");\n+\tstruct pando_tcp_conn *pCon = (struct pando_tcp_conn *)arg;\n+\tMQTT_Client* client = (MQTT_Client *)pCon->reverse;\n+\n+    struct data_buf buffer;\n+\n+\tnet_tcp_register_disconnected_callback(pCon, mqtt_tcpclient_discon_cb);\n+\tnet_tcp_register_recv_callback(pCon, mqtt_tcpclient_recv);////////\n+\tnet_tcp_register_sent_callback(pCon, mqtt_tcpclient_sent_cb);///////\n+\tINFO(\"MQTT: Connected to broker %s:%d\\r\\n\", client->host, client->port);\n+\n+\n+\tmqtt_msg_init(&client->mqtt_state.mqtt_connection, client->mqtt_state.out_buffer, client->mqtt_state.out_buffer_length);\n+\tclient->mqtt_state.outbound_message = mqtt_msg_connect(&client->mqtt_state.mqtt_connection, client->mqtt_state.connect_info);\n+\tclient->mqtt_state.pending_msg_type = mqtt_get_type(client->mqtt_state.outbound_message->data);\n+\tclient->mqtt_state.pending_msg_id = mqtt_get_id(client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);\n+\n+\tclient->sendTimeout = MQTT_SEND_TIMOUT;\n+\n+    buffer.length = client->mqtt_state.outbound_message->length;\n+    buffer.data = client->mqtt_state.outbound_message->data;\n+    INFO(\"MQTT: Sending, type: %d, id: %04X\\r\\n\",client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);\n+\tif(client->security){\n+        net_tcp_send(pCon, buffer, client->sendTimeout);\n+        //espconn_secure_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);\n+\t}\n+\telse{\n+        net_tcp_send(pCon, buffer, client->sendTimeout);\n+\t\t//espconn_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);\n+\t}\n+\n+\tclient->mqtt_state.outbound_message = NULL;\n+\tclient->connState = MQTT_CONNECT_SENDING;\n+\tMQTT_Task(client);\n+}\n+\n+/**\n+  * @brief  Tcp client connect repeat callback function.\n+  * @param  arg: contain the ip link information\n+  * @retval None\n+  */\n+void FUNCTION_ATTRIBUTE\n+mqtt_tcpclient_recon_cb(void *arg, int8_t errType)\n+{\n+\tstruct pando_tcp_conn *pCon = (struct pando_tcp_conn *)arg;\n+\tMQTT_Client* client = (MQTT_Client *)pCon->reverse;\n+\n+\tINFO(\"TCP: Reconnect to %s:%d\\r\\n\", client->host, client->port);\n+\n+\tclient->connState = TCP_RECONNECT_REQ;\n+\n+\tMQTT_Task(client);\n+\n+}\n+\n+/**\n+  * @brief  MQTT publish function.\n+  * @param  client: \tMQTT_Client reference\n+  * @param  topic: \t\tstring topic will publish to\n+  * @param  data: \t\tbuffer data send point to\n+  * @param  data_length: length of data\n+  * @param  qos:\t\tqos\n+  * @param  retain:\t\tretain\n+  * @retval TRUE if success queue\n+  */\n+BOOL FUNCTION_ATTRIBUTE\n+MQTT_Publish(MQTT_Client *client, const char* topic, const char* data, int data_length, int qos, int retain)\n+{\n+\tuint8_t dataBuffer[MQTT_BUF_SIZE];\n+\tuint16_t dataLen;\n+\tclient->mqtt_state.outbound_message = mqtt_msg_publish(&client->mqtt_state.mqtt_connection,\n+\t\t\t\t\t\t\t\t\t\t topic, data, data_length,\n+\t\t\t\t\t\t\t\t\t\t qos, retain,\n+\t\t\t\t\t\t\t\t\t\t &client->mqtt_state.pending_msg_id);\n+\tif(client->mqtt_state.outbound_message->length == 0){\n+\t\tINFO(\"MQTT: Queuing publish failed\\r\\n\");\n+\t\treturn FALSE;\n+\t}\n+\tINFO(\"MQTT: queuing publish, length: %d, queue size(%d/%d)\\r\\n\", client->mqtt_state.outbound_message->length, client->msgQueue.rb.fill_cnt, client->msgQueue.rb.size);\n+\twhile(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){\n+\t\tINFO(\"MQTT: Queue full\\r\\n\");\n+\t\tif(QUEUE_Gets(&client->msgQueue, dataBuffer, &dataLen, MQTT_BUF_SIZE) == -1) {\n+\t\t\tINFO(\"MQTT: Serious buffer error\\r\\n\");\n+\t\t\treturn FALSE;\n+\t\t}\n+\t}\n+\tpd_printf(\"client->mqtt_state.outbound_message->length:%d\",client->mqtt_state.outbound_message->length);\n+\tMQTT_Task(client);\n+\treturn TRUE;\n+}\n+\n+/**\n+  * @brief  MQTT subscibe function.\n+  * @param  client: \tMQTT_Client reference\n+  * @param  topic: \t\tstring topic will subscribe\n+  * @param  qos:\t\tqos\n+  * @retval TRUE if success queue\n+  */\n+BOOL FUNCTION_ATTRIBUTE\n+MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos)\n+{\n+\tuint8_t dataBuffer[MQTT_BUF_SIZE];\n+\tuint16_t dataLen;\n+\n+\tclient->mqtt_state.outbound_message = mqtt_msg_subscribe(&client->mqtt_state.mqtt_connection,\n+\t\t\t\t\t\t\t\t\t\t\ttopic, 0,\n+\t\t\t\t\t\t\t\t\t\t\t&client->mqtt_state.pending_msg_id);\n+\tINFO(\"MQTT: queue subscribe, topic\\\"%s\\\", id: %d\\r\\n\",topic, client->mqtt_state.pending_msg_id);\n+\twhile(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){\n+\t\tINFO(\"MQTT: Queue full\\r\\n\");\n+\t\tif(QUEUE_Gets(&client->msgQueue, dataBuffer, &dataLen, MQTT_BUF_SIZE) == -1) {\n+\t\t\tINFO(\"MQTT: Serious buffer error\\r\\n\");\n+\t\t\treturn FALSE;\n+\t\t}\n+\t}\n+\tMQTT_Task(client);\n+\treturn TRUE;\n+}\n+\n+void FUNCTION_ATTRIBUTE\n+MQTT_Task(MQTT_Client *client)\n+{\n+\tINFO(\"MQTT TASK\\n\");\n+\tuint8_t dataBuffer[MQTT_BUF_SIZE];\n+\tuint16_t dataLen;\n+    struct data_buf buffer;\n+    \n+\tif(client == NULL)\n+\t\treturn;\n+\tswitch(client->connState){\n+\n+\tcase TCP_RECONNECT_REQ:\n+\t\tbreak;\n+\tcase TCP_RECONNECT:\n+\t\tMQTT_Connect(client);\n+\t\tINFO(\"TCP: Reconnect to: %s:%d\\r\\n\", client->host, client->port);\n+\t\tclient->connState = TCP_CONNECTING;\n+\t\tbreak;\n+\tcase MQTT_DATA:\n+\t\tINFO(\"MQTT TASK DATA\\n\");\n+\t\tif(QUEUE_IsEmpty(&client->msgQueue) || client->sendTimeout != 0)\n+\t\t{\n+\t\t\tbreak;\n+\t\t}\n+\t\tif(QUEUE_Gets(&client->msgQueue, dataBuffer, &dataLen, MQTT_BUF_SIZE) == 0)\n+\t\t{\n+\t\t\tclient->mqtt_state.pending_msg_type = mqtt_get_type(dataBuffer);\n+\t\t\tclient->mqtt_state.pending_msg_id = mqtt_get_id(dataBuffer, dataLen);\n+\n+\t\t\tclient->sendTimeout = MQTT_SEND_TIMOUT;\n+\t\t\tbuffer.data = dataBuffer;\n+\t\t\tbuffer.length = dataLen;\n+\n+\t\t\tINFO(\"MQTT: Sending, type: %d, id: %04X\\r\\n\",client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);\n+\t\t\tif(client->security){\n+                net_tcp_send(client->pCon, buffer, client->sendTimeout);\n+                //espconn_secure_sent(client->pCon, dataBuffer, dataLen);\n+\t\t\t}\n+\t\t\telse{\n+\t\t\t\tINFO(\"net_tcp_send\\n\");\n+\t\t\t\tINFO(\"client:%d\",client);\n+\n+                net_tcp_send(client->pCon, buffer, client->sendTimeout);\n+\t\t\t\t//espconn_sent(client->pCon, dataBuffer, dataLen);\n+\t\t\t}\n+\n+\t\t\tclient->mqtt_state.outbound_message = NULL;\n+\t\t\tbreak;\n+\t\t}\n+\t\tbreak;\n+\t}\n+}\n+\n+/**\n+  * @brief  MQTT initialization connection function\n+  * @param  client: \tMQTT_Client reference\n+  * @param  host: \tDomain or IP string\n+  * @param  port: \tPort to connect\n+  * @param  security:\t\t1 for ssl, 0 for none\n+  * @retval None\n+  */\n+void FUNCTION_ATTRIBUTE\n+MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32_t port, uint8_t security)\n+{\n+\tuint32_t temp;\n+\tINFO(\"MQTT_InitConnection\\r\\n\");\n+\tpd_memset(mqttClient, 0, sizeof(MQTT_Client));\n+\ttemp = pd_strlen(host);\n+\tmqttClient->host = (uint8_t*)pd_malloc(temp + 1);\n+    pd_memset(mqttClient->host, 0, temp + 1);\n+\tpd_strncpy(mqttClient->host, host);","path":"framework/gateway/mqtt/mqtt.c","position":496,"original_position":496,"commit_id":"7148a2a0ceca1f3c410dda518d8aeb229a1b0462","original_commit_id":"7148a2a0ceca1f3c410dda518d8aeb229a1b0462","user":{"login":"leeshineSZ","id":11749994,"avatar_url":"https://avatars.githubusercontent.com/u/11749994?v=3","gravatar_id":"","url":"https://api.github.com/users/leeshineSZ","html_url":"https://github.com/leeshineSZ","followers_url":"https://api.github.com/users/leeshineSZ/followers","following_url":"https://api.github.com/users/leeshineSZ/following{/other_user}","gists_url":"https://api.github.com/users/leeshineSZ/gists{/gist_id}","starred_url":"https://api.github.com/users/leeshineSZ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leeshineSZ/subscriptions","organizations_url":"https://api.github.com/users/leeshineSZ/orgs","repos_url":"https://api.github.com/users/leeshineSZ/repos","events_url":"https://api.github.com/users/leeshineSZ/events{/privacy}","received_events_url":"https://api.github.com/users/leeshineSZ/received_events","type":"User","site_admin":false},"body":"此处未按4个空格对齐。","created_at":"2015-12-22T08:51:43Z","updated_at":"2015-12-22T08:51:43Z","html_url":"https://github.com/PandoCloud/pando-embedded-framework/pull/11#discussion_r48232665","pull_request_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11","_links":{"self":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/comments/48232665"},"html":{"href":"https://github.com/PandoCloud/pando-embedded-framework/pull/11#discussion_r48232665"},"pull_request":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11"}}},"pull_request":{"url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11","id":54326845,"html_url":"https://github.com/PandoCloud/pando-embedded-framework/pull/11","diff_url":"https://github.com/PandoCloud/pando-embedded-framework/pull/11.diff","patch_url":"https://github.com/PandoCloud/pando-embedded-framework/pull/11.patch","issue_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/11","number":11,"state":"open","locked":false,"title":"Platform 8266","user":{"login":"LottesLiu","id":14155719,"avatar_url":"https://avatars.githubusercontent.com/u/14155719?v=3","gravatar_id":"","url":"https://api.github.com/users/LottesLiu","html_url":"https://github.com/LottesLiu","followers_url":"https://api.github.com/users/LottesLiu/followers","following_url":"https://api.github.com/users/LottesLiu/following{/other_user}","gists_url":"https://api.github.com/users/LottesLiu/gists{/gist_id}","starred_url":"https://api.github.com/users/LottesLiu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LottesLiu/subscriptions","organizations_url":"https://api.github.com/users/LottesLiu/orgs","repos_url":"https://api.github.com/users/LottesLiu/repos","events_url":"https://api.github.com/users/LottesLiu/events{/privacy}","received_events_url":"https://api.github.com/users/LottesLiu/received_events","type":"User","site_admin":false},"body":"检测联网状态的接口还没有统一，请先合并其他的内容","created_at":"2015-12-22T02:36:21Z","updated_at":"2015-12-22T08:51:43Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6a6e53db92a2e8a4cd58232def0d1de314502391","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11/commits","review_comments_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11/comments","review_comment_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/comments{/number}","comments_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/11/comments","statuses_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/statuses/7148a2a0ceca1f3c410dda518d8aeb229a1b0462","head":{"label":"PandoCloud:platform_8266","ref":"platform_8266","sha":"7148a2a0ceca1f3c410dda518d8aeb229a1b0462","user":{"login":"PandoCloud","id":9478963,"avatar_url":"https://avatars.githubusercontent.com/u/9478963?v=3","gravatar_id":"","url":"https://api.github.com/users/PandoCloud","html_url":"https://github.com/PandoCloud","followers_url":"https://api.github.com/users/PandoCloud/followers","following_url":"https://api.github.com/users/PandoCloud/following{/other_user}","gists_url":"https://api.github.com/users/PandoCloud/gists{/gist_id}","starred_url":"https://api.github.com/users/PandoCloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PandoCloud/subscriptions","organizations_url":"https://api.github.com/users/PandoCloud/orgs","repos_url":"https://api.github.com/users/PandoCloud/repos","events_url":"https://api.github.com/users/PandoCloud/events{/privacy}","received_events_url":"https://api.github.com/users/PandoCloud/received_events","type":"Organization","site_admin":false},"repo":{"id":44523488,"name":"pando-embedded-framework","full_name":"PandoCloud/pando-embedded-framework","owner":{"login":"PandoCloud","id":9478963,"avatar_url":"https://avatars.githubusercontent.com/u/9478963?v=3","gravatar_id":"","url":"https://api.github.com/users/PandoCloud","html_url":"https://github.com/PandoCloud","followers_url":"https://api.github.com/users/PandoCloud/followers","following_url":"https://api.github.com/users/PandoCloud/following{/other_user}","gists_url":"https://api.github.com/users/PandoCloud/gists{/gist_id}","starred_url":"https://api.github.com/users/PandoCloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PandoCloud/subscriptions","organizations_url":"https://api.github.com/users/PandoCloud/orgs","repos_url":"https://api.github.com/users/PandoCloud/repos","events_url":"https://api.github.com/users/PandoCloud/events{/privacy}","received_events_url":"https://api.github.com/users/PandoCloud/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PandoCloud/pando-embedded-framework","description":"a middleware between server and hardware driver","fork":false,"url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework","forks_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/forks","keys_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/teams","hooks_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/hooks","issue_events_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/events{/number}","events_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/events","assignees_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/assignees{/user}","branches_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/branches{/branch}","tags_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/tags","blobs_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/statuses/{sha}","languages_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/languages","stargazers_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/stargazers","contributors_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/contributors","subscribers_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/subscribers","subscription_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/subscription","commits_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/contents/{+path}","compare_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/merges","archive_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/downloads","issues_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues{/number}","pulls_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls{/number}","milestones_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/milestones{/number}","notifications_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/labels{/name}","releases_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/releases{/id}","created_at":"2015-10-19T09:14:02Z","updated_at":"2015-12-16T14:40:46Z","pushed_at":"2015-12-22T02:36:22Z","git_url":"git://github.com/PandoCloud/pando-embedded-framework.git","ssh_url":"git@github.com:PandoCloud/pando-embedded-framework.git","clone_url":"https://github.com/PandoCloud/pando-embedded-framework.git","svn_url":"https://github.com/PandoCloud/pando-embedded-framework","homepage":"","size":1472,"stargazers_count":17,"watchers_count":17,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":3,"forks":4,"open_issues":3,"watchers":17,"default_branch":"master"}},"base":{"label":"PandoCloud:master","ref":"master","sha":"4015b4e4f3d03410c786eed623bb7de82682a445","user":{"login":"PandoCloud","id":9478963,"avatar_url":"https://avatars.githubusercontent.com/u/9478963?v=3","gravatar_id":"","url":"https://api.github.com/users/PandoCloud","html_url":"https://github.com/PandoCloud","followers_url":"https://api.github.com/users/PandoCloud/followers","following_url":"https://api.github.com/users/PandoCloud/following{/other_user}","gists_url":"https://api.github.com/users/PandoCloud/gists{/gist_id}","starred_url":"https://api.github.com/users/PandoCloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PandoCloud/subscriptions","organizations_url":"https://api.github.com/users/PandoCloud/orgs","repos_url":"https://api.github.com/users/PandoCloud/repos","events_url":"https://api.github.com/users/PandoCloud/events{/privacy}","received_events_url":"https://api.github.com/users/PandoCloud/received_events","type":"Organization","site_admin":false},"repo":{"id":44523488,"name":"pando-embedded-framework","full_name":"PandoCloud/pando-embedded-framework","owner":{"login":"PandoCloud","id":9478963,"avatar_url":"https://avatars.githubusercontent.com/u/9478963?v=3","gravatar_id":"","url":"https://api.github.com/users/PandoCloud","html_url":"https://github.com/PandoCloud","followers_url":"https://api.github.com/users/PandoCloud/followers","following_url":"https://api.github.com/users/PandoCloud/following{/other_user}","gists_url":"https://api.github.com/users/PandoCloud/gists{/gist_id}","starred_url":"https://api.github.com/users/PandoCloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/PandoCloud/subscriptions","organizations_url":"https://api.github.com/users/PandoCloud/orgs","repos_url":"https://api.github.com/users/PandoCloud/repos","events_url":"https://api.github.com/users/PandoCloud/events{/privacy}","received_events_url":"https://api.github.com/users/PandoCloud/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/PandoCloud/pando-embedded-framework","description":"a middleware between server and hardware driver","fork":false,"url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework","forks_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/forks","keys_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/teams","hooks_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/hooks","issue_events_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/events{/number}","events_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/events","assignees_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/assignees{/user}","branches_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/branches{/branch}","tags_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/tags","blobs_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/statuses/{sha}","languages_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/languages","stargazers_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/stargazers","contributors_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/contributors","subscribers_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/subscribers","subscription_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/subscription","commits_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/contents/{+path}","compare_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/merges","archive_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/downloads","issues_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues{/number}","pulls_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls{/number}","milestones_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/milestones{/number}","notifications_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/labels{/name}","releases_url":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/releases{/id}","created_at":"2015-10-19T09:14:02Z","updated_at":"2015-12-16T14:40:46Z","pushed_at":"2015-12-22T02:36:22Z","git_url":"git://github.com/PandoCloud/pando-embedded-framework.git","ssh_url":"git@github.com:PandoCloud/pando-embedded-framework.git","clone_url":"https://github.com/PandoCloud/pando-embedded-framework.git","svn_url":"https://github.com/PandoCloud/pando-embedded-framework","homepage":"","size":1472,"stargazers_count":17,"watchers_count":17,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":3,"forks":4,"open_issues":3,"watchers":17,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11"},"html":{"href":"https://github.com/PandoCloud/pando-embedded-framework/pull/11"},"issue":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/11"},"comments":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/issues/11/comments"},"review_comments":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11/comments"},"review_comment":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/pulls/11/commits"},"statuses":{"href":"https://api.github.com/repos/PandoCloud/pando-embedded-framework/statuses/7148a2a0ceca1f3c410dda518d8aeb229a1b0462"}}}}