sys_arch.c 18 KB

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