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