slipif.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**
  2. * @file
  3. * SLIP Interface
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the Institute nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
  35. *
  36. * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
  37. * Simon Goldschmidt
  38. */
  39. /**
  40. * @defgroup slipif SLIP netif
  41. * @ingroup addons
  42. *
  43. * This is an arch independent SLIP netif. The specific serial hooks must be
  44. * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send
  45. *
  46. * Usage: This netif can be used in three ways:\n
  47. * 1) For NO_SYS==0, an RX thread can be used which blocks on sio_read()
  48. * until data is received.\n
  49. * 2) In your main loop, call slipif_poll() to check for new RX bytes,
  50. * completed packets are fed into netif->input().\n
  51. * 3) Call slipif_received_byte[s]() from your serial RX ISR and
  52. * slipif_process_rxqueue() from your main loop. ISR level decodes
  53. * packets and puts completed packets on a queue which is fed into
  54. * the stack from the main loop (needs SYS_LIGHTWEIGHT_PROT for
  55. * pbuf_alloc to work on ISR level!).
  56. *
  57. */
  58. #include "netif/slipif.h"
  59. #include "lwip/opt.h"
  60. #include "lwip/def.h"
  61. #include "lwip/pbuf.h"
  62. #include "lwip/stats.h"
  63. #include "lwip/snmp.h"
  64. #include "lwip/sys.h"
  65. #include "lwip/sio.h"
  66. #define SLIP_END 0xC0 /* 0300: start and end of every packet */
  67. #define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */
  68. #define SLIP_ESC_END 0xDC /* 0334: following escape: original byte is 0xC0 (END) */
  69. #define SLIP_ESC_ESC 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */
  70. /** Maximum packet size that is received by this netif */
  71. #ifndef SLIP_MAX_SIZE
  72. #define SLIP_MAX_SIZE 1500
  73. #endif
  74. /** Define this to the interface speed for SNMP
  75. * (sio_fd is the sio_fd_t returned by sio_open).
  76. * The default value of zero means 'unknown'.
  77. */
  78. #ifndef SLIP_SIO_SPEED
  79. #define SLIP_SIO_SPEED(sio_fd) 0
  80. #endif
  81. enum slipif_recv_state {
  82. SLIP_RECV_NORMAL,
  83. SLIP_RECV_ESCAPE
  84. };
  85. struct slipif_priv {
  86. sio_fd_t sd;
  87. /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
  88. struct pbuf *p, *q;
  89. u8_t state;
  90. u16_t i, recved;
  91. #if SLIP_RX_FROM_ISR
  92. struct pbuf *rxpackets;
  93. #endif
  94. };
  95. /**
  96. * Send a pbuf doing the necessary SLIP encapsulation
  97. *
  98. * Uses the serial layer's sio_send()
  99. *
  100. * @param netif the lwip network interface structure for this slipif
  101. * @param p the pbuf chain packet to send
  102. * @return always returns ERR_OK since the serial layer does not provide return values
  103. */
  104. static err_t
  105. slipif_output(struct netif *netif, struct pbuf *p)
  106. {
  107. struct slipif_priv *priv;
  108. struct pbuf *q;
  109. u16_t i;
  110. u8_t c;
  111. LWIP_ASSERT("netif != NULL", (netif != NULL));
  112. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  113. LWIP_ASSERT("p != NULL", (p != NULL));
  114. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output(%"U16_F"): sending %"U16_F" bytes\n", (u16_t)netif->num, p->tot_len));
  115. priv = (struct slipif_priv *)netif->state;
  116. /* Send pbuf out on the serial I/O device. */
  117. /* Start with packet delimiter. */
  118. sio_send(SLIP_END, priv->sd);
  119. for (q = p; q != NULL; q = q->next) {
  120. for (i = 0; i < q->len; i++) {
  121. c = ((u8_t *)q->payload)[i];
  122. switch (c) {
  123. case SLIP_END:
  124. /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */
  125. sio_send(SLIP_ESC, priv->sd);
  126. sio_send(SLIP_ESC_END, priv->sd);
  127. break;
  128. case SLIP_ESC:
  129. /* need to escape this byte (0xDB -> 0xDB, 0xDD) */
  130. sio_send(SLIP_ESC, priv->sd);
  131. sio_send(SLIP_ESC_ESC, priv->sd);
  132. break;
  133. default:
  134. /* normal byte - no need for escaping */
  135. sio_send(c, priv->sd);
  136. break;
  137. }
  138. }
  139. }
  140. /* End with packet delimiter. */
  141. sio_send(SLIP_END, priv->sd);
  142. return ERR_OK;
  143. }
  144. #if LWIP_IPV4
  145. /**
  146. * Send a pbuf doing the necessary SLIP encapsulation
  147. *
  148. * Uses the serial layer's sio_send()
  149. *
  150. * @param netif the lwip network interface structure for this slipif
  151. * @param p the pbuf chain packet to send
  152. * @param ipaddr the ip address to send the packet to (not used for slipif)
  153. * @return always returns ERR_OK since the serial layer does not provide return values
  154. */
  155. static err_t
  156. slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
  157. {
  158. LWIP_UNUSED_ARG(ipaddr);
  159. return slipif_output(netif, p);
  160. }
  161. #endif /* LWIP_IPV4 */
  162. #if LWIP_IPV6
  163. /**
  164. * Send a pbuf doing the necessary SLIP encapsulation
  165. *
  166. * Uses the serial layer's sio_send()
  167. *
  168. * @param netif the lwip network interface structure for this slipif
  169. * @param p the pbuf chain packet to send
  170. * @param ipaddr the ip address to send the packet to (not used for slipif)
  171. * @return always returns ERR_OK since the serial layer does not provide return values
  172. */
  173. static err_t
  174. slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
  175. {
  176. LWIP_UNUSED_ARG(ipaddr);
  177. return slipif_output(netif, p);
  178. }
  179. #endif /* LWIP_IPV6 */
  180. /**
  181. * Handle the incoming SLIP stream character by character
  182. *
  183. * @param netif the lwip network interface structure for this slipif
  184. * @param c received character (multiple calls to this function will
  185. * return a complete packet, NULL is returned before - used for polling)
  186. * @return The IP packet when SLIP_END is received
  187. */
  188. static struct pbuf*
  189. slipif_rxbyte(struct netif *netif, u8_t c)
  190. {
  191. struct slipif_priv *priv;
  192. struct pbuf *t;
  193. LWIP_ASSERT("netif != NULL", (netif != NULL));
  194. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  195. priv = (struct slipif_priv *)netif->state;
  196. switch (priv->state) {
  197. case SLIP_RECV_NORMAL:
  198. switch (c) {
  199. case SLIP_END:
  200. if (priv->recved > 0) {
  201. /* Received whole packet. */
  202. /* Trim the pbuf to the size of the received packet. */
  203. pbuf_realloc(priv->q, priv->recved);
  204. LINK_STATS_INC(link.recv);
  205. LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet (%"U16_F" bytes)\n", priv->recved));
  206. t = priv->q;
  207. priv->p = priv->q = NULL;
  208. priv->i = priv->recved = 0;
  209. return t;
  210. }
  211. return NULL;
  212. case SLIP_ESC:
  213. priv->state = SLIP_RECV_ESCAPE;
  214. return NULL;
  215. default:
  216. break;
  217. } /* end switch (c) */
  218. break;
  219. case SLIP_RECV_ESCAPE:
  220. /* un-escape END or ESC bytes, leave other bytes
  221. (although that would be a protocol error) */
  222. switch (c) {
  223. case SLIP_ESC_END:
  224. c = SLIP_END;
  225. break;
  226. case SLIP_ESC_ESC:
  227. c = SLIP_ESC;
  228. break;
  229. default:
  230. break;
  231. }
  232. priv->state = SLIP_RECV_NORMAL;
  233. break;
  234. default:
  235. break;
  236. } /* end switch (priv->state) */
  237. /* byte received, packet not yet completely received */
  238. if (priv->p == NULL) {
  239. /* allocate a new pbuf */
  240. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
  241. priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN - PBUF_LINK_ENCAPSULATION_HLEN), PBUF_POOL);
  242. if (priv->p == NULL) {
  243. LINK_STATS_INC(link.drop);
  244. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
  245. /* don't process any further since we got no pbuf to receive to */
  246. return NULL;
  247. }
  248. if (priv->q != NULL) {
  249. /* 'chain' the pbuf to the existing chain */
  250. pbuf_cat(priv->q, priv->p);
  251. } else {
  252. /* p is the first pbuf in the chain */
  253. priv->q = priv->p;
  254. }
  255. }
  256. /* this automatically drops bytes if > SLIP_MAX_SIZE */
  257. if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
  258. ((u8_t *)priv->p->payload)[priv->i] = c;
  259. priv->recved++;
  260. priv->i++;
  261. if (priv->i >= priv->p->len) {
  262. /* on to the next pbuf */
  263. priv->i = 0;
  264. if (priv->p->next != NULL && priv->p->next->len > 0) {
  265. /* p is a chain, on to the next in the chain */
  266. priv->p = priv->p->next;
  267. } else {
  268. /* p is a single pbuf, set it to NULL so next time a new
  269. * pbuf is allocated */
  270. priv->p = NULL;
  271. }
  272. }
  273. }
  274. return NULL;
  275. }
  276. /** Like slipif_rxbyte, but passes completed packets to netif->input
  277. *
  278. * @param netif The lwip network interface structure for this slipif
  279. * @param c received character
  280. */
  281. static void
  282. slipif_rxbyte_input(struct netif *netif, u8_t c)
  283. {
  284. struct pbuf *p;
  285. p = slipif_rxbyte(netif, c);
  286. if (p != NULL) {
  287. if (netif->input(p, netif) != ERR_OK) {
  288. pbuf_free(p);
  289. }
  290. }
  291. }
  292. #if SLIP_USE_RX_THREAD
  293. /**
  294. * The SLIP input thread.
  295. *
  296. * Feed the IP layer with incoming packets
  297. *
  298. * @param nf the lwip network interface structure for this slipif
  299. */
  300. static void
  301. slipif_loop_thread(void *nf)
  302. {
  303. u8_t c;
  304. struct netif *netif = (struct netif *)nf;
  305. struct slipif_priv *priv = (struct slipif_priv *)netif->state;
  306. while (1) {
  307. if (sio_read(priv->sd, &c, 1) > 0) {
  308. slipif_rxbyte_input(netif, c);
  309. }
  310. }
  311. }
  312. #endif /* SLIP_USE_RX_THREAD */
  313. /**
  314. * SLIP netif initialization
  315. *
  316. * Call the arch specific sio_open and remember
  317. * the opened device in the state field of the netif.
  318. *
  319. * @param netif the lwip network interface structure for this slipif
  320. * @return ERR_OK if serial line could be opened,
  321. * ERR_MEM if no memory could be allocated,
  322. * ERR_IF is serial line couldn't be opened
  323. *
  324. * @note netif->num must contain the number of the serial port to open
  325. * (0 by default). If netif->state is != NULL, it is interpreted as an
  326. * u8_t pointer pointing to the serial port number instead of netif->num.
  327. *
  328. */
  329. err_t
  330. slipif_init(struct netif *netif)
  331. {
  332. struct slipif_priv *priv;
  333. u8_t sio_num;
  334. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
  335. /* Allocate private data */
  336. priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv));
  337. if (!priv) {
  338. return ERR_MEM;
  339. }
  340. netif->name[0] = 's';
  341. netif->name[1] = 'l';
  342. #if LWIP_IPV4
  343. netif->output = slipif_output_v4;
  344. #endif /* LWIP_IPV4 */
  345. #if LWIP_IPV6
  346. netif->output_ip6 = slipif_output_v6;
  347. #endif /* LWIP_IPV6 */
  348. netif->mtu = SLIP_MAX_SIZE;
  349. /* netif->state or netif->num contain the port number */
  350. if (netif->state != NULL) {
  351. sio_num = *(u8_t*)netif->state;
  352. } else {
  353. sio_num = netif->num;
  354. }
  355. /* Try to open the serial port. */
  356. priv->sd = sio_open(sio_num);
  357. if (!priv->sd) {
  358. /* Opening the serial port failed. */
  359. mem_free(priv);
  360. return ERR_IF;
  361. }
  362. /* Initialize private data */
  363. priv->p = NULL;
  364. priv->q = NULL;
  365. priv->state = SLIP_RECV_NORMAL;
  366. priv->i = 0;
  367. priv->recved = 0;
  368. #if SLIP_RX_FROM_ISR
  369. priv->rxpackets = NULL;
  370. #endif
  371. netif->state = priv;
  372. /* initialize the snmp variables and counters inside the struct netif */
  373. MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd));
  374. #if SLIP_USE_RX_THREAD
  375. /* Create a thread to poll the serial line. */
  376. sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
  377. SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO);
  378. #endif /* SLIP_USE_RX_THREAD */
  379. return ERR_OK;
  380. }
  381. /**
  382. * Polls the serial device and feeds the IP layer with incoming packets.
  383. *
  384. * @param netif The lwip network interface structure for this slipif
  385. */
  386. void
  387. slipif_poll(struct netif *netif)
  388. {
  389. u8_t c;
  390. struct slipif_priv *priv;
  391. LWIP_ASSERT("netif != NULL", (netif != NULL));
  392. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  393. priv = (struct slipif_priv *)netif->state;
  394. while (sio_tryread(priv->sd, &c, 1) > 0) {
  395. slipif_rxbyte_input(netif, c);
  396. }
  397. }
  398. #if SLIP_RX_FROM_ISR
  399. /**
  400. * Feeds the IP layer with incoming packets that were receive
  401. *
  402. * @param netif The lwip network interface structure for this slipif
  403. */
  404. void
  405. slipif_process_rxqueue(struct netif *netif)
  406. {
  407. struct slipif_priv *priv;
  408. SYS_ARCH_DECL_PROTECT(old_level);
  409. LWIP_ASSERT("netif != NULL", (netif != NULL));
  410. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  411. priv = (struct slipif_priv *)netif->state;
  412. SYS_ARCH_PROTECT(old_level);
  413. while (priv->rxpackets != NULL) {
  414. struct pbuf *p = priv->rxpackets;
  415. #if SLIP_RX_QUEUE
  416. /* dequeue packet */
  417. struct pbuf *q = p;
  418. while ((q->len != q->tot_len) && (q->next != NULL)) {
  419. q = q->next;
  420. }
  421. priv->rxpackets = q->next;
  422. q->next = NULL;
  423. #else /* SLIP_RX_QUEUE */
  424. priv->rxpackets = NULL;
  425. #endif /* SLIP_RX_QUEUE */
  426. SYS_ARCH_UNPROTECT(old_level);
  427. if (netif->input(p, netif) != ERR_OK) {
  428. pbuf_free(p);
  429. }
  430. SYS_ARCH_PROTECT(old_level);
  431. }
  432. }
  433. /** Like slipif_rxbyte, but queues completed packets.
  434. *
  435. * @param netif The lwip network interface structure for this slipif
  436. * @param data Received serial byte
  437. */
  438. static void
  439. slipif_rxbyte_enqueue(struct netif *netif, u8_t data)
  440. {
  441. struct pbuf *p;
  442. struct slipif_priv *priv = (struct slipif_priv *)netif->state;
  443. SYS_ARCH_DECL_PROTECT(old_level);
  444. p = slipif_rxbyte(netif, data);
  445. if (p != NULL) {
  446. SYS_ARCH_PROTECT(old_level);
  447. if (priv->rxpackets != NULL) {
  448. #if SLIP_RX_QUEUE
  449. /* queue multiple pbufs */
  450. struct pbuf *q = p;
  451. while (q->next != NULL) {
  452. q = q->next;
  453. }
  454. q->next = p;
  455. } else {
  456. #else /* SLIP_RX_QUEUE */
  457. pbuf_free(priv->rxpackets);
  458. }
  459. {
  460. #endif /* SLIP_RX_QUEUE */
  461. priv->rxpackets = p;
  462. }
  463. SYS_ARCH_UNPROTECT(old_level);
  464. }
  465. }
  466. /**
  467. * Process a received byte, completed packets are put on a queue that is
  468. * fed into IP through slipif_process_rxqueue().
  469. *
  470. * This function can be called from ISR if SYS_LIGHTWEIGHT_PROT is enabled.
  471. *
  472. * @param netif The lwip network interface structure for this slipif
  473. * @param data received character
  474. */
  475. void
  476. slipif_received_byte(struct netif *netif, u8_t data)
  477. {
  478. LWIP_ASSERT("netif != NULL", (netif != NULL));
  479. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  480. slipif_rxbyte_enqueue(netif, data);
  481. }
  482. /**
  483. * Process multiple received byte, completed packets are put on a queue that is
  484. * fed into IP through slipif_process_rxqueue().
  485. *
  486. * This function can be called from ISR if SYS_LIGHTWEIGHT_PROT is enabled.
  487. *
  488. * @param netif The lwip network interface structure for this slipif
  489. * @param data received character
  490. * @param len Number of received characters
  491. */
  492. void
  493. slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len)
  494. {
  495. u8_t i;
  496. u8_t *rxdata = data;
  497. LWIP_ASSERT("netif != NULL", (netif != NULL));
  498. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  499. for (i = 0; i < len; i++, rxdata++) {
  500. slipif_rxbyte_enqueue(netif, *rxdata);
  501. }
  502. }
  503. #endif /* SLIP_RX_FROM_ISR */