]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
d91026a8c8ee928e215ba68287f090974c768609
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 all 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
);
313 curr_rx
= curr_rx
->next
;
327 static int hmlan_parse_in(int fd
, void *data
)
329 struct hmcfgusb_dev
*dev
= data
;
331 uint8_t out
[0x40]; //FIXME!!!
338 memset(buf
, 0, sizeof(buf
));
340 r
= read(fd
, buf
, sizeof(buf
)-1);
342 uint8_t *inend
= buf
+ r
;
346 while (inpos
< inend
) {
347 uint8_t *instart
= inpos
;
349 if ((*inpos
== '\r') || (*inpos
== '\n')) {
356 last
= inend
- inpos
;
358 for (i
= 0; i
< last
; i
++) {
359 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
370 for (i
= 0; i
< last
; i
++)
371 printf("%c", instart
[i
]);
375 memset(out
, 0, sizeof(out
));
376 *outpos
++ = *inpos
++;
380 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_LENGTH_BYTE
);
388 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
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
)), FLAG_LENGTH_BYTE
);
393 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
397 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
409 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
411 struct hmcfgusb_dev
*dev
;
412 uint8_t out
[0x40]; //FIXME!!!
415 hmcfgusb_set_debug(debug
);
417 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
419 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
423 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
424 fprintf(stderr
, "Can't add client to pollfd!\n");
429 if (master_socket
>= 0) {
430 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
431 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
437 memset(out
, 0, sizeof(out
));
440 hmcfgusb_send_null_frame(dev
);
441 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
446 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
448 if (fd
== master_socket
) {
451 client
= accept(master_socket
, NULL
, 0);
453 shutdown(client
, SHUT_RDWR
);
457 if (hmlan_parse_in(fd
, dev
) <= 0) {
461 } else if (fd
== -1) {
463 perror("hmcfgusb_poll");
466 /* periodically wakeup the device */
467 hmcfgusb_send_null_frame(dev
);
476 void sigterm_handler(int sig
)
478 if (unlink(PID_FILE
) == -1)
479 perror("Can't remove PID file");
484 #define FLAG_DAEMON (1 << 0)
485 #define FLAG_PID_FILE (1 << 1)
487 static int socket_server(char *iface
, int port
, int flags
)
489 struct sigaction sact
;
490 struct sockaddr_in sin
;
495 if (flags
& FLAG_DAEMON
) {
496 FILE *pidfile
= NULL
;
498 if (flags
& FLAG_PID_FILE
) {
501 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
503 if (errno
== EEXIST
) {
505 pidfile
= fopen(PID_FILE
, "r");
507 perror("PID file " PID_FILE
" already exists, already running?");
511 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
513 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
519 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
522 perror("Can't create PID file " PID_FILE
);
526 pidfile
= fdopen(fd
, "w");
528 perror("Can't reopen PID file fd");
532 memset(&sact
, 0, sizeof(sact
));
533 sact
.sa_handler
= sigterm_handler
;
535 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
536 perror("sigaction(SIGTERM)");
544 fprintf(pidfile
, "%u\n", pid
);
548 printf("Daemon with PID %u started!\n", pid
);
550 } else if (pid
< 0) {
559 memset(&sact
, 0, sizeof(sact
));
560 sact
.sa_handler
= SIG_IGN
;
562 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
563 perror("sigaction(SIGPIPE)");
567 impersonate_hmlanif
= 1;
569 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
571 perror("Can't open socket");
576 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
577 perror("Can't set socket options");
581 memset(&sin
, 0, sizeof(sin
));
582 sin
.sin_family
= AF_INET
;
583 sin
.sin_port
= htons(port
);
585 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
587 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
593 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
594 perror("Can't bind socket");
598 if (listen(sock
, 1) == -1) {
599 perror("Can't listen on socket");
604 struct sockaddr_in csin
;
607 in_addr_t client_addr
;
609 memset(&csin
, 0, sizeof(csin
));
610 csinlen
= sizeof(csin
);
611 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
613 perror("Couldn't accept client");
617 /* FIXME: getnameinfo... */
618 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
621 printf("Client %d.%d.%d.%d connected!\n",
622 (client_addr
& 0xff000000) >> 24,
623 (client_addr
& 0x00ff0000) >> 16,
624 (client_addr
& 0x0000ff00) >> 8,
625 (client_addr
& 0x000000ff));
628 comm(client
, client
, sock
, flags
);
630 shutdown(client
, SHUT_RDWR
);
634 printf("Connection to %d.%d.%d.%d closed!\n",
635 (client_addr
& 0xff000000) >> 24,
636 (client_addr
& 0x00ff0000) >> 16,
637 (client_addr
& 0x0000ff00) >> 8,
638 (client_addr
& 0x000000ff));
646 static int interactive_server(int flags
)
648 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
654 void hmlan_syntax(char *prog
)
656 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
657 fprintf(stderr
, "Possible options:\n");
658 fprintf(stderr
, "\t-D\tdebug mode\n");
659 fprintf(stderr
, "\t-d\tdaemon mode\n");
660 fprintf(stderr
, "\t-h\tthis help\n");
661 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
662 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
663 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
664 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
665 fprintf(stderr
, "\t-v\tverbose mode\n");
669 int main(int argc
, char **argv
)
678 while((opt
= getopt(argc
, argv
, "DdhiPp:Rl:v")) != -1) {
685 flags
|= FLAG_DAEMON
;
691 flags
|= FLAG_PID_FILE
;
694 port
= strtoul(optarg
, &ep
, 10);
696 fprintf(stderr
, "Can't parse port!\n");
701 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
713 hmlan_syntax(argv
[0]);
720 return interactive_server(flags
);
722 return socket_server(iface
, port
, flags
);