xref: /openbmc/phosphor-power/phosphor-power-supply/util_base.hpp (revision f54021972b91be5058b50e9046bb0dd5a3b22a80)
1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <sdbusplus/bus/match.hpp>
6 
7 #include <bitset>
8 #include <chrono>
9 
10 namespace phosphor::power::psu
11 {
12 
13 /**
14  * @class UtilBase
15  * A base class to allow for mocking certain utility functions.
16  */
17 class UtilBase
18 {
19   public:
20     virtual ~UtilBase() = default;
21 
22     virtual bool getPresence(sdbusplus::bus_t& bus,
23                              const std::string& invpath) const = 0;
24 
25     virtual void setPresence(sdbusplus::bus_t& bus, const std::string& invpath,
26                              bool present, const std::string& name) const = 0;
27 
28     virtual void setAvailable(sdbusplus::bus_t& bus, const std::string& invpath,
29                               bool available) const = 0;
30 
31     virtual void handleChassisHealthRollup(sdbusplus::bus_t& bus,
32                                            const std::string& invpath,
33                                            bool addRollup) const = 0;
34 
35     virtual std::string getChassis(sdbusplus::bus_t& /*bus*/,
36                                    const std::string& /*invpath*/) const = 0;
37 };
38 
39 const UtilBase& getUtils();
40 
getPresence(sdbusplus::bus_t & bus,const std::string & invpath)41 inline bool getPresence(sdbusplus::bus_t& bus, const std::string& invpath)
42 {
43     return getUtils().getPresence(bus, invpath);
44 }
45 
setPresence(sdbusplus::bus_t & bus,const std::string & invpath,bool present,const std::string & name)46 inline void setPresence(sdbusplus::bus_t& bus, const std::string& invpath,
47                         bool present, const std::string& name)
48 {
49     return getUtils().setPresence(bus, invpath, present, name);
50 }
51 
setAvailable(sdbusplus::bus_t & bus,const std::string & invpath,bool available)52 inline void setAvailable(sdbusplus::bus_t& bus, const std::string& invpath,
53                          bool available)
54 {
55     getUtils().setAvailable(bus, invpath, available);
56 }
57 
handleChassisHealthRollup(sdbusplus::bus_t & bus,const std::string & invpath,bool addRollup)58 inline void handleChassisHealthRollup(
59     sdbusplus::bus_t& bus, const std::string& invpath, bool addRollup)
60 {
61     getUtils().handleChassisHealthRollup(bus, invpath, addRollup);
62 }
63 
getChassis(sdbusplus::bus_t & bus,const std::string & invpath)64 inline std::string getChassis(sdbusplus::bus_t& bus, const std::string& invpath)
65 {
66     return getUtils().getChassis(bus, invpath);
67 }
68 
69 class GPIOInterfaceBase
70 {
71   public:
72     virtual ~GPIOInterfaceBase() = default;
73 
74     virtual int read() = 0;
75     virtual void write(int value, std::bitset<32> flags) = 0;
76     virtual void toggleLowHigh(const std::chrono::milliseconds& delay) = 0;
77     virtual std::string getName() const = 0;
78 };
79 
80 } // namespace phosphor::power::psu
81