]>
git.zerfleddert.de Git - hmcfgusb/blob - hmland.c
3366c8b5dae00023fb13ee2f60766a923ef9b55c
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 <libusb-1.0/libusb.h>
37 void hmlan_format_out(uint8_t *buf
, int buf_len
, void *data
)
45 //FIXME: Buffer here and write to fd_out
53 for (i
= 2; i
< len
+ 2; i
++) {
56 printf(",%02X%02X,", buf
[i
],
61 for (; i
< len
; i
++) {
66 for (; i
< len
; i
++) {
67 printf("%02X", buf
[i
]);
82 for (i
= 0; i
< len
; i
++) {
84 printf("%02X", buf
[1+i
]);
100 for (i
= 0; i
< len
; i
++) {
102 printf("%02X", buf
[1+i
]);
117 //HM> 0x0000: 49 00 00 00 00 55 53 42 2d 49 46 03 bc 0a 4a 45 I....USB-IF...JE
118 //HM> 0x0010: 51 30 35 33 35 31 32 32 1d b1 55 68 ea 13 00 14 Q0535122..Uh....
119 //HM> 0x0020: 9f a6 00 03 00 00 00 00 00 00 00 00 00 00 00 00 ................
120 //HM> 0x0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
122 for (i
= 1; i
< buf_len
; i
++)
123 printf("%02X", buf
[i
]);
124 hexdump(buf
, buf_len
, "Unknown> ");
130 void hmlan_parse_in(int fd
, void *data
)
132 struct hmcfgusb_dev
*dev
= data
;
133 unsigned char buf
[1024];
134 unsigned char send_buf
[0x40]; //FIXME!!!
139 r
= read(fd
, buf
, sizeof(buf
));
143 memset(send_buf
, 0, sizeof(send_buf
));
144 for (i
= 0; i
< r
; i
++) {
145 if ((buf
[i
] == 0x0a) ||
152 send_buf
[0] = buf
[0];
155 for (i
= 1; i
< r
; i
++) {
161 memmove(buf
+i
+2, buf
+i
+1, r
-(i
+1));
162 snprintf(tmp
, 3, "%02X", (int)((r
-(i
+1))/2));
163 memcpy(buf
+i
, tmp
, 2);
168 memmove(buf
+i
, buf
+i
+1, r
-(i
+1));
176 memset(tmp
, 0, sizeof(tmp
));
177 for (i
= 1; i
< r
; i
+=2) {
178 memcpy(tmp
, buf
+ i
, 2);
179 send_buf
[1+(i
/2)] = strtoul(tmp
, NULL
, 16);
181 hmcfgusb_send(dev
, send_buf
, 1+(i
/2), 1);
187 int main(int argc
, char **argv
)
189 struct hmcfgusb_dev
*dev
;
192 dev
= hmcfgusb_init(hmlan_format_out
, NULL
);
194 fprintf(stderr
, "Can't initialize HM-CFG-USB!\n");
198 if (!hmcfgusb_add_pfd(dev
, STDIN_FILENO
, POLLIN
)) {
199 fprintf(stderr
, "Can't add stdin to pollfd!\n");
203 hmcfgusb_send(dev
, (unsigned char*)"K", 1, 1);
208 fd
= hmcfgusb_poll(dev
, 3600);
210 hmlan_parse_in(fd
, dev
);
211 } else if (fd
== -1) {
213 perror("hmcfgusb_poll");