]> sigrok.org Git - libserialport.git/blob - linux_termios.c
Fix a potential segfault in sp_get_port_handle().
[libserialport.git] / linux_termios.c
1 /*
2  * This file is part of the libserialport project.
3  *
4  * Copyright (C) 2013 Martin Ling <martin-libserialport@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * At the time of writing, glibc does not support the Linux kernel interfaces
22  * for setting non-standard baud rates and flow control. We therefore have to
23  * prepare the correct ioctls ourselves, for which we need the declarations in
24  * linux/termios.h.
25  *
26  * We can't include linux/termios.h in serialport.c however, because its
27  * contents conflict with the termios.h provided by glibc. So this file exists
28  * to isolate the bits of code which use the kernel termios declarations.
29  *
30  * The details vary by architecture. Some architectures have c_ispeed/c_ospeed
31  * in struct termios, accessed with TCSETS/TCGETS. Others have these fields in
32  * struct termios2, accessed with TCSETS2/TCGETS2. Some architectures have the
33  * TCSETX/TCGETX ioctls used with struct termiox, others do not.
34  */
35
36 #include <stdlib.h>
37 #include <linux/termios.h>
38 #include "linux_termios.h"
39
40 SP_PRIV unsigned long get_termios_get_ioctl(void)
41 {
42 #ifdef HAVE_TERMIOS2
43         return TCGETS2;
44 #else
45         return TCGETS;
46 #endif
47 }
48
49 SP_PRIV unsigned long get_termios_set_ioctl(void)
50 {
51 #ifdef HAVE_TERMIOS2
52         return TCSETS2;
53 #else
54         return TCSETS;
55 #endif
56 }
57
58 SP_PRIV size_t get_termios_size(void)
59 {
60 #ifdef HAVE_TERMIOS2
61         return sizeof(struct termios2);
62 #else
63         return sizeof(struct termios);
64 #endif
65 }
66
67 #if (defined(HAVE_TERMIOS_SPEED) || defined(HAVE_TERMIOS2_SPEED)) && defined(HAVE_BOTHER)
68 SP_PRIV int get_termios_speed(void *data)
69 {
70 #ifdef HAVE_TERMIOS2
71         struct termios2 *term = (struct termios2 *) data;
72 #else
73         struct termios *term = (struct termios *) data;
74 #endif
75         if (term->c_ispeed != term->c_ospeed)
76                 return -1;
77         else
78                 return term->c_ispeed;
79 }
80
81 SP_PRIV void set_termios_speed(void *data, int speed)
82 {
83 #ifdef HAVE_TERMIOS2
84         struct termios2 *term = (struct termios2 *) data;
85 #else
86         struct termios *term = (struct termios *) data;
87 #endif
88         term->c_cflag &= ~CBAUD;
89         term->c_cflag |= BOTHER;
90         term->c_ispeed = term->c_ospeed = speed;
91 }
92 #endif
93
94 #ifdef HAVE_TERMIOX
95 SP_PRIV size_t get_termiox_size(void)
96 {
97         return sizeof(struct termiox);
98 }
99
100 SP_PRIV int get_termiox_flow(void *data, int *rts, int *cts, int *dtr, int *dsr)
101 {
102         struct termiox *termx = (struct termiox *) data;
103         int flags = 0;
104
105         *rts = (termx->x_cflag & RTSXOFF);
106         *cts = (termx->x_cflag & CTSXON);
107         *dtr = (termx->x_cflag & DTRXOFF);
108         *dsr = (termx->x_cflag & DSRXON);
109
110         return flags;
111 }
112
113 SP_PRIV void set_termiox_flow(void *data, int rts, int cts, int dtr, int dsr)
114 {
115         struct termiox *termx = (struct termiox *) data;
116
117         termx->x_cflag &= ~(RTSXOFF | CTSXON | DTRXOFF | DSRXON);
118
119         if (rts)
120                 termx->x_cflag |= RTSXOFF;
121         if (cts)
122                 termx->x_cflag |= CTSXON;
123         if (dtr)
124                 termx->x_cflag |= DTRXOFF;
125         if (dsr)
126                 termx->x_cflag |= DSRXON;
127 }
128 #endif