]>
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
) {
445 fprintf(stderr
, "HM-CFG-USB in bootloader mode, restarting in normal mode...\n");
446 memset(out
, 0, sizeof(out
));
448 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
454 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
455 fprintf(stderr
, "Can't add client to pollfd!\n");
460 if (master_socket
>= 0) {
461 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
462 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
468 memset(out
, 0, sizeof(out
));
471 hmcfgusb_send_null_frame(dev
, 1);
472 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
477 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
479 if (fd
== master_socket
) {
482 client
= accept(master_socket
, NULL
, 0);
484 shutdown(client
, SHUT_RDWR
);
488 if (hmlan_parse_in(fd
, dev
) <= 0) {
492 } else if (fd
== -1) {
494 if (errno
!= ETIMEDOUT
) {
495 perror("hmcfgusb_poll");
498 /* periodically wakeup the device */
499 hmcfgusb_send_null_frame(dev
, 1);
501 memset(out
, 0, sizeof(out
));
503 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
509 if (reboot_seconds
&& ((dev
->opened_at
+ reboot_seconds
) <= time(NULL
))) {
511 fprintf(stderr
, "HM-CFG-USB running since %lu seconds, rebooting now...\n",
512 time(NULL
) - dev
->opened_at
);
514 hmcfgusb_enter_bootloader(dev
);
522 void sigterm_handler(int sig
)
524 if (unlink(PID_FILE
) == -1)
525 perror("Can't remove PID file");
530 #define FLAG_DAEMON (1 << 0)
531 #define FLAG_PID_FILE (1 << 1)
533 static int socket_server(char *iface
, int port
, int flags
)
535 struct sigaction sact
;
536 struct sockaddr_in sin
;
541 if (flags
& FLAG_DAEMON
) {
542 FILE *pidfile
= NULL
;
544 if (flags
& FLAG_PID_FILE
) {
547 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
549 if (errno
== EEXIST
) {
551 pidfile
= fopen(PID_FILE
, "r");
553 perror("PID file " PID_FILE
" already exists, already running?");
557 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
559 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
565 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
568 perror("Can't create PID file " PID_FILE
);
572 pidfile
= fdopen(fd
, "w");
574 perror("Can't reopen PID file fd");
578 memset(&sact
, 0, sizeof(sact
));
579 sact
.sa_handler
= sigterm_handler
;
581 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
582 perror("sigaction(SIGTERM)");
590 fprintf(pidfile
, "%u\n", pid
);
594 printf("Daemon with PID %u started!\n", pid
);
596 } else if (pid
< 0) {
605 memset(&sact
, 0, sizeof(sact
));
606 sact
.sa_handler
= SIG_IGN
;
608 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
609 perror("sigaction(SIGPIPE)");
613 impersonate_hmlanif
= 1;
615 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
617 perror("Can't open socket");
622 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
623 perror("Can't set socket options");
627 memset(&sin
, 0, sizeof(sin
));
628 sin
.sin_family
= AF_INET
;
629 sin
.sin_port
= htons(port
);
631 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
633 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
634 fprintf(stderr
, "Can't convert IP %s, aborting!\n", iface
);
639 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
640 perror("Can't bind socket");
644 if (listen(sock
, 1) == -1) {
645 perror("Can't listen on socket");
650 struct sockaddr_in csin
;
653 in_addr_t client_addr
;
655 memset(&csin
, 0, sizeof(csin
));
656 csinlen
= sizeof(csin
);
657 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
659 perror("Couldn't accept client");
663 /* FIXME: getnameinfo... */
664 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
667 print_timestamp(stdout
);
668 printf("Client %d.%d.%d.%d connected!\n",
669 (client_addr
& 0xff000000) >> 24,
670 (client_addr
& 0x00ff0000) >> 16,
671 (client_addr
& 0x0000ff00) >> 8,
672 (client_addr
& 0x000000ff));
675 comm(client
, client
, sock
, flags
);
677 shutdown(client
, SHUT_RDWR
);
681 print_timestamp(stdout
);
682 printf("Connection to %d.%d.%d.%d closed!\n",
683 (client_addr
& 0xff000000) >> 24,
684 (client_addr
& 0x00ff0000) >> 16,
685 (client_addr
& 0x0000ff00) >> 8,
686 (client_addr
& 0x000000ff));
694 static int interactive_server(int flags
)
696 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
702 void hmlan_syntax(char *prog
)
704 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
705 fprintf(stderr
, "Possible options:\n");
706 fprintf(stderr
, "\t-D\tdebug mode\n");
707 fprintf(stderr
, "\t-d\tdaemon mode\n");
708 fprintf(stderr
, "\t-h\tthis help\n");
709 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
710 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
711 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
712 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
713 fprintf(stderr
, "\t-r n\treboot HM-CFG-USB after n seconds (0: no reboot, default: %u)\n", DEFAULT_REBOOT_SECONDS
);
714 fprintf(stderr
, "\t-v\tverbose mode\n");
718 int main(int argc
, char **argv
)
727 reboot_seconds
= DEFAULT_REBOOT_SECONDS
;
729 while((opt
= getopt(argc
, argv
, "DdhiPp:Rr:l:v")) != -1) {
736 flags
|= FLAG_DAEMON
;
742 flags
|= FLAG_PID_FILE
;
745 port
= strtoul(optarg
, &ep
, 10);
747 fprintf(stderr
, "Can't parse port!\n");
752 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
755 reboot_seconds
= strtoul(optarg
, &ep
, 10);
757 fprintf(stderr
, "Can't parse reboot-timeout!\n");
771 hmlan_syntax(argv
[0]);
778 return interactive_server(flags
);
780 return socket_server(iface
, port
, flags
);