Use of LIBUSB library _report

Source: Internet
Author: User
Tags sprintf

Use of the LIBUSB library

Turn from: http://blog.sina.com.cn/s/blog_4a5b632801009upv.html

  Use LIBUSB before your Linux system must have a USB file system installed, here also describes the use of Hiddev device files to access the device, in order to not only compare the ease of using USB, but also provides a conversion into a libusb-driven case. 3.1 Find device Any driver first step is to look for the equipment to operate, we first look at how HID drive to find the device. We assume that the search for the function of the device device_find (note: The code is just to facilitate interpretation, does not guarantee the integrity of the code) int Device_find () {    char dir_str[100];        char hiddev[100];    dir dir;                memset (dir_str, 0, sizeof (DIR_STR)); memset (hiddev, 0, sizeof (Hiddev));     Dir=opendir ("/dev/usb/hid");     if (dir) {                 sprintf (Dir_str, "/dev/usb/hid/");         closedir (dir);     }else{                 sprintf (Dir_str, "/dev/usb/"); &NBSP;&NBSP;&NBSP;&NBSP}     for (i = 0; i < DEVICe_minor; i++) {            sprintf (Hiddev, "%shiddev%d", dir_str,i);               fd = open (Hiddev, O_RDWR);        if (FD > 0) {                      ioctl (FD, Hidiocgdevinfo, &info);                               if (info.vendor== vendor_id && info.product== Product _id) {                                                    device_num++;              }             close (FD); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}     }     return device_num;         } Let's see how LIBUSB is looking for and initializing the device int device_find () {struct Usb_bus  & nbsp;          *busses;     int                            device_num = 0;     device_num = 0;                usb_init ();                usb _find_busses ();       usb_find_devices ();         busses = usb_get_Busses ();     struct usb_bus       *bus;         for (bus = busses; bus = bus->next) {     & Nbsp;  struct Usb_device *dev;                 for (dev = bus-> Devices Dev dev = dev->next) {           if (dev->descriptor.idvendor== vendor_id&& dev->descriptor.idproduct = = product_id) {                                                     device_num++;  }                        }            }     return device_num;       } Note: In the new version of the LIBUSB, usb_get_busses can not be used, this function is to return the system of the USB bus linked list handle Here we use the usb_busses variable directly, which is defined as an external variable in usb.h so it can be written directly: struct Usb_bus    *bus;         for (bus = usb_busses, bus, bus = bus->next) {   & Nbsp;           struct Usb_device *dev;         for (dev = bus->devices; dev; dev = dev->next) {                   &nbsp}} 3.2  Open device Suppose the function name of the open device we defined is device_open, int device_open () {    int handle;     handle = Open ("Hiddev0", o_rdonly); int Device_open () {struct usb_device*    udev; usb_dev_handle*        device_handle; Device_handle = Usb_open (Udev); 3.3  read-write equipment and operating equipment assume that our equipment uses controlled transmission mode, as for batch transmission and interrupt transmission is limited to space here we define three functions, Device_write, Device_read, Device_report Device_report function send receive function Device_write function Write data device_read    function read Data device_write and Device_read call Device_ The report sends the written information and reads the information, the developer according to sends the command protocol to design, we here only simple implementation sends the data the function. Let's say we want to send 72 bytes of data to the device, the first 8 bytes are the report headers, we define the device-related rules, and the second 64 bits are data. HID-driven implementations (here just code to help understand that code is pseudocode) int device_report (int fd, unsigned char *buffer72) {int      & Nbsp;ret; int      index;     unsigned Char send_data[72]; unsigned char recv_data[72];     struct Hiddev_usage_ref Uref;     struct Hiddev_report_info Rinfo;     ret = IOCTL (FD, Hidiocinitreport, 0);     if (ret!=0) {        return NOT_OPENED_DEVICE; &NBSP;&NBSP;&NBSP;&NBSP}      &Nbsp;  for (index = 0; index < index++) {             uref.report_type = hid_report_type_feature;     uref.report_id = Hid_report_id_first;     uref.usage_index = index;     uref.field_index = 0;     uref.value = Send_data[index];     ioctl (FD, Hidiocgucode, &uref);     ret=ioctl (FD, Hidiocsusage, &uref);     if (ret!= 0) {           return Unknown_error; &NBSP;&NBSP;&NBSP;&NBSP} rinfo.report_type = Hid_report_type_feature; rinfo.report_id = Hid_report_id_first; Rinfo.num_fields = 1; Ret=ioctl (FD, Hidiocsreport, &rinfo);   if (ret!= 0) {         return write_report; RET = IOCTL (FD, Hidiocinitreport, 0); for (index = 0; index < index++) {&NBSP;&NBSP;&NBsp; uref.report_type = hid_report_type_feature;     uref.report_id = Hid_report_id_first;     uref.usage_index = index;     uref.field_index = 0;     ioctl (FD, Hidiocgucode, &uref);     ret = IOCTL (FD, Hidiocgusage, &uref);     if (ret!= 0) {        return UNKNOWN_ERROR;  & NBSP;&NBSP;&NBSP}     recv_data[index] = Uref.value; } memcpy (BUFFER72, Recv_data, 72); return SUCCESS; LIBUSB-Driven implementation int Device_report (int fd, unsigned char *buffer72) {        usb_dev_ Handle* Device_handle;             unsigned char   send_data[72];     unsigned char   recv_data[72];         int              send_len;     int             recv_len;             memset (send_data, 0, sizeof (send_data));     memset (recv_data, 0, sizeof (recv_data));             device_handle = (usb_dev_handle*) (G_list[fd].device_ handle);     if (Device_handle = = NULL) {        return NOT_ Opened_device; } usb_claim_interface (Device_handle, 0); Send_len = Usb_control_msg (device_handle, usb_endpoint_out + Usb_type_class + usb_recip_interface,    & nbsp;                            hid_report_set,                                 0x300,                                  0,                                  send_data, usb_timeout); if (Send_len < 0) {        return write_report} if (Send_len!=) {         return Send_len; } Recv_len = Usb_control_msg (device_handle, usb_endpoint_in + Usb_type_class + usb_recip_interface,    & nbsp;                            hid_report_get, &NBSP;&NBSP;&Nbsp;                             0x300,                                    0,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.