| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | /** |
| 5 | * @file line-info.hpp |
| 6 | */ |
| 7 | |
| 8 | #ifndef __LIBGPIOD_CXX_LINE_INFO_HPP__ |
| 9 | #define __LIBGPIOD_CXX_LINE_INFO_HPP__ |
| 10 | |
| 11 | #if !defined(__LIBGPIOD_GPIOD_CXX_INSIDE__) |
| 12 | #error "Only gpiod.hpp can be included directly." |
| 13 | #endif |
| 14 | |
| 15 | #include <chrono> |
| 16 | #include <iostream> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | |
| 20 | namespace gpiod { |
| 21 | |
| 22 | class chip; |
| 23 | class info_event; |
| 24 | |
| 25 | /** |
| 26 | * @brief Contains an immutable snapshot of the line's state at the |
| 27 | * time when the object of this class was instantiated. |
| 28 | */ |
| 29 | class line_info final |
| 30 | { |
| 31 | public: |
| 32 | |
| 33 | /** |
| 34 | * @brief Copy constructor. |
| 35 | * @param other Object to copy. |
| 36 | */ |
| 37 | line_info(const line_info& other) noexcept; |
| 38 | |
| 39 | /** |
| 40 | * @brief Move constructor. |
| 41 | * @param other Object to move. |
| 42 | */ |
| 43 | line_info(line_info&& other) noexcept; |
| 44 | |
| 45 | ~line_info(); |
| 46 | |
| 47 | /** |
| 48 | * @brief Copy assignment operator. |
| 49 | * @param other Object to copy. |
| 50 | * @return Reference to self. |
| 51 | */ |
| 52 | line_info& operator=(const line_info& other) noexcept; |
| 53 | |
| 54 | /** |
| 55 | * @brief Move assignment operator. |
| 56 | * @param other Object to move. |
| 57 | * @return Reference to self. |
| 58 | */ |
| 59 | line_info& operator=(line_info&& other) noexcept; |
| 60 | |
| 61 | /** |
| 62 | * @brief Get the hardware offset of the line. |
| 63 | * @return Offset of the line within the parent chip. |
| 64 | */ |
| 65 | line::offset offset() const noexcept; |
| 66 | |
| 67 | /** |
| 68 | * @brief Get the GPIO line name. |
| 69 | * @return Name of the GPIO line as it is represented in the kernel. |
| 70 | * This routine returns an empty string if the line is unnamed. |
| 71 | */ |
| 72 | ::std::string name() const noexcept; |
| 73 | |
| 74 | /** |
| 75 | * @brief Check if the line is currently in use. |
| 76 | * @return True if the line is in use, false otherwise. |
| 77 | * |
| 78 | * The user space can't know exactly why a line is busy. It may have |
| 79 | * been requested by another process or hogged by the kernel. It only |
| 80 | * matters that the line is used and we can't request it. |
| 81 | */ |
| 82 | bool used() const noexcept; |
| 83 | |
| 84 | /** |
| 85 | * @brief Read the GPIO line consumer name. |
| 86 | * @return Name of the GPIO consumer name as it is represented in the |
| 87 | * kernel. This routine returns an empty string if the line is |
| 88 | * not used. |
| 89 | */ |
| 90 | ::std::string consumer() const noexcept; |
| 91 | |
| 92 | /** |
| 93 | * @brief Read the GPIO line direction setting. |
| 94 | * @return Returns DIRECTION_INPUT or DIRECTION_OUTPUT. |
| 95 | */ |
| 96 | line::direction direction() const; |
| 97 | |
| 98 | /** |
| 99 | * @brief Read the current edge detection setting of this line. |
| 100 | * @return Returns EDGE_NONE, EDGE_RISING, EDGE_FALLING or EDGE_BOTH. |
| 101 | */ |
| 102 | line::edge edge_detection() const; |
| 103 | |
| 104 | /** |
| 105 | * @brief Read the GPIO line bias setting. |
| 106 | * @return Returns BIAS_PULL_UP, BIAS_PULL_DOWN, BIAS_DISABLE or |
| 107 | * BIAS_UNKNOWN. |
| 108 | */ |
| 109 | line::bias bias() const; |
| 110 | |
| 111 | /** |
| 112 | * @brief Read the GPIO line drive setting. |
| 113 | * @return Returns DRIVE_PUSH_PULL, DRIVE_OPEN_DRAIN or |
| 114 | * DRIVE_OPEN_SOURCE. |
| 115 | */ |
| 116 | line::drive drive() const; |
| 117 | |
| 118 | /** |
| 119 | * @brief Check if the signal of this line is inverted. |
| 120 | * @return True if this line is "active-low", false otherwise. |
| 121 | */ |
| 122 | bool active_low() const noexcept; |
| 123 | |
| 124 | /** |
| 125 | * @brief Check if this line is debounced (either by hardware or by the |
| 126 | * kernel software debouncer). |
| 127 | * @return True if the line is debounced, false otherwise. |
| 128 | */ |
| 129 | bool debounced() const noexcept; |
| 130 | |
| 131 | /** |
| 132 | * @brief Read the current debounce period in microseconds. |
| 133 | * @return Current debounce period in microseconds, 0 if the line is |
| 134 | * not debounced. |
| 135 | */ |
| 136 | ::std::chrono::microseconds debounce_period() const noexcept; |
| 137 | |
| 138 | /** |
| 139 | * @brief Read the current event clock setting used for edge event |
| 140 | * timestamps. |
| 141 | * @return Returns MONOTONIC, REALTIME or HTE. |
| 142 | */ |
| 143 | line::clock event_clock() const; |
| 144 | |
| 145 | private: |
| 146 | |
| 147 | line_info(); |
| 148 | |
| 149 | struct impl; |
| 150 | |
| 151 | ::std::shared_ptr<impl> _m_priv; |
| 152 | |
| 153 | friend chip; |
| 154 | friend info_event; |
| 155 | }; |
| 156 | |
| 157 | /** |
| 158 | * @brief Stream insertion operator for GPIO line info objects. |
| 159 | * @param out Output stream to write to. |
| 160 | * @param info GPIO line info object to insert into the output stream. |
| 161 | * @return Reference to out. |
| 162 | */ |
| 163 | ::std::ostream& operator<<(::std::ostream& out, const line_info& info); |
| 164 | |
| 165 | } /* namespace gpiod */ |
| 166 | |
| 167 | #endif /* __LIBGPIOD_CXX_LINE_INFO_HPP__ */ |