17 #if !USE_ARBITRARY_BAUDRATE
20 #include <linux/termios.h>
25 #define B100000 100000
32 case B1200:
return 1200;
33 case B2400:
return 2400;
34 case B4800:
return 4800;
35 case B9600:
return 9600;
57 #define UPARITY_EVEN 2
71 #define TRACE(type,fmt,args...)
86 #if !USE_ARBITRARY_BAUDRATE
94 if (tcflush(me->
fd, TCIFLUSH)) {
95 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n",
"", strerror(errno), errno);
96 fprintf(stderr,
"flush (%d) failed: %s (%d)\n", me->
fd, strerror(errno), errno);
105 if (tcflush(me->
fd, TCOFLUSH)) {
106 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n",
"", strerror(errno), errno);
107 fprintf(stderr,
"flush (%d) failed: %s (%d)\n", me->
fd, strerror(errno), errno);
113 if ((me->
fd = open(device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
114 TRACE(
TRACE_ERROR,
"%s, open failed: %s (%d)\n", device, strerror(errno), errno);
118 TRACE(
TRACE_ERROR,
"%s, get term settings failed: %s (%d)\n", device, strerror(errno), errno);
124 me->
cur_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
125 | ICRNL |
IUCLC | IXON | IXANY | IXOFF | IMAXBEL);
128 me->
cur_termios.c_cflag &= ~(CSIZE | PARENB | CRTSCTS | PARODD | HUPCL | CSTOPB);
131 me->
cur_termios.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | FLUSHO | PENDIN);
134 me->
cur_termios.c_oflag &=~(OPOST|ONLCR|OCRNL|ONOCR|ONLRET);
137 TRACE(
TRACE_ERROR,
"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
142 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n", device, strerror(errno), errno);
151 void(*term_conf_callback)(
struct termios *, speed_t *))
155 if ((me->
fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
156 TRACE(
TRACE_ERROR,
"%s, open failed: %s (%d)\n", device, strerror(errno), errno);
160 TRACE(
TRACE_ERROR,
"%s, get term settings failed: %s (%d)\n", device, strerror(errno), errno);
167 TRACE(
TRACE_ERROR,
"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
172 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n", device, strerror(errno), errno);
185 if (!me || me->
fd < 0) {
188 if (tcflush(me->
fd, TCIOFLUSH)) {
194 TRACE(
TRACE_ERROR,
"restoring term attributes (%s) (%d)\n", strerror(errno), errno);
210 if (!me || me->
fd < 0) {
214 TRACE(
TRACE_ERROR,
"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
224 if (!me || me->
fd < 0) {
252 TRACE(
TRACE_ERROR,
"setting term attributes (%s) (%d)\n", strerror(errno), errno);
258 #else // USE_ARBITRARY_BAUDRATE
263 int ioctl(
int d,
int request, ...);
264 int tcflush (
int __fd,
int __queue_selector);
271 if (tcflush(me->
fd, TCIFLUSH)) {
272 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n",
"", strerror(errno), errno);
273 fprintf(stderr,
"flush (%d) failed: %s (%d)\n", me->
fd, strerror(errno), errno);
282 if (tcflush(me->
fd, TCOFLUSH)) {
283 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n",
"", strerror(errno), errno);
284 fprintf(stderr,
"flush (%d) failed: %s (%d)\n", me->
fd, strerror(errno), errno);
290 if ((me->
fd = open(device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
291 TRACE(
TRACE_ERROR,
"%s, open failed: %s (%d)\n", device, strerror(errno), errno);
303 me->
cur_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
304 | ICRNL |
IUCLC | IXON | IXANY | IXOFF | IMAXBEL);
307 me->
cur_termios.c_cflag &= ~(CSIZE | PARENB | CRTSCTS | PARODD | HUPCL | CSTOPB);
310 me->
cur_termios.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | FLUSHO | PENDIN);
313 me->
cur_termios.c_oflag &=~(OPOST|ONLCR|OCRNL|ONOCR|ONLRET);
338 void(*term_conf_callback)(
struct termios *, speed_t *))
342 (void)term_conf_callback;
350 if (!me || me->
fd < 0) {
353 if (tcflush(me->
fd, TCIOFLUSH)) {
359 TRACE(
TRACE_ERROR,
"restoring term attributes (%s) (%d)\n", strerror(errno), errno);
375 if (!me || me->
fd < 0) {
397 if (!me || me->
fd < 0) {
int serial_port_set_bits_stop_parity(struct SerialPort *me, const int bits, const int stop, const int parity)
void serial_port_flush_output(struct SerialPort *me)
#define TRACE(type, fmt, args...)
struct termios orig_termios
void serial_port_flush(struct SerialPort *me)
void serial_port_free(struct SerialPort *me)
#define B100000
some definitions from PPRZ uart driver as we can't include it directly
struct termios cur_termios
struct SerialPort * serial_port_new(void)
void serial_port_close(struct SerialPort *me)
int serial_port_open_raw(struct SerialPort *me, const char *device, speed_t speed)
int serial_port_open(struct SerialPort *me, const char *device, void(*term_conf_callback)(struct termios *, speed_t *))
int serial_port_set_baudrate(struct SerialPort *me, speed_t speed)
static int uart_speed(int def)