annotate rvinterf/lowlevel/openport.c @ 1015:9ced8e13cf91

fc-shell tch-dl command implementation: brown paper bag
author Mychaela Falconia <falcon@ivan.Harhan.ORG>
date Sun, 20 Mar 2016 23:07:46 +0000
parents f42854da4563
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
173
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This module takes care of opening the serial port and setting the
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 * proper "raw" termios modes, including the baud rate.
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4 */
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/types.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/file.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/ioctl.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <termios.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdio.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <stdlib.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <string.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include <strings.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include <unistd.h>
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 int target_fd;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 char *baudrate_name = "115200";
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 static struct baudrate {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20 char *name;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 speed_t termios_code;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 } baud_rate_table[] = {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 {"19200", B19200},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 {"38400", B38400},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 {"57600", B57600},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 {"115200", B115200},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 /* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 {"203125", B230400},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 {"406250", B460800},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 {"812500", B921600},
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 /* table search terminator */
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 {NULL, B0}
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 };
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 static speed_t
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 get_baud_rate()
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 struct baudrate *br;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40 for (br = baud_rate_table; br->name; br++)
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 if (!strcmp(br->name, baudrate_name))
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 break;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 if (!br->name) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 fprintf(stderr, "baud rate \"%s\" unknown/unsupported\n",
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 baudrate_name);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 exit(1);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 return br->termios_code;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 open_target_serial(ttydev)
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 char *ttydev;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54 struct termios target_termios;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 speed_t br_code;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 br_code = get_baud_rate();
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58 target_fd = open(ttydev, O_RDWR|O_NONBLOCK);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 if (target_fd < 0) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 perror(ttydev);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 exit(1);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 target_termios.c_iflag = IGNBRK;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 target_termios.c_oflag = 0;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 target_termios.c_lflag = 0;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 target_termios.c_cc[VMIN] = 1;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 target_termios.c_cc[VTIME] = 0;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 cfsetispeed(&target_termios, br_code);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 cfsetospeed(&target_termios, br_code);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 perror("initial tcsetattr on target");
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 exit(1);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 return 0;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 }
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 set_serial_nonblock(state)
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 int state;
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 {
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 ioctl(target_fd, FIONBIO, &state);
f42854da4563 rvinterf: beginning of refactoring
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 }