| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | /** |
| 5 | * @file line-request.hpp |
| 6 | */ |
| 7 | |
| 8 | #ifndef __LIBGPIOD_CXX_LINE_REQUEST_HPP__ |
| 9 | #define __LIBGPIOD_CXX_LINE_REQUEST_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 <cstddef> |
| 17 | #include <iostream> |
| 18 | #include <memory> |
| 19 | |
| 20 | #include "misc.hpp" |
| 21 | |
| 22 | namespace gpiod { |
| 23 | |
| 24 | class chip; |
| 25 | class edge_event; |
| 26 | class edge_event_buffer; |
| 27 | class line_config; |
| 28 | |
| 29 | /** |
| 30 | * @brief Stores the context of a set of requested GPIO lines. |
| 31 | */ |
| 32 | class line_request final |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | line_request(const line_request& other) = delete; |
| 37 | |
| 38 | /** |
| 39 | * @brief Move constructor. |
| 40 | * @param other Object to move. |
| 41 | */ |
| 42 | line_request(line_request&& other) noexcept; |
| 43 | |
| 44 | ~line_request(); |
| 45 | |
| 46 | line_request& operator=(const line_request& other) = delete; |
| 47 | |
| 48 | /** |
| 49 | * @brief Move assignment operator. |
| 50 | * @param other Object to move. |
| 51 | * @return Reference to self. |
| 52 | */ |
| 53 | line_request& operator=(line_request&& other) noexcept; |
| 54 | |
| 55 | /** |
| 56 | * @brief Check if this object is valid. |
| 57 | * @return True if this object's methods can be used, false otherwise. |
| 58 | * False usually means the request was released. If the user |
| 59 | * calls any of the methods of this class on an object for |
| 60 | * which this operator returned false, a logic_error will be |
| 61 | * thrown. |
| 62 | */ |
| 63 | explicit operator bool() const noexcept; |
| 64 | |
| 65 | /** |
| 66 | * @brief Release the requested lines and free all associated resources. |
| 67 | * @note The object can still be used after this method is called but |
| 68 | * using any of the mutators will result in throwing |
| 69 | * a logic_error exception. |
| 70 | */ |
| 71 | void release(); |
| 72 | |
| 73 | /** |
| 74 | * @brief Get the name of the chip this request was made on. |
| 75 | * @return Name to the GPIO chip. |
| 76 | */ |
| 77 | ::std::string chip_name() const; |
| 78 | |
| 79 | /** |
| 80 | * @brief Get the number of requested lines. |
| 81 | * @return Number of lines in this request. |
| 82 | */ |
| 83 | ::std::size_t num_lines() const; |
| 84 | |
| 85 | /** |
| 86 | * @brief Get the list of offsets of requested lines. |
| 87 | * @return List of hardware offsets of the lines in this request. |
| 88 | */ |
| 89 | line::offsets offsets() const; |
| 90 | |
| 91 | /** |
| 92 | * @brief Get the value of a single requested line. |
| 93 | * @param offset Offset of the line to read within the chip. |
| 94 | * @return Current line value. |
| 95 | */ |
| 96 | line::value get_value(line::offset offset); |
| 97 | |
| 98 | /** |
| 99 | * @brief Get the values of a subset of requested lines. |
| 100 | * @param offsets Vector of line offsets |
| 101 | * @return Vector of lines values with indexes of values corresponding |
| 102 | * to those of the offsets. |
| 103 | */ |
| 104 | line::values get_values(const line::offsets& offsets); |
| 105 | |
| 106 | /** |
| 107 | * @brief Get the values of all requested lines. |
| 108 | * @return List of read values. |
| 109 | */ |
| 110 | line::values get_values(); |
| 111 | |
| 112 | /** |
| 113 | * @brief Get the values of a subset of requested lines into a vector |
| 114 | * supplied by the caller. |
| 115 | * @param offsets Vector of line offsets. |
| 116 | * @param values Vector for storing the values. Its size must be at |
| 117 | * least that of the offsets vector. The indexes of read |
| 118 | * values will correspond with those in the offsets |
| 119 | * vector. |
| 120 | */ |
| 121 | void get_values(const line::offsets& offsets, line::values& values); |
| 122 | |
| 123 | /** |
| 124 | * @brief Get the values of all requested lines. |
| 125 | * @param values Array in which the values will be stored. Must hold |
| 126 | * at least the number of elements returned by |
| 127 | * line_request::num_lines. |
| 128 | */ |
| 129 | void get_values(line::values& values); |
| 130 | |
| 131 | /** |
| 132 | * @brief Set the value of a single requested line. |
| 133 | * @param offset Offset of the line to set within the chip. |
| 134 | * @param value New line value. |
| 135 | * @return Reference to self. |
| 136 | */ |
| 137 | line_request& set_value(line::offset offset, line::value value); |
| 138 | |
| 139 | /** |
| 140 | * @brief Set the values of a subset of requested lines. |
| 141 | * @param values Vector containing a set of offset->value mappings. |
| 142 | * @return Reference to self. |
| 143 | */ |
| 144 | line_request& set_values(const line::value_mappings& values); |
| 145 | |
| 146 | /** |
| 147 | * @brief Set the values of a subset of requested lines. |
| 148 | * @param offsets Vector containing the offsets of lines to set. |
| 149 | * @param values Vector containing new values with indexes |
| 150 | * corresponding with those in the offsets vector. |
| 151 | * @return Reference to self. |
| 152 | */ |
| 153 | line_request& set_values(const line::offsets& offsets, const line::values& values); |
| 154 | |
| 155 | /** |
| 156 | * @brief Set the values of all requested lines. |
| 157 | * @param values Array of new line values. The size must be equal to |
| 158 | * the value returned by line_request::num_lines. |
| 159 | * @return Reference to self. |
| 160 | */ |
| 161 | line_request& set_values(const line::values& values); |
| 162 | |
| 163 | /** |
| 164 | * @brief Apply new config options to requested lines. |
| 165 | * @param config New configuration. |
| 166 | * @return Reference to self. |
| 167 | */ |
| 168 | line_request& reconfigure_lines(const line_config& config); |
| 169 | |
| 170 | /** |
| 171 | * @brief Get the file descriptor number associated with this line |
| 172 | * request. |
| 173 | * @return File descriptor number. |
| 174 | */ |
| 175 | int fd() const; |
| 176 | |
| 177 | /** |
| 178 | * @brief Wait for edge events on any of the lines requested with edge |
| 179 | * detection enabled. |
| 180 | * @param timeout Wait time limit in nanoseconds. If set to 0, the |
| 181 | * function returns immediately. If set to a negative |
| 182 | * number, the function blocks indefinitely until an |
| 183 | * event becomes available. |
| 184 | * @return True if at least one event is ready to be read. False if the |
| 185 | * wait timed out. |
| 186 | */ |
| 187 | bool wait_edge_events(const ::std::chrono::nanoseconds& timeout) const; |
| 188 | |
| 189 | /** |
| 190 | * @brief Read a number of edge events from this request up to the |
| 191 | * maximum capacity of the buffer. |
| 192 | * @param buffer Edge event buffer to read events into. |
| 193 | * @return Number of events read. |
| 194 | */ |
| 195 | ::std::size_t read_edge_events(edge_event_buffer& buffer); |
| 196 | |
| 197 | /** |
| 198 | * @brief Read a number of edge events from this request. |
| 199 | * @param buffer Edge event buffer to read events into. |
| 200 | * @param max_events Maximum number of events to read. Limited by the |
| 201 | * capacity of the buffer. |
| 202 | * @return Number of events read. |
| 203 | */ |
| 204 | ::std::size_t read_edge_events(edge_event_buffer& buffer, ::std::size_t max_events); |
| 205 | |
| 206 | private: |
| 207 | |
| 208 | line_request(); |
| 209 | |
| 210 | struct impl; |
| 211 | |
| 212 | ::std::unique_ptr<impl> _m_priv; |
| 213 | |
| 214 | friend request_builder; |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * @brief Stream insertion operator for line requests. |
| 219 | * @param out Output stream to write to. |
| 220 | * @param request Line request object to insert into the output stream. |
| 221 | * @return Reference to out. |
| 222 | */ |
| 223 | ::std::ostream& operator<<(::std::ostream& out, const line_request& request); |
| 224 | |
| 225 | } /* namespace gpiod */ |
| 226 | |
| 227 | #endif /* __LIBGPIOD_CXX_LINE_REQUEST_HPP__ */ |