Database.h 1.9 KB

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