xref: /openbmc/gpioplus/src/gpioplus/chip.hpp (revision 7ba248ad685c8a2029f9cfdbae98d161e9664a3b)
1 #pragma once
2 #include <gpioplus/internal/fd.hpp>
3 #include <gpioplus/internal/sys.hpp>
4 
5 #include <cstdint>
6 #include <string>
7 
8 namespace gpioplus
9 {
10 
11 /** @brief Information about the queried gpio chip */
12 struct ChipInfo
13 {
14     /** @brief Kernel name of the chip */
15     std::string name;
16     /** @brief Functional name of the chip */
17     std::string label;
18     /** @brief Number of lines on the chip */
19     uint32_t lines;
20 };
21 
22 /** @brief Flags pertaining to the gpio line */
23 struct LineFlags
24 {
25     /** @brief Is the kernel currently using the line */
26     bool kernel;
27     /** @brief Is the line used for output (otherwise input) */
28     bool output;
29     /** @brief Is the line value active at low voltage */
30     bool active_low;
31     /** @brief Is the line an open drain */
32     bool open_drain;
33     /** @brief Is the line an open source */
34     bool open_source;
35 
36     /** @brief Converts the syscall flags to this struct
37      *
38      *  @param[in] flags - The int bitfield of flags
39      */
40     LineFlags(uint32_t flags);
41 };
42 
43 /** @brief Information about the queried gpio line */
44 struct LineInfo
45 {
46     /** @brief Flags that apply to the line */
47     LineFlags flags;
48     /** @brief name of the line as specified by the gpio chip */
49     std::string name;
50     /** @brief the name of the consumer of the line */
51     std::string consumer;
52 };
53 
54 /** @class Chip
55  *  @brief Handle to a gpio chip
56  *  @details Provides a c++ interface to gpio chip operations
57  */
58 class Chip
59 {
60   public:
61     /** @brief Creates a new chip from chip id
62      *         Ids come from the /sys/bus/gpio/devices/gpiochip{id}
63      *
64      *  @param[in] id  - Id of the chip to open
65      *  @param[in] sys - Optional underlying syscall implementation
66      */
67     Chip(unsigned id, const internal::Sys* sys = &internal::sys_impl);
68 
69     /** @brief Gets the information about this chip
70      *
71      *  @throws std::system_error for underlying syscall failures
72      *  @return The chip information
73      */
74     ChipInfo getChipInfo() const;
75 
76     /** @brief Gets the information about a line on the chip
77      *
78      *  @throws std::system_error for underlying syscall failures
79      *  @return The line information
80      */
81     LineInfo getLineInfo(uint32_t offset) const;
82 
83     /** @brief Get the file descriptor associated with the chip
84      *
85      *  @return The file descriptor
86      */
87     const internal::Fd& getFd() const;
88 
89   private:
90     internal::Fd fd;
91 };
92 
93 } // namespace gpioplus
94