xref: /openbmc/gpioplus/src/gpioplus/handle.hpp (revision 7ba248ad685c8a2029f9cfdbae98d161e9664a3b)
1 #pragma once
2 #include <gpioplus/chip.hpp>
3 #include <gpioplus/internal/fd.hpp>
4 
5 #include <cstdint>
6 #include <string_view>
7 #include <vector>
8 
9 namespace gpioplus
10 {
11 
12 /** @brief Flags to set when taking a handle */
13 struct HandleFlags
14 {
15     /** @brief Is the line used for output (otherwise input) */
16     bool output;
17     /** @brief Is the line value active at low voltage */
18     bool active_low;
19     /** @brief Is the line an open drain */
20     bool open_drain;
21     /** @brief Is the line an open source */
22     bool open_source;
23 
24     HandleFlags() = default;
25 
26     /** @brief Creates handle flags from the gpio line flags
27      *
28      *  @param[in] line_flags - Line flags used for population
29      */
30     explicit HandleFlags(LineFlags line_flags);
31 
32     /** @brief Converts this struct to an int bitfield
33      *
34      *  @return The int bitfield usable by the syscall interface
35      */
36     uint32_t toInt() const;
37 };
38 
39 /** @class HandleInterface
40  *  @brief Handle interface to provide a set of methods required to exist in
41  * derived objects.
42  */
43 class HandleInterface
44 {
45   public:
46     virtual ~HandleInterface() = default;
47 
48     virtual std::vector<uint8_t> getValues() const = 0;
49     virtual void getValues(std::vector<uint8_t>& values) const = 0;
50     virtual void setValues(const std::vector<uint8_t>& values) const = 0;
51 };
52 
53 /** @class Handle
54  *  @brief Handle to a gpio line handle
55  *  @details Provides a c++ interface for gpio handle operations
56  */
57 class Handle : public HandleInterface
58 {
59   public:
60     /** @brief Per line information used to construct a handle */
61     struct Line
62     {
63         /** @brief Offset of the line on the gpio chip */
64         uint32_t offset;
65         /** @brief Default output value of the line */
66         uint8_t default_value;
67     };
68 
69     /** @brief Creates a new gpio handle
70      *         The underlying implementation of the handle is independent of
71      *         the provided chip object. It is safe to destroy any of the
72      *         provided inputs while this handle is alive.
73      *
74      *  @param[in] chip           - The gpio chip which provides the handle
75      *  @param[in] lines          - A collection of lines the handle provides
76      *  @param[in] flags          - The flags applied to all lines
77      *  @param[in] consumer_label - The functional name of this consumer
78      *  @throws std::system_error for underlying syscall failures
79      */
80     Handle(const Chip& chip, const std::vector<Line>& lines, HandleFlags flags,
81            std::string_view consumer_label);
82 
83     /** @brief Get the file descriptor used for the handle
84      *
85      *  @return The gpio handle file descriptor
86      */
87     const internal::Fd& getFd() const;
88 
89     /** @brief Get the current values of all associated lines
90      *
91      *  @throws std::system_error for underlying syscall failures
92      *  @return The values of the gpio lines
93      */
94     std::vector<uint8_t> getValues() const override;
95 
96     /** @brief Gets the current values of all associated lines
97      *
98      *  @param[out] values - The values of the gpio lines
99      *  @throws std::system_error for underlying syscall failures
100      */
101     void getValues(std::vector<uint8_t>& values) const override;
102 
103     /** @brief Sets the current values of all associated lines
104      *
105      *  @param[in] values - The new values of the gpio lines
106      *  @throws std::system_error for underlying syscall failures
107      */
108     void setValues(const std::vector<uint8_t>& values) const override;
109 
110   private:
111     internal::Fd fd;
112     uint32_t nlines;
113 };
114 
115 } // namespace gpioplus
116