272 lines
6.9 KiB
C
272 lines
6.9 KiB
C
#include "fusb.h"
|
||
|
||
#define USB_DEBUG_ENABLE
|
||
#define USB_VID 0x0FE6 // USB Device Vendor ID
|
||
//#define USB_PID 0x811e // USB Device Product ID
|
||
#define USB_PID 0x8800 // USB Device Product ID
|
||
#define USB_EP_ADDR_OUT 0x01 // USB Device Endpoint OUT Address
|
||
#define USB_IF_NUM 0 // USB Device Interface Number
|
||
|
||
#ifdef USB_DEBUG_ENABLE
|
||
#define USB_LOG(format, ...) printf(format"\n", ##__VA_ARGS__)
|
||
#else
|
||
#define USB_LOG(format, ...)
|
||
#endif
|
||
|
||
// libusb_context *usb_ctx = NULL;
|
||
// libusb_device_handle *dev_hd = NULL;
|
||
|
||
const char* usb_port_name_list[SUPPORT_USB_MAX_NUM] = {
|
||
"/sys/bus/usb/devices/1-1.1",
|
||
"/sys/bus/usb/devices/1-1.2",
|
||
"/sys/bus/usb/devices/1-1.3",
|
||
"/sys/bus/usb/devices/1-1.4",
|
||
};
|
||
|
||
printer_usb_info_t printer_dev[SUPPORT_USB_MAX_NUM];
|
||
|
||
|
||
/**
|
||
* @brief USB模块初始化
|
||
*/
|
||
int usb_init(void)
|
||
{
|
||
for (size_t i = 0; i < SUPPORT_USB_MAX_NUM; i++)
|
||
{
|
||
if(access(usb_port_name_list[i], F_OK) == 0)
|
||
{
|
||
if (printer_dev[i].usb_ctx != NULL)
|
||
{
|
||
continue;
|
||
}
|
||
if (libusb_init(&printer_dev[i].usb_ctx) != LIBUSB_SUCCESS)
|
||
{
|
||
USB_LOG("failed to init libusb");
|
||
return USB_ERR_LIBUSB_INIT_FAILED;
|
||
}
|
||
printer_dev[i].Serial = i;
|
||
printer_dev[i].pid = Get_Printer_device_info(usb_port_name_list[i],"PID");
|
||
printer_dev[i].vid = Get_Printer_device_info(usb_port_name_list[i],"VID");
|
||
|
||
}else{
|
||
printer_dev[i].Serial = 0xff;
|
||
}
|
||
}
|
||
|
||
USB_LOG("libusb inited");
|
||
|
||
return USB_OK;
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief 打开USB设备
|
||
*/
|
||
int usb_open(printer_usb_info_t* printer_dev)
|
||
{
|
||
int ret;
|
||
int dev_conf;
|
||
libusb_context *usb_ctx = printer_dev->usb_ctx;
|
||
//libusb_device_handle *dev_hd = printer_dev->dev_handle;
|
||
if (usb_ctx == NULL)
|
||
{
|
||
return USB_ERR_LIBUSB_NOT_INIT;
|
||
}
|
||
|
||
if (printer_dev->dev_handle != NULL)
|
||
{
|
||
return USB_OK;
|
||
}
|
||
|
||
printer_dev->dev_handle = libusb_open_device_with_vid_pid(usb_ctx, USB_VID, USB_PID);
|
||
if (printer_dev->dev_handle == NULL)
|
||
{
|
||
USB_LOG("failed to open USB device");
|
||
return USB_ERR_OPEN_FAILED;
|
||
}
|
||
|
||
ret = libusb_get_configuration(printer_dev->dev_handle, &dev_conf);
|
||
if (ret != LIBUSB_SUCCESS)
|
||
{
|
||
USB_LOG("failed to get configuration");
|
||
libusb_close(printer_dev->dev_handle);
|
||
return USB_ERR_GET_CONF;
|
||
}
|
||
|
||
if (dev_conf != 1)
|
||
{
|
||
ret = libusb_set_configuration(printer_dev->dev_handle, 1);
|
||
USB_LOG("failed to set configuration");
|
||
libusb_close(printer_dev->dev_handle);
|
||
return USB_ERR_SET_CONF;
|
||
}
|
||
|
||
if (libusb_kernel_driver_active(printer_dev->dev_handle, 0) == 1)
|
||
{
|
||
ret = libusb_detach_kernel_driver(printer_dev->dev_handle, 0);
|
||
if (ret != LIBUSB_SUCCESS)
|
||
{
|
||
USB_LOG("failed to detach kernel driver");
|
||
libusb_close(printer_dev->dev_handle);
|
||
return USB_ERR_DETACH_FAILED;
|
||
}
|
||
}
|
||
|
||
ret = libusb_claim_interface(printer_dev->dev_handle, USB_IF_NUM);
|
||
if (ret != LIBUSB_SUCCESS)
|
||
{
|
||
USB_LOG("failed to claim interface");
|
||
libusb_close(printer_dev->dev_handle);
|
||
return USB_ERR_CLAIM_IF;
|
||
}
|
||
|
||
USB_LOG("device found and opened");
|
||
|
||
return USB_OK;
|
||
}
|
||
|
||
/**
|
||
* @brief 关闭USB设备
|
||
*/
|
||
int usb_close(printer_usb_info_t* printer_dev)
|
||
{
|
||
libusb_device_handle *dev_hd = printer_dev->dev_handle;
|
||
if (dev_hd == NULL)
|
||
{
|
||
return USB_OK;
|
||
}
|
||
|
||
if (libusb_release_interface(dev_hd, USB_IF_NUM) != LIBUSB_SUCCESS)
|
||
{
|
||
USB_LOG("failed to release interface");
|
||
}
|
||
|
||
libusb_close(dev_hd);
|
||
dev_hd = NULL;
|
||
|
||
USB_LOG("device closed");
|
||
|
||
return USB_OK;
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* @brief 使用USB Bulk向打印机发送数据
|
||
*
|
||
* @param buff 数据指针
|
||
* @param optsize 期望的数据发送大小
|
||
* @param ressize 实际的数据发送大小
|
||
* @param timeout 超时时间
|
||
*/
|
||
int usb_write(printer_usb_info_t* printer_dev,const void *buff, int size)
|
||
{
|
||
int ret;
|
||
int rest_size;
|
||
int pack_size;
|
||
int actual_size;
|
||
const uint8_t *buff_offs;
|
||
libusb_device_handle *dev_hd = printer_dev->dev_handle;
|
||
if (dev_hd == NULL || buff == NULL)
|
||
{
|
||
return USB_ERR_BAD_ARG;
|
||
}
|
||
|
||
if (size == 0)
|
||
{
|
||
return USB_OK;
|
||
}
|
||
|
||
rest_size = size;
|
||
buff_offs = (const uint8_t *)buff;
|
||
|
||
/* 其实USB全速设备传输一个最大页(17280字节)只需要1.44ms左右,
|
||
这完全足够及时中断USB传输,但是USB设备那边可能有时无法满足接收
|
||
条件(比如正在处理紧急事务等),超时时间如果很短的话,就有可能
|
||
最后发送发生超时,所以需要写成分段发送的形式,并且在每个分段之前
|
||
判断当前传输是否需要中断 */
|
||
while (rest_size > 0)
|
||
{
|
||
if (rest_size >= 512)
|
||
{
|
||
pack_size = 512;
|
||
}
|
||
else
|
||
{
|
||
pack_size = rest_size;
|
||
}
|
||
ret = libusb_bulk_transfer(dev_hd, USB_EP_ADDR_OUT, buff_offs, pack_size, &actual_size, 5);
|
||
if (ret == LIBUSB_SUCCESS || ret == LIBUSB_ERROR_TIMEOUT)
|
||
{
|
||
rest_size -= actual_size;
|
||
buff_offs += actual_size;
|
||
}
|
||
else if (ret == LIBUSB_ERROR_NO_DEVICE)
|
||
{
|
||
usb_close(printer_dev);
|
||
USB_LOG("device not found when using bulk transfer");
|
||
return USB_ERR_NO_DEV;
|
||
}
|
||
else
|
||
{
|
||
usb_close(printer_dev);
|
||
USB_LOG("failed to use bulk transfer, ret: %d %s", ret,libusb_error_name(ret));
|
||
return USB_ERR_UNKNOWN;
|
||
}
|
||
}
|
||
|
||
return USB_OK;
|
||
}
|
||
|
||
int Send_data_usb(printer_usb_info_t* printer_dev,uint8_t* buff,uint16_t len)
|
||
{
|
||
int ret = 0;
|
||
if(printer_dev == NULL)
|
||
{
|
||
return -1;
|
||
}
|
||
ret = usb_open(printer_dev);
|
||
if (ret != USB_OK)
|
||
{
|
||
printf("USB open fail\n");
|
||
return -1;
|
||
}
|
||
if(printer_dev->dev_handle == NULL)
|
||
{
|
||
printf("USB open fail\n");
|
||
}
|
||
usb_write(printer_dev,&buff[0],len);
|
||
usb_close(printer_dev);
|
||
return len;
|
||
}
|
||
|
||
int Get_Printer_device_info(char* device_dir,char* type)
|
||
{
|
||
FILE *File;
|
||
uint8_t index = 0;
|
||
char echo_buf[128] = {0};
|
||
char readBuf[5] = {0};
|
||
int num = 0;
|
||
if(type == NULL)
|
||
{
|
||
return -1;
|
||
}else if(strcmp(type,"VID") == 0)
|
||
{
|
||
sprintf(&echo_buf[0],"cat %s/idVendor",device_dir);
|
||
index = strlen(echo_buf);
|
||
echo_buf[index] = '\0';
|
||
printf("%s\r\n",&echo_buf[0]);
|
||
}else if(strcmp(type,"PID") == 0)
|
||
{
|
||
sprintf(&echo_buf[0],"cat %s/idProduct",device_dir);
|
||
index = strlen(echo_buf);
|
||
echo_buf[index] = '\0';
|
||
printf("%s\r\n",&echo_buf[0]);
|
||
}
|
||
char *endptr;
|
||
File = popen(&echo_buf[0],"r");
|
||
fread(readBuf,4,1,File);
|
||
num = strtol(readBuf, &endptr, 16);
|
||
printf("Devices--%s:0x%s\n",type,readBuf);
|
||
return num;
|
||
} |