Database.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 PCIDB {
  15. struct Subsystem {
  16. u16 vendor_id;
  17. u16 device_id;
  18. StringView name;
  19. };
  20. struct Device {
  21. u16 id;
  22. StringView name;
  23. HashMap<int, NonnullOwnPtr<Subsystem>> subsystems;
  24. };
  25. struct Vendor {
  26. u16 id;
  27. StringView name;
  28. HashMap<int, NonnullOwnPtr<Device>> devices;
  29. };
  30. struct ProgrammingInterface {
  31. u8 id { 0 };
  32. StringView name {};
  33. };
  34. struct Subclass {
  35. u8 id { 0 };
  36. StringView name {};
  37. HashMap<int, NonnullOwnPtr<ProgrammingInterface>> programming_interfaces;
  38. };
  39. struct Class {
  40. u8 id { 0 };
  41. StringView name {};
  42. HashMap<int, NonnullOwnPtr<Subclass>> subclasses;
  43. };
  44. class Database : public RefCounted<Database> {
  45. public:
  46. static RefPtr<Database> open(ByteString const& filename);
  47. static RefPtr<Database> open() { return open("/res/pci.ids"); }
  48. const StringView get_vendor(u16 vendor_id) const;
  49. const StringView get_device(u16 vendor_id, u16 device_id) const;
  50. const StringView get_subsystem(u16 vendor_id, u16 device_id, u16 subvendor_id, u16 subdevice_id) const;
  51. const StringView get_class(u8 class_id) const;
  52. const StringView get_subclass(u8 class_id, u8 subclass_id) const;
  53. const StringView get_programming_interface(u8 class_id, u8 subclass_id, u8 programming_interface_id) const;
  54. private:
  55. explicit Database(NonnullOwnPtr<Core::MappedFile> file)
  56. : m_file(move(file))
  57. {
  58. }
  59. int init();
  60. enum ParseMode {
  61. UnknownMode,
  62. VendorMode,
  63. ClassMode,
  64. };
  65. NonnullOwnPtr<Core::MappedFile> m_file;
  66. StringView m_view {};
  67. HashMap<int, NonnullOwnPtr<Vendor>> m_vendors;
  68. HashMap<int, NonnullOwnPtr<Class>> m_classes;
  69. bool m_ready { false };
  70. };
  71. }