Userland: Use LibPCIDB in lspci to print device names

This commit is contained in:
Conrad Pankoff 2019-08-14 18:10:54 +10:00 committed by Andreas Kling
parent 37a2c03655
commit c7040cee62
Notes: sideshowbarker 2024-07-19 12:41:20 +09:00

View file

@ -2,6 +2,7 @@
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <LibCore/CFile.h>
#include <LibPCIDB/Database.h>
#include <stdio.h>
int main(int argc, char** argv)
@ -9,15 +10,19 @@ int main(int argc, char** argv)
UNUSED_PARAM(argc);
UNUSED_PARAM(argv);
CFile file("/proc/pci");
if (!file.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", file.error_string());
auto db = PCIDB::Database::open();
if (!db)
fprintf(stderr, "Couldn't open PCI ID database\n");
CFile proc_pci("/proc/pci");
if (!proc_pci.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error: %s\n", proc_pci.error_string());
return 1;
}
auto file_contents = file.read_all();
auto file_contents = proc_pci.read_all();
auto json = JsonValue::from_string(file_contents).as_array();
json.for_each([](auto& value) {
json.for_each([db](auto& value) {
auto dev = value.as_object();
auto bus = dev.get("bus").to_u32();
@ -28,10 +33,25 @@ int main(int argc, char** argv)
auto revision_id = dev.get("revision_id").to_u32();
auto class_id = dev.get("class").to_u32();
printf("%02x:%02x.%d %04x: %04x:%04x (rev %02x)\n",
String vendor_name = String::format("%02x", vendor_id);
auto vendor = db->get_vendor(vendor_id);
if (vendor != "")
vendor_name = vendor;
String device_name = String::format("%02x", device_id);
auto device = db->get_device(vendor_id, device_id);
if (device != "")
device_name = device;
String class_name = String::format("%04x", class_id);
auto class_ptr = db->get_class(class_id);
if (class_ptr != "")
class_name = class_ptr;
printf("%02x:%02x.%d %s: %s %s (rev %02x)\n",
bus, slot, function,
class_id, vendor_id, device_id, revision_id
);
class_name.characters(), vendor_name.characters(),
device_name.characters(), revision_id);
});
return 0;