Contiki-NG
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  */
32 #include "dev/serial-line.h"
33 #include <string.h> /* for memcpy() */
34 
35 #include "lib/ringbuf.h"
36 
37 #ifdef SERIAL_LINE_CONF_BUFSIZE
38 #define BUFSIZE SERIAL_LINE_CONF_BUFSIZE
39 #else /* SERIAL_LINE_CONF_BUFSIZE */
40 #define BUFSIZE 128
41 #endif /* SERIAL_LINE_CONF_BUFSIZE */
42 
43 #if (BUFSIZE & (BUFSIZE - 1)) != 0
44 #error SERIAL_LINE_CONF_BUFSIZE must be a power of two (i.e., 1, 2, 4, 8, 16, 32, 64, ...).
45 #error Change SERIAL_LINE_CONF_BUFSIZE in contiki-conf.h.
46 #endif
47 
48 #ifndef IGNORE_CHAR
49 #define IGNORE_CHAR(c) (c == 0x0d)
50 #endif
51 
52 #ifndef END
53 #define END 0x0a
54 #endif
55 
56 static struct ringbuf rxbuf;
57 static uint8_t rxbuf_data[BUFSIZE];
58 
59 PROCESS(serial_line_process, "Serial driver");
60 
61 process_event_t serial_line_event_message;
62 
63 /*---------------------------------------------------------------------------*/
64 int
65 serial_line_input_byte(unsigned char c)
66 {
67  static uint8_t overflow = 0; /* Buffer overflow: ignore until END */
68 
69  if(IGNORE_CHAR(c)) {
70  return 0;
71  }
72 
73  if(!overflow) {
74  /* Add character */
75  if(ringbuf_put(&rxbuf, c) == 0) {
76  /* Buffer overflow: ignore the rest of the line */
77  overflow = 1;
78  }
79  } else {
80  /* Buffer overflowed:
81  * Only (try to) add terminator characters, otherwise skip */
82  if(c == END && ringbuf_put(&rxbuf, c) != 0) {
83  overflow = 0;
84  }
85  }
86 
87  /* Wake up consumer process */
88  process_poll(&serial_line_process);
89  return 1;
90 }
91 /*---------------------------------------------------------------------------*/
92 PROCESS_THREAD(serial_line_process, ev, data)
93 {
94  static char buf[BUFSIZE];
95  static int ptr;
96 
97  PROCESS_BEGIN();
98 
100  ptr = 0;
101 
102  while(1) {
103  /* Fill application buffer until newline or empty */
104  int c = ringbuf_get(&rxbuf);
105 
106  if(c == -1) {
107  /* Buffer empty, wait for poll */
108  PROCESS_YIELD();
109  } else {
110  if(c != END) {
111  if(ptr < BUFSIZE-1) {
112  buf[ptr++] = (uint8_t)c;
113  } else {
114  /* Ignore character (wait for EOL) */
115  }
116  } else {
117  /* Terminate */
118  buf[ptr++] = (uint8_t)'\0';
119 
120  /* Broadcast event */
121  process_post(PROCESS_BROADCAST, serial_line_event_message, buf);
122 
123  /* Wait until all processes have handled the serial line event */
124  if(PROCESS_ERR_OK ==
125  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL)) {
126  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE);
127  }
128  ptr = 0;
129  }
130  }
131  }
132 
133  PROCESS_END();
134 }
135 /*---------------------------------------------------------------------------*/
136 void
137 serial_line_init(void)
138 {
139  ringbuf_init(&rxbuf, rxbuf_data, sizeof(rxbuf_data));
140  process_start(&serial_line_process, NULL);
141 }
142 /*---------------------------------------------------------------------------*/
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#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
int ringbuf_get(struct ringbuf *r)
Get a byte from the ring buffer.
Definition: ringbuf.c:80
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
Header file for the ring buffer library
#define PROCESS_ERR_OK
Return value indicating that an operation was successful.
Definition: process.h:74
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
int serial_line_input_byte(unsigned char c)
Get one byte of input from the serial driver.
Definition: serial-line.c:65
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define PROCESS_YIELD()
Yield the currently running process.
Definition: process.h:164
process_event_t process_alloc_event(void)
Allocate a global event number.
Definition: process.c:93
process_event_t serial_line_event_message
Event posted when a line of input has been received.
Definition: serial-line.c:61
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
Generic serial I/O process header filer.
Structure that holds the state of a ring buffer.
Definition: ringbuf.h:68
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1035
int ringbuf_put(struct ringbuf *r, uint8_t c)
Insert a byte into the ring buffer.
Definition: ringbuf.c:53
void ringbuf_init(struct ringbuf *r, uint8_t *dataptr, uint8_t size)
Initialize a ring buffer.
Definition: ringbuf.c:44
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99