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