blob: 96f1ea465af8684eac0d8c810903cc14811bcd97 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001#pragma once
2
3extern "C" {
4#include <pci/pci.h>
5} // extern "C"
6
7#include <optional>
8#include <vector>
9
10namespace host_tool
11{
12
13/* The ASPEED AST2400 & AST2500 have the same VIDDID. */
14
15/**
16 * The PciDevice structure is a copy of the information to uniquely identify a
17 * PCI device.
18 */
19struct PciDevice
20{
21 int vid;
22 int did;
23 int bus;
24 int dev;
25 int func;
26};
27
28/**
29 * The PciFilter structure is a simple mechanism for filtering devices by their
30 * vendor and/or device ids.
31 */
32struct PciFilter
33{
34 int vid;
35 int did;
36};
37
38class PciUtilInterface
39{
40 public:
41 virtual ~PciUtilInterface() = default;
42
43 /**
44 * Get a list of PCI devices from a system.
45 *
46 * @param[in] filter - optional filter for the list.
47 * @return the list of devices.
48 */
49 virtual std::vector<PciDevice>
50 getPciDevices(std::optional<PciFilter> filter = std::nullopt) = 0;
51};
52
53class PciUtilImpl : public PciUtilInterface
54{
55 public:
56 PciUtilImpl()
57 {
58 pacc = pci_alloc();
59 pci_init(pacc);
60 }
61 ~PciUtilImpl()
62 {
63 pci_cleanup(pacc);
64 }
65
66 std::vector<PciDevice>
67 getPciDevices(std::optional<PciFilter> filter = std::nullopt) override;
68
69 private:
70 struct pci_access* pacc;
71};
72
73} // namespace host_tool