adb设备,根据serial获取vid pid
使用adb devices命令,可以轻松获取到所有连接到PC的adb设备的serial值。
但是adb命令无法获取adb usb设备的vendor id和product id。
本程序根据adb协议,遍历usb设备,使用ioctrl获取serial和vid,pid,这样可以将serial和vid pid匹配起来。
Windows版本的实现可以根据adb_api.h实现,但是有一个问题,adb的服务会独占adb设备,如果adb服务正在运行,那么,这个实现是无法兑现功能的。
谁有好的办法,欢迎共享。Linux平台则无此限制。
下面提供Linux平台的实现,Windows的实现,因为受限于adb服务独占设备,就不贴出来了,谁有兴趣的,可以问我要代码。
具体Linux实现参见代码:
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <ctype.h>
- #include <linux/usbdevice_fs.h>
- #include <linux/version.h>
- #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
- #include <linux/usb/ch9.h>
- #else
- #include <linux/usb_ch9.h>
- #endif
- #include <asm/byteorder.h>
- #define ADB_CLASS 0xff
- #define ADB_SUBCLASS 0x42
- #define ADB_PROTOCOL 0x1
- #include <string>
- #include <vector>
- typedef struct tag_devpath {
- int serial_index;
- std::string adb_dev_path;
- }ADB_DEV;
- std::vector<ADB_DEV> va;
- inline int badname(const char *name)
- {
- while(*name) {
- if(!isdigit(*name++)) return ;
- }
- return ;
- }
- int is_adb_interface(int usb_class, int usb_subclass, int usb_protocol)
- {
- if (usb_class == ADB_CLASS && usb_subclass == ADB_SUBCLASS &&
- usb_protocol == ADB_PROTOCOL) {
- return ;
- }
- return ;
- }
- int get_serial()
- {
- for(int i = ; i < va.size(); ++i)
- {
- int fd = open(va[i].adb_dev_path.c_str(), O_RDWR);
- if(fd > )
- {
- char serial[];
- int n = ;
- int serial_index = va[i].serial_index;
- serial[] = ;
- memset(serial, , n);
- if (serial_index) {
- struct usbdevfs_ctrltransfer ctrl;
- __u16 buffer[];
- __u16 languages[];
- int i, result;
- int languageCount = ;
- memset(languages, , sizeof(languages));
- memset(&ctrl, , sizeof(ctrl));
- // read list of supported languages
- ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
- ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
- ctrl.wValue = (USB_DT_STRING << ) | ;
- ctrl.wIndex = ;
- ctrl.wLength = sizeof(languages);
- ctrl.data = languages;
- ctrl.timeout = ;
- result = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
- if (result > )
- languageCount = (result - ) / ;
- else if(result < )
- printf("ioctl failed: %s\n", strerror(errno));
- printf("languageCount = %d\n", languageCount);
- for (i = ; i <= languageCount; i++) {
- memset(buffer, , sizeof(buffer));
- memset(&ctrl, , sizeof(ctrl));
- ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
- ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
- ctrl.wValue = (USB_DT_STRING << ) | serial_index;
- ctrl.wIndex = __le16_to_cpu(languages[i]);
- ctrl.wLength = sizeof(buffer);
- ctrl.data = buffer;
- ctrl.timeout = ;
- result = ioctl(fd, USBDEVFS_CONTROL, &ctrl);
- if (result > ) {
- int i;
- // skip first word, and copy the rest to the serial string, changing shorts to bytes.
- result /= ;
- for (i = ; i < result; i++)
- serial[i - ] = __le16_to_cpu(buffer[i]);
- serial[i - ] = ;
- printf("serial: %s\n", serial);
- break;
- }
- else if(result < )
- {
- printf("ioctl failed: %s\n", strerror(errno));
- }
- }
- }
- }
- else
- {
- printf("open %s failed: %s", va[i].adb_dev_path.c_str(), strerror(errno));
- }
- }
- return ;
- }
- void find_usb_device(void)
- {
- const char* base = "/dev/bus/usb";
- char busname[], devname[];
- DIR *busdir , *devdir ;
- struct dirent *de;
- int fd ;
- busdir = opendir(base);
- if(busdir == ) return;
- va.clear();
- while((de = readdir(busdir)) != ) {
- if(badname(de->d_name)) continue;
- snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);
- devdir = opendir(busname);
- if(devdir == ) continue;
- while((de = readdir(devdir))) {
- unsigned char devdesc[];
- unsigned char* bufptr = devdesc;
- unsigned char* bufend;
- struct usb_device_descriptor* device;
- struct usb_config_descriptor* config;
- struct usb_interface_descriptor* interface;
- struct usb_endpoint_descriptor *ep1, *ep2;
- unsigned zero_mask = ;
- unsigned vid, pid;
- size_t desclength;
- if(badname(de->d_name)) continue;
- snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
- if((fd = open(devname, O_RDONLY)) < ) {
- continue;
- }
- desclength = read(fd, devdesc, sizeof(devdesc));
- bufend = bufptr + desclength;
- // should have device and configuration descriptors, and atleast two endpoints
- if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
- close(fd);
- continue;
- }
- device = (struct usb_device_descriptor*)bufptr;
- bufptr += USB_DT_DEVICE_SIZE;
- if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
- close(fd);
- continue;
- }
- vid = device->idVendor;
- pid = device->idProduct;
- config = (struct usb_config_descriptor *)bufptr;
- bufptr += USB_DT_CONFIG_SIZE;
- if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
- close(fd);
- continue;
- }
- // loop through all the descriptors and look for the ADB interface
- while (bufptr < bufend) {
- unsigned char length = bufptr[];
- unsigned char type = bufptr[];
- if (type == USB_DT_INTERFACE) {
- interface = (struct usb_interface_descriptor *)bufptr;
- bufptr += length;
- if (length != USB_DT_INTERFACE_SIZE) {
- break;
- }
- if (is_adb_interface(interface->bInterfaceClass,
- interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
- ADB_DEV ad;
- ad.serial_index = device->iSerialNumber;
- ad.adb_dev_path = devname;
- va.push_back(ad);
- //get devname vendor_id product_id
- printf("get:\ndevname = %s\nidVendor=0x%x idProduct=0x%x\n",
- devname, vid, pid);
- break;
- }
- } else {
- bufptr += length;
- }
- } // end of while
- close(fd);
- } // end of devdir while
- closedir(devdir);
- } //end of busdir while
- closedir(busdir);
- }
- int main(void)
- {
- find_usb_device();
- for(int i = ; i < va.size(); ++i)
- {
- printf("dev:%s ===> serial index: %d\n", va[i].adb_dev_path.c_str(), va[i].serial_index);
- }
- get_serial();
- return ;
- }
adb设备,根据serial获取vid pid的更多相关文章
- USB VID PID 查询
USB VID PID 查询:http://www.linux-usb.org/usb.ids 说明: USB设备中有VID何PID,分别表示此USB设备是哪个厂商的哪种设备. 一个USB的VID对应 ...
- adb 设备命令
一.adb 设备命令1.查看机型时,可以使用以下命令$ adb shell getprop ro.product.model 2.如果我们忘记具体系统属性的名字$ adb shell getprop ...
- Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...
- linux命令(26):Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...
- Python 使用标准库根据进程名获取进程PID
应用场景 在进行 Linux 运维的环境中,我们经常会遇到维护同一台服务器上的多个程序,涉及到程序的启动.关闭和重启操作. 通常这些程序之间存在着相互依存的关系需要进行依次的启动关闭操作. 下面介绍几 ...
- [TimLinux] Linux shell获取进程pid
调用脚本时,获取进程PID: (/this/is/a/script/file.sh > /out/to/log.txt & echo $!) & 脚本内部,获取进程PID: ec ...
- 014-交互式Shell和shell脚本获取进程 pid
Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...
- Ubuntu及Windows ADB设备no permissions的解决方案
不少人曾在Windows下及Ubuntu下都遇到过Android设备无法识别的情况,就是run as Android Application的时候,target显示"??????" ...
- android唯一设备标识、设备号、设备ID的获取方法
##如何获取Android设备唯一ID? ###问题 每一个android设备都有唯一ID吗?如果有?怎么用java最简单取得呢? ###回答1(最佳) 如何取得android唯一码? 好处: 1.不 ...
随机推荐
- Qt 线程基础(Thread Basics的翻译,线程的五种使用情况)
Qt 线程基础(QThread.QtConcurrent等) 转载自:http://blog.csdn.net/dbzhang800/article/details/6554104 昨晚看Qt的Man ...
- python之json学习
1. 从python原始类型向json类型的转换过程,具体的转换如下: import json json.dump(obj, fp, skipkeys=False,ensure_ascii=True, ...
- jquery_EasyUI的学习
1 Accordion(可折叠标签) 1.1 实例 1.1.1 代码 <html> <head> <meta http-equiv="Content-Type& ...
- cocos2d-x创建的九宫图变白块
用UIImageView 创建的九宫图变白,直接用CCScale9Sprite创建的也是变白,找了半天原来是自己为了调整UI方便,开启了CCSprite边缘画线导致的,在ccConfig.h下 宏CC ...
- android 25 跨进程启动activity
跨进程启动activity,启动系统预定义的activity就是跨进程的. client项目启动server项目的activity. clientActivity.java package com.s ...
- iOS-iPad开发之popoverController使用介绍
iOS-iPad开发之popoverController使用介绍 iOS开发UI篇-popoverController使用注意 iOS SDK:自定义Popover(弹出窗口) 实现的简单例子: // ...
- java中调用js脚本
JDK1.6加入了对Script(JSR223)的支持.这是一个脚本框架,提供了让脚本语言来访问Java内部的方法.你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本.这个脚本API允许你为脚 ...
- MySQL存储过程学习笔记
MySQL在5.0以前并不支持存储过程,这使得MySQL在应用上大打折扣.MySQL 5.0终于开始支持存储过程了. MySQL的关键字大小写通用.该学习笔记对关键字使用大写:变量名,表名使用小写. ...
- PHP 通过随机数获得ASCII 值返回字符。
for($i=0;$i<20;$i++){ $Matrix .= chr(65 + rand(0,26)); }
- 【转】Windows环境下.NET 操作Oracle问题
目前,Windows操作系统可以分成两类,32位和64位(64位也区分x86_64位和Itanium ),同时Oracle客户端也做了同样的区分. 在安装和开发的过程中,经常会遇到一些问题,本文就总结 ...