blob: 2f283e6bc6ee0559ccb76849d5282b494a8a1e15 [file] [log] [blame]
Patrick Ventureb5bf0fc2019-05-03 14:33:49 -07001/*
2 * Copyright 2019 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "pci.hpp"
18
19extern "C" {
20#include <pci/pci.h>
21} // extern "C"
22
23#include <optional>
24#include <vector>
25
26namespace host_tool
27{
28
29std::vector<PciDevice>
30 PciUtilImpl::getPciDevices(std::optional<PciFilter> filter)
31{
32 PciFilter test;
33 bool check = false;
34 std::vector<PciDevice> results;
35
36 if (filter.has_value())
37 {
38 test = filter.value();
39 check = true;
40 }
41
42 pci_scan_bus(pacc);
43 struct pci_dev* dev;
44
45 for (dev = pacc->devices; dev; dev = dev->next)
46 {
47 PciDevice item;
48
49 pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
50
51 item.bus = dev->bus;
52 item.dev = dev->dev;
53 item.func = dev->func;
54 item.vid = dev->vendor_id;
55 item.did = dev->device_id;
56
57 if (check)
58 {
59 if (test.vid == dev->vendor_id && test.did == dev->device_id)
60 {
61 results.push_back(item);
62 }
63 }
64 else
65 {
66 results.push_back(item);
67 }
68 }
69
70 return results;
71}
72
73} // namespace host_tool