xref: /openbmc/phosphor-power/phosphor-regulators/src/presence_detection.hpp (revision 0c9a33d66f74a76c1842990f41da678f79fa0c05)
1bfe2c25aSShawn McCarney /**
2bfe2c25aSShawn McCarney  * Copyright © 2020 IBM Corporation
3bfe2c25aSShawn McCarney  *
4bfe2c25aSShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5bfe2c25aSShawn McCarney  * you may not use this file except in compliance with the License.
6bfe2c25aSShawn McCarney  * You may obtain a copy of the License at
7bfe2c25aSShawn McCarney  *
8bfe2c25aSShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9bfe2c25aSShawn McCarney  *
10bfe2c25aSShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11bfe2c25aSShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12bfe2c25aSShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bfe2c25aSShawn McCarney  * See the License for the specific language governing permissions and
14bfe2c25aSShawn McCarney  * limitations under the License.
15bfe2c25aSShawn McCarney  */
16bfe2c25aSShawn McCarney #pragma once
17bfe2c25aSShawn McCarney 
18bfe2c25aSShawn McCarney #include "action.hpp"
19462e5926SBob King #include "services.hpp"
20bfe2c25aSShawn McCarney 
21bfe2c25aSShawn McCarney #include <memory>
22462e5926SBob King #include <optional>
23bfe2c25aSShawn McCarney #include <utility>
24bfe2c25aSShawn McCarney #include <vector>
25bfe2c25aSShawn McCarney 
26bfe2c25aSShawn McCarney namespace phosphor::power::regulators
27bfe2c25aSShawn McCarney {
28bfe2c25aSShawn McCarney 
29462e5926SBob King // Forward declarations to avoid circular dependencies
30462e5926SBob King class Chassis;
31462e5926SBob King class Device;
32462e5926SBob King class System;
33462e5926SBob King 
34bfe2c25aSShawn McCarney /**
35bfe2c25aSShawn McCarney  * @class PresenceDetection
36bfe2c25aSShawn McCarney  *
37bfe2c25aSShawn McCarney  * Specifies how to detect whether a device is present.
38bfe2c25aSShawn McCarney  *
39bfe2c25aSShawn McCarney  * Some devices are only present in certain system configurations.  For example:
40bfe2c25aSShawn McCarney  * - A regulator is only present when a related processor or memory module is
41bfe2c25aSShawn McCarney  *   present.
42bfe2c25aSShawn McCarney  * - A system supports multiple storage backplane types, and the device only
43bfe2c25aSShawn McCarney  *   exists on one of the backplanes.
44bfe2c25aSShawn McCarney  *
45bfe2c25aSShawn McCarney  * Device presence is detected by executing actions, such as
46bfe2c25aSShawn McCarney  * ComparePresenceAction and CompareVPDAction.
47bfe2c25aSShawn McCarney  *
48bfe2c25aSShawn McCarney  * Device operations like configuration and sensor monitoring will only be
49bfe2c25aSShawn McCarney  * performed if the actions indicate the device is present.
50bfe2c25aSShawn McCarney  *
51bfe2c25aSShawn McCarney  * Device presence will only be detected once per boot of the system.  Presence
52bfe2c25aSShawn McCarney  * will be determined prior to the first device operation (such as
53bfe2c25aSShawn McCarney  * configuration).  When the system is re-booted, presence will be re-detected.
54bfe2c25aSShawn McCarney  * As a result, presence detection is not supported for devices that can be
55bfe2c25aSShawn McCarney  * removed or added (hot-plugged) while the system is booted and running.
56bfe2c25aSShawn McCarney  */
57bfe2c25aSShawn McCarney class PresenceDetection
58bfe2c25aSShawn McCarney {
59bfe2c25aSShawn McCarney   public:
60bfe2c25aSShawn McCarney     // Specify which compiler-generated methods we want
61bfe2c25aSShawn McCarney     PresenceDetection() = delete;
62bfe2c25aSShawn McCarney     PresenceDetection(const PresenceDetection&) = delete;
63bfe2c25aSShawn McCarney     PresenceDetection(PresenceDetection&&) = delete;
64bfe2c25aSShawn McCarney     PresenceDetection& operator=(const PresenceDetection&) = delete;
65bfe2c25aSShawn McCarney     PresenceDetection& operator=(PresenceDetection&&) = delete;
66bfe2c25aSShawn McCarney     ~PresenceDetection() = default;
67bfe2c25aSShawn McCarney 
68bfe2c25aSShawn McCarney     /**
69bfe2c25aSShawn McCarney      * Constructor.
70bfe2c25aSShawn McCarney      *
71bfe2c25aSShawn McCarney      * @param actions actions that detect whether the device is present
72bfe2c25aSShawn McCarney      */
PresenceDetection(std::vector<std::unique_ptr<Action>> actions)73bfe2c25aSShawn McCarney     explicit PresenceDetection(std::vector<std::unique_ptr<Action>> actions) :
74bfe2c25aSShawn McCarney         actions{std::move(actions)}
75*0c9a33d6SAdriana Kobylak     {}
76bfe2c25aSShawn McCarney 
77bfe2c25aSShawn McCarney     /**
78462e5926SBob King      * Clears the cached presence value.
79462e5926SBob King      */
clearCache(void)80462e5926SBob King     void clearCache(void)
81462e5926SBob King     {
82462e5926SBob King         isPresent.reset();
83462e5926SBob King     }
84462e5926SBob King 
85462e5926SBob King     /**
86bfe2c25aSShawn McCarney      * Executes the actions to detect whether the device is present.
87bfe2c25aSShawn McCarney      *
88bfe2c25aSShawn McCarney      * The return value of the last action indicates whether the device is
89bfe2c25aSShawn McCarney      * present.  A return value of true means the device is present; false means
90bfe2c25aSShawn McCarney      * the device is missing.
91bfe2c25aSShawn McCarney      *
92462e5926SBob King      * Caches the resulting presence value.  Subsequent calls to execute() will
93462e5926SBob King      * return the cached value rather than re-executing the actions.  This
94462e5926SBob King      * provides a performance improvement since the actions may be expensive to
95462e5926SBob King      * execute, such as I2C reads or D-Bus method calls.  The cached value can
96462e5926SBob King      * be cleared by calling clearCache().
97462e5926SBob King      *
98bfe2c25aSShawn McCarney      * @return true if device is present, false otherwise
99bfe2c25aSShawn McCarney      */
100462e5926SBob King     bool execute(Services& services, System& system, Chassis& chassis,
101462e5926SBob King                  Device& device);
102bfe2c25aSShawn McCarney 
103bfe2c25aSShawn McCarney     /**
104bfe2c25aSShawn McCarney      * Returns the actions that detect whether the device is present.
105bfe2c25aSShawn McCarney      *
106bfe2c25aSShawn McCarney      * @return actions
107bfe2c25aSShawn McCarney      */
getActions() const108bfe2c25aSShawn McCarney     const std::vector<std::unique_ptr<Action>>& getActions() const
109bfe2c25aSShawn McCarney     {
110bfe2c25aSShawn McCarney         return actions;
111bfe2c25aSShawn McCarney     }
112bfe2c25aSShawn McCarney 
113462e5926SBob King     /**
114462e5926SBob King      * Returns the cached presence value, if any.
115462e5926SBob King      *
116462e5926SBob King      * @return cached presence value
117462e5926SBob King      */
getCachedPresence() const118462e5926SBob King     std::optional<bool> getCachedPresence() const
119462e5926SBob King     {
120462e5926SBob King         return isPresent;
121462e5926SBob King     }
122462e5926SBob King 
123bfe2c25aSShawn McCarney   private:
124bfe2c25aSShawn McCarney     /**
125bfe2c25aSShawn McCarney      * Actions that detect whether the device is present.
126bfe2c25aSShawn McCarney      */
127bfe2c25aSShawn McCarney     std::vector<std::unique_ptr<Action>> actions{};
128462e5926SBob King 
129462e5926SBob King     /**
130462e5926SBob King      * Cached presence value.  Initially has no value.
131462e5926SBob King      */
132462e5926SBob King     std::optional<bool> isPresent{};
133bfe2c25aSShawn McCarney };
134bfe2c25aSShawn McCarney 
135bfe2c25aSShawn McCarney } // namespace phosphor::power::regulators
136