Porting PAHO Open source MQTT libraries using LWIP callback programming

Source: Internet
Author: User

The article content is the project debugging record, the content typesetting is scattered, the reading is cautious into!
LWIP Raw callback programming debug record
1. Tcp_connect Connection problem:
1.1 Not connected, no callback function call, delay can occasionally solve the problemSolution: Check the state of the TCP_PCB block in the software timer periodically
1.2 If the server is actively disconnected, you will receive no data response in the TCP_RECV_CB .Here it is determined that the state should have been disconnected
1.3 Tcpip_callback What to do with the effect of shielding afterThe function sends a mailbox to the TCPIP thread, and after the TCPIP thread receives it, calls the incoming callback function to join the currently requested TPCB to the Tcp_tw_pcbs list and registers a timer event in the TCPIP process as the core timer for the current PCB connection. Following the analysis, it is found that after the current TPCB establishes the connection, the connection success callback function immediately removes the current TPCB from the Tcp_tw_pcbs. It can be seen that the intent is to open a kernel timer for the current TPCB by means of a TCPIP callback. Because the tcp_connect called in a normal process or function has a tcp_reg call, it hangs the current TPCB software timer event on the current thread's software timer list, if the current call to Tcp_connect process is not through Sys_thread_ New established, or is a normal function, at this time even the software timer chain list can not find, naturally there is no timing event triggered, this is also bothering me for a few days why the TPCB kernel timer did not start the reason.
1.4 2 Software timers responsible for disconnection detection and re-sending respectivelyThe project comes with a ttcp.c routine for testing raw TCP, and the main reference to this routine, which bothers me for a few days is that the software timer is used to process the continuation of the data. Because the data may not be passed at once, the TTCP.C uses a software timer to send the remaining data to send the current data, in the actual test most of the time is not a problem, but the reference to this way, but occasionally the occurrence of data can not be sent. Debug discovery is sent buffer full, catch packet discovery is the server constantly reply to an ACK value, and LWIP client did not resend, after Swaiiow five wood reminders, realize that the TCP kernel in LWIP processing is done in a process, frequent trigger timer to perform tpc_write operation, This problem can occur if the TCP kernel execution order is disrupted. The modified version uses Tcp_sent to renew the remaining data, using only one software timer to detect the status of the TPCB.
1.5. Establish the connection and the status of not connectedEstablish connection: current pcb:2000b3f8, state = established has not been connected successfully : current pcb:2000b3f8, state = Syn_sent Server Disconnect:other statesnot connected, occasionally callback Tcp_err in the callback function, need to identify the state, waiting for the processing of the timer
2. Data Exceptions received
made a low-level mistake, put the previous codeMqtt_comm_p->recvlen = p->tot_len; for (data_p = mqtt_comm_p->recvbuf, q = p; Q->next! = NULL; Q = q->next)     {memcpy (data_p, Q->payload, Q->len);data_p + = q->len;    }change to the following codeMqtt_comm_p->recvlen = p->tot_len; data_p = mqtt_comm_p->recvbuf;q = p;   While (q! = NULL)    {memcpy (data_p, Q->payload, Q->len);data_p + = q->len;q = q->next;    }
3. Data cannot be sent immediately and sent out after receiving data
after calling Tcp_write, the data is not sent out immediately, followed by a call to Tcp_ouput to send the data that is hanging. In the current write send function, there is no option to use Tcp_output, because TPCB's kernel timer will call it periodically.

4. Processing of data transmission
Initial idea: Use the queue to complete the filling and sending of data. This will reduce the efficiency of sending data, because it involves copying data into the queue, copying the data out of the queue, and copying the data to the sending queue.
New improvements: No queue, no send buffer, use beacons and pointers to handle sending. This is also not contrary to the original intention of saving memory.

