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 static void hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
47 int fd
= *((int*)data
);
54 memset(out
, 0, sizeof(out
));
60 if (impersonate_hmlanif
) {
66 for (i
= 2; i
< len
+ 2; i
++) {
69 snprintf(pos
, 7, ",%02X%02X,", buf
[i
], buf
[i
+1]);
75 for (; i
< len
; i
++) {
80 for (; i
< len
; i
++) {
81 snprintf(pos
, 3, "%02X", buf
[i
]);
98 for (i
= 0; i
< len
; i
++) {
100 snprintf(pos
, 3, "%02X", buf
[i
+1]);
118 for (i
= 0; i
< len
; i
++) {
120 snprintf(pos
, 3, "%02X", buf
[i
+1]);
137 //HM> 0x0000: 49 00 00 00 00 55 53 42 2d 49 46 03 bc 0a 4a 45 I....USB-IF...JE
138 //HM> 0x0010: 51 30 35 33 35 31 32 32 1d b1 55 68 ea 13 00 14 Q0535122..Uh....
139 //HM> 0x0020: 9f a6 00 03 00 00 00 00 00 00 00 00 00 00 00 00 ................
140 //HM> 0x0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
142 for (i
= 1; i
< buf_len
; i
++) {
143 snprintf(pos
, 3, "%02X", buf
[i
]);
146 hexdump(buf
, buf_len
, "Unknown> ");
151 write(fd
, out
, pos
-out
);
154 static int hmlan_parse_in(int fd
, void *data
)
156 struct hmcfgusb_dev
*dev
= data
;
157 unsigned char buf
[1024];
158 unsigned char send_buf
[0x40]; //FIXME!!!
163 r
= read(fd
, buf
, sizeof(buf
));
167 memset(send_buf
, 0, sizeof(send_buf
));
168 for (i
= 0; i
< r
; i
++) {
169 if ((buf
[i
] == 0x0a) ||
176 send_buf
[0] = buf
[0];
179 for (i
= 1; i
< r
; i
++) {
185 memmove(buf
+i
+2, buf
+i
+1, r
-(i
+1));
186 snprintf(tmp
, 3, "%02X", (int)((r
-(i
+1))/2));
187 memcpy(buf
+i
, tmp
, 2);
192 memmove(buf
+i
, buf
+i
+1, r
-(i
+1));
200 memset(tmp
, 0, sizeof(tmp
));
201 for (i
= 1; i
< r
; i
+=2) {
202 memcpy(tmp
, buf
+ i
, 2);
203 send_buf
[1+(i
/2)] = strtoul(tmp
, NULL
, 16);
205 hmcfgusb_send(dev
, send_buf
, 1+(i
/2), 1);
216 static int comm(int fd_in
, int fd_out
, int master_socket
)
218 struct hmcfgusb_dev
*dev
;
221 dev
= hmcfgusb_init(hmlan_format_out
, &fd_out
);
223 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
227 if (!hmcfgusb_add_pfd(dev
, fd_in
, POLLIN
)) {
228 fprintf(stderr
, "Can't add client to pollfd!\n");
233 if (master_socket
>= 0) {
234 if (!hmcfgusb_add_pfd(dev
, master_socket
, POLLIN
)) {
235 fprintf(stderr
, "Can't add master_socket to pollfd!\n");
241 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
246 fd
= hmcfgusb_poll(dev
, 3600);
248 if (fd
== master_socket
) {
251 client
= accept(master_socket
, NULL
, 0);
253 shutdown(client
, SHUT_RDWR
);
257 if (hmlan_parse_in(fd
, dev
) <= 0) {
261 } else if (fd
== -1) {
263 perror("hmcfgusb_poll");
273 static int socket_server(int port
)
275 struct sockaddr_in sin
;
279 impersonate_hmlanif
= 1;
281 sock
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
);
283 perror("Can't open socket");
288 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &n
, sizeof(n
)) == -1) {
289 perror("Can't set socket options");
293 memset(&sin
, 0, sizeof(sin
));
294 sin
.sin_family
= AF_INET
;
295 sin
.sin_port
= htons(port
);
296 sin
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
298 if (bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) == -1) {
299 perror("Can't bind socket");
303 if (listen(sock
, 1) == -1) {
304 perror("Can't listen on socket");
309 struct sockaddr_in csin
;
313 memset(&csin
, 0, sizeof(csin
));
314 csinlen
= sizeof(csin
);
315 client
= accept(sock
, (struct sockaddr
*)&csin
, &csinlen
);
317 perror("Couldn't accept client");
321 printf("Client accepted!\n");
323 comm(client
, client
, sock
);
325 shutdown(client
, SHUT_RDWR
);
328 printf("Connection closed!\n");
335 static int interactive_server(void)
337 if (!comm(STDIN_FILENO
, STDOUT_FILENO
, -1))
343 int main(int argc
, char **argv
)
345 //return interactive_server();
346 return socket_server(1234);