STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
slipif.c
Go to the documentation of this file.
1 
7 /*
8  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9  * All rights reserved.
10  *
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.
22  *
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.
34  *
35  * This file is built upon the file: src/arch/rtxc/netif/sioslip.c
36  *
37  * Author: Magnus Ivarsson <magnus.ivarsson(at)volvo.com>
38  * Simon Goldschmidt
39  *
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!).
50  *
51  */
52 
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
56  */
57 
58 #include "netif/slipif.h"
59 #include "lwip/opt.h"
60 
61 #if LWIP_HAVE_SLIPIF
62 
63 #include "lwip/def.h"
64 #include "lwip/pbuf.h"
65 #include "lwip/stats.h"
66 #include "lwip/snmp.h"
67 #include "lwip/sys.h"
68 #include "lwip/sio.h"
69 
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) */
74 
76 #ifndef SLIP_MAX_SIZE
77 #define SLIP_MAX_SIZE 1500
78 #endif
79 
84 #ifndef SLIP_SIO_SPEED
85 #define SLIP_SIO_SPEED(sio_fd) 0
86 #endif
87 
88 enum slipif_recv_state {
89  SLIP_RECV_NORMAL,
90  SLIP_RECV_ESCAPE
91 };
92 
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
102 };
103 
113 static err_t
114 slipif_output(struct netif *netif, struct pbuf *p)
115 {
116  struct slipif_priv *priv;
117  struct pbuf *q;
118  u16_t i;
119  u8_t c;
120 
121  LWIP_ASSERT("netif != NULL", (netif != NULL));
122  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
123  LWIP_ASSERT("p != NULL", (p != NULL));
124 
125  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_output(%"U16_F"): sending %"U16_F" bytes\n", (u16_t)netif->num, p->tot_len));
126  priv = (struct slipif_priv *)netif->state;
127 
128  /* Send pbuf out on the serial I/O device. */
129  /* Start with packet delimiter. */
130  sio_send(SLIP_END, priv->sd);
131 
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;
150  }
151  }
152  }
153  /* End with packet delimiter. */
154  sio_send(SLIP_END, priv->sd);
155  return ERR_OK;
156 }
157 
168 static err_t
169 slipif_output_v4(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
170 {
171  LWIP_UNUSED_ARG(ipaddr);
172  return slipif_output(netif, p);
173 }
174 
175 #if LWIP_IPV6
176 
186 static err_t
187 slipif_output_v6(struct netif *netif, struct pbuf *p, const ip6_addr_t *ipaddr)
188 {
189  LWIP_UNUSED_ARG(ipaddr);
190  return slipif_output(netif, p);
191 }
192 #endif /* LWIP_IPV6 */
193 
202 static struct pbuf*
203 slipif_rxbyte(struct netif *netif, u8_t c)
204 {
205  struct slipif_priv *priv;
206  struct pbuf *t;
207 
208  LWIP_ASSERT("netif != NULL", (netif != NULL));
209  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
210 
211  priv = (struct slipif_priv *)netif->state;
212 
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);
221 
222  LINK_STATS_INC(link.recv);
223 
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;
229  }
230  return NULL;
231  case SLIP_ESC:
232  priv->state = SLIP_RECV_ESCAPE;
233  return NULL;
234  default:
235  break;
236  } /* end switch (c) */
237  break;
238  case SLIP_RECV_ESCAPE:
239  /* un-escape END or ESC bytes, leave other bytes
240  (although that would be a protocol error) */
241  switch (c) {
242  case SLIP_ESC_END:
243  c = SLIP_END;
244  break;
245  case SLIP_ESC_ESC:
246  c = SLIP_ESC;
247  break;
248  default:
249  break;
250  }
251  priv->state = SLIP_RECV_NORMAL;
252  break;
253  default:
254  break;
255  } /* end switch (priv->state) */
256 
257  /* byte received, packet not yet completely received */
258  if (priv->p == NULL) {
259  /* allocate a new pbuf */
260  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: alloc\n"));
262 
263  if (priv->p == NULL) {
264  LINK_STATS_INC(link.drop);
265  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_input: no new pbuf! (DROP)\n"));
266  /* don't process any further since we got no pbuf to receive to */
267  return NULL;
268  }
269 
270  if (priv->q != NULL) {
271  /* 'chain' the pbuf to the existing chain */
272  pbuf_cat(priv->q, priv->p);
273  } else {
274  /* p is the first pbuf in the chain */
275  priv->q = priv->p;
276  }
277  }
278 
279  /* this automatically drops bytes if > SLIP_MAX_SIZE */
280  if ((priv->p != NULL) && (priv->recved <= SLIP_MAX_SIZE)) {
281  ((u8_t *)priv->p->payload)[priv->i] = c;
282  priv->recved++;
283  priv->i++;
284  if (priv->i >= priv->p->len) {
285  /* on to the next pbuf */
286  priv->i = 0;
287  if (priv->p->next != NULL && priv->p->next->len > 0) {
288  /* p is a chain, on to the next in the chain */
289  priv->p = priv->p->next;
290  } else {
291  /* p is a single pbuf, set it to NULL so next time a new
292  * pbuf is allocated */
293  priv->p = NULL;
294  }
295  }
296  }
297  return NULL;
298 }
299 
305 static void
306 slipif_rxbyte_input(struct netif *netif, u8_t c)
307 {
308  struct pbuf *p;
309  p = slipif_rxbyte(netif, c);
310  if (p != NULL) {
311  if (netif->input(p, netif) != ERR_OK) {
312  pbuf_free(p);
313  }
314  }
315 }
316 
317 #if SLIP_USE_RX_THREAD
318 
325 static void
326 slipif_loop_thread(void *nf)
327 {
328  u8_t c;
329  struct netif *netif = (struct netif *)nf;
330  struct slipif_priv *priv = (struct slipif_priv *)netif->state;
331 
332  while (1) {
333  if (sio_read(priv->sd, &c, 1) > 0) {
334  slipif_rxbyte_input(netif, c);
335  }
336  }
337 }
338 #endif /* SLIP_USE_RX_THREAD */
339 
356 err_t
357 slipif_init(struct netif *netif)
358 {
359  struct slipif_priv *priv;
360  u8_t sio_num;
361 
362  LWIP_DEBUGF(SLIP_DEBUG, ("slipif_init: netif->num=%"U16_F"\n", (u16_t)netif->num));
363 
364  /* Allocate private data */
365  priv = (struct slipif_priv *)mem_malloc(sizeof(struct slipif_priv));
366  if (!priv) {
367  return ERR_MEM;
368  }
369 
370  netif->name[0] = 's';
371  netif->name[1] = 'l';
372  netif->output = slipif_output_v4;
373 #if LWIP_IPV6
374  netif->output_ip6 = slipif_output_v6;
375 #endif /* LWIP_IPV6 */
376  netif->mtu = SLIP_MAX_SIZE;
377 
378  /* netif->state or netif->num contain the port number */
379  if (netif->state != NULL) {
380  sio_num = *(u8_t*)netif->state;
381  } else {
382  sio_num = netif->num;
383  }
384  /* Try to open the serial port. */
385  priv->sd = sio_open(sio_num);
386  if (!priv->sd) {
387  /* Opening the serial port failed. */
388  mem_free(priv);
389  return ERR_IF;
390  }
391 
392  /* Initialize private data */
393  priv->p = NULL;
394  priv->q = NULL;
395  priv->state = SLIP_RECV_NORMAL;
396  priv->i = 0;
397  priv->recved = 0;
398 #if SLIP_RX_FROM_ISR
399  priv->rxpackets = NULL;
400 #endif
401 
402  netif->state = priv;
403 
404  /* initialize the snmp variables and counters inside the struct netif */
405  MIB2_INIT_NETIF(netif, snmp_ifType_slip, SLIP_SIO_SPEED(priv->sd));
406 
407 #if SLIP_USE_RX_THREAD
408  /* Create a thread to poll the serial line. */
409  sys_thread_new(SLIPIF_THREAD_NAME, slipif_loop_thread, netif,
411 #endif /* SLIP_USE_RX_THREAD */
412  return ERR_OK;
413 }
414 
420 void
421 slipif_poll(struct netif *netif)
422 {
423  u8_t c;
424  struct slipif_priv *priv;
425 
426  LWIP_ASSERT("netif != NULL", (netif != NULL));
427  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
428 
429  priv = (struct slipif_priv *)netif->state;
430 
431  while (sio_tryread(priv->sd, &c, 1) > 0) {
432  slipif_rxbyte_input(netif, c);
433  }
434 }
435 
436 #if SLIP_RX_FROM_ISR
437 
442 void
443 slipif_process_rxqueue(struct netif *netif)
444 {
445  struct slipif_priv *priv;
446  SYS_ARCH_DECL_PROTECT(old_level);
447 
448  LWIP_ASSERT("netif != NULL", (netif != NULL));
449  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
450 
451  priv = (struct slipif_priv *)netif->state;
452 
453  SYS_ARCH_PROTECT(old_level);
454  while (priv->rxpackets != NULL) {
455  struct pbuf *p = priv->rxpackets;
456 #if SLIP_RX_QUEUE
457  /* dequeue packet */
458  struct pbuf *q = p;
459  while ((q->len != q->tot_len) && (q->next != NULL)) {
460  q = q->next;
461  }
462  priv->rxpackets = q->next;
463  q->next = NULL;
464 #else /* SLIP_RX_QUEUE */
465  priv->rxpackets = NULL;
466 #endif /* SLIP_RX_QUEUE */
467  SYS_ARCH_UNPROTECT(old_level);
468  if (netif->input(p, netif) != ERR_OK) {
469  pbuf_free(p);
470  }
471  SYS_ARCH_PROTECT(old_level);
472  }
473 }
474 
480 static void
481 slipif_rxbyte_enqueue(struct netif *netif, u8_t data)
482 {
483  struct pbuf *p;
484  struct slipif_priv *priv = (struct slipif_priv *)netif->state;
485  SYS_ARCH_DECL_PROTECT(old_level);
486 
487  p = slipif_rxbyte(netif, data);
488  if (p != NULL) {
489  SYS_ARCH_PROTECT(old_level);
490  if (priv->rxpackets != NULL) {
491 #if SLIP_RX_QUEUE
492  /* queue multiple pbufs */
493  struct pbuf *q = p;
494  while (q->next != NULL) {
495  q = q->next;
496  }
497  q->next = p;
498  } else {
499 #else /* SLIP_RX_QUEUE */
500  pbuf_free(priv->rxpackets);
501  }
502  {
503 #endif /* SLIP_RX_QUEUE */
504  priv->rxpackets = p;
505  }
506  SYS_ARCH_UNPROTECT(old_level);
507  }
508 }
509 
519 void
520 slipif_received_byte(struct netif *netif, u8_t data)
521 {
522  LWIP_ASSERT("netif != NULL", (netif != NULL));
523  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
524  slipif_rxbyte_enqueue(netif, data);
525 }
526 
537 void
538 slipif_received_bytes(struct netif *netif, u8_t *data, u8_t len)
539 {
540  u8_t i;
541  u8_t *rxdata = data;
542  LWIP_ASSERT("netif != NULL", (netif != NULL));
543  LWIP_ASSERT("netif->state != NULL", (netif->state != NULL));
544 
545  for (i = 0; i < len; i++, rxdata++) {
546  slipif_rxbyte_enqueue(netif, *rxdata);
547  }
548 }
549 #endif /* SLIP_RX_FROM_ISR */
550 
551 #endif /* LWIP_HAVE_SLIPIF */
552 
#define SLIPIF_THREAD_STACKSIZE
Definition: opt.h:1391
sys_thread_t sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize, int prio)
Definition: sys_arch.c:391
err_t slipif_init(struct netif *netif)
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
#define SLIP_DEBUG
Definition: opt.h:2971
void * state
Definition: netif.h:234
void mem_free(void *rmem)
Definition: mem.c:334
#define U16_F
Definition: cc.h:48
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
void pbuf_realloc(struct pbuf *p, u16_t new_len)
Definition: pbuf.c:431
netif_input_fn input
Definition: netif.h:201
u16_t mtu
Definition: netif.h:263
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
#define PBUF_POOL_BUFSIZE
Definition: lwipopts.h:105
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:304
Definition: pbuf.h:66
char name[2]
Definition: netif.h:271
void * sio_fd_t
Definition: sio.h:47
void sio_send(u8_t c, sio_fd_t fd)
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
u8_t num
Definition: netif.h:273
#define PBUF_LINK_ENCAPSULATION_HLEN
Definition: opt.h:1193
#define ERR_OK
Definition: err.h:52
#define ERR_IF
Definition: err.h:73
u16_t tot_len
Definition: pbuf.h:122
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
Definition: pbuf.h:90
#define LINK_STATS_INC(x)
Definition: stats.h:318
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
#define SLIPIF_THREAD_PRIO
Definition: opt.h:1400
Definition: netif.h:182
struct pbuf * next
Definition: pbuf.h:110
sio_fd_t sio_open(u8_t devnum)
void slipif_poll(struct netif *netif)
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
unsigned char u8_t
Definition: cc.h:38
#define SYS_ARCH_PROTECT(lev)
Definition: sys.h:305
#define SYS_ARCH_UNPROTECT(lev)
Definition: sys.h:306
#define SLIPIF_THREAD_NAME
Definition: opt.h:1382
#define MIB2_INIT_NETIF(netif, type, speed)
Definition: snmp.h:120
void * mem_malloc(mem_size_t size)
Definition: mem.c:517
#define PBUF_LINK_HLEN
Definition: opt.h:1184
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113