Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/HashMap.h>
11#include <AK/NonnullOwnPtr.h>
12#include <AK/RefCounted.h>
13#include <AK/RefPtr.h>
14#include <AK/StringView.h>
15#include <LibCore/MappedFile.h>
16
17namespace USBDB {
18
19struct Interface {
20 u16 interface;
21 StringView name;
22};
23
24struct Device {
25 u16 id;
26 StringView name;
27 HashMap<int, NonnullOwnPtr<Interface>> interfaces;
28};
29
30struct Vendor {
31 u16 id;
32 StringView name;
33 HashMap<int, NonnullOwnPtr<Device>> devices;
34};
35
36struct Protocol {
37 u8 id { 0 };
38 StringView name {};
39};
40
41struct Subclass {
42 u8 id { 0 };
43 StringView name {};
44 HashMap<int, NonnullOwnPtr<Protocol>> protocols;
45};
46
47struct Class {
48 u8 id { 0 };
49 StringView name {};
50 HashMap<int, NonnullOwnPtr<Subclass>> subclasses;
51};
52
53class Database : public RefCounted<Database> {
54public:
55 static RefPtr<Database> open(DeprecatedString const& filename);
56 static RefPtr<Database> open() { return open("/res/usb.ids"); };
57
58 const StringView get_vendor(u16 vendor_id) const;
59 const StringView get_device(u16 vendor_id, u16 device_id) const;
60 const StringView get_interface(u16 vendor_id, u16 device_id, u16 interface_id) const;
61 const StringView get_class(u8 class_id) const;
62 const StringView get_subclass(u8 class_id, u8 subclass_id) const;
63 const StringView get_protocol(u8 class_id, u8 subclass_id, u8 protocol_id) const;
64
65private:
66 explicit Database(NonnullRefPtr<Core::MappedFile> file)
67 : m_file(move(file))
68 {
69 }
70
71 int init();
72
73 enum ParseMode {
74 UnknownMode,
75 VendorMode,
76 ClassMode,
77 };
78
79 NonnullRefPtr<Core::MappedFile> m_file;
80 StringView m_view {};
81 HashMap<int, NonnullOwnPtr<Vendor>> m_vendors;
82 HashMap<int, NonnullOwnPtr<Class>> m_classes;
83 bool m_ready { false };
84};
85
86}