blob: 5f9e28bc21de4a07010a4d26976e698700ae3246 [file] [log] [blame]
Brad Bishop5c589482017-06-14 22:32:20 -04001#pragma once
2
Brad Bishop5c589482017-06-14 22:32:20 -04003#include "evdevpp/evdev.hpp"
Brad Bishop5c589482017-06-14 22:32:20 -04004#include "psensor.hpp"
5#include "utility.hpp"
6
Matthew Barth2d2caa32020-05-26 11:07:24 -05007#include <sdeventplus/source/io.hpp>
8
9#include <optional>
10
Brad Bishop5c589482017-06-14 22:32:20 -040011namespace phosphor
12{
13namespace fan
14{
15namespace presence
16{
17class RedundancyPolicy;
18
19/**
20 * @class Gpio
21 * @brief Gpio presence sensor implementation.
22 *
23 * The Gpio class uses a gpio wire to determine presence state.
24 */
25class Gpio : public PresenceSensor
26{
Matthew Barth2d2caa32020-05-26 11:07:24 -050027 public:
28 /**
29 * @brief
30 *
31 * Cannot move or copy due to this ptr as context
32 * for sdevent callbacks.
33 */
34 Gpio() = delete;
35 Gpio(const Gpio&) = delete;
36 Gpio& operator=(const Gpio&) = delete;
37 Gpio(Gpio&&) = delete;
38 Gpio& operator=(Gpio&&) = delete;
39 ~Gpio() = default;
Brad Bishop5c589482017-06-14 22:32:20 -040040
Matthew Barth2d2caa32020-05-26 11:07:24 -050041 /**
42 * @brief Construct a gpio sensor.
43 *
44 * @param[in] physDevice - The physical gpio device path.
45 * @param[in] device - The gpio-keys input device.
46 * @param[in] physPin - The physical gpio pin number.
47 */
48 Gpio(const std::string& physDevice, const std::string& device,
49 unsigned int physPin);
Brad Bishop5c589482017-06-14 22:32:20 -040050
Matthew Barth2d2caa32020-05-26 11:07:24 -050051 /**
52 * @brief start
53 *
54 * Register for an sdevent io callback on the gpio.
55 * Query the initial state of the gpio.
56 *
57 * @return The current sensor state.
58 */
59 bool start() override;
Brad Bishop5c589482017-06-14 22:32:20 -040060
Matthew Barth2d2caa32020-05-26 11:07:24 -050061 /**
62 * @brief stop
63 *
64 * De-register sdevent io callback.
65 */
66 void stop() override;
Brad Bishop5c589482017-06-14 22:32:20 -040067
Matthew Barth2d2caa32020-05-26 11:07:24 -050068 /**
69 * @brief fail
70 *
71 * Call the gpio out.
72 */
73 void fail() override;
Brad Bishop5c589482017-06-14 22:32:20 -040074
Matthew Barth2d2caa32020-05-26 11:07:24 -050075 /**
76 * @brief Check the sensor.
77 *
78 * Query the gpio.
79 */
80 bool present() override;
Brad Bishop5c589482017-06-14 22:32:20 -040081
Matthew Barth2d2caa32020-05-26 11:07:24 -050082 private:
83 /** @brief Get the policy associated with this sensor. */
84 virtual RedundancyPolicy& getPolicy() = 0;
Brad Bishop5c589482017-06-14 22:32:20 -040085
Matthew Barth2d2caa32020-05-26 11:07:24 -050086 /** @brief sdevent io callback. */
87 void ioCallback();
Brad Bishop5c589482017-06-14 22:32:20 -040088
Matthew Barth2d2caa32020-05-26 11:07:24 -050089 /** The current state of the sensor. */
90 bool currentState;
Brad Bishop5c589482017-06-14 22:32:20 -040091
Matthew Barth2d2caa32020-05-26 11:07:24 -050092 /** Gpio event device file descriptor. */
93 util::FileDescriptor evdevfd;
Brad Bishop5c589482017-06-14 22:32:20 -040094
Matthew Barth2d2caa32020-05-26 11:07:24 -050095 /** Gpio event device. */
96 evdevpp::evdev::EvDev evdev;
Brad Bishop5c589482017-06-14 22:32:20 -040097
Matthew Barth2d2caa32020-05-26 11:07:24 -050098 /** Physical gpio device. */
99 std::string phys;
Brad Bishop5c589482017-06-14 22:32:20 -0400100
Matthew Barth2d2caa32020-05-26 11:07:24 -0500101 /** Gpio pin number. */
102 unsigned int pin;
Brad Bishop5c589482017-06-14 22:32:20 -0400103
Matthew Barth2d2caa32020-05-26 11:07:24 -0500104 /** sdevent io handle. */
105 std::optional<sdeventplus::source::IO> source;
Brad Bishop5c589482017-06-14 22:32:20 -0400106};
107
108} // namespace presence
109} // namespace fan
110} // namespace phosphor