plptools
Loading...
Searching...
No Matches
mp_serial.c
Go to the documentation of this file.
1/*
2 // PLP - An implementation of the PSION link protocol
3 //
4 // The code in this file was written by Rudolf Koenig
5 // (rfkoenig@immd4.informatik.uni-erlangen.de). (from his p3nfs code)
6 // The Copyright remains his.
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 along
19 // along with this program; if not, see <https://www.gnu.org/licenses/>.
20 //
21 // e-mail philip.proudman@btinternet.com
22 */
23#include "config.h"
24
25#include <stdio.h>
26#include <fcntl.h>
27#include <unistd.h> /* for usleep() */
28#include <string.h> /* for bzero() */
29#include <termios.h>
30#if defined(linux) || defined(_IBMR2) || \
31 (defined(__APPLE__) && defined(__MACH__)) || \
32 defined(__NetBSD__) || defined(__FreeBSD__)
33#include <sys/ioctl.h> /* for ioctl() */
34#endif
35#include <errno.h>
36#ifdef sun
37#include <sys/ttold.h> /* sun has TIOCEXCL there */
38#endif
39#include <stdlib.h>
40
41#ifdef hpux
42#include <sys/termiox.h>
43#include <sys/modem.h>
44#endif
45
46#include "mp_serial.h"
47
48
49#ifdef __sgi
50#define CRTSCTS CNEW_RTSCTS
51#endif
52
53#ifndef O_NOCTTY
54#define O_NOCTTY 0
55#endif
56
57int
58init_serial(const char *dev, int speed, int debug)
59{
60 int fd, baud;
61 int uid, euid;
62 struct termios ti;
63#ifdef hpux
64 struct termiox tx;
65#endif
66 static struct baud {
67 int speed, baud;
68 } btable[] = {
69 { 9600, B9600 },
70#ifdef B19200
71 { 19200, B19200 },
72#else
73#ifdef EXTA
74 { 19200, EXTA },
75#endif
76#endif
77#ifdef B38400
78 { 38400, B38400 },
79#else
80#ifdef EXTB
81 { 38400, EXTB },
82#endif
83#endif
84#ifdef B57600
85 { 57600, B57600 },
86#endif
87#ifdef B115200
88 { 115200, B115200 },
89#endif
90 { 4800, B4800 },
91 { 2400, B2400 },
92 { 1200, B1200 },
93 { 300, B300 },
94 { 75, B75 },
95 { 50, B50 },
96 { 0, 0 }
97 }, *bptr;
98
99 if (speed) {
100 for (bptr = btable; bptr->speed; bptr++)
101 if (bptr->speed == speed)
102 break;
103 if (!bptr->baud) {
104 fprintf(stderr, "Cannot match selected speed %d\n", speed);
105 exit(1);
106 }
107 baud = bptr->baud;
108 } else
109 baud = 0;
110
111 if (debug)
112 printf("using %s...\n", dev);
113 euid = geteuid();
114 uid = getuid();
115
116#ifdef hpux
117#define seteuid(a) setresuid(-1, a, -1)
118#endif
119
120 if (seteuid(uid)) {
121 perror("seteuid");
122 exit(1);
123 }
124 if ((fd = open(dev, O_RDWR | O_NOCTTY, 0)) < 0) {
125 perror(dev);
126 exit(1);
127 }
128 if (seteuid(euid)) {
129 perror("seteuid back");
130 exit(1);
131 }
132 if (debug)
133 printf("open done\n");
134#ifdef TIOCEXCL
135 ioctl(fd, TIOCEXCL, (char *) 0); /* additional open() calls shall fail */
136#else
137 fprintf(stderr, "WARNING: opened %s non-exclusive!\n", dev);
138#endif
139
140 memset(&ti, 0, sizeof(struct termios));
141 ti.c_cflag = CS8 | HUPCL | CLOCAL | CREAD;
142#if defined(sun) || defined(linux) || defined(__sgi) || \
143 (defined(__APPLE__) && defined(__MACH__)) || \
144 defined(__NetBSD__) || defined(__FreeBSD__)
145 ti.c_cflag |= CRTSCTS;
146 ti.c_iflag = IGNBRK | IGNPAR;
147 ti.c_cc[VMIN] = 1;
148 ti.c_cc[VTIME] = 0;
149#endif
150 cfsetispeed(&ti, baud);
151 cfsetospeed(&ti, baud);
152
153 if (tcsetattr(fd, TCSADRAIN, &ti) < 0)
154 perror("tcsetattr TCSADRAIN");
155
156#ifdef hpux
157 bzero(&tx, sizeof(struct termiox));
158 tx.x_hflag = RTSXOFF | CTSXON;
159 if (ioctl(fd, TCSETXW, &tx) < 0)
160 perror("TCSETXW");
161#endif
162#if defined(_IBMR2)
163 ioctl(fd, TXDELCD, "dtr");
164 ioctl(fd, TXDELCD, "xon");
165 ioctl(fd, TXADDCD, "rts"); /* That's how AIX does CRTSCTS */
166#endif
167
168 return fd;
169}
170
171void
172ser_exit(int fd)
173{
174 struct termios ti;
175
176#ifdef TIOCNXCL
177 ioctl(fd, TIOCNXCL, (char *) 0);
178#endif
179 if (tcgetattr(fd, &ti) < 0)
180 perror("tcgetattr");
181 ti.c_cflag &= ~CRTSCTS;
182 if (tcsetattr(fd, TCSANOW, &ti) < 0)
183 perror("tcsetattr");
184 (void) close(fd);
185}
int debug
Definition: fuse.c:47
#define O_NOCTTY
Definition: mp_serial.c:54
void ser_exit(int fd)
Definition: mp_serial.c:172
int init_serial(const char *dev, int speed, int debug)
Definition: mp_serial.c:58