]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
f8071649001504b8fb1dfdf074c2f7e1d3c6bb79
1 /* HM-CFG-LAN emuldation 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
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <libusb-1.0/libusb.h>
41 static int impersonate_hmlanif
= 0;
43 #define FLAG_LENGTH_BYTE (1<<0)
44 #define FLAG_FORMAT_HEX (1<<1)
45 #define FLAG_COMMA_BEFORE (1<<2)
46 #define FLAG_COMMA_AFTER (1<<3)
47 #define FLAG_NL (1<<4)
48 #define FLAG_IGNORE_COMMAS (1<<5)
50 #define CHECK_SPACE(x) if ((*outpos + x) > outend) { fprintf(stderr, "Not enough space!\n"); return 0; }
51 #define CHECK_AVAIL(x) if ((*inpos + x) > inend) { fprintf(stderr, "Not enough input available!\n"); return 0; }
53 static int format_part_out(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int len
, int flags
)
55 uint8_t *buf_out
= *outpos
;
56 uint8_t *outend
= *outpos
+ outlen
;
57 uint8_t *inend
= *inpos
+ inlen
;
60 if (flags
& FLAG_COMMA_BEFORE
) {
66 if (flags
& FLAG_LENGTH_BYTE
) {
72 if (flags
& FLAG_FORMAT_HEX
) {
75 memset(hex
, 0, sizeof(hex
));
79 for (i
= 0; i
< len
; i
++) {
80 if (snprintf(hex
, sizeof(hex
), "%02X", **inpos
) != 2) {
81 fprintf(stderr
, "Can't format hex-string!\n");
85 memcpy(*outpos
, hex
, 2);
91 memcpy(*outpos
, *inpos
, len
);
96 if (flags
& FLAG_COMMA_AFTER
) {
102 if (flags
& FLAG_NL
) {
110 return *outpos
- buf_out
;
113 static int parse_part_in(uint8_t **inpos
, int inlen
, uint8_t **outpos
, int outlen
, int flags
)
115 uint8_t *buf_out
= *outpos
;
116 uint8_t *outend
= *outpos
+ outlen
;
117 uint8_t *inend
= *inpos
+ inlen
;
120 memset(hex
, 0, sizeof(hex
));
122 if (flags
& FLAG_LENGTH_BYTE
) {
130 if (!(flags
& FLAG_IGNORE_COMMAS
))
139 **outpos
= (len
/ 2);
143 while(*inpos
< inend
) {
144 if (**inpos
== ',') {
146 if (!(flags
& FLAG_IGNORE_COMMAS
))
154 memcpy(hex
, *inpos
, 2);
157 **outpos
= strtoul(hex
, NULL
, 16);
161 return *outpos
- buf_out
;
164 static void hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
169 int fd
= *((int*)data
);
174 memset(out
, 0, sizeof(out
));
178 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 0);
181 if (impersonate_hmlanif
) {
186 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_LENGTH_BYTE
);
187 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
188 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0, FLAG_COMMA_BEFORE
| FLAG_LENGTH_BYTE
);
189 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
190 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
191 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
192 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
196 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 3, FLAG_FORMAT_HEX
);
197 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
198 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
199 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
200 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
201 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
);
205 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
);
206 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 2, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
207 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 4, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
);
208 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 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
);
210 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
);
214 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, FLAG_FORMAT_HEX
);
215 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 1, 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
)), 1, FLAG_FORMAT_HEX
| FLAG_COMMA_BEFORE
| FLAG_NL
);
221 format_part_out(&inpos
, (buf_len
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), buf_len
-1, FLAG_FORMAT_HEX
| FLAG_NL
);
222 hexdump(buf
, buf_len
, "Unknown> ");
225 write(fd
, out
, outpos
-out
);
228 static int hmlan_parse_in(int fd
, void *data
)
230 struct hmcfgusb_dev
*dev
= data
;
232 uint8_t out
[0x40]; //FIXME!!!
238 r
= read(fd
, buf
, sizeof(buf
));
240 memset(out
, 0, sizeof(out
));
241 for (i
= 0; i
< r
; i
++) {
242 if ((buf
[i
] == 0x0a) ||
258 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
259 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
260 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
261 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
262 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), 0);
263 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_LENGTH_BYTE
);
266 parse_part_in(&inpos
, (r
-(inpos
-buf
)), &outpos
, (sizeof(out
)-(outpos
-out
)), FLAG_IGNORE_COMMAS
);
270 hmcfgusb_send(dev
, out
, outpos
-out
, 1);
281 static int comm(int fd_in
, int fd_out
, int master_socket
)
283 struct hmcfgusb_dev
*dev
;
286 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
288 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
292 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
293 fprintf(stderr
, "Can't add client to pollfd!\n");
298 if (master_socket
>= 0) {
299 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
300 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
306 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
311 fd
= hmcfgusb_poll(dev
, 3600);
313 if (fd
== master_socket
) {
316 client
= accept(master_socket
, NULL
, 0);
318 shutdown(client
, SHUT_RDWR
);
322 if (hmlan_parse_in(fd
, dev
) <= 0) {
326 } else if (fd
== -1) {
328 perror("hmcfgusb_poll");
338 static int socket_server(int port
)
340 struct sockaddr_in sin
;
344 impersonate_hmlanif
= 1;
346 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
348 perror("Can't open socket");
353 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
354 perror("Can't set socket options");
358 memset(&sin
, 0, sizeof(sin
));
359 sin
.sin_family
= AF_INET
;
360 sin
.sin_port
= htons(port
);
361 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
363 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
364 perror("Can't bind socket");
368 if (listen(sock
, 1) == -1) {
369 perror("Can't listen on socket");
374 struct sockaddr_in csin
;
378 memset(&csin
, 0, sizeof(csin
));
379 csinlen
= sizeof(csin
);
380 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
382 perror("Couldn't accept client");
386 printf("Client accepted!\n");
388 comm(client
, client
, sock
);
390 shutdown(client
, SHUT_RDWR
);
393 printf("Connection closed!\n");
400 static int interactive_server(void)
402 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1))
408 int main(int argc
, char **argv
)
410 //return interactive_server();
411 return socket_server(1234);