]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
1 /* HM-CFG-LAN emulation for HM-CFG-USB
3 * Copyright (c) 2013-15 Michael Gernoth <michael@gernoth.net>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <arpa/inet.h>
41 #include <libusb-1.0/libusb.h>
47 #define PID_FILE "/var/run/hmland.pid"
49 #define DEFAULT_REBOOT_SECONDS 86400
50 #define LAN_READ_CHUNK_SIZE 2048
51 /* Don't allow remote clients to consume all of our memory */
52 #define LAN_MAX_LINE_LENGTH 4096
53 #define LAN_MAX_BUF_LENGTH 1048576
57 static int impersonate_hmlanif
= 0;
59 static int verbose
= 0;
60 static FILE *logfile
= NULL
;
61 static int reboot_seconds
= 0;
62 static int reboot_at_hour
= -1;
63 static int reboot_at_minute
= -1;
64 static int reboot_set
= 0;
65 static uint8_t *lan_read_buf
= NULL
;
66 static int lan_read_buflen
= 0;
71 struct queued_rx
*next
;
74 static struct queued_rx
*qrx
= NULL
;
75 static int wait_for_h
= 0;
77 #define FLAG_LENGTH_BYTE (1<<0)
78 #define FLAG_FORMAT_HEX (1<<1)
79 #define FLAG_COMMA_BEFORE (1<<2)
80 #define FLAG_COMMA_AFTER (1<<3)
81 #define FLAG_NL (1<<4)
82 #define FLAG_IGNORE_COMMAS (1<<5)
84 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
85 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
87 static void print_timestamp(FILE *f
)
93 gettimeofday(&tv
, NULL
);
94 tmp
= localtime(&tv
.tv_sec
);
95 memset(ts
, 0, sizeof(ts
));
96 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
97 fprintf(f
, "%s.%06ld: ", ts
, tv
.tv_usec
);
100 static void write_log(char *buf
, int len
, char *fmt
, ...)
105 if ((!logfile
) && (!verbose
))
109 print_timestamp(logfile
);
111 print_timestamp(stdout
);
116 vfprintf(logfile
, fmt
, ap
);
127 for (i
= 0; i
< len
; i
++) {
129 fprintf(logfile
, "%c", buf
[i
]);
131 printf("%c", buf
[i
]);
134 fprintf(logfile
, "\n");
142 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
144 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
145 'A', 'B', 'C', 'D', 'E', 'F'};
146 uint8_t *buf_out
= *outpos
;
147 uint8_t *outend
= *outpos
+ outlen
;
148 uint8_t *inend
= *inpos
+ inlen
;
151 if (flags
& FLAG_COMMA_BEFORE
) {
157 if (flags
& FLAG_LENGTH_BYTE
) {
163 if (flags
& FLAG_FORMAT_HEX
) {
166 for (i
= 0; i
< len
; i
++) {
167 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
169 **outpos
= nibble
[((**inpos
) & 0xf)];
170 *inpos
+= 1; *outpos
+= 1;
175 memcpy(*outpos
, *inpos
, len
);
180 if (flags
& FLAG_COMMA_AFTER
) {
186 if (flags
& FLAG_NL
) {
194 return *outpos
- buf_out
;
197 static uint8_t ascii_to_nibble(uint8_t a
)
201 if ((a
>= '0') && (a
<= '9')) {
203 } else if ((a
>= 'A') && (a
<= 'F')) {
205 } else if ((a
>= 'a') && (a
<= 'f')) {
212 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
214 uint8_t *buf_out
= *outpos
;
215 uint8_t *outend
= *outpos
+ outlen
;
216 uint8_t *inend
= *inpos
+ inlen
;
218 if (flags
& FLAG_LENGTH_BYTE
) {
226 if (!(flags
& FLAG_IGNORE_COMMAS
))
235 **outpos
= (len
/ 2);
239 while(*inpos
< inend
) {
240 if (**inpos
== ',') {
242 if (!(flags
& FLAG_IGNORE_COMMAS
))
251 **outpos
= ascii_to_nibble(**inpos
) << 4;
253 **outpos
|= ascii_to_nibble(**inpos
);
254 *inpos
+= 1; *outpos
+= 1;
257 return *outpos
- buf_out
;
260 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
266 int fd
= *((int*)data
);
272 memset(out
, 0, sizeof(out
));
276 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
279 if (impersonate_hmlanif
) {
284 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
285 version
= inpos
[0] << 8;
287 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
288 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
289 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
290 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
291 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
292 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
295 int new_reboot_seconds
;
297 if (version
< 0x03c7) {
298 new_reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
300 new_reboot_seconds
= 0;
303 if (verbose
&& new_reboot_seconds
&& (reboot_seconds
!= new_reboot_seconds
))
304 printf("Rebooting in %u seconds due to old firmware (0.%d)\n",
305 new_reboot_seconds
, version
);
307 reboot_seconds
= new_reboot_seconds
;
312 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, 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
);
321 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
322 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
323 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, 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
);
325 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
326 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
);
330 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
331 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
332 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
333 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
337 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_NL
);
341 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
342 hexdump(buf
, buf_len
, "Unknown> ");
346 /* Queue packet until first respone to 'K' is received */
347 if (wait_for_h
&& buf
[0] != 'H') {
348 struct queued_rx
**rxp
= &qrx
;
351 rxp
= &((*rxp
)->next
);
353 *rxp
= malloc(sizeof(struct queued_rx
));
359 memset(*rxp
, 0, sizeof(struct queued_rx
));
360 (*rxp
)->len
= outpos
-out
;
361 (*rxp
)->rx
= malloc((*rxp
)->len
);
366 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
367 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
372 write_log((char*)out
, outpos
-out
-2, "LAN < ");
374 w
= write(fd
, out
, outpos
-out
);
380 /* Send all queued packets */
382 struct queued_rx
*curr_rx
= qrx
;
383 struct queued_rx
*last_rx
;
386 write_log(curr_rx
->rx
, curr_rx
->len
-2, "LAN < ");
388 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
393 curr_rx
= curr_rx
->next
;
407 static int hmlan_parse_one(uint8_t *cmd
, int last
, void *data
)
409 struct hmcfgusb_dev
*dev
= data
;
410 uint8_t out
[0x40]; //FIXME!!!
412 uint8_t *inpos
= cmd
;
419 write_log((char*)cmd
, last
, "LAN > ");
421 memset(out
, 0, sizeof(out
));
422 *outpos
++ = *inpos
++;
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
)), 0);
428 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
429 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_LENGTH_BYTE
);
434 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
435 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
436 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
439 parse_part_in(&inpos
, (last
-(inpos
-cmd
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
443 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
448 static int hmlan_parse_in(int fd
, void *data
)
454 newbuf
= realloc(lan_read_buf
, lan_read_buflen
+ LAN_READ_CHUNK_SIZE
);
459 lan_read_buf
= newbuf
;
460 r
= read(fd
, lan_read_buf
+ lan_read_buflen
, LAN_READ_CHUNK_SIZE
);
462 lan_read_buflen
+= r
;
463 if (lan_read_buflen
> LAN_MAX_BUF_LENGTH
) {
465 printf("Our buffer is bigger than %d bytes (%d bytes), closing connection!\n", LAN_MAX_BUF_LENGTH
, lan_read_buflen
);
468 while(lan_read_buflen
> 0) {
471 for (i
= 0; i
< lan_read_buflen
; i
++) {
472 if ((lan_read_buf
[i
] == '\r') || (lan_read_buf
[i
] == '\n')) {
474 hmlan_parse_one(lan_read_buf
, i
, data
);
475 memmove(lan_read_buf
, lan_read_buf
+ i
+ 1, lan_read_buflen
- (i
+ 1));
476 lan_read_buflen
-= (i
+ 1);
480 if (i
> LAN_MAX_LINE_LENGTH
) {
482 printf("Client sent more than %d bytes without newline, closing connection!\n", LAN_MAX_LINE_LENGTH
);
488 newbuf
= realloc(lan_read_buf
, lan_read_buflen
);
489 if (lan_read_buflen
&& !newbuf
) {
493 lan_read_buf
= newbuf
;
496 if (errno
!= ECONNRESET
)
506 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
508 struct hmcfgusb_dev
*dev
;
509 uint8_t out
[0x40]; //FIXME!!!
512 hmcfgusb_set_debug(debug
);
514 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
516 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
520 if (dev
->bootloader
) {
522 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
524 hmcfgusb_leave_bootloader(dev
);
531 if ((reboot_at_hour
!= -1) && (reboot_at_minute
!= -1)) {
536 tm_s
= localtime(&tm
);
542 tm_s
->tm_hour
= reboot_at_hour
;
543 tm_s
->tm_min
= reboot_at_minute
;
547 reboot_seconds
= tm
- dev
->opened_at
;
549 while (reboot_seconds
<= 0)
550 reboot_seconds
+= 86400;
553 if (verbose
&& reboot_seconds
)
554 printf("Rebooting in %u seconds\n", reboot_seconds
);
557 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
558 fprintf(stderr
, "Can't add client to pollfd!\n");
563 if (master_socket
>= 0) {
564 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
565 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
571 memset(out
, 0, sizeof(out
));
574 hmcfgusb_send_null_frame(dev
, 1);
575 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
580 fd
= hmcfgusb_poll(dev
, 1000); /* Wakeup device/bus at least once a second */
582 if (fd
== master_socket
) {
585 client
= accept(master_socket
, NULL
, 0);
587 shutdown(client
, SHUT_RDWR
);
591 if (hmlan_parse_in(fd
, dev
) <= 0) {
595 } else if (fd
== -1) {
597 if (errno
!= ETIMEDOUT
) {
598 perror("hmcfgusb_poll");
601 /* periodically wakeup the device */
602 hmcfgusb_send_null_frame(dev
, 1);
604 memset(out
, 0, sizeof(out
));
606 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
612 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
614 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
615 time(NULL
) - dev
->opened_at
);
617 hmcfgusb_enter_bootloader(dev
);
625 void sigterm_handler(int sig
)
627 if (unlink(PID_FILE
) == -1)
628 perror("Can't remove PID file");
633 #define FLAG_DAEMON (1 << 0)
634 #define FLAG_PID_FILE (1 << 1)
636 static int socket_server(char *iface
, int port
, int flags
)
638 struct sigaction sact
;
639 struct sockaddr_in sin
;
644 if (flags
& FLAG_DAEMON
) {
645 FILE *pidfile
= NULL
;
647 if (flags
& FLAG_PID_FILE
) {
650 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
652 if (errno
== EEXIST
) {
654 pidfile
= fopen(PID_FILE
, "r");
656 perror("PID file " PID_FILE
" already exists, already running?");
660 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
662 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
668 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
671 perror("Can't create PID file " PID_FILE
);
675 pidfile
= fdopen(fd
, "w");
677 perror("Can't reopen PID file fd");
681 memset(&sact
, 0, sizeof(sact
));
682 sact
.sa_handler
= sigterm_handler
;
684 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
685 perror("sigaction(SIGTERM)");
693 fprintf(pidfile
, "%u\n", pid
);
697 printf("Daemon with PID %u started!\n", pid
);
699 } else if (pid
< 0) {
708 memset(&sact
, 0, sizeof(sact
));
709 sact
.sa_handler
= SIG_IGN
;
711 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
712 perror("sigaction(SIGPIPE)");
716 impersonate_hmlanif
= 1;
718 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
720 perror("Can't open socket");
725 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
726 perror("Can't set socket options");
730 memset(&sin
, 0, sizeof(sin
));
731 sin
.sin_family
= AF_INET
;
732 sin
.sin_port
= htons(port
);
734 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
736 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
737 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
742 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
743 perror("Can't bind socket");
747 if (listen(sock
, 1) == -1) {
748 perror("Can't listen on socket");
753 struct sockaddr_in csin
;
756 in_addr_t client_addr
;
758 memset(&csin
, 0, sizeof(csin
));
759 csinlen
= sizeof(csin
);
760 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
762 perror("Couldn't accept client");
766 /* FIXME: getnameinfo... */
767 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
769 write_log(NULL
, 0, "Client %d.%d.%d.%d connected!\n",
770 (client_addr
& 0xff000000) >> 24,
771 (client_addr
& 0x00ff0000) >> 16,
772 (client_addr
& 0x0000ff00) >> 8,
773 (client_addr
& 0x000000ff));
775 comm(client
, client
, sock
, flags
);
777 shutdown(client
, SHUT_RDWR
);
785 write_log(NULL
, 0, "Connection to %d.%d.%d.%d closed!\n",
786 (client_addr
& 0xff000000) >> 24,
787 (client_addr
& 0x00ff0000) >> 16,
788 (client_addr
& 0x0000ff00) >> 8,
789 (client_addr
& 0x000000ff));
796 static int interactive_server(int flags
)
798 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
804 void hmlan_syntax(char *prog
)
806 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
807 fprintf(stderr
, "Possible options:\n");
808 fprintf(stderr
, "\t-D\t\tdebug mode\n");
809 fprintf(stderr
, "\t-d\t\tdaemon mode\n");
810 fprintf(stderr
, "\t-h\t\tthis help\n");
811 fprintf(stderr
, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
812 fprintf(stderr
, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
813 fprintf(stderr
, "\t-L logfile\tlog network-communication to logfile\n");
814 fprintf(stderr
, "\t-P\t\tcreate PID file " PID_FILE
" in daemon mode\n");
815 fprintf(stderr
, "\t-p n\t\tlisten on port n (default: 1000)\n");
816 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
);
817 fprintf(stderr
, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
818 fprintf(stderr
, "\t-v\t\tverbose mode\n");
819 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
823 int main(int argc
, char **argv
)
832 while((opt
= getopt(argc
, argv
, "DdhiPp:Rr:l:L:vV")) != -1) {
839 flags
|= FLAG_DAEMON
;
845 flags
|= FLAG_PID_FILE
;
848 port
= strtoul(optarg
, &ep
, 10);
850 fprintf(stderr
, "Can't parse port!\n");
855 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
858 reboot_seconds
= strtoul(optarg
, &ep
, 10);
861 reboot_at_hour
= reboot_seconds
;
863 reboot_at_minute
= strtoul(ep
, &ep
, 10);
865 fprintf(stderr
, "Can't parse reboot-time!\n");
871 fprintf(stderr
, "Can't parse reboot-timeout!\n");
881 logfile
= fopen(optarg
, "a");
883 perror("fopen(logfile)");
891 printf("hmland " VERSION
"\n");
892 printf("Copyright (c) 2013 Michael Gernoth\n\n");
898 hmlan_syntax(argv
[0]);
905 return interactive_server(flags
);
907 return socket_server(iface
, port
, flags
);