The Pedigree Project  0.1
slipif.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
26 /*
27  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
28  * All rights reserved.
29  *
30  * Redistribution and use in source and binary forms, with or without
31  * modification, are permitted provided that the following conditions
32  * are met:
33  * 1. Redistributions of source code must retain the above copyright
34  * notice, this list of conditions and the following disclaimer.
35  * 2. Redistributions in binary form must reproduce the above copyright
36  * notice, this list of conditions and the following disclaimer in the
37  * documentation and/or other materials provided with the distribution.
38  * 3. Neither the name of the Institute nor the names of its contributors
39  * may be used to endorse or promote products derived from this software
40  * without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
55  *
56  * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
57  * Simon Goldschmidt
58  */
59 
60 
81 #include "netif/slipif.h"
82 #include "lwip/opt.h"
83 
84 #include "lwip/def.h"
85 #include "lwip/pbuf.h"
86 #include "lwip/stats.h"
87 #include "lwip/snmp.h"
88 #include "lwip/sys.h"
89 #include "lwip/sio.h"
90 
91 #define SLIP_END 0xC0 /* 0300: start and end of every packet */
92 #define SLIP_ESC 0xDB /* 0333: escape start (one byte escaped data follows) */
93 #define SLIP_ESC_END 0xDC /* 0334: following escape: original byte is 0xC0 (END) */
94 #define SLIP_ESC_ESC 0xDD /* 0335: following escape: original byte is 0xDB (ESC) */
95 
97 #ifndef SLIP_MAX_SIZE
98 #define SLIP_MAX_SIZE 1500
99 #endif
100 
105 #ifndef SLIP_SIO_SPEED
106 #define SLIP_SIO_SPEED(sio_fd) 0
107 #endif
108 
109 enum slipif_recv_state {
110  SLIP_RECV_NORMAL,
111  SLIP_RECV_ESCAPE
112 };
113 
114 struct slipif_priv {
115  sio_fd_t sd;
116  /* q is the whole pbuf chain for a packet, p is the current pbuf in the chain */
117  struct pbuf *p, *q;
118  u8_t state;
119  u16_t i, recved;
120 #if SLIP_RX_FROM_ISR
121  struct pbuf *rxpackets;
122 #endif
123 };
124 
134 static err_t
135 slipif_output(struct netif *netif, struct pbuf *p)
136 {
137  struct slipif_priv *priv;
138  struct pbuf *q;
139  u16_t i;
140  u8_t c;
141 
142  LWIP_ASSERT("netif != NULL", (netif != NULL));
143  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
144  LWIP_ASSERT("p != NULL", (p != NULL));
145 
146  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output(%"U16_F"): sending %"U16_F" bytes\n", (u16_t)netif->num, p->tot_len));
147  priv = (struct slipif_priv *)netif->state;
148 
149  /* Send pbuf out on the serial I/O device. */
150  /* Start with packet delimiter. */
151  sio_send(SLIP_END, priv->sd);
152 
153  for (q = p; q != NULL; q = q->next) {
154  for (i = 0; i < q->len; i++) {
155  c = ((u8_t *)q->payload)[i];
156  switch (c) {
157  case SLIP_END:
158  /* need to escape this byte (0xC0 -> 0xDB, 0xDC) */
159  sio_send(SLIP_ESC, priv->sd);
160  sio_send(SLIP_ESC_END, priv->sd);
161  break;
162  case SLIP_ESC:
163  /* need to escape this byte (0xDB -> 0xDB, 0xDD) */
164  sio_send(SLIP_ESC, priv->sd);
165  sio_send(SLIP_ESC_ESC, priv->sd);
166  break;
167  default:
168  /* normal byte - no need for escaping */
169  sio_send(c, priv->sd);
170  break;
171  }
172  }
173  }
174  /* End with packet delimiter. */
175  sio_send(SLIP_END, priv->sd);
176  return ERR_OK;
177 }
178 
179 #if LWIP_IPV4
180 
190 static err_t
191 slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
192 {
193  LWIP_UNUSED_ARG(ipaddr);
194  return slipif_output(netif, p);
195 }
196 #endif /* LWIP_IPV4 */
197 
198 #if LWIP_IPV6
199 
209 static err_t
210 slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
211 {
212  LWIP_UNUSED_ARG(ipaddr);
213  return slipif_output(netif, p);
214 }
215 #endif /* LWIP_IPV6 */
216 
225 static struct pbuf*
226 slipif_rxbyte(struct netif *netif, u8_t c)
227 {
228  struct slipif_priv *priv;
229  struct pbuf *t;
230 
231  LWIP_ASSERT("netif != NULL", (netif != NULL));
232  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
233 
234  priv = (struct slipif_priv *)netif->state;
235 
236  switch (priv->state) {
237  case SLIP_RECV_NORMAL:
238  switch (c) {
239  case SLIP_END:
240  if (priv->recved > 0) {
241  /* Received whole packet. */
242  /* Trim the pbuf to the size of the received packet. */
243  pbuf_realloc(priv->q, priv->recved);
244 
245  LINK_STATS_INC(link.recv);
246 
247  LWIP_DEBUGF(SLIP_DEBUG, ("slipif: Got packet (%"U16_F" bytes)\n", priv->recved));
248  t = priv->q;
249  priv->p = priv->q = NULL;
250  priv->i = priv->recved = 0;
251  return t;
252  }
253  return NULL;
254  case SLIP_ESC:
255  priv->state = SLIP_RECV_ESCAPE;
256  return NULL;
257  default:
258  break;
259  } /* end switch (c) */
260  break;
261  case SLIP_RECV_ESCAPE:
262  /* un-escape END or ESC bytes, leave other bytes
263  (although that would be a protocol error) */
264  switch (c) {
265  case SLIP_ESC_END:
266  c = SLIP_END;
267  break;
268  case SLIP_ESC_ESC:
269  c = SLIP_ESC;
270  break;
271  default:
272  break;
273  }
274  priv->state = SLIP_RECV_NORMAL;
275  break;
276  default:
277  break;
278  } /* end switch (priv->state) */
279 
280  /* byte received, packet not yet completely received */
281  if (priv->p == NULL) {
282  /* allocate a new pbuf */
283  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
285 
286  if (priv->p == NULL) {
287  LINK_STATS_INC(link.drop);
288  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
289  /* don't process any further since we got no pbuf to receive to */
290  return NULL;
291  }
292 
293  if (priv->q != NULL) {
294  /* 'chain' the pbuf to the existing chain */
295  pbuf_cat(priv->q, priv->p);
296  } else {
297  /* p is the first pbuf in the chain */
298  priv->q = priv->p;
299  }
300  }
301 
302  /* this automatically drops bytes if > SLIP_MAX_SIZE */
303  if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
304  ((u8_t *)priv->p->payload)[priv->i] = c;
305  priv->recved++;
306  priv->i++;
307  if (priv->i >= priv->p->len) {
308  /* on to the next pbuf */
309  priv->i = 0;
310  if (priv->p->next != NULL && priv->p->next->len > 0) {
311  /* p is a chain, on to the next in the chain */
312  priv->p = priv->p->next;
313  } else {
314  /* p is a single pbuf, set it to NULL so next time a new
315  * pbuf is allocated */
316  priv->p = NULL;
317  }
318  }
319  }
320  return NULL;
321 }
322 
328 static void
329 slipif_rxbyte_input(struct netif *netif, u8_t c)
330 {
331  struct pbuf *p;
332  p = slipif_rxbyte(netif, c);
333  if (p != NULL) {
334  if (netif->input(p, netif) != ERR_OK) {
335  pbuf_free(p);
336  }
337  }
338 }
339 
340 #if SLIP_USE_RX_THREAD
341 
348 static void
349 slipif_loop_thread(void *nf)
350 {
351  u8_t c;
352  struct netif *netif = (struct netif *)nf;
353  struct slipif_priv *priv = (struct slipif_priv *)netif->state;
354 
355  while (1) {
356  if (sio_read(priv->sd, &c, 1) > 0) {
357  slipif_rxbyte_input(netif, c);
358  }
359  }
360 }
361 #endif /* SLIP_USE_RX_THREAD */
362 
379 err_t
380 slipif_init(struct netif *netif)
381 {
382  struct slipif_priv *priv;
383  u8_t sio_num;
384 
385  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
386 
387  /* Allocate private data */
388  priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv));
389  if (!priv) {
390  return ERR_MEM;
391  }
392 
393  netif->name[0] = 's';
394  netif->name[1] = 'l';
395 #if LWIP_IPV4
396  netif->output = slipif_output_v4;
397 #endif /* LWIP_IPV4 */
398 #if LWIP_IPV6
399  netif->output_ip6 = slipif_output_v6;
400 #endif /* LWIP_IPV6 */
401  netif->mtu = SLIP_MAX_SIZE;
402 
403  /* netif->state or netif->num contain the port number */
404  if (netif->state != NULL) {
405  sio_num = *(u8_t*)netif->state;
406  } else {
407  sio_num = netif->num;
408  }
409  /* Try to open the serial port. */
410  priv->sd = sio_open(sio_num);
411  if (!priv->sd) {
412  /* Opening the serial port failed. */
413  mem_free(priv);
414  return ERR_IF;
415  }
416 
417  /* Initialize private data */
418  priv->p = NULL;
419  priv->q = NULL;
420  priv->state = SLIP_RECV_NORMAL;
421  priv->i = 0;
422  priv->recved = 0;
423 #if SLIP_RX_FROM_ISR
424  priv->rxpackets = NULL;
425 #endif
426 
427  netif->state = priv;
428 
429  /* initialize the snmp variables and counters inside the struct netif */
430  MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd));
431 
432 #if SLIP_USE_RX_THREAD
433  /* Create a thread to poll the serial line. */
434  sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
436 #endif /* SLIP_USE_RX_THREAD */
437  return ERR_OK;
438 }
439 
445 void
446 slipif_poll(struct netif *netif)
447 {
448  u8_t c;
449  struct slipif_priv *priv;
450 
451  LWIP_ASSERT("netif != NULL", (netif != NULL));
452  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
453 
454  priv = (struct slipif_priv *)netif->state;
455 
456  while (sio_tryread(priv->sd, &c, 1) > 0) {
457  slipif_rxbyte_input(netif, c);
458  }
459 }
460 
461 #if SLIP_RX_FROM_ISR
462 
467 void
468 slipif_process_rxqueue(struct netif *netif)
469 {
470  struct slipif_priv *priv;
471  SYS_ARCH_DECL_PROTECT(old_level);
472 
473  LWIP_ASSERT("netif != NULL", (netif != NULL));
474  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
475 
476  priv = (struct slipif_priv *)netif->state;
477 
478  SYS_ARCH_PROTECT(old_level);
479  while (priv->rxpackets != NULL) {
480  struct pbuf *p = priv->rxpackets;
481 #if SLIP_RX_QUEUE
482  /* dequeue packet */
483  struct pbuf *q = p;
484  while ((q->len != q->tot_len) && (q->next != NULL)) {
485  q = q->next;
486  }
487  priv->rxpackets = q->next;
488  q->next = NULL;
489 #else /* SLIP_RX_QUEUE */
490  priv->rxpackets = NULL;
491 #endif /* SLIP_RX_QUEUE */
492  SYS_ARCH_UNPROTECT(old_level);
493  if (netif->input(p, netif) != ERR_OK) {
494  pbuf_free(p);
495  }
496  SYS_ARCH_PROTECT(old_level);
497  }
498 }
499 
505 static void
506 slipif_rxbyte_enqueue(struct netif *netif, u8_t data)
507 {
508  struct pbuf *p;
509  struct slipif_priv *priv = (struct slipif_priv *)netif->state;
510  SYS_ARCH_DECL_PROTECT(old_level);
511 
512  p = slipif_rxbyte(netif, data);
513  if (p != NULL) {
514  SYS_ARCH_PROTECT(old_level);
515  if (priv->rxpackets != NULL) {
516 #if SLIP_RX_QUEUE
517  /* queue multiple pbufs */
518  struct pbuf *q = p;
519  while (q->next != NULL) {
520  q = q->next;
521  }
522  q->next = p;
523  } else {
524 #else /* SLIP_RX_QUEUE */
525  pbuf_free(priv->rxpackets);
526  }
527  {
528 #endif /* SLIP_RX_QUEUE */
529  priv->rxpackets = p;
530  }
531  SYS_ARCH_UNPROTECT(old_level);
532  }
533 }
534 
544 void
545 slipif_received_byte(struct netif *netif, u8_t data)
546 {
547  LWIP_ASSERT("netif != NULL", (netif != NULL));
548  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
549  slipif_rxbyte_enqueue(netif, data);
550 }
551 
562 void
563 slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len)
564 {
565  u8_t i;
566  u8_t *rxdata = data;
567  LWIP_ASSERT("netif != NULL", (netif != NULL));
568  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
569 
570  for (i = 0; i < len; i++, rxdata++) {
571  slipif_rxbyte_enqueue(netif, *rxdata);
572  }
573 }
574 #endif /* SLIP_RX_FROM_ISR */
u16_t tot_len
Definition: pbuf.h:175
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition: pbuf.c:512
struct pbuf * next
Definition: pbuf.h:163
u16_t len
Definition: pbuf.h:178
void mem_free(void *rmem)
Definition: mem.c:438
u8_t num
Definition: netif.h:328
#define SLIP_DEBUG
Definition: opt.h:2840
#define PBUF_LINK_HLEN
Definition: opt.h:1374
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:420
#define SLIPIF_THREAD_STACKSIZE
Definition: opt.h:1609
Definition: err.h:84
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
Definition: sys_arch.cc:83
#define SLIP_SIO_SPEED(sio_fd)
Definition: slipif.c:106
Definition: pbuf.h:161
Definition: netif.h:244
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:859
err_t slipif_init(struct netif *netif)
Definition: slipif.c:380
Definition: err.h:106
s8_t err_t
Definition: err.h:76
void slipif_poll(struct netif *netif)
Definition: slipif.c:446
u16_t mtu
Definition: netif.h:318
#define LWIP_DEBUGF(debug, message)
#define PBUF_POOL_BUFSIZE
Definition: opt.h:1392
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:267
#define SLIPIF_THREAD_NAME
Definition: opt.h:1600
void * state
Definition: netif.h:298
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:327
netif_input_fn input
Definition: netif.h:263
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:734
Definition: err.h:82
void * payload
Definition: pbuf.h:166
char name[2]
Definition: netif.h:326
void * mem_malloc(mem_size_t size)
Definition: mem.c:622
#define SLIPIF_THREAD_PRIO
Definition: opt.h:1618
#define PBUF_LINK_ENCAPSULATION_HLEN
Definition: opt.h:1383
#define SLIP_MAX_SIZE
Definition: slipif.c:98