blob: 709f8cef78e424184e63962be172a08b6d53295d [file] [log] [blame]
wdenk6dd652f2003-06-19 23:40:20 +00001/*
2 * (C) Copyright 2000
3 * Murray Jensen <Murray.Jensen@csiro.au>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk6dd652f2003-06-19 23:40:20 +00006 */
7
wdenkbca52d62001-10-15 12:12:42 +00008#include <unistd.h>
9#include <string.h>
10#include <fcntl.h>
11#include <sys/time.h>
12#include "serial.h"
13
14#if defined(__sun__) || \
15 defined(__OpenBSD__) || \
16 defined(__FreeBSD__) || \
17 defined(__NetBSD__) || \
18 defined(__APPLE__)
19static struct termios tios = { BRKINT, 0, B115200|CS8|CREAD, 0, { 0 } };
20#else
21static struct termios tios = { BRKINT, 0, B115200|CS8|CREAD, 0, 0 };
22#endif
23
24static struct speedmap {
25 char *str;
26 speed_t val;
27} speedmap[] = {
28 { "50", B50 }, { "75", B75 }, { "110", B110 },
29 { "134", B134 }, { "150", B150 }, { "200", B200 },
30 { "300", B300 }, { "600", B600 }, { "1200", B1200 },
31 { "1800", B1800 }, { "2400", B2400 }, { "4800", B4800 },
32 { "9600", B9600 }, { "19200", B19200 }, { "38400", B38400 },
33 { "57600", B57600 },
34#ifdef B76800
35 { "76800", B76800 },
36#endif
37 { "115200", B115200 },
38#ifdef B153600
39 { "153600", B153600 },
40#endif
41 { "230400", B230400 },
42#ifdef B307200
43 { "307200", B307200 },
44#endif
45#ifdef B460800
46 { "460800", B460800 }
47#endif
48};
49static int nspeeds = sizeof speedmap / sizeof speedmap[0];
50
51speed_t
52cvtspeed(char *str)
53{
54 struct speedmap *smp = speedmap, *esmp = &speedmap[nspeeds];
55
56 while (smp < esmp) {
57 if (strcmp(str, smp->str) == 0)
58 return (smp->val);
59 smp++;
60 }
61 return B0;
62}
63
64int
65serialopen(char *device, speed_t speed)
66{
67 int fd;
68
69 if (cfsetospeed(&tios, speed) < 0)
70 return -1;
71
72 if ((fd = open(device, O_RDWR)) < 0)
73 return -1;
74
75 if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
76 (void)close(fd);
77 return -1;
78 }
79
80 return fd;
81}
82
83int
84serialreadchar(int fd, int timeout)
85{
86 fd_set fds;
87 struct timeval tv;
88 int n;
89 char ch;
90
91 tv.tv_sec = timeout;
92 tv.tv_usec = 0;
93
94 FD_ZERO(&fds);
95 FD_SET(fd, &fds);
96
97 /* this is a fucking horrible quick hack - fix this */
98
99 if ((n = select(fd + 1, &fds, 0, 0, &tv)) < 0)
100 return SERIAL_ERROR;
101
102 if (n == 0)
103 return SERIAL_TIMEOUT;
104
105 if ((n = read(fd, &ch, 1)) < 0)
106 return SERIAL_ERROR;
107
108 if (n == 0)
109 return SERIAL_EOF;
110
111 return ch;
112}
113
114int
115serialwrite(int fd, char *buf, int len)
116{
117 int n;
118
119 do {
120 n = write(fd, buf, len);
121 if (n < 0)
122 return 1;
123 len -= n;
124 buf += n;
125 } while (len > 0);
126 return 0;
127}
128
129int
130serialclose(int fd)
131{
132 return close(fd);
133}