1 /* 2 * Copyright 2020 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 #pragma once 18 19 extern "C" 20 { 21 #include <pciaccess.h> 22 } // extern "C" 23 24 namespace host_tool 25 { 26 27 /** 28 * @class PciAccess 29 * @brief Overridable interface to libpciacess for unit testing 30 */ 31 class PciAccess 32 { 33 public: 34 virtual struct pci_device_iterator* pci_id_match_iterator_create( 35 const struct pci_id_match* match) const = 0; 36 virtual void 37 pci_iterator_destroy(struct pci_device_iterator* iter) const = 0; 38 virtual struct pci_device* 39 pci_device_next(struct pci_device_iterator* iter) const = 0; 40 virtual int pci_device_probe(struct pci_device* dev) const = 0; 41 virtual int pci_device_map_range(struct pci_device* dev, pciaddr_t base, 42 pciaddr_t size, unsigned map_flags, 43 void** addr) const = 0; 44 virtual int pci_device_unmap_range(struct pci_device* dev, void* memory, 45 pciaddr_t size) const = 0; 46 47 virtual ~PciAccess() = default; 48 }; 49 50 /** 51 * @class PciAccessImpl 52 * @brief libpciaccess concrete implementation 53 * @details Passes through all calls to the underlying library. 54 */ 55 class PciAccessImpl : public PciAccess 56 { 57 public: 58 struct pci_device_iterator* pci_id_match_iterator_create( 59 const struct pci_id_match* match) const override; 60 void pci_iterator_destroy(struct pci_device_iterator* iter) const override; 61 struct pci_device* 62 pci_device_next(struct pci_device_iterator* iter) const override; 63 int pci_device_probe(struct pci_device* dev) const override; 64 int pci_device_map_range(struct pci_device* dev, pciaddr_t base, 65 pciaddr_t size, unsigned map_flags, 66 void** addr) const override; 67 int pci_device_unmap_range(struct pci_device* dev, void* memory, 68 pciaddr_t size) const override; 69 70 static PciAccessImpl& getInstance() 71 { 72 static PciAccessImpl instance; 73 return instance; 74 } 75 76 private: 77 PciAccessImpl(); 78 virtual ~PciAccessImpl(); 79 }; 80 81 } // namespace host_tool 82