adb设备,根据serial获取vid pid

时间:2023-03-09 16:03:03
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 ;
}