-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbto_ir_cmd.c
324 lines (273 loc) · 7.3 KB
/
bto_ir_cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <libusb-1.0/libusb.h>
#define VENDOR_ID 0x22ea
#define PRODUCT_ID 0x001e
#define BTO_EP_IN 0x84
#define BTO_EP_OUT 0x04
#define MAX_SIZE 64
#define IR_DATA_SIZE 7
#define IR_DATA_SIZE_EX 35
#define BTO_CMD_ECHO_BACK 0x41
#define BTO_CMD_GET_DATA 0x50
#define BTO_CMD_SET_RECEIVE_MODE 0x51
#define BTO_CMD_GET_DATA_EX 0x52
#define BTO_CMD_SET_RECEIVE_MODE_EX 0x53
#define BTO_CMD_GET_FIRMWARE_VERSION 0x56
#define BTO_CMD_SET_DATA 0x60
#define BTO_CMD_SET_DATA_EX 0x61
#define RECEIVE_WAIT_MODE_NONE 0
#define RECEIVE_WAIT_MODE_WAIT 1
#define ECHO_BACK 0
#define GET_DATA 1
#define RECEIVE_MODE 2
#define GET_VERSION 3
#define SET_DATA 4
static char bto_commands[] = {
BTO_CMD_ECHO_BACK,
BTO_CMD_GET_DATA,
BTO_CMD_SET_RECEIVE_MODE,
BTO_CMD_GET_FIRMWARE_VERSION,
BTO_CMD_SET_DATA
};
static char bto_commands_ex[] = {
BTO_CMD_ECHO_BACK,
BTO_CMD_GET_DATA_EX,
BTO_CMD_SET_RECEIVE_MODE_EX,
BTO_CMD_GET_FIRMWARE_VERSION,
BTO_CMD_SET_DATA_EX
};
char get_command(int no, int extend) {
char cmd;
if (extend)
cmd = bto_commands_ex[no];
else
cmd = bto_commands[no];
return cmd;
}
int get_data_length(int extend) {
if (extend)
return IR_DATA_SIZE_EX;
else
return IR_DATA_SIZE;
}
void close_device(libusb_context *ctx, libusb_device_handle *devh) {
libusb_close(devh);
libusb_exit(ctx);
}
libusb_device_handle* open_device(libusb_context *ctx) {
struct libusb_device_handle *devh = NULL;
libusb_device *dev;
libusb_device **devs;
int r = 1;
int i = 0;
int cnt = 0;
libusb_set_debug(ctx, 3);
if ((libusb_get_device_list(ctx, &devs)) < 0) {
perror("no usb device found");
exit(1);
}
/* check every usb devices */
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev, &desc) < 0) {
perror("failed to get device descriptor\n");
}
/* count how many device connected */
if (desc.idVendor == VENDOR_ID && desc.idProduct == PRODUCT_ID) {
cnt++;
}
}
/* device not found */
if (cnt == 0) {
fprintf(stderr, "device not connected\n");
exit(1);
}
if (cnt > 1) {
fprintf(stderr, "multi device is not implemented yet\n");
exit(1);
}
/* open device */
if ((devh = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID)) < 0) {
perror("can't find device\n");
close_device(ctx, devh);
exit(1);
}
/* detach kernel driver if attached. */
r = libusb_kernel_driver_active(devh, 3);
if (r == 1) {
/* detaching kernel driver */
r = libusb_detach_kernel_driver(devh, 3);
if (r != 0) {
perror("detaching kernel driver failed");
exit(1);
}
}
r = libusb_claim_interface(devh, 3);
if (r < 0) {
fprintf(stderr, "claim interface failed (%d): %s\n", r, strerror(errno));
exit(1);
}
return devh;
}
void write_device(struct libusb_device_handle *devh, unsigned char *cmd, int len) {
int i, r;
uint8_t buf[MAX_SIZE];
int size = 0;
memset(buf, 0xff, sizeof(buf));
for (i = 0; i < len; i++) {
buf[i] = cmd[i];
}
r = libusb_interrupt_transfer(devh, BTO_EP_OUT, buf, sizeof(buf) ,&size, 1000);
if (r < 0) {
fprintf(stderr, "libusb_interrupt_transfer (%d): %s\n", r, strerror(errno));
exit(1);
}
}
int read_device(struct libusb_device_handle *devh, unsigned char *buf, int bufsize) {
int size = 0;
memset(buf, 0x00, bufsize);
int r = libusb_interrupt_transfer(devh, BTO_EP_IN, buf, bufsize, &size, 1000);
if (r < 0) {
fprintf(stderr, "libusb_interrupt_transfer (%d): %s\n", r, strerror(errno));
exit(1);
}
return size;
}
void clear_device_buffer(struct libusb_device_handle *devh) {
/* clear read buffer */
unsigned char buf[MAX_SIZE];
memset(buf, 0xff, sizeof(buf));
buf[0] = BTO_CMD_ECHO_BACK;
write_device(devh, buf, MAX_SIZE);
read_device(devh, buf, MAX_SIZE);
}
int receive_ir(struct libusb_device_handle *devh, unsigned char *data, int length, int extend) {
unsigned char buf[MAX_SIZE];
char cmd;
int i;
int retval = 0;
clear_device_buffer(devh);
memset(buf, 0xff, sizeof(buf));
cmd = get_command(RECEIVE_MODE, extend);
buf[0] = cmd;
buf[1] = RECEIVE_WAIT_MODE_WAIT;
write_device(devh, buf, MAX_SIZE);
read_device(devh, buf, MAX_SIZE);
while (1) {
memset(buf, 0xFF, sizeof(buf));
cmd = get_command(GET_DATA, extend);
buf[0] = cmd;
write_device(devh, buf, MAX_SIZE);
read_device(devh, buf, MAX_SIZE);
if (buf[0] == cmd && buf[1] != 0) {
for (i = 0; i < length; i++) {
data[i] = buf[i+1];
}
retval = 1;
break;
}
}
memset(buf, 0xff, sizeof(buf));
buf[0] = get_command(RECEIVE_MODE, extend);
buf[1] = RECEIVE_WAIT_MODE_NONE;
write_device(devh, buf, MAX_SIZE);
read_device(devh, buf, MAX_SIZE);
return retval;
}
void transfer_ir(struct libusb_device_handle *devh, char *data, int length, int extend) {
unsigned char buf[MAX_SIZE];
int i;
char *e, s[3] = "\0\0";
memset(buf, 0xff, MAX_SIZE);
buf[0] = get_command(SET_DATA, extend);
// hex to byte
for (i = 0; i < length; i++) {
s[0] = data[i*2];
s[1] = data[i*2+1];
buf[i+1] = (unsigned char)strtoul(s, &e, 16);
if (*e != '\0') {
break;
}
}
if (i != length) {
fprintf(stderr, "[%s] is skipped..\n", data);
return;
}
write_device(devh, buf, MAX_SIZE);
clear_device_buffer(devh);
}
void usage() {
fprintf(stderr, "usage: bto_ir_cmd <option>\n");
fprintf(stderr, " -r \tReceive Infrared code.\n");
fprintf(stderr, " -t <code>\tTransfer Infrared code.\n");
fprintf(stderr, " -e \tExtend Infrared code.\n");
fprintf(stderr, " \t Normal: 7 octets\n");
fprintf(stderr, " \t Extend: 35 octets\n");
}
int main(int argc, char *argv[]) {
libusb_context *ctx = NULL;
int r = 1;
int i;
unsigned char buf[MAX_SIZE];
int receive_mode = 0;
int transfer_mode = 0;
int extend = 0;
int ir_data_size;
char *ir_data;
while ((r = getopt(argc, argv, "ehrt:")) != -1) {
switch(r) {
case 'r':
receive_mode = 1;
break;
case 't':
transfer_mode = 1;
ir_data = optarg;
break;
case 'e':
extend = 1;
printf("Extend mode on.\n");
break;
default:
usage();
exit(1);
}
}
if (receive_mode == 1 || transfer_mode == 1) {
/* libusb initialize*/
if ((r = libusb_init(&ctx)) < 0) {
perror("libusb_init\n");
exit(1);
}
/* open device */
libusb_device_handle *devh = open_device(ctx);
ir_data_size = get_data_length(extend);
if (transfer_mode == 1) {
printf("Transfer mode\n");
printf(" Transfer code: %s\n", ir_data);
transfer_ir(devh, ir_data, ir_data_size, extend);
}
else if (receive_mode == 1) {
printf("Receive mode\n");
memset(buf, 0x00, ir_data_size);
r = receive_ir(devh, buf, ir_data_size, extend);
if (r == 1) {
printf(" Received code: ");
for (i = 0; i < ir_data_size; i++) {
printf("%02X", buf[i]);
}
printf("\n");
}
}
/* close device */
close_device(ctx, devh);
}
else {
usage();
exit(1);
}
return 0;
}