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);
140 TRACE(
TRACE_ERROR,
"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
145 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n", device, strerror(errno), errno);
154 void(*term_conf_callback)(
struct termios *, speed_t *))
158 if ((me->
fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
159 TRACE(
TRACE_ERROR,
"%s, open failed: %s (%d)\n", device, strerror(errno), errno);
163 TRACE(
TRACE_ERROR,
"%s, get term settings failed: %s (%d)\n", device, strerror(errno), errno);
170 TRACE(
TRACE_ERROR,
"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
175 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n", device, strerror(errno), errno);
188 if (!me || me->
fd < 0) {
191 if (tcflush(me->
fd, TCIOFLUSH)) {
197 TRACE(
TRACE_ERROR,
"restoring term attributes (%s) (%d)\n", strerror(errno), errno);
213 if (!me || me->
fd < 0) {
217 TRACE(
TRACE_ERROR,
"%s, set term speed failed: %s (%d)\n", device, strerror(errno), errno);
227 if (!me || me->
fd < 0) {
255 TRACE(
TRACE_ERROR,
"setting term attributes (%s) (%d)\n", strerror(errno), errno);
266 int ioctl(
int d,
int request, ...);
267 int tcflush (
int __fd,
int __queue_selector);
274 if (tcflush(me->
fd, TCIFLUSH)) {
275 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n",
"", strerror(errno), errno);
276 fprintf(stderr,
"flush (%d) failed: %s (%d)\n", me->
fd, strerror(errno), errno);
285 if (tcflush(me->
fd, TCOFLUSH)) {
286 TRACE(
TRACE_ERROR,
"%s, set term attr failed: %s (%d)\n",
"", strerror(errno), errno);
287 fprintf(stderr,
"flush (%d) failed: %s (%d)\n", me->
fd, strerror(errno), errno);
293 if ((me->
fd = open(device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
294 TRACE(
TRACE_ERROR,
"%s, open failed: %s (%d)\n", device, strerror(errno), errno);
306 me->
cur_termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INPCK | ISTRIP | INLCR | IGNCR
307 | ICRNL |
IUCLC | IXON | IXANY | IXOFF | IMAXBEL);
310 me->
cur_termios.c_cflag &= ~(CSIZE | PARENB | CRTSCTS | PARODD | HUPCL | CSTOPB);
313 me->
cur_termios.c_lflag &= ~(ISIG | ICANON | IEXTEN | ECHO | FLUSHO | PENDIN);
316 me->
cur_termios.c_oflag &=~(OPOST|ONLCR|OCRNL|ONOCR|ONLRET);
344 void(*term_conf_callback)(
struct termios *, speed_t *))
348 (void)term_conf_callback;
356 if (!me || me->
fd < 0) {
359 if (tcflush(me->
fd, TCIOFLUSH)) {
365 TRACE(
TRACE_ERROR,
"restoring term attributes (%s) (%d)\n", strerror(errno), errno);
381 if (!me || me->
fd < 0) {
403 if (!me || me->
fd < 0) {
#define TRACE(type, fmt, args...)
void serial_port_free(struct SerialPort *me)
void serial_port_close(struct SerialPort *me)
static int uart_speed(int def)
int serial_port_open_raw(struct SerialPort *me, const char *device, speed_t speed)
#define B100000
some definitions from PPRZ uart driver as we can't include it directly
void serial_port_flush_output(struct SerialPort *me)
int serial_port_set_bits_stop_parity(struct SerialPort *me, const int bits, const int stop, const int parity)
int serial_port_open(struct SerialPort *me, const char *device, void(*term_conf_callback)(struct termios *, speed_t *))
struct SerialPort * serial_port_new(void)
int serial_port_set_baudrate(struct SerialPort *me, speed_t speed)
void serial_port_flush(struct SerialPort *me)
struct termios cur_termios
struct termios orig_termios