]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
68ce00e2dabbc1454578f1014225690f0841c95f
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
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
268 hexdump(buf
, buf_len
, "Unknown> ");
272 /* Queue packet until first respone to 'K' is received */
273 if (wait_for_h
&& buf
[0] != 'H') {
274 struct queued_rx
**rxp
= &qrx
;
277 rxp
= &((*rxp
)->next
);
279 *rxp
= malloc(sizeof(struct queued_rx
));
285 memset(*rxp
, 0, sizeof(struct queued_rx
));
286 (*rxp
)->len
= outpos
-out
;
287 (*rxp
)->rx
= malloc((*rxp
)->len
);
292 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
293 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
301 print_timestamp(stdout
);
303 for (i
= 0; i
< outpos
-out
-2; i
++)
304 printf("%c", out
[i
]);
308 w
= write(fd
, out
, outpos
-out
);
314 /* Send all queued packets */
316 struct queued_rx
*curr_rx
= qrx
;
317 struct queued_rx
*last_rx
;
323 print_timestamp(stdout
);
325 for (i
= 0; i
< curr_rx
->len
-2; i
++)
326 printf("%c", curr_rx
->rx
[i
]);
330 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
335 curr_rx
= curr_rx
->next
;
349 static int hmlan_parse_in(int fd
, void *data
)
351 struct hmcfgusb_dev
*dev
= data
;
353 uint8_t out
[0x40]; //FIXME!!!
360 memset(buf
, 0, sizeof(buf
));
362 r
= read(fd
, buf
, sizeof(buf
)-1);
364 uint8_t *inend
= buf
+ r
;
368 while (inpos
< inend
) {
369 uint8_t *instart
= inpos
;
371 if ((*inpos
== '\r') || (*inpos
== '\n')) {
378 last
= inend
- inpos
;
380 for (i
= 0; i
< last
; i
++) {
381 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
391 print_timestamp(stdout
);
393 for (i
= 0; i
< last
; i
++)
394 printf("%c", instart
[i
]);
398 memset(out
, 0, sizeof(out
));
399 *outpos
++ = *inpos
++;
403 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
404 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
405 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
406 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_LENGTH_BYTE
);
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
)), 0);
413 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
416 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
420 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
423 if (errno
!= ECONNRESET
)
433 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
435 struct hmcfgusb_dev
*dev
;
436 uint8_t out
[0x40]; //FIXME!!!
439 hmcfgusb_set_debug(debug
);
441 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
443 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
447 if (dev
->bootloader
) {
449 printf("HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
451 hmcfgusb_leave_bootloader(dev
);
458 if ((reboot_at_hour
!= -1) && (reboot_at_minute
!= -1)) {
463 tm_s
= localtime(&tm
);
469 tm_s
->tm_hour
= reboot_at_hour
;
470 tm_s
->tm_min
= reboot_at_minute
;
474 reboot_seconds
= tm
- dev
->opened_at
;
476 while (reboot_seconds
<= 0)
477 reboot_seconds
+= 86400;
480 if (verbose
&& reboot_seconds
)
481 printf("Rebooting in %u seconds\n", reboot_seconds
);
484 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
485 fprintf(stderr
, "Can't add client to pollfd!\n");
490 if (master_socket
>= 0) {
491 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
492 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
498 memset(out
, 0, sizeof(out
));
501 hmcfgusb_send_null_frame(dev
, 1);
502 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
507 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
509 if (fd
== master_socket
) {
512 client
= accept(master_socket
, NULL
, 0);
514 shutdown(client
, SHUT_RDWR
);
518 if (hmlan_parse_in(fd
, dev
) <= 0) {
522 } else if (fd
== -1) {
524 if (errno
!= ETIMEDOUT
) {
525 perror("hmcfgusb_poll");
528 /* periodically wakeup the device */
529 hmcfgusb_send_null_frame(dev
, 1);
531 memset(out
, 0, sizeof(out
));
533 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
539 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
541 printf("HM-CFG-USB running since %lu seconds, rebooting now...\n",
542 time(NULL
) - dev
->opened_at
);
544 hmcfgusb_enter_bootloader(dev
);
552 void sigterm_handler(int sig
)
554 if (unlink(PID_FILE
) == -1)
555 perror("Can't remove PID file");
560 #define FLAG_DAEMON (1 << 0)
561 #define FLAG_PID_FILE (1 << 1)
563 static int socket_server(char *iface
, int port
, int flags
)
565 struct sigaction sact
;
566 struct sockaddr_in sin
;
571 if (flags
& FLAG_DAEMON
) {
572 FILE *pidfile
= NULL
;
574 if (flags
& FLAG_PID_FILE
) {
577 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
579 if (errno
== EEXIST
) {
581 pidfile
= fopen(PID_FILE
, "r");
583 perror("PID file " PID_FILE
" already exists, already running?");
587 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
589 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
595 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
598 perror("Can't create PID file " PID_FILE
);
602 pidfile
= fdopen(fd
, "w");
604 perror("Can't reopen PID file fd");
608 memset(&sact
, 0, sizeof(sact
));
609 sact
.sa_handler
= sigterm_handler
;
611 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
612 perror("sigaction(SIGTERM)");
620 fprintf(pidfile
, "%u\n", pid
);
624 printf("Daemon with PID %u started!\n", pid
);
626 } else if (pid
< 0) {
635 memset(&sact
, 0, sizeof(sact
));
636 sact
.sa_handler
= SIG_IGN
;
638 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
639 perror("sigaction(SIGPIPE)");
643 impersonate_hmlanif
= 1;
645 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
647 perror("Can't open socket");
652 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
653 perror("Can't set socket options");
657 memset(&sin
, 0, sizeof(sin
));
658 sin
.sin_family
= AF_INET
;
659 sin
.sin_port
= htons(port
);
661 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
663 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
664 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
669 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
670 perror("Can't bind socket");
674 if (listen(sock
, 1) == -1) {
675 perror("Can't listen on socket");
680 struct sockaddr_in csin
;
683 in_addr_t client_addr
;
685 memset(&csin
, 0, sizeof(csin
));
686 csinlen
= sizeof(csin
);
687 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
689 perror("Couldn't accept client");
693 /* FIXME: getnameinfo... */
694 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
697 print_timestamp(stdout
);
698 printf("Client %d.%d.%d.%d connected!\n",
699 (client_addr
& 0xff000000) >> 24,
700 (client_addr
& 0x00ff0000) >> 16,
701 (client_addr
& 0x0000ff00) >> 8,
702 (client_addr
& 0x000000ff));
705 comm(client
, client
, sock
, flags
);
707 shutdown(client
, SHUT_RDWR
);
711 print_timestamp(stdout
);
712 printf("Connection to %d.%d.%d.%d closed!\n",
713 (client_addr
& 0xff000000) >> 24,
714 (client_addr
& 0x00ff0000) >> 16,
715 (client_addr
& 0x0000ff00) >> 8,
716 (client_addr
& 0x000000ff));
724 static int interactive_server(int flags
)
726 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
732 void hmlan_syntax(char *prog
)
734 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
735 fprintf(stderr
, "Possible options:\n");
736 fprintf(stderr
, "\t-D\t\tdebug mode\n");
737 fprintf(stderr
, "\t-d\t\tdaemon mode\n");
738 fprintf(stderr
, "\t-h\t\tthis help\n");
739 fprintf(stderr
, "\t-i\t\tinteractive mode (connect HM-CFG-USB to terminal)\n");
740 fprintf(stderr
, "\t-l ip\t\tlisten on given IP address only (for example 127.0.0.1)\n");
741 fprintf(stderr
, "\t-P\t\tcreate PID file " PID_FILE
" in daemon mode\n");
742 fprintf(stderr
, "\t-p n\t\tlisten on port n (default: 1000)\n");
743 fprintf(stderr
, "\t-r n\t\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS
);
744 fprintf(stderr
, "\t hh:mm\treboot HM-CFG-USB daily at hh:mm\n");
745 fprintf(stderr
, "\t-v\t\tverbose mode\n");
746 fprintf(stderr
, "\t-V\t\tshow version (" VERSION
")\n");
750 int main(int argc
, char **argv
)
759 reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
761 while((opt
= getopt(argc
, argv
, "DdhiPp:Rr:l:vV")) != -1) {
768 flags
|= FLAG_DAEMON
;
774 flags
|= FLAG_PID_FILE
;
777 port
= strtoul(optarg
, &ep
, 10);
779 fprintf(stderr
, "Can't parse port!\n");
784 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
787 reboot_seconds
= strtoul(optarg
, &ep
, 10);
790 reboot_at_hour
= reboot_seconds
;
792 reboot_at_minute
= strtoul(ep
, &ep
, 10);
794 fprintf(stderr
, "Can't parse reboot-time!\n");
800 fprintf(stderr
, "Can't parse reboot-timeout!\n");
812 printf("hmland " VERSION
"\n");
813 printf("Copyright (c) 2013 Michael Gernoth\n\n");
819 hmlan_syntax(argv
[0]);
826 return interactive_server(flags
);
828 return socket_server(iface
, port
, flags
);