-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.c
305 lines (265 loc) · 6.91 KB
/
support.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
/*
* FreeWX - logger for Davis weather stations
*
* Copyright 2003-2019 Michael Galassi, all rights reserved.
*
* This code may be re-distributed under the terms and conditions of
* the BSD 2 clause license.
*
* This code is loosely based on Alan Batie's wiz3d.
*
* All questions & comments should be directed to me at michael at
* galassi dot us.
*/
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>
#include <sys/ioctl.h>
#ifndef IF_SPEED
#define IF_SPEED 19200 /* default, override on cc command line */
#endif /*IF_SPEED*/
#define MAX_TIMEOUT 30 /* the longest time station can take to xmit */
#define MAX_READ 256 /* the longest data station can xmit */
#define ACK 0x06 /* station ACKs commands with this */
/* used for debugging */
#define LINELEN 80 /* length of a line */
#define CHARLEN 5 /* width to display one char */
static void __inline
dumpbyte(unsigned char c, char *buf)
{
#if 0
if (isprint(c)) {
sprintf(buf, " %c ", c);
} else {
#endif
sprintf(buf, "0x%02x ", c);
#if 0
}
#endif
}
void
dumpbuf(FILE *stream, unsigned char *p, size_t len)
{
int i;
char buf[LINELEN+2]; /* text, newline, & null terminator */
i = 0;
while (len-- > 0) {
dumpbyte(*p++, &buf[i*CHARLEN]);
if (++i >= LINELEN/CHARLEN || len == 0) {
strcat(&buf[i*CHARLEN], "\n");
fputs(&buf[0], stream);
i = 0;
}
}
return;
}
int
wxopen(char *devname)
{
struct termios termios;
char *wxdev;
int wxfd;
/* validate (and maybe complete) device name */
if (*devname == '/') {
wxdev = devname;
} else {
wxdev = alloca(strlen(devname)+sizeof("/dev/")+1);
strcpy(wxdev, "/dev/");
strcat(wxdev, devname);
}
#ifdef DEBUG_WXOPEN
fprintf(stderr, "wxopen - device - %s\n", wxdev);
#endif /*DEBUG_WXOPEN*/
if ((wxfd = open(wxdev, O_RDWR|O_NONBLOCK)) == -1) {
perror("wxopen - open");
return -1;
}
/* set for our use only */
if (ioctl(wxfd, TIOCEXCL, NULL) == -1) {
perror("wxopen - ioctl(TIOCEXCL)");
(void)close(wxfd);
return -1;
}
/*
* clear NONBLOCK flag as it should no longer be needed
*/
if (fcntl(wxfd, F_SETFL, 0) == -1) {
perror("wxopen - fcntl(F_SETFL)");
(void)close(wxfd);
return -1;
}
if (tcgetattr(wxfd, &termios) != 0) {
perror("wxopen - tcgetattr");
(void)close(wxfd);
return -1;
}
cfmakeraw(&termios); /* redundant */
termios.c_iflag = IGNBRK;
termios.c_oflag = 0;
termios.c_cflag = CS8|CREAD|CLOCAL;
termios.c_lflag = NOKERNINFO;
termios.c_cc[VMIN] = 0;
termios.c_cc[VTIME] = 0;
cfsetspeed(&termios, IF_SPEED);
if (tcsetattr(wxfd, TCSAFLUSH, &termios) == -1) {
perror("wxopen - tcsetattr");
(void)close(wxfd);
return -1;
}
#ifdef DEBUG_WXOPEN
fprintf(stderr, "wxopen complete\n");
#endif /*DEBUG_WXOPEN*/
return wxfd;
}
int
wxsettimeout(int fd, int timeout)
{
struct termios termios;
if (tcgetattr(fd, &termios) == -1) {
perror("wxsettimeout - tcgetattr");
return -1;
}
termios.c_cc[VMIN] = 0;
termios.c_cc[VTIME] = timeout*10;
if (tcsetattr(fd, TCSANOW|TCSASOFT, &termios) == -1) {
perror("wxsettimeout - tcsetattr");
return -1;
}
return 0;
}
int
wxread(int fd, void *buf, size_t len, int timeout)
{
time_t expire;
ssize_t remaining;
ssize_t rc;
char *bufp;
if (timeout <= 0 || timeout > MAX_TIMEOUT) {
fprintf(stderr, "wxread - timeout %d out of range\n", timeout);
return -1;
}
if (len == 0 || len > MAX_READ) {
fprintf(stderr, "wxread - length %zu out of range\n", len);
return -1;
}
expire = time((time_t *)NULL) + timeout;
remaining = len;
bufp = (char *)buf;
while (remaining > 0 && timeout > 0) {
if (wxsettimeout(fd, timeout) == -1) {
fprintf(stderr, "wxread - failed to set timeout\n");
return -1;
}
if ((rc = read(fd, bufp, remaining)) != remaining) {
if (rc == -1) {
perror("wxread - read");
return -1;
}
#ifdef DEBUG_WXREAD
fprintf(stderr, "wxread - read got %zd expected %zd\n",
rc, remaining);
#endif /*DEBUG_WXREAD*/
}
remaining -= rc;
bufp += rc;
timeout = expire - time((time_t *)NULL);
}
#ifdef DEBUG_WXREAD
dumpbuf(stderr, buf, len - remaining);
#endif /*DEBUG_WXREAD*/
return (len - remaining);
}
int
wxflush(int fd)
{
struct termios termios;
if (tcgetattr(fd, &termios) == -1) {
perror("wxflush - tcgetattr");
return -1;
}
if (tcsetattr(fd, TCSAFLUSH|TCSASOFT, &termios) == -1) {
perror("wxflush - tcsetattr");
return -1;
}
return 0;
}
int
wxwakeup(int fd)
{
ssize_t rc;
char resp[2];
if (wxflush(fd) != 0) {
fprintf(stderr, "wxwakeup - flush failed\n");
return -1;
}
if ((rc = write(fd, &"\n", 1)) != 1) {
if (rc == -1) {
perror("wxwakeup - write");
return -1;
}
fprintf(stderr, "wxwakeup - newline send failed\n");
return -1;
}
if (wxread(fd, (void *)&resp, sizeof(resp), 5) == -1) {
fprintf(stderr, "wxwakeup - wxread failed\n");
return -1;
}
/* Davis doesn't make it clear which to expect */
if ((resp[0] != '\r' && resp[1] != '\n') &&
(resp[0] != '\n' && resp[1] != '\r')) {
#ifdef DEBUG_WXWAKEUP
fprintf(stderr, "wxwakeup - didn't get expected bytes\n"
"wxwakeup - got 0x%x 0x%x\n",
resp[0] & 0xff, resp[1] & 0xff);
#endif /*DEBUG_WXWAKEUP*/
return -1;
}
return 0;
}
int
wxgetack(int fd)
{
int i;
unsigned char ackbuf;
i = 0;
do {
if (wxread(fd, (void *)&ackbuf, sizeof(ackbuf), 1) == -1) {
fprintf(stderr, "wxgetack - wxread failed\n");
return -1;
}
if (i++ >= 5) {
fprintf(stderr, "wxgetack - failing after %d attempts\n", i);
return -1;
}
} while (ackbuf != ACK);
return 0;
}
int
wxcmd(int fd, char *cmd)
{
size_t len;
ssize_t rc;
len = strlen(cmd);
if ((rc = write(fd, cmd, len)) == -1) {
perror("wxcmd - write");
return -1;
}
if (rc != (ssize_t)len) {
fprintf(stderr, "wxcmd - write expected %zu, got %zd\n", len, rc);
return -1;
}
if (wxgetack(fd) != 0) {
fprintf(stderr, "wxcmd - failed getting cmd ack\n");
return -1;
}
#ifdef DEBUG_WXCMD
fprintf(stderr, "wxcmd - sent command %s\n", cmd);
#endif /*DEBUG_WXCMD*/
return 0;
}