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>
19 
20 #include <map>
21 #include <string>
22 
23 namespace phosphor::power::regulators
24 {
25 
26 /**
27  * @class PresenceService
28  *
29  * Abstract base class that provides an interface to hardware presence data.
30  *
31  * The interface is used to determine whether hardware is present.
32  */
33 class PresenceService
34 {
35   public:
36     // Specify which compiler-generated methods we want
37     PresenceService() = default;
38     PresenceService(const PresenceService&) = delete;
39     PresenceService(PresenceService&&) = delete;
40     PresenceService& operator=(const PresenceService&) = delete;
41     PresenceService& operator=(PresenceService&&) = delete;
42     virtual ~PresenceService() = default;
43 
44     /**
45      * Clears any cached hardware presence data.
46      */
47     virtual void clearCache(void) = 0;
48 
49     /**
50      * Returns whether the hardware with the specified inventory path is
51      * present.
52      *
53      * May return a cached value if one is available to improve performance.
54      *
55      * Throws an exception if an error occurs while obtaining the presence
56      * value.
57      *
58      * @param inventoryPath D-Bus inventory path of the hardware
59      * @return true if hardware is present, false otherwise
60      */
61     virtual bool isPresent(const std::string& inventoryPath) = 0;
62 };
63 
64 /**
65  * @class DBusPresenceService
66  *
67  * Implementation of the PresenceService interface using D-Bus method calls.
68  */
69 class DBusPresenceService : public PresenceService
70 {
71   public:
72     // Specify which compiler-generated methods we want
73     DBusPresenceService() = delete;
74     DBusPresenceService(const DBusPresenceService&) = delete;
75     DBusPresenceService(DBusPresenceService&&) = delete;
76     DBusPresenceService& operator=(const DBusPresenceService&) = delete;
77     DBusPresenceService& operator=(DBusPresenceService&&) = delete;
78     virtual ~DBusPresenceService() = default;
79 
80     /**
81      * Constructor.
82      *
83      * @param bus D-Bus bus object
84      */
85     explicit DBusPresenceService(sdbusplus::bus::bus& bus) : bus{bus}
86     {
87     }
88 
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     /**
100      * D-Bus bus object.
101      */
102     sdbusplus::bus::bus& bus;
103 
104     /**
105      * Cached presence data.
106      *
107      * Map from inventory paths to presence values.
108      */
109     std::map<std::string, bool> cache{};
110 };
111 
112 } // namespace phosphor::power::regulators
113