5. Spend a day looking for two hardfault:
5.1 buf points to the resident memory area and then points to the static memory area//vportfree (rtctrlpkgack);//vportfree (picinfopkg);//vportfree (BUF);release error,
5.2 Subscription Topic initialization not fullymqtt_comm_p->mqttpkgsubs->subcount = 1; Only one subscription topic
/////before initializing the loop value to Mqtt_comm_p->mqttpkgsubs->subcount, which is 1 instead of 4, causing the subject name to be used later topicstring internal variable uninitialized, copy data memory hardware error                  . For (i = 0; i < 4; i++)  {mqtt_comm_p->mqttpkgsubs->topicstring[i].cstring = NULL;mqtt_comm_p->mqttpkgsubs->topicstring[i].lenstring.data = NULL;Mqtt_comm_p->mqttpkgsubs->topicstring[i].lenstring.len = 0;//Mqttstring_initializerMqtt_comm_p->mqttpkgsubs->qoss[i] = 0;//quality of service}
Lesson: Memory allocation and release processing, must be careful and careful, to develop a good habit of variable initialization.
6. TPCB control block created in the software timer, unable to send data using Tcp_write
The tcp_connect operation of the 6.1 TPCB block has a tcp_reg call, which is intended to register the current TPCB to the TCP active list, and then create a time-out timer in the TCPIP time-out list. However, Tcp_connect is not called in the TCPIP, it will not be able to create a timer in the TCPIP time-out list, TPCB the kernel timer will not start naturally, the data cannot be sent, which is why Tcpip_callback (tcp_connect_timer_ CB, MQTT_COMM_P->TPCB); The cause;Improvement: Can not call Tcpip_callback in the external program, but directly tcp_connect in the Tcp_reg (), replace the Tcpip_callback, but this changes the kernel code, careful. After the TPCB is disconnected, it is prudent to tcp_rmv the deletion to prevent the next time the new TCP kernel timer sys_timeout cannot be created.
6.2 Do not use the software timer to trigger the resend, so as not to disrupt the TCP kernel data processing, use Tcp_sent to complete the remaining data transmission. 6.3 More than 2 reasons thanks to the Swaiiow five wood point, here very grateful ~.
7. LwIP Raw TCP Sender Reference:
/MQTT data send function static osstatus tcp_mqtt_send (struct mqtt_comm_t *mqtt_comm_p, uint8_t data[], uint32_t len, int msblock, UIn    t8_t apiflag) {mqtt_comm_p->sendbuf = data;    Mqtt_comm_p->sendlen = Len;         Mqtt_comm_p->apiflags = Apiflag;    Sys_log ("Tcp_mqtt_send,sendlen:%d.\r\n", Mqtt_comm_p->sendlen);        Tcp_send_data (mqtt_comm_p);        if (Xsemaphoretake (Mqtt_comm_p->sendsema, msblock)! = pdtrue) {sys_log ("tcp Send timeout.\r\n");    return tcp_mqtt_send_err;   } if (Mqtt_comm_p->sendlen! = 0) return tcp_mqtt_send_err; else return NOERR;}    TCP data send function void Tcp_send_data (struct mqtt_comm_t *mqtt_comm_p) {err_t err;    uint32_t Len;    Len = mqtt_comm_p->sendlen;  Len = (len > Tcp_sndbuf (MQTT_COMM_P-&GT;TPCB))?        Tcp_sndbuf (MQTT_COMM_P-&GT;TPCB): Len; Connection Disconnect if ((MQTT_COMM_P-&GT;TPCB = = NULL) | |    (mqtt_comm_p->tpcb->state! = established)) {xsemaphoregive (Mqtt_comm_p->sendsema);        Sys_log ("TCP_SEND_DATA:TPCB error.\r\n");    Return        }//Send buffer full if (tcp_sndbuf (MQTT_COMM_P-&GT;TPCB) = = 0) {vtaskdelay (1);    Return        } do {err = Tcp_write (MQTT_COMM_P-&GT;TPCB, Mqtt_comm_p->sendbuf, Len, mqtt_comm_p->apiflags);    if (err = = Err_mem) len/= 2;    } while (err = = Err_mem && len > 1);        if (err = = ERR_OK) {Mqtt_comm_p->sendlen-= Len;    Mqtt_comm_p->sendbuf + = Len;    } else Sys_log ("[Tcp_send_data]: Tcp_write failed.\r\n");    }err_t tcp_send_cb (void *arg, struct TCP_PCB *tpcb, u16_t len) {struct mqtt_comm_t *mqtt_comm_p = arg;  if (Mqtt_comm_p->sendlen > 0) {tcp_send_data (mqtt_comm_p);  } else {xsemaphoregive (Mqtt_comm_p->sendsema); } return ERR_OK;}



Mqtt Migration Record
not to be continued

Porting PAHO Open source MQTT libraries using LWIP callback programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.