sys_arch.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. return (int)(*sem);
  179. }
  180. #endif
  181. #ifndef sys_sem_set_invalid
  182. /** Set a semaphore invalid so that sys_sem_valid returns 0
  183. */
  184. void sys_sem_set_invalid(sys_sem_t *sem)
  185. {
  186. *sem = RT_NULL;
  187. }
  188. #endif
  189. /* ====================== Mutex ====================== */
  190. /** Create a new mutex
  191. * @param mutex pointer to the mutex to create
  192. * @return a new mutex
  193. */
  194. err_t sys_mutex_new(sys_mutex_t *mutex)
  195. {
  196. static unsigned short counter = 0;
  197. char tname[RT_NAME_MAX];
  198. sys_mutex_t tmpmutex;
  199. RT_DEBUG_NOT_IN_INTERRUPT;
  200. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MUTEX_NAME, counter);
  201. counter ++;
  202. tmpmutex = rt_mutex_create(tname, RT_IPC_FLAG_PRIO);
  203. if (tmpmutex == RT_NULL)
  204. {
  205. return ERR_MEM;
  206. }
  207. else
  208. {
  209. *mutex = tmpmutex;
  210. return ERR_OK;
  211. }
  212. }
  213. /** Lock a mutex
  214. * @param mutex the mutex to lock
  215. */
  216. void sys_mutex_lock(sys_mutex_t *mutex)
  217. {
  218. RT_DEBUG_NOT_IN_INTERRUPT;
  219. rt_mutex_take(*mutex, RT_WAITING_FOREVER);
  220. return;
  221. }
  222. /** Unlock a mutex
  223. * @param mutex the mutex to unlock
  224. */
  225. void sys_mutex_unlock(sys_mutex_t *mutex)
  226. {
  227. rt_mutex_release(*mutex);
  228. }
  229. /** Delete a semaphore
  230. * @param mutex the mutex to delete
  231. */
  232. void sys_mutex_free(sys_mutex_t *mutex)
  233. {
  234. RT_DEBUG_NOT_IN_INTERRUPT;
  235. rt_mutex_delete(*mutex);
  236. }
  237. #ifndef sys_mutex_valid
  238. /** Check if a mutex is valid/allocated:
  239. * return 1 for valid, 0 for invalid
  240. */
  241. int sys_mutex_valid(sys_mutex_t *mutex)
  242. {
  243. return (int)(*mutex);
  244. }
  245. #endif
  246. #ifndef sys_mutex_set_invalid
  247. /** Set a mutex invalid so that sys_mutex_valid returns 0
  248. */
  249. void sys_mutex_set_invalid(sys_mutex_t *mutex)
  250. {
  251. *mutex = RT_NULL;
  252. }
  253. #endif
  254. /* ====================== Mailbox ====================== */
  255. /*
  256. * Create an empty mailbox for maximum "size" elements
  257. *
  258. * @return the operation status, ERR_OK on OK; others on error
  259. */
  260. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  261. {
  262. static unsigned short counter = 0;
  263. char tname[RT_NAME_MAX];
  264. sys_mbox_t tmpmbox;
  265. RT_DEBUG_NOT_IN_INTERRUPT;
  266. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MBOX_NAME, counter);
  267. counter ++;
  268. tmpmbox = rt_mb_create(tname, size, RT_IPC_FLAG_FIFO);
  269. if (tmpmbox != RT_NULL)
  270. {
  271. *mbox = tmpmbox;
  272. return ERR_OK;
  273. }
  274. return ERR_MEM;
  275. }
  276. /*
  277. * Deallocate a mailbox
  278. */
  279. void sys_mbox_free(sys_mbox_t *mbox)
  280. {
  281. RT_DEBUG_NOT_IN_INTERRUPT;
  282. rt_mb_delete(*mbox);
  283. return;
  284. }
  285. /** Post a message to an mbox - may not fail
  286. * -> blocks if full, only used from tasks not from ISR
  287. * @param mbox mbox to posts the message
  288. * @param msg message to post (ATTENTION: can be NULL)
  289. */
  290. void sys_mbox_post(sys_mbox_t *mbox, void *msg)
  291. {
  292. RT_DEBUG_NOT_IN_INTERRUPT;
  293. rt_mb_send_wait(*mbox, (rt_ubase_t)msg, RT_WAITING_FOREVER);
  294. return;
  295. }
  296. /*
  297. * Try to post the "msg" to the mailbox
  298. *
  299. * @return return ERR_OK if the "msg" is posted, ERR_MEM if the mailbox is full
  300. */
  301. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  302. {
  303. if (rt_mb_send(*mbox, (rt_ubase_t)msg) == RT_EOK)
  304. {
  305. return ERR_OK;
  306. }
  307. return ERR_MEM;
  308. }
  309. #if (LWIP_VERSION_MAJOR * 100 + LWIP_VERSION_MINOR) >= 201 /* >= v2.1.0 */
  310. err_t sys_mbox_trypost_fromisr(sys_mbox_t *q, void *msg)
  311. {
  312. return sys_mbox_trypost(q, msg);
  313. }
  314. #endif /* (LWIP_VERSION_MAJOR * 100 + LWIP_VERSION_MINOR) >= 201 */
  315. /** Wait for a new message to arrive in the mbox
  316. * @param mbox mbox to get a message from
  317. * @param msg pointer where the message is stored
  318. * @param timeout maximum time (in milliseconds) to wait for a message
  319. * @return time (in milliseconds) waited for a message, may be 0 if not waited
  320. or SYS_ARCH_TIMEOUT on timeout
  321. * The returned time has to be accurate to prevent timer jitter!
  322. */
  323. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  324. {
  325. rt_err_t ret;
  326. s32_t t;
  327. u32_t tick;
  328. RT_DEBUG_NOT_IN_INTERRUPT;
  329. /* get the begin tick */
  330. tick = rt_tick_get();
  331. if(timeout == 0)
  332. {
  333. t = RT_WAITING_FOREVER;
  334. }
  335. else
  336. {
  337. /* convirt msecond to os tick */
  338. if (timeout < (1000 / RT_TICK_PER_SECOND))
  339. t = 1;
  340. else
  341. t = timeout / (1000 / RT_TICK_PER_SECOND);
  342. }
  343. ret = rt_mb_recv(*mbox, (rt_ubase_t *)msg, t);
  344. if(ret != RT_EOK)
  345. {
  346. return SYS_ARCH_TIMEOUT;
  347. }
  348. /* get elapse msecond */
  349. tick = rt_tick_get() - tick;
  350. /* convert tick to msecond */
  351. tick = tick * (1000 / RT_TICK_PER_SECOND);
  352. if (tick == 0)
  353. tick = 1;
  354. return tick;
  355. }
  356. /**
  357. * @ingroup sys_mbox
  358. * This is similar to sys_arch_mbox_fetch, however if a message is not
  359. * present in the mailbox, it immediately returns with the code
  360. * SYS_MBOX_EMPTY. On success 0 is returned.
  361. * To allow for efficient implementations, this can be defined as a
  362. * function-like macro in sys_arch.h instead of a normal function. For
  363. * example, a naive implementation could be:
  364. * \#define sys_arch_mbox_tryfetch(mbox,msg) sys_arch_mbox_fetch(mbox,msg,1)
  365. * although this would introduce unnecessary delays.
  366. *
  367. * @param mbox mbox to get a message from
  368. * @param msg pointer where the message is stored
  369. * @return 0 (milliseconds) if a message has been received
  370. * or SYS_MBOX_EMPTY if the mailbox is empty
  371. */
  372. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  373. {
  374. int ret;
  375. ret = rt_mb_recv(*mbox, (rt_ubase_t *)msg, 0);
  376. if(ret == -RT_ETIMEOUT)
  377. {
  378. return SYS_ARCH_TIMEOUT;
  379. }
  380. else
  381. {
  382. if (ret == RT_EOK)
  383. ret = 0;
  384. }
  385. return ret;
  386. }
  387. #ifndef sys_mbox_valid
  388. /** Check if an mbox is valid/allocated:
  389. * return 1 for valid, 0 for invalid
  390. */
  391. int sys_mbox_valid(sys_mbox_t *mbox)
  392. {
  393. return (int)(*mbox);
  394. }
  395. #endif
  396. #ifndef sys_mbox_set_invalid
  397. /** Set an mbox invalid so that sys_mbox_valid returns 0
  398. */
  399. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  400. {
  401. *mbox = RT_NULL;
  402. }
  403. #endif
  404. /* ====================== System ====================== */
  405. /*
  406. * Start a new thread named "name" with priority "prio" that will begin
  407. * its execution in the function "thread()". The "arg" argument will be
  408. * passed as an argument to the thread() function
  409. */
  410. sys_thread_t sys_thread_new(const char *name,
  411. lwip_thread_fn thread,
  412. void *arg,
  413. int stacksize,
  414. int prio)
  415. {
  416. rt_thread_t t;
  417. RT_DEBUG_NOT_IN_INTERRUPT;
  418. /* create thread */
  419. t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
  420. RT_ASSERT(t != RT_NULL);
  421. /* startup thread */
  422. rt_thread_startup(t);
  423. return t;
  424. }
  425. sys_prot_t sys_arch_protect(void)
  426. {
  427. rt_base_t level;
  428. level = rt_hw_interrupt_disable(); /* disable interrupt */
  429. return level;
  430. }
  431. void sys_arch_unprotect(sys_prot_t pval)
  432. {
  433. rt_hw_interrupt_enable(pval); /* enable interrupt */
  434. }
  435. void sys_arch_assert(const char *file, int line)
  436. {
  437. rt_kprintf("\nAssertion: %d in %s, thread %s\n",
  438. line, file, rt_thread_self()->name);
  439. RT_ASSERT(0);
  440. }
  441. u32_t sys_jiffies(void)
  442. {
  443. return rt_tick_get();
  444. }
  445. u32_t sys_now(void)
  446. {
  447. return rt_tick_get_millisecond();
  448. }
  449. RT_WEAK void mem_init(void)
  450. {
  451. }
  452. void *mem_calloc(mem_size_t count, mem_size_t size)
  453. {
  454. return rt_calloc(count, size);
  455. }
  456. void *mem_trim(void *mem, mem_size_t size)
  457. {
  458. // return rt_realloc(mem, size);
  459. /* not support trim yet */
  460. return mem;
  461. }
  462. void *mem_malloc(mem_size_t size)
  463. {
  464. return rt_malloc(size);
  465. }
  466. void mem_free(void *mem)
  467. {
  468. rt_free(mem);
  469. }
  470. #ifdef RT_LWIP_PPP
  471. u32_t sio_read(sio_fd_t fd, u8_t *buf, u32_t size)
  472. {
  473. u32_t len;
  474. RT_ASSERT(fd != RT_NULL);
  475. len = rt_device_read((rt_device_t)fd, 0, buf, size);
  476. if (len <= 0)
  477. return 0;
  478. return len;
  479. }
  480. u32_t sio_write(sio_fd_t fd, u8_t *buf, u32_t size)
  481. {
  482. RT_ASSERT(fd != RT_NULL);
  483. return rt_device_write((rt_device_t)fd, 0, buf, size);
  484. }
  485. void sio_read_abort(sio_fd_t fd)
  486. {
  487. rt_kprintf("read_abort\n");
  488. }
  489. void ppp_trace(int level, const char *format, ...)
  490. {
  491. va_list args;
  492. rt_size_t length;
  493. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  494. va_start(args, format);
  495. length = rt_vsprintf(rt_log_buf, format, args);
  496. rt_device_write((rt_device_t)rt_console_get_device(), 0, rt_log_buf, length);
  497. va_end(args);
  498. }
  499. #endif /* RT_LWIP_PPP */
  500. #if LWIP_VERSION_MAJOR >= 2 /* >= v2.x */
  501. #if MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK
  502. /**
  503. * Check if a mep element was victim of an overflow or underflow
  504. * (e.g. the restricted area after/before it has been altered)
  505. *
  506. * @param p the mem element to check
  507. * @param size allocated size of the element
  508. * @param descr1 description of the element source shown on error
  509. * @param descr2 description of the element source shown on error
  510. */
  511. void mem_overflow_check_raw(void *p, size_t size, const char *descr1, const char *descr2)
  512. {
  513. #if MEM_SANITY_REGION_AFTER_ALIGNED || MEM_SANITY_REGION_BEFORE_ALIGNED
  514. u16_t k;
  515. u8_t *m;
  516. #if MEM_SANITY_REGION_AFTER_ALIGNED > 0
  517. m = (u8_t *)p + size;
  518. for (k = 0; k < MEM_SANITY_REGION_AFTER_ALIGNED; k++) {
  519. if (m[k] != 0xcd) {
  520. char errstr[128];
  521. rt_snprintf(errstr, sizeof(errstr), "detected mem overflow in %s%s", descr1, descr2);
  522. LWIP_ASSERT(errstr, 0);
  523. }
  524. }
  525. #endif /* MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  526. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0
  527. m = (u8_t *)p - MEM_SANITY_REGION_BEFORE_ALIGNED;
  528. for (k = 0; k < MEM_SANITY_REGION_BEFORE_ALIGNED; k++) {
  529. if (m[k] != 0xcd) {
  530. char errstr[128];
  531. rt_snprintf(errstr, sizeof(errstr), "detected mem underflow in %s%s", descr1, descr2);
  532. LWIP_ASSERT(errstr, 0);
  533. }
  534. }
  535. #endif /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 */
  536. #else
  537. LWIP_UNUSED_ARG(p);
  538. LWIP_UNUSED_ARG(descr1);
  539. LWIP_UNUSED_ARG(descr2);
  540. #endif /* MEM_SANITY_REGION_AFTER_ALIGNED || MEM_SANITY_REGION_BEFORE_ALIGNED */
  541. }
  542. /**
  543. * Initialize the restricted area of a mem element.
  544. */
  545. void mem_overflow_init_raw(void *p, size_t size)
  546. {
  547. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0
  548. u8_t *m;
  549. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0
  550. m = (u8_t *)p - MEM_SANITY_REGION_BEFORE_ALIGNED;
  551. rt_memset(m, 0xcd, MEM_SANITY_REGION_BEFORE_ALIGNED);
  552. #endif
  553. #if MEM_SANITY_REGION_AFTER_ALIGNED > 0
  554. m = (u8_t *)p + size;
  555. rt_memset(m, 0xcd, MEM_SANITY_REGION_AFTER_ALIGNED);
  556. #endif
  557. #else /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  558. LWIP_UNUSED_ARG(p);
  559. LWIP_UNUSED_ARG(size);
  560. #endif /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  561. }
  562. #endif /* MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK */
  563. #ifdef LWIP_HOOK_IP4_ROUTE_SRC
  564. struct netif *lwip_ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src)
  565. {
  566. struct netif *netif;
  567. /* iterate through netifs */
  568. for (netif = netif_list; netif != NULL; netif = netif->next)
  569. {
  570. /* is the netif up, does it have a link and a valid address? */
  571. if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif)))
  572. {
  573. /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */
  574. if (src != NULL)
  575. {
  576. if (ip4_addr_cmp(src, netif_ip4_addr(netif)))
  577. {
  578. return netif;
  579. }
  580. }
  581. }
  582. }
  583. netif = netif_default;
  584. return netif;
  585. }
  586. #endif /* LWIP_HOOK_IP4_ROUTE_SRC */
  587. #endif /*LWIP_VERSION_MAJOR >= 2 */
  588. #if LWIP_SOCKET
  589. #include <lwip/sockets.h>
  590. RTM_EXPORT(lwip_accept);
  591. RTM_EXPORT(lwip_bind);
  592. RTM_EXPORT(lwip_shutdown);
  593. RTM_EXPORT(lwip_getpeername);
  594. RTM_EXPORT(lwip_getsockname);
  595. RTM_EXPORT(lwip_getsockopt);
  596. RTM_EXPORT(lwip_setsockopt);
  597. RTM_EXPORT(lwip_close);
  598. RTM_EXPORT(lwip_connect);
  599. RTM_EXPORT(lwip_listen);
  600. RTM_EXPORT(lwip_recv);
  601. RTM_EXPORT(lwip_read);
  602. RTM_EXPORT(lwip_recvfrom);
  603. RTM_EXPORT(lwip_send);
  604. RTM_EXPORT(lwip_sendto);
  605. RTM_EXPORT(lwip_socket);
  606. RTM_EXPORT(lwip_write);
  607. RTM_EXPORT(lwip_select);
  608. RTM_EXPORT(lwip_ioctl);
  609. RTM_EXPORT(lwip_fcntl);
  610. RTM_EXPORT(lwip_htons);
  611. RTM_EXPORT(lwip_htonl);
  612. #if LWIP_DNS
  613. #include <lwip/netdb.h>
  614. RTM_EXPORT(lwip_gethostbyname);
  615. RTM_EXPORT(lwip_gethostbyname_r);
  616. RTM_EXPORT(lwip_freeaddrinfo);
  617. RTM_EXPORT(lwip_getaddrinfo);
  618. #endif /* LWIP_DNS */
  619. #endif /* LWIP_SOCKET */
  620. #if LWIP_DHCP
  621. #include <lwip/dhcp.h>
  622. RTM_EXPORT(dhcp_start);
  623. RTM_EXPORT(dhcp_renew);
  624. RTM_EXPORT(dhcp_stop);
  625. #endif /* LWIP_DHCP */
  626. #if LWIP_NETIF_API
  627. #include <lwip/netifapi.h>
  628. RTM_EXPORT(netifapi_netif_set_addr);
  629. #endif /* LWIP_NETIF_API */
  630. #if LWIP_NETIF_LINK_CALLBACK
  631. RTM_EXPORT(netif_set_link_callback);
  632. #endif /* LWIP_NETIF_LINK_CALLBACK */
  633. #if LWIP_NETIF_STATUS_CALLBACK
  634. RTM_EXPORT(netif_set_status_callback);
  635. #endif /* LWIP_NETIF_STATUS_CALLBACK */
  636. RTM_EXPORT(netif_find);
  637. RTM_EXPORT(netif_set_addr);
  638. RTM_EXPORT(netif_set_ipaddr);
  639. RTM_EXPORT(netif_set_gw);
  640. RTM_EXPORT(netif_set_netmask);