blob: 5ae96d30dac6e22c7b851745f69b20fbfb0fbcff [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 Williams48781ae2023-05-10 07:50:50 -050086 explicit DBusPresenceService(sdbusplus::bus_t& bus) : bus{bus} {}
Shawn McCarneyd58858c2020-11-04 18:28:01 -060087
88 /** @copydoc PresenceService::clearCache() */
89 virtual void clearCache(void) override
90 {
91 cache.clear();
92 }
93
94 /** @copydoc PresenceService::isPresent() */
95 virtual bool isPresent(const std::string& inventoryPath) override;
96
97 private:
98 /**
Shawn McCarney3cd2b402021-01-30 15:35:58 -060099 * Returns whether the specified D-Bus exception is one of the expected
100 * types that can be thrown if hardware is not present.
101 *
102 * @return true if exception type is expected, false otherwise
103 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500104 bool isExpectedException(const sdbusplus::exception_t& e);
Shawn McCarney3cd2b402021-01-30 15:35:58 -0600105
106 /**
Shawn McCarneyd58858c2020-11-04 18:28:01 -0600107 * D-Bus bus object.
108 */
Patrick Williams7354ce62022-07-22 19:26:56 -0500109 sdbusplus::bus_t& bus;
Shawn McCarneyd58858c2020-11-04 18:28:01 -0600110
111 /**
112 * Cached presence data.
113 *
114 * Map from inventory paths to presence values.
115 */
116 std::map<std::string, bool> cache{};
117};
118
119} // namespace phosphor::power::regulators