]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
3dae5459b0dc06c7c335d8f93991fd3c303133bf
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;
52 #define FLAG_LENGTH_BYTE (1<<0)
53 #define FLAG_FORMAT_HEX (1<<1)
54 #define FLAG_COMMA_BEFORE (1<<2)
55 #define FLAG_COMMA_AFTER (1<<3)
56 #define FLAG_NL (1<<4)
57 #define FLAG_IGNORE_COMMAS (1<<5)
59 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
60 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
62 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
64 const uint8_t nibble
[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
65 'A', 'B', 'C', 'D', 'E', 'F'};
66 uint8_t *buf_out
= *outpos
;
67 uint8_t *outend
= *outpos
+ outlen
;
68 uint8_t *inend
= *inpos
+ inlen
;
71 if (flags
& FLAG_COMMA_BEFORE
) {
77 if (flags
& FLAG_LENGTH_BYTE
) {
83 if (flags
& FLAG_FORMAT_HEX
) {
86 for (i
= 0; i
< len
; i
++) {
87 **outpos
= nibble
[((**inpos
) & 0xf0) >> 4];
89 **outpos
= nibble
[((**inpos
) & 0xf)];
90 *inpos
+= 1; *outpos
+= 1;
95 memcpy(*outpos
, *inpos
, len
);
100 if (flags
& FLAG_COMMA_AFTER
) {
106 if (flags
& FLAG_NL
) {
114 return *outpos
- buf_out
;
117 static uint8_t ascii_to_nibble(uint8_t a
)
121 if ((a
>= '0') && (a
<= '9')) {
123 } else if ((a
>= 'A') && (a
<= 'F')) {
125 } else if ((a
>= 'a') && (a
<= 'f')) {
132 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
134 uint8_t *buf_out
= *outpos
;
135 uint8_t *outend
= *outpos
+ outlen
;
136 uint8_t *inend
= *inpos
+ inlen
;
138 if (flags
& FLAG_LENGTH_BYTE
) {
146 if (!(flags
& FLAG_IGNORE_COMMAS
))
155 **outpos
= (len
/ 2);
159 while(*inpos
< inend
) {
160 if (**inpos
== ',') {
162 if (!(flags
& FLAG_IGNORE_COMMAS
))
171 **outpos
= ascii_to_nibble(**inpos
) << 4;
173 **outpos
|= ascii_to_nibble(**inpos
);
174 *inpos
+= 1; *outpos
+= 1;
177 return *outpos
- buf_out
;
180 static int hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
185 int fd
= *((int*)data
);
191 memset(out
, 0, sizeof(out
));
195 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
198 if (impersonate_hmlanif
) {
203 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
204 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
205 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
206 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
207 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
208 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
209 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
213 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
214 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
215 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
216 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
217 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
218 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
);
222 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, 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
)), 1, FLAG_FORMAT_HEX
);
232 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
233 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 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
| FLAG_NL
);
238 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
239 hexdump(buf
, buf_len
, "Unknown> ");
246 for (i
= 0; i
< outpos
-out
; i
++)
247 printf("%c", out
[i
]);
250 w
= write(fd
, out
, outpos
-out
);
259 static int hmlan_parse_in(int fd
, void *data
)
261 struct hmcfgusb_dev
*dev
= data
;
263 uint8_t out
[0x40]; //FIXME!!!
270 memset(buf
, 0, sizeof(buf
));
272 r
= read(fd
, buf
, sizeof(buf
)-1);
274 uint8_t *inend
= buf
+ r
;
278 while (inpos
< inend
) {
279 uint8_t *instart
= inpos
;
281 if ((*inpos
== '\r') || (*inpos
== '\n')) {
288 last
= inend
- inpos
;
290 for (i
= 0; i
< last
; i
++) {
291 if ((inpos
[i
] == '\r') || (inpos
[i
] == '\n')) {
302 for (i
= 0; i
< last
; i
++)
303 printf("%c", instart
[i
]);
307 memset(out
, 0, sizeof(out
));
308 *outpos
++ = *inpos
++;
312 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
313 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
314 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
315 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
316 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
317 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
320 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
321 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
322 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
325 parse_part_in(&inpos
, (last
-(inpos
-instart
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
329 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
341 static int comm(int fd_in
, int fd_out
, int master_socket
, int flags
)
343 struct hmcfgusb_dev
*dev
;
344 uint8_t out
[0x40]; //FIXME!!!
347 hmcfgusb_set_debug(debug
);
349 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
351 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
355 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
356 fprintf(stderr
, "Can't add client to pollfd!\n");
361 if (master_socket
>= 0) {
362 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
363 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
369 memset(out
, 0, sizeof(out
));
371 hmcfgusb_send_null_frame(dev
);
372 hmcfgusb_send(dev
, out
, sizeof(out
), 1);
377 fd
= hmcfgusb_poll(dev
, 1); /* Wakeup device/bus at least once a second */
379 if (fd
== master_socket
) {
382 client
= accept(master_socket
, NULL
, 0);
384 shutdown(client
, SHUT_RDWR
);
388 if (hmlan_parse_in(fd
, dev
) <= 0) {
392 } else if (fd
== -1) {
394 perror("hmcfgusb_poll");
397 /* periodically wakeup the device */
398 hmcfgusb_send_null_frame(dev
);
407 void sigterm_handler(int sig
)
409 if (unlink(PID_FILE
) == -1)
410 perror("Can't remove PID file");
415 #define FLAG_DAEMON (1 << 0)
416 #define FLAG_PID_FILE (1 << 1)
418 static int socket_server(char *iface
, int port
, int flags
)
420 struct sigaction sact
;
421 struct sockaddr_in sin
;
426 if (flags
& FLAG_DAEMON
) {
427 FILE *pidfile
= NULL
;
429 if (flags
& FLAG_PID_FILE
) {
432 fd
= open(PID_FILE
, O_CREAT
| O_EXCL
| O_WRONLY
, 0644);
434 if (errno
== EEXIST
) {
436 pidfile
= fopen(PID_FILE
, "r");
438 perror("PID file " PID_FILE
" already exists, already running?");
442 if (fscanf(pidfile
, "%u", &old_pid
) != 1) {
444 fprintf(stderr
, "Can't read old PID from " PID_FILE
", already running?\n");
450 fprintf(stderr
, "Already running with PID %u according to " PID_FILE
"!\n", old_pid
);
453 perror("Can't create PID file " PID_FILE
);
457 pidfile
= fdopen(fd
, "w");
459 perror("Can't reopen PID file fd");
463 memset(&sact
, 0, sizeof(sact
));
464 sact
.sa_handler
= sigterm_handler
;
466 if (sigaction(SIGTERM
, &sact
, NULL
) == -1) {
467 perror("sigaction(SIGTERM)");
475 fprintf(pidfile
, "%u\n", pid
);
479 printf("Daemon with PID %u started!\n", pid
);
481 } else if (pid
< 0) {
490 memset(&sact
, 0, sizeof(sact
));
491 sact
.sa_handler
= SIG_IGN
;
493 if (sigaction(SIGPIPE
, &sact
, NULL
) == -1) {
494 perror("sigaction(SIGPIPE)");
498 impersonate_hmlanif
= 1;
500 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
502 perror("Can't open socket");
507 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
508 perror("Can't set socket options");
512 memset(&sin
, 0, sizeof(sin
));
513 sin
.sin_family
= AF_INET
;
514 sin
.sin_port
= htons(port
);
516 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
518 if (inet_pton(AF_INET
, iface
, &(sin
.sin_addr
.s_addr
)) != 1) {
524 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
525 perror("Can't bind socket");
529 if (listen(sock
, 1) == -1) {
530 perror("Can't listen on socket");
535 struct sockaddr_in csin
;
538 in_addr_t client_addr
;
540 memset(&csin
, 0, sizeof(csin
));
541 csinlen
= sizeof(csin
);
542 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
544 perror("Couldn't accept client");
548 /* FIXME: getnameinfo... */
549 client_addr
= ntohl(csin
.sin_addr
.s_addr
);
552 printf("Client %d.%d.%d.%d connected!\n",
553 (client_addr
& 0xff000000) >> 24,
554 (client_addr
& 0x00ff0000) >> 16,
555 (client_addr
& 0x0000ff00) >> 8,
556 (client_addr
& 0x000000ff));
559 comm(client
, client
, sock
, flags
);
561 shutdown(client
, SHUT_RDWR
);
565 printf("Connection to %d.%d.%d.%d closed!\n",
566 (client_addr
& 0xff000000) >> 24,
567 (client_addr
& 0x00ff0000) >> 16,
568 (client_addr
& 0x0000ff00) >> 8,
569 (client_addr
& 0x000000ff));
577 static int interactive_server(int flags
)
579 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1, flags
))
585 void hmlan_syntax(char *prog
)
587 fprintf(stderr
, "Syntax: %s options\n\n", prog
);
588 fprintf(stderr
, "Possible options:\n");
589 fprintf(stderr
, "\t-D\tdebug mode\n");
590 fprintf(stderr
, "\t-d\tdaemon mode\n");
591 fprintf(stderr
, "\t-h\tthis help\n");
592 fprintf(stderr
, "\t-i\tinteractive mode (connect HM-CFG-USB to terminal)\n");
593 fprintf(stderr
, "\t-l ip\tlisten on given IP address only (for example 127.0.0.1)\n");
594 fprintf(stderr
, "\t-P\tcreate PID file " PID_FILE
" in daemon mode\n");
595 fprintf(stderr
, "\t-p n\tlisten on port n (default 1000)\n");
596 fprintf(stderr
, "\t-v\tverbose mode\n");
600 int main(int argc
, char **argv
)
609 while((opt
= getopt(argc
, argv
, "DdhiPp:Rl:v")) != -1) {
616 flags
|= FLAG_DAEMON
;
622 flags
|= FLAG_PID_FILE
;
625 port
= strtoul(optarg
, &ep
, 10);
627 fprintf(stderr
, "Can't parse port!\n");
632 fprintf(stderr
, "-R is no longer needed (1s wakeup is default)\n");
644 hmlan_syntax(argv
[0]);
651 return interactive_server(flags
);
653 return socket_server(iface
, port
, flags
);