summaryrefslogtreecommitdiffstats
path: root/presence/gpio.hpp
blob: 2829bea46f3115070b16c91a2f8069d28969948c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once

#include <sdeventplus/source/io.hpp>
#include <optional>
#include "evdevpp/evdev.hpp"
#include "psensor.hpp"
#include "utility.hpp"

namespace phosphor
{
namespace fan
{
namespace presence
{
class RedundancyPolicy;

/**
 * @class Gpio
 * @brief Gpio presence sensor implementation.
 *
 * The Gpio class uses a gpio wire to determine presence state.
 */
class Gpio : public PresenceSensor
{
    public:
        /**
         * @brief
         *
         * Cannot move or copy due to this ptr as context
         * for sdevent callbacks.
         */
        Gpio() = delete;
        Gpio(const Gpio&) = delete;
        Gpio& operator=(const Gpio&) = delete;
        Gpio(Gpio&&) = delete;
        Gpio& operator=(Gpio&&) = delete;
        ~Gpio() = default;

        /**
         * @brief Construct a gpio sensor.
         *
         * @param[in] physDevice - The physical gpio device path.
         * @param[in] device - The gpio-keys input device.
         * @param[in] physPin - The physical gpio pin number.
         */
        Gpio(
                const std::string& physDevice,
                const std::string& device,
                unsigned int physPin);

        /**
         * @brief start
         *
         * Register for an sdevent io callback on the gpio.
         * Query the initial state of the gpio.
         *
         * @return The current sensor state.
         */
        bool start() override;

        /**
         * @brief stop
         *
         * De-register sdevent io callback.
         */
        void stop() override;

        /**
         * @brief fail
         *
         * Call the gpio out.
         */
        void fail() override;

        /**
         * @brief Check the sensor.
         *
         * Query the gpio.
         */
        bool present() override;

    private :
         /** @brief Get the policy associated with this sensor. */
        virtual RedundancyPolicy& getPolicy() = 0;

         /** @brief sdevent io callback. */
        void ioCallback();

        /** The current state of the sensor. */
        bool currentState;

        /** Gpio event device file descriptor. */
        util::FileDescriptor evdevfd;

        /** Gpio event device. */
        evdevpp::evdev::EvDev evdev;

        /** Physical gpio device. */
        std::string phys;

        /** Gpio pin number. */
        unsigned int pin;

        /** sdevent io handle. */
        std::optional<sdeventplus::source::IO> source;
};

} // namespace presence
} // namespace fan
} // namespace phosphor
OpenPOWER on IntegriCloud