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 END
49 #define END 0x0a
50 #endif
51 #ifndef END2
52 #define END2 0x0d
53 #endif
54 
55 static struct ringbuf rxbuf;
56 static uint8_t rxbuf_data[BUFSIZE];
57 
58 PROCESS(serial_line_process, "Serial driver");
59 
60 process_event_t serial_line_event_message;
61 
62 /*---------------------------------------------------------------------------*/
63 int
64 serial_line_input_byte(unsigned char c)
65 {
66  static uint8_t overflow = 0; /* Buffer overflow: ignore until END */
67 
68  if(!overflow) {
69  /* Add character */
70  if(ringbuf_put(&rxbuf, c) == 0) {
71  /* Buffer overflow: ignore the rest of the line */
72  overflow = 1;
73  }
74  } else {
75  /* Buffer overflowed:
76  * Only (try to) add terminator characters, otherwise skip */
77  if((c == END || c == END2) && ringbuf_put(&rxbuf, c) != 0) {
78  overflow = 0;
79  }
80  }
81 
82  /* Wake up consumer process */
83  process_poll(&serial_line_process);
84  return 1;
85 }
86 /*---------------------------------------------------------------------------*/
87 PROCESS_THREAD(serial_line_process, ev, data)
88 {
89  static char buf[BUFSIZE];
90  static int ptr;
91 
92  PROCESS_BEGIN();
93 
95  ptr = 0;
96 
97  while(1) {
98  /* Fill application buffer until newline or empty */
99  int c = ringbuf_get(&rxbuf);
100 
101  if(c == -1) {
102  /* Buffer empty, wait for poll */
103  PROCESS_YIELD();
104  } else {
105  if((c != END && c != END2)) {
106  if(ptr < BUFSIZE-1) {
107  buf[ptr++] = (uint8_t)c;
108  } else {
109  /* Ignore character (wait for EOL) */
110  }
111  } else {
112  /* Terminate */
113  buf[ptr++] = (uint8_t)'\0';
114 
115  /* Broadcast event */
116  process_post(PROCESS_BROADCAST, serial_line_event_message, buf);
117 
118  /* Wait until all processes have handled the serial line event */
119  if(PROCESS_ERR_OK ==
120  process_post(PROCESS_CURRENT(), PROCESS_EVENT_CONTINUE, NULL)) {
121  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE);
122  }
123  ptr = 0;
124  }
125  }
126  }
127 
128  PROCESS_END();
129 }
130 /*---------------------------------------------------------------------------*/
131 void
132 serial_line_init(void)
133 {
134  ringbuf_init(&rxbuf, rxbuf_data, sizeof(rxbuf_data));
135  process_start(&serial_line_process, NULL);
136 }
137 /*---------------------------------------------------------------------------*/
#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:64
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:60
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:1107
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