-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathosint_linux.c
524 lines (480 loc) · 13.3 KB
/
osint_linux.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/**
* @file osint_linux.c
*
* Serial I/O functions used by PLoadLib.c
*
* Copyright (c) 2009 by John Steven Denson
* Modified in 2011 by David Michael Betz
* Modified in 2019-2022 by Eric R. Smith
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/timeb.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <limits.h>
#include <signal.h>
#include <time.h>
#ifdef MACOSX
#include <IOKit/serial/ioss.h>
#endif
#include "osint.h"
typedef int HANDLE;
static HANDLE hSerial = -1;
static struct termios old_sparm;
static unsigned long last_baud = -1;
static char last_port[PATH_MAX];
extern int ignoreEof; /* in main file */
/* normally we use DTR for reset but setting this variable to non-zero will use RTS instead */
static int use_rts_for_reset = 0;
void serial_use_rts_for_reset(int use_rts)
{
use_rts_for_reset = use_rts;
}
static void chk(char *fun, int sts)
{
if (sts != 0)
printf("%s failed\n", fun);
}
//
// on Linux, changing baud messes with the DTR/RTS lines, which
// resets the P2 board :(. This function used to exist to force the
// loader baud to match the user baud. We work around the problem
// now by re-opening the handle, but this legacy code is left over.
//
int get_loader_baud(int ubaud, int lbaud)
{
return lbaud;
}
static void sigint_handler(int signum)
{
serial_done();
exit(1);
}
#if !defined(MACOSX)
// MACOSX uses a different method to set baud
static int set_baud(struct termios *sparm, unsigned long baud)
{
int tbaud = 0;
switch(baud) {
case 0: // default
tbaud = B115200;
break;
#ifdef B2000000
case 2000000:
tbaud = B2000000;
break;
#endif
#ifdef B921600
case 921600:
tbaud = B921600;
break;
#endif
#ifdef B576000
case 576000:
tbaud = B576000;
break;
#endif
#ifdef B500000
case 500000:
tbaud = B500000;
break;
#endif
#ifdef B460800
case 460800:
tbaud = B460800;
break;
#endif
#ifdef B230400
case 230400:
tbaud = B230400;
break;
#endif
case 115200:
tbaud = B115200;
break;
case 57600:
tbaud = B57600;
break;
case 38400:
tbaud = B38400;
break;
case 19200:
tbaud = B19200;
break;
case 9600:
tbaud = B9600;
break;
default:
tbaud = baud;
printf("Unsupported baudrate %lu. Use ", baud);
#ifdef B921600
printf("921600, ");
#endif
#ifdef B576000
printf("576000, ");
#endif
#ifdef B500000
printf("500000, ");
#endif
#ifdef B460800
printf("460800, ");
#endif
#ifdef B230400
printf("230400, ");
#endif
printf("115200, 57600, 38400, 19200, or 9600\n");
serial_done();
promptexit(2);
break;
}
/* set raw input */
chk("cfsetispeed", cfsetispeed(sparm, tbaud));
chk("cfsetospeed", cfsetospeed(sparm, tbaud));
return 1;
}
#endif /* !MACOSX */
/**
* open serial port
* @param port - COMn port name
* @param baud - baud rate
* @returns 1 for success and 0 for failure
*/
int serial_init(const char* port, unsigned long baud)
{
struct termios sparm;
/* open the port */
#if defined(MACOSX)
speed_t speed = (speed_t) baud;
hSerial = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
#else
hSerial = open(port, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
#endif
if(hSerial == -1) {
//printf("error: opening '%s' -- %s\n", port, strerror(errno));
return 0;
}
last_baud = baud;
strncpy(last_port, port, PATH_MAX-1);
signal(SIGINT, sigint_handler);
fcntl(hSerial, F_SETFL, 0);
/* get the current options */
chk("tcgetattr", tcgetattr(hSerial, &old_sparm));
sparm = old_sparm;
/* set raw input */
cfmakeraw(&sparm);
sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1;
#if defined(MACOSX)
cfsetospeed(&sparm, B9600); // dummy speed, overridden later
cfsetispeed(&sparm, B9600); // dummy speed
#else
if (!set_baud(&sparm, baud)) {
close(hSerial);
printf("failure setting baud %ld\n", (long)baud);
return 0;
}
#endif
/* set the options */
chk("tcsetattr", tcsetattr(hSerial, TCSANOW, &sparm));
#ifdef MACOSX
if (ioctl(hSerial, IOSSIOSPEED, &speed) != 0)
{
close(hSerial);
printf("failure setting speed %ld\n", (long)baud);
return 0;
}
#endif
chk("tcflush", tcflush(hSerial, TCIFLUSH));
return 1;
}
/**
* change the baud rate of the serial port
* @param baud - baud rate
* @returns 1 for success and 0 for failure
* On Linux this gets tricky, changiing baud on an already open
* handle drops DTR and resets the board. So we have to initialize
* new connection and then close the old one.
*/
int serial_baud(unsigned long baud)
{
if (baud != last_baud) {
HANDLE oldSerial = hSerial;
if (!serial_init(last_port, baud)) {
printf("serial_init of %s failed\n", last_port);
promptexit(1);
}
close(oldSerial);
}
return 1;
}
/**
* flush all input
*/
int flush_input(void)
{
return tcflush(hSerial, TCIFLUSH);
}
/**
* wait for tx buffer to be empty
*/
int wait_drain(void)
{
return tcdrain(hSerial);
}
/**
* close serial port
*/
void serial_done(void)
{
if (hSerial != -1) {
tcflush(hSerial, TCIOFLUSH);
//tcsetattr(hSerial, TCSANOW, &old_sparm);
ioctl(hSerial, TIOCNXCL);
close(hSerial);
hSerial = -1;
}
}
/**
* receive a buffer
* @param buff - char pointer to buffer
* @param n - number of bytes in buffer to read
* @returns number of bytes read
*/
int rx(uint8_t* buff, int n)
{
ssize_t bytes = read(hSerial, buff, n);
if(bytes < 1) {
printf("Error reading port: %d\n", (int)bytes);
return 0;
}
return (int)bytes;
}
/**
* transmit a buffer
* @param buff - char pointer to buffer
* @param n - number of bytes in buffer to send
* @returns zero on failure
*/
int tx(uint8_t* buff, int n)
{
ssize_t bytes;
#if 0
int j = 0;
while(j < n) {
printf("%02x ",buff[j++]);
}
printf("tx %d byte(s)\n",n);
#endif
bytes = write(hSerial, buff, n);
if(bytes != n) {
printf("Error writing port\n");
return 0;
}
return (int)bytes;
}
/**
* receive a buffer with a timeout
* @param buff - char pointer to buffer
* @param n - number of bytes in buffer to read
* @param timeout - timeout in milliseconds
* @returns number of bytes read or SERIAL_TIMEOUT
*/
int rx_timeout(uint8_t* buff, int n, int timeout)
{
ssize_t bytes = 0;
struct timeval toval;
fd_set set;
FD_ZERO(&set);
FD_SET(hSerial, &set);
toval.tv_sec = timeout / 1000;
toval.tv_usec = (timeout % 1000) * 1000;
if (select(hSerial + 1, &set, NULL, NULL, &toval) > 0) {
if (FD_ISSET(hSerial, &set))
bytes = read(hSerial, buff, n);
}
return (int)(bytes > 0 ? bytes : SERIAL_TIMEOUT);
}
/**
* hwreset ... resets Propeller hardware using DTR or RTS
* @param sparm - pointer to DCB serial control struct
* @returns void
*/
void hwreset(void)
{
int cmd = use_rts_for_reset ? TIOCM_RTS : TIOCM_DTR;
ioctl(hSerial, TIOCMBIS, &cmd); /* assert bit */
msleep(2);
ioctl(hSerial, TIOCMBIC, &cmd); /* clear bit */
msleep(2);
ioctl(hSerial, TIOCMBIS, &cmd); /* assert bit */
msleep(2);
tcflush(hSerial, TCIFLUSH);
}
/**
* sleep for ms milliseconds
* @param ms - time to wait in milliseconds
*/
void msleep(int ms)
{
#if 0
volatile struct timeb t0, t1;
do {
ftime((struct timeb*)&t0);
do {
ftime((struct timeb*)&t1);
} while (t1.millitm == t0.millitm);
} while(ms-- > 0);
#else
usleep(ms * 1000);
#endif
}
/**
* simple terminal emulator
*/
void terminal_mode(int runterm_mode, int pst_mode)
{
struct termios oldt, newt;
char buf[128], realbuf[256]; // double in case buf is filled with \r in PST mode
ssize_t cnt;
fd_set set;
int exit_char = 0xdead; /* not a valid character */
int sawexit_char = 0;
int sawexit_valid = 0;
int exitcode = 0;
int continue_terminal = 1;
int check_for_exit = runterm_mode != 0;
int check_for_files = runterm_mode & 2;
if (isatty(STDIN_FILENO)) {
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
cfmakeraw(&newt);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}
if (check_for_exit)
{
exit_char = 0xff;
}
#if 0
/* make it possible to detect breaks */
tcgetattr(hSerial, &newt);
newt.c_iflag &= ~IGNBRK;
newt.c_iflag |= PARMRK;
tcsetattr(hSerial, TCSANOW, &newt);
#endif
do {
FD_ZERO(&set);
FD_SET(hSerial, &set);
FD_SET(STDIN_FILENO, &set);
if (select(hSerial + 1, &set, NULL, NULL, NULL) > 0) {
if (FD_ISSET(hSerial, &set)) {
if ((cnt = read(hSerial, buf, sizeof(buf))) > 0) {
int i;
// check for breaks
ssize_t realbytes = 0;
for (i = 0; i < cnt; i++) {
if (sawexit_valid)
{
exitcode = buf[i];
//printf("exitcode: %02x\n", buf[i]);
continue_terminal = 0;
}
else if (sawexit_char) {
//printf("exitchar 2: %02x\n", buf[i]);
if (buf[i] == 0) {
sawexit_valid = 1;
} else if (buf[i] == 1 && check_for_files) {
int r = u9fs_process(cnt - (i+1), &buf[i+1]);
// printf("u9fs_process(%d) returned: %d\n", cnt - (i+1), r);
i += (r-1);
sawexit_char = 0;
break;
} else {
realbuf[realbytes++] = exit_char;
realbuf[realbytes++] = buf[i];
sawexit_char = 0;
}
} else if (((int)buf[i] & 0xff) == exit_char) {
//printf("exitchar: %02x\n", buf[i]);
sawexit_char = 1;
} else {
realbuf[realbytes++] = buf[i];
if (pst_mode && buf[i] == '\r')
realbuf[realbytes++] = '\n';
}
}
if (realbytes > 0) {
write(fileno(stdout), realbuf, realbytes);
}
}
}
if (FD_ISSET(STDIN_FILENO, &set)) {
cnt = read(STDIN_FILENO, buf, sizeof(buf));
if (cnt == 0 && !ignoreEof) {
// EOF on stdin: bail
waitAtExit = 0;
goto done;
}
if (cnt > 0) {
int i;
for (i = 0; i < cnt; ++i) {
//printf("%02x\n", buf[i]);
if (buf[i] == EXIT_CHAR0 || buf[i] == EXIT_CHAR1) {
waitAtExit = 0; // user chose to quit
goto done;
}
}
write(hSerial, buf, cnt);
}
}
}
} while (continue_terminal);
done:
if (isatty(STDIN_FILENO)) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
if (sawexit_valid)
{
promptexit(exitcode);
}
}
unsigned long long
elapsedms(void)
{
struct timeval t;
if (!gettimeofday(&t, NULL)) {
// how could this fail??
return time(NULL) * 1000ULL;
}
return 1000 * (unsigned long long)t.tv_sec + ((unsigned long long)t.tv_usec/1000);
}