Contiki 2.5
serial-line.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
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  * @(#)$Id: serial-line.c,v 1.4 2010/02/23 18:26:26 adamdunkels Exp $
32  */
33 #include "dev/serial-line.h"
34 #include <string.h> /* for memcpy() */
35 
36 #include "lib/ringbuf.h"
37 
38 #ifdef SERIAL_LINE_CONF_BUFSIZE
39 #define BUFSIZE SERIAL_LINE_CONF_BUFSIZE
40 #else /* SERIAL_LINE_CONF_BUFSIZE */
41 #define BUFSIZE 128
42 #endif /* SERIAL_LINE_CONF_BUFSIZE */
43 
44 #if (BUFSIZE & (BUFSIZE - 1)) != 0
45 #error SERIAL_LINE_CONF_BUFSIZE must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
46 #error Change SERIAL_LINE_CONF_BUFSIZE in contiki-conf.h.
47 #endif
48 
49 #define IGNORE_CHAR(c) (c == 0x0d)
50 #define END 0x0a
51 
52 static struct ringbuf rxbuf;
53 static uint8_t rxbuf_data[BUFSIZE];
54 
55 PROCESS(serial_line_process, "Serial driver");
56 
57 process_event_t serial_line_event_message;
58 
59 /*---------------------------------------------------------------------------*/
60 int
61 serial_line_input_byte(unsigned char c)
62 {
63  static uint8_t overflow = 0; /* Buffer overflow: ignore until END */
64 
65  if(IGNORE_CHAR(c)) {
66  return 0;
67  }
68 
69  if(!overflow) {
70  /* Add character */
71  if(ringbuf_put(&rxbuf, c) == 0) {
72  /* Buffer overflow: ignore the rest of the line */
73  overflow = 1;
74  }
75  } else {
76  /* Buffer overflowed:
77  * Only (try to) add terminator characters, otherwise skip */
78  if(c == END && ringbuf_put(&rxbuf, c) != 0) {
79  overflow = 0;
80  }
81  }
82 
83  /* Wake up consumer process */
84  process_poll(&serial_line_process);
85  return 1;
86 }
87 /*---------------------------------------------------------------------------*/
88 PROCESS_THREAD(serial_line_process, ev, data)
89 {
90  static char buf[BUFSIZE];
91  static int ptr;
92 
93  PROCESS_BEGIN();
94 
95  serial_line_event_message = process_alloc_event();
96  ptr = 0;
97 
98  while(1) {
99  /* Fill application buffer until newline or empty */
100  int c = ringbuf_get(&rxbuf);
101 
102  if(c == -1) {
103  /* Buffer empty, wait for poll */
104  PROCESS_YIELD();
105  } else {
106  if(c != END) {
107  if(ptr < BUFSIZE-1) {
108  buf[ptr++] = (uint8_t)c;
109  } else {
110  /* Ignore character (wait for EOL) */
111  }
112  } else {
113  /* Terminate */
114  buf[ptr++] = (uint8_t)'\0';
115 
116  /* Broadcast event */
117  process_post(PROCESS_BROADCAST, serial_line_event_message, buf);
118 
119  /* Wait until all processes have handled the serial line event */
120  if(PROCESS_ERR_OK ==
121  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL)) {
122  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE);
123  }
124  ptr = 0;
125  }
126  }
127  }
128 
129  PROCESS_END();
130 }
131 /*---------------------------------------------------------------------------*/
132 void
133 serial_line_init(void)
134 {
135  ringbuf_init(&rxbuf, rxbuf_data, sizeof(rxbuf_data));
136  process_start(&serial_line_process, NULL);
137 }
138 /*---------------------------------------------------------------------------*/