資源簡介
linux C獲取PCI設備名和廠商名,編譯時加上-lpci

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#define?PREPATH?“/sys/bus/pci/devices/“
int?get_pci_vendor_device_id(?char?*pciAddress?int?*vendor?int?*device?);
int?get_pci_name(?int?vendor?int?device?char?*name?);
int
main(?int?argc?char?**argv?)
{
int?vendor?device;
char?name[?256?]?=?{?0?};
if(?argc?!=?2?)
{
fprintf(?stderr?“usage:?%s?pciAddress\n“?argv[?0?]?);
return?-1;
}
if(?get_pci_vendor_device_id(?argv[?1?]?&vendor?&device?)?)?
{
fprintf(?stderr?“fail?to?get?vendor?and?device?id\n“?);
return?-1;
}
printf(?“vendor?id[?%x?]?device?id[?%x?]\n“?vendor?device?);
if(?get_pci_name(?vendor?device?name?)?)
{
fprintf(?stderr?“fail?to?get?pci?device?name\n“?);
return?-1;
}
return?0;
}
int?
get_pci_vendor_device_id(?char?*pciAddress?int?*vendor?int?*device?)
{
char?vendorPath[?256?]?=?{?0?};
char?devicePath[?256?]?=?{?0?};
char?buf[?256?]?=?{?0?};
int??fd?len;
strcpy(?vendorPath?PREPATH?);
strcat(?vendorPath?pciAddress?);
strcat(?vendorPath?“/“?);
strcpy(?devicePath?vendorPath?);
strcat(?vendorPath?“vendor“?);
strcat(?devicePath?“device“?);
if(?(?fd?=?open(?vendorPath?O_RDONLY?)?)?0?)
{
fprintf(?stderr?“path:?%s\n“?vendorPath?);
perror(?“open“?);
return?-1;
}
if(?(?len?=?read(?fd?buf?256?)?)?>?0?)
{
buf[?len?]?=?‘\0‘;
sscanf(?buf?“%x“?vendor?);
}
close(?fd?);
if(?(?fd?=?open(?devicePath?O_RDONLY?)?)?0?)
{
fprintf(?stderr?“path:?%s\n“?devicePath?);
perror(?“open“?);
return?-1;
}
if(?(?len?=?read(?fd?buf?256?)?)?>?0?)
{
buf[?len?]?=?‘\0‘;
sscanf(?buf?“%x“?device?);
}
close(?fd?);
return?0;
}
int?
get_pci_name(?int?vendor?int?device?char?*name?)
{
struct?pci_access?*pacc;
pacc?=?pci_alloc();
if(?!pci_lookup_name(?pacc?name?256?
????????PCI_LOOKUP_VENDOR
????vendor?device?)?)
{
fprintf(?stderr?“fail?to?get?pci?vendor\n“?);
return?-1;
}
printf(?“vendor:?%s\n“?name?);
if(?!pci_lookup_name(?pacc?name?256?
????????PCI_LOOKUP_DEVICE
????vendor?device?)?)
{
fprintf(?stderr?“fail?to?get?pci?device\n“?);
return?-1;
}
printf(?“device:?%s\n“?name?);
pci_cleanup(?pacc?);
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????2242??2016-02-03?16:04??PCIname\pci_name.c
?????目錄??????????0??2016-02-03?16:09??PCIname
-----------?---------??----------?-----??----
?????????????????2242????????????????????2
- 上一篇:SVPWMPMSM.slx
- 下一篇:代價敏感支持向量機CSSVM
評論
共有 條評論