加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.c 17.75 KB
一键复制 编辑 原始数据 按行查看 历史
null 提交于 2020-05-25 08:36 . fixed 1bug
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
#include <stdio.h>
#include "libusb.h"
int ControlDevice(int vid, int pid, char requesttype, char request, short value, short index, char *buff, int len);
int switchReport(int vid, int pid, unsigned char *buffer, int buffer_size, unsigned char *returnbuffer, int returnbuffer_size);
struct userDevice
{
/*Device descriptor*/
/** USB-IF vendor ID */
uint16_t idVendor;
/** USB-IF product ID */
uint16_t idProduct;
/*Interface descriptor*/
/** USB-IF class code for this interface. See \ref libusb_class_code. */
unsigned char bInterfaceClass;
/** USB-IF subclass code for this interface, qualified by the
* bInterfaceClass value */
unsigned char bInterfaceSubClass;
/*Endpoint descriptor*/
/** Attributes which apply to the endpoint when it is configured using
* the bConfigurationValue. Bits 0:1 determine the transfer type and
* correspond to \ref libusb_transfer_type. Bits 2:3 are only used for
* isochronous endpoints and correspond to \ref libusb_iso_sync_type.
* Bits 4:5 are also only used for isochronous endpoints and correspond to
* \ref libusb_iso_usage_type. Bits 6:7 are reserved.
*/
unsigned char bmAttributes;
/*save parameter*/
libusb_device *dev;
unsigned char bInEndpointAddress;
unsigned char bOutEndpointAddress;
/* Number of this interface */
unsigned char bInterfaceNumber;
};
int init_libusb(void)
{
/*1. init libusb lib*/
int rv = libusb_init(NULL);
if (rv < 0)
{
printf("*** initial USB lib failed! \n");
return -1;
}
return rv;
}
int get_device_descriptor(struct libusb_device_descriptor *dev_desc, struct userDevice *user_device)
{
int rv = -2, i = 0, j, k;
ssize_t cnt;
libusb_device **devs;
libusb_device *dev;
struct libusb_config_descriptor *conf_desc;
unsigned char isFind = 0;
cnt = libusb_get_device_list(NULL, &devs); //check the device number
if (cnt < 0) return (int)cnt;
while ((dev = devs[i++]) != NULL)
{
rv = libusb_get_device_descriptor(dev, dev_desc);
if (rv < 0)
{
printf("*** libusb_get_device_descriptor failed! i:%d \n", i);
return -1;
}
if (dev_desc->idProduct == user_device->idProduct && dev_desc->idVendor == user_device->idVendor)
{
user_device->dev = dev;
rv = 0;
break;
}
}//找不到设备
if (user_device->dev == NULL)
{
libusb_free_device_list(devs, 1);
return -1;
}
for (i = 0; i < dev_desc->bNumConfigurations; i++)
{
rv = libusb_get_config_descriptor(user_device->dev, i, &conf_desc);
if (rv < 0)
{
printf("*** libusb_get_config_descriptor failed! \n");
return -1;
}
if(conf_desc==NULL)continue;
for (j = 0; j < conf_desc->bNumInterfaces; j++)
for (k = 0; k < conf_desc->interface[j].num_altsetting; k++)
{
//枚举找到端点描述符
if (conf_desc->interface[j].altsetting[k].bInterfaceClass != user_device->bInterfaceClass)
continue;
if (!match_with_endpoint(&(conf_desc->interface[j].altsetting[k]), user_device))
continue;
user_device->bInterfaceNumber = conf_desc->interface[j].altsetting[k].bInterfaceNumber;
libusb_free_config_descriptor(conf_desc);
rv = 0;
return rv;
}
}
//return 0;
libusb_free_device_list(devs, 1);
return rv;
}
int match_with_endpoint(const struct libusb_interface_descriptor *interface, struct userDevice *user_device)
{
int i, ret = 0;
for (i = 0; i < interface->bNumEndpoints; i++)
{
if ((interface->endpoint[i].bmAttributes & 0x03) != user_device->bmAttributes)
continue; //transfer type :bulk ,control, interrupt
if (interface->endpoint[i].bEndpointAddress & 0x80) //out endpoint & in endpoint
{
ret |= 1;
user_device->bInEndpointAddress = interface->endpoint[i].bEndpointAddress;
}
else
{
ret |= 2;
user_device->bOutEndpointAddress = interface->endpoint[i].bEndpointAddress;
}
}
return (ret == 3 ? 1 : 0);
}
int testidcard()
{
unsigned char buff[512] = {0xaa, 0xaa, 0xaa, 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x21};
unsigned char retbuff[0x50];
int retlen;
switchReportBulk(0x400,0xc35a,buff,10,retbuff,&retlen);
return 0;
}
void testk80()
{
unsigned char buff[11] = {0x1b, 0x40, 0x1b, 0x3c, 0x1b, 0x33, 0x18, 0x30, 0x31, 0x32, 0x0A};
WriteDevice(0x0DD4, 0x0237, buff, 11);
}
void testek80()
{
unsigned char buff[2] = {0x1b, 0x40};
WriteDevice(0x0519, 0x2013, buff, 2);
}
int main()
{
printf("000\r\n");
//testidcard();
testek80();
return 0;
}
int switchReport(int vid, int pid, unsigned char *buffer, int buffer_size, unsigned char *returnbuffer, int returnbuffer_size)
{
int rv;
init_libusb();
libusb_device_handle *usb_handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
if (usb_handle == NULL)
{
printf("*** Permission denied or Can not find the USB Device!\n");
return -1;
}
rv = libusb_control_transfer(usb_handle, 0x21, 0x09, 0x0301, 0x00, buffer, buffer_size, 1000);
if (rv < 0)
printf("*** write failed! %d\n", rv);
return -1;
rv = libusb_control_transfer(usb_handle, 0xA1, 0x01, 0x0100, 0x00, returnbuffer, returnbuffer_size, 1000);
if (rv < 0)
{
printf("*** read failed! %d\n", rv);
return -1;
}
libusb_close(usb_handle);
libusb_exit(NULL);
return rv;
}
int switchReportBulk(int vid, int pid, unsigned char *buffer, int buffer_size, unsigned char *returnbuffer, int* returnbuffer_size)
{
int rv = -2, i = 0, j, k, l, length;
ssize_t iret;
libusb_context *ctx = NULL;
libusb_device **devs;
libusb_device_handle *g_usb_handle; //设备句柄
struct libusb_config_descriptor *conf_desc; //配置描述符
struct libusb_device_descriptor dev_desc; //设备描述符
struct libusb_interface_descriptor interface;
struct userDevice user_device;
libusb_init(&ctx);
//libusb_set_debug(ctx, 4);
iret = libusb_get_device_list(NULL, &devs); //check the device number
if (iret < 0)
{
libusb_exit(ctx);
return (int)iret;
}
{ //获取设备描述符.对比vid pid
unsigned char isFound = 0;
while ((user_device.dev = devs[i++]) != NULL)
{
iret = libusb_get_device_descriptor(user_device.dev, &dev_desc);
if (iret < 0)
continue;
if (dev_desc.idVendor == vid && dev_desc.idProduct == pid)
{
isFound = 1;
break;
}
}
if (isFound == 0)
{
libusb_exit(ctx);
libusb_free_device_list(devs, 1);
return -2;
}
}
libusb_free_device_list(devs, 1);
int isdetached = 0;
{ //打开设备
iret = libusb_open(user_device.dev, &g_usb_handle);
if (iret < 0)
{//打开失败
libusb_exit(ctx);
return -2;
}
{ //声明接口
if (libusb_kernel_driver_active(g_usb_handle, 0) == 1) //?
{
if (libusb_detach_kernel_driver(g_usb_handle, 0) < 0)
{
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
isdetached = 1;
}
if (libusb_claim_interface(g_usb_handle, 0) < 0)
{
if (libusb_detach_kernel_driver(g_usb_handle, 0) < 0)
{
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
isdetached = 1;
if (libusb_claim_interface(g_usb_handle, 0) < 0)
{
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
}
}
}
{ //声明接口
//枚举设备有多少个配置描述符
for (i = 0; i < dev_desc.bNumConfigurations; i++)
{
//读取配置描述符
if (libusb_get_config_descriptor(user_device.dev, i, &conf_desc) < 0) continue;
iret = 0;
// 此配置所支持的接口数量
for (j = 0; j < conf_desc->bNumInterfaces; j++)
{
// 根据接口的设置数量枚举
for (k = 0; k < conf_desc->interface[j].num_altsetting; k++)
{
interface = conf_desc->interface[j].altsetting[k];
//枚举找到端点描述符
for (l = 0; l < interface.bNumEndpoints; l++)
{
if ((interface.endpoint[l].bmAttributes & LIBUSB_TRANSFER_TYPE_BULK) == 0) continue;//判断是否支持指定类型的传输方式
if (interface.endpoint[l].bEndpointAddress & 0x80) //out endpoint & in endpoint
{
iret |= 1;
user_device.bInEndpointAddress = interface.endpoint[l].bEndpointAddress;
}
else
{
iret |= 2;
user_device.bOutEndpointAddress = interface.endpoint[l].bEndpointAddress;
}
}
user_device.bInterfaceNumber = interface.bInterfaceNumber;
}
}
libusb_free_config_descriptor(conf_desc);
}
}
if (isdetached == 1) libusb_attach_kernel_driver(g_usb_handle, 0);
if (iret != 3)
{
printf("*** endpoint not enough! \n");
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
//printf("3\r\n");
rv = libusb_bulk_transfer(g_usb_handle, user_device.bOutEndpointAddress, buffer, buffer_size, &length, 1000);
if (rv < 0)
{
printf("*** bulk_transfer failed! \n");
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -101;
}
//printf("writed\r\n");
rv = libusb_bulk_transfer(g_usb_handle, user_device.bInEndpointAddress, returnbuffer, 64, &length, 1000);
//int rv = switchReportBulk(0x0400, 0Xc35A, buff, 10, retbuff, 0x40);
if (rv < 0)
{
printf("*** bulk_transfer recv failed! rv=%s\n", libusb_error_name(rv));
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -201;
}
returnbuffer_size = (returnbuffer[5] * 0x100) + returnbuffer[6] + 5;
printf("returnbuffer_size= %d\n",returnbuffer_size);
for (i = length; i <= returnbuffer_size;)
{
rv = libusb_bulk_transfer(g_usb_handle, user_device.bInEndpointAddress, &returnbuffer[i], 64, &length, 1000);
if (rv < 0)
{
printf("*** bulk_transfer recv2 failed! rv=%s\n", libusb_error_name(rv));
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -1;
}
printf("i=%d\n",i);
i += length;
}
printf("%d %d\r\n",length, returnbuffer_size);
libusb_close(g_usb_handle);
libusb_exit(ctx);
return rv;
}
int switchReportBulk2(int vid, int pid, unsigned char *buffer, int buffer_size, unsigned char *returnbuffer, int* returnbuffer_size)
{
int rv = -2, i = 0, j, k, l, length;
ssize_t iret;
libusb_context *ctx = NULL;
libusb_device **devs;
libusb_device_handle *g_usb_handle; //设备句柄
struct libusb_config_descriptor *conf_desc; //配置描述符
struct libusb_device_descriptor dev_desc; //设备描述符
struct libusb_interface_descriptor interface;
struct userDevice user_device;
libusb_init(&ctx);
//libusb_set_debug(ctx, 4);
iret = libusb_get_device_list(NULL, &devs); //check the device number
if (iret < 0)
{
libusb_exit(ctx);
return (int)iret;
}
{ //获取设备描述符.对比vid pid
unsigned char isFound = 0;
while ((user_device.dev = devs[i++]) != NULL)
{
iret = libusb_get_device_descriptor(user_device.dev, &dev_desc);
if (iret < 0)
continue;
if (dev_desc.idVendor == vid && dev_desc.idProduct == pid)
{
isFound = 1;
break;
}
}
if (isFound == 0)
{
libusb_exit(ctx);
libusb_free_device_list(devs, 1);
return -2;
}
}
libusb_free_device_list(devs, 1);
int isdetached = 0;
{ //打开设备
iret = libusb_open(user_device.dev, &g_usb_handle);
if (iret < 0)
{//打开失败
libusb_exit(ctx);
return -2;
}
{ //声明接口
if (libusb_kernel_driver_active(g_usb_handle, 0) == 1) //?
{
if (libusb_detach_kernel_driver(g_usb_handle, 0) < 0)
{
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
isdetached = 1;
}
if (libusb_claim_interface(g_usb_handle, 0) < 0)
{
if (libusb_detach_kernel_driver(g_usb_handle, 0) < 0)
{
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
isdetached = 1;
if (libusb_claim_interface(g_usb_handle, 0) < 0)
{
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
}
}
}
{ //声明接口
//枚举设备有多少个配置描述符
for (i = 0; i < dev_desc.bNumConfigurations; i++)
{
//读取配置描述符
if (libusb_get_config_descriptor(user_device.dev, i, &conf_desc) < 0) continue;
iret = 0;
// 此配置所支持的接口数量
for (j = 0; j < conf_desc->bNumInterfaces; j++)
{
// 根据接口的设置数量枚举
for (k = 0; k < conf_desc->interface[j].num_altsetting; k++)
{
interface = conf_desc->interface[j].altsetting[k];
//枚举找到端点描述符
for (l = 0; l < interface.bNumEndpoints; l++)
{
if ((interface.endpoint[l].bmAttributes & LIBUSB_TRANSFER_TYPE_BULK) == 0) continue;//判断是否支持指定类型的传输方式
if (interface.endpoint[l].bEndpointAddress & 0x80) //out endpoint & in endpoint
{
iret |= 1;
user_device.bInEndpointAddress = interface.endpoint[l].bEndpointAddress;
}
else
{
iret |= 2;
user_device.bOutEndpointAddress = interface.endpoint[l].bEndpointAddress;
}
}
user_device.bInterfaceNumber = interface.bInterfaceNumber;
}
}
libusb_free_config_descriptor(conf_desc);
}
}
if (isdetached == 1) libusb_attach_kernel_driver(g_usb_handle, 0);
if (iret != 3)
{
printf("*** endpoint not enough! \n");
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -3;
}
//printf("3\r\n");
rv = libusb_bulk_transfer(g_usb_handle, user_device.bOutEndpointAddress, buffer, buffer_size, &length, 1000);
if (rv < 0)
{
printf("*** bulk_transfer failed! \n");
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -101;
}
//printf("writed\r\n");
rv = libusb_bulk_transfer(g_usb_handle, user_device.bInEndpointAddress, returnbuffer, 64, &length, 1000);
//int rv = switchReportBulk(0x0400, 0Xc35A, buff, 10, retbuff, 0x40);
if (rv < 0)
{
printf("*** bulk_transfer recv failed! rv=%s\n", libusb_error_name(rv));
libusb_close(g_usb_handle);
libusb_exit(ctx);
return -201;
}
printf("%d %d\r\n",length, returnbuffer_size);
libusb_close(g_usb_handle);
libusb_exit(ctx);
return rv;
}
int WriteDevice(int vid, int pid, char *buff, int len)
{
int rv, length;
libusb_device_handle *g_usb_handle;
struct userDevice user_device;
struct libusb_device_descriptor dev_desc;
user_device.idProduct = pid;
user_device.idVendor = vid;
user_device.bInterfaceClass = LIBUSB_CLASS_PRINTER;
user_device.bInterfaceSubClass = LIBUSB_CLASS_PRINTER;
user_device.bmAttributes = LIBUSB_TRANSFER_TYPE_BULK;
user_device.dev = NULL;
//printf("%d %d\n",user_device.bInEndpointAddress,user_device.bOutEndpointAddress);
//printf("1\n");
init_libusb();
//printf("2\n");
rv = get_device_descriptor(&dev_desc, &user_device);
//printf("3\n");
if (rv < 0)
{
printf("*** get_device_descriptor failed! \n");
return -1;
}
printf("%d %d\n",user_device.bInEndpointAddress,user_device.bOutEndpointAddress);
g_usb_handle = libusb_open_device_with_vid_pid(NULL, user_device.idVendor, user_device.idProduct);
if (g_usb_handle == NULL)
{
printf("*** Permission denied or Can not find the USB board (Maybe the USB driver has not been installed correctly), quit!\n");
return -1;
}
rv = libusb_claim_interface(g_usb_handle, user_device.bInterfaceNumber);
if (rv < 0)
{
rv = libusb_detach_kernel_driver(g_usb_handle, user_device.bInterfaceNumber);
if (rv < 0)
{
printf("*** libusb_detach_kernel_driver failed! rv%d\n", rv);
return -1;
}
rv = libusb_claim_interface(g_usb_handle, user_device.bInterfaceNumber);
if (rv < 0)
{
printf("*** libusb_claim_interface failed! rv%d\n", rv);
return -1;
}
}
rv = libusb_bulk_transfer(g_usb_handle, user_device.bOutEndpointAddress, buff, len, &length, 1000);
if (rv < 0)
{
printf("*** bulk_transfer failed! \n");
return -1;
}
libusb_close(g_usb_handle);
libusb_release_interface(g_usb_handle, user_device.bInterfaceNumber);
libusb_exit(NULL);
return 0;
}
int ReadDevice(int vid, int pid, char *buff, int len)
{
int rv, length;
libusb_device_handle *g_usb_handle;
struct userDevice user_device;
struct libusb_device_descriptor dev_desc;
user_device.idProduct = pid;
user_device.idVendor = vid;
user_device.bInterfaceClass = LIBUSB_CLASS_PRINTER;
user_device.bInterfaceSubClass = LIBUSB_CLASS_PRINTER;
user_device.bmAttributes = LIBUSB_TRANSFER_TYPE_BULK;
user_device.dev = NULL;
//printf("%d %d\n",user_device.bInEndpointAddress,user_device.bOutEndpointAddress);
init_libusb();
rv = get_device_descriptor(&dev_desc, &user_device);
if (rv < 0)
{
printf("*** get_device_descriptor failed! \n");
return -1;
}
//printf("%d %d\n",user_device.bInEndpointAddress,user_device.bOutEndpointAddress);
g_usb_handle = libusb_open_device_with_vid_pid(NULL, user_device.idVendor, user_device.idProduct);
if (g_usb_handle == NULL)
{
printf("*** Permission denied or Can not find the USB board (Maybe the USB driver has not been installed correctly), quit!\n");
return -1;
}
rv = libusb_claim_interface(g_usb_handle, user_device.bInterfaceNumber);
if (rv < 0)
{
rv = libusb_detach_kernel_driver(g_usb_handle, user_device.bInterfaceNumber);
if (rv < 0)
{
printf("*** libusb_detach_kernel_driver failed! rv%d\n", rv);
return -1;
}
rv = libusb_claim_interface(g_usb_handle, user_device.bInterfaceNumber);
if (rv < 0)
{
printf("*** libusb_claim_interface failed! rv%d\n", rv);
return -1;
}
}
rv = libusb_bulk_transfer(g_usb_handle, user_device.bInEndpointAddress, buff, len, &length, 1000);
if (rv < 0)
{
printf("*** bulk_transfer failed! \n");
return -1;
}
libusb_close(g_usb_handle);
libusb_release_interface(g_usb_handle, user_device.bInterfaceNumber);
libusb_exit(NULL);
return 0;
}
int ControlDevice(int vid, int pid, char requesttype, char request, short value, short index, char *buff, int len)
{
int rv, length;
libusb_device_handle *g_usb_handle;
init_libusb();
g_usb_handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
if (g_usb_handle == NULL)
{
printf("*** Permission denied or Can not find the USB board (Maybe the USB driver has not been installed correctly), quit!\n");
libusb_exit(NULL);
return -1;
}
rv = libusb_control_transfer(g_usb_handle, requesttype, request, value, index, buff, len, 1000);
if (rv < 0)
{
printf("*** bulk_transfer failed! %d\n", rv);
return -1;
}
libusb_close(g_usb_handle);
//libusb_release_interface(g_usb_handle,LIBUSB_CLASS_PRINTER);
libusb_exit(NULL);
return rv;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化