sys_arch.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * COPYRIGHT (C) 2006-2021, RT-Thread Development Team
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * Change Logs:
  28. * Date Author Notes
  29. * 2012-12-8 Bernard add file header
  30. * export bsd socket symbol for RT-Thread Application Module
  31. * 2017-11-15 Bernard add lock for init_done callback.
  32. * 2018-11-02 MurphyZhao port to lwip2.1.0
  33. */
  34. #include <rtthread.h>
  35. #include <rthw.h>
  36. #include "lwip/sys.h"
  37. #include "lwip/opt.h"
  38. #include "lwip/stats.h"
  39. #include "lwip/err.h"
  40. #include "arch/sys_arch.h"
  41. #include "lwip/debug.h"
  42. #include "lwip/netif.h"
  43. #include "lwip/netifapi.h"
  44. #include "lwip/tcpip.h"
  45. #include "netif/ethernetif.h"
  46. #include "lwip/sio.h"
  47. #include "lwip/init.h"
  48. #include "lwip/dhcp.h"
  49. #include "lwip/inet.h"
  50. #include "netif/etharp.h"
  51. #include <string.h>
  52. #include <stdio.h>
  53. /*
  54. * Initialize the network interface device
  55. *
  56. * @return the operation status, ERR_OK on OK, ERR_IF on error
  57. */
  58. static err_t netif_device_init(struct netif *netif)
  59. {
  60. struct eth_device *ethif;
  61. ethif = (struct eth_device *)netif->state;
  62. if (ethif != RT_NULL)
  63. {
  64. rt_device_t device;
  65. /* get device object */
  66. device = (rt_device_t) ethif;
  67. if (rt_device_init(device) != RT_EOK)
  68. {
  69. return ERR_IF;
  70. }
  71. /* copy device flags to netif flags */
  72. netif->flags = ethif->flags;
  73. netif->mtu = ETHERNET_MTU;
  74. /* set output */
  75. netif->output = etharp_output;
  76. return ERR_OK;
  77. }
  78. return ERR_IF;
  79. }
  80. /*
  81. * Initialize the ethernetif layer and set network interface device up
  82. */
  83. static void tcpip_init_done_callback(void *arg)
  84. {
  85. rt_device_t device;
  86. struct eth_device *ethif;
  87. ip4_addr_t ipaddr, netmask, gw;
  88. struct rt_list_node* node;
  89. struct rt_object* object;
  90. struct rt_object_information *information;
  91. LWIP_ASSERT("invalid arg.\n",arg);
  92. IP4_ADDR(&gw, 0,0,0,0);
  93. IP4_ADDR(&ipaddr, 0,0,0,0);
  94. IP4_ADDR(&netmask, 0,0,0,0);
  95. /* enter critical */
  96. rt_enter_critical();
  97. /* for each network interfaces */
  98. information = rt_object_get_information(RT_Object_Class_Device);
  99. RT_ASSERT(information != RT_NULL);
  100. for (node = information->object_list.next;
  101. node != &(information->object_list);
  102. node = node->next)
  103. {
  104. object = rt_list_entry(node, struct rt_object, list);
  105. device = (rt_device_t)object;
  106. if (device->type == RT_Device_Class_NetIf)
  107. {
  108. ethif = (struct eth_device *)device;
  109. /* leave critical */
  110. rt_exit_critical();
  111. LOCK_TCPIP_CORE();
  112. netif_add(ethif->netif, &ipaddr, &netmask, &gw,
  113. ethif, netif_device_init, tcpip_input);
  114. if (netif_default == RT_NULL)
  115. netif_set_default(ethif->netif);
  116. #if LWIP_DHCP
  117. /* set interface up */
  118. netif_set_up(ethif->netif);
  119. /* if this interface uses DHCP, start the DHCP client */
  120. dhcp_start(ethif->netif);
  121. #else
  122. /* set interface up */
  123. netif_set_up(ethif->netif);
  124. #endif
  125. if (ethif->flags & ETHIF_LINK_PHYUP)
  126. {
  127. netif_set_link_up(ethif->netif);
  128. }
  129. UNLOCK_TCPIP_CORE();
  130. /* enter critical */
  131. rt_enter_critical();
  132. }
  133. }
  134. /* leave critical */
  135. rt_exit_critical();
  136. rt_sem_release((rt_sem_t)arg);
  137. }
  138. /**
  139. * LwIP system initialization
  140. */
  141. extern int eth_system_device_init_private(void);
  142. int lwip_system_init(void)
  143. {
  144. rt_err_t rc;
  145. struct rt_semaphore done_sem;
  146. static rt_bool_t init_ok = RT_FALSE;
  147. if (init_ok)
  148. {
  149. rt_kprintf("lwip system already init.\n");
  150. return 0;
  151. }
  152. eth_system_device_init_private();
  153. /* set default netif to NULL */
  154. netif_default = RT_NULL;
  155. rc = rt_sem_init(&done_sem, "done", 0, RT_IPC_FLAG_FIFO);
  156. if (rc != RT_EOK)
  157. {
  158. LWIP_ASSERT("Failed to create semaphore", 0);
  159. return -1;
  160. }
  161. tcpip_init(tcpip_init_done_callback, (void *)&done_sem);
  162. /* waiting for initialization done */
  163. if (rt_sem_take(&done_sem, RT_WAITING_FOREVER) != RT_EOK)
  164. {
  165. rt_sem_detach(&done_sem);
  166. return -1;
  167. }
  168. rt_sem_detach(&done_sem);
  169. /* set default ip address */
  170. #if !LWIP_DHCP
  171. if (netif_default != RT_NULL)
  172. {
  173. struct ip4_addr ipaddr, netmask, gw;
  174. ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
  175. gw.addr = inet_addr(RT_LWIP_GWADDR);
  176. netmask.addr = inet_addr(RT_LWIP_MSKADDR);
  177. netifapi_netif_set_addr(netif_default, &ipaddr, &netmask, &gw);
  178. }
  179. #endif
  180. rt_kprintf("lwIP-%d.%d.%d initialized!\n", LWIP_VERSION_MAJOR, LWIP_VERSION_MINOR, LWIP_VERSION_REVISION);
  181. init_ok = RT_TRUE;
  182. return 0;
  183. }
  184. INIT_PREV_EXPORT(lwip_system_init);
  185. void sys_init(void)
  186. {
  187. /* nothing on RT-Thread porting */
  188. }
  189. void lwip_sys_init(void)
  190. {
  191. lwip_system_init();
  192. }
  193. /*
  194. * Create a new semaphore
  195. *
  196. * @return the operation status, ERR_OK on OK; others on error
  197. */
  198. err_t sys_sem_new(sys_sem_t *sem, u8_t count)
  199. {
  200. static unsigned short counter = 0;
  201. char tname[RT_NAME_MAX];
  202. sys_sem_t tmpsem;
  203. RT_DEBUG_NOT_IN_INTERRUPT;
  204. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_SEM_NAME, counter);
  205. counter ++;
  206. tmpsem = rt_sem_create(tname, count, RT_IPC_FLAG_FIFO);
  207. if (tmpsem == RT_NULL)
  208. return ERR_MEM;
  209. else
  210. {
  211. *sem = tmpsem;
  212. return ERR_OK;
  213. }
  214. }
  215. /*
  216. * Deallocate a semaphore
  217. */
  218. void sys_sem_free(sys_sem_t *sem)
  219. {
  220. RT_DEBUG_NOT_IN_INTERRUPT;
  221. rt_sem_delete(*sem);
  222. }
  223. /*
  224. * Signal a semaphore
  225. */
  226. void sys_sem_signal(sys_sem_t *sem)
  227. {
  228. rt_sem_release(*sem);
  229. }
  230. /*
  231. * Block the thread while waiting for the semaphore to be signaled
  232. *
  233. * @return If the timeout argument is non-zero, it will return the number of milliseconds
  234. * spent waiting for the semaphore to be signaled; If the semaphore isn't signaled
  235. * within the specified time, it will return SYS_ARCH_TIMEOUT; If the thread doesn't
  236. * wait for the semaphore, it will return zero
  237. */
  238. u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout)
  239. {
  240. rt_err_t ret;
  241. s32_t t;
  242. u32_t tick;
  243. RT_DEBUG_NOT_IN_INTERRUPT;
  244. /* get the begin tick */
  245. tick = rt_tick_get();
  246. if (timeout == 0)
  247. t = RT_WAITING_FOREVER;
  248. else
  249. {
  250. /* convert msecond to os tick */
  251. if (timeout < (1000/RT_TICK_PER_SECOND))
  252. t = 1;
  253. else
  254. t = timeout / (1000/RT_TICK_PER_SECOND);
  255. }
  256. ret = rt_sem_take(*sem, t);
  257. if (ret == -RT_ETIMEOUT)
  258. return SYS_ARCH_TIMEOUT;
  259. else
  260. {
  261. if (ret == RT_EOK)
  262. ret = 1;
  263. }
  264. /* get elapse msecond */
  265. tick = rt_tick_get() - tick;
  266. /* convert tick to msecond */
  267. tick = tick * (1000 / RT_TICK_PER_SECOND);
  268. if (tick == 0)
  269. tick = 1;
  270. return tick;
  271. }
  272. #ifndef sys_sem_valid
  273. /** Check if a semaphore is valid/allocated:
  274. * return 1 for valid, 0 for invalid
  275. */
  276. int sys_sem_valid(sys_sem_t *sem)
  277. {
  278. return (int)(*sem);
  279. }
  280. #endif
  281. #ifndef sys_sem_set_invalid
  282. /** Set a semaphore invalid so that sys_sem_valid returns 0
  283. */
  284. void sys_sem_set_invalid(sys_sem_t *sem)
  285. {
  286. *sem = RT_NULL;
  287. }
  288. #endif
  289. /* ====================== Mutex ====================== */
  290. /** Create a new mutex
  291. * @param mutex pointer to the mutex to create
  292. * @return a new mutex
  293. */
  294. err_t sys_mutex_new(sys_mutex_t *mutex)
  295. {
  296. static unsigned short counter = 0;
  297. char tname[RT_NAME_MAX];
  298. sys_mutex_t tmpmutex;
  299. RT_DEBUG_NOT_IN_INTERRUPT;
  300. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MUTEX_NAME, counter);
  301. counter ++;
  302. tmpmutex = rt_mutex_create(tname, RT_IPC_FLAG_PRIO);
  303. if (tmpmutex == RT_NULL)
  304. return ERR_MEM;
  305. else
  306. {
  307. *mutex = tmpmutex;
  308. return ERR_OK;
  309. }
  310. }
  311. /** Lock a mutex
  312. * @param mutex the mutex to lock
  313. */
  314. void sys_mutex_lock(sys_mutex_t *mutex)
  315. {
  316. RT_DEBUG_NOT_IN_INTERRUPT;
  317. rt_mutex_take(*mutex, RT_WAITING_FOREVER);
  318. return;
  319. }
  320. /** Unlock a mutex
  321. * @param mutex the mutex to unlock
  322. */
  323. void sys_mutex_unlock(sys_mutex_t *mutex)
  324. {
  325. rt_mutex_release(*mutex);
  326. }
  327. /** Delete a semaphore
  328. * @param mutex the mutex to delete
  329. */
  330. void sys_mutex_free(sys_mutex_t *mutex)
  331. {
  332. RT_DEBUG_NOT_IN_INTERRUPT;
  333. rt_mutex_delete(*mutex);
  334. }
  335. #ifndef sys_mutex_valid
  336. /** Check if a mutex is valid/allocated:
  337. * return 1 for valid, 0 for invalid
  338. */
  339. int sys_mutex_valid(sys_mutex_t *mutex)
  340. {
  341. return (int)(*mutex);
  342. }
  343. #endif
  344. #ifndef sys_mutex_set_invalid
  345. /** Set a mutex invalid so that sys_mutex_valid returns 0
  346. */
  347. void sys_mutex_set_invalid(sys_mutex_t *mutex)
  348. {
  349. *mutex = RT_NULL;
  350. }
  351. #endif
  352. /* ====================== Mailbox ====================== */
  353. /*
  354. * Create an empty mailbox for maximum "size" elements
  355. *
  356. * @return the operation status, ERR_OK on OK; others on error
  357. */
  358. err_t sys_mbox_new(sys_mbox_t *mbox, int size)
  359. {
  360. static unsigned short counter = 0;
  361. char tname[RT_NAME_MAX];
  362. sys_mbox_t tmpmbox;
  363. RT_DEBUG_NOT_IN_INTERRUPT;
  364. rt_snprintf(tname, RT_NAME_MAX, "%s%d", SYS_LWIP_MBOX_NAME, counter);
  365. counter ++;
  366. tmpmbox = rt_mb_create(tname, size, RT_IPC_FLAG_FIFO);
  367. if (tmpmbox != RT_NULL)
  368. {
  369. *mbox = tmpmbox;
  370. return ERR_OK;
  371. }
  372. return ERR_MEM;
  373. }
  374. /*
  375. * Deallocate a mailbox
  376. */
  377. void sys_mbox_free(sys_mbox_t *mbox)
  378. {
  379. RT_DEBUG_NOT_IN_INTERRUPT;
  380. rt_mb_delete(*mbox);
  381. return;
  382. }
  383. /** Post a message to an mbox - may not fail
  384. * -> blocks if full, only used from tasks not from ISR
  385. * @param mbox mbox to posts the message
  386. * @param msg message to post (ATTENTION: can be NULL)
  387. */
  388. void sys_mbox_post(sys_mbox_t *mbox, void *msg)
  389. {
  390. RT_DEBUG_NOT_IN_INTERRUPT;
  391. rt_mb_send_wait(*mbox, (rt_uint32_t)msg, RT_WAITING_FOREVER);
  392. return;
  393. }
  394. /*
  395. * Try to post the "msg" to the mailbox
  396. *
  397. * @return return ERR_OK if the "msg" is posted, ERR_MEM if the mailbox is full
  398. */
  399. err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg)
  400. {
  401. if (rt_mb_send(*mbox, (rt_uint32_t)msg) == RT_EOK)
  402. return ERR_OK;
  403. return ERR_MEM;
  404. }
  405. err_t
  406. sys_mbox_trypost_fromisr(sys_mbox_t *q, void *msg)
  407. {
  408. return sys_mbox_trypost(q, msg);
  409. }
  410. /** Wait for a new message to arrive in the mbox
  411. * @param mbox mbox to get a message from
  412. * @param msg pointer where the message is stored
  413. * @param timeout maximum time (in milliseconds) to wait for a message
  414. * @return time (in milliseconds) waited for a message, may be 0 if not waited
  415. or SYS_ARCH_TIMEOUT on timeout
  416. * The returned time has to be accurate to prevent timer jitter!
  417. */
  418. u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
  419. {
  420. rt_err_t ret;
  421. s32_t t;
  422. u32_t tick;
  423. RT_DEBUG_NOT_IN_INTERRUPT;
  424. /* get the begin tick */
  425. tick = rt_tick_get();
  426. if(timeout == 0)
  427. t = RT_WAITING_FOREVER;
  428. else
  429. {
  430. /* convirt msecond to os tick */
  431. if (timeout < (1000/RT_TICK_PER_SECOND))
  432. t = 1;
  433. else
  434. t = timeout / (1000/RT_TICK_PER_SECOND);
  435. }
  436. ret = rt_mb_recv(*mbox, (rt_ubase_t *)msg, t);
  437. if(ret != RT_EOK)
  438. {
  439. return SYS_ARCH_TIMEOUT;
  440. }
  441. /* get elapse msecond */
  442. tick = rt_tick_get() - tick;
  443. /* convert tick to msecond */
  444. tick = tick * (1000 / RT_TICK_PER_SECOND);
  445. if (tick == 0)
  446. tick = 1;
  447. return tick;
  448. }
  449. /** Wait for a new message to arrive in the mbox
  450. * @param mbox mbox to get a message from
  451. * @param msg pointer where the message is stored
  452. * @param timeout maximum time (in milliseconds) to wait for a message
  453. * @return 0 (milliseconds) if a message has been received
  454. * or SYS_MBOX_EMPTY if the mailbox is empty
  455. */
  456. u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg)
  457. {
  458. int ret;
  459. ret = rt_mb_recv(*mbox, (rt_ubase_t *)msg, 0);
  460. if(ret == -RT_ETIMEOUT)
  461. return SYS_ARCH_TIMEOUT;
  462. else
  463. {
  464. if (ret == RT_EOK)
  465. ret = 0;
  466. }
  467. return ret;
  468. }
  469. #ifndef sys_mbox_valid
  470. /** Check if an mbox is valid/allocated:
  471. * return 1 for valid, 0 for invalid
  472. */
  473. int sys_mbox_valid(sys_mbox_t *mbox)
  474. {
  475. return (int)(*mbox);
  476. }
  477. #endif
  478. #ifndef sys_mbox_set_invalid
  479. /** Set an mbox invalid so that sys_mbox_valid returns 0
  480. */
  481. void sys_mbox_set_invalid(sys_mbox_t *mbox)
  482. {
  483. *mbox = RT_NULL;
  484. }
  485. #endif
  486. /* ====================== System ====================== */
  487. /*
  488. * Start a new thread named "name" with priority "prio" that will begin
  489. * its execution in the function "thread()". The "arg" argument will be
  490. * passed as an argument to the thread() function
  491. */
  492. sys_thread_t sys_thread_new(const char *name,
  493. lwip_thread_fn thread,
  494. void *arg,
  495. int stacksize,
  496. int prio)
  497. {
  498. rt_thread_t t;
  499. RT_DEBUG_NOT_IN_INTERRUPT;
  500. /* create thread */
  501. t = rt_thread_create(name, thread, arg, stacksize, prio, 20);
  502. RT_ASSERT(t != RT_NULL);
  503. /* startup thread */
  504. rt_thread_startup(t);
  505. return t;
  506. }
  507. sys_prot_t sys_arch_protect(void)
  508. {
  509. rt_base_t level;
  510. /* disable interrupt */
  511. level = rt_hw_interrupt_disable();
  512. return level;
  513. }
  514. void sys_arch_unprotect(sys_prot_t pval)
  515. {
  516. /* enable interrupt */
  517. rt_hw_interrupt_enable(pval);
  518. return;
  519. }
  520. void sys_arch_assert(const char *file, int line)
  521. {
  522. rt_kprintf("\nAssertion: %d in %s, thread %s\n",
  523. line, file, rt_thread_self()->name);
  524. RT_ASSERT(0);
  525. }
  526. u32_t sys_jiffies(void)
  527. {
  528. return rt_tick_get();
  529. }
  530. u32_t sys_now(void)
  531. {
  532. return rt_tick_get_millisecond();
  533. }
  534. #if MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK
  535. /**
  536. * Check if a mep element was victim of an overflow or underflow
  537. * (e.g. the restricted area after/before it has been altered)
  538. *
  539. * @param p the mem element to check
  540. * @param size allocated size of the element
  541. * @param descr1 description of the element source shown on error
  542. * @param descr2 description of the element source shown on error
  543. */
  544. void
  545. mem_overflow_check_raw(void *p, size_t size, const char *descr1, const char *descr2)
  546. {
  547. #if MEM_SANITY_REGION_AFTER_ALIGNED || MEM_SANITY_REGION_BEFORE_ALIGNED
  548. u16_t k;
  549. u8_t *m;
  550. #if MEM_SANITY_REGION_AFTER_ALIGNED > 0
  551. m = (u8_t *)p + size;
  552. for (k = 0; k < MEM_SANITY_REGION_AFTER_ALIGNED; k++) {
  553. if (m[k] != 0xcd) {
  554. char errstr[128];
  555. snprintf(errstr, sizeof(errstr), "detected mem overflow in %s%s", descr1, descr2);
  556. LWIP_ASSERT(errstr, 0);
  557. }
  558. }
  559. #endif /* MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  560. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0
  561. m = (u8_t *)p - MEM_SANITY_REGION_BEFORE_ALIGNED;
  562. for (k = 0; k < MEM_SANITY_REGION_BEFORE_ALIGNED; k++) {
  563. if (m[k] != 0xcd) {
  564. char errstr[128];
  565. snprintf(errstr, sizeof(errstr), "detected mem underflow in %s%s", descr1, descr2);
  566. LWIP_ASSERT(errstr, 0);
  567. }
  568. }
  569. #endif /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 */
  570. #else
  571. LWIP_UNUSED_ARG(p);
  572. LWIP_UNUSED_ARG(desc);
  573. LWIP_UNUSED_ARG(descr);
  574. #endif
  575. }
  576. /**
  577. * Initialize the restricted area of a mem element.
  578. */
  579. void
  580. mem_overflow_init_raw(void *p, size_t size)
  581. {
  582. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0
  583. u8_t *m;
  584. #if MEM_SANITY_REGION_BEFORE_ALIGNED > 0
  585. m = (u8_t *)p - MEM_SANITY_REGION_BEFORE_ALIGNED;
  586. memset(m, 0xcd, MEM_SANITY_REGION_BEFORE_ALIGNED);
  587. #endif
  588. #if MEM_SANITY_REGION_AFTER_ALIGNED > 0
  589. m = (u8_t *)p + size;
  590. memset(m, 0xcd, MEM_SANITY_REGION_AFTER_ALIGNED);
  591. #endif
  592. #else /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  593. LWIP_UNUSED_ARG(p);
  594. LWIP_UNUSED_ARG(desc);
  595. #endif /* MEM_SANITY_REGION_BEFORE_ALIGNED > 0 || MEM_SANITY_REGION_AFTER_ALIGNED > 0 */
  596. }
  597. #endif /* MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK */
  598. RT_WEAK
  599. void mem_init(void)
  600. {
  601. }
  602. void *mem_calloc(mem_size_t count, mem_size_t size)
  603. {
  604. return rt_calloc(count, size);
  605. }
  606. void *mem_trim(void *mem, mem_size_t size)
  607. {
  608. // return rt_realloc(mem, size);
  609. /* not support trim yet */
  610. return mem;
  611. }
  612. void *mem_malloc(mem_size_t size)
  613. {
  614. return rt_malloc(size);
  615. }
  616. void mem_free(void *mem)
  617. {
  618. rt_free(mem);
  619. }
  620. #ifdef RT_LWIP_PPP
  621. u32_t sio_read(sio_fd_t fd, u8_t *buf, u32_t size)
  622. {
  623. u32_t len;
  624. RT_ASSERT(fd != RT_NULL);
  625. len = rt_device_read((rt_device_t)fd, 0, buf, size);
  626. if (len <= 0)
  627. return 0;
  628. return len;
  629. }
  630. u32_t sio_write(sio_fd_t fd, u8_t *buf, u32_t size)
  631. {
  632. RT_ASSERT(fd != RT_NULL);
  633. return rt_device_write((rt_device_t)fd, 0, buf, size);
  634. }
  635. void sio_read_abort(sio_fd_t fd)
  636. {
  637. rt_kprintf("read_abort\n");
  638. }
  639. void ppp_trace(int level, const char *format, ...)
  640. {
  641. va_list args;
  642. rt_size_t length;
  643. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  644. va_start(args, format);
  645. length = rt_vsprintf(rt_log_buf, format, args);
  646. rt_device_write((rt_device_t)rt_console_get_device(), 0, rt_log_buf, length);
  647. va_end(args);
  648. }
  649. #endif
  650. #ifdef LWIP_HOOK_IP4_ROUTE_SRC
  651. struct netif *lwip_ip4_route_src(const ip4_addr_t *dest, const ip4_addr_t *src)
  652. {
  653. struct netif *netif;
  654. /* iterate through netifs */
  655. for (netif = netif_list; netif != NULL; netif = netif->next)
  656. {
  657. /* is the netif up, does it have a link and a valid address? */
  658. if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif)))
  659. {
  660. /* gateway matches on a non broadcast interface? (i.e. peer in a point to point interface) */
  661. if (src != NULL)
  662. {
  663. if (ip4_addr_cmp(src, netif_ip4_addr(netif)))
  664. {
  665. return netif;
  666. }
  667. }
  668. }
  669. }
  670. netif = netif_default;
  671. return netif;
  672. }
  673. #endif /* LWIP_HOOK_IP4_ROUTE_SRC */
  674. /*
  675. * export bsd socket symbol for RT-Thread Application Module
  676. */
  677. #if LWIP_SOCKET
  678. #include <lwip/sockets.h>
  679. RTM_EXPORT(lwip_accept);
  680. RTM_EXPORT(lwip_bind);
  681. RTM_EXPORT(lwip_shutdown);
  682. RTM_EXPORT(lwip_getpeername);
  683. RTM_EXPORT(lwip_getsockname);
  684. RTM_EXPORT(lwip_getsockopt);
  685. RTM_EXPORT(lwip_setsockopt);
  686. RTM_EXPORT(lwip_close);
  687. RTM_EXPORT(lwip_connect);
  688. RTM_EXPORT(lwip_listen);
  689. RTM_EXPORT(lwip_recv);
  690. RTM_EXPORT(lwip_read);
  691. RTM_EXPORT(lwip_recvfrom);
  692. RTM_EXPORT(lwip_send);
  693. RTM_EXPORT(lwip_sendto);
  694. RTM_EXPORT(lwip_socket);
  695. RTM_EXPORT(lwip_write);
  696. RTM_EXPORT(lwip_select);
  697. RTM_EXPORT(lwip_ioctl);
  698. RTM_EXPORT(lwip_fcntl);
  699. RTM_EXPORT(lwip_htons);
  700. RTM_EXPORT(lwip_htonl);
  701. #if LWIP_DNS
  702. #include <lwip/netdb.h>
  703. RTM_EXPORT(lwip_gethostbyname);
  704. RTM_EXPORT(lwip_gethostbyname_r);
  705. RTM_EXPORT(lwip_freeaddrinfo);
  706. RTM_EXPORT(lwip_getaddrinfo);
  707. #endif
  708. #endif
  709. #if LWIP_DHCP
  710. #include <lwip/dhcp.h>
  711. RTM_EXPORT(dhcp_start);
  712. RTM_EXPORT(dhcp_renew);
  713. RTM_EXPORT(dhcp_stop);
  714. #endif
  715. #if LWIP_NETIF_API
  716. #include <lwip/netifapi.h>
  717. RTM_EXPORT(netifapi_netif_set_addr);
  718. #endif
  719. #if LWIP_NETIF_LINK_CALLBACK
  720. RTM_EXPORT(netif_set_link_callback);
  721. #endif
  722. #if LWIP_NETIF_STATUS_CALLBACK
  723. RTM_EXPORT(netif_set_status_callback);
  724. #endif
  725. RTM_EXPORT(netif_find);
  726. RTM_EXPORT(netif_set_addr);
  727. RTM_EXPORT(netif_set_ipaddr);
  728. RTM_EXPORT(netif_set_gw);
  729. RTM_EXPORT(netif_set_netmask);