]>
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>
45 #define PID_FILE "/var/run/hmland.pid"
47 #define DEFAULT_REBOOT_SECONDS 86400
51 static int impersonate_hmlanif
= 0;
53 static int verbose
= 0;
54 static int reboot_seconds
= 0;
59 struct queued_rx
*next
;
62 static struct queued_rx
*qrx
= NULL
;
63 static int wait_for_h
= 0;
65 #define FLAG_LENGTH_BYTE (1<<0)
66 #define FLAG_FORMAT_HEX (1<<1)
67 #define FLAG_COMMA_BEFORE (1<<2)
68 #define FLAG_COMMA_AFTER (1<<3)
69 #define FLAG_NL (1<<4)
70 #define FLAG_IGNORE_COMMAS (1<<5)
72 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
73 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
75 static void print_timestamp(FILE *f
)
81 gettimeofday(&tv
, NULL
);
82 tmp
= localtime(&tv
.tv_sec
);
83 memset(ts
, 0, sizeof(ts
));
84 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
85 fprintf(f
, "%s.%06ld: ", ts
, tv
.tv_usec
);
88 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
90 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
91 'A', 'B', 'C', 'D', 'E', 'F'};
92 uint8_t *buf_out
= *outpos
;
93 uint8_t *outend
= *outpos
+ outlen
;
94 uint8_t *inend
= *inpos
+ inlen
;
97 if (flags
& FLAG_COMMA_BEFORE
) {
103 if (flags
& FLAG_LENGTH_BYTE
) {
109 if (flags
& FLAG_FORMAT_HEX
) {
112 for (i
= 0; i
< len
; i
++) {
113 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
115 **outpos
= nibble
[((**inpos
) & 0xf)];
116 *inpos
+= 1; *outpos
+= 1;
121 memcpy(*outpos
, *inpos
, len
);
126 if (flags
& FLAG_COMMA_AFTER
) {
132 if (flags
& FLAG_NL
) {
140 return *outpos
- buf_out
;
143 static uint8_t ascii_to_nibble(uint8_t a
)
147 if ((a
>= '0') && (a
<= '9')) {
149 } else if ((a
>= 'A') && (a
<= 'F')) {
151 } else if ((a
>= 'a') && (a
<= 'f')) {
158 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
160 uint8_t *buf_out
= *outpos
;
161 uint8_t *outend
= *outpos
+ outlen
;
162 uint8_t *inend
= *inpos
+ inlen
;
164 if (flags
& FLAG_LENGTH_BYTE
) {
172 if (!(flags
& FLAG_IGNORE_COMMAS
))
181 **outpos
= (len
/ 2);
185 while(*inpos
< inend
) {
186 if (**inpos
== ',') {
188 if (!(flags
& FLAG_IGNORE_COMMAS
))
197 **outpos
= ascii_to_nibble(**inpos
) << 4;
199 **outpos
|= ascii_to_nibble(**inpos
);
200 *inpos
+= 1; *outpos
+= 1;
203 return *outpos
- buf_out
;
206 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
211 int fd
= *((int*)data
);
217 memset(out
, 0, sizeof(out
));
221 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
224 if (impersonate_hmlanif
) {
229 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
230 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
231 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
232 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
233 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
234 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
235 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
239 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
240 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
241 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
242 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
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
)), 0, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
| FLAG_NL
);
248 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
249 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
250 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
251 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
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
)), 0, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
| FLAG_NL
);
257 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
258 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
259 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
260 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
264 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
265 hexdump(buf
, buf_len
, "Unknown> ");
269 /* Queue packet until first respone to 'K' is received */
270 if (wait_for_h
&& buf
[0] != 'H') {
271 struct queued_rx
**rxp
= &qrx
;
274 rxp
= &((*rxp
)->next
);
276 *rxp
= malloc(sizeof(struct queued_rx
));
282 memset(*rxp
, 0, sizeof(struct queued_rx
));
283 (*rxp
)->len
= outpos
-out
;
284 (*rxp
)->rx
= malloc((*rxp
)->len
);
289 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
290 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
298 print_timestamp(stdout
);
300 for (i
= 0; i
< outpos
-out
-2; i
++)
301 printf("%c", out
[i
]);
305 w
= write(fd
, out
, outpos
-out
);
311 /* Send all queued packets */
313 struct queued_rx
*curr_rx
= qrx
;
314 struct queued_rx
*last_rx
;
320 print_timestamp(stdout
);
322 for (i
= 0; i
< curr_rx
->len
-2; i
++)
323 printf("%c", curr_rx
->rx
[i
]);
327 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
332 curr_rx
= curr_rx
->next
;
346 static int hmlan_parse_in(int fd
, void *data
)
348 struct hmcfgusb_dev
*dev
= data
;
350 uint8_t out
[0x40]; //FIXME!!!
357 memset(buf
, 0, sizeof(buf
));
359 r
= read(fd
, buf
, sizeof(buf
)-1);
361 uint8_t *inend
= buf
+ r
;
365 while (inpos
< inend
) {
366 uint8_t *instart
= inpos
;
368 if ((*inpos
== '\r') || (*inpos
== '\n')) {
375 last
= inend
- inpos
;
377 for (i
= 0; i
< last
; i
++) {
378 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
388 print_timestamp(stdout
);
390 for (i
= 0; i
< last
; i
++)
391 printf("%c", instart
[i
]);
395 memset(out
, 0, sizeof(out
));
396 *outpos
++ = *inpos
++;
400 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
401 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
402 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_LENGTH_BYTE
);
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
)), FLAG_LENGTH_BYTE
);
413 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
417 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
420 if (errno
!= ECONNRESET
)
430 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
432 struct hmcfgusb_dev
*dev
;
433 uint8_t out
[0x40]; //FIXME!!!
436 hmcfgusb_set_debug(debug
);
438 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
440 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
444 if (dev
->bootloader
) {
446 fprintf(stderr
, "HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
448 hmcfgusb_leave_bootloader(dev
);
455 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
456 fprintf(stderr
, "Can't add client to pollfd!\n");
461 if (master_socket
>= 0) {
462 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
463 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
469 memset(out
, 0, sizeof(out
));
472 hmcfgusb_send_null_frame(dev
, 1);
473 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
478 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
480 if (fd
== master_socket
) {
483 client
= accept(master_socket
, NULL
, 0);
485 shutdown(client
, SHUT_RDWR
);
489 if (hmlan_parse_in(fd
, dev
) <= 0) {
493 } else if (fd
== -1) {
495 if (errno
!= ETIMEDOUT
) {
496 perror("hmcfgusb_poll");
499 /* periodically wakeup the device */
500 hmcfgusb_send_null_frame(dev
, 1);
502 memset(out
, 0, sizeof(out
));
504 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
510 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
512 fprintf(stderr
, "HM-CFG-USB running since %lu seconds, rebooting now...\n",
513 time(NULL
) - dev
->opened_at
);
515 hmcfgusb_enter_bootloader(dev
);
523 void sigterm_handler(int sig
)
525 if (unlink(PID_FILE
) == -1)
526 perror("Can't remove PID file");
531 #define FLAG_DAEMON (1 << 0)
532 #define FLAG_PID_FILE (1 << 1)
534 static int socket_server(char *iface
, int port
, int flags
)
536 struct sigaction sact
;
537 struct sockaddr_in sin
;
542 if (flags
& FLAG_DAEMON
) {
543 FILE *pidfile
= NULL
;
545 if (flags
& FLAG_PID_FILE
) {
548 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
550 if (errno
== EEXIST
) {
552 pidfile
= fopen(PID_FILE
, "r");
554 perror("PID file " PID_FILE
" already exists, already running?");
558 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
560 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
566 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
569 perror("Can't create PID file " PID_FILE
);
573 pidfile
= fdopen(fd
, "w");
575 perror("Can't reopen PID file fd");
579 memset(&sact
, 0, sizeof(sact
));
580 sact
.sa_handler
= sigterm_handler
;
582 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
583 perror("sigaction(SIGTERM)");
591 fprintf(pidfile
, "%u\n", pid
);
595 printf("Daemon with PID %u started!\n", pid
);
597 } else if (pid
< 0) {
606 memset(&sact
, 0, sizeof(sact
));
607 sact
.sa_handler
= SIG_IGN
;
609 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
610 perror("sigaction(SIGPIPE)");
614 impersonate_hmlanif
= 1;
616 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
618 perror("Can't open socket");
623 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
624 perror("Can't set socket options");
628 memset(&sin
, 0, sizeof(sin
));
629 sin
.sin_family
= AF_INET
;
630 sin
.sin_port
= htons(port
);
632 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
634 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
635 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
640 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
641 perror("Can't bind socket");
645 if (listen(sock
, 1) == -1) {
646 perror("Can't listen on socket");
651 struct sockaddr_in csin
;
654 in_addr_t client_addr
;
656 memset(&csin
, 0, sizeof(csin
));
657 csinlen
= sizeof(csin
);
658 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
660 perror("Couldn't accept client");
664 /* FIXME: getnameinfo... */
665 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
668 print_timestamp(stdout
);
669 printf("Client %d.%d.%d.%d connected!\n",
670 (client_addr
& 0xff000000) >> 24,
671 (client_addr
& 0x00ff0000) >> 16,
672 (client_addr
& 0x0000ff00) >> 8,
673 (client_addr
& 0x000000ff));
676 comm(client
, client
, sock
, flags
);
678 shutdown(client
, SHUT_RDWR
);
682 print_timestamp(stdout
);
683 printf("Connection to %d.%d.%d.%d closed!\n",
684 (client_addr
& 0xff000000) >> 24,
685 (client_addr
& 0x00ff0000) >> 16,
686 (client_addr
& 0x0000ff00) >> 8,
687 (client_addr
& 0x000000ff));
695 static int interactive_server(int flags
)
697 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
703 void hmlan_syntax(char *prog
)
705 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
706 fprintf(stderr
, "Possible options:\n");
707 fprintf(stderr
, "\t-D\tdebug mode\n");
708 fprintf(stderr
, "\t-d\tdaemon mode\n");
709 fprintf(stderr
, "\t-h\tthis help\n");
710 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
711 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
712 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
713 fprintf(stderr
, "\t-p n\tlisten on port n (default: 1000)\n");
714 fprintf(stderr
, "\t-r n\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS
);
715 fprintf(stderr
, "\t-v\tverbose mode\n");
719 int main(int argc
, char **argv
)
728 reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
730 while((opt
= getopt(argc
, argv
, "DdhiPp:Rr:l:v")) != -1) {
737 flags
|= FLAG_DAEMON
;
743 flags
|= FLAG_PID_FILE
;
746 port
= strtoul(optarg
, &ep
, 10);
748 fprintf(stderr
, "Can't parse port!\n");
753 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
756 reboot_seconds
= strtoul(optarg
, &ep
, 10);
758 fprintf(stderr
, "Can't parse reboot-timeout!\n");
772 hmlan_syntax(argv
[0]);
779 return interactive_server(flags
);
781 return socket_server(iface
, port
, flags
);