slipif.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. */
  38. /*
  39. * This is an arch independent SLIP netif. The specific serial hooks must be
  40. * provided by another file. They are sio_open, sio_read/sio_tryread and sio_send
  41. */
  42. #include "netif/slipif.h"
  43. #include "lwip/opt.h"
  44. #if LWIP_HAVE_SLIPIF
  45. #include "lwip/def.h"
  46. #include "lwip/pbuf.h"
  47. #include "lwip/sys.h"
  48. #include "lwip/stats.h"
  49. #include "lwip/snmp.h"
  50. #include "lwip/sio.h"
  51. #define SLIP_BLOCK 1
  52. #define SLIP_DONTBLOCK 0
  53. #define SLIP_END 0300 /* 0xC0 */
  54. #define SLIP_ESC 0333 /* 0xDB */
  55. #define SLIP_ESC_END 0334 /* 0xDC */
  56. #define SLIP_ESC_ESC 0335 /* 0xDD */
  57. #define SLIP_MAX_SIZE 1500
  58. enum slipif_recv_state {
  59. SLIP_RECV_NORMAL,
  60. SLIP_RECV_ESCAPE,
  61. };
  62. struct slipif_priv {
  63. sio_fd_t sd;
  64. /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
  65. struct pbuf *p, *q;
  66. enum slipif_recv_state state;
  67. u16_t i, recved;
  68. };
  69. /**
  70. * Send a pbuf doing the necessary SLIP encapsulation
  71. *
  72. * Uses the serial layer's sio_send()
  73. *
  74. * @param netif the lwip network interface structure for this slipif
  75. * @param p the pbuf chaing packet to send
  76. * @param ipaddr the ip address to send the packet to (not used for slipif)
  77. * @return always returns ERR_OK since the serial layer does not provide return values
  78. */
  79. err_t
  80. slipif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
  81. {
  82. struct slipif_priv *priv;
  83. struct pbuf *q;
  84. u16_t i;
  85. u8_t c;
  86. LWIP_ASSERT("netif != NULL", (netif != NULL));
  87. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  88. LWIP_ASSERT("p != NULL", (p != NULL));
  89. LWIP_UNUSED_ARG(ipaddr);
  90. priv = netif->state;
  91. /* Send pbuf out on the serial I/O device. */
  92. sio_send(SLIP_END, priv->sd);
  93. for (q = p; q != NULL; q = q->next) {
  94. for (i = 0; i < q->len; i++) {
  95. c = ((u8_t *)q->payload)[i];
  96. switch (c) {
  97. case SLIP_END:
  98. sio_send(SLIP_ESC, priv->sd);
  99. sio_send(SLIP_ESC_END, priv->sd);
  100. break;
  101. case SLIP_ESC:
  102. sio_send(SLIP_ESC, priv->sd);
  103. sio_send(SLIP_ESC_ESC, priv->sd);
  104. break;
  105. default:
  106. sio_send(c, priv->sd);
  107. break;
  108. }
  109. }
  110. }
  111. sio_send(SLIP_END, priv->sd);
  112. return ERR_OK;
  113. }
  114. /**
  115. * Static function for easy use of blockig or non-blocking
  116. * sio_read
  117. *
  118. * @param fd serial device handle
  119. * @param data pointer to data buffer for receiving
  120. * @param len maximum length (in bytes) of data to receive
  121. * @param block if 1, call sio_read; if 0, call sio_tryread
  122. * @return return value of sio_read of sio_tryread
  123. */
  124. static u32_t
  125. slip_sio_read(sio_fd_t fd, u8_t* data, u32_t len, u8_t block)
  126. {
  127. if (block) {
  128. return sio_read(fd, data, len);
  129. } else {
  130. return sio_tryread(fd, data, len);
  131. }
  132. }
  133. /**
  134. * Handle the incoming SLIP stream character by character
  135. *
  136. * Poll the serial layer by calling sio_read() or sio_tryread().
  137. *
  138. * @param netif the lwip network interface structure for this slipif
  139. * @param block if 1, block until data is received; if 0, return when all data
  140. * from the buffer is received (multiple calls to this function will
  141. * return a complete packet, NULL is returned before - used for polling)
  142. * @return The IP packet when SLIP_END is received
  143. */
  144. static struct pbuf *
  145. slipif_input(struct netif *netif, u8_t block)
  146. {
  147. struct slipif_priv *priv;
  148. u8_t c;
  149. struct pbuf *t;
  150. LWIP_ASSERT("netif != NULL", (netif != NULL));
  151. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  152. priv = netif->state;
  153. while (slip_sio_read(priv->sd, &c, 1, block) > 0) {
  154. switch (priv->state) {
  155. case SLIP_RECV_NORMAL:
  156. switch (c) {
  157. case SLIP_END:
  158. if (priv->recved > 0) {
  159. /* Received whole packet. */
  160. /* Trim the pbuf to the size of the received packet. */
  161. pbuf_realloc(priv->q, priv->recved);
  162. LINK_STATS_INC(link.recv);
  163. LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet\n"));
  164. t = priv->q;
  165. priv->p = priv->q = NULL;
  166. priv->i = priv->recved = 0;
  167. return t;
  168. }
  169. continue;
  170. case SLIP_ESC:
  171. priv->state = SLIP_RECV_ESCAPE;
  172. continue;
  173. }
  174. break;
  175. case SLIP_RECV_ESCAPE:
  176. switch (c) {
  177. case SLIP_ESC_END:
  178. c = SLIP_END;
  179. break;
  180. case SLIP_ESC_ESC:
  181. c = SLIP_ESC;
  182. break;
  183. }
  184. priv->state = SLIP_RECV_NORMAL;
  185. /* FALLTHROUGH */
  186. }
  187. /* byte received, packet not yet completely received */
  188. if (priv->p == NULL) {
  189. /* allocate a new pbuf */
  190. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
  191. priv->p = pbuf_alloc(PBUF_LINK, (PBUF_POOL_BUFSIZE - PBUF_LINK_HLEN), PBUF_POOL);
  192. if (priv->p == NULL) {
  193. LINK_STATS_INC(link.drop);
  194. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
  195. /* don't process any further since we got no pbuf to receive to */
  196. break;
  197. }
  198. if (priv->q != NULL) {
  199. /* 'chain' the pbuf to the existing chain */
  200. pbuf_cat(priv->q, priv->p);
  201. } else {
  202. /* p is the first pbuf in the chain */
  203. priv->q = priv->p;
  204. }
  205. }
  206. /* this automatically drops bytes if > SLIP_MAX_SIZE */
  207. if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
  208. ((u8_t *)priv->p->payload)[priv->i] = c;
  209. priv->recved++;
  210. priv->i++;
  211. if (priv->i >= priv->p->len) {
  212. /* on to the next pbuf */
  213. priv->i = 0;
  214. if (priv->p->next != NULL && priv->p->next->len > 0) {
  215. /* p is a chain, on to the next in the chain */
  216. priv->p = priv->p->next;
  217. } else {
  218. /* p is a single pbuf, set it to NULL so next time a new
  219. * pbuf is allocated */
  220. priv->p = NULL;
  221. }
  222. }
  223. }
  224. }
  225. return NULL;
  226. }
  227. #if !NO_SYS
  228. /**
  229. * The SLIP input thread.
  230. *
  231. * Feed the IP layer with incoming packets
  232. *
  233. * @param nf the lwip network interface structure for this slipif
  234. */
  235. static void
  236. slipif_loop_thread(void *nf)
  237. {
  238. struct pbuf *p;
  239. struct netif *netif = (struct netif *)nf;
  240. while (1) {
  241. p = slipif_input(netif, SLIP_BLOCK);
  242. if (p != NULL) {
  243. if (netif->input(p, netif) != ERR_OK) {
  244. pbuf_free(p);
  245. p = NULL;
  246. }
  247. }
  248. }
  249. }
  250. #endif /* !NO_SYS */
  251. /**
  252. * SLIP netif initialization
  253. *
  254. * Call the arch specific sio_open and remember
  255. * the opened device in the state field of the netif.
  256. *
  257. * @param netif the lwip network interface structure for this slipif
  258. * @return ERR_OK if serial line could be opened,
  259. * ERR_MEM if no memory could be allocated,
  260. * ERR_IF is serial line couldn't be opened
  261. *
  262. * @note netif->num must contain the number of the serial port to open
  263. * (0 by default)
  264. */
  265. err_t
  266. slipif_init(struct netif *netif)
  267. {
  268. struct slipif_priv *priv;
  269. LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
  270. /* Allocate private data */
  271. priv = mem_malloc(sizeof(struct slipif_priv));
  272. if (!priv) {
  273. return ERR_MEM;
  274. }
  275. netif->name[0] = 's';
  276. netif->name[1] = 'l';
  277. netif->output = slipif_output;
  278. netif->mtu = SLIP_MAX_SIZE;
  279. netif->flags |= NETIF_FLAG_POINTTOPOINT;
  280. /* Try to open the serial port (netif->num contains the port number). */
  281. priv->sd = sio_open(netif->num);
  282. if (!priv->sd) {
  283. /* Opening the serial port failed. */
  284. mem_free(priv);
  285. return ERR_IF;
  286. }
  287. /* Initialize private data */
  288. priv->p = NULL;
  289. priv->q = NULL;
  290. priv->state = SLIP_RECV_NORMAL;
  291. priv->i = 0;
  292. priv->recved = 0;
  293. netif->state = priv;
  294. /* initialize the snmp variables and counters inside the struct netif
  295. * ifSpeed: no assumption can be made without knowing more about the
  296. * serial line!
  297. */
  298. NETIF_INIT_SNMP(netif, snmp_ifType_slip, 0);
  299. /* Create a thread to poll the serial line. */
  300. sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
  301. SLIPIF_THREAD_STACKSIZE, SLIPIF_THREAD_PRIO);
  302. return ERR_OK;
  303. }
  304. /**
  305. * Polls the serial device and feeds the IP layer with incoming packets.
  306. *
  307. * @param netif The lwip network interface structure for this slipif
  308. */
  309. void
  310. slipif_poll(struct netif *netif)
  311. {
  312. struct pbuf *p;
  313. struct slipif_priv *priv;
  314. LWIP_ASSERT("netif != NULL", (netif != NULL));
  315. LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
  316. priv = netif->state;
  317. while ((p = slipif_input(netif, SLIP_DONTBLOCK)) != NULL) {
  318. if (netif->input(p, netif) != ERR_OK) {
  319. pbuf_free(p);
  320. }
  321. }
  322. }
  323. #endif /* LWIP_HAVE_SLIPIF */