-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermios.jai
319 lines (273 loc) · 8.1 KB
/
termios.jai
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
#scope_module
tcflag_t :: u64;
speed_t :: u64;
cc_t :: u8;
CRTSCTS :: 0x80000000;
ccflags :: enum {
B0;
B50;
B75;
B110;
B134;
B150;
B200;
B300;
B600;
B1200;
B1800;
B2400;
B4800;
B9600;
B19200;
B38400;
}
c_cc_chars :: enum {
VINTR;
VQUIT;
VERASE;
VKILL;
VEOF;
VTIME;
VMIN;
VSWTC;
VSTART;
VSTOP;
VSUSP;
VEOL;
VREPRINT;
VDISCARD;
VWERASE;
VLNEXT;
VEOL2;
}
c_iflag_bits :: enum_flags {
IGNBRK; /* Ignore break condition. */
BRKINT; /* Signal interrupt on break. */
IGNPAR; /* Ignore characters with parity errors. */
PARMRK; /* Mark parity and framing errors. */
INPCK; /* Enable input parity check. */
ISTRIP; /* Strip 8th bit off characters. */
INLCR; /* Map NL to CR on input. */
IGNCR; /* Ignore CR. */
ICRNL; /* Map CR to NL on input. */
IUCLC; /* Map uppercase characters to lowercase on input
(not in POSIX). */
IXON; /* Enable start/stop output control. */
IXANY; /* Enable any character to restart output. */
IXOFF; /* Enable start/stop input control. */
IMAXBEL;/* Ring bell when input queue is full
(not in POSIX). */
IUTF8; /* Input is UTF8 (not in POSIX). */
}
c_oflag_bits :: enum_flags {
OPOST :: 1; /* Post-process output. */
OLCUC; /* Map lowercase characters to uppercase on output.
(not in POSIX). */
ONLCR; /* Map NL to CR-NL on output. */
OCRNL; /* Map CR to NL on output. */
ONOCR; /* No CR output at column 0. */
ONLRET; /* NL performs CR function. */
OFILL; /* Use fill characters for delay. */
OFDEL; /* Fill is DEL. */
VTDLY :: 0x4000; /* Select vertical-tab delays: */
VT0 :: 0; /* Vertical-tab delay type 0. */
VT1 :: 0x4000; /* Vertical-tab delay type 1. */
}
c_cflag_bits :: enum #specified {
CSIZE :: 0x030;
CS5 :: 0x000;
CS6 :: 0x010;
CS7 :: 0x020;
CS8 :: 0x030;
CSTOPB :: 0x040;
CREAD :: 0x080;
PARENB :: 0x100;
PARODD :: 0x200;
HUPCL :: 0x400;
CLOCAL :: 0x800;
}
c_lflag_bits :: enum_flags {
ISIG :: 1; /* Enable signals. */
ICANON; /* Canonical input (erase and kill processing). */
XCASE;
ECHO; /* Enable echo. */
ECHOE; /* Echo erase character as error-correcting
backspace. */
ECHOK; /* Echo KILL. */
ECHONL; /* Echo NL. */
NOFLSH; /* Disable flush after interrupt or quit. */
TOSTOP; /* Send SIGTTOU for background output. */
IEXTEN :: 0x8000; /* Enable implementation-defined input
processing. */
}
baud_rate :: enum {
B57600 :: 0x1001;
B115200;
B230400;
B460800;
B500000;
B576000;
B921600;
B1000000;
B1152000;
B1500000;
B2000000;
B2500000;
B3000000;
B3500000;
B4000000;
}
tcflow_flags :: enum {
TCOOFF;
TCOON;
TCIOFF;
TCION;
}
tcflush_flags :: enum {
TCIFLUSH;
TCOFLUSH;
TCIOFLUSH;
}
tcsetaddr_flags :: enum {
TCSANOW;
TCSADRAIN;
TCSAFLUSH;
}
NCCS :: 32;
termios :: struct {
c_iflag: tcflag_t; /* input modes */
c_oflag: tcflag_t; /* output modes */
c_cflag: tcflag_t; /* control modes */
c_lflag: tcflag_t; /* local modes */
c_line: cc_t;
c_cc: [NCCS]cc_t; /* special characters */
}
#scope_file
libc :: #system_library "libc";
#scope_module
serial_config :: struct {
br: baud_rate;
parity: bool;
two_stop_bits: bool;
bits: c_cflag_bits;
hardware_flow_control: bool;
software_flow_control: bool;
canonical: bool; // does a newline mean we get a read
echo: bool;
raw: bool;
minimum_characters: u8;
timeout: u8;
}
set_interface_attributes :: (fd: int, config: *serial_config) -> bool {
tty: termios;
if (tcgetattr(fd, *tty)) {
perror("tcgetattr");
return false;
}
using c_cflag_bits;
using c_iflag_bits;
using c_lflag_bits;
using c_oflag_bits;
using c_cc_chars;
using tcflush_flags;
using tcsetaddr_flags;
speed := config.br;
set_flag :: (flag: *tcflag_t, val: $T) {
<<flag |= xx val;
}
clear_flag :: (flag: *tcflag_t, val: $T) {
<<flag &= xx ~val;
}
cfsetospeed (*tty, xx speed);
cfsetispeed (*tty, xx speed);
if config.parity {
set_flag(*tty.c_cflag, PARENB);
} else {
clear_flag(*tty.c_cflag, PARENB);
}
if config.two_stop_bits {
set_flag(*tty.c_cflag, CSTOPB);
} else {
clear_flag(*tty.c_cflag, CSTOPB);
}
clear_flag(*tty.c_cflag, CSIZE);
if config.bits == {
case CS5;
#through;
case CS6;
#through;
case CS7;
#through;
case CS8;
set_flag(*tty.c_cflag, CS8);
case;
print("invalid bits flag passed to serial port config");
assert(false);
}
if config.hardware_flow_control {
print("enable hardware flow control\n");
tty.c_cflag |= xx CRTSCTS;
} else {
print("disable hardware flow control\n");
tty.c_cflag &= xx ~CRTSCTS;
}
// this is pretty much guranteed to be set
tty.c_cflag |= xx (CREAD | CLOCAL);
if config.canonical {
set_flag(*tty.c_lflag, ICANON);
} else {
clear_flag(*tty.c_lflag, ICANON);
}
if config.echo {
set_flag(*tty.c_lflag, ECHO);
set_flag(*tty.c_lflag, ECHOE);
set_flag(*tty.c_lflag, ECHONL);
} else {
clear_flag(*tty.c_lflag, ECHO);
clear_flag(*tty.c_lflag, ECHOE);
clear_flag(*tty.c_lflag, ECHONL);
}
// this is always off for serial
tty.c_lflag &= xx ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
if config.software_flow_control {
print("enable software flow control\n");
tty.c_iflag |= xx (IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
} else {
print("disable software flow control\n");
tty.c_iflag &= xx ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
}
if config.raw {
// just raw data, no special handling of bytes for input and output
tty.c_iflag &= xx ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL);
tty.c_oflag &= xx ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= xx ~ONLCR; // Prevent conversion of newline to carriage return/line feed
cfmakeraw(*tty);
}
/*
VMIN = 0, VTIME = 0: No blocking, return immediately with what is available
VMIN > 0, VTIME = 0: This will make read() always wait for bytes (exactly how many is determined by VMIN), so read() could block indefinitely.
VMIN = 0, VTIME > 0: This is a blocking read of any number of chars with a maximum timeout (given by VTIME). read() will block until either any amount of data is available, or the timeout occurs. This happens to be my favourite mode (and the one I use the most).
VMIN > 0, VTIME > 0: Block until either VMIN characters have been received, or VTIME after first character has elapsed. Note that the timeout for VTIME does not begin until the first character is received.
*/
tty.c_cc[VMIN] = xx config.minimum_characters;
tty.c_cc[VTIME] = xx config.timeout;
tcflush(fd, xx TCIFLUSH);
if (tcsetattr(fd, xx TCSANOW, *tty)) {
perror("tcsetattr failed");
return false;
}
return true;
}
tcgetattr :: (fd: int, termios_p: *termios) -> int #foreign libc;
tcsetattr :: (fd: int, optional_actions: int,
termios_p: *termios) -> int #foreign libc;
tcsendbreak :: (fd: int, duration: int) -> int #foreign libc;
tcdrain :: (fd: int) -> int #foreign libc;
tcflush :: (fd: int, queue_selector: int) -> int #foreign libc;
tcflow :: (fd: int, action: int) -> int #foreign libc;
cfmakeraw :: (termios_p: *termios) #foreign libc;
cfgetispeed :: (termios_p: *termios) -> speed_t #foreign libc;
cfgetospeed :: (termios_p: *termios) -> speed_t #foreign libc;
cfsetispeed :: (termios_p: *termios, speed: speed_t) -> int #foreign libc;
cfsetospeed :: (termios_p: *termios, speed: speed_t) -> int #foreign libc;
cfsetspeed :: (termios_p: *termios, speed: speed_t) -> int #foreign libc;