Commit | Line | Data |
---|---|---|
ae0ea72e SC |
1 | /* Remote serial interface for OS's with termios |
2 | ||
3 | Copyright 1992 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | #include "defs.h" | |
23 | #include "serial.h" | |
24 | #include <fcntl.h> | |
25 | #include <termios.h> | |
26 | #include <sys/time.h> | |
27 | ||
28 | static int desc; | |
29 | ||
30 | ||
31 | CONST char * | |
32 | DEFUN_VOID(serial_default_name) | |
33 | { | |
34 | return "/dev/ttya"; | |
35 | } | |
36 | ||
37 | void | |
38 | DEFUN_VOID(serial_raw) | |
39 | { | |
40 | /* Now happens inside of serial_open */ | |
41 | } | |
42 | ||
43 | static struct termios otermios; | |
44 | static int oflags; | |
45 | ||
46 | void | |
47 | DEFUN_VOID(serial_normal) | |
48 | { | |
49 | fcntl(desc, oflags, 0); | |
50 | ||
51 | if (tcsetattr(desc, TCSANOW, &otermios)) | |
52 | { | |
53 | printf("tcgetattr failed: errno=%d\n", errno); | |
54 | } | |
55 | } | |
56 | ||
57 | int | |
58 | DEFUN(serial_open,(name), | |
59 | CONST char *name) | |
60 | { | |
61 | struct termios termios; | |
62 | ||
63 | desc = open (name, O_RDWR); | |
64 | if (desc < 0) | |
65 | perror("Open failed: "); | |
66 | ||
67 | oflags = fcntl(desc, F_GETFL, 0); | |
68 | ||
69 | fcntl(desc, F_SETFL, oflags|FNDELAY); | |
70 | ||
71 | if (tcgetattr(desc, &termios)) { | |
72 | printf("tcgetattr failed: errno=%d\n", errno); | |
73 | } | |
74 | ||
75 | otermios = termios; | |
76 | ||
77 | termios.c_iflag = 0; | |
78 | termios.c_oflag = 0; | |
79 | termios.c_lflag = 0; | |
80 | termios.c_cc[VMIN] = 0; | |
81 | termios.c_cc[VTIME] = 0; | |
82 | ||
83 | if (tcsetattr(desc, TCSANOW, &termios)) { | |
84 | printf("tcgetattr failed: errno=%d\n", errno); | |
85 | } | |
86 | ||
87 | return 1; | |
88 | } | |
89 | ||
90 | int | |
91 | DEFUN(serial_timedreadchar,(timeout, ok), | |
92 | int timeout AND | |
93 | int *ok) | |
94 | { | |
95 | unsigned char buf; | |
96 | fd_set readfds; | |
97 | int val; | |
98 | struct timeval tv; | |
99 | ||
100 | FD_ZERO(&readfds); | |
101 | FD_SET(desc, &readfds); | |
102 | ||
103 | tv.tv_sec = timeout; | |
104 | tv.tv_usec = 0; | |
105 | ||
106 | val = select(desc+1, &readfds, 0, 0, &tv); | |
107 | ||
108 | if (val > 0 && FD_ISSET(desc, &readfds)) | |
109 | { | |
110 | val = read (desc, &buf, 1); | |
111 | ||
112 | if (val == 1) | |
113 | { | |
114 | *ok = 1; | |
115 | return buf; | |
116 | } | |
117 | } | |
118 | ||
119 | *ok = 0; | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | /* Translate baud rates from integers to damn B_codes. Unix should | |
125 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
126 | ||
127 | #ifndef B19200 | |
128 | #define B19200 EXTA | |
129 | #endif | |
130 | #ifndef B38400 | |
131 | #define B38400 EXTB | |
132 | #endif | |
133 | ||
134 | static struct {int rate, damn_b;} baudtab[] = { | |
135 | {9600, B9600}, | |
136 | ||
137 | {19200, B19200}, | |
138 | #if 0 | |
139 | {300, B300}, | |
140 | {1200, B1200}, | |
141 | {2400, B2400}, | |
142 | {4800, B4800}, | |
143 | #endif | |
144 | {-1, -1}, | |
145 | }; | |
146 | ||
147 | static int | |
148 | DEFUN(damn_b,(rate), | |
149 | int rate) | |
150 | { | |
151 | int i; | |
152 | for (i = 0; baudtab[i].rate != -1; i++) | |
153 | { | |
154 | if (rate == baudtab[i].rate) | |
155 | { | |
156 | return i; | |
157 | } | |
158 | } | |
159 | return -1; | |
160 | } | |
161 | ||
162 | int | |
163 | DEFUN(serial_setbaudrate,(rate),int rate) | |
164 | { | |
165 | struct termios termios; | |
166 | ||
167 | if (tcgetattr(desc, &termios)) { | |
168 | printf("tcgetattr failed: errno=%d\n", errno); | |
169 | } | |
170 | ||
171 | cfsetospeed(&termios, baudtab[damn_b(rate)].damn_b); | |
172 | cfsetispeed(&termios, baudtab[damn_b(rate)].damn_b); | |
173 | ||
174 | if (tcsetattr(desc, TCSANOW, &termios)) { | |
175 | printf("tcgetattr failed: errno=%d\n", errno); | |
176 | } | |
177 | ||
178 | return 1; | |
179 | } | |
180 | ||
181 | int | |
182 | DEFUN(serial_nextbaudrate,(rate), | |
183 | int rate) | |
184 | { | |
185 | int lookup; | |
186 | lookup = damn_b(rate); | |
187 | if (lookup == -1) | |
188 | return baudtab[0].rate; | |
189 | lookup++; | |
190 | if (baudtab[lookup].rate == -1) | |
191 | return baudtab[0].rate; | |
192 | return baudtab[lookup].rate; | |
193 | } | |
194 | ||
195 | int | |
196 | DEFUN(serial_write,(str, len), | |
197 | CONST char *str AND | |
198 | int len) | |
199 | { | |
200 | write (desc, str, len); | |
201 | } | |
202 | ||
203 | ||
204 | int | |
205 | DEFUN_VOID(serial_close) | |
206 | { | |
207 | close(desc); | |
208 | } |