]>
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"
49 static int impersonate_hmlanif
= 0;
51 static int verbose
= 0;
56 struct queued_rx
*next
;
59 static struct queued_rx
*qrx
= NULL
;
60 static int wait_for_h
= 0;
62 #define FLAG_LENGTH_BYTE (1<<0)
63 #define FLAG_FORMAT_HEX (1<<1)
64 #define FLAG_COMMA_BEFORE (1<<2)
65 #define FLAG_COMMA_AFTER (1<<3)
66 #define FLAG_NL (1<<4)
67 #define FLAG_IGNORE_COMMAS (1<<5)
69 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
70 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
72 static void print_timestamp(FILE *f
)
78 gettimeofday(&tv
, NULL
);
79 tmp
= localtime(&tv
.tv_sec
);
80 memset(ts
, 0, sizeof(ts
));
81 strftime(ts
, sizeof(ts
)-1, "%Y-%m-%d %H:%M:%S", tmp
);
82 fprintf(f
, "%s.%06ld: ", ts
, tv
.tv_usec
);
85 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
87 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
88 'A', 'B', 'C', 'D', 'E', 'F'};
89 uint8_t *buf_out
= *outpos
;
90 uint8_t *outend
= *outpos
+ outlen
;
91 uint8_t *inend
= *inpos
+ inlen
;
94 if (flags
& FLAG_COMMA_BEFORE
) {
100 if (flags
& FLAG_LENGTH_BYTE
) {
106 if (flags
& FLAG_FORMAT_HEX
) {
109 for (i
= 0; i
< len
; i
++) {
110 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
112 **outpos
= nibble
[((**inpos
) & 0xf)];
113 *inpos
+= 1; *outpos
+= 1;
118 memcpy(*outpos
, *inpos
, len
);
123 if (flags
& FLAG_COMMA_AFTER
) {
129 if (flags
& FLAG_NL
) {
137 return *outpos
- buf_out
;
140 static uint8_t ascii_to_nibble(uint8_t a
)
144 if ((a
>= '0') && (a
<= '9')) {
146 } else if ((a
>= 'A') && (a
<= 'F')) {
148 } else if ((a
>= 'a') && (a
<= 'f')) {
155 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
157 uint8_t *buf_out
= *outpos
;
158 uint8_t *outend
= *outpos
+ outlen
;
159 uint8_t *inend
= *inpos
+ inlen
;
161 if (flags
& FLAG_LENGTH_BYTE
) {
169 if (!(flags
& FLAG_IGNORE_COMMAS
))
178 **outpos
= (len
/ 2);
182 while(*inpos
< inend
) {
183 if (**inpos
== ',') {
185 if (!(flags
& FLAG_IGNORE_COMMAS
))
194 **outpos
= ascii_to_nibble(**inpos
) << 4;
196 **outpos
|= ascii_to_nibble(**inpos
);
197 *inpos
+= 1; *outpos
+= 1;
200 return *outpos
- buf_out
;
203 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
208 int fd
= *((int*)data
);
214 memset(out
, 0, sizeof(out
));
218 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
221 if (impersonate_hmlanif
) {
226 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
227 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
228 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
229 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
230 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
231 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
232 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
236 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
237 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
238 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
239 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
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
)), 0, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
| FLAG_NL
);
245 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
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
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
248 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
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
)), 0, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
| FLAG_NL
);
254 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
255 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
256 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
257 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
261 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
262 hexdump(buf
, buf_len
, "Unknown> ");
266 /* Queue packet until first respone to 'K' is received */
267 if (wait_for_h
&& buf
[0] != 'H') {
268 struct queued_rx
**rxp
= &qrx
;
271 rxp
= &((*rxp
)->next
);
273 *rxp
= malloc(sizeof(struct queued_rx
));
279 memset(*rxp
, 0, sizeof(struct queued_rx
));
280 (*rxp
)->len
= outpos
-out
;
281 (*rxp
)->rx
= malloc((*rxp
)->len
);
286 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
287 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
295 print_timestamp(stdout
);
297 for (i
= 0; i
< outpos
-out
-2; i
++)
298 printf("%c", out
[i
]);
302 w
= write(fd
, out
, outpos
-out
);
308 /* Send all queued packets */
310 struct queued_rx
*curr_rx
= qrx
;
311 struct queued_rx
*last_rx
;
317 print_timestamp(stdout
);
319 for (i
= 0; i
< curr_rx
->len
-2; i
++)
320 printf("%c", curr_rx
->rx
[i
]);
324 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
329 curr_rx
= curr_rx
->next
;
343 static int hmlan_parse_in(int fd
, void *data
)
345 struct hmcfgusb_dev
*dev
= data
;
347 uint8_t out
[0x40]; //FIXME!!!
354 memset(buf
, 0, sizeof(buf
));
356 r
= read(fd
, buf
, sizeof(buf
)-1);
358 uint8_t *inend
= buf
+ r
;
362 while (inpos
< inend
) {
363 uint8_t *instart
= inpos
;
365 if ((*inpos
== '\r') || (*inpos
== '\n')) {
372 last
= inend
- inpos
;
374 for (i
= 0; i
< last
; i
++) {
375 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
385 print_timestamp(stdout
);
387 for (i
= 0; i
< last
; i
++)
388 printf("%c", instart
[i
]);
392 memset(out
, 0, sizeof(out
));
393 *outpos
++ = *inpos
++;
397 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
398 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
399 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_LENGTH_BYTE
);
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
)), FLAG_LENGTH_BYTE
);
410 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
414 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
417 if (errno
!= ECONNRESET
)
427 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
429 struct hmcfgusb_dev
*dev
;
430 uint8_t out
[0x40]; //FIXME!!!
433 hmcfgusb_set_debug(debug
);
435 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
437 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
441 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
442 fprintf(stderr
, "Can't add client to pollfd!\n");
447 if (master_socket
>= 0) {
448 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
449 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
455 memset(out
, 0, sizeof(out
));
458 hmcfgusb_send_null_frame(dev
, 1);
459 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
464 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
466 if (fd
== master_socket
) {
469 client
= accept(master_socket
, NULL
, 0);
471 shutdown(client
, SHUT_RDWR
);
475 if (hmlan_parse_in(fd
, dev
) <= 0) {
479 } else if (fd
== -1) {
481 if (errno
!= ETIMEDOUT
) {
482 perror("hmcfgusb_poll");
485 /* periodically wakeup the device */
486 hmcfgusb_send_null_frame(dev
, 1);
488 memset(out
, 0, sizeof(out
));
490 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
501 void sigterm_handler(int sig
)
503 if (unlink(PID_FILE
) == -1)
504 perror("Can't remove PID file");
509 #define FLAG_DAEMON (1 << 0)
510 #define FLAG_PID_FILE (1 << 1)
512 static int socket_server(char *iface
, int port
, int flags
)
514 struct sigaction sact
;
515 struct sockaddr_in sin
;
520 if (flags
& FLAG_DAEMON
) {
521 FILE *pidfile
= NULL
;
523 if (flags
& FLAG_PID_FILE
) {
526 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
528 if (errno
== EEXIST
) {
530 pidfile
= fopen(PID_FILE
, "r");
532 perror("PID file " PID_FILE
" already exists, already running?");
536 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
538 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
544 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
547 perror("Can't create PID file " PID_FILE
);
551 pidfile
= fdopen(fd
, "w");
553 perror("Can't reopen PID file fd");
557 memset(&sact
, 0, sizeof(sact
));
558 sact
.sa_handler
= sigterm_handler
;
560 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
561 perror("sigaction(SIGTERM)");
569 fprintf(pidfile
, "%u\n", pid
);
573 printf("Daemon with PID %u started!\n", pid
);
575 } else if (pid
< 0) {
584 memset(&sact
, 0, sizeof(sact
));
585 sact
.sa_handler
= SIG_IGN
;
587 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
588 perror("sigaction(SIGPIPE)");
592 impersonate_hmlanif
= 1;
594 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
596 perror("Can't open socket");
601 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
602 perror("Can't set socket options");
606 memset(&sin
, 0, sizeof(sin
));
607 sin
.sin_family
= AF_INET
;
608 sin
.sin_port
= htons(port
);
610 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
612 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
618 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
619 perror("Can't bind socket");
623 if (listen(sock
, 1) == -1) {
624 perror("Can't listen on socket");
629 struct sockaddr_in csin
;
632 in_addr_t client_addr
;
634 memset(&csin
, 0, sizeof(csin
));
635 csinlen
= sizeof(csin
);
636 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
638 perror("Couldn't accept client");
642 /* FIXME: getnameinfo... */
643 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
646 print_timestamp(stdout
);
647 printf("Client %d.%d.%d.%d connected!\n",
648 (client_addr
& 0xff000000) >> 24,
649 (client_addr
& 0x00ff0000) >> 16,
650 (client_addr
& 0x0000ff00) >> 8,
651 (client_addr
& 0x000000ff));
654 comm(client
, client
, sock
, flags
);
656 shutdown(client
, SHUT_RDWR
);
660 print_timestamp(stdout
);
661 printf("Connection to %d.%d.%d.%d closed!\n",
662 (client_addr
& 0xff000000) >> 24,
663 (client_addr
& 0x00ff0000) >> 16,
664 (client_addr
& 0x0000ff00) >> 8,
665 (client_addr
& 0x000000ff));
673 static int interactive_server(int flags
)
675 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
681 void hmlan_syntax(char *prog
)
683 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
684 fprintf(stderr
, "Possible options:\n");
685 fprintf(stderr
, "\t-D\tdebug mode\n");
686 fprintf(stderr
, "\t-d\tdaemon mode\n");
687 fprintf(stderr
, "\t-h\tthis help\n");
688 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
689 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
690 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
691 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
692 fprintf(stderr
, "\t-v\tverbose mode\n");
696 int main(int argc
, char **argv
)
705 while((opt
= getopt(argc
, argv
, "DdhiPp:Rl:v")) != -1) {
712 flags
|= FLAG_DAEMON
;
718 flags
|= FLAG_PID_FILE
;
721 port
= strtoul(optarg
, &ep
, 10);
723 fprintf(stderr
, "Can't parse port!\n");
728 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
740 hmlan_syntax(argv
[0]);
747 return interactive_server(flags
);
749 return socket_server(iface
, port
, flags
);