xref: /openbmc/phosphor-fan-presence/presence/gpio.hpp (revision 61b7329603e737b76b04b98746d69c1f410761b8)
15c589487SBrad Bishop #pragma once
25c589487SBrad Bishop 
35c589487SBrad Bishop #include "evdevpp/evdev.hpp"
45c589487SBrad Bishop #include "psensor.hpp"
55c589487SBrad Bishop #include "utility.hpp"
65c589487SBrad Bishop 
72d2caa34SMatthew Barth #include <sdeventplus/source/io.hpp>
82d2caa34SMatthew Barth 
92d2caa34SMatthew Barth #include <optional>
102d2caa34SMatthew Barth 
115c589487SBrad Bishop namespace phosphor
125c589487SBrad Bishop {
135c589487SBrad Bishop namespace fan
145c589487SBrad Bishop {
155c589487SBrad Bishop namespace presence
165c589487SBrad Bishop {
175c589487SBrad Bishop class RedundancyPolicy;
185c589487SBrad Bishop 
195c589487SBrad Bishop /**
205c589487SBrad Bishop  * @class Gpio
215c589487SBrad Bishop  * @brief Gpio presence sensor implementation.
225c589487SBrad Bishop  *
235c589487SBrad Bishop  * The Gpio class uses a gpio wire to determine presence state.
245c589487SBrad Bishop  */
255c589487SBrad Bishop class Gpio : public PresenceSensor
265c589487SBrad Bishop {
275c589487SBrad Bishop   public:
285c589487SBrad Bishop     /**
295c589487SBrad Bishop      * @brief
305c589487SBrad Bishop      *
315c589487SBrad Bishop      * Cannot move or copy due to this ptr as context
325c589487SBrad Bishop      * for sdevent callbacks.
335c589487SBrad Bishop      */
345c589487SBrad Bishop     Gpio() = delete;
355c589487SBrad Bishop     Gpio(const Gpio&) = delete;
365c589487SBrad Bishop     Gpio& operator=(const Gpio&) = delete;
375c589487SBrad Bishop     Gpio(Gpio&&) = delete;
385c589487SBrad Bishop     Gpio& operator=(Gpio&&) = delete;
395c589487SBrad Bishop     ~Gpio() = default;
405c589487SBrad Bishop 
415c589487SBrad Bishop     /**
425c589487SBrad Bishop      * @brief Construct a gpio sensor.
435c589487SBrad Bishop      *
445c589487SBrad Bishop      * @param[in] physDevice - The physical gpio device path.
452e9788d7SBrad Bishop      * @param[in] device - The gpio-keys input device.
465c589487SBrad Bishop      * @param[in] physPin - The physical gpio pin number.
475c589487SBrad Bishop      */
482d2caa34SMatthew Barth     Gpio(const std::string& physDevice, const std::string& device,
492e9788d7SBrad Bishop          unsigned int physPin);
505c589487SBrad Bishop 
515c589487SBrad Bishop     /**
525c589487SBrad Bishop      * @brief start
535c589487SBrad Bishop      *
545c589487SBrad Bishop      * Register for an sdevent io callback on the gpio.
555c589487SBrad Bishop      * Query the initial state of the gpio.
565c589487SBrad Bishop      *
575c589487SBrad Bishop      * @return The current sensor state.
585c589487SBrad Bishop      */
595c589487SBrad Bishop     bool start() override;
605c589487SBrad Bishop 
615c589487SBrad Bishop     /**
625c589487SBrad Bishop      * @brief stop
635c589487SBrad Bishop      *
645c589487SBrad Bishop      * De-register sdevent io callback.
655c589487SBrad Bishop      */
665c589487SBrad Bishop     void stop() override;
675c589487SBrad Bishop 
685c589487SBrad Bishop     /**
695c589487SBrad Bishop      * @brief fail
705c589487SBrad Bishop      *
715c589487SBrad Bishop      * Call the gpio out.
725c589487SBrad Bishop      */
735c589487SBrad Bishop     void fail() override;
745c589487SBrad Bishop 
755c589487SBrad Bishop     /**
765c589487SBrad Bishop      * @brief Check the sensor.
775c589487SBrad Bishop      *
785c589487SBrad Bishop      * Query the gpio.
795c589487SBrad Bishop      */
805c589487SBrad Bishop     bool present() override;
815c589487SBrad Bishop 
82c65d91d6SMatt Spinler     /**
83c65d91d6SMatt Spinler      * @brief Called when this presence sensor doesn't agree with other ones.
84c65d91d6SMatt Spinler      *
85c65d91d6SMatt Spinler      * @param[in] fanInventoryPath - The fan inventory D-Bus object path.
86c65d91d6SMatt Spinler      */
87c65d91d6SMatt Spinler     void logConflict(const std::string& fanInventoryPath) const override;
88c65d91d6SMatt Spinler 
895c589487SBrad Bishop   private:
905c589487SBrad Bishop     /** @brief Get the policy associated with this sensor. */
915c589487SBrad Bishop     virtual RedundancyPolicy& getPolicy() = 0;
925c589487SBrad Bishop 
935c589487SBrad Bishop     /** @brief sdevent io callback. */
941cfc2f11SWilliam A. Kennington III     void ioCallback();
955c589487SBrad Bishop 
965c589487SBrad Bishop     /** The current state of the sensor. */
975c589487SBrad Bishop     bool currentState;
985c589487SBrad Bishop 
995c589487SBrad Bishop     /** Gpio event device file descriptor. */
1005c589487SBrad Bishop     util::FileDescriptor evdevfd;
1015c589487SBrad Bishop 
1025c589487SBrad Bishop     /** Gpio event device. */
1035c589487SBrad Bishop     evdevpp::evdev::EvDev evdev;
1045c589487SBrad Bishop 
1055c589487SBrad Bishop     /** Physical gpio device. */
1065c589487SBrad Bishop     std::string phys;
1075c589487SBrad Bishop 
1085c589487SBrad Bishop     /** Gpio pin number. */
1095c589487SBrad Bishop     unsigned int pin;
1105c589487SBrad Bishop 
1111cfc2f11SWilliam A. Kennington III     /** sdevent io handle. */
1121cfc2f11SWilliam A. Kennington III     std::optional<sdeventplus::source::IO> source;
1135c589487SBrad Bishop };
1145c589487SBrad Bishop 
115a35a890bSMike Capps /**
116a35a890bSMike Capps  * @class NullGpio
117a35a890bSMike Capps  * @brief a phony presence sensor implementation that always
118a35a890bSMike Capps  *        reports not-present. Used to keep fan-presence service
119a35a890bSMike Capps  *        running when hardware is offline.
120a35a890bSMike Capps  *
121a35a890bSMike Capps  */
122a35a890bSMike Capps class NullGpio : public PresenceSensor
123a35a890bSMike Capps {
124a35a890bSMike Capps   public:
125a35a890bSMike Capps     NullGpio() = default;
126a35a890bSMike Capps 
127a35a890bSMike Capps     /**
128a35a890bSMike Capps      * @brief start
129a35a890bSMike Capps      *
130a35a890bSMike Capps      * Required to conform to interface
131a35a890bSMike Capps      *
132a35a890bSMike Capps      * @return false [dummy implementation]
133a35a890bSMike Capps      */
start()134a35a890bSMike Capps     bool start() override
135a35a890bSMike Capps     {
136a35a890bSMike Capps         return false;
137a35a890bSMike Capps     }
138a35a890bSMike Capps 
139a35a890bSMike Capps     /**
140a35a890bSMike Capps      * @brief stop
141a35a890bSMike Capps      *
142a35a890bSMike Capps      * Required to conform to interface
143a35a890bSMike Capps      */
stop()144*61b73296SPatrick Williams     void stop() override {}
145a35a890bSMike Capps 
146a35a890bSMike Capps     /**
147a35a890bSMike Capps      * @brief Check the sensor.
148a35a890bSMike Capps      *
149a35a890bSMike Capps      * @return false [dummy implementation]
150a35a890bSMike Capps      */
present()151a35a890bSMike Capps     bool present() override
152a35a890bSMike Capps     {
153a35a890bSMike Capps         return false;
154a35a890bSMike Capps     }
155a35a890bSMike Capps 
156a35a890bSMike Capps     /**
157a35a890bSMike Capps      * @brief Called when this presence sensor doesn't agree with other ones.
158a35a890bSMike Capps      *
159a35a890bSMike Capps      * @param[in] fanInventoryPath - The fan inventory D-Bus object path.
160a35a890bSMike Capps      */
logConflict(const std::string &) const161*61b73296SPatrick Williams     void logConflict(const std::string& /*fanInventoryPath*/) const override {}
162a35a890bSMike Capps 
163a35a890bSMike Capps   private:
164a35a890bSMike Capps     /**
165a35a890bSMike Capps      * @brief Required to conform to interface
166a35a890bSMike Capps      *
167a35a890bSMike Capps      */
168a35a890bSMike Capps     virtual RedundancyPolicy& getPolicy() = 0;
169a35a890bSMike Capps };
170a35a890bSMike Capps 
1715c589487SBrad Bishop } // namespace presence
1725c589487SBrad Bishop } // namespace fan
1735c589487SBrad Bishop } // namespace phosphor
174