sys_arch.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-12-8 Bernard add file header
  9. * export bsd socket symbol for RT-Thread Application Module
  10. * 2013-05-25 Bernard port to v1.4.1
  11. * 2017-03-26 HuangXiHans port to v2.0.2
  12. * 2017-11-15 Bernard add lock for init_done callback
  13. * 2018-11-02 MurphyZhao port to v2.1.0
  14. * 2020-06-20 liuxianliang port to v2.1.2
  15. * 2021-06-25 liuxianliang port to v2.0.3
  16. * 2022-01-18 Meco Man remove v2.0.2
  17. * 2022-02-20 Meco Man integrate v1.4.1 v2.0.3 and v2.1.2 porting layer
  18. */
  19. #include <rtthread.h>
  20. #include <rthw.h>
  21. #include <arch/sys_arch.h>
  22. #include <lwip/sys.h>
  23. #include <lwip/opt.h>
  24. #include <lwip/stats.h>
  25. #include <lwip/err.h>
  26. #include <lwip/debug.h>
  27. #include <lwip/netif.h>
  28. #include <lwip/netifapi.h>
  29. #include <lwip/tcpip.h>
  30. #include <lwip/sio.h>
  31. #include <lwip/init.h>
  32. #include <lwip/dhcp.h>
  33. #include <lwip/inet.h>
  34. #include <netif/ethernetif.h>
  35. #include <netif/etharp.h>
  36. /*
  37. * Initialize the ethernetif layer and set network interface device up
  38. */
  39. static void tcpip_init_done_callback(void *arg)
  40. {
  41. rt_sem_release((rt_sem_t)arg);
  42. }
  43. /**
  44. * LwIP system initialization
  45. */
  46. int lwip_system_init(void)
  47. {
  48. rt_err_t rc;
  49. struct rt_semaphore done_sem;
  50. static rt_bool_t init_ok = RT_FALSE;
  51. if (init_ok)
  52. {
  53. rt_kprintf("lwip system already init.\n");
  54. return 0;
  55. }
  56. extern int eth_system_device_init_private(void);
  57. eth_system_device_init_private();
  58. /* set default netif to NULL */
  59. netif_default = RT_NULL;
  60. rc = rt_sem_init(&done_sem, "done", 0, RT_IPC_FLAG_FIFO);
  61. if (rc != RT_EOK)
  62. {
  63. LWIP_ASSERT("Failed to create semaphore", 0);
  64. return -1;
  65. }
  66. tcpip_init(tcpip_init_done_callback, (void *)&done_sem);
  67. /* waiting for initialization done */
  68. if (rt_sem_take(&done_sem, RT_WAITING_FOREVER) != RT_EOK)
  69. {
  70. rt_sem_detach(&done_sem);
  71. return -1;
  72. }
  73. rt_sem_detach(&done_sem);
  74. rt_kprintf("lwIP-%d.%d.%d initialized!\n", LWIP_VERSION_MAJOR, LWIP_VERSION_MINOR, LWIP_VERSION_REVISION);
  75. init_ok = RT_TRUE;
  76. return 0;
  77. }
  78. INIT_PREV_EXPORT(lwip_system_init);
  79. void sys_init(void)
  80. {
  81. /* nothing on RT-Thread porting */
  82. }
  83. void lwip_sys_init(void)
  84. {
  85. lwip_system_init();
  86. }
  87. /*
  88. * Create a new semaphore
  89. *
  90. * @return the operation status, ERR_OK on OK; others on error
  91. */
  92. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  93. {
  94. static unsigned short counter = 0;
  95. char tname[RT_NAME_MAX];
  96. sys_sem_t tmpsem;
  97. RT_DEBUG_NOT_IN_INTERRUPT;
  98. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_SEM_NAME, counter);
  99. counter ++;
  100. tmpsem = rt_sem_create(tname, count, RT_IPC_FLAG_FIFO);
  101. if (tmpsem == RT_NULL)
  102. {
  103. return ERR_MEM;
  104. }
  105. else
  106. {
  107. *sem = tmpsem;
  108. return ERR_OK;
  109. }
  110. }
  111. /*
  112. * Deallocate a semaphore
  113. */
  114. void sys_sem_free(sys_sem_t *sem)
  115. {
  116. RT_DEBUG_NOT_IN_INTERRUPT;
  117. rt_sem_delete(*sem);
  118. }
  119. /*
  120. * Signal a semaphore
  121. */
  122. void sys_sem_signal(sys_sem_t *sem)
  123. {
  124. rt_sem_release(*sem);
  125. }
  126. /*
  127. * Block the thread while waiting for the semaphore to be signaled
  128. *
  129. * @return If the timeout argument is non-zero, it will return the number of milliseconds
  130. * spent waiting for the semaphore to be signaled; If the semaphore isn't signaled
  131. * within the specified time, it will return SYS_ARCH_TIMEOUT; If the thread doesn't
  132. * wait for the semaphore, it will return zero
  133. */
  134. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  135. {
  136. rt_err_t ret;
  137. s32_t t;
  138. u32_t tick;
  139. RT_DEBUG_NOT_IN_INTERRUPT;
  140. /* get the begin tick */
  141. tick = rt_tick_get();
  142. if (timeout == 0)
  143. {
  144. t = RT_WAITING_FOREVER;
  145. }
  146. else
  147. {
  148. /* convert msecond to os tick */
  149. if (timeout < (1000 / RT_TICK_PER_SECOND))
  150. t = 1;
  151. else
  152. t = timeout / (1000 / RT_TICK_PER_SECOND);
  153. }
  154. ret = rt_sem_take(*sem, t);
  155. if (ret == -RT_ETIMEOUT)
  156. {
  157. return SYS_ARCH_TIMEOUT;
  158. }
  159. else
  160. {
  161. if (ret == RT_EOK)
  162. ret = 1;
  163. }
  164. /* get elapse msecond */
  165. tick = rt_tick_get() - tick;
  166. /* convert tick to msecond */
  167. tick = tick * (1000 / RT_TICK_PER_SECOND);
  168. if (tick == 0)
  169. tick = 1;
  170. return tick;
  171. }
  172. #ifndef sys_sem_valid
  173. /** Check if a semaphore is valid/allocated:
  174. * return 1 for valid, 0 for invalid
  175. */
  176. int sys_sem_valid(sys_sem_t *sem)
  177. {
  178. int ret = 0;
  179. if (*sem) ret = 1;
  180. return ret;
  181. }
  182. #endif
  183. #ifndef sys_sem_set_invalid
  184. /** Set a semaphore invalid so that sys_sem_valid returns 0
  185. */
  186. void sys_sem_set_invalid(sys_sem_t *sem)
  187. {
  188. *sem = RT_NULL;
  189. }
  190. #endif
  191. /* ====================== Mutex ====================== */
  192. /** Create a new mutex
  193. * @param mutex pointer to the mutex to create
  194. * @return a new mutex
  195. */
  196. err_t sys_mutex_new(sys_mutex_t *mutex)
  197. {
  198. static unsigned short counter = 0;
  199. char tname[RT_NAME_MAX];
  200. sys_mutex_t tmpmutex;
  201. RT_DEBUG_NOT_IN_INTERRUPT;
  202. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MUTEX_NAME, counter);
  203. counter ++;
  204. tmpmutex = rt_mutex_create(tname, RT_IPC_FLAG_PRIO);
  205. if (tmpmutex == RT_NULL)
  206. {
  207. return ERR_MEM;
  208. }
  209. else
  210. {
  211. *mutex = tmpmutex;
  212. return ERR_OK;
  213. }
  214. }
  215. /** Lock a mutex
  216. * @param mutex the mutex to lock
  217. */
  218. void sys_mutex_lock(sys_mutex_t *mutex)
  219. {
  220. RT_DEBUG_NOT_IN_INTERRUPT;
  221. rt_mutex_take(*mutex, RT_WAITING_FOREVER);
  222. return;
  223. }
  224. /** Unlock a mutex
  225. * @param mutex the mutex to unlock
  226. */
  227. void sys_mutex_unlock(sys_mutex_t *mutex)
  228. {
  229. rt_mutex_release(*mutex);
  230. }
  231. /** Delete a semaphore
  232. * @param mutex the mutex to delete
  233. */
  234. void sys_mutex_free(sys_mutex_t *mutex)
  235. {
  236. RT_DEBUG_NOT_IN_INTERRUPT;
  237. rt_mutex_delete(*mutex);
  238. }
  239. #ifndef sys_mutex_valid
  240. /** Check if a mutex is valid/allocated:
  241. * return 1 for valid, 0 for invalid
  242. */
  243. int sys_mutex_valid(sys_mutex_t *mutex)
  244. {
  245. int ret = 0;
  246. if (*mutex) ret = 1;
  247. return ret;
  248. }
  249. #endif
  250. #ifndef sys_mutex_set_invalid
  251. /** Set a mutex invalid so that sys_mutex_valid returns 0
  252. */
  253. void sys_mutex_set_invalid(sys_mutex_t *mutex)
  254. {
  255. *mutex = RT_NULL;
  256. }
  257. #endif
  258. /* ====================== Mailbox ====================== */
  259. /*
  260. * Create an empty mailbox for maximum "size" elements
  261. *
  262. * @return the operation status, ERR_OK on OK; others on error
  263. */
  264. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  265. {
  266. static unsigned short counter = 0;
  267. char tname[RT_NAME_MAX];
  268. sys_mbox_t tmpmbox;
  269. RT_DEBUG_NOT_IN_INTERRUPT;
  270. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MBOX_NAME, counter);
  271. counter ++;
  272. tmpmbox = rt_mb_create(tname, size, RT_IPC_FLAG_FIFO);
  273. if (tmpmbox != RT_NULL)
  274. {
  275. *mbox = tmpmbox;
  276. return ERR_OK;
  277. }
  278. return ERR_MEM;
  279. }
  280. /*
  281. * Deallocate a mailbox
  282. */
  283. void sys_mbox_free(sys_mbox_t *mbox)
  284. {
  285. RT_DEBUG_NOT_IN_INTERRUPT;
  286. rt_mb_delete(*mbox);
  287. return;
  288. }
  289. /** Post a message to an mbox - may not fail
  290. * -> blocks if full, only used from tasks not from ISR
  291. * @param mbox mbox to posts the message
  292. * @param msg message to post (ATTENTION: can be NULL)
  293. */
  294. void sys_mbox_post(sys_mbox_t *mbox, void *msg)
  295. {
  296. RT_DEBUG_NOT_IN_INTERRUPT;
  297. rt_mb_send_wait(*mbox, (rt_ubase_t)msg, RT_WAITING_FOREVER);
  298. return;
  299. }
  300. /*
  301. * Try to post the "msg" to the mailbox
  302. *
  303. * @return return ERR_OK if the "msg" is posted, ERR_MEM if the mailbox is full
  304. */
  305. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  306. {
  307. if (rt_mb_send(*mbox, (rt_ubase_t)msg) == RT_EOK)
  308. {
  309. return ERR_OK;
  310. }
  311. return ERR_MEM;
  312. }
  313. #if (LWIP_VERSION_MAJOR * 100 + LWIP_VERSION_MINOR) >= 201 /* >= v2.1.0 */
  314. err_t sys_mbox_trypost_fromisr(sys_mbox_t *q, void *msg)
  315. {
  316. return sys_mbox_trypost(q, msg);
  317. }
  318. #endif /* (LWIP_VERSION_MAJOR * 100 + LWIP_VERSION_MINOR) >= 201 */
  319. /** Wait for a new message to arrive in the mbox
  320. * @param mbox mbox to get a message from
  321. * @param msg pointer where the message is stored
  322. * @param timeout maximum time (in milliseconds) to wait for a message
  323. * @return time (in milliseconds) waited for a message, may be 0 if not waited
  324. or SYS_ARCH_TIMEOUT on timeout
  325. * The returned time has to be accurate to prevent timer jitter!
  326. */
  327. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  328. {
  329. rt_err_t ret;
  330. s32_t t;
  331. u32_t tick;
  332. RT_DEBUG_NOT_IN_INTERRUPT;
  333. /* get the begin tick */
  334. tick = rt_tick_get();
  335. if(timeout == 0)
  336. {
  337. t = RT_WAITING_FOREVER;
  338. }
  339. else
  340. {
  341. /* convirt msecond to os tick */
  342. if (timeout < (1000 / RT_TICK_PER_SECOND))
  343. t = 1;
  344. else
  345. t = timeout / (1000 / RT_TICK_PER_SECOND);
  346. }
  347. ret = rt_mb_recv(*mbox, (rt_ubase_t *)msg, t);
  348. if(ret != RT_EOK)
  349. {
  350. return SYS_ARCH_TIMEOUT;
  351. }
  352. /* get elapse msecond */
  353. tick = rt_tick_get() - tick;
  354. /* convert tick to msecond */
  355. tick = tick * (1000 / RT_TICK_PER_SECOND);
  356. if (tick == 0)
  357. tick = 1;
  358. return tick;
  359. }
  360. /**
  361. * @ingroup sys_mbox
  362. * This is similar to sys_arch_mbox_fetch, however if a message is not
  363. * present in the mailbox, it immediately returns with the code
  364. * SYS_MBOX_EMPTY. On success 0 is returned.
  365. * To allow for efficient implementations, this can be defined as a
  366. * function-like macro in sys_arch.h instead of a normal function. For
  367. * example, a naive implementation could be:
  368. * \#define sys_arch_mbox_tryfetch(mbox,msg) sys_arch_mbox_fetch(mbox,msg,1)
  369. * although this would introduce unnecessary delays.
  370. *
  371. * @param mbox mbox to get a message from
  372. * @param msg pointer where the message is stored
  373. * @return 0 (milliseconds) if a message has been received
  374. * or SYS_MBOX_EMPTY if the mailbox is empty
  375. */
  376. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  377. {
  378. int ret;
  379. ret = rt_mb_recv(*mbox, (rt_ubase_t *)msg, 0);
  380. if(ret == -RT_ETIMEOUT)
  381. {
  382. return SYS_ARCH_TIMEOUT;
  383. }
  384. else
  385. {
  386. if (ret == RT_EOK)
  387. ret = 0;
  388. }
  389. return ret;
  390. }
  391. #ifndef sys_mbox_valid
  392. /** Check if an mbox is valid/allocated:
  393. * return 1 for valid, 0 for invalid
  394. */
  395. int sys_mbox_valid(sys_mbox_t *mbox)
  396. {
  397. int ret = 0;
  398. if (*mbox) ret = 1;
  399. return ret;
  400. }
  401. #endif
  402. #ifndef sys_mbox_set_invalid
  403. /** Set an mbox invalid so that sys_mbox_valid returns 0
  404. */
  405. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  406. {
  407. *mbox = RT_NULL;
  408. }
  409. #endif
  410. /* ====================== System ====================== */
  411. /*
  412. * Start a new thread named "name" with priority "prio" that will begin
  413. * its execution in the function "thread()". The "arg" argument will be
  414. * passed as an argument to the thread() function
  415. */
  416. sys_thread_t sys_thread_new(const char *name,
  417. lwip_thread_fn thread,
  418. void *arg,
  419. int stacksize,
  420. int prio)
  421. {
  422. rt_thread_t t;
  423. RT_DEBUG_NOT_IN_INTERRUPT;
  424. /* create thread */
  425. t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
  426. RT_ASSERT(t != RT_NULL);
  427. /* startup thread */
  428. rt_thread_startup(t);
  429. return t;
  430. }
  431. sys_prot_t sys_arch_protect(void)
  432. {
  433. rt_base_t level;
  434. level = rt_hw_interrupt_disable(); /* disable interrupt */
  435. return level;
  436. }
  437. void sys_arch_unprotect(sys_prot_t pval)
  438. {
  439. rt_hw_interrupt_enable(pval); /* enable interrupt */
  440. }
  441. void sys_arch_assert(const char *file, int line)
  442. {
  443. rt_kprintf("\nAssertion: %d in %s, thread %s\n",
  444. line, file, rt_thread_self()->parent.name);
  445. RT_ASSERT(0);
  446. }
  447. u32_t sys_jiffies(void)
  448. {
  449. return rt_tick_get();
  450. }
  451. u32_t sys_now(void)
  452. {
  453. return rt_tick_get_millisecond();
  454. }
  455. rt_weak void mem_init(void)
  456. {
  457. }
  458. void *mem_calloc(mem_size_t count, mem_size_t size)
  459. {
  460. return rt_calloc(count, size);
  461. }
  462. void *mem_trim(void *mem, mem_size_t size)
  463. {
  464. // return rt_realloc(mem, size);
  465. /* not support trim yet */
  466. return mem;
  467. }
  468. void *mem_malloc(mem_size_t size)
  469. {
  470. return rt_malloc(size);
  471. }
  472. void mem_free(void *mem)
  473. {
  474. rt_free(mem);
  475. }
  476. #ifdef RT_LWIP_PPP
  477. u32_t sio_read(sio_fd_t fd, u8_t *buf, u32_t size)
  478. {
  479. u32_t len;
  480. RT_ASSERT(fd != RT_NULL);
  481. len = rt_device_read((rt_device_t)fd, 0, buf, size);
  482. if (len <= 0)
  483. return 0;
  484. return len;
  485. }
  486. u32_t sio_write(sio_fd_t fd, u8_t *buf, u32_t size)
  487. {
  488. RT_ASSERT(fd != RT_NULL);
  489. return rt_device_write((rt_device_t)fd, 0, buf, size);
  490. }
  491. void sio_read_abort(sio_fd_t fd)
  492. {
  493. rt_kprintf("read_abort\n");
  494. }
  495. void ppp_trace(int level, const char *format, ...)
  496. {
  497. va_list args;
  498. rt_size_t length;
  499. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  500. va_start(args, format);
  501. length = rt_vsprintf(rt_log_buf, format, args);
  502. rt_device_write((rt_device_t)rt_console_get_device(), 0, rt_log_buf, length);
  503. va_end(args);
  504. }
  505. #endif /* RT_LWIP_PPP */
  506. #if LWIP_VERSION_MAJOR >= 2 /* >= v2.x */
  507. #if MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK
  508. /**
  509. * Check if a mep element was victim of an overflow or underflow
  510. * (e.g. the restricted area after/before it has been altered)
  511. *
  512. * @param p the mem element to check
  513. * @param size allocated size of the element
  514. * @param descr1 description of the element source shown on error
  515. * @param descr2 description of the element source shown on error
  516. */
  517. void mem_overflow_check_raw(void *p, size_t size, const char *descr1, const char *descr2)
  518. {
  519. #if MEM_SANITY_REGION_AFTER_ALIGNED || MEM_SANITY_REGION_BEFORE_ALIGNED
  520. u16_t k;
  521. u8_t *m;
  522. #if MEM_SANITY_REGION_AFTER_ALIGNED > 0
  523. m = (u8_t *)p + size;
  524. for (k = 0; k < MEM_SANITY_REGION_AFTER_ALIGNED; k++) {
  525. if (m[k] != 0xcd) {
  526. char errstr[128];
  527. rt_snprintf(errstr, sizeof(errstr), "detected mem overflow in %s%s", descr1, descr2);
  528. LWIP_ASSERT(errstr, 0);
  529. }
  530. }
  531. #endif /* MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  532. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0
  533. m = (u8_t *)p - MEM_SANITY_REGION_BEFORE_ALIGNED;
  534. for (k = 0; k < MEM_SANITY_REGION_BEFORE_ALIGNED; k++) {
  535. if (m[k] != 0xcd) {
  536. char errstr[128];
  537. rt_snprintf(errstr, sizeof(errstr), "detected mem underflow in %s%s", descr1, descr2);
  538. LWIP_ASSERT(errstr, 0);
  539. }
  540. }
  541. #endif /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 */
  542. #else
  543. LWIP_UNUSED_ARG(p);
  544. LWIP_UNUSED_ARG(descr1);
  545. LWIP_UNUSED_ARG(descr2);
  546. #endif /* MEM_SANITY_REGION_AFTER_ALIGNED || MEM_SANITY_REGION_BEFORE_ALIGNED */
  547. }
  548. /**
  549. * Initialize the restricted area of a mem element.
  550. */
  551. void mem_overflow_init_raw(void *p, size_t size)
  552. {
  553. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0
  554. u8_t *m;
  555. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0
  556. m = (u8_t *)p - MEM_SANITY_REGION_BEFORE_ALIGNED;
  557. rt_memset(m, 0xcd, MEM_SANITY_REGION_BEFORE_ALIGNED);
  558. #endif
  559. #if MEM_SANITY_REGION_AFTER_ALIGNED > 0
  560. m = (u8_t *)p + size;
  561. rt_memset(m, 0xcd, MEM_SANITY_REGION_AFTER_ALIGNED);
  562. #endif
  563. #else /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  564. LWIP_UNUSED_ARG(p);
  565. LWIP_UNUSED_ARG(size);
  566. #endif /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  567. }
  568. #endif /* MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK */
  569. #ifdef LWIP_HOOK_IP4_ROUTE_SRC
  570. struct netif *lwip_ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src)
  571. {
  572. struct netif *netif;
  573. if (src == NULL)
  574. return NULL;
  575. /* iterate through netifs */
  576. for (netif = netif_list; netif != NULL; netif = netif->next)
  577. {
  578. /* is the netif up, does it have a link and a valid address? */
  579. if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif)))
  580. {
  581. /* source ip address equals netif's ip address? */
  582. if (ip4_addr_cmp(src, netif_ip4_addr(netif)))
  583. {
  584. return netif;
  585. }
  586. }
  587. }
  588. return NULL;
  589. }
  590. #endif /* LWIP_HOOK_IP4_ROUTE_SRC */
  591. #endif /*LWIP_VERSION_MAJOR >= 2 */
  592. #if LWIP_SOCKET
  593. #include <lwip/sockets.h>
  594. RTM_EXPORT(lwip_accept);
  595. RTM_EXPORT(lwip_bind);
  596. RTM_EXPORT(lwip_shutdown);
  597. RTM_EXPORT(lwip_getpeername);
  598. RTM_EXPORT(lwip_getsockname);
  599. RTM_EXPORT(lwip_getsockopt);
  600. RTM_EXPORT(lwip_setsockopt);
  601. RTM_EXPORT(lwip_close);
  602. RTM_EXPORT(lwip_connect);
  603. RTM_EXPORT(lwip_listen);
  604. RTM_EXPORT(lwip_recv);
  605. RTM_EXPORT(lwip_read);
  606. RTM_EXPORT(lwip_recvfrom);
  607. RTM_EXPORT(lwip_send);
  608. RTM_EXPORT(lwip_sendto);
  609. RTM_EXPORT(lwip_socket);
  610. RTM_EXPORT(lwip_write);
  611. RTM_EXPORT(lwip_select);
  612. RTM_EXPORT(lwip_ioctl);
  613. RTM_EXPORT(lwip_fcntl);
  614. RTM_EXPORT(lwip_htons);
  615. RTM_EXPORT(lwip_htonl);
  616. #if LWIP_DNS
  617. #include <lwip/netdb.h>
  618. RTM_EXPORT(lwip_gethostbyname);
  619. RTM_EXPORT(lwip_gethostbyname_r);
  620. RTM_EXPORT(lwip_freeaddrinfo);
  621. RTM_EXPORT(lwip_getaddrinfo);
  622. #endif /* LWIP_DNS */
  623. #endif /* LWIP_SOCKET */
  624. #if LWIP_DHCP
  625. #include <lwip/dhcp.h>
  626. RTM_EXPORT(dhcp_start);
  627. RTM_EXPORT(dhcp_renew);
  628. RTM_EXPORT(dhcp_stop);
  629. #endif /* LWIP_DHCP */
  630. #if LWIP_NETIF_API
  631. #include <lwip/netifapi.h>
  632. RTM_EXPORT(netifapi_netif_set_addr);
  633. #endif /* LWIP_NETIF_API */
  634. #if LWIP_NETIF_LINK_CALLBACK
  635. RTM_EXPORT(netif_set_link_callback);
  636. #endif /* LWIP_NETIF_LINK_CALLBACK */
  637. #if LWIP_NETIF_STATUS_CALLBACK
  638. RTM_EXPORT(netif_set_status_callback);
  639. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  640. RTM_EXPORT(netif_find);
  641. RTM_EXPORT(netif_set_addr);
  642. RTM_EXPORT(netif_set_ipaddr);
  643. RTM_EXPORT(netif_set_gw);
  644. RTM_EXPORT(netif_set_netmask);