-
Notifications
You must be signed in to change notification settings - Fork 0
/
termdefs.c
409 lines (372 loc) · 11.9 KB
/
termdefs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// src/aksl/termdefs.c 2017-10-25 Alan U. Kennington.
/*-----------------------------------------------------------------------------
Copyright (C) 1989-2018, Alan U. Kennington.
You may distribute this software under the terms of Alan U. Kennington's
modified Artistic Licence, as specified in the accompanying LICENCE file.
-----------------------------------------------------------------------------*/
// Some definitions for terminal interfaces.
#ifdef unix
/*------------------------------------------------------------------------------
Functions in this file:
baud_rate_print_diff
termios_print_diff
term_config
terminal::
config
------------------------------------------------------------------------------*/
// AKSL header files:
#include "aksl/termdefs.h"
#ifndef AKSL_STR_H
#include "aksl/str.h"
#endif
#ifndef AKSL_NUMPRINT_H
#include "aksl/numprint.h"
#endif
#ifndef AKSL_CONFIG_H
#include "aksl/config.h"
#endif
// System header files:
// stdio.h should not be used for I/O -- use only sprintf().
// Use C++ streams instead of printf, fprintf etc.
#ifndef AKSL_X_STDIO_H
#define AKSL_X_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_FCNTL_H
#ifndef AKSL_X_FCNTL_H
#define AKSL_X_FCNTL_H
#include <fcntl.h>
#endif
#endif
#ifdef HAVE_UNISTD_H
#ifndef AKSL_X_UNISTD_H
#define AKSL_X_UNISTD_H
#include <unistd.h>
#endif
#endif
#ifndef AKSL_X_ERRNO_H
#define AKSL_X_ERRNO_H
#include <errno.h>
#endif
// This is needed on goose, but not on emu.
#ifndef AKSL_X_IOMANIP_H
#define AKSL_X_IOMANIP_H
#include <iomanip>
#endif
#ifdef linux
#ifndef AKSL_X_SYS_IOCTL_H
#define AKSL_X_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#endif
// Serial port defaults:
static const char deft_dev_name[] = "/dev/ttya";
const ulong deft_speed_mask = B9600;
//----------------------//
// baud_rate_print_diff //
//----------------------//
void baud_rate_print_diff(ulong cbaud, ulong cbaud0) {
long b = 0;
switch (cbaud0) {
case B0: b = 0; break;
case B50: b = 50; break;
case B75: b = 75; break;
case B110: b = 110; break;
case B134: b = 134; break;
case B150: b = 150; break;
case B200: b = 200; break;
case B300: b = 300; break;
case B600: b = 600; break;
case B1200: b = 1200; break;
case B1800: b = 1800; break;
case B2400: b = 2400; break;
case B4800: b = 4800; break;
case B9600: b = 9600; break;
case B19200: b = 19200; break;
case B38400: b = 38400; break;
default:
b = -1;
cout << "Unrecognized baud rate.\n";
break;
} // End of switch(cbaud0).
long b0 = b;
switch(cbaud) {
case B0: b = 0; break;
case B50: b = 50; break;
case B75: b = 75; break;
case B110: b = 110; break;
case B134: b = 134; break;
case B150: b = 150; break;
case B200: b = 200; break;
case B300: b = 300; break;
case B600: b = 600; break;
case B1200: b = 1200; break;
case B1800: b = 1800; break;
case B2400: b = 2400; break;
case B4800: b = 4800; break;
case B9600: b = 9600; break;
case B19200: b = 19200; break;
case B38400: b = 38400; break;
default:
b = -1;
cout << "Unrecognized baud rate.\n";
break;
} // End of switch(cbaud).
cout << " Baud rate = " << b << " [" << b0 << "].\n";
} // End of function baud_rate_print_diff.
/*------------------------------------------------------------------------------
The second parameter is taken to be the previous value of the structure.
------------------------------------------------------------------------------*/
//----------------------//
// termios_print_diff //
//----------------------//
void termios_print_diff(termios* tio, termios* tio0) {
if (!tio || !tio0)
return;
#define pr0(s, m) \
cout << s << " = 0x" \
<< hex << tio->m << " [0x" << tio0->m << "].\n" << dec
#define pr1(s, m) \
cout << " " << s << " = " << bool_string(m & tio->c_iflag) \
<< " [" << bool_string(m & tio0->c_iflag) << "].\n"
#define pr2(s, m) \
cout << " " << s << " = " << bool_string(m & tio->c_oflag) \
<< " [" << bool_string(m & tio0->c_oflag) << "].\n"
#define pr3(s, m) \
cout << " " << s << " = " << bool_string(m & tio->c_cflag) \
<< " [" << bool_string(m & tio0->c_cflag) << "].\n"
#define pr4(s, m) \
cout << " " << s << " = " << bool_string(m & tio->c_lflag) \
<< " [" << bool_string(m & tio0->c_lflag) << "].\n"
#define pr5(s, ii) \
cout << " " << s << " = 0x" << (long)tio->c_cc[ii] \
<< " [0x" << (long)tio0->c_cc[ii] << "].\n";
pr0("c_iflag", c_iflag);
pr1("IGNBRK ", IGNBRK);
pr1("BRKINT ", BRKINT);
pr1("IGNPAR ", IGNPAR);
pr1("PARMRK ", PARMRK);
pr1("INPCK ", INPCK);
pr1("ISTRIP ", ISTRIP);
pr1("INLCR ", INLCR);
pr1("IGNCR ", IGNCR);
pr1("ICRNL ", ICRNL);
pr1("IUCLC ", IUCLC);
pr1("IXON ", IXON);
pr1("IXANY ", IXANY);
pr1("IXOFF ", IXOFF);
pr1("IMAXBEL ", IMAXBEL);
pr0("c_oflag", c_oflag);
pr2("OPOST ", OPOST);
pr2("OLCUC ", OLCUC);
pr2("ONLCR ", ONLCR);
pr2("OCRNL ", OCRNL);
pr2("ONOCR ", ONOCR);
pr2("ONLRET ", ONLRET);
pr2("OFILL ", OFILL);
pr2("OFDEL ", OFDEL);
pr2("NLDLY ", NLDLY);
cout << " CRDLY = 0x"
<< hex << ((CRDLY & tio->c_oflag) >> 9)
<< " [0x" << ((CRDLY & tio0->c_oflag) >> 9) << "].\n" << dec;
cout << " TABDLY = 0x"
<< hex << ((TABDLY & tio->c_oflag) >> 11)
<< " [0x" << ((TABDLY & tio0->c_oflag) >> 11) << "].\n" << dec;
pr2("BSDLY ", BSDLY);
pr2("VTDLY ", VTDLY);
pr2("FFDLY ", FFDLY);
pr0("c_cflag", c_cflag);
ulong cbaud = CBAUD & tio->c_cflag;
ulong cbaud0 = CBAUD & tio0->c_cflag;
cout << " CBAUD = 0x"
<< hex << cbaud << " [0x" << cbaud0 << "].\n" << dec;
baud_rate_print_diff(cbaud, cbaud0);
long csize = CSIZE & tio->c_cflag;
long csize0 = CSIZE & tio0->c_cflag;
cout << " CSIZE = 0x"
<< hex << (csize >> 4) << " [0x" << (csize0 >> 4) << "].\n" << dec;
int cs = 0;
int cs0 = 0;
switch(csize) {
case CS5: cs = 5; break;
case CS6: cs = 6; break;
case CS7: cs = 7; break;
case CS8: cs = 8; break;
} // End of switch(csize).
switch(csize0) {
case CS5: cs0 = 5; break;
case CS6: cs0 = 6; break;
case CS7: cs0 = 7; break;
case CS8: cs0 = 8; break;
} // End of switch(csize0).
cout << "Character size = " << hex << cs
<< " [" << cs0 << "] bits.\n" << dec;
pr3("CSTOPB ", CSTOPB);
pr3("CREAD ", CREAD);
pr3("PARENB ", PARENB);
pr3("PARODD ", PARODD);
pr3("HUPCL ", HUPCL);
pr3("CLOCAL ", CLOCAL);
ulong cibaud = CIBAUD & tio->c_cflag;
ulong cibaud0 = CIBAUD & tio0->c_cflag;
#ifndef linux
cout << " CIBAUD = 0x"
<< hex << (cibaud >> IBSHIFT)
<< " [0x" << (cibaud0 >> IBSHIFT) << "].\n" << dec;
cout << "Input baud rate:\n";
baud_rate_print_diff(cibaud >> IBSHIFT, cibaud0 >> IBSHIFT);
#endif
pr3("CRTSCTS ", CRTSCTS);
pr0("c_lflag", c_lflag);
pr4("ISIG ", ISIG);
pr4("ICANON ", ICANON);
pr4("XCASE ", XCASE);
pr4("ECHO ", ECHO);
pr4("ECHOE ", ECHOE);
pr4("ECHOK ", ECHOK);
pr4("ECHONL ", ECHONL);
pr4("NOFLSH ", NOFLSH);
pr4("TOSTOP ", TOSTOP);
pr4("ECHOCTL ", ECHOCTL);
pr4("ECHOPRT ", ECHOPRT);
pr4("ECHOKE ", ECHOKE);
pr4("FLUSHO ", FLUSHO);
pr4("PENDIN ", PENDIN);
pr4("IEXTEN ", IEXTEN);
pr5("VINTR ", 0);
pr5("VQUIT ", 1);
pr5("VERASE ", 2);
pr5("VKILL ", 3);
pr5("VEOF ", 4);
pr5("VEOL ", 5);
pr5("VEOL2 ", 6);
pr5("VSWTCH ", 7);
pr5("VSTART ", 8);
pr5("VSTOP ", 9);
pr5("VSUSP ", 10);
pr5(" ", 11);
pr5("VREPRINT", 12);
pr5("VDISCARD", 13);
pr5("VWERASE ", 14);
pr5("VLNEXT ", 15);
pr5(" ", 16);
} // End of function termios_print_diff.
/*------------------------------------------------------------------------------
If successful, return the fd value of the new channel. On failure, a negative
value is returned.
------------------------------------------------------------------------------*/
//----------------------//
// term_config //
//----------------------//
int term_config(bool_enum software_carrier_mode, bool_enum crtscts,
const char* dev_name,
ulong speed_mask, int trace) {
if (!dev_name)
dev_name = deft_dev_name;
if (speed_mask == 0)
speed_mask = deft_speed_mask;
// Open hf device driver:
if (trace >= 4)
cout << dev_name << ": opening device..." << endl;
int open_mode = O_RDWR;
if (software_carrier_mode)
open_mode |= O_NDELAY;
int fd = open(dev_name, open_mode);
if (fd < 0) {
cerr << "Failed to open device " << dev_name << DOTNL;
perror("term_config");
return -1;
}
if (trace >= 4)
cout << dev_name << ": opened device." << endl;
// Do the ioctls here:
if (software_carrier_mode) {
// Set the software DCD mode to "on":
int i = 1;
int ret = ioctl(fd, (int)TIOCSSOFTCAR, (caddr_t)&i);
if (ret < 0) {
cerr << "ioctl TIOCSSOFTCAR failed for device "
<< dev_name << DOTNL;
perror("term_config: ioctl TIOCSSOFTCAR");
return -1;
}
}
termios tio;
int ret = ioctl(fd, TCGETS, &tio);
if (ret < 0) {
cerr << "Ioctl failed for device " << dev_name << DOTNL;
perror("term_config");
return -1;
}
if (trace >= 4)
cout << dev_name << ": ioctl TCGETS succeeded.\n" << flush;
// Save the initial values:
termios tio0 = tio;
// Set up the TTY interface for clear 8-bit communication:
// Note; should get a better understanding of this, and do it properly!
// #define FORCE_SIMPLE_CONFIG
#if defined(SOLARIS) || defined(FORCE_SIMPLE_CONFIG)
tio.c_iflag = 0;
tio.c_oflag = 0;
tio.c_cflag = B9600|CS8|CREAD|CLOCAL;
if (crtscts)
tio.c_cflag |= CRTSCTS;
tio.c_lflag = 0;
#else
tio.c_iflag |= (IGNBRK|BRKINT|IGNPAR);
tio.c_iflag &= ~(INLCR|IGNCR|ICRNL|IUCLC|IXON|IXOFF|ISTRIP);
// tio.c_oflag &= ~(ONLCR|OCRNL|ONLRET|OPOST);
// Modify output baud rate:
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= speed_mask;
// Modify input baud rate:
#ifndef linux
tio.c_cflag &= ~CIBAUD;
tio.c_cflag |= (speed_mask << IBSHIFT) & CIBAUD;
#endif
// Change from 7-bit to 8-bit:
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS8;
// Turn on/off RTS-CTS:
tio.c_cflag |= (CLOCAL|HUPCL|CREAD);
if (crtscts)
tio.c_cflag |= CRTSCTS;
else
tio.c_cflag &= ~CRTSCTS;
// tio.c_cflag |= (CLOCAL|HUPCL);
// Turn off parity enable:
tio.c_cflag &= ~(PARENB);
// Turn off echo and canonical mode:
// tio.c_lflag &= ~(ISIG|ICANON|IEXTEN|ECHO);
// Get immediate return if >= VMIN bytes are available:
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 0;
#endif
// Send the new setup to the tty:
ret = ioctl(fd, TCSETS, &tio);
if (ret < 0 ) {
perror("term_config");
return -1;
}
if (trace >= 5) {
cout << dev_name << ": ioctl TCSETS succeeded.\n";
cout << "State of " << dev_name << " after [before] configuration:\n";
termios_print_diff(&tio, &tio0);
cout << flush;
}
return fd;
} // End of function term_config.
//----------------------//
// terminal::config //
//----------------------//
int terminal::config(const char* dev_name, ulong speed, int trace) {
int ret = term_config(software_carrier_mode,
crtscts, dev_name, speed, trace);
if (ret < 0)
return ret;
delete[] dev_path;
dev_path = new_strcpy(dev_name);
speed_mask = speed;
return ret;
} // End of function terminal::config.
#endif /* unix */