]>
cvs.zerfleddert.de Git - hmcfgusb/blob - hmland.c
1 /* HM-CFG-LAN emulation for HM-CFG-USB
3 * Copyright (c) 2013 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
33 #include <sys/types.h>
34 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <libusb-1.0/libusb.h>
46 #define PID_FILE "/var/run/hmland.pid"
48 #define DEFAULT_REBOOT_SECONDS 86400
52 static int impersonate_hmlanif
= 0;
54 static int verbose
= 0;
55 static int reboot_seconds
= 0;
56 static int reboot_at_hour
= -1;
57 static int reboot_at_minute
= -1;
62 struct queued_rx
*next
;
65 static struct queued_rx
*qrx
= NULL
;
66 static int wait_for_h
= 0;
68 #define FLAG_LENGTH_BYTE (1<<0)
69 #define FLAG_FORMAT_HEX (1<<1)
70 #define FLAG_COMMA_BEFORE (1<<2)
71 #define FLAG_COMMA_AFTER (1<<3)
72 #define FLAG_NL (1<<4)
73 #define FLAG_IGNORE_COMMAS (1<<5)
75 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
76 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
78 static void print_timestamp(FILE *f
)
84 gettimeofday(&tv
, NULL
);
85 tmp
= localtime(&tv
.tv_sec
);
86 memset(ts
, 0, sizeof(ts
));
87 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
88 fprintf(f
, "%s.%06ld: ", ts
, tv
.tv_usec
);
91 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
93 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
94 'A', 'B', 'C', 'D', 'E', 'F'};
95 uint8_t *buf_out
= *outpos
;
96 uint8_t *outend
= *outpos
+ outlen
;
97 uint8_t *inend
= *inpos
+ inlen
;
100 if (flags
& FLAG_COMMA_BEFORE
) {
106 if (flags
& FLAG_LENGTH_BYTE
) {
112 if (flags
& FLAG_FORMAT_HEX
) {
115 for (i
= 0; i
< len
; i
++) {
116 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
118 **outpos
= nibble
[((**inpos
) & 0xf)];
119 *inpos
+= 1; *outpos
+= 1;
124 memcpy(*outpos
, *inpos
, len
);
129 if (flags
& FLAG_COMMA_AFTER
) {
135 if (flags
& FLAG_NL
) {
143 return *outpos
- buf_out
;
146 static uint8_t ascii_to_nibble(uint8_t a
)
150 if ((a
>= '0') && (a
<= '9')) {
152 } else if ((a
>= 'A') && (a
<= 'F')) {
154 } else if ((a
>= 'a') && (a
<= 'f')) {
161 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
163 uint8_t *buf_out
= *outpos
;
164 uint8_t *outend
= *outpos
+ outlen
;
165 uint8_t *inend
= *inpos
+ inlen
;
167 if (flags
& FLAG_LENGTH_BYTE
) {
175 if (!(flags
& FLAG_IGNORE_COMMAS
))
184 **outpos
= (len
/ 2);
188 while(*inpos
< inend
) {
189 if (**inpos
== ',') {
191 if (!(flags
& FLAG_IGNORE_COMMAS
))
200 **outpos
= ascii_to_nibble(**inpos
) << 4;
202 **outpos
|= ascii_to_nibble(**inpos
);
203 *inpos
+= 1; *outpos
+= 1;
206 return *outpos
- buf_out
;
209 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
214 int fd
= *((int*)data
);
220 memset(out
, 0, sizeof(out
));
224 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
227 if (impersonate_hmlanif
) {
232 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
233 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
234 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
235 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
236 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
237 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
238 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
242 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
243 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
244 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
245 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
246 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
247 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
);
251 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
252 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
253 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
254 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
255 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
256 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
);
260 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
261 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
262 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
263 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
267 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_NL
);
271 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
272 hexdump(buf
, buf_len
, "Unknown> ");
276 /* Queue packet until first respone to 'K' is received */
277 if (wait_for_h
&& buf
[0] != 'H') {
278 struct queued_rx
**rxp
= &qrx
;
281 rxp
= &((*rxp
)->next
);
283 *rxp
= malloc(sizeof(struct queued_rx
));
289 memset(*rxp
, 0, sizeof(struct queued_rx
));
290 (*rxp
)->len
= outpos
-out
;
291 (*rxp
)->rx
= malloc((*rxp
)->len
);
296 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
297 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
305 print_timestamp(stdout
);
307 for (i
= 0; i
< outpos
-out
-2; i
++)
308 printf("%c", out
[i
]);
312 w
= write(fd
, out
, outpos
-out
);
318 /* Send all queued packets */
320 struct queued_rx
*curr_rx
= qrx
;
321 struct queued_rx
*last_rx
;
327 print_timestamp(stdout
);
329 for (i
= 0; i
< curr_rx
->len
-2; i
++)
330 printf("%c", curr_rx
->rx
[i
]);
334 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
339 curr_rx
= curr_rx
->next
;
353 static int hmlan_parse_in(int fd
, void *data
)
355 struct hmcfgusb_dev
*dev
= data
;
357 uint8_t out
[0x40]; //FIXME!!!
364 memset(buf
, 0, sizeof(buf
));
366 r
= read(fd
, buf
, sizeof(buf
)-1);
368 uint8_t *inend
= buf
+ r
;
372 while (inpos
< inend
) {
373 uint8_t *instart
= inpos
;
375 if ((*inpos
== '\r') || (*inpos
== '\n')) {
382 last
= inend
- inpos
;
384 for (i
= 0; i
< last
; i
++) {
385 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
395 print_timestamp(stdout
);
397 for (i
= 0; i
< last
; i
++)
398 printf("%c", instart
[i
]);
402 memset(out
, 0, sizeof(out
));
403 *outpos
++ = *inpos
++;
407 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
408 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
409 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
410 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
411 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
412 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
415 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
416 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
417 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
420 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
424 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
427 if (errno
!= ECONNRESET
)
437 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
439 struct hmcfgusb_dev
*dev
;
440 uint8_t out
[0x40]; //FIXME!!!
443 hmcfgusb_set_debug(debug
);
445 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
447 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
451 if (dev
->bootloader
) {
453 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
455 hmcfgusb_leave_bootloader(dev
);
462 if ((reboot_at_hour
!= -1) && (reboot_at_minute
!= -1)) {
467 tm_s
= localtime(&tm
);
473 tm_s
->tm_hour
= reboot_at_hour
;
474 tm_s
->tm_min
= reboot_at_minute
;
478 reboot_seconds
= tm
- dev
->opened_at
;
480 while (reboot_seconds
<= 0)
481 reboot_seconds
+= 86400;
484 if (verbose
&& reboot_seconds
)
485 printf("Rebooting in %u seconds\n", reboot_seconds
);
488 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
489 fprintf(stderr
, "Can't add client to pollfd!\n");
494 if (master_socket
>= 0) {
495 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
496 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
502 memset(out
, 0, sizeof(out
));
505 hmcfgusb_send_null_frame(dev
, 1);
506 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
511 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
513 if (fd
== master_socket
) {
516 client
= accept(master_socket
, NULL
, 0);
518 shutdown(client
, SHUT_RDWR
);
522 if (hmlan_parse_in(fd
, dev
) <= 0) {
526 } else if (fd
== -1) {
528 if (errno
!= ETIMEDOUT
) {
529 perror("hmcfgusb_poll");
532 /* periodically wakeup the device */
533 hmcfgusb_send_null_frame(dev
, 1);
535 memset(out
, 0, sizeof(out
));
537 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
543 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
545 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
546 time(NULL
) - dev
->opened_at
);
548 hmcfgusb_enter_bootloader(dev
);
556 void sigterm_handler(int sig
)
558 if (unlink(PID_FILE
) == -1)
559 perror("Can't remove PID file");
564 #define FLAG_DAEMON (1 << 0)
565 #define FLAG_PID_FILE (1 << 1)
567 static int socket_server(char *iface
, int port
, int flags
)
569 struct sigaction sact
;
570 struct sockaddr_in sin
;
575 if (flags
& FLAG_DAEMON
) {
576 FILE *pidfile
= NULL
;
578 if (flags
& FLAG_PID_FILE
) {
581 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
583 if (errno
== EEXIST
) {
585 pidfile
= fopen(PID_FILE
, "r");
587 perror("PID file " PID_FILE
" already exists, already running?");
591 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
593 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
599 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
602 perror("Can't create PID file " PID_FILE
);
606 pidfile
= fdopen(fd
, "w");
608 perror("Can't reopen PID file fd");
612 memset(&sact
, 0, sizeof(sact
));
613 sact
.sa_handler
= sigterm_handler
;
615 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
616 perror("sigaction(SIGTERM)");
624 fprintf(pidfile
, "%u\n", pid
);
628 printf("Daemon with PID %u started!\n", pid
);
630 } else if (pid
< 0) {
639 memset(&sact
, 0, sizeof(sact
));
640 sact
.sa_handler
= SIG_IGN
;
642 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
643 perror("sigaction(SIGPIPE)");
647 impersonate_hmlanif
= 1;
649 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
651 perror("Can't open socket");
656 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
657 perror("Can't set socket options");
661 memset(&sin
, 0, sizeof(sin
));
662 sin
.sin_family
= AF_INET
;
663 sin
.sin_port
= htons(port
);
665 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
667 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
668 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
673 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
674 perror("Can't bind socket");
678 if (listen(sock
, 1) == -1) {
679 perror("Can't listen on socket");
684 struct sockaddr_in csin
;
687 in_addr_t client_addr
;
689 memset(&csin
, 0, sizeof(csin
));
690 csinlen
= sizeof(csin
);
691 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
693 perror("Couldn't accept client");
697 /* FIXME: getnameinfo... */
698 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
701 print_timestamp(stdout
);
702 printf("Client %d.%d.%d.%d connected!\n",
703 (client_addr
& 0xff000000) >> 24,
704 (client_addr
& 0x00ff0000) >> 16,
705 (client_addr
& 0x0000ff00) >> 8,
706 (client_addr
& 0x000000ff));
709 comm(client
, client
, sock
, flags
);
711 shutdown(client
, SHUT_RDWR
);
715 print_timestamp(stdout
);
716 printf("Connection to %d.%d.%d.%d closed!\n",
717 (client_addr
& 0xff000000) >> 24,
718 (client_addr
& 0x00ff0000) >> 16,
719 (client_addr
& 0x0000ff00) >> 8,
720 (client_addr
& 0x000000ff));
728 static int interactive_server(int flags
)
730 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
736 void hmlan_syntax(char *prog
)
738 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
739 fprintf(stderr
, "Possible options:\n");
740 fprintf(stderr
, "\t-D\t\tdebug mode\n");
741 fprintf(stderr
, "\t-d\t\tdaemon mode\n");
742 fprintf(stderr
, "\t-h\t\tthis help\n");
743 fprintf(stderr
, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
744 fprintf(stderr
, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
745 fprintf(stderr
, "\t-P\t\tcreate PID file " PID_FILE
" in daemon mode\n");
746 fprintf(stderr
, "\t-p n\t\tlisten on port n (default: 1000)\n");
747 fprintf(stderr
, "\t-r n\t\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS
);
748 fprintf(stderr
, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
749 fprintf(stderr
, "\t-v\t\tverbose mode\n");
750 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
754 int main(int argc
, char **argv
)
763 reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
765 while((opt
= getopt(argc
, argv
, "DdhiPp:Rr:l:vV")) != -1) {
772 flags
|= FLAG_DAEMON
;
778 flags
|= FLAG_PID_FILE
;
781 port
= strtoul(optarg
, &ep
, 10);
783 fprintf(stderr
, "Can't parse port!\n");
788 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
791 reboot_seconds
= strtoul(optarg
, &ep
, 10);
794 reboot_at_hour
= reboot_seconds
;
796 reboot_at_minute
= strtoul(ep
, &ep
, 10);
798 fprintf(stderr
, "Can't parse reboot-time!\n");
804 fprintf(stderr
, "Can't parse reboot-timeout!\n");
816 printf("hmland " VERSION
"\n");
817 printf("Copyright (c) 2013 Michael Gernoth\n\n");
823 hmlan_syntax(argv
[0]);
830 return interactive_server(flags
);
832 return socket_server(iface
, port
, flags
);