]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
1ccc843d23017f87de2ee12d04225c28615212c6
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>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <libusb-1.0/libusb.h>
44 #define PID_FILE "/var/run/hmland.pid"
48 static int impersonate_hmlanif
= 0;
50 static int verbose
= 0;
55 struct queued_rx
*next
;
58 static struct queued_rx
*qrx
= NULL
;
59 static int wait_for_h
= 0;
61 #define FLAG_LENGTH_BYTE (1<<0)
62 #define FLAG_FORMAT_HEX (1<<1)
63 #define FLAG_COMMA_BEFORE (1<<2)
64 #define FLAG_COMMA_AFTER (1<<3)
65 #define FLAG_NL (1<<4)
66 #define FLAG_IGNORE_COMMAS (1<<5)
68 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
69 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
71 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
73 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
74 'A', 'B', 'C', 'D', 'E', 'F'};
75 uint8_t *buf_out
= *outpos
;
76 uint8_t *outend
= *outpos
+ outlen
;
77 uint8_t *inend
= *inpos
+ inlen
;
80 if (flags
& FLAG_COMMA_BEFORE
) {
86 if (flags
& FLAG_LENGTH_BYTE
) {
92 if (flags
& FLAG_FORMAT_HEX
) {
95 for (i
= 0; i
< len
; i
++) {
96 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
98 **outpos
= nibble
[((**inpos
) & 0xf)];
99 *inpos
+= 1; *outpos
+= 1;
104 memcpy(*outpos
, *inpos
, len
);
109 if (flags
& FLAG_COMMA_AFTER
) {
115 if (flags
& FLAG_NL
) {
123 return *outpos
- buf_out
;
126 static uint8_t ascii_to_nibble(uint8_t a
)
130 if ((a
>= '0') && (a
<= '9')) {
132 } else if ((a
>= 'A') && (a
<= 'F')) {
134 } else if ((a
>= 'a') && (a
<= 'f')) {
141 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
143 uint8_t *buf_out
= *outpos
;
144 uint8_t *outend
= *outpos
+ outlen
;
145 uint8_t *inend
= *inpos
+ inlen
;
147 if (flags
& FLAG_LENGTH_BYTE
) {
155 if (!(flags
& FLAG_IGNORE_COMMAS
))
164 **outpos
= (len
/ 2);
168 while(*inpos
< inend
) {
169 if (**inpos
== ',') {
171 if (!(flags
& FLAG_IGNORE_COMMAS
))
180 **outpos
= ascii_to_nibble(**inpos
) << 4;
182 **outpos
|= ascii_to_nibble(**inpos
);
183 *inpos
+= 1; *outpos
+= 1;
186 return *outpos
- buf_out
;
189 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
194 int fd
= *((int*)data
);
200 memset(out
, 0, sizeof(out
));
204 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
207 if (impersonate_hmlanif
) {
212 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
213 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
214 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
215 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
216 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
217 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
218 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
222 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
223 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
224 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
225 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
226 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
227 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
);
231 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
232 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
233 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
234 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 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
);
236 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
);
240 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
241 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 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
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
247 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
248 hexdump(buf
, buf_len
, "Unknown> ");
252 /* Queue packet until first respone to 'K' is received */
253 if (wait_for_h
&& buf
[0] != 'H') {
254 struct queued_rx
**rxp
= &qrx
;
257 rxp
= &((*rxp
)->next
);
259 *rxp
= malloc(sizeof(struct queued_rx
));
265 memset(*rxp
, 0, sizeof(struct queued_rx
));
266 (*rxp
)->len
= outpos
-out
;
267 (*rxp
)->rx
= malloc((*rxp
)->len
);
272 memset((*rxp
)->rx
, 0, (*rxp
)->len
);
273 memcpy((*rxp
)->rx
, out
, (*rxp
)->len
);
282 for (i
= 0; i
< outpos
-out
-2; i
++)
283 printf("%c", out
[i
]);
287 w
= write(fd
, out
, outpos
-out
);
293 /* Send al queued packets */
295 struct queued_rx
*curr_rx
= qrx
;
296 struct queued_rx
*last_rx
;
303 for (i
= 0; i
< curr_rx
->len
-2; i
++)
304 printf("%c", curr_rx
->rx
[i
]);
308 w
= write(fd
, curr_rx
->rx
, curr_rx
->len
);
314 curr_rx
= curr_rx
->next
;
328 static int hmlan_parse_in(int fd
, void *data
)
330 struct hmcfgusb_dev
*dev
= data
;
332 uint8_t out
[0x40]; //FIXME!!!
339 memset(buf
, 0, sizeof(buf
));
341 r
= read(fd
, buf
, sizeof(buf
)-1);
343 uint8_t *inend
= buf
+ r
;
347 while (inpos
< inend
) {
348 uint8_t *instart
= inpos
;
350 if ((*inpos
== '\r') || (*inpos
== '\n')) {
357 last
= inend
- inpos
;
359 for (i
= 0; i
< last
; i
++) {
360 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
371 for (i
= 0; i
< last
; i
++)
372 printf("%c", instart
[i
]);
376 memset(out
, 0, sizeof(out
));
377 *outpos
++ = *inpos
++;
381 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
382 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
383 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
384 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
385 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
386 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
389 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
390 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
391 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
394 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
398 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
410 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
412 struct hmcfgusb_dev
*dev
;
413 uint8_t out
[0x40]; //FIXME!!!
416 hmcfgusb_set_debug(debug
);
418 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
420 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
424 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
425 fprintf(stderr
, "Can't add client to pollfd!\n");
430 if (master_socket
>= 0) {
431 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
432 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
438 memset(out
, 0, sizeof(out
));
441 hmcfgusb_send_null_frame(dev
);
442 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
447 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
449 if (fd
== master_socket
) {
452 client
= accept(master_socket
, NULL
, 0);
454 shutdown(client
, SHUT_RDWR
);
458 if (hmlan_parse_in(fd
, dev
) <= 0) {
462 } else if (fd
== -1) {
464 perror("hmcfgusb_poll");
467 /* periodically wakeup the device */
468 hmcfgusb_send_null_frame(dev
);
477 void sigterm_handler(int sig
)
479 if (unlink(PID_FILE
) == -1)
480 perror("Can't remove PID file");
485 #define FLAG_DAEMON (1 << 0)
486 #define FLAG_PID_FILE (1 << 1)
488 static int socket_server(char *iface
, int port
, int flags
)
490 struct sigaction sact
;
491 struct sockaddr_in sin
;
496 if (flags
& FLAG_DAEMON
) {
497 FILE *pidfile
= NULL
;
499 if (flags
& FLAG_PID_FILE
) {
502 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
504 if (errno
== EEXIST
) {
506 pidfile
= fopen(PID_FILE
, "r");
508 perror("PID file " PID_FILE
" already exists, already running?");
512 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
514 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
520 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
523 perror("Can't create PID file " PID_FILE
);
527 pidfile
= fdopen(fd
, "w");
529 perror("Can't reopen PID file fd");
533 memset(&sact
, 0, sizeof(sact
));
534 sact
.sa_handler
= sigterm_handler
;
536 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
537 perror("sigaction(SIGTERM)");
545 fprintf(pidfile
, "%u\n", pid
);
549 printf("Daemon with PID %u started!\n", pid
);
551 } else if (pid
< 0) {
560 memset(&sact
, 0, sizeof(sact
));
561 sact
.sa_handler
= SIG_IGN
;
563 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
564 perror("sigaction(SIGPIPE)");
568 impersonate_hmlanif
= 1;
570 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
572 perror("Can't open socket");
577 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
578 perror("Can't set socket options");
582 memset(&sin
, 0, sizeof(sin
));
583 sin
.sin_family
= AF_INET
;
584 sin
.sin_port
= htons(port
);
586 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
588 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
594 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
595 perror("Can't bind socket");
599 if (listen(sock
, 1) == -1) {
600 perror("Can't listen on socket");
605 struct sockaddr_in csin
;
608 in_addr_t client_addr
;
610 memset(&csin
, 0, sizeof(csin
));
611 csinlen
= sizeof(csin
);
612 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
614 perror("Couldn't accept client");
618 /* FIXME: getnameinfo... */
619 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
622 printf("Client %d.%d.%d.%d connected!\n",
623 (client_addr
& 0xff000000) >> 24,
624 (client_addr
& 0x00ff0000) >> 16,
625 (client_addr
& 0x0000ff00) >> 8,
626 (client_addr
& 0x000000ff));
629 comm(client
, client
, sock
, flags
);
631 shutdown(client
, SHUT_RDWR
);
635 printf("Connection to %d.%d.%d.%d closed!\n",
636 (client_addr
& 0xff000000) >> 24,
637 (client_addr
& 0x00ff0000) >> 16,
638 (client_addr
& 0x0000ff00) >> 8,
639 (client_addr
& 0x000000ff));
647 static int interactive_server(int flags
)
649 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
655 void hmlan_syntax(char *prog
)
657 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
658 fprintf(stderr
, "Possible options:\n");
659 fprintf(stderr
, "\t-D\tdebug mode\n");
660 fprintf(stderr
, "\t-d\tdaemon mode\n");
661 fprintf(stderr
, "\t-h\tthis help\n");
662 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
663 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
664 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
665 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
666 fprintf(stderr
, "\t-v\tverbose mode\n");
670 int main(int argc
, char **argv
)
679 while((opt
= getopt(argc
, argv
, "DdhiPp:Rl:v")) != -1) {
686 flags
|= FLAG_DAEMON
;
692 flags
|= FLAG_PID_FILE
;
695 port
= strtoul(optarg
, &ep
, 10);
697 fprintf(stderr
, "Can't parse port!\n");
702 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
714 hmlan_syntax(argv
[0]);
721 return interactive_server(flags
);
723 return socket_server(iface
, port
, flags
);