Shawn McCarney | d58858c | 2020-11-04 18:28:01 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 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 | #pragma once |
| 17 | |
| 18 | #include <sdbusplus/bus.hpp> |
Shawn McCarney | 3cd2b40 | 2021-01-30 15:35:58 -0600 | [diff] [blame] | 19 | #include <sdbusplus/exception.hpp> |
Shawn McCarney | d58858c | 2020-11-04 18:28:01 -0600 | [diff] [blame] | 20 | |
| 21 | #include <map> |
| 22 | #include <string> |
| 23 | |
| 24 | namespace phosphor::power::regulators |
| 25 | { |
| 26 | |
| 27 | /** |
| 28 | * @class PresenceService |
| 29 | * |
| 30 | * Abstract base class that provides an interface to hardware presence data. |
| 31 | * |
| 32 | * The interface is used to determine whether hardware is present. |
| 33 | */ |
| 34 | class PresenceService |
| 35 | { |
| 36 | public: |
| 37 | // Specify which compiler-generated methods we want |
| 38 | PresenceService() = default; |
| 39 | PresenceService(const PresenceService&) = delete; |
| 40 | PresenceService(PresenceService&&) = delete; |
| 41 | PresenceService& operator=(const PresenceService&) = delete; |
| 42 | PresenceService& operator=(PresenceService&&) = delete; |
| 43 | virtual ~PresenceService() = default; |
| 44 | |
| 45 | /** |
| 46 | * Clears any cached hardware presence data. |
| 47 | */ |
| 48 | virtual void clearCache(void) = 0; |
| 49 | |
| 50 | /** |
| 51 | * Returns whether the hardware with the specified inventory path is |
| 52 | * present. |
| 53 | * |
| 54 | * May return a cached value if one is available to improve performance. |
| 55 | * |
| 56 | * Throws an exception if an error occurs while obtaining the presence |
| 57 | * value. |
| 58 | * |
| 59 | * @param inventoryPath D-Bus inventory path of the hardware |
| 60 | * @return true if hardware is present, false otherwise |
| 61 | */ |
| 62 | virtual bool isPresent(const std::string& inventoryPath) = 0; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * @class DBusPresenceService |
| 67 | * |
| 68 | * Implementation of the PresenceService interface using D-Bus method calls. |
| 69 | */ |
| 70 | class DBusPresenceService : public PresenceService |
| 71 | { |
| 72 | public: |
| 73 | // Specify which compiler-generated methods we want |
| 74 | DBusPresenceService() = delete; |
| 75 | DBusPresenceService(const DBusPresenceService&) = delete; |
| 76 | DBusPresenceService(DBusPresenceService&&) = delete; |
| 77 | DBusPresenceService& operator=(const DBusPresenceService&) = delete; |
| 78 | DBusPresenceService& operator=(DBusPresenceService&&) = delete; |
| 79 | virtual ~DBusPresenceService() = default; |
| 80 | |
| 81 | /** |
| 82 | * Constructor. |
| 83 | * |
| 84 | * @param bus D-Bus bus object |
| 85 | */ |
| 86 | explicit DBusPresenceService(sdbusplus::bus::bus& bus) : bus{bus} |
| 87 | { |
| 88 | } |
| 89 | |
| 90 | /** @copydoc PresenceService::clearCache() */ |
| 91 | virtual void clearCache(void) override |
| 92 | { |
| 93 | cache.clear(); |
| 94 | } |
| 95 | |
| 96 | /** @copydoc PresenceService::isPresent() */ |
| 97 | virtual bool isPresent(const std::string& inventoryPath) override; |
| 98 | |
| 99 | private: |
| 100 | /** |
Shawn McCarney | 3cd2b40 | 2021-01-30 15:35:58 -0600 | [diff] [blame] | 101 | * Returns whether the specified D-Bus exception is one of the expected |
| 102 | * types that can be thrown if hardware is not present. |
| 103 | * |
| 104 | * @return true if exception type is expected, false otherwise |
| 105 | */ |
Patrick Williams | 69f10ad | 2021-09-02 09:46:49 -0500 | [diff] [blame] | 106 | bool isExpectedException(const sdbusplus::exception::exception& e); |
Shawn McCarney | 3cd2b40 | 2021-01-30 15:35:58 -0600 | [diff] [blame] | 107 | |
| 108 | /** |
Shawn McCarney | d58858c | 2020-11-04 18:28:01 -0600 | [diff] [blame] | 109 | * D-Bus bus object. |
| 110 | */ |
| 111 | sdbusplus::bus::bus& bus; |
| 112 | |
| 113 | /** |
| 114 | * Cached presence data. |
| 115 | * |
| 116 | * Map from inventory paths to presence values. |
| 117 | */ |
| 118 | std::map<std::string, bool> cache{}; |
| 119 | }; |
| 120 | |
| 121 | } // namespace phosphor::power::regulators |