summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibUSBDB/Database.h
blob: 207fedcc1ed9e52cc28c2129066b792f7efe65eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <LibCore/MappedFile.h>

namespace USBDB {

struct Interface {
    u16 interface;
    StringView name;
};

struct Device {
    u16 id;
    StringView name;
    HashMap<int, NonnullOwnPtr<Interface>> interfaces;
};

struct Vendor {
    u16 id;
    StringView name;
    HashMap<int, NonnullOwnPtr<Device>> devices;
};

struct Protocol {
    u8 id { 0 };
    StringView name {};
};

struct Subclass {
    u8 id { 0 };
    StringView name {};
    HashMap<int, NonnullOwnPtr<Protocol>> protocols;
};

struct Class {
    u8 id { 0 };
    StringView name {};
    HashMap<int, NonnullOwnPtr<Subclass>> subclasses;
};

class Database : public RefCounted<Database> {
public:
    static RefPtr<Database> open(const String& filename);
    static RefPtr<Database> open() { return open("/res/usb.ids"); };

    const StringView get_vendor(u16 vendor_id) const;
    const StringView get_device(u16 vendor_id, u16 device_id) const;
    const StringView get_interface(u16 vendor_id, u16 device_id, u16 interface_id) const;
    const StringView get_class(u8 class_id) const;
    const StringView get_subclass(u8 class_id, u8 subclass_id) const;
    const StringView get_protocol(u8 class_id, u8 subclass_id, u8 protocol_id) const;

private:
    explicit Database(NonnullRefPtr<Core::MappedFile> file)
        : m_file(move(file))
    {
    }

    int init();

    enum ParseMode {
        UnknownMode,
        VendorMode,
        ClassMode,
    };

    NonnullRefPtr<Core::MappedFile> m_file;
    StringView m_view {};
    HashMap<int, NonnullOwnPtr<Vendor>> m_vendors;
    HashMap<int, NonnullOwnPtr<Class>> m_classes;
    bool m_ready { false };
};

}