blob: c81a671f9ee9149491330f4b83713d20b71506dc [file] [log] [blame]
Shawn McCarneyd58858c2020-11-04 18:28:01 -06001/**
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 McCarney3cd2b402021-01-30 15:35:58 -060019#include <sdbusplus/exception.hpp>
Shawn McCarneyd58858c2020-11-04 18:28:01 -060020
21#include <map>
22#include <string>
23
24namespace 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 */
34class 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 */
70class 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 McCarney3cd2b402021-01-30 15:35:58 -0600101 * 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 */
106 bool isExpectedException(const sdbusplus::exception::SdBusError& e);
107
108 /**
Shawn McCarneyd58858c2020-11-04 18:28:01 -0600109 * 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