| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | /** |
| 5 | * @file chip.hpp |
| 6 | */ |
| 7 | |
| 8 | #ifndef __LIBGPIOD_CXX_CHIP_HPP__ |
| 9 | #define __LIBGPIOD_CXX_CHIP_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 <filesystem> |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "line.hpp" |
| 22 | |
| 23 | namespace gpiod { |
| 24 | |
| 25 | class chip_info; |
| 26 | class info_event; |
| 27 | class line_config; |
| 28 | class line_info; |
| 29 | class line_request; |
| 30 | class request_builder; |
| 31 | class request_config; |
| 32 | |
| 33 | /** |
| 34 | * @brief Represents a GPIO chip. |
| 35 | */ |
| 36 | class chip final |
| 37 | { |
| 38 | public: |
| 39 | |
| 40 | /** |
| 41 | * @brief Instantiates a new chip object by opening the device file |
| 42 | * indicated by the \p path argument. |
| 43 | * @param path Path to the device file to open. |
| 44 | */ |
| 45 | explicit chip(const ::std::filesystem::path& path); |
| 46 | |
| 47 | /** |
| 48 | * @brief Move constructor. |
| 49 | * @param other Object to move. |
| 50 | */ |
| 51 | chip(chip&& other) noexcept; |
| 52 | |
| 53 | ~chip(); |
| 54 | |
| 55 | chip& operator=(const chip& other) = delete; |
| 56 | |
| 57 | /** |
| 58 | * @brief Move assignment operator. |
| 59 | * @param other Object to move. |
| 60 | * @return Reference to self. |
| 61 | */ |
| 62 | chip& operator=(chip&& other) noexcept; |
| 63 | |
| 64 | /** |
| 65 | * @brief Check if this object is valid. |
| 66 | * @return True if this object's methods can be used, false otherwise. |
| 67 | * False usually means the chip was closed. If the user calls |
| 68 | * any of the methods of this class on an object for which this |
| 69 | * operator returned false, a logic_error will be thrown. |
| 70 | */ |
| 71 | explicit operator bool() const noexcept; |
| 72 | |
| 73 | /** |
| 74 | * @brief Close the GPIO chip device file and free associated resources. |
| 75 | * @note The chip object can live after calling this method but any of |
| 76 | * the chip's mutators will throw a logic_error exception. |
| 77 | */ |
| 78 | void close(); |
| 79 | |
| 80 | /** |
| 81 | * @brief Get the filesystem path that was used to open this GPIO chip. |
| 82 | * @return Path to the underlying character device file. |
| 83 | */ |
| 84 | ::std::filesystem::path path() const; |
| 85 | |
| 86 | /** |
| 87 | * @brief Get information about the chip. |
| 88 | * @return New chip_info object. |
| 89 | */ |
| 90 | chip_info get_info() const; |
| 91 | |
| 92 | /** |
| 93 | * @brief Retrieve the current snapshot of line information for a |
| 94 | * single line. |
| 95 | * @param offset Offset of the line to get the info for. |
| 96 | * @return New ::gpiod::line_info object. |
| 97 | */ |
| 98 | line_info get_line_info(line::offset offset) const; |
| 99 | |
| 100 | /** |
| 101 | * @brief Wrapper around ::gpiod::chip::get_line_info that retrieves |
| 102 | * the line info and starts watching the line for changes. |
| 103 | * @param offset Offset of the line to get the info for. |
| 104 | * @return New ::gpiod::line_info object. |
| 105 | */ |
| 106 | line_info watch_line_info(line::offset offset) const; |
| 107 | |
| 108 | /** |
| 109 | * @brief Stop watching the line at given offset for info events. |
| 110 | * @param offset Offset of the line to get the info for. |
| 111 | */ |
| 112 | void unwatch_line_info(line::offset offset) const; |
| 113 | |
| 114 | /** |
| 115 | * @brief Get the file descriptor associated with this chip. |
| 116 | * @return File descriptor number. |
| 117 | */ |
| 118 | int fd() const; |
| 119 | |
| 120 | /** |
| 121 | * @brief Wait for line status events on any of the watched lines |
| 122 | * exposed by this chip. |
| 123 | * @param timeout Wait time limit in nanoseconds. If set to 0, the |
| 124 | * function returns immediately. If set to a negative |
| 125 | * number, the function blocks indefinitely until an |
| 126 | * event becomes available. |
| 127 | * @return True if at least one event is ready to be read. False if the |
| 128 | * wait timed out. |
| 129 | */ |
| 130 | bool wait_info_event(const ::std::chrono::nanoseconds& timeout) const; |
| 131 | |
| 132 | /** |
| 133 | * @brief Read a single line status change event from this chip. |
| 134 | * @return New info_event object. |
| 135 | */ |
| 136 | info_event read_info_event() const; |
| 137 | |
| 138 | /** |
| 139 | * @brief Map a GPIO line's name to its offset within the chip. |
| 140 | * @param name Name of the GPIO line to map. |
| 141 | * @return Offset of the line within the chip or -1 if the line with |
| 142 | * given name is not exposed by this chip. |
| 143 | */ |
| 144 | int get_line_offset_from_name(const ::std::string& name) const; |
| 145 | |
| 146 | /** |
| 147 | * @brief Create a request_builder associated with this chip. |
| 148 | * @return New request_builder object. |
| 149 | */ |
| 150 | request_builder prepare_request(); |
| 151 | |
| 152 | private: |
| 153 | |
| 154 | struct impl; |
| 155 | |
| 156 | ::std::shared_ptr<impl> _m_priv; |
| 157 | |
| 158 | chip(const chip& other); |
| 159 | |
| 160 | friend request_builder; |
| 161 | }; |
| 162 | |
| 163 | /** |
| 164 | * @brief Stream insertion operator for GPIO chip objects. |
| 165 | * @param out Output stream to write to. |
| 166 | * @param chip GPIO chip to insert into the output stream. |
| 167 | * @return Reference to out. |
| 168 | */ |
| 169 | ::std::ostream& operator<<(::std::ostream& out, const chip& chip); |
| 170 | |
| 171 | } /* namespace gpiod */ |
| 172 | |
| 173 | #endif /* __LIBGPIOD_CXX_CHIP_HPP__ */ |