使用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的更多相关文章

  1. USB VID PID 查询

    USB VID PID 查询:http://www.linux-usb.org/usb.ids 说明: USB设备中有VID何PID,分别表示此USB设备是哪个厂商的哪种设备. 一个USB的VID对应 ...

  2. adb 设备命令

    一.adb 设备命令1.查看机型时,可以使用以下命令$ adb shell getprop ro.product.model 2.如果我们忘记具体系统属性的名字$ adb shell getprop ...

  3. Bash Shell 获取进程 PID

    转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...

  4. linux命令(26):Bash Shell 获取进程 PID

    转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...

  5. Python 使用标准库根据进程名获取进程PID

    应用场景 在进行 Linux 运维的环境中,我们经常会遇到维护同一台服务器上的多个程序,涉及到程序的启动.关闭和重启操作. 通常这些程序之间存在着相互依存的关系需要进行依次的启动关闭操作. 下面介绍几 ...

  6. [TimLinux] Linux shell获取进程pid

    调用脚本时,获取进程PID: (/this/is/a/script/file.sh > /out/to/log.txt & echo $!) & 脚本内部,获取进程PID: ec ...

  7. 014-交互式Shell和shell脚本获取进程 pid

    Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运行进程 1.交互式 Bash Shell 获取进程 pid 在已知进程名(name)的前提下,交互式 ...

  8. Ubuntu及Windows ADB设备no permissions的解决方案

    不少人曾在Windows下及Ubuntu下都遇到过Android设备无法识别的情况,就是run as Android Application的时候,target显示"??????" ...

  9. android唯一设备标识、设备号、设备ID的获取方法

    ##如何获取Android设备唯一ID? ###问题 每一个android设备都有唯一ID吗?如果有?怎么用java最简单取得呢? ###回答1(最佳) 如何取得android唯一码? 好处: 1.不 ...

随机推荐

  1. weblogic下 微信公众平台获取token报错

    问题描述: 开发微信公众平台,本地tomcat可以正常获取token并发送模板消息,部署在weblogic后报错,异常信息如下: java.security.InvalidKeyException: ...

  2. SuperSocket使用demo

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using SuperSocket. ...

  3. history对象back()、forward()、go()

    history对象back().forward().go()方法history.back() 功能:加载历史列表中的前一个URL(后退). 语法:history.back() 调用该方法的效果等价于点 ...

  4. OfficePickers

    OfficePickers http://www.codeproject.com/Articles/12327/Office-2003-Color-Picker     来自为知笔记(Wiz) 附件列 ...

  5. [RxJS] Transformation operator: map and mapTo

    We made our first operator called multiplyBy, which looks a bit useful, but in practice we don't nee ...

  6. Java 调用Dll

    Java 中怎么能调用到dll中的函数呢? 关键是java中生的本地函数名參数和dll中的本地函数名參数一模一样. 这个程序是java中调用dll中的求和函数. 一,java代码部分操作 1.新建pr ...

  7. kafka版本0.8.2.0-Producer Configs之request.required.acks

    This value controls when a produce request is considered completed. Specifically, how many other bro ...

  8. post和get的区别?

    1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...

  9. 【转】IOS 30多个iOS常用动画,带详细注释

    原文: http://blog.csdn.net/zhibudefeng/article/details/8691567 CoreAnimationEffect.h 文件 // CoreAnimati ...

  10. android查看真机中的数据库

    0.在有网的前提下1.安装 Android Studio,Lantern,Chrome浏览器2.在在githab上搜索stetho,打开第一个facebook/stetho3.在Gradle Scri ...