title: Best way to implement interthread communication in FreeRTOS? - checking the state of a lwIP cable
body: <p>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) </p>
  
  <ul>
  <li>First solution:</li>
  </ul>
  
  <p>I call:</p>
  
  <pre><code>tcpip_init(network_init, &amp;net);
  </code></pre>
  
  <p>and then after that: </p>
  
  <pre><code>xTaskCreate(hc_task, ( signed portCHAR * ) "send hc",1536u, NULL, TCPIP_THREAD_PRIO, NULL);
  </code></pre>
  
  <p>So there are 2 threads the main <code>lwIP</code> thread and the <code>hc_task</code> thread. <code>hc_task</code> is:</p>
  
  <pre><code>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(&amp;mes, 0);
      vTaskDelete(NULL);
  }
  </code></pre>
  
  <p><code>hc_send</code> does a TCP connection to some page to download it.</p>
  
  <p>Then I have a polling timer with an interval of 2 seconds which checks PHY and reads the cable state:</p>
  
  <pre><code>if cable is on  -&gt; ok
  if cable is off -&gt; call netif_set_link_down(pxNetIfInUse);
  </code></pre>
  
  <p>Now next time when the cable goes on I call:</p>
  
  <pre><code>netif_set_link_up(pxNetIfInUse);
  </code></pre>
  
  <p>I am guessing that is correct to call these functions in a timer interrupt.</p>
  
  <ul>
  <li>Second solution:</li>
  </ul>
  
  <p>The second method is that the timer interrupt sends a message to the main <code>lwip</code> thread:</p>
  
  <pre><code>Link off -&gt; res = sys_mbox_trypost(toOutput, &amp;msg); 
  </code></pre>
  
  <p>The timer interrupt sends the main lwip thread a message to call either: </p>
  
  <pre><code>netif_set_link_up(pxNetIfInUse);
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>netif_set_link_down(pxNetIfInUse); //(in lwip main thread)
  </code></pre>
  
  <p>I have a global variable to store the cable state (saved from that timer interrupt), and I check it in the main <code>lwip</code> thread to call the right function (link up or down).</p>
  
  <p>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?</p>
  
  <p>My configuration:
  Microprocessor LPC1768
  - LAN8720 PHY
  - FreeRTOS 7.4
  - lwip 1.4.1
  - DHCP included
  - TCP/IP included</p>