ethernetif.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * COPYRIGHT (C) 2006-2010, RT-Thread Development Team
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  20. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  21. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  22. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  26. * OF SUCH DAMAGE.
  27. *
  28. * This file is part of the lwIP TCP/IP stack.
  29. *
  30. * Author: Adam Dunkels <adam@sics.se>
  31. *
  32. * Change Logs:
  33. * Date Author Notes
  34. * 2010-07-07 Bernard fix send mail to mailbox issue.
  35. * 2011-07-30 mbbill port lwIP 1.4.0 to RT-Thread
  36. * 2012-04-10 Bernard add more compatible with RT-Thread.
  37. * 2012-11-12 Bernard The network interface can be initialized
  38. * after lwIP initialization.
  39. * 2013-02-28 aozima fixed list_tcps bug: ipaddr_ntoa isn't reentrant.
  40. */
  41. #include <rtthread.h>
  42. #include "lwip/opt.h"
  43. #include "lwip/debug.h"
  44. #include "lwip/def.h"
  45. #include "lwip/mem.h"
  46. #include "lwip/pbuf.h"
  47. #include "lwip/sys.h"
  48. #include "lwip/netif.h"
  49. #include "lwip/stats.h"
  50. #include "lwip/tcpip.h"
  51. #include "netif/etharp.h"
  52. #include "netif/ethernetif.h"
  53. #include "lwip/inet.h"
  54. #define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
  55. #define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
  56. #ifndef RT_LWIP_ETHTHREAD_PRIORITY
  57. #define RT_ETHERNETIF_THREAD_PREORITY 0x90
  58. #else
  59. #define RT_ETHERNETIF_THREAD_PREORITY RT_LWIP_ETHTHREAD_PRIORITY
  60. #endif
  61. #ifndef LWIP_NO_TX_THREAD
  62. /**
  63. * Tx message structure for Ethernet interface
  64. */
  65. struct eth_tx_msg
  66. {
  67. struct netif *netif;
  68. struct pbuf *buf;
  69. };
  70. static struct rt_mailbox eth_tx_thread_mb;
  71. static struct rt_thread eth_tx_thread;
  72. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  73. static char eth_tx_thread_mb_pool[32 * 4];
  74. static char eth_tx_thread_stack[512];
  75. #else
  76. static char eth_tx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  77. static char eth_tx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  78. #endif
  79. #endif
  80. #ifndef LWIP_NO_RX_THREAD
  81. static struct rt_mailbox eth_rx_thread_mb;
  82. static struct rt_thread eth_rx_thread;
  83. #ifndef RT_LWIP_ETHTHREAD_MBOX_SIZE
  84. static char eth_rx_thread_mb_pool[48 * 4];
  85. static char eth_rx_thread_stack[1024];
  86. #else
  87. static char eth_rx_thread_mb_pool[RT_LWIP_ETHTHREAD_MBOX_SIZE * 4];
  88. static char eth_rx_thread_stack[RT_LWIP_ETHTHREAD_STACKSIZE];
  89. #endif
  90. #endif
  91. #ifdef RT_USING_NETDEV
  92. #include "lwip/ip.h"
  93. #include "lwip/init.h"
  94. #include "lwip/netdb.h"
  95. #include <netdev.h>
  96. static int lwip_netdev_set_up(struct netdev *netif)
  97. {
  98. netif_set_up((struct netif *)netif->user_data);
  99. return ERR_OK;
  100. }
  101. static int lwip_netdev_set_down(struct netdev *netif)
  102. {
  103. netif_set_down((struct netif *)netif->user_data);
  104. return ERR_OK;
  105. }
  106. static int lwip_netdev_set_addr_info(struct netdev *netif, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
  107. {
  108. if (ip_addr && netmask && gw)
  109. {
  110. netif_set_addr((struct netif *)netif->user_data, ip_addr, netmask, gw);
  111. }
  112. else
  113. {
  114. if (ip_addr)
  115. {
  116. netif_set_ipaddr((struct netif *)netif->user_data, ip_addr);
  117. }
  118. if (netmask)
  119. {
  120. netif_set_netmask((struct netif *)netif->user_data, netmask);
  121. }
  122. if (gw)
  123. {
  124. netif_set_gw((struct netif *)netif->user_data, gw);
  125. }
  126. }
  127. return ERR_OK;
  128. }
  129. #ifdef RT_LWIP_DNS
  130. static int lwip_netdev_set_dns_server(struct netdev *netif, uint8_t dns_num, ip_addr_t *dns_server)
  131. {
  132. extern void dns_setserver(uint8_t dns_num, ip_addr_t *dns_server);
  133. dns_setserver(dns_num, dns_server);
  134. return ERR_OK;
  135. }
  136. #endif /* RT_LWIP_DNS */
  137. #ifdef RT_LWIP_DHCP
  138. static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled)
  139. {
  140. netdev_low_level_set_dhcp_status(netif, is_enabled);
  141. if(RT_TRUE == is_enabled)
  142. {
  143. dhcp_start((struct netif *)netif->user_data);
  144. }
  145. else
  146. {
  147. dhcp_stop((struct netif *)netif->user_data);
  148. }
  149. return ERR_OK;
  150. }
  151. #endif /* RT_LWIP_DHCP */
  152. #ifdef RT_USING_FINSH
  153. #ifdef RT_LWIP_USING_PING
  154. extern int lwip_ping_recv(int s, int *ttl);
  155. extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
  156. int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
  157. uint32_t timeout, struct netdev_ping_resp *ping_resp)
  158. {
  159. int s, ttl, recv_len, result = 0;
  160. int elapsed_time;
  161. rt_tick_t recv_start_tick;
  162. #if LWIP_VERSION_MAJOR >= 2U
  163. struct timeval recv_timeout = { timeout / RT_TICK_PER_SECOND, timeout % RT_TICK_PER_SECOND };
  164. #else
  165. int recv_timeout = timeout * 1000UL / RT_TICK_PER_SECOND;
  166. #endif
  167. ip_addr_t target_addr;
  168. struct addrinfo hint, *res = RT_NULL;
  169. struct sockaddr_in *h = RT_NULL;
  170. struct in_addr ina;
  171. RT_ASSERT(netif);
  172. RT_ASSERT(host);
  173. RT_ASSERT(ping_resp);
  174. rt_memset(&hint, 0x00, sizeof(hint));
  175. /* convert URL to IP */
  176. if (lwip_getaddrinfo(host, RT_NULL, &hint, &res) != 0)
  177. {
  178. return -RT_ERROR;
  179. }
  180. SMEMCPY(&h, &res->ai_addr, sizeof(struct sockaddr_in *));
  181. SMEMCPY(&ina, &h->sin_addr, sizeof(ina));
  182. lwip_freeaddrinfo(res);
  183. if (inet_aton(inet_ntoa(ina), &target_addr) == 0)
  184. {
  185. return -RT_ERROR;
  186. }
  187. SMEMCPY(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
  188. /* new a socket */
  189. if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
  190. {
  191. return -RT_ERROR;
  192. }
  193. lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &recv_timeout, sizeof(recv_timeout));
  194. if (lwip_ping_send(s, &target_addr, data_len) == ERR_OK)
  195. {
  196. recv_start_tick = rt_tick_get();
  197. if ((recv_len = lwip_ping_recv(s, &ttl)) >= 0)
  198. {
  199. elapsed_time = (rt_tick_get() - recv_start_tick) * 1000UL / RT_TICK_PER_SECOND;
  200. ping_resp->data_len = recv_len;
  201. ping_resp->ttl = ttl;
  202. ping_resp->ticks = elapsed_time;
  203. }
  204. else
  205. {
  206. result = -RT_ETIMEOUT;
  207. goto __exit;
  208. }
  209. }
  210. else
  211. {
  212. result = -RT_ETIMEOUT;
  213. goto __exit;
  214. }
  215. __exit:
  216. lwip_close(s);
  217. return result;
  218. }
  219. #endif /* RT_LWIP_USING_PING */
  220. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  221. void lwip_netdev_netstat(struct netdev *netif)
  222. {
  223. extern void list_tcps(void);
  224. extern void list_udps(void);
  225. #ifdef RT_LWIP_TCP
  226. list_tcps();
  227. #endif
  228. #ifdef RT_LWIP_UDP
  229. list_udps();
  230. #endif
  231. }
  232. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  233. #endif /* RT_USING_FINSH */
  234. static int lwip_netdev_set_default(struct netdev *netif)
  235. {
  236. netif_set_default((struct netif *)netif->user_data);
  237. return ERR_OK;
  238. }
  239. const struct netdev_ops lwip_netdev_ops =
  240. {
  241. lwip_netdev_set_up,
  242. lwip_netdev_set_down,
  243. lwip_netdev_set_addr_info,
  244. #ifdef RT_LWIP_DNS
  245. lwip_netdev_set_dns_server,
  246. #else
  247. NULL,
  248. #endif /* RT_LWIP_DNS */
  249. #ifdef RT_LWIP_DHCP
  250. lwip_netdev_set_dhcp,
  251. #else
  252. NULL,
  253. #endif /* RT_LWIP_DHCP */
  254. #ifdef RT_USING_FINSH
  255. #ifdef RT_LWIP_USING_PING
  256. lwip_netdev_ping,
  257. #else
  258. NULL,
  259. #endif /* RT_LWIP_USING_PING */
  260. #if defined (RT_LWIP_TCP) || defined (RT_LWIP_UDP)
  261. lwip_netdev_netstat,
  262. #endif /* RT_LWIP_TCP || RT_LWIP_UDP */
  263. #endif /* RT_USING_FINSH */
  264. lwip_netdev_set_default,
  265. };
  266. static int netdev_add(struct netif *lwip_netif)
  267. {
  268. #define LWIP_NETIF_NAME_LEN 2
  269. int result = 0;
  270. struct netdev *netdev = RT_NULL;
  271. char name[LWIP_NETIF_NAME_LEN + 1] = {0};
  272. RT_ASSERT(lwip_netif);
  273. netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
  274. if (netdev == RT_NULL)
  275. {
  276. return -ERR_IF;
  277. }
  278. #ifdef SAL_USING_LWIP
  279. extern int sal_lwip_netdev_set_pf_info(struct netdev *netdev);
  280. /* set the lwIP network interface device protocol family information */
  281. sal_lwip_netdev_set_pf_info(netdev);
  282. #endif /* SAL_USING_LWIP */
  283. rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
  284. result = netdev_register(netdev, name, (void *)lwip_netif);
  285. /* Update netdev info after registered */
  286. netdev->flags = lwip_netif->flags;
  287. netdev->mtu = lwip_netif->mtu;
  288. netdev->ops = &lwip_netdev_ops;
  289. netdev->hwaddr_len = lwip_netif->hwaddr_len;
  290. SMEMCPY(netdev->hwaddr, lwip_netif->hwaddr, lwip_netif->hwaddr_len);
  291. netdev->ip_addr = lwip_netif->ip_addr;
  292. netdev->gw = lwip_netif->gw;
  293. netdev->netmask = lwip_netif->netmask;
  294. #ifdef RT_LWIP_DHCP
  295. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  296. #endif
  297. return result;
  298. }
  299. /* synchronize lwIP network interface device and network interface device flags */
  300. static int netdev_flags_sync(struct netif *lwip_netif)
  301. {
  302. struct netdev *netdev = NULL;
  303. RT_ASSERT(lwip_netif);
  304. netdev = netdev_get_by_name(lwip_netif->name);
  305. if (netdev == RT_NULL)
  306. {
  307. return -ERR_IF;
  308. }
  309. netdev->flags |= lwip_netif->flags;
  310. return ERR_OK;
  311. }
  312. #endif /* RT_USING_NETDEV */
  313. static err_t ethernetif_linkoutput(struct netif *netif, struct pbuf *p)
  314. {
  315. #ifndef LWIP_NO_TX_THREAD
  316. struct eth_tx_msg msg;
  317. struct eth_device* enetif;
  318. RT_ASSERT(netif != RT_NULL);
  319. enetif = (struct eth_device*)netif->state;
  320. /* send a message to eth tx thread */
  321. msg.netif = netif;
  322. msg.buf = p;
  323. if (rt_mb_send(&eth_tx_thread_mb, (rt_uint32_t) &msg) == RT_EOK)
  324. {
  325. /* waiting for ack */
  326. rt_sem_take(&(enetif->tx_ack), RT_WAITING_FOREVER);
  327. }
  328. #else
  329. struct eth_device* enetif;
  330. RT_ASSERT(netif != RT_NULL);
  331. enetif = (struct eth_device*)netif->state;
  332. if (enetif->eth_tx(&(enetif->parent), p) != RT_EOK)
  333. {
  334. return ERR_IF;
  335. }
  336. #endif
  337. return ERR_OK;
  338. }
  339. static err_t eth_netif_device_init(struct netif *netif)
  340. {
  341. struct eth_device *ethif;
  342. ethif = (struct eth_device*)netif->state;
  343. if (ethif != RT_NULL)
  344. {
  345. rt_device_t device;
  346. #ifdef RT_USING_NETDEV
  347. /* network interface device register */
  348. netdev_add(netif);
  349. #endif /* RT_USING_NETDEV */
  350. /* get device object */
  351. device = (rt_device_t) ethif;
  352. if (rt_device_init(device) != RT_EOK)
  353. {
  354. return ERR_IF;
  355. }
  356. /* copy device flags to netif flags */
  357. netif->flags = (ethif->flags & 0xff);
  358. /* set default netif */
  359. if (netif_default == RT_NULL)
  360. netif_set_default(ethif->netif);
  361. #if LWIP_DHCP
  362. if (ethif->flags & NETIF_FLAG_DHCP)
  363. {
  364. /* if this interface uses DHCP, start the DHCP client */
  365. dhcp_start(ethif->netif);
  366. }
  367. else
  368. #endif
  369. {
  370. /* set interface up */
  371. netif_set_up(ethif->netif);
  372. }
  373. if (!(ethif->flags & ETHIF_LINK_PHYUP))
  374. {
  375. /* set link_up for this netif */
  376. netif_set_link_up(ethif->netif);
  377. }
  378. return ERR_OK;
  379. }
  380. return ERR_IF;
  381. }
  382. /* Keep old drivers compatible in RT-Thread */
  383. rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
  384. {
  385. struct netif* netif;
  386. #if LWIP_NETIF_HOSTNAME
  387. #define LWIP_HOSTNAME_LEN 16
  388. char *hostname = RT_NULL;
  389. netif = (struct netif*) rt_calloc (1, sizeof(struct netif) + LWIP_HOSTNAME_LEN);
  390. #else
  391. netif = (struct netif*) rt_calloc (1, sizeof(struct netif));
  392. #endif
  393. if (netif == RT_NULL)
  394. {
  395. rt_kprintf("malloc netif failed\n");
  396. return -RT_ERROR;
  397. }
  398. /* set netif */
  399. dev->netif = netif;
  400. /* device flags, which will be set to netif flags when initializing */
  401. dev->flags = flags;
  402. /* link changed status of device */
  403. dev->link_changed = 0x00;
  404. /* avoid send the same mail to mailbox */
  405. dev->rx_notice = 0x00;
  406. dev->parent.type = RT_Device_Class_NetIf;
  407. /* register to RT-Thread device manager */
  408. rt_device_register(&(dev->parent), name, RT_DEVICE_FLAG_RDWR);
  409. rt_sem_init(&(dev->tx_ack), name, 0, RT_IPC_FLAG_FIFO);
  410. /* set name */
  411. netif->name[0] = name[0];
  412. netif->name[1] = name[1];
  413. /* set hw address to 6 */
  414. netif->hwaddr_len = 6;
  415. /* maximum transfer unit */
  416. netif->mtu = ETHERNET_MTU;
  417. /* get hardware MAC address */
  418. rt_device_control(&(dev->parent), NIOCTL_GADDR, netif->hwaddr);
  419. /* set output */
  420. netif->output = etharp_output;
  421. netif->linkoutput = ethernetif_linkoutput;
  422. #if LWIP_NETIF_HOSTNAME
  423. /* Initialize interface hostname */
  424. hostname = (char *)netif + sizeof(struct netif);
  425. rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
  426. netif->hostname = hostname;
  427. #endif /* LWIP_NETIF_HOSTNAME */
  428. /* if tcp thread has been started up, we add this netif to the system */
  429. if (rt_thread_find("tcpip") != RT_NULL)
  430. {
  431. struct ip_addr ipaddr, netmask, gw;
  432. #if LWIP_DHCP
  433. if (dev->flags & NETIF_FLAG_DHCP)
  434. {
  435. IP4_ADDR(&ipaddr, 0, 0, 0, 0);
  436. IP4_ADDR(&gw, 0, 0, 0, 0);
  437. IP4_ADDR(&netmask, 0, 0, 0, 0);
  438. }
  439. else
  440. #endif
  441. {
  442. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  443. gw.addr = inet_addr(RT_LWIP_GWADDR);
  444. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  445. }
  446. netifapi_netif_add(netif, &ipaddr, &netmask, &gw, dev, eth_netif_device_init, tcpip_input);
  447. }
  448. #ifdef RT_USING_NETDEV
  449. /* network interface device flags synchronize */
  450. netdev_flags_sync(netif);
  451. #endif /* RT_USING_NETDEV */
  452. return RT_EOK;
  453. }
  454. rt_err_t eth_device_init(struct eth_device * dev, const char *name)
  455. {
  456. rt_uint16_t flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  457. #if LWIP_DHCP
  458. /* DHCP support */
  459. flags |= NETIF_FLAG_DHCP;
  460. #endif
  461. #if LWIP_IGMP
  462. /* IGMP support */
  463. flags |= NETIF_FLAG_IGMP;
  464. #endif
  465. return eth_device_init_with_flag(dev, name, flags);
  466. }
  467. #ifndef LWIP_NO_RX_THREAD
  468. rt_err_t eth_device_ready(struct eth_device* dev)
  469. {
  470. if (dev->netif)
  471. {
  472. if(dev->rx_notice == RT_FALSE)
  473. {
  474. dev->rx_notice = RT_TRUE;
  475. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  476. }
  477. else
  478. return RT_EOK;
  479. /* post message to Ethernet thread */
  480. }
  481. else
  482. return -RT_ERROR; /* netif is not initialized yet, just return. */
  483. }
  484. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  485. {
  486. rt_uint32_t level;
  487. RT_ASSERT(dev != RT_NULL);
  488. level = rt_hw_interrupt_disable();
  489. dev->link_changed = 0x01;
  490. if (up == RT_TRUE)
  491. dev->link_status = 0x01;
  492. else
  493. dev->link_status = 0x00;
  494. rt_hw_interrupt_enable(level);
  495. /* post message to ethernet thread */
  496. return rt_mb_send(&eth_rx_thread_mb, (rt_uint32_t)dev);
  497. }
  498. #else
  499. /* NOTE: please not use it in interrupt when no RxThread exist */
  500. rt_err_t eth_device_linkchange(struct eth_device* dev, rt_bool_t up)
  501. {
  502. if (up == RT_TRUE)
  503. netifapi_netif_set_link_up(dev->netif);
  504. else
  505. netifapi_netif_set_link_down(dev->netif);
  506. return RT_EOK;
  507. }
  508. #endif
  509. #ifndef LWIP_NO_TX_THREAD
  510. /* Ethernet Tx Thread */
  511. static void eth_tx_thread_entry(void* parameter)
  512. {
  513. struct eth_tx_msg* msg;
  514. while (1)
  515. {
  516. if (rt_mb_recv(&eth_tx_thread_mb, (rt_ubase_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
  517. {
  518. struct eth_device* enetif;
  519. RT_ASSERT(msg->netif != RT_NULL);
  520. RT_ASSERT(msg->buf != RT_NULL);
  521. enetif = (struct eth_device*)msg->netif->state;
  522. if (enetif != RT_NULL)
  523. {
  524. /* call driver's interface */
  525. if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
  526. {
  527. /* transmit eth packet failed */
  528. }
  529. }
  530. /* send ACK */
  531. rt_sem_release(&(enetif->tx_ack));
  532. }
  533. }
  534. }
  535. #endif
  536. #ifndef LWIP_NO_RX_THREAD
  537. /* Ethernet Rx Thread */
  538. static void eth_rx_thread_entry(void* parameter)
  539. {
  540. struct eth_device* device;
  541. while (1)
  542. {
  543. if (rt_mb_recv(&eth_rx_thread_mb, (rt_ubase_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
  544. {
  545. rt_base_t level;
  546. struct pbuf *p;
  547. /* check link status */
  548. if (device->link_changed)
  549. {
  550. int status;
  551. rt_uint32_t level;
  552. level = rt_hw_interrupt_disable();
  553. status = device->link_status;
  554. device->link_changed = 0x00;
  555. rt_hw_interrupt_enable(level);
  556. if (status)
  557. netifapi_netif_set_link_up(device->netif);
  558. else
  559. netifapi_netif_set_link_down(device->netif);
  560. }
  561. level = rt_hw_interrupt_disable();
  562. /* 'rx_notice' will be modify in the interrupt or here */
  563. device->rx_notice = RT_FALSE;
  564. rt_hw_interrupt_enable(level);
  565. /* receive all of buffer */
  566. while(1)
  567. {
  568. if(device->eth_rx == RT_NULL) break;
  569. p = device->eth_rx(&(device->parent));
  570. if (p != RT_NULL)
  571. {
  572. /* notify to upper layer */
  573. if( device->netif->input(p, device->netif) != ERR_OK )
  574. {
  575. LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input error\n"));
  576. pbuf_free(p);
  577. p = NULL;
  578. }
  579. }
  580. else break;
  581. }
  582. }
  583. else
  584. {
  585. LWIP_ASSERT("Should not happen!\n",0);
  586. }
  587. }
  588. }
  589. #endif
  590. int eth_system_device_init(void)
  591. {
  592. rt_err_t result = RT_EOK;
  593. /* initialize Rx thread. */
  594. #ifndef LWIP_NO_RX_THREAD
  595. /* initialize mailbox and create Ethernet Rx thread */
  596. result = rt_mb_init(&eth_rx_thread_mb, "erxmb",
  597. &eth_rx_thread_mb_pool[0], sizeof(eth_rx_thread_mb_pool)/4,
  598. RT_IPC_FLAG_FIFO);
  599. RT_ASSERT(result == RT_EOK);
  600. result = rt_thread_init(&eth_rx_thread, "erx", eth_rx_thread_entry, RT_NULL,
  601. &eth_rx_thread_stack[0], sizeof(eth_rx_thread_stack),
  602. RT_ETHERNETIF_THREAD_PREORITY, 16);
  603. RT_ASSERT(result == RT_EOK);
  604. result = rt_thread_startup(&eth_rx_thread);
  605. RT_ASSERT(result == RT_EOK);
  606. #endif
  607. /* initialize Tx thread */
  608. #ifndef LWIP_NO_TX_THREAD
  609. /* initialize mailbox and create Ethernet Tx thread */
  610. result = rt_mb_init(&eth_tx_thread_mb, "etxmb",
  611. &eth_tx_thread_mb_pool[0], sizeof(eth_tx_thread_mb_pool)/4,
  612. RT_IPC_FLAG_FIFO);
  613. RT_ASSERT(result == RT_EOK);
  614. result = rt_thread_init(&eth_tx_thread, "etx", eth_tx_thread_entry, RT_NULL,
  615. &eth_tx_thread_stack[0], sizeof(eth_tx_thread_stack),
  616. RT_ETHERNETIF_THREAD_PREORITY, 16);
  617. RT_ASSERT(result == RT_EOK);
  618. result = rt_thread_startup(&eth_tx_thread);
  619. RT_ASSERT(result == RT_EOK);
  620. #endif
  621. return (int)result;
  622. }
  623. INIT_PREV_EXPORT(eth_system_device_init);
  624. void set_if(char* netif_name, char* ip_addr, char* gw_addr, char* nm_addr)
  625. {
  626. struct ip_addr *ip;
  627. struct ip_addr addr;
  628. struct netif * netif = netif_list;
  629. if(strlen(netif_name) > sizeof(netif->name))
  630. {
  631. rt_kprintf("network interface name too long!\r\n");
  632. return;
  633. }
  634. while(netif != RT_NULL)
  635. {
  636. if(strncmp(netif_name, netif->name, sizeof(netif->name)) == 0)
  637. break;
  638. netif = netif->next;
  639. if( netif == RT_NULL )
  640. {
  641. rt_kprintf("network interface: %s not found!\r\n", netif_name);
  642. return;
  643. }
  644. }
  645. ip = (struct ip_addr *)&addr;
  646. /* set ip address */
  647. if ((ip_addr != RT_NULL) && ipaddr_aton(ip_addr, &addr))
  648. {
  649. netif_set_ipaddr(netif, ip);
  650. }
  651. /* set gateway address */
  652. if ((gw_addr != RT_NULL) && ipaddr_aton(gw_addr, &addr))
  653. {
  654. netif_set_gw(netif, ip);
  655. }
  656. /* set netmask address */
  657. if ((nm_addr != RT_NULL) && ipaddr_aton(nm_addr, &addr))
  658. {
  659. netif_set_netmask(netif, ip);
  660. }
  661. }
  662. #ifdef RT_USING_FINSH
  663. #include <finsh.h>
  664. FINSH_FUNCTION_EXPORT(set_if, set network interface address);
  665. #if LWIP_DNS
  666. #include <lwip/dns.h>
  667. void set_dns(uint8_t dns_num, char* dns_server)
  668. {
  669. struct ip_addr addr;
  670. if ((dns_server != RT_NULL) && ipaddr_aton(dns_server, &addr))
  671. {
  672. dns_setserver(dns_num, &addr);
  673. }
  674. }
  675. FINSH_FUNCTION_EXPORT(set_dns, set DNS server address);
  676. #endif
  677. void list_if(void)
  678. {
  679. rt_ubase_t index;
  680. struct netif * netif;
  681. rt_enter_critical();
  682. netif = netif_list;
  683. while( netif != RT_NULL )
  684. {
  685. rt_kprintf("network interface: %c%c%s\n",
  686. netif->name[0],
  687. netif->name[1],
  688. (netif == netif_default)?" (Default)":"");
  689. rt_kprintf("MTU: %d\n", netif->mtu);
  690. rt_kprintf("MAC: ");
  691. for (index = 0; index < netif->hwaddr_len; index ++)
  692. rt_kprintf("%02x ", netif->hwaddr[index]);
  693. rt_kprintf("\nFLAGS:");
  694. if (netif->flags & NETIF_FLAG_UP) rt_kprintf(" UP");
  695. else rt_kprintf(" DOWN");
  696. if (netif->flags & NETIF_FLAG_LINK_UP) rt_kprintf(" LINK_UP");
  697. else rt_kprintf(" LINK_DOWN");
  698. if (netif->flags & NETIF_FLAG_DHCP) rt_kprintf(" DHCP");
  699. if (netif->flags & NETIF_FLAG_POINTTOPOINT) rt_kprintf(" PPP");
  700. if (netif->flags & NETIF_FLAG_ETHARP) rt_kprintf(" ETHARP");
  701. if (netif->flags & NETIF_FLAG_IGMP) rt_kprintf(" IGMP");
  702. rt_kprintf("\n");
  703. rt_kprintf("ip address: %s\n", ipaddr_ntoa(&(netif->ip_addr)));
  704. rt_kprintf("gw address: %s\n", ipaddr_ntoa(&(netif->gw)));
  705. rt_kprintf("net mask : %s\n", ipaddr_ntoa(&(netif->netmask)));
  706. rt_kprintf("\r\n");
  707. netif = netif->next;
  708. }
  709. #if LWIP_DNS
  710. {
  711. struct ip_addr ip_addr;
  712. for(index=0; index<DNS_MAX_SERVERS; index++)
  713. {
  714. ip_addr = dns_getserver(index);
  715. rt_kprintf("dns server #%d: %s\n", index, ipaddr_ntoa(&(ip_addr)));
  716. }
  717. }
  718. #endif /**< #if LWIP_DNS */
  719. rt_exit_critical();
  720. }
  721. FINSH_FUNCTION_EXPORT(list_if, list network interface information);
  722. #if LWIP_TCP
  723. #include <lwip/tcp.h>
  724. #include <lwip/tcp_impl.h>
  725. void list_tcps(void)
  726. {
  727. rt_uint32_t num = 0;
  728. struct tcp_pcb *pcb;
  729. char local_ip_str[16];
  730. char remote_ip_str[16];
  731. extern struct tcp_pcb *tcp_active_pcbs;
  732. extern union tcp_listen_pcbs_t tcp_listen_pcbs;
  733. extern struct tcp_pcb *tcp_tw_pcbs;
  734. extern const char *tcp_state_str[];
  735. rt_enter_critical();
  736. rt_kprintf("Active PCB states:\n");
  737. for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  738. {
  739. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  740. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  741. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  742. num++,
  743. local_ip_str,
  744. pcb->local_port,
  745. remote_ip_str,
  746. pcb->remote_port,
  747. pcb->snd_nxt,
  748. pcb->rcv_nxt);
  749. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  750. }
  751. rt_kprintf("Listen PCB states:\n");
  752. num = 0;
  753. for(pcb = (struct tcp_pcb *)tcp_listen_pcbs.pcbs; pcb != NULL; pcb = pcb->next)
  754. {
  755. rt_kprintf("#%d local port %d ", num++, pcb->local_port);
  756. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  757. }
  758. rt_kprintf("TIME-WAIT PCB states:\n");
  759. num = 0;
  760. for(pcb = tcp_tw_pcbs; pcb != NULL; pcb = pcb->next)
  761. {
  762. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  763. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  764. rt_kprintf("#%d %s:%d <==> %s:%d snd_nxt 0x%08X rcv_nxt 0x%08X ",
  765. num++,
  766. local_ip_str,
  767. pcb->local_port,
  768. remote_ip_str,
  769. pcb->remote_port,
  770. pcb->snd_nxt,
  771. pcb->rcv_nxt);
  772. rt_kprintf("state: %s\n", tcp_state_str[pcb->state]);
  773. }
  774. rt_exit_critical();
  775. }
  776. FINSH_FUNCTION_EXPORT(list_tcps, list all of tcp connections);
  777. #endif
  778. #if LWIP_UDP
  779. #include "lwip/udp.h"
  780. void list_udps(void)
  781. {
  782. struct udp_pcb *pcb;
  783. rt_uint32_t num = 0;
  784. char local_ip_str[16];
  785. char remote_ip_str[16];
  786. rt_enter_critical();
  787. rt_kprintf("Active UDP PCB states:\n");
  788. for (pcb = udp_pcbs; pcb != NULL; pcb = pcb->next)
  789. {
  790. strcpy(local_ip_str, ipaddr_ntoa(&(pcb->local_ip)));
  791. strcpy(remote_ip_str, ipaddr_ntoa(&(pcb->remote_ip)));
  792. rt_kprintf("#%d %d %s:%d <==> %s:%d \n",
  793. num, (int)pcb->flags,
  794. local_ip_str,
  795. pcb->local_port,
  796. remote_ip_str,
  797. pcb->remote_port);
  798. num++;
  799. }
  800. rt_exit_critical();
  801. }
  802. FINSH_FUNCTION_EXPORT(list_udps, list all of udp connections);
  803. #endif /* LWIP_UDP */
  804. #endif