]> git.zerfleddert.de Git - hmcfgusb/blame - hmland.c
hmland.init: try hmland-location from Fhem-wiki, too
[hmcfgusb] / hmland.c
CommitLineData
4732a863 1/* HM-CFG-LAN emulation for HM-CFG-USB
9db2e455 2 *
7ba4ea19 3 * Copyright (c) 2013-16 Michael Gernoth <michael@gernoth.net>
9db2e455
MG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <stdint.h>
18e63b25 28#include <stdarg.h>
9db2e455
MG
29#include <string.h>
30#include <strings.h>
31#include <poll.h>
4371275b 32#include <signal.h>
9db2e455 33#include <errno.h>
e0a7146e
MG
34#include <sys/types.h>
35#include <sys/socket.h>
b0bf6ad2 36#include <sys/stat.h>
e6ab4631 37#include <fcntl.h>
fbbbfa37 38#include <time.h>
e0a7146e
MG
39#include <netinet/in.h>
40#include <arpa/inet.h>
9db2e455
MG
41#include <libusb-1.0/libusb.h>
42
c44f15b8 43#include "version.h"
9db2e455
MG
44#include "hexdump.h"
45#include "hmcfgusb.h"
bcbce09d 46#include "util.h"
9db2e455 47
b0bf6ad2
MG
48#define PID_FILE "/var/run/hmland.pid"
49
6a744bb9 50#define POLL_TIMEOUT_MS 250 /* Wake up device/bus at least once every 250ms */
62f25bae 51#define DEFAULT_REBOOT_SECONDS 86400
b229c878
MG
52#define LAN_READ_CHUNK_SIZE 2048
53/* Don't allow remote clients to consume all of our memory */
54#define LAN_MAX_LINE_LENGTH 4096
55#define LAN_MAX_BUF_LENGTH 1048576
62f25bae 56
627e3f33
MG
57extern char *optarg;
58
e0a7146e 59static int impersonate_hmlanif = 0;
627e3f33
MG
60static int debug = 0;
61static int verbose = 0;
18e63b25 62static FILE *logfile = NULL;
62f25bae 63static int reboot_seconds = 0;
30a2aa7a
MG
64static int reboot_at_hour = -1;
65static int reboot_at_minute = -1;
c8cafbea 66static int reboot_set = 0;
b229c878
MG
67static uint8_t *lan_read_buf = NULL;
68static int lan_read_buflen = 0;
f51714be 69static char *serial = NULL;
e0a7146e 70
d84111c4
MG
71struct queued_rx {
72 char *rx;
73 int len;
74 struct queued_rx *next;
75};
76
77static struct queued_rx *qrx = NULL;
78static int wait_for_h = 0;
79
e75295bb
MG
80#define FLAG_LENGTH_BYTE (1<<0)
81#define FLAG_FORMAT_HEX (1<<1)
82#define FLAG_COMMA_BEFORE (1<<2)
83#define FLAG_COMMA_AFTER (1<<3)
84#define FLAG_NL (1<<4)
85#define FLAG_IGNORE_COMMAS (1<<5)
86
87#define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
88#define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
89
fbbbfa37
MG
90static void print_timestamp(FILE *f)
91{
92 struct timeval tv;
93 struct tm *tmp;
94 char ts[32];
95
96 gettimeofday(&tv, NULL);
97 tmp = localtime(&tv.tv_sec);
98 memset(ts, 0, sizeof(ts));
99 strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
100 fprintf(f, "%s.%06ld: ", ts, tv.tv_usec);
101}
102
18e63b25
MG
103static void write_log(char *buf, int len, char *fmt, ...)
104{
105 va_list ap;
106 int i;
107
108 if ((!logfile) && (!verbose))
109 return;
110
111 if (logfile)
112 print_timestamp(logfile);
113 if (verbose)
114 print_timestamp(stdout);
115
116 if (fmt) {
117 if (logfile) {
118 va_start(ap, fmt);
119 vfprintf(logfile, fmt, ap);
120 va_end(ap);
121 }
122 if (verbose) {
123 va_start(ap, fmt);
124 vprintf(fmt, ap);
125 va_end(ap);
126 }
127 }
128
129 if (buf && len) {
130 for (i = 0; i < len; i++) {
131 if (logfile)
132 fprintf(logfile, "%c", buf[i]);
133 if (verbose)
134 printf("%c", buf[i]);
135 }
136 if (logfile)
137 fprintf(logfile, "\n");
138 if (verbose)
139 printf("\n");
140 }
141 if (logfile)
142 fflush(logfile);
143}
144
e75295bb
MG
145static int format_part_out(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int len, int flags)
146{
147 uint8_t *buf_out = *outpos;
148 uint8_t *outend = *outpos + outlen;
149 uint8_t *inend = *inpos + inlen;
150 int i;
151
152 if (flags & FLAG_COMMA_BEFORE) {
153 CHECK_SPACE(1);
154 **outpos=',';
155 *outpos += 1;
156 }
157
158 if (flags & FLAG_LENGTH_BYTE) {
159 CHECK_AVAIL(1);
160 len = **inpos;
161 *inpos += 1;
162 }
163
164 if (flags & FLAG_FORMAT_HEX) {
e75295bb
MG
165 CHECK_AVAIL(len);
166 CHECK_SPACE(len*2);
167 for (i = 0; i < len; i++) {
bcbce09d 168 **outpos = nibble_to_ascii(((**inpos) & 0xf0) >> 4);
51d4ece6 169 *outpos += 1;
bcbce09d 170 **outpos = nibble_to_ascii(((**inpos) & 0xf));
51d4ece6 171 *inpos += 1; *outpos += 1;
e75295bb
MG
172 }
173 } else {
174 CHECK_AVAIL(len);
175 CHECK_SPACE(len);
176 memcpy(*outpos, *inpos, len);
177 *outpos += len;
178 *inpos += len;
179 }
180
181 if (flags & FLAG_COMMA_AFTER) {
182 CHECK_SPACE(1);
183 **outpos=',';
184 *outpos += 1;
185 }
186
187 if (flags & FLAG_NL) {
188 CHECK_SPACE(2);
189 **outpos='\r';
190 *outpos += 1;
191 **outpos='\n';
192 *outpos += 1;
193 }
194
195 return *outpos - buf_out;
196}
197
198static int parse_part_in(uint8_t **inpos, int inlen, uint8_t **outpos, int outlen, int flags)
199{
200 uint8_t *buf_out = *outpos;
201 uint8_t *outend = *outpos + outlen;
202 uint8_t *inend = *inpos + inlen;
e75295bb
MG
203
204 if (flags & FLAG_LENGTH_BYTE) {
205 int len = 0;
206 uint8_t *ip;
207
208 ip = *inpos;
209 while(ip < inend) {
210 if (*ip == ',') {
211 ip++;
212 if (!(flags & FLAG_IGNORE_COMMAS))
213 break;
214
215 continue;
216 }
217 len++;
218 ip++;
219 }
220 CHECK_SPACE(1);
221 **outpos = (len / 2);
222 *outpos += 1;
223 }
224
225 while(*inpos < inend) {
226 if (**inpos == ',') {
227 *inpos += 1;
228 if (!(flags & FLAG_IGNORE_COMMAS))
229 break;
230
231 continue;
232 }
233
234 CHECK_SPACE(1);
235 CHECK_AVAIL(2);
e75295bb 236
51d4ece6
MG
237 **outpos = ascii_to_nibble(**inpos) << 4;
238 *inpos += 1;
239 **outpos |= ascii_to_nibble(**inpos);
240 *inpos += 1; *outpos += 1;
e75295bb
MG
241 }
242
243 return *outpos - buf_out;
244}
245
4371275b 246static int hmlan_format_out(uint8_t *buf, int buf_len, void *data)
9db2e455 247{
e75295bb
MG
248 uint8_t out[1024];
249 uint8_t *outpos;
250 uint8_t *inpos;
c8cafbea 251 uint16_t version;
e0a7146e 252 int fd = *((int*)data);
4371275b 253 int w;
9db2e455
MG
254
255 if (buf_len < 1)
4371275b 256 return 1;
9db2e455 257
e0a7146e 258 memset(out, 0, sizeof(out));
e75295bb
MG
259 outpos = out;
260 inpos = buf;
e0a7146e 261
e75295bb 262 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, 0);
9db2e455
MG
263 switch(buf[0]) {
264 case 'H':
e0a7146e
MG
265 if (impersonate_hmlanif) {
266 buf[5] = 'L';
267 buf[6] = 'A';
268 buf[7] = 'N';
269 }
e75295bb 270 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_LENGTH_BYTE);
c8cafbea
MG
271 version = inpos[0] << 8;
272 version |= inpos[1];
e75295bb
MG
273 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
274 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE);
275 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
276 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
277 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
dff10a33
MG
278 if (version < 0x03c7) {
279 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
280 } else {
281 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
282 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
283 }
9db2e455 284
c8cafbea
MG
285 if (!reboot_set) {
286 int new_reboot_seconds;
287
288 if (version < 0x03c7) {
289 new_reboot_seconds = DEFAULT_REBOOT_SECONDS;
290 } else {
291 new_reboot_seconds = 0;
292 }
293
294 if (verbose && new_reboot_seconds && (reboot_seconds != new_reboot_seconds))
295 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
296 new_reboot_seconds, version);
297
298 reboot_seconds = new_reboot_seconds;
299 }
300
9db2e455
MG
301 break;
302 case 'E':
e75295bb
MG
303 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 3, FLAG_FORMAT_HEX);
304 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
305 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
306 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
307 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
308 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE | FLAG_NL);
309
9db2e455
MG
310 break;
311 case 'R':
e75295bb
MG
312 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX);
313 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
314 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 4, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
315 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
316 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 2, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
317 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 0, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_LENGTH_BYTE | FLAG_NL);
318
9db2e455
MG
319 break;
320 case 'I':
e75295bb
MG
321 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX);
322 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
323 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE);
324 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_COMMA_BEFORE | FLAG_NL);
325
53d7bde2
MG
326 break;
327 case 'G':
328 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), 1, FLAG_FORMAT_HEX | FLAG_NL);
329
e75295bb 330 break;
9db2e455 331 default:
e75295bb 332 format_part_out(&inpos, (buf_len-(inpos-buf)), &outpos, (sizeof(out)-(outpos-out)), buf_len-1, FLAG_FORMAT_HEX | FLAG_NL);
9db2e455
MG
333 hexdump(buf, buf_len, "Unknown> ");
334 break;
335 }
d84111c4
MG
336
337 /* Queue packet until first respone to 'K' is received */
338 if (wait_for_h && buf[0] != 'H') {
339 struct queued_rx **rxp = &qrx;
340
341 while (*rxp)
342 rxp = &((*rxp)->next);
343
344 *rxp = malloc(sizeof(struct queued_rx));
345 if (!*rxp) {
346 perror("malloc");
347 return 0;
348 }
349
350 memset(*rxp, 0, sizeof(struct queued_rx));
351 (*rxp)->len = outpos-out;
352 (*rxp)->rx = malloc((*rxp)->len);
353 if (!(*rxp)->rx) {
354 perror("malloc");
355 return 0;
356 }
357 memset((*rxp)->rx, 0, (*rxp)->len);
358 memcpy((*rxp)->rx, out, (*rxp)->len);
359
360 return 1;
361 }
362
18e63b25 363 write_log((char*)out, outpos-out-2, "LAN < ");
4371275b
MG
364
365 w = write(fd, out, outpos-out);
366 if (w <= 0) {
367 perror("write");
368 return 0;
369 }
370
59360394 371 /* Send all queued packets */
d84111c4
MG
372 if (wait_for_h) {
373 struct queued_rx *curr_rx = qrx;
374 struct queued_rx *last_rx;
375
376 while (curr_rx) {
18e63b25 377 write_log(curr_rx->rx, curr_rx->len-2, "LAN < ");
d84111c4
MG
378
379 w = write(fd, curr_rx->rx, curr_rx->len);
380 if (w <= 0) {
381 perror("write");
d84111c4
MG
382 }
383 last_rx = curr_rx;
384 curr_rx = curr_rx->next;
385
386 free(last_rx->rx);
387 free(last_rx);
388 }
389
390 qrx = NULL;
391
392 wait_for_h = 0;
393 }
394
4371275b 395 return 1;
9db2e455
MG
396}
397
b229c878 398static int hmlan_parse_one(uint8_t *cmd, int last, void *data)
9db2e455
MG
399{
400 struct hmcfgusb_dev *dev = data;
e75295bb
MG
401 uint8_t out[0x40]; //FIXME!!!
402 uint8_t *outpos;
b229c878
MG
403 uint8_t *inpos = cmd;
404
405 outpos = out;
9db2e455 406
b229c878
MG
407 if (last == 0)
408 return 1;
627e3f33 409
b229c878 410 write_log((char*)cmd, last, "LAN > ");
627e3f33 411
b229c878
MG
412 memset(out, 0, sizeof(out));
413 *outpos++ = *inpos++;
414
415 switch(*cmd) {
416 case 'S':
417 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
418 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
419 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
420 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
421 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
422 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
423 break;
424 case 'Y':
425 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
426 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
427 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
428 break;
6b663018
MG
429 case '+':
430 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
431 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
432 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), 0);
433 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_LENGTH_BYTE);
b229c878
MG
434 default:
435 parse_part_in(&inpos, (last-(inpos-cmd)), &outpos, (sizeof(out)-(outpos-out)), FLAG_IGNORE_COMMAS);
436 break;
437 }
627e3f33 438
b229c878 439 hmcfgusb_send(dev, out, sizeof(out), 1);
627e3f33 440
b229c878
MG
441 return 1;
442}
9db2e455 443
b229c878
MG
444static int hmlan_parse_in(int fd, void *data)
445{
446 uint8_t *newbuf;
447 int r;
448 int i;
627e3f33 449
b229c878
MG
450 newbuf = realloc(lan_read_buf, lan_read_buflen + LAN_READ_CHUNK_SIZE);
451 if (!newbuf) {
452 perror("realloc");
453 return 0;
454 }
455 lan_read_buf = newbuf;
456 r = read(fd, lan_read_buf + lan_read_buflen, LAN_READ_CHUNK_SIZE);
457 if (r > 0) {
458 lan_read_buflen += r;
459 if (lan_read_buflen > LAN_MAX_BUF_LENGTH) {
460 if (verbose)
461 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH, lan_read_buflen);
462 return -1;
463 }
464 while(lan_read_buflen > 0) {
465 int found = 0;
466
467 for (i = 0; i < lan_read_buflen; i++) {
468 if ((lan_read_buf[i] == '\r') || (lan_read_buf[i] == '\n')) {
469 if (i > 0)
470 hmlan_parse_one(lan_read_buf, i, data);
471 memmove(lan_read_buf, lan_read_buf + i + 1, lan_read_buflen - (i + 1));
472 lan_read_buflen -= (i + 1);
473 found = 1;
627e3f33
MG
474 break;
475 }
b229c878
MG
476 if (i > LAN_MAX_LINE_LENGTH) {
477 if (verbose)
478 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH);
479 return -1;
480 }
627e3f33 481 }
b229c878
MG
482 if (!found)
483 break;
484 newbuf = realloc(lan_read_buf, lan_read_buflen);
485 if (lan_read_buflen && !newbuf) {
486 perror("realloc");
487 return 0;
627e3f33 488 }
b229c878 489 lan_read_buf = newbuf;
627e3f33 490 }
9db2e455 491 } else if (r < 0) {
fbbbfa37
MG
492 if (errno != ECONNRESET)
493 perror("read");
e0a7146e
MG
494 return r;
495 } else {
496 return 0;
9db2e455 497 }
e0a7146e
MG
498
499 return 1;
9db2e455
MG
500}
501
23d96b59 502static int comm(int fd_in, int fd_out, int master_socket, int flags)
9db2e455
MG
503{
504 struct hmcfgusb_dev *dev;
23d96b59 505 uint8_t out[0x40]; //FIXME!!!
9db2e455
MG
506 int quit = 0;
507
627e3f33
MG
508 hmcfgusb_set_debug(debug);
509
f51714be 510 dev = hmcfgusb_init(hmlan_format_out, &fd_out, serial);
9db2e455
MG
511 if (!dev) {
512 fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
e0a7146e
MG
513 return 0;
514 }
515
62f25bae 516 if (dev->bootloader) {
0ddb3740 517 if (verbose)
30a2aa7a 518 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
0ddb3740
MG
519
520 hmcfgusb_leave_bootloader(dev);
521
62f25bae
MG
522 hmcfgusb_close(dev);
523 sleep(1);
524 return 0;
525 }
526
30a2aa7a
MG
527 if ((reboot_at_hour != -1) && (reboot_at_minute != -1)) {
528 struct tm *tm_s;
529 time_t tm;
530
531 tm = time(NULL);
532 tm_s = localtime(&tm);
533 if (tm_s == NULL) {
534 perror("localtime");
535 return 0;
536 }
537
30a2aa7a
MG
538 tm_s->tm_hour = reboot_at_hour;
539 tm_s->tm_min = reboot_at_minute;
540 tm_s->tm_sec = 0;
541
ec00d63b 542 tm = mktime(tm_s);
30a2aa7a 543 reboot_seconds = tm - dev->opened_at;
ec00d63b
MG
544
545 while (reboot_seconds <= 0)
546 reboot_seconds += 86400;
30a2aa7a
MG
547 }
548
549 if (verbose && reboot_seconds)
550 printf("Rebooting in %u seconds\n", reboot_seconds);
551
552
e0a7146e
MG
553 if (!hmcfgusb_add_pfd(dev, fd_in, POLLIN)) {
554 fprintf(stderr, "Can't add client to pollfd!\n");
555 hmcfgusb_close(dev);
556 return 0;
9db2e455
MG
557 }
558
e0a7146e
MG
559 if (master_socket >= 0) {
560 if (!hmcfgusb_add_pfd(dev, master_socket, POLLIN)) {
561 fprintf(stderr, "Can't add master_socket to pollfd!\n");
562 hmcfgusb_close(dev);
563 return 0;
564 }
9db2e455
MG
565 }
566
23d96b59
MG
567 memset(out, 0, sizeof(out));
568 out[0] = 'K';
d84111c4 569 wait_for_h = 1;
6262005e 570 hmcfgusb_send_null_frame(dev, 1);
23d96b59 571 hmcfgusb_send(dev, out, sizeof(out), 1);
9db2e455
MG
572
573 while(!quit) {
574 int fd;
575
6a744bb9 576 fd = hmcfgusb_poll(dev, POLL_TIMEOUT_MS);
9db2e455 577 if (fd >= 0) {
e0a7146e
MG
578 if (fd == master_socket) {
579 int client;
580
581 client = accept(master_socket, NULL, 0);
582 if (client >= 0) {
583 shutdown(client, SHUT_RDWR);
584 close(client);
585 }
586 } else {
587 if (hmlan_parse_in(fd, dev) <= 0) {
588 quit = 1;
589 }
590 }
9db2e455
MG
591 } else if (fd == -1) {
592 if (errno) {
1e79d00a
MG
593 if (errno != ETIMEDOUT) {
594 perror("hmcfgusb_poll");
595 quit = 1;
596 } else {
597 /* periodically wakeup the device */
598 hmcfgusb_send_null_frame(dev, 1);
599 if (wait_for_h) {
600 memset(out, 0, sizeof(out));
601 out[0] = 'K';
602 hmcfgusb_send(dev, out, sizeof(out), 1);
603 }
604 }
9db2e455
MG
605 }
606 }
62f25bae
MG
607
608 if (reboot_seconds && ((dev->opened_at + reboot_seconds) <= time(NULL))) {
609 if (verbose) {
30a2aa7a 610 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
62f25bae
MG
611 time(NULL) - dev->opened_at);
612 }
613 hmcfgusb_enter_bootloader(dev);
614 }
9db2e455
MG
615 }
616
813afd12 617 hmcfgusb_close(dev);
e0a7146e
MG
618 return 1;
619}
620
b0bf6ad2
MG
621void sigterm_handler(int sig)
622{
623 if (unlink(PID_FILE) == -1)
624 perror("Can't remove PID file");
625
626 exit(EXIT_SUCCESS);
627}
628
629#define FLAG_DAEMON (1 << 0)
630#define FLAG_PID_FILE (1 << 1)
631
632static int socket_server(char *iface, int port, int flags)
e0a7146e 633{
4371275b 634 struct sigaction sact;
e0a7146e
MG
635 struct sockaddr_in sin;
636 int sock;
637 int n;
627e3f33
MG
638 pid_t pid;
639
b0bf6ad2
MG
640 if (flags & FLAG_DAEMON) {
641 FILE *pidfile = NULL;
642
643 if (flags & FLAG_PID_FILE) {
e6ab4631
MG
644 int fd;
645
646 fd = open(PID_FILE, O_CREAT | O_EXCL | O_WRONLY, 0644);
647 if (fd == -1) {
648 if (errno == EEXIST) {
649 pid_t old_pid;
650 pidfile = fopen(PID_FILE, "r");
651 if (!pidfile) {
652 perror("PID file " PID_FILE " already exists, already running?");
653 exit(EXIT_FAILURE);
654 }
655
656 if (fscanf(pidfile, "%u", &old_pid) != 1) {
657 fclose(pidfile);
658 fprintf(stderr, "Can't read old PID from " PID_FILE ", already running?\n");
659 exit(EXIT_FAILURE);
660 }
661
662 fclose(pidfile);
663
664 fprintf(stderr, "Already running with PID %u according to " PID_FILE "!\n", old_pid);
665 exit(EXIT_FAILURE);
666 }
667 perror("Can't create PID file " PID_FILE);
668 exit(EXIT_FAILURE);
669 }
b0bf6ad2 670
e6ab4631 671 pidfile = fdopen(fd, "w");
b0bf6ad2 672 if (!pidfile) {
e6ab4631 673 perror("Can't reopen PID file fd");
b0bf6ad2
MG
674 exit(EXIT_FAILURE);
675 }
676
677 memset(&sact, 0, sizeof(sact));
678 sact.sa_handler = sigterm_handler;
679
680 if (sigaction(SIGTERM, &sact, NULL) == -1) {
681 perror("sigaction(SIGTERM)");
682 exit(EXIT_FAILURE);
683 }
684 }
685
627e3f33
MG
686 pid = fork();
687 if (pid > 0) {
b0bf6ad2
MG
688 if (pidfile) {
689 fprintf(pidfile, "%u\n", pid);
690 fclose(pidfile);
691 }
692
627e3f33
MG
693 printf("Daemon with PID %u started!\n", pid);
694 exit(EXIT_SUCCESS);
695 } else if (pid < 0) {
696 perror("fork");
697 exit(EXIT_FAILURE);
698 }
b0bf6ad2
MG
699
700 if (pidfile)
701 fclose(pidfile);
627e3f33 702 }
e0a7146e 703
4371275b
MG
704 memset(&sact, 0, sizeof(sact));
705 sact.sa_handler = SIG_IGN;
706
707 if (sigaction(SIGPIPE, &sact, NULL) == -1) {
b0bf6ad2 708 perror("sigaction(SIGPIPE)");
27f4063e 709 exit(EXIT_FAILURE);
4371275b
MG
710 }
711
e0a7146e
MG
712 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
713 if (sock == -1) {
714 perror("Can't open socket");
715 return EXIT_FAILURE;
716 }
717
718 n = 1;
719 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof(n)) == -1) {
720 perror("Can't set socket options");
721 return EXIT_FAILURE;
722 }
723
724 memset(&sin, 0, sizeof(sin));
725 sin.sin_family = AF_INET;
726 sin.sin_port = htons(port);
4a8a2269
MG
727 if (!iface) {
728 sin.sin_addr.s_addr = htonl(INADDR_ANY);
729 } else {
730 if (inet_pton(AF_INET, iface, &(sin.sin_addr.s_addr)) != 1) {
560c3078 731 fprintf(stderr, "Can't convert IP %s, aborting!\n", iface);
4a8a2269
MG
732 return EXIT_FAILURE;
733 }
734 }
e0a7146e
MG
735
736 if (bind(sock, (struct sockaddr*)&sin, sizeof(sin)) == -1) {
737 perror("Can't bind socket");
738 return EXIT_FAILURE;
739 }
740
741 if (listen(sock, 1) == -1) {
742 perror("Can't listen on socket");
743 return EXIT_FAILURE;
744 }
745
746 while(1) {
747 struct sockaddr_in csin;
748 socklen_t csinlen;
749 int client;
627e3f33 750 in_addr_t client_addr;
e0a7146e
MG
751
752 memset(&csin, 0, sizeof(csin));
753 csinlen = sizeof(csin);
754 client = accept(sock, (struct sockaddr*)&csin, &csinlen);
755 if (client == -1) {
756 perror("Couldn't accept client");
757 continue;
758 }
759
627e3f33
MG
760 /* FIXME: getnameinfo... */
761 client_addr = ntohl(csin.sin_addr.s_addr);
762
18e63b25
MG
763 write_log(NULL, 0, "Client %d.%d.%d.%d connected!\n",
764 (client_addr & 0xff000000) >> 24,
765 (client_addr & 0x00ff0000) >> 16,
766 (client_addr & 0x0000ff00) >> 8,
767 (client_addr & 0x000000ff));
e0a7146e 768
23d96b59 769 comm(client, client, sock, flags);
e0a7146e
MG
770
771 shutdown(client, SHUT_RDWR);
772 close(client);
773
b229c878
MG
774 if (lan_read_buf)
775 free(lan_read_buf);
776 lan_read_buf = NULL;
777 lan_read_buflen = 0;
778
18e63b25
MG
779 write_log(NULL, 0, "Connection to %d.%d.%d.%d closed!\n",
780 (client_addr & 0xff000000) >> 24,
781 (client_addr & 0x00ff0000) >> 16,
782 (client_addr & 0x0000ff00) >> 8,
783 (client_addr & 0x000000ff));
e09806b6 784 sleep(1);
e0a7146e
MG
785 }
786
787 return EXIT_SUCCESS;
788}
789
23d96b59 790static int interactive_server(int flags)
e0a7146e 791{
23d96b59 792 if (!comm(STDIN_FILENO, STDOUT_FILENO, -1, flags))
e0a7146e 793 return EXIT_FAILURE;
9db2e455
MG
794
795 return EXIT_SUCCESS;
796}
e0a7146e 797
627e3f33
MG
798void hmlan_syntax(char *prog)
799{
800 fprintf(stderr, "Syntax: %s options\n\n", prog);
801 fprintf(stderr, "Possible options:\n");
30a2aa7a
MG
802 fprintf(stderr, "\t-D\t\tdebug mode\n");
803 fprintf(stderr, "\t-d\t\tdaemon mode\n");
804 fprintf(stderr, "\t-h\t\tthis help\n");
dc9f58f0 805 fprintf(stderr, "\t-I\t\tpretend to be HM-LAN-IF for compatibility with client-software (previous default)\n");
30a2aa7a
MG
806 fprintf(stderr, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
807 fprintf(stderr, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
18e63b25 808 fprintf(stderr, "\t-L logfile\tlog network-communication to logfile\n");
30a2aa7a
MG
809 fprintf(stderr, "\t-P\t\tcreate PID file " PID_FILE " in daemon mode\n");
810 fprintf(stderr, "\t-p n\t\tlisten on port n (default: 1000)\n");
c8cafbea 811 fprintf(stderr, "\t-r n\t\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u if FW < 0.967, 0 otherwise)\n", DEFAULT_REBOOT_SECONDS);
30a2aa7a 812 fprintf(stderr, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
f51714be 813 fprintf(stderr, "\t-S serial\tuse HM-CFG-USB with given serial (for multiple hmland instances)\n");
30a2aa7a 814 fprintf(stderr, "\t-v\t\tverbose mode\n");
c44f15b8 815 fprintf(stderr, "\t-V\t\tshow version (" VERSION ")\n");
627e3f33
MG
816
817}
818
e0a7146e
MG
819int main(int argc, char **argv)
820{
627e3f33 821 int port = 1000;
4a8a2269 822 char *iface = NULL;
627e3f33 823 int interactive = 0;
b0bf6ad2 824 int flags = 0;
627e3f33
MG
825 char *ep;
826 int opt;
62f25bae 827
f51714be 828 while((opt = getopt(argc, argv, "DdhIiPp:Rr:l:L:S:vV")) != -1) {
627e3f33
MG
829 switch (opt) {
830 case 'D':
831 debug = 1;
832 verbose = 1;
833 break;
834 case 'd':
b0bf6ad2 835 flags |= FLAG_DAEMON;
627e3f33 836 break;
dc9f58f0
MG
837 case 'I':
838 impersonate_hmlanif = 1;
839 break;
627e3f33
MG
840 case 'i':
841 interactive = 1;
842 break;
b0bf6ad2
MG
843 case 'P':
844 flags |= FLAG_PID_FILE;
845 break;
627e3f33
MG
846 case 'p':
847 port = strtoul(optarg, &ep, 10);
848 if (*ep != '\0') {
849 fprintf(stderr, "Can't parse port!\n");
850 exit(EXIT_FAILURE);
851 }
852 break;
23d96b59 853 case 'R':
b55c340d 854 fprintf(stderr, "-R is no longer needed (1s wakeup is default)\n");
23d96b59 855 break;
62f25bae
MG
856 case 'r':
857 reboot_seconds = strtoul(optarg, &ep, 10);
858 if (*ep != '\0') {
30a2aa7a
MG
859 if (*ep == ':') {
860 reboot_at_hour = reboot_seconds;
861 ep++;
862 reboot_at_minute = strtoul(ep, &ep, 10);
863 if (*ep != '\0') {
864 fprintf(stderr, "Can't parse reboot-time!\n");
865 exit(EXIT_FAILURE);
866 }
867
868 reboot_seconds = 0;
869 } else {
870 fprintf(stderr, "Can't parse reboot-timeout!\n");
871 exit(EXIT_FAILURE);
872 }
62f25bae 873 }
c8cafbea 874 reboot_set = 1;
62f25bae 875 break;
4a8a2269
MG
876 case 'l':
877 iface = optarg;
878 break;
18e63b25
MG
879 case 'L':
880 logfile = fopen(optarg, "a");
881 if (!logfile) {
882 perror("fopen(logfile)");
883 exit(EXIT_FAILURE);
884 }
885 break;
f51714be
MG
886 case 'S':
887 serial = optarg;
888 break;
627e3f33
MG
889 case 'v':
890 verbose = 1;
891 break;
c44f15b8
MG
892 case 'V':
893 printf("hmland " VERSION "\n");
7ba4ea19 894 printf("Copyright (c) 2013-16 Michael Gernoth\n\n");
c44f15b8 895 exit(EXIT_SUCCESS);
627e3f33
MG
896 case 'h':
897 case ':':
898 case '?':
899 default:
900 hmlan_syntax(argv[0]);
901 exit(EXIT_FAILURE);
902 break;
903 }
904 }
905
906 if (interactive) {
23d96b59 907 return interactive_server(flags);
627e3f33 908 } else {
b0bf6ad2 909 return socket_server(iface, port, flags);
627e3f33 910 }
e0a7146e 911}
Impressum, Datenschutz