mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
Userland: Add lsusb
:^)
This commit is contained in:
parent
f2ff55dd09
commit
119b8a2692
Notes:
sideshowbarker
2024-07-18 12:05:33 +09:00
Author: https://github.com/Quaker762 Commit: https://github.com/SerenityOS/serenity/commit/119b8a26923 Pull-request: https://github.com/SerenityOS/serenity/pull/7944 Reviewed-by: https://github.com/alimpfard
1 changed files with 66 additions and 0 deletions
66
Userland/Utilities/lsusb.cpp
Normal file
66
Userland/Utilities/lsusb.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* 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>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (pledge("stdio rpath", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil("/proc/bus/usb", "r") < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (unveil(nullptr, nullptr) < 0) {
|
||||
perror("unveil");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Core::ArgsParser args;
|
||||
args.set_general_help("List USB devices.");
|
||||
args.parse(argc, argv);
|
||||
|
||||
Core::DirIterator usb_devices("/proc/bus/usb", Core::DirIterator::SkipDots);
|
||||
|
||||
while (usb_devices.has_next()) {
|
||||
auto full_path = LexicalPath(usb_devices.next_full_path());
|
||||
|
||||
auto proc_usb_device = Core::File::construct(full_path.string());
|
||||
auto bus_id = proc_usb_device->filename().split('/').last();
|
||||
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();
|
||||
auto json = JsonValue::from_string(contents);
|
||||
VERIFY(json.has_value());
|
||||
|
||||
json.value().as_array().for_each([bus_id](auto& value) {
|
||||
auto& device_descriptor = value.as_object();
|
||||
|
||||
auto vendor_id = device_descriptor.get("vendor_id").to_u32();
|
||||
auto product_id = device_descriptor.get("product_id").to_u32();
|
||||
|
||||
outln("Device {}: ID {:04x}:{:04x}", bus_id, vendor_id, product_id);
|
||||
});
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue