-
Notifications
You must be signed in to change notification settings - Fork 1
/
fhz.c
230 lines (187 loc) · 4.44 KB
/
fhz.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
/*
* fhz2mqtt, a FHZ to MQTT bridge
*
* Copyright (c) Ralf Ramsauer, 2018
*
* Authors:
* Ralf Ramsauer <[email protected]>
*
* This work is licensed under the terms of the GNU GPL, version 2. See
* the COPYING file in the top-level directory.
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include "fhz.h"
#define FHZ_MAGIC 0x81
#define BAUDRATE B9600
#ifdef DEBUG
static inline void hexdump(const unsigned char *data, size_t length)
{
int __i;
for (__i = 0; __i < length; __i++)
printf("%02X ", data[__i]);
printf("\n");
}
#else
#define hexdump(...)
#endif
static int fhz_receive(int fd, struct payload *payload)
{
unsigned char buffer[256 + 2];
unsigned char *payload_data;
struct timeval tv = {0, 0};
unsigned char payload_len;
const int maxfd = fd + 1;
unsigned char bc; /* the dump checksum */
fd_set readset;
ssize_t length;
int ret;
int i;
FD_ZERO(&readset);
FD_SET(fd, &readset);
errno = EAGAIN;
ret = select(maxfd, &readset, NULL, NULL, &tv);
if (ret <= 0) /* on error and timeout */
return -errno;
/* on receive, just wait a bit... */
usleep(300000);
length = read(fd, buffer, 2);
if (length < 2) {
if (length == -1) {
error("Read from serial fail: %s\n", strerror(errno));
return -errno;
} else {
error("Packet shorter than four bytes: %zd\n", length);
return -EINVAL;
}
}
if (buffer[0] != FHZ_MAGIC) {
fprintf(stderr, "Invalid packet magic\n");
return -EINVAL;
}
length = read(fd, buffer + 2, buffer[1]);
if (length < buffer[1]) {
if (length == -1) {
error("Read from serial fail: %s\n", strerror(errno));
return -errno;
} else {
error("Packet shorter expected: got %zd, expected %u\n",
length, buffer[1]);
return -EINVAL;
}
}
length += 2;
hexdump(buffer, length);
if (length < 4) {
fprintf(stderr, "Packet misses type or crc\n");
return -EINVAL;
}
payload_data = buffer + 4;
payload_len = length - 4;
bc = 0;
for (i = 0; i < payload_len; i++)
bc += payload_data[i];
if (bc != buffer[3]) {
fprintf(stderr, "Packet checksum mismatch\n");
return -EINVAL;
}
payload->tt = buffer[2];
memcpy(payload->data, payload_data, payload_len);
payload->len = payload_len;
return 0;
}
int fhz_handle(int fd, struct fhz_message *message)
{
struct payload payload;
int err;
err = fhz_receive(fd, &payload);
if (err)
return err;
err = fht_decode(&payload, &message->fht);
if (!err) {
message->machine = FHT;
return 0;
} else if (!(err == -EINVAL || err == -EAGAIN)) {
return err;
}
/* handle payloads other than FHT */
return err;
}
int fhz_send(int fd, const struct payload *payload)
{
unsigned char buffer[256-2];
unsigned char bc;
int i, ret;
bc = 0;
for (i = 0; i < payload->len; i++)
bc += payload->data[i];
buffer[0] = FHZ_MAGIC;
buffer[1] = payload->len + 2;
buffer[2] = payload->tt;
buffer[3] = bc;
memcpy(buffer + 4, payload->data, payload->len);
hexdump(buffer, payload->len + 4);
#ifndef NO_SEND
ret = write(fd, buffer, payload->len + 4);
if (ret != payload->len + 4) {
fprintf(stderr, "Error sending FHZ sequence\n");
return -EINVAL;
}
#else
(void)ret; /* surpress compiler warning */
#endif
return 0;
}
int fhz_open_serial(const char *device)
{
struct termios tty;
int err, fd;
fd = open(device, O_RDWR | O_NOCTTY);
if (fd == -1) {
error("opening %s: %s\n", device, strerror(errno));
return -errno;
}
memset(&tty, 0, sizeof tty);
err = tcgetattr (fd, &tty);
if (err) {
error("tcgetattr: %s\n", strerror(errno));
goto close_out;
}
err = cfsetospeed (&tty, BAUDRATE);
if (err) {
error("cfsetospeed: %s\n", strerror(errno));
goto close_out;
}
err = cfsetispeed(&tty, BAUDRATE);
if (err) {
error("cfsetispeed: %s\n", strerror(errno));
goto close_out;
}
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag &= ~IGNBRK;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cc[VMIN] = 0;
tty.c_cc[VTIME] = 10;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
err = tcsetattr (fd, TCSANOW, &tty);
if (err) {
error("tcsetattr: %s", strerror(errno));
goto close_out;
}
return fd;
close_out:
close(fd);
return -errno;
}