Last active
December 19, 2015 20:08
-
-
Save kjmkznr/6010868 to your computer and use it in GitHub Desktop.
Tools for Bit Trade One LTD. USB IR REMOCON on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.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 RECEIVE_WAIT_MODE_NONE 0 | |
#define RECEIVE_WAIT_MODE_WAIT 1 | |
#define MAX_SIZE 64 | |
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); | |
printf("init done\n"); | |
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++; | |
printf("BitTradeOne IRR device found\n"); | |
} | |
} | |
/* 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); | |
} else { | |
printf("device opened\n"); | |
} | |
/* 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 dump(char *buf, size_t len) { | |
int i; | |
for (i = 0; i < len; i++) { | |
fprintf(stdout, "0x%02x ", (unsigned char) buf[i]); | |
if (i % 16 == 15) | |
fputs("\n", stdout); | |
} | |
} | |
void send_cmd(struct libusb_device_handle *devh, 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_bulk_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_buffer(struct libusb_device_handle *devh, char *buf, int bufsize) { | |
int size = 0; | |
memset(buf, 0x00, bufsize); | |
int r = libusb_bulk_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; | |
} | |
int main() { | |
libusb_context *ctx = NULL; | |
int r = 1; | |
int size; | |
int i; | |
/* libusb initialize*/ | |
if ((r = libusb_init(&ctx)) < 0) { | |
perror("libusb_init\n"); | |
exit(1); | |
} | |
/* open device */ | |
libusb_device_handle *devh = open_device(ctx); | |
/* clear read buffer */ | |
char buf[MAX_SIZE]; | |
memset(buf, 0xff, sizeof(buf)); | |
buf[0] = 0x40; | |
send_cmd(devh, buf, MAX_SIZE); | |
read_buffer(devh, buf, MAX_SIZE); | |
memset(buf, 0xff, sizeof(buf)); | |
buf[0] = 0x51; | |
buf[1] = RECEIVE_WAIT_MODE_WAIT; | |
send_cmd(devh, buf, MAX_SIZE); | |
read_buffer(devh, buf, MAX_SIZE); | |
while (1) { | |
memset(buf, 0xFF, sizeof(buf)); | |
buf[0] = 0x50; | |
send_cmd(devh, buf, MAX_SIZE); | |
read_buffer(devh, buf, MAX_SIZE); | |
if (buf[0] == 0x50 && buf[1] != 0) { | |
// 受信データありなら 16 進表示 | |
for (i = 0; i < 7; i++) { | |
printf("%02X", buf[i+2]); | |
} | |
putchar('\n'); | |
break; | |
} | |
} | |
memset(buf, 0xff, sizeof(buf)); | |
buf[0] = 0x51; | |
buf[1] = RECEIVE_WAIT_MODE_NONE; | |
send_cmd(devh, buf, MAX_SIZE); | |
read_buffer(devh, buf, MAX_SIZE); | |
/* close device */ | |
close_device(ctx, devh); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment