slipif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. * Usage: This netif can be used in three ways:
  40. * 1) For NO_SYS==0, an RX thread can be used which blocks on sio_read()
  41. * until data is received.
  42. * 2) In your main loop, call slipif_poll() to check for new RX bytes,
  43. * completed packets are fed into netif->input().
  44. * 3) Call slipif_received_byte[s]() from your serial RX ISR and
  45. * slipif_process_rxqueue() from your main loop. ISR level decodes
  46. * packets and puts completed packets on a queue which is fed into
  47. * the stack from the main loop (needs SYS_LIGHTWEIGHT_PROT for
  48. * pbuf_alloc to work on ISR level!).
  49. *
  50. */
  51. /*
  52. * This is an arch independent SLIP netif. The specific serial hooks must be
  53. * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send
  54. */
  55. #include "netif/slipif.h"
  56. #include "lwip/opt.h"
  57. #if LWIP_HAVE_SLIPIF
  58. #include "lwip/def.h"
  59. #include "lwip/pbuf.h"
  60. #include "lwip/stats.h"
  61. #include "lwip/snmp.h"
  62. #include "lwip/sio.h"
  63. #include "lwip/sys.h"
  64. #define SLIP_END 0xC0 /* 0300: start and end of every packet */
  65. #define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */
  66. #define SLIP_ESC_END 0xDC /* 0334: following escape: original byte is 0xC0 (END) */
  67. #define SLIP_ESC_ESC 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */
  68. /** Maximum packet size that is received by this netif */
  69. #ifndef SLIP_MAX_SIZE
  70. #define SLIP_MAX_SIZE 1500
  71. #endif
  72. /** Define this to the interface speed for SNMP
  73. * (sio_fd is the sio_fd_t returned by sio_open).
  74. * The default value of zero means 'unknown'.
  75. */
  76. #ifndef SLIP_SIO_SPEED
  77. #define SLIP_SIO_SPEED(sio_fd) 0
  78. #endif
  79. enum slipif_recv_state {
  80. SLIP_RECV_NORMAL,
  81. SLIP_RECV_ESCAPE,
  82. };
  83. struct slipif_priv {
  84. sio_fd_t sd;
  85. /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
  86. struct pbuf *p, *q;
  87. u8_t state;
  88. u16_t i, recved;
  89. #if SLIP_RX_FROM_ISR
  90. struct pbuf *rxpackets;
  91. #endif
  92. };
  93. /**
  94. * Send a pbuf doing the necessary SLIP encapsulation
  95. *
  96. * Uses the serial layer's sio_send()
  97. *
  98. * @param netif the lwip network interface structure for this slipif
  99. * @param p the pbuf chaing packet to send
  100. * @param ipaddr the ip address to send the packet to (not used for slipif)
  101. * @return always returns ERR_OK since the serial layer does not provide return values
  102. */
  103. err_t
  104. slipif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
  105. {
  106. struct slipif_priv *priv;
  107. struct pbuf *q;
  108. u16_t i;
  109. u8_t c;
  110. LWIP_ASSERT("netif != NULL", (netif != NULL));
  111. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  112. LWIP_ASSERT("p != NULL", (p != NULL));
  113. LWIP_UNUSED_ARG(ipaddr);
  114. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output(%"U16_F"): sending %"U16_F" bytes\n", (u16_t)netif->num, p->tot_len));
  115. 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. /**
  145. * Handle the incoming SLIP stream character by character
  146. *
  147. * @param netif the lwip network interface structure for this slipif
  148. * @param c received character (multiple calls to this function will
  149. * return a complete packet, NULL is returned before - used for polling)
  150. * @return The IP packet when SLIP_END is received
  151. */
  152. static struct pbuf*
  153. slipif_rxbyte(struct netif *netif, u8_t c)
  154. {
  155. struct slipif_priv *priv;
  156. struct pbuf *t;
  157. LWIP_ASSERT("netif != NULL", (netif != NULL));
  158. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  159. priv = netif->state;
  160. switch (priv->state) {
  161. case SLIP_RECV_NORMAL:
  162. switch (c) {
  163. case SLIP_END:
  164. if (priv->recved > 0) {
  165. /* Received whole packet. */
  166. /* Trim the pbuf to the size of the received packet. */
  167. pbuf_realloc(priv->q, priv->recved);
  168. LINK_STATS_INC(link.recv);
  169. LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet (%"U16_F" bytes)\n", priv->recved));
  170. t = priv->q;
  171. priv->p = priv->q = NULL;
  172. priv->i = priv->recved = 0;
  173. return t;
  174. }
  175. return NULL;
  176. case SLIP_ESC:
  177. priv->state = SLIP_RECV_ESCAPE;
  178. return NULL;
  179. } /* end switch (c) */
  180. break;
  181. case SLIP_RECV_ESCAPE:
  182. /* un-escape END or ESC bytes, leave other bytes
  183. (although that would be a protocol error) */
  184. switch (c) {
  185. case SLIP_ESC_END:
  186. c = SLIP_END;
  187. break;
  188. case SLIP_ESC_ESC:
  189. c = SLIP_ESC;
  190. break;
  191. }
  192. priv->state = SLIP_RECV_NORMAL;
  193. break;
  194. } /* end switch (priv->state) */
  195. /* byte received, packet not yet completely received */
  196. if (priv->p == NULL) {
  197. /* allocate a new pbuf */
  198. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
  199. priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN), PBUF_POOL);
  200. if (priv->p == NULL) {
  201. LINK_STATS_INC(link.drop);
  202. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
  203. /* don't process any further since we got no pbuf to receive to */
  204. return NULL;
  205. }
  206. if (priv->q != NULL) {
  207. /* 'chain' the pbuf to the existing chain */
  208. pbuf_cat(priv->q, priv->p);
  209. } else {
  210. /* p is the first pbuf in the chain */
  211. priv->q = priv->p;
  212. }
  213. }
  214. /* this automatically drops bytes if > SLIP_MAX_SIZE */
  215. if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
  216. ((u8_t *)priv->p->payload)[priv->i] = c;
  217. priv->recved++;
  218. priv->i++;
  219. if (priv->i >= priv->p->len) {
  220. /* on to the next pbuf */
  221. priv->i = 0;
  222. if (priv->p->next != NULL && priv->p->next->len > 0) {
  223. /* p is a chain, on to the next in the chain */
  224. priv->p = priv->p->next;
  225. } else {
  226. /* p is a single pbuf, set it to NULL so next time a new
  227. * pbuf is allocated */
  228. priv->p = NULL;
  229. }
  230. }
  231. }
  232. return NULL;
  233. }
  234. /** Like slipif_rxbyte, but passes completed packets to netif->input
  235. *
  236. * @param netif The lwip network interface structure for this slipif
  237. * @param data received character
  238. */
  239. static void
  240. slipif_rxbyte_input(struct netif *netif, u8_t c)
  241. {
  242. struct pbuf *p;
  243. p = slipif_rxbyte(netif, c);
  244. if (p != NULL) {
  245. if (netif->input(p, netif) != ERR_OK) {
  246. pbuf_free(p);
  247. }
  248. }
  249. }
  250. #if SLIP_USE_RX_THREAD
  251. /**
  252. * The SLIP input thread.
  253. *
  254. * Feed the IP layer with incoming packets
  255. *
  256. * @param nf the lwip network interface structure for this slipif
  257. */
  258. static void
  259. slipif_loop_thread(void *nf)
  260. {
  261. u8_t c;
  262. struct netif *netif = (struct netif *)nf;
  263. struct slipif_priv *priv = (struct slipif_priv *)netif->state;
  264. while (1) {
  265. if (sio_read(priv->sd, &c, 1) > 0) {
  266. slipif_rxbyte_input(netif, c);
  267. }
  268. }
  269. }
  270. #endif /* SLIP_USE_RX_THREAD */
  271. /**
  272. * SLIP netif initialization
  273. *
  274. * Call the arch specific sio_open and remember
  275. * the opened device in the state field of the netif.
  276. *
  277. * @param netif the lwip network interface structure for this slipif
  278. * @return ERR_OK if serial line could be opened,
  279. * ERR_MEM if no memory could be allocated,
  280. * ERR_IF is serial line couldn't be opened
  281. *
  282. * @note netif->num must contain the number of the serial port to open
  283. * (0 by default). If netif->state is != NULL, it is interpreted as an
  284. * u8_t pointer pointing to the serial port number instead of netif->num.
  285. *
  286. */
  287. err_t
  288. slipif_init(struct netif *netif)
  289. {
  290. struct slipif_priv *priv;
  291. u8_t sio_num;
  292. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
  293. /* Allocate private data */
  294. priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv));
  295. if (!priv) {
  296. return ERR_MEM;
  297. }
  298. netif->name[0] = 's';
  299. netif->name[1] = 'l';
  300. netif->output = slipif_output;
  301. netif->mtu = SLIP_MAX_SIZE;
  302. netif->flags |= NETIF_FLAG_POINTTOPOINT;
  303. /* netif->state or netif->num contain the port number */
  304. if (netif->state != NULL) {
  305. sio_num = *(u8_t*)netif->state;
  306. } else {
  307. sio_num = netif->num;
  308. }
  309. /* Try to open the serial port. */
  310. priv->sd = sio_open(sio_num);
  311. if (!priv->sd) {
  312. /* Opening the serial port failed. */
  313. mem_free(priv);
  314. return ERR_IF;
  315. }
  316. /* Initialize private data */
  317. priv->p = NULL;
  318. priv->q = NULL;
  319. priv->state = SLIP_RECV_NORMAL;
  320. priv->i = 0;
  321. priv->recved = 0;
  322. #if SLIP_RX_FROM_ISR
  323. priv->rxpackets = NULL;
  324. #endif
  325. netif->state = priv;
  326. /* initialize the snmp variables and counters inside the struct netif */
  327. NETIF_INIT_SNMP(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd));
  328. #if SLIP_USE_RX_THREAD
  329. /* Create a thread to poll the serial line. */
  330. sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
  331. SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO);
  332. #endif /* SLIP_USE_RX_THREAD */
  333. return ERR_OK;
  334. }
  335. /**
  336. * Polls the serial device and feeds the IP layer with incoming packets.
  337. *
  338. * @param netif The lwip network interface structure for this slipif
  339. */
  340. void
  341. slipif_poll(struct netif *netif)
  342. {
  343. u8_t c;
  344. struct slipif_priv *priv;
  345. LWIP_ASSERT("netif != NULL", (netif != NULL));
  346. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  347. priv = (struct slipif_priv *)netif->state;
  348. while (sio_tryread(priv->sd, &c, 1) > 0) {
  349. slipif_rxbyte_input(netif, c);
  350. }
  351. }
  352. #if SLIP_RX_FROM_ISR
  353. /**
  354. * Feeds the IP layer with incoming packets that were receive
  355. *
  356. * @param netif The lwip network interface structure for this slipif
  357. */
  358. void
  359. slipif_process_rxqueue(struct netif *netif)
  360. {
  361. struct slipif_priv *priv;
  362. SYS_ARCH_DECL_PROTECT(old_level);
  363. LWIP_ASSERT("netif != NULL", (netif != NULL));
  364. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  365. priv = (struct slipif_priv *)netif->state;
  366. SYS_ARCH_PROTECT(old_level);
  367. while (priv->rxpackets != NULL) {
  368. struct pbuf *p = priv->rxpackets;
  369. #if SLIP_RX_QUEUE
  370. /* dequeue packet */
  371. struct pbuf *q = p;
  372. while ((q->len != q->tot_len) && (q->next != NULL)) {
  373. q = q->next;
  374. }
  375. priv->rxpackets = q->next;
  376. q->next = NULL;
  377. #else /* SLIP_RX_QUEUE */
  378. priv->rxpackets = NULL;
  379. #endif /* SLIP_RX_QUEUE */
  380. SYS_ARCH_UNPROTECT(old_level);
  381. if (netif->input(p, netif) != ERR_OK) {
  382. pbuf_free(p);
  383. }
  384. SYS_ARCH_PROTECT(old_level);
  385. }
  386. }
  387. /** Like slipif_rxbyte, but queues completed packets.
  388. *
  389. * @param netif The lwip network interface structure for this slipif
  390. * @param data Received serial byte
  391. */
  392. static void
  393. slipif_rxbyte_enqueue(struct netif *netif, u8_t data)
  394. {
  395. struct pbuf *p;
  396. struct slipif_priv *priv = (struct slipif_priv *)netif->state;
  397. SYS_ARCH_DECL_PROTECT(old_level);
  398. p = slipif_rxbyte(netif, data);
  399. if (p != NULL) {
  400. SYS_ARCH_PROTECT(old_level);
  401. if (priv->rxpackets != NULL) {
  402. #if SLIP_RX_QUEUE
  403. /* queue multiple pbufs */
  404. struct pbuf *q = p;
  405. while(q->next != NULL) {
  406. q = q->next;
  407. }
  408. q->next = p;
  409. } else {
  410. #else /* SLIP_RX_QUEUE */
  411. pbuf_free(priv->rxpackets);
  412. }
  413. {
  414. #endif /* SLIP_RX_QUEUE */
  415. priv->rxpackets = p;
  416. }
  417. SYS_ARCH_UNPROTECT(old_level);
  418. }
  419. }
  420. /**
  421. * Process a received byte, completed packets are put on a queue that is
  422. * fed into IP through slipif_process_rxqueue().
  423. *
  424. * This function can be called from ISR if SYS_LIGHTWEIGHT_PROT is enabled.
  425. *
  426. * @param netif The lwip network interface structure for this slipif
  427. * @param data received character
  428. */
  429. void
  430. slipif_received_byte(struct netif *netif, u8_t data)
  431. {
  432. LWIP_ASSERT("netif != NULL", (netif != NULL));
  433. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  434. slipif_rxbyte_enqueue(netif, data);
  435. }
  436. /**
  437. * Process multiple received byte, completed packets are put on a queue that is
  438. * fed into IP through slipif_process_rxqueue().
  439. *
  440. * This function can be called from ISR if SYS_LIGHTWEIGHT_PROT is enabled.
  441. *
  442. * @param netif The lwip network interface structure for this slipif
  443. * @param data received character
  444. * @param len Number of received characters
  445. */
  446. void
  447. slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len)
  448. {
  449. u8_t i;
  450. u8_t *rxdata = data;
  451. LWIP_ASSERT("netif != NULL", (netif != NULL));
  452. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  453. for (i = 0; i < len; i++, rxdata++) {
  454. slipif_rxbyte_enqueue(netif, *rxdata);
  455. }
  456. }
  457. #endif /* SLIP_RX_FROM_ISR */
  458. #endif /* LWIP_HAVE_SLIPIF */