Contiki-NG
slip.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, SICS Swedish ICT.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Alternative implementation for SLIP:
36  * 1. Accepts more than two packet
37  * 2. Disables UART rx interrupt when buffer is full
38  * (thus invoking flow control if configured)
39  * \author
40  * Niklas Finne <nfi@sics.se>
41  * Beshr Al Nahas <beshr@sics.se>
42  *
43  */
44 
45 #include "contiki.h"
46 
47 #include <MicroInt.h>
48 #include "net/ipv6/uip.h"
49 #define BUF ((struct uip_tcpip_hdr *)&uip_buf[UIP_LLH_LEN])
50 
51 #include "dev/slip.h"
52 
53 #define DEBUG 0
54 #if DEBUG
55 #include <stdio.h>
56 #define PRINTF(...) printf(__VA_ARGS__)
57 #define PUTCHAR(X) do { putchar(X); putchar('\n'); } while(0)
58 #else
59 #define PRINTF(...) do {} while(0)
60 #define PUTCHAR(X) do {} while(0)
61 #endif
62 
63 #define SLIP_END 0300
64 #define SLIP_ESC 0333
65 #define SLIP_ESC_END 0334
66 #define SLIP_ESC_ESC 0335
67 #define SLIP_NEUTRAL 0 /* means: none of the above */
68 #define SLIP_ESC_XON 0336
69 #define SLIP_ESC_XOFF 0337
70 #define XON ((unsigned char)17)
71 #define XOFF ((unsigned char)19)
72 #if UART_XONXOFF_FLOW_CTRL
73 volatile unsigned char xonxoff_state = XON;
74 #endif /* UART_XONXOFF_FLOW_CTRL */
75 
76 PROCESS(slip_process, "SLIP driver");
77 
78 #include "dev/uart0.h"
79 #define STORE_UART_INTERRUPTS uart0_store_interrupts
80 #define RESTORE_UART_INTERRUPTS uart0_restore_interrupts
81 #define DISABLE_UART_INTERRUPTS uart0_disable_interrupts
82 #define ENABLE_UART_INTERRUPTS uart0_enable_interrupts
83 
84 /**
85  * @brief A block of code may be made atomic by wrapping it with this
86  * macro. Something which is atomic cannot be interrupted by interrupts.
87  */
88 /* A specific ATMOIC that disables UART interrupts only */
89 #define ATOMIC(blah) \
90  { \
91  /* STORE_UART_INTERRUPTS(); */ \
92  DISABLE_UART_INTERRUPTS(); \
93  { blah } \
94  /* RESTORE_UART_INTERRUPTS(); */ \
95  ENABLE_UART_INTERRUPTS(); \
96  }
97 
98 /* A generic ATMOIC that disables all interrupts */
99 #define GLOBAL_ATOMIC(blah) \
100  { \
101  MICRO_DISABLE_INTERRUPTS(); \
102  { blah } \
103  MICRO_ENABLE_INTERRUPTS(); \
104  }
105 
106 #if 1
107 #define SLIP_STATISTICS(statement)
108 #else
109 uint16_t slip_drop_bytes, slip_overflow, slip_error_drop;
110 /* No used in this file */
111 uint16_t slip_rubbish, slip_twopackets, slip_ip_drop;
112 unsigned long slip_received, slip_frames;
113 #define SLIP_STATISTICS(statement) statement
114 #endif
115 
116 /* Must be at least one byte larger than UIP_BUFSIZE (for SLIP_END)! */
117 #ifdef SLIP_CONF_RX_BUFSIZE
118 #define RX_BUFSIZE SLIP_CONF_RX_BUFSIZE
119 
120 #if RX_BUFSIZE < (UIP_BUFSIZE - UIP_LLH_LEN + 16)
121 #error "SLIP_CONF_RX_BUFSIZE too small for UIP_BUFSIZE"
122 #endif
123 
124 #else
125 #define RX_BUFSIZE (UIP_CONF_BUFFER_SIZE * 2)
126 #endif
127 
128 /*
129  * Variables begin and end manage the buffer space in a cyclic
130  * fashion. The first used byte is at begin and end is one byte past
131  * the last. I.e. [begin, end) is the actively used space.
132  */
133 
134 static volatile uint16_t begin, end, end_counter;
135 static uint8_t rxbuf[RX_BUFSIZE];
136 static volatile uint8_t is_dropping = 0;
137 static volatile uint8_t is_full = 0;
138 
139 static void (*input_callback)(void) = NULL;
140 /*---------------------------------------------------------------------------*/
141 void
142 slip_set_input_callback(void (*c)(void))
143 {
144  input_callback = c;
145 }
146 static void
147 slip_write_char(uint8_t c)
148 {
149  /* Escape SLIP control characters */
150  if(c == SLIP_END) {
151  slip_arch_writeb(SLIP_ESC);
152  c = SLIP_ESC_END;
153  } else if(c == SLIP_ESC) {
154  slip_arch_writeb(SLIP_ESC);
155  c = SLIP_ESC_ESC;
156  }
157 #if UART_XONXOFF_FLOW_CTRL
158  /* Escape XON/XOFF characters */
159  else if(c == XON) {
160  slip_arch_writeb(SLIP_ESC);
161  c = SLIP_ESC_XON;
162  } else if(c == XOFF) {
163  slip_arch_writeb(SLIP_ESC);
164  c = SLIP_ESC_XOFF;
165  }
166 #endif /* UART_XONXOFF_FLOW_CTRL */
167  slip_arch_writeb(c);
168 }
169 /*---------------------------------------------------------------------------*/
170 void
171 slip_write(const void *_ptr, int len)
172 {
173  const uint8_t *ptr = _ptr;
174  uint16_t i;
175  uint8_t c;
176 
177  slip_arch_writeb(SLIP_END);
178 
179  for(i = 0; i < len; ++i) {
180  c = *ptr++;
181  slip_write_char(c);
182  }
183  slip_arch_writeb(SLIP_END);
184 }
185 /*---------------------------------------------------------------------------*/
186 /* slip_send: forward (IPv4) packets with {UIP_FW_NETIF(..., slip_send)}
187  * was used in slip-bridge.c
188  */
189 void
191 {
192  uint16_t i;
193  uint8_t *ptr;
194  uint8_t c;
195 
196  slip_arch_writeb(SLIP_END);
197 
198  ptr = &uip_buf[UIP_LLH_LEN];
199  for(i = 0; i < uip_len; ++i) {
200  c = *ptr++;
201  slip_write_char(c);
202  }
203  slip_arch_writeb(SLIP_END);
204 }
205 /*---------------------------------------------------------------------------*/
206 static void
207 rxbuf_init(void)
208 {
209  begin = end = end_counter = 0;
210  is_dropping = 0;
211 }
212 /*---------------------------------------------------------------------------*/
213 /* Upper half does the polling. */
214 static uint16_t
215 slip_poll_handler(uint8_t *outbuf, uint16_t blen)
216 {
217  uint16_t len;
218  uint16_t pos;
219  uint8_t c;
220  uint8_t state;
221 
222  if(end_counter == 0 && is_full == 0) {
223  return 0;
224  }
225  for(len = 0, pos = begin, state = c = SLIP_NEUTRAL;
226  len < blen + 1; /* +1 for SLIP_END! */
227  ) {
228 
229  c = rxbuf[pos++];
230 
231  if(pos == RX_BUFSIZE) {
232  /* Circular buffer: warp around */
233  pos = 0;
234  }
235  if(c == SLIP_END) {
236  /* End of packet */
237  break;
238  }
239  if(len >= blen) {
240  /* End of buffer with no SLIP_END
241  * ==> something wrong happened */
242  break;
243  }
244  switch(c) {
245  case SLIP_ESC:
246  state = SLIP_ESC;
247  break;
248  case SLIP_ESC_END:
249  if(state == SLIP_ESC) {
250  outbuf[len++] = SLIP_END;
251  state = SLIP_NEUTRAL;
252  } else {
253  outbuf[len++] = c;
254  } break;
255  case SLIP_ESC_ESC:
256  if(state == SLIP_ESC) {
257  outbuf[len++] = SLIP_ESC;
258  state = SLIP_NEUTRAL;
259  } else {
260  outbuf[len++] = c;
261  } break;
262 #if UART_XONXOFF_FLOW_CTRL
263  case SLIP_ESC_XON:
264  if(state == SLIP_ESC) {
265  outbuf[len++] = XON;
266  state = SLIP_NEUTRAL;
267  } else {
268  outbuf[len++] = c;
269  } break;
270  case SLIP_ESC_XOFF:
271  if(state == SLIP_ESC) {
272  outbuf[len++] = XOFF;
273  state = SLIP_NEUTRAL;
274  } else {
275  outbuf[len++] = c;
276  } break;
277 #endif /* UART_XONXOFF_FLOW_CTRL */
278  default:
279  outbuf[len++] = c;
280  state = SLIP_NEUTRAL;
281  break;
282  }
283  }
284 
285  /* Update counters */
286  if(c == SLIP_END) {
287  ATOMIC(begin = pos;
288  if(end_counter) {
289  end_counter--;
290  }
291  )
292  PUTCHAR('P');
293  } else {
294  /* Something went wrong, no SLIP_END found, drop everything */
295  ATOMIC(rxbuf_init();
296  is_dropping = 1;
297  )
298  SLIP_STATISTICS(slip_error_drop++);
299  len = 0;
300  PRINTF("SLIP: *** out of sync!\n");
301  }
302 
303  if(end_counter > 0) {
304  /* One more packet is buffered, need to be polled again! */
305  process_poll(&slip_process);
306  }
307  return len;
308 }
309 /*---------------------------------------------------------------------------*/
310 PROCESS_THREAD(slip_process, ev, data)
311 {
312  PROCESS_BEGIN();
313 
314  rxbuf_init();
315 
316  while(1) {
317  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
318 
319  /* Move packet from rxbuf to buffer provided by uIP. */
320  uip_len = slip_poll_handler(&uip_buf[UIP_LLH_LEN],
321  UIP_BUFSIZE - UIP_LLH_LEN);
322 
323  PRINTF("SLIP: recv bytes %u frames RECV: %u. is_full %u, is_dropping %u.\n",
324  end_counter, uip_len, is_full, is_dropping);
325 
326  /* We have free space now, resume slip RX */
327  if(is_full) {
328  is_full = 0;
329  ENABLE_UART_INTERRUPTS();
330  }
331 
332  if(uip_len > 0) {
333  if(input_callback) {
334  input_callback();
335  }
336 #ifdef SLIP_CONF_TCPIP_INPUT
337  SLIP_CONF_TCPIP_INPUT();
338 #else
339  tcpip_input();
340 #endif
341  }
342  }
343 
344  PROCESS_END();
345 }
346 /*---------------------------------------------------------------------------*/
347 /* Return status from slip_input_byte:
348  * -1 means RX buffer overflow ==> stop reading
349  * 0 means do not exit power saving mode
350  * 1 means exit power saving mode
351  **/
352 int
353 slip_input_byte(unsigned char c)
354 {
355  static int in_frame = 0;
356  uint16_t next, next_next;
357  int error_return_code = is_full ? -1 : 0;
358  int success_return_code = is_full ? -1 : 1;
359 
360  SLIP_STATISTICS(slip_received++);
361 
362 #if UART_XONXOFF_FLOW_CTRL
363  if(c == XOFF || c == XON) {
364  xonxoff_state = c;
365  return 1;
366  } else {
367  /* ANY char would be XON */
368  xonxoff_state = XON;
369  }
370 #endif /* UART_XONXOFF_FLOW_CTRL */
371 
372  if(is_dropping) {
373  /* Make sure to drop full frames when overflow or
374  * out of sync happens */
375  if(c != SLIP_END) {
376  SLIP_STATISTICS(slip_drop_bytes++);
377  } else {
378  is_dropping = 0;
379  in_frame = 0;
380  }
381  return error_return_code;
382  }
383 
384  if(!in_frame && c == SLIP_END) {
385  /* Ignore slip end when not receiving frame */
386  return error_return_code;
387  /* increment and wrap */
388  }
389  next = end + 1;
390  if(next >= RX_BUFSIZE) {
391  next = 0;
392  }
393  next_next = next + 1;
394  if(next_next >= RX_BUFSIZE) {
395  next_next = 0;
396  /* Next byte will overflow. Stop accepting. */
397  }
398  if(next_next == begin) {
399  is_full = 1;
400  /* disable UART interrupts */
401  DISABLE_UART_INTERRUPTS();
402  process_poll(&slip_process);
403  }
404 
405  /* Buffer is full. We can't store anymore.
406  * Shall not happen normally,
407  * because of overflow protection above. */
408  if(next == begin) {
409  is_dropping = 1;
410  SLIP_STATISTICS(slip_overflow++);
411  is_full = 1;
412  /* disable UART interrupts */
413  DISABLE_UART_INTERRUPTS();
414  process_poll(&slip_process);
415  return -1;
416  }
417 
418  rxbuf[end] = c;
419  end = next;
420  in_frame = 1;
421 
422  if(c == SLIP_END) {
423  in_frame = 0;
424  end_counter++;
425  SLIP_STATISTICS(slip_frames++);
426  process_poll(&slip_process);
427  return success_return_code;
428  }
429  return error_return_code;
430 }
431 /*---------------------------------------------------------------------------*/
void slip_set_input_callback(void(*c)(void))
Set a function to be called when there is activity on the SLIP interface; used for detecting if a nod...
Definition: slip.c:142
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
void slip_write(const void *_ptr, int len)
Send using SLIP len bytes starting from the location pointed to by ptr.
Definition: slip.c:171
uint16_t uip_len
The length of the packet in the uip_buf buffer.
Definition: uip6.c:179
#define ATOMIC(blah)
A block of code may be made atomic by wrapping it with this macro.
Definition: slip.c:89
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define UIP_BUFSIZE
The size of the uIP packet buffer.
Definition: uipopt.h:154
void slip_arch_writeb(unsigned char c)
Write a single byte over SLIP.
Definition: slip-arch.c:53
void slip_send(void)
Send an IP packet from the uIP buffer with SLIP.
Definition: slip.c:190
int slip_input_byte(unsigned char c)
Input a SLIP byte.
Definition: slip.c:353
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define UIP_LLH_LEN
The link level header length.
Definition: uipopt.h:141
#define uip_buf
Macro to access uip_aligned_buf as an array of bytes.
Definition: uip.h:513
Header file for the uIP TCP/IP stack.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1035
void tcpip_input(void)
Deliver an incoming packet to the TCP/IP stack.
Definition: tcpip.c:449