zoukankan      html  css  js  c++  java
  • 基于libhid/libusb进行开发

    操作环境:ubuntu,基于libhid/libusb进行开发
     
    libusb介绍:

    libusb 设计了一系列的外部API 为应用程序所调用,通过这些API应用程序可以操作硬件,从libusb的源代码可以看出,这些API 调用了内核的底层接口(ioctl),和kernel driver中所用到的函数所实现的功能差不多,只是libusb更加接近USB 规范。使得libusb的使用也比开发内核驱动相对容易的多。相关资料见http://www.libusb.org/。中文资料见百度百科libusb。

    libhid介绍
          libhid是在libusb基础上封装了一层API,简化了数据操作,使得HID设备读写更方便,也更贴近PC端编程习惯。源码下载地址http://libhid.alioth.debian.org/
     
    1.libusb操作流程简介
    1.1.初始化
    struct usb_bus *bus;
    struct usb_device *dev;
    usb_init();
    usb_find_busses();
    usb_find_devices();

     

    1.2.找到设备

    for (bus = busses; bus; bus = bus->next)

    {        

    for (dev = bus->devices; dev; dev = dev->next)

    {              

    if(dev->descriptor.idVendor==VENDOR_ID&& dev->descriptor.idProduct == PRODUCT_ID)

     {

    //找到设备

    break;

    }

           }

    }

    1.3.打开设备
    dev_handle = usb_open(dev);//打开设备
    usb_set_configuration( dev_handle , bConfigureType); //设置设备config类型
    usb_claim_interface( dev_handle , 0);//注册与操作系统通信的接口
    Note:
    usb_claim_interface失败
    1)
    现象1:函数返回值=-1,获取usb_strerror,为Operation not permitted
    原因:是应用程序没有/dev/mnt/usb目录的写权限。
    解决方法:一种方法是root登陆;另一种是“chmod o+w -R /dev/bus/usb into /etc/init.d/rc shell script”
    2)
    现象2:函数返回-2,错误信息为"USB error: could not claim interface 0: No such file or directory"
    usb_set_configuration中的参数必须与控制描述符中的bConfigureType一致
     
     
    1.4.获取报告描述符 
    usb_control_msg(dev_handle,
          USB_ENDPOINT_IN+1,
          USB_REQ_GET_DESCRIPTOR,
          (USB_DT_REPORT << 8) + 0, hidif->interface,
          (char*)hidif->hid_parser->ReportDesc, hidif->hid_parser->ReportDescSize,
          USB_TIMEOUT);
     
    报告描述符中得到ReportSize
     
    1.5. 设置/获取HID Report
    //get_input_report
    len = usb_control_msg(dev_handle,
          USB_ENDPOINT_IN + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
          HID_REPORT_GET,
          hidif->hid_data->ReportID + (HID_RT_INPUT << 8),
          hidif->interface,
          buffer, REPROTSIZE, USB_TIMEOUT);
    //set output report
     int len = usb_control_msg(hidif->dev_handle,
          USB_ENDPOINT_OUT + USB_TYPE_CLASS + USB_RECIP_INTERFACE,
          HID_REPORT_SET,
          hidif->hid_data->ReportID + (HID_RT_OUTPUT << 8),
          hidif->interface,
          (char*)buffer, REPROTSIZE, USB_TIMEOUT);
     
    1.6.关闭设备
    usb_release_interfaces
    usb_close
     
    1.7. debug
    lib_usb_setdebug
    usb_strerror
     
    2.libhid接口使用说明
    2.1初始化
    hid_init()//同1.1
    hid_new_HIDInterface,准备结构体memory
    2.2 查找设备,并打开设备,注册通信接口
    hid_force_open();//同1.2,1.3,1.4
    2.3 报告操作
    2.4 debug
    hid_set_debug(HID_DEBUG_ALL);
    hid_set_usb_debug(0);
  • 相关阅读:
    不可或缺 Windows Native (15)
    不可或缺 Windows Native (14)
    不可或缺 Windows Native (13)
    不可或缺 Windows Native (12)
    不可或缺 Windows Native (11)
    不可或缺 Windows Native (10)
    不可或缺 Windows Native (9)
    不可或缺 Windows Native (8)
    不可或缺 Windows Native (7)
    不可或缺 Windows Native (6)
  • 原文地址:https://www.cnblogs.com/ahuo/p/4626867.html
Copyright © 2011-2022 走看看