I want to make a lwip cable on/off bulletproof with DHCP on and a TCP connection. Both solutions work ok but I don't know which one is the more sensible and right. (I am afraid of wrong context function calls)
I call:
tcpip_init(network_init, &net);
and then after that:
xTaskCreate(hc_task, ( signed portCHAR * ) "send hc",1536u, NULL, TCPIP_THREAD_PRIO, NULL);
So there are 2 threads the main lwIP
thread and the hc_task
thread. hc_task
is:
static void hc_task(void *parameters)
{
struct hc_message mes;
mes.client_port_number = 4839;
mes.host_addres = "192.168.1.183\0";
mes.host_add[0] = 192;
mes.host_add[1] = 168;
mes.host_add[2] = 1;
mes.host_add[3] = 183;
mes.host_file = "test/data.php\0";
mes.host_port_number = 80;
mes.target = "http://192.168.1.183/test/data.php\0";
transferEnd = 0;
numberOfPacketsProc = 0;
totalRecievedSize = 0;
hc_send(&mes, 0);
vTaskDelete(NULL);
}
hc_send
does a TCP connection to some page to download it.
Then I have a polling timer with an interval of 2 seconds which checks PHY and reads the cable state:
if cable is on -> ok
if cable is off -> call netif_set_link_down(pxNetIfInUse);
Now next time when the cable goes on I call:
netif_set_link_up(pxNetIfInUse);
I am guessing that is correct to call these functions in a timer interrupt.
The second method is that the timer interrupt sends a message to the main lwip
thread:
Link off -> res = sys_mbox_trypost(toOutput, &msg);
The timer interrupt sends the main lwip thread a message to call either:
netif_set_link_up(pxNetIfInUse);
or:
netif_set_link_down(pxNetIfInUse); //(in lwip main thread)
I have a global variable to store the cable state (saved from that timer interrupt), and I check it in the main lwip
thread to call the right function (link up or down).
So I am asking which solution is the better/right way to do it: in the timer interrupt call link down or up (these are lwip functions) OR the timer interrupt sends a message to main lwIP thread?
My configuration: Microprocessor LPC1768 - LAN8720 PHY - FreeRTOS 7.4 - lwip 1.4.1 - DHCP included - TCP/IP included
After some days in thanks to lwip user support (thanks to Pomeroy Marty, Sylvain Rochet, Richard and other who hellped me - link to debate) forum I came to answer:
In a timer call:
//link off
printf( "Interrupt LINK OFF\n" );
iface_up = 0;
resultCall = tcpip_callback_with_block(tcp_set_link_status, NULL, 0);
//link on
printf("Interrupt LINK ON\n" );
resultCall = tcpip_callback_with_block(tcp_set_link_status, NULL, 0);
So the function below will be executed in thread context of main lwip TCP/IP thread which is correct:
void tcp_set_link_status(void)
{
if(cableState == 0)
{
printf("TCP callback printf set link down \n");
netif_set_link_down(pxNetIfInUse);
}
else
{
printf("TCP callback Printf set link up \n");
netif_set_link_up(pxNetIfInUse);
}
}