BOROSOFTWAREAll work →
scantool/diag_tty_unix.h 182 lines
1/*
2 * diag_tty_unix.h
3 *
4 * This is totally unix-exclusive and should only be included by diag_tty_unix.c !
5 * Public functions are in diag_tty.h
6 *
7 * This file is part of freediag - Vehicle Diagnostic Utility
8 *
9 * Copyright (C) 2001-2004 Richard Almeida & others
10 * Copyright (C) 2004 Steve Baker <[email protected]>
11 * Copyright (C) 2004 Steve Meisner <[email protected]>
12 * Copyright (C) 2004 Vasco Nevoa <[email protected]>
13 * Copyright (C) 2011-2015 fenugrec <[email protected]>
14 * Copyright (C) 2015 Tomasz Kaźmierczak <[email protected]>
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31#ifndef _DIAG_TTY_UNIX_H_
32#define _DIAG_TTY_UNIX_H_
33
34#if defined(__cplusplus)
35extern "C" {
36#endif
37
38
39#include <unistd.h>
40
41/****** OS-specific implementation selectors ******/
42/* These are for testing/debugging only, to force compilation of certain implementations
43 for diag_tty* and diag_os* functions.
44
45 ### Map of features with more than one implementation related to POSIX ###
46
47 ## tty-related features ##
48 SEL_TIMEOUT: diag_tty_{read,write}() timeouts
49 S_POSIX) needs _POSIX_TIMERS, uses timer_create + sigaction for a SIGUSR1 handler
50 S_OTHER) use select(timeout) + read + manual timeout check loop
51 S_LINUX) needs __linux__ && /dev/rtc
52 X) (ugly, not implemented) : increase OS periodic callback frequency, control timeout manually
53 SEL_TTYOPEN: diag_tty_open() : open() flags:
54 ALT1) needs O_NONBLOCK; open non-blocking then clear flag
55 ALT2) don't set O_NONBLOCK.
56 SEL_TTYBAUD: diag_tty_setup() : tty settings (bps, parity etc)
57 ALT1) needs __linux__ : termios2 + BOTHER
58 ALT2) needs __linux__ : uses TIOCSSERIAL, ASYNC_SPD_CUST, CBAUD.
59 ALT3) needs "B9600 == 9600" etc. Calls cfset{i,o}speed with speed in bps (very non-portable)
60 ALTx) picks nearest standard Bxxxx; calls cfset{i,o}speed (universal fallback)
61 ######
62 For every feature listed above, it's possible to force compilation of
63 a specific implementation using the #defines below.
64 TODO: add compile tests to cmake?
65 */
66#define S_AUTO 0
67/* First set, for obviously OS-dependant features: */
68#define S_POSIX 1
69#define S_LINUX 2
70#define S_OTHER 3
71/* Second set, not necessarily OS-dependant */
72#define S_ALT1 1
73#define S_ALT2 2
74#define S_ALT3 3
75/** Insert desired selectors here **/
76//example:
77#define SEL_TIMEOUT S_OTHER
78//#define SEL_TIMEOUT S_LINUX
79//#define SEL_TTYBAUD S_ALT3
80
81/* Default selectors: anything still undefined is set to S_AUTO which
82 means "force nothing", i.e. "use most appropriate implementation". */
83#ifndef SEL_TIMEOUT
84#define SEL_TIMEOUT S_AUTO
85#endif
86#ifndef SEL_TTYOPEN
87#define SEL_TTYOPEN S_AUTO
88#endif
89#ifndef SEL_TTYBAUD
90#define SEL_TTYBAUD S_AUTO
91#endif
92/****** ******/
93
94
95/*
96 There are two possible definitions for "struct termios". One is found
97 in <termios.h> provided by glibc; the other is in <asm/termios.h> provided
98 by linux kernel headers ! They differ and are mutually exclusive, of course.
99 */
100
101/** FUGLY HACKS BELOW **/
102#if defined(__linux__) && (SEL_TTYBAUD==S_ALT1 || SEL_TTYBAUD==S_AUTO)
103 #define USE_TERMIOS2
104#endif
105
106#ifdef USE_TERMIOS2
107 #include <asm/termbits.h> //including <asm/termios.h> causes duplicate def errors
108
109/* Ugliness necessary because :
110 0- <asm/termios.h>, or at least <asm/termbits.h> is needed for BOTHER and struct termios2
111 1- ioctl() is provided by glibc, and defined in <sys/ioctl.h>
112 2- <asm/ioctls.h> (included through sys/ioctl.h -> bits/ioctls.h -> asm/ioctls.h) is needed for TCSETS2
113 3- <asm/termios.h> defines winsize and some other junk covered by bits/ioctl-types;
114 3b- <bits/ioctl-types.h> is included by <sys/ioctl.h> !
115
116 Could we someday have a sane way of setting integer baud rates ? pfah.
117 */
118extern int cfsetispeed(struct termios *__termios_p, speed_t __speed);
119extern int cfsetospeed(struct termios *__termios_p, speed_t __speed);
120#else
121 #include <termios.h> //has speed_t, tcsetattr, struct termios, cfset*speed, etc
122#endif // USE_TERMIOS2
123/** END OF FUGLINESS **/
124
125#include <sys/ioctl.h>
126
127#if defined(_POSIX_TIMERS)
128 #include <signal.h> //sig_atomic_t
129 #include <time.h>
130#endif
131
132#if defined(__linux__)
133 #include <linux/rtc.h>
134 #include <linux/serial.h> /* For Linux-specific struct serial_struct */
135#endif
136
137#include "diag_tty.h"
138
139#define DL0D_INVALIDHANDLE -1
140
141
142//struct tty_int : internal data, one per L0 struct
143struct unix_tty_int {
144 char *name; /* port name */
145 int fd; /* File descriptor */
146
147#if defined(__linux__)
148 //struct serial_struct : only used with TIOCGSERIAL + TIOCSSERIAL ioctls,
149 // which are not always available. Hence this flag:
150 int tioc_works; //indicate if TIOCGSERIAL + TIOCSSERIAL work
151 //TODO : expand to a more general "detected tty capabilities" set of flags.
152
153 /* For recording state before/while/after messing with the interface: */
154 struct serial_struct ss_orig; //original backup
155 struct serial_struct ss_cur; //current state
156
157#endif
158
159 //backup & current termios structs
160 struct termios st_orig;
161 struct termios st_cur;
162#ifdef USE_TERMIOS2
163 struct termios2 st2_cur;
164 struct termios2 st2_orig;
165#endif
166
167 //flags backup (ioctl TIOCMGET, TIOCMSET)
168 int modemflags;
169
170#if defined(_POSIX_TIMERS)
171 timer_t timerid; //Used for read() and write() timeouts
172 volatile sig_atomic_t pt_expired; //flag timeout expiry
173#endif
174
175 unsigned long int byte_write_timeout_us; //single byte write timeout in microseconds
176};
177
178#if defined(__cplusplus)
179}
180#endif
181#endif /*_DIAG_TTY_UNIX_H_ */
182