2021-06-09 14:23:49 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
#include <AK/String.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/DirIterator.h>
|
|
|
|
#include <LibCore/File.h>
|
2021-11-23 19:17:26 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-06-09 17:24:04 +03:00
|
|
|
#include <LibUSBDB/Database.h>
|
2021-06-09 14:23:49 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-11-23 19:17:26 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-06-09 14:23:49 +03:00
|
|
|
{
|
2021-11-28 01:26:34 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2021-11-23 19:17:26 +03:00
|
|
|
TRY(Core::System::unveil("/sys/bus/usb", "r"));
|
|
|
|
TRY(Core::System::unveil("/res/usb.ids", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-06-09 14:23:49 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args;
|
|
|
|
args.set_general_help("List USB devices.");
|
2021-11-23 19:17:26 +03:00
|
|
|
args.parse(arguments);
|
2021-06-09 14:23:49 +03:00
|
|
|
|
2021-07-18 01:49:53 +03:00
|
|
|
Core::DirIterator usb_devices("/sys/bus/usb", Core::DirIterator::SkipDots);
|
2021-06-09 14:23:49 +03:00
|
|
|
|
2021-06-09 17:24:04 +03:00
|
|
|
RefPtr<USBDB::Database> usb_db = USBDB::Database::open();
|
|
|
|
if (!usb_db) {
|
|
|
|
warnln("Failed to open usb.ids");
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:23:49 +03:00
|
|
|
while (usb_devices.has_next()) {
|
|
|
|
auto full_path = LexicalPath(usb_devices.next_full_path());
|
|
|
|
|
|
|
|
auto proc_usb_device = Core::File::construct(full_path.string());
|
|
|
|
if (!proc_usb_device->open(Core::OpenMode::ReadOnly)) {
|
|
|
|
warnln("Failed to open {}: {}", proc_usb_device->name(), proc_usb_device->error_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto contents = proc_usb_device->read_all();
|
2021-11-15 03:46:51 +03:00
|
|
|
auto json = JsonValue::from_string(contents).release_value_but_fixme_should_propagate_errors();
|
2021-06-09 14:23:49 +03:00
|
|
|
|
2021-11-15 03:46:51 +03:00
|
|
|
json.as_array().for_each([usb_db](auto& value) {
|
2021-06-09 14:23:49 +03:00
|
|
|
auto& device_descriptor = value.as_object();
|
|
|
|
|
2021-08-29 09:24:55 +03:00
|
|
|
auto device_address = device_descriptor.get("device_address").to_u32();
|
2021-06-09 14:23:49 +03:00
|
|
|
auto vendor_id = device_descriptor.get("vendor_id").to_u32();
|
|
|
|
auto product_id = device_descriptor.get("product_id").to_u32();
|
|
|
|
|
2021-06-09 17:24:04 +03:00
|
|
|
StringView vendor_string = usb_db->get_vendor(vendor_id);
|
|
|
|
StringView device_string = usb_db->get_device(vendor_id, product_id);
|
|
|
|
if (device_string.is_empty())
|
|
|
|
device_string = "Unknown Device";
|
|
|
|
|
2021-08-29 09:24:55 +03:00
|
|
|
outln("Device {}: ID {:04x}:{:04x} {} {}", device_address, vendor_id, product_id, vendor_string, device_string);
|
2021-06-09 14:23:49 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|