blob: 9520dbd2d64fdaa586067f75186c30640fa61d5d [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 */
Patrick Williams7354ce62022-07-22 19:26:56 -050086 explicit DBusPresenceService(sdbusplus::bus_t& bus) : bus{bus}
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +000087 {}
Shawn McCarneyd58858c2020-11-04 18:28:01 -060088
89 /** @copydoc PresenceService::clearCache() */
90 virtual void clearCache(void) override
91 {
92 cache.clear();
93 }
94
95 /** @copydoc PresenceService::isPresent() */
96 virtual bool isPresent(const std::string& inventoryPath) override;
97
98 private:
99 /**
Shawn McCarney3cd2b402021-01-30 15:35:58 -0600100 * Returns whether the specified D-Bus exception is one of the expected
101 * types that can be thrown if hardware is not present.
102 *
103 * @return true if exception type is expected, false otherwise
104 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500105 bool isExpectedException(const sdbusplus::exception_t& e);
Shawn McCarney3cd2b402021-01-30 15:35:58 -0600106
107 /**
Shawn McCarneyd58858c2020-11-04 18:28:01 -0600108 * D-Bus bus object.
109 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500110 sdbusplus::bus_t& bus;
Shawn McCarneyd58858c2020-11-04 18:28:01 -0600111
112 /**
113 * Cached presence data.
114 *
115 * Map from inventory paths to presence values.
116 */
117 std::map<std::string, bool> cache{};
118};
119
120} // namespace phosphor::power::regulators