xref: /openbmc/gpioplus/src/gpioplus/event.hpp (revision 91214423)
1 #pragma once
2 #include <gpioplus/chip.hpp>
3 #include <gpioplus/handle.hpp>
4 #include <gpioplus/internal/fd.hpp>
5 
6 #include <chrono>
7 #include <cstdint>
8 #include <optional>
9 #include <ratio>
10 #include <string_view>
11 
12 namespace gpioplus
13 {
14 
15 /** @brief The flags used for registering an event */
16 struct EventFlags
17 {
18     /** @brief Are rising edge events reported */
19     bool rising_edge;
20     /** @brief Are falling edge events reported */
21     bool falling_edge;
22 
23     /** @brief Converts this struct to an int bitfield
24      *
25      *  @return The int bitfield usable by the syscall interface
26      */
27     uint32_t toInt() const;
28 };
29 
30 /** @class EventInterface
31  *  @brief Interface used for providing gpio events
32  */
33 class EventInterface
34 {
35   public:
36     virtual ~EventInterface() = default;
37 
38     /** @brief Event data read from the gpio line */
39     struct Data
40     {
41         /** @brief The estimate of the time the event occurred */
42         std::chrono::duration<uint64_t, std::nano> timestamp;
43         /** @brief The identifier of the event */
44         uint32_t id;
45     };
46 
47     virtual std::optional<Data> read() const = 0;
48     virtual uint8_t getValue() const = 0;
49 };
50 
51 /** @class Event
52  *  @brief Handle to a gpio line event
53  *  @details Provides a c++ interface for gpio event operations
54  */
55 class Event : public EventInterface
56 {
57   public:
58     /** @brief Creates a new gpio line event handler
59      *         The underlying implementation of the event is independent of
60      *         the provided chip object. It is safe to destroy any of the
61      *         provided inputs while this event is alive.
62      *
63      *  @param[in] chip           - The gpio chip which provides the events
64      *  @param[in] line_offset    - The offset of the line generating events
65      *  @param[in] handle_flags   - The handle flags applied
66      *  @param[in] event_flags    - The event flags applied
67      *  @param[in] consumer_label - The functional name of this consumer
68      *  @throws std::system_error for underlying syscall failures
69      */
70     Event(const Chip& chip, uint32_t line_offset, HandleFlags handle_flags,
71           EventFlags event_flags, std::string_view consumer_label);
72 
73     /** @brief Get the file descriptor used for the handle
74      *
75      *  @return The gpio handle file descriptor
76      */
77     const internal::Fd& getFd() const;
78 
79     /** @brief Reads an event from the event file descriptor
80      *         Follows the read(2) semantics of the underlying file descriptor
81      *
82      *  @throws std::system_error for underlying syscall failures
83      *  @return The value of the event or std::nullopt if the file descriptor
84      *          is non-blocking and no event has occurred
85      */
86     std::optional<Data> read() const override;
87 
88     /** @brief Get the current value of the associated line
89      *
90      *  @throws std::system_error for underlying syscall failures
91      *  @return The value of the gpio line
92      */
93     uint8_t getValue() const override;
94 
95   private:
96     internal::Fd fd;
97 };
98 
99 } // namespace gpioplus
100