]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
a9d9670186f8dc522b1bc772b3a1db689f0d3267
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
);
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 perror("hmcfgusb_poll");
484 /* periodically wakeup the device */
485 hmcfgusb_send_null_frame(dev
);
494 void sigterm_handler(int sig
)
496 if (unlink(PID_FILE
) == -1)
497 perror("Can't remove PID file");
502 #define FLAG_DAEMON (1 << 0)
503 #define FLAG_PID_FILE (1 << 1)
505 static int socket_server(char *iface
, int port
, int flags
)
507 struct sigaction sact
;
508 struct sockaddr_in sin
;
513 if (flags
& FLAG_DAEMON
) {
514 FILE *pidfile
= NULL
;
516 if (flags
& FLAG_PID_FILE
) {
519 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
521 if (errno
== EEXIST
) {
523 pidfile
= fopen(PID_FILE
, "r");
525 perror("PID file " PID_FILE
" already exists, already running?");
529 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
531 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
537 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
540 perror("Can't create PID file " PID_FILE
);
544 pidfile
= fdopen(fd
, "w");
546 perror("Can't reopen PID file fd");
550 memset(&sact
, 0, sizeof(sact
));
551 sact
.sa_handler
= sigterm_handler
;
553 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
554 perror("sigaction(SIGTERM)");
562 fprintf(pidfile
, "%u\n", pid
);
566 printf("Daemon with PID %u started!\n", pid
);
568 } else if (pid
< 0) {
577 memset(&sact
, 0, sizeof(sact
));
578 sact
.sa_handler
= SIG_IGN
;
580 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
581 perror("sigaction(SIGPIPE)");
585 impersonate_hmlanif
= 1;
587 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
589 perror("Can't open socket");
594 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
595 perror("Can't set socket options");
599 memset(&sin
, 0, sizeof(sin
));
600 sin
.sin_family
= AF_INET
;
601 sin
.sin_port
= htons(port
);
603 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
605 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
611 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
612 perror("Can't bind socket");
616 if (listen(sock
, 1) == -1) {
617 perror("Can't listen on socket");
622 struct sockaddr_in csin
;
625 in_addr_t client_addr
;
627 memset(&csin
, 0, sizeof(csin
));
628 csinlen
= sizeof(csin
);
629 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
631 perror("Couldn't accept client");
635 /* FIXME: getnameinfo... */
636 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
639 print_timestamp(stdout
);
640 printf("Client %d.%d.%d.%d connected!\n",
641 (client_addr
& 0xff000000) >> 24,
642 (client_addr
& 0x00ff0000) >> 16,
643 (client_addr
& 0x0000ff00) >> 8,
644 (client_addr
& 0x000000ff));
647 comm(client
, client
, sock
, flags
);
649 shutdown(client
, SHUT_RDWR
);
653 print_timestamp(stdout
);
654 printf("Connection to %d.%d.%d.%d closed!\n",
655 (client_addr
& 0xff000000) >> 24,
656 (client_addr
& 0x00ff0000) >> 16,
657 (client_addr
& 0x0000ff00) >> 8,
658 (client_addr
& 0x000000ff));
666 static int interactive_server(int flags
)
668 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
674 void hmlan_syntax(char *prog
)
676 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
677 fprintf(stderr
, "Possible options:\n");
678 fprintf(stderr
, "\t-D\tdebug mode\n");
679 fprintf(stderr
, "\t-d\tdaemon mode\n");
680 fprintf(stderr
, "\t-h\tthis help\n");
681 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
682 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
683 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
684 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
685 fprintf(stderr
, "\t-v\tverbose mode\n");
689 int main(int argc
, char **argv
)
698 while((opt
= getopt(argc
, argv
, "DdhiPp:Rl:v")) != -1) {
705 flags
|= FLAG_DAEMON
;
711 flags
|= FLAG_PID_FILE
;
714 port
= strtoul(optarg
, &ep
, 10);
716 fprintf(stderr
, "Can't parse port!\n");
721 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
733 hmlan_syntax(argv
[0]);
740 return interactive_server(flags
);
742 return socket_server(iface
, port
, flags
);