-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradio433client.c
More file actions
188 lines (179 loc) · 5.55 KB
/
radio433client.c
File metadata and controls
188 lines (179 loc) · 5.55 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "radio433_types.h"
#include "radio433_dev.h"
#define RADIO433_DEFAULT_HOST "127.0.0.1"
#define RADIO433_DEFAULT_PORT 5433
#define RECONNECT_DELAY_SEC 10
#define RADMSG_SIZE 128 /* radio daemon message size */
#define RADBUF_SIZE (RADMSG_SIZE * 8)
#define MSG_HDR "<RX>"
#define MSG_EOT "<ZZ>"
extern char *optarg;
extern int optind, opterr, optopt;
/* Show help */
void help(char *progname)
{
printf("\nUsage:\n\t%s [-w] [-P] [-T] [-R] [-r ipaddr] [-p tcpport]\n\n", progname);
puts("Where:");
puts("\t-w - wait for server when starting (optional)");
puts("\t-P,-T,-R - select power (-P), weather (-T) or remote");
puts("\t control (-R) messages (optional, default: all)");
puts("\t-r ipaddr - IPv4 address of radio433daemon server (optional)");
puts("\t-p tcpport - TCP port of radio433daemon server (optional)");
printf("\nWhen no parameter is specified, client tries to connect to server %s on port %d.\n\n",
RADIO433_DEFAULT_HOST, RADIO433_DEFAULT_PORT);
}
/* ********** */
/* * MAIN * */
/* ********** */
int main(int argc, char *argv[])
{
time_t tss;
unsigned int tsms, filt;
struct tm *tl;
unsigned long long code;
int opt, tid, type, bits;
char *stype[] = { "NUL", "PWR", "THM", "RMT" };
int sysid, devid, btn;
int ch, batlow, tdir, humid;
double temp;
char trend[3] = { '_', '/', '\\' };
int port, clfd, msglen;
struct sockaddr_in clntsin;
char buf[RADBUF_SIZE + 1];
char *msgptr, *msgend;
int waitflag;
int codelen, repeats, interval;
/* get parameters */
memset((char *)&clntsin, 0, sizeof(clntsin));
clntsin.sin_family = AF_INET;
inet_aton(RADIO433_DEFAULT_HOST, &clntsin.sin_addr);
port = RADIO433_DEFAULT_PORT;
waitflag = 0;
filt = 0;
while((opt = getopt(argc, argv, "hwPTRr:p:")) != -1) {
if (opt == 'r') {
if (!inet_aton(optarg, &clntsin.sin_addr)) {
fputs("Invalid IPv4 address specification.\n", stderr);
exit(EXIT_FAILURE);
}
}
else if (opt == 'p')
sscanf(optarg, "%d", port);
else if (opt == 'w')
waitflag = 1;
else if (opt == 'P')
filt |= RADIO433_CLASS_POWER;
else if (opt == 'T')
filt |= RADIO433_CLASS_WEATHER;
else if (opt == 'R')
filt |= RADIO433_CLASS_REMOTE;
else if (opt == '?' || opt == 'h') {
help(argv[0]);
exit(EXIT_FAILURE);
}
}
clntsin.sin_port = htons(port);
/* connect to server */
for(;;) {
clfd = socket(AF_INET, SOCK_STREAM, 0);
if (clfd == -1) {
fprintf(stderr, "Unable to create socket: %s\n",
strerror (errno));
exit(EXIT_FAILURE);
}
if (connect(clfd, (struct sockaddr *)&clntsin, sizeof(clntsin)) == -1) {
fprintf(stderr, "Unable to connect to server: %s\n",
strerror (errno));
if (waitflag) {
fprintf(stderr, "Retrying in %d seconds...\n",
RECONNECT_DELAY_SEC);
sleep(RECONNECT_DELAY_SEC);
continue;
}
else
exit(EXIT_FAILURE);
} else
break;
}
printf("Connected to server %s port %d. Awaiting messages...\n",
inet_ntoa(clntsin.sin_addr), port);
/* function loop - never ends, send signal to exit */
for(;;) {
do
msglen = recv(clfd, buf, RADBUF_SIZE, 0);
while (msglen == -1 && errno == EINTR);
if (msglen < 0) {
fprintf(stderr, "Error receiving data from server: %s\n",
strerror (errno));
exit(EXIT_FAILURE);
} else if (!msglen) {
fputs("Server has closed connection.\n", stderr);
exit(EXIT_FAILURE);
}
buf[msglen] = 0;
msgptr = buf;
do {
msgptr = strstr(msgptr, MSG_HDR);
if (msgptr == NULL)
break;
msgend = strstr(msgptr, MSG_EOT);
if (msgend == NULL)
break;
if (sscanf(msgptr + 4, "%lu.%u;%d;%d;%d;0x%X;%d;0x%llX;",
&tss, &tsms, &codelen, &repeats, &interval, &type,
&bits, &code) == 8)
if (!filt || (type & filt)) {
tl = localtime(&tss);
printf("%d-%02d-%02d %02d:%02d:%02d.%03u",
1900 + tl->tm_year, tl->tm_mon + 1,
tl->tm_mday, tl->tm_hour, tl->tm_min,
tl->tm_sec, tsms);
if (type & RADIO433_CLASS_POWER)
tid = 1;
else if (type & RADIO433_CLASS_WEATHER)
tid = 2;
else if (type & RADIO433_CLASS_REMOTE)
tid = 3;
else
tid = 0;
printf(" %s%s len = %d , code = 0x%0*llX", filt ? "*" : "",
stype[tid], bits, (bits + 3) >> 2, code);
if (type == RADIO433_DEVICE_KEMOTURZ1226) {
if (Radio433_pwrGetCommand(code, &sysid, &devid, &btn))
printf(" , %d : %s%s%s%s%s : %s\n", sysid,
devid & POWER433_DEVICE_A ? "A" : "",
devid & POWER433_DEVICE_B ? "B" : "",
devid & POWER433_DEVICE_C ? "C" : "",
devid & POWER433_DEVICE_D ? "D" : "",
devid & POWER433_DEVICE_E ? "E" : "",
btn ? "ON" : "OFF");
else
puts("");
} else if (type == RADIO433_DEVICE_HYUWSSENZOR77TH) {
if (Radio433_thmGetData(code, &sysid, &devid, &ch,
&batlow, &tdir, &temp, &humid))
printf(" , %1d , T: %+.1lf C %c , H: %d %% %c\n",
ch, temp, tdir < 0 ? '!' : trend[tdir],
humid, batlow ? 'b' : ' ');
else
puts("");
} else
puts("");
}
msgptr = msgend + 5;
} while (msgptr < buf + msglen);
} /* main loop ends here */
}