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