uip_ethernetif.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * File : ethernetif.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-07-07 Bernard fix ethernetif_linkoutput send mail issue.
  13. */
  14. /*
  15. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without modification,
  19. * are permitted provided that the following conditions are met:
  20. *
  21. * 1. Redistributions of source code must retain the above copyright notice,
  22. * this list of conditions and the following disclaimer.
  23. * 2. Redistributions in binary form must reproduce the above copyright notice,
  24. * this list of conditions and the following disclaimer in the documentation
  25. * and/or other materials provided with the distribution.
  26. * 3. The name of the author may not be used to endorse or promote products
  27. * derived from this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  32. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  34. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  38. * OF SUCH DAMAGE.
  39. *
  40. * This file is part of the lwIP TCP/IP stack.
  41. *
  42. * Author: Adam Dunkels <adam@sics.se>
  43. *
  44. */
  45. #include <rtthread.h>
  46. #include "uip.h"
  47. #include "uip_arp.h"
  48. #include "uip_netif.h"
  49. #include "uip_ethernetif.h"
  50. #include "uip_ipaddr.h"
  51. #ifndef NULL
  52. #define NULL (void *)0
  53. #endif /* NULL */
  54. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
  55. extern u16_t uip_len, uip_slen;
  56. /* eth rx/tx thread */
  57. static struct rt_mailbox eth_rx_thread_mb;
  58. static struct rt_thread eth_rx_thread;
  59. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  60. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  61. static char eth_rx_thread_mb_pool[48 * 4];
  62. static char eth_rx_thread_stack[1024];
  63. #else
  64. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  65. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  66. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  67. #endif
  68. struct eth_tx_msg
  69. {
  70. struct netif *netif;
  71. struct pbuf *buf;
  72. };
  73. static struct rt_mailbox eth_tx_thread_mb;
  74. static struct rt_thread eth_tx_thread;
  75. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  76. static char eth_tx_thread_mb_pool[32 * 4];
  77. static char eth_tx_thread_stack[512];
  78. #else
  79. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  80. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  81. #endif
  82. /* the interface provided to uIP */
  83. err_t eth_init(struct netif *netif)
  84. {
  85. return 0;
  86. }
  87. extern err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p);
  88. err_t eth_input(struct pbuf *p,struct netif *inp)
  89. {
  90. struct eth_hdr *ethhdr;
  91. if(p != RT_NULL)
  92. {
  93. #ifdef LINK_STATS
  94. //LINK_STATS_INC(link.recv);
  95. #endif LINK_STATS
  96. ethhdr = p->payload;
  97. switch(uip_htons(ethhdr->type))
  98. {
  99. case ETHTYPE_IP: //ETHTYPE_IP
  100. etharp_ip_input(inp, p);
  101. pbuf_header(p, -((rt_int16_t)sizeof(struct eth_hdr)));
  102. if (tcpip_input(p, inp) != ERR_OK)
  103. {
  104. // discard packet
  105. pbuf_free(p);
  106. }
  107. break;
  108. case ETHTYPE_ARP:
  109. etharp_arp_input(inp, (struct eth_addr *)inp->hwaddr, p);
  110. break;
  111. default:
  112. pbuf_free(p);
  113. p = RT_NULL;
  114. break;
  115. }
  116. }
  117. return ERR_OK;
  118. }
  119. err_t ethernetif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
  120. {
  121. return etharp_output(netif, p, ipaddr);
  122. }
  123. err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  124. {
  125. struct eth_tx_msg msg;
  126. struct eth_device* enetif;
  127. enetif = (struct eth_device*)netif->state;
  128. /* send a message to eth tx thread */
  129. msg.netif = netif;
  130. msg.buf = p;
  131. if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
  132. {
  133. /* waiting for ack */
  134. rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
  135. }
  136. return ERR_OK;
  137. }
  138. /* ethernetif APIs */
  139. rt_err_t eth_device_init(struct eth_device* dev, const char* name)
  140. {
  141. struct netif* netif;
  142. uip_ipaddr_t ipaddr;
  143. netif = (struct netif*) rt_malloc (sizeof(struct netif));
  144. if (netif == RT_NULL)
  145. {
  146. rt_kprintf("malloc netif failed\n");
  147. return -RT_ERROR;
  148. }
  149. rt_memset(netif, 0, sizeof(struct netif));
  150. /* set netif */
  151. dev->netif = netif;
  152. /* register to rt-thread device manager */
  153. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  154. dev->parent.type = RT_Device_Class_NetIf;
  155. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  156. /* set name */
  157. netif->name[0] = name[0];
  158. netif->name[1] = name[1];
  159. /* set hw address to 6 */
  160. netif->hwaddr_len = 6;
  161. /* maximum transfer unit */
  162. netif->mtu = ETHERNET_MTU;
  163. /* broadcast capability */
  164. netif->flags = NETIF_FLAG_BROADCAST;
  165. #if LWIP_IGMP
  166. /* igmp support */
  167. netif->flags |= NETIF_FLAG_IGMP;
  168. #endif
  169. /* get hardware address */
  170. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  171. /* set output */
  172. netif->output = ethernetif_output;
  173. netif->linkoutput = ethernetif_linkoutput;
  174. /* add netif to lwip */
  175. if (netif_add(netif, IP_ADDR_ANY, IP_ADDR_BROADCAST, IP_ADDR_ANY, dev,
  176. eth_init, eth_input) == RT_NULL)
  177. {
  178. /* failed, unregister device and free netif */
  179. rt_device_unregister(&(dev->parent));
  180. rt_free(netif);
  181. return -RT_ERROR;
  182. }
  183. netif_set_default(netif);
  184. return RT_EOK;
  185. }
  186. /* ethernet buffer */
  187. void eth_tx_thread_entry(void* parameter)
  188. {
  189. struct eth_tx_msg* msg;
  190. while (1)
  191. {
  192. if (rt_mb_recv(&eth_tx_thread_mb, (rt_uint32_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
  193. {
  194. struct eth_device* enetif;
  195. RT_ASSERT(msg->netif != RT_NULL);
  196. RT_ASSERT(msg->buf != RT_NULL);
  197. enetif = (struct eth_device*)msg->netif->state;
  198. if (enetif != RT_NULL)
  199. {
  200. /* call driver's interface */
  201. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  202. {
  203. rt_kprintf("transmit eth packet failed\n");
  204. }
  205. }
  206. /* send ack */
  207. rt_sem_release(&(enetif->tx_ack));
  208. }
  209. }
  210. }
  211. /* ethernet buffer */
  212. void eth_rx_thread_entry(void* parameter)
  213. {
  214. struct eth_device* device;
  215. while (1)
  216. {
  217. if (rt_mb_recv(&eth_rx_thread_mb, (rt_uint32_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
  218. {
  219. struct pbuf *p;
  220. /* receive all of buffer */
  221. while (1)
  222. {
  223. p = device->eth_rx(&(device->parent));
  224. if (p != RT_NULL)
  225. {
  226. /* notify to upper layer */
  227. eth_input(p, device->netif);
  228. }
  229. else break;
  230. }
  231. }
  232. }
  233. }
  234. rt_err_t eth_device_ready(struct eth_device* dev)
  235. {
  236. /* post message to ethernet thread */
  237. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  238. }
  239. rt_err_t eth_system_device_init()
  240. {
  241. rt_err_t result = RT_EOK;
  242. /* init rx thread */
  243. /* init mailbox and create ethernet thread */
  244. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  245. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  246. RT_IPC_FLAG_FIFO);
  247. RT_ASSERT(result == RT_EOK);
  248. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  249. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  250. RT_ETHERNETIF_THREAD_PREORITY, 16);
  251. RT_ASSERT(result == RT_EOK);
  252. result = rt_thread_startup(&eth_rx_thread);
  253. RT_ASSERT(result == RT_EOK);
  254. /* init tx thread */
  255. /* init mailbox and create ethernet thread */
  256. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  257. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  258. RT_IPC_FLAG_FIFO);
  259. RT_ASSERT(result == RT_EOK);
  260. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  261. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  262. RT_ETHERNETIF_THREAD_PREORITY, 16);
  263. RT_ASSERT(result == RT_EOK);
  264. result = rt_thread_startup(&eth_tx_thread);
  265. RT_ASSERT(result == RT_EOK);
  266. return result;
  267. }