sys_arch.c 18 KB

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