8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
35 * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
37 * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
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!).
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"
64 #include "lwip/pbuf.h"
65 #include "lwip/stats.h"
66 #include "lwip/snmp.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 */
77 #define SLIP_MAX_SIZE 1500
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
88 enum slipif_recv_state
{
95 /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
100 struct pbuf
*rxpackets
;
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
114 slipif_output(struct netif
*netif
, struct pbuf
*p
)
116 struct slipif_priv
*priv
;
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
));
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
];
137 /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */
138 sio_send(SLIP_ESC
, priv
->sd
);
139 sio_send(SLIP_ESC_END
, priv
->sd
);
142 /* need to escape this byte (0xDB -> 0xDB, 0xDD) */
143 sio_send(SLIP_ESC
, priv
->sd
);
144 sio_send(SLIP_ESC_ESC
, priv
->sd
);
147 /* normal byte - no need for escaping */
148 sio_send(c
, priv
->sd
);
153 /* End with packet delimiter. */
154 sio_send(SLIP_END
, priv
->sd
);
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
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
);
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
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
203 slipif_rxbyte(struct netif
*netif
, u8_t c
)
205 struct slipif_priv
*priv
;
208 LWIP_ASSERT("netif != NULL", (netif
!= NULL
));
209 LWIP_ASSERT("netif->state != NULL", (netif
->state
!= NULL
));
213 switch (priv
->state
) {
214 case SLIP_RECV_NORMAL
:
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
));
226 priv
->p
= priv
->q
= NULL
;
227 priv
->i
= priv
->recved
= 0;
232 priv
->state
= SLIP_RECV_ESCAPE
;
234 } /* end switch (c) */
236 case SLIP_RECV_ESCAPE
:
237 /* un-escape END or ESC bytes, leave other bytes
238 (although that would be a protocol error) */
247 priv
->state
= SLIP_RECV_NORMAL
;
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 */
264 if (priv
->q
!= NULL
) {
265 /* 'chain' the pbuf to the existing chain */
266 pbuf_cat(priv
->q
, priv
->p
);
268 /* p is the first pbuf in the chain */
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
;
278 if (priv
->i
>= priv
->p
->len
) {
279 /* on to the next pbuf */
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
;
285 /* p is a single pbuf, set it to NULL so next time a new
286 * pbuf is allocated */
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
300 slipif_rxbyte_input(struct netif
*netif
, u8_t c
)
303 p
= slipif_rxbyte(netif
, c
);
305 if (netif
->input(p
, netif
) != ERR_OK
) {
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
320 slipif_loop_thread(void *nf
)
323 struct netif
*netif
= (struct netif
*)nf
;
324 struct slipif_priv
*priv
= (struct slipif_priv
*)netif
->state
;
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.
351 slipif_init(struct netif
*netif
)
353 struct slipif_priv
*priv
;
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
));
364 netif
->name
[0] = 's';
365 netif
->name
[1] = 'l';
366 netif
->output
= slipif_output_v4
;
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
;
377 sio_num
= netif
->num
;
379 /* Try to open the serial port. */
380 priv
->sd
= sio_open(sio_num
);
382 /* Opening the serial port failed. */
387 /* Initialize private data */
390 priv
->state
= SLIP_RECV_NORMAL
;
394 priv
->rxpackets
= NULL
;
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 */
411 * Polls the serial device and feeds the IP layer with incoming packets.
413 * @param netif The lwip network interface structure for this slipif
416 slipif_poll(struct netif
*netif
)
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
);
433 * Feeds the IP layer with incoming packets that were receive
435 * @param netif The lwip network interface structure for this slipif
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
;
454 while ((q
->len
!= q
->tot_len
) && (q
->next
!= NULL
)) {
457 priv
->rxpackets
= q
->next
;
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
) {
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
476 slipif_rxbyte_enqueue(struct netif
*netif
, u8_t data
)
479 struct slipif_priv
*priv
= (struct slipif_priv
*)netif
->state
;
480 SYS_ARCH_DECL_PROTECT(old_level
);
482 p
= slipif_rxbyte(netif
, data
);
484 SYS_ARCH_PROTECT(old_level
);
485 if (priv
->rxpackets
!= NULL
) {
487 /* queue multiple pbufs */
489 while(q
->next
!= NULL
) {
494 #else /* SLIP_RX_QUEUE */
495 pbuf_free(priv
->rxpackets
);
498 #endif /* SLIP_RX_QUEUE */
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
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
533 slipif_received_bytes(struct netif
*netif
, u8_t
*data
, u8_t len
)
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 */