sys_arch.c 18 KB

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