xref: /openbmc/dbus-sensors/src/leakdetector/LeakGPIODetector.hpp (revision 15dde8641baa1cb902d17f9857effb081e384944)
1 #pragma once
2 
3 #include "GPIOInterface.hpp"
4 #include "LeakEvents.hpp"
5 
6 #include <phosphor-logging/lg2.hpp>
7 #include <sdbusplus/async.hpp>
8 #include <sdbusplus/async/server.hpp>
9 #include <xyz/openbmc_project/Association/Definitions/aserver.hpp>
10 #include <xyz/openbmc_project/Configuration/GPIOLeakDetector/client.hpp>
11 #include <xyz/openbmc_project/State/Leak/Detector/aserver.hpp>
12 
13 #include <array>
14 #include <string>
15 #include <string_view>
16 #include <utility>
17 
18 PHOSPHOR_LOG2_USING;
19 
20 namespace leak
21 {
22 
23 class GPIODetector;
24 
25 using DetectorConfigIntf =
26     sdbusplus::client::xyz::openbmc_project::configuration::GPIOLeakDetector<>;
27 
28 using DetectorIntf = sdbusplus::async::server_t<
29     GPIODetector,
30     sdbusplus::aserver::xyz::openbmc_project::association::Definitions,
31     sdbusplus::aserver::xyz::openbmc_project::state::leak::Detector>;
32 
33 namespace config
34 {
35 
36 /** @brief Detector type to enum map */
37 static constexpr std::array<
38     std::pair<std::string_view, DetectorIntf::DetectorType>, 2>
39     validDetectorTypes = {
40         {{"LeakSensingCable", DetectorIntf::DetectorType::LeakSensingCable},
41          {"Unknown", DetectorIntf::DetectorType::Unknown}}};
42 
43 /** @brief GPIO polarity */
44 enum class PinPolarity
45 {
46     activeLow,
47     activeHigh,
48     unknown
49 };
50 
51 /** @brief Polarity name to enum map */
52 static constexpr std::array<std::pair<std::string_view, PinPolarity>, 2>
53     validPinPolarity = {
54         {{"Low", PinPolarity::activeLow}, {"High", PinPolarity::activeHigh}}};
55 
56 /** @brief Detector level */
57 enum class DetectorLevel
58 {
59     critical,
60     warning,
61     unknown
62 };
63 
64 /** @brief Leak detector level name to enum map */
65 static constexpr std::array<std::pair<std::string_view, DetectorLevel>, 2>
66     validDetectorLevel = {{{"Warning", DetectorLevel::warning},
67                            {"Critical", DetectorLevel::critical}}};
68 
69 /** @brief Leak detector configuration */
70 struct DetectorConfig
71 {
72     std::string name = Defaults::name;
73     DetectorIntf::DetectorType type = DetectorIntf::DetectorType::Unknown;
74     std::string pinName = Defaults::pinName;
75     PinPolarity polarity = PinPolarity::unknown;
76     DetectorLevel level = DetectorLevel::unknown;
77 
78     struct Defaults
79     {
80         static constexpr auto name = "unknown";
81         static constexpr auto pinName = "unknown";
82     };
83 };
84 
85 }; // namespace config
86 
87 class GPIODetector : public DetectorIntf
88 {
89   public:
90     explicit GPIODetector(sdbusplus::async::context& ctx, Events& leakEvents,
91                           const config::DetectorConfig& config);
92 
93     auto updateGPIOStateAsync(bool gpioState) -> sdbusplus::async::task<>;
94 
95   private:
96     sdbusplus::async::context& ctx;
97     Events& leakEvents;
98     config::DetectorConfig config;
99     gpio::GPIOInterface gpioInterface;
100 };
101 
102 } // namespace leak
103