| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | #ifndef __LIBGPIOD_CXX_INTERNAL_HPP__ |
| 5 | #define __LIBGPIOD_CXX_INTERNAL_HPP__ |
| 6 | |
| 7 | #include <gpiod.h> |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include "gpiod.hpp" |
| 15 | |
| 16 | namespace gpiod { |
| 17 | |
| 18 | template<class cxx_enum_type, class c_enum_type> |
| 19 | cxx_enum_type get_mapped_value(c_enum_type value, |
| 20 | const ::std::map<c_enum_type, cxx_enum_type>& mapping) |
| 21 | { |
| 22 | try { |
| 23 | return mapping.at(value); |
| 24 | } catch (const ::std::out_of_range& err) { |
| 25 | /* FIXME Demangle the name. */ |
| 26 | throw bad_mapping(::std::string("invalid value for ") + |
| 27 | typeid(cxx_enum_type).name()); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void throw_from_errno(const ::std::string& what); |
| 32 | ::gpiod_line_value map_output_value(line::value value); |
| 33 | |
| 34 | template<class T, void F(T*)> struct deleter |
| 35 | { |
| 36 | void operator()(T* ptr) |
| 37 | { |
| 38 | F(ptr); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | using chip_deleter = deleter<::gpiod_chip, ::gpiod_chip_close>; |
| 43 | using chip_info_deleter = deleter<::gpiod_chip_info, ::gpiod_chip_info_free>; |
| 44 | using line_info_deleter = deleter<::gpiod_line_info, ::gpiod_line_info_free>; |
| 45 | using info_event_deleter = deleter<::gpiod_info_event, ::gpiod_info_event_free>; |
| 46 | using line_settings_deleter = deleter<::gpiod_line_settings, ::gpiod_line_settings_free>; |
| 47 | using line_config_deleter = deleter<::gpiod_line_config, ::gpiod_line_config_free>; |
| 48 | using request_config_deleter = deleter<::gpiod_request_config, ::gpiod_request_config_free>; |
| 49 | using line_request_deleter = deleter<::gpiod_line_request, ::gpiod_line_request_release>; |
| 50 | using edge_event_deleter = deleter<::gpiod_edge_event, ::gpiod_edge_event_free>; |
| 51 | using edge_event_buffer_deleter = deleter<::gpiod_edge_event_buffer, |
| 52 | ::gpiod_edge_event_buffer_free>; |
| 53 | |
| 54 | using chip_ptr = ::std::unique_ptr<::gpiod_chip, chip_deleter>; |
| 55 | using chip_info_ptr = ::std::unique_ptr<::gpiod_chip_info, chip_info_deleter>; |
| 56 | using line_info_ptr = ::std::unique_ptr<::gpiod_line_info, line_info_deleter>; |
| 57 | using info_event_ptr = ::std::unique_ptr<::gpiod_info_event, info_event_deleter>; |
| 58 | using line_settings_ptr = ::std::unique_ptr<::gpiod_line_settings, line_settings_deleter>; |
| 59 | using line_config_ptr = ::std::unique_ptr<::gpiod_line_config, line_config_deleter>; |
| 60 | using request_config_ptr = ::std::unique_ptr<::gpiod_request_config, request_config_deleter>; |
| 61 | using line_request_ptr = ::std::unique_ptr<::gpiod_line_request, line_request_deleter>; |
| 62 | using edge_event_ptr = ::std::unique_ptr<::gpiod_edge_event, edge_event_deleter>; |
| 63 | using edge_event_buffer_ptr = ::std::unique_ptr<::gpiod_edge_event_buffer, |
| 64 | edge_event_buffer_deleter>; |
| 65 | |
| 66 | struct chip::impl |
| 67 | { |
| 68 | impl(const ::std::filesystem::path& path); |
| 69 | impl(const impl& other) = delete; |
| 70 | impl(impl&& other) = delete; |
| 71 | impl& operator=(const impl& other) = delete; |
| 72 | impl& operator=(impl&& other) = delete; |
| 73 | |
| 74 | void throw_if_closed() const; |
| 75 | |
| 76 | chip_ptr chip; |
| 77 | }; |
| 78 | |
| 79 | struct chip_info::impl |
| 80 | { |
| 81 | impl() = default; |
| 82 | impl(const impl& other) = delete; |
| 83 | impl(impl&& other) = delete; |
| 84 | impl& operator=(const impl& other) = delete; |
| 85 | impl& operator=(impl&& other) = delete; |
| 86 | |
| 87 | void set_info_ptr(chip_info_ptr& new_info); |
| 88 | |
| 89 | chip_info_ptr info; |
| 90 | }; |
| 91 | |
| 92 | struct line_info::impl |
| 93 | { |
| 94 | impl() = default; |
| 95 | impl(const impl& other) = delete; |
| 96 | impl(impl&& other) = delete; |
| 97 | impl& operator=(const impl& other) = delete; |
| 98 | impl& operator=(impl&& other) = delete; |
| 99 | |
| 100 | void set_info_ptr(line_info_ptr& new_info); |
| 101 | |
| 102 | line_info_ptr info; |
| 103 | }; |
| 104 | |
| 105 | struct info_event::impl |
| 106 | { |
| 107 | impl() = default; |
| 108 | impl(const impl& other) = delete; |
| 109 | impl(impl&& other) = delete; |
| 110 | impl& operator=(const impl& other) = delete; |
| 111 | impl& operator=(impl&& other) = delete; |
| 112 | |
| 113 | void set_info_event_ptr(info_event_ptr& new_event); |
| 114 | |
| 115 | info_event_ptr event; |
| 116 | line_info info; |
| 117 | }; |
| 118 | |
| 119 | struct line_settings::impl |
| 120 | { |
| 121 | impl(); |
| 122 | impl(const impl& other); |
| 123 | impl(impl&& other) = delete; |
| 124 | impl& operator=(const impl& other) = delete; |
| 125 | impl& operator=(impl&& other) = delete; |
| 126 | |
| 127 | line_settings_ptr settings; |
| 128 | }; |
| 129 | |
| 130 | struct line_config::impl |
| 131 | { |
| 132 | impl(); |
| 133 | impl(const impl& other) = delete; |
| 134 | impl(impl&& other) = delete; |
| 135 | impl& operator=(const impl& other) = delete; |
| 136 | impl& operator=(impl&& other) = delete; |
| 137 | |
| 138 | line_config_ptr config; |
| 139 | }; |
| 140 | |
| 141 | struct request_config::impl |
| 142 | { |
| 143 | impl(); |
| 144 | impl(const impl& other) = delete; |
| 145 | impl(impl&& other) = delete; |
| 146 | impl& operator=(const impl& other) = delete; |
| 147 | impl& operator=(impl&& other) = delete; |
| 148 | |
| 149 | request_config_ptr config; |
| 150 | }; |
| 151 | |
| 152 | struct line_request::impl |
| 153 | { |
| 154 | impl() = default; |
| 155 | impl(const impl& other) = delete; |
| 156 | impl(impl&& other) = delete; |
| 157 | impl& operator=(const impl& other) = delete; |
| 158 | impl& operator=(impl&& other) = delete; |
| 159 | |
| 160 | void throw_if_released() const; |
| 161 | void set_request_ptr(line_request_ptr& ptr); |
| 162 | void fill_offset_buf(const line::offsets& offsets); |
| 163 | |
| 164 | line_request_ptr request; |
| 165 | |
| 166 | /* |
| 167 | * Used when reading/setting the line values in order to avoid |
| 168 | * allocating a new buffer on every call. We're not doing it for |
| 169 | * offsets in the line & request config structures because they don't |
| 170 | * require high performance unlike the set/get value calls. |
| 171 | */ |
| 172 | ::std::vector<unsigned int> offset_buf; |
| 173 | }; |
| 174 | |
| 175 | struct edge_event::impl |
| 176 | { |
| 177 | impl() = default; |
| 178 | impl(const impl& other) = delete; |
| 179 | impl(impl&& other) = delete; |
| 180 | virtual ~impl() = default; |
| 181 | impl& operator=(const impl& other) = delete; |
| 182 | impl& operator=(impl&& other) = delete; |
| 183 | |
| 184 | virtual ::gpiod_edge_event* get_event_ptr() const noexcept = 0; |
| 185 | virtual ::std::shared_ptr<impl> copy(const ::std::shared_ptr<impl>& self) const = 0; |
| 186 | }; |
| 187 | |
| 188 | struct edge_event::impl_managed final : public edge_event::impl |
| 189 | { |
| 190 | impl_managed() = default; |
| 191 | impl_managed(const impl_managed& other) = delete; |
| 192 | impl_managed(impl_managed&& other) = delete; |
| 193 | ~impl_managed() = default; |
| 194 | impl_managed& operator=(const impl_managed& other) = delete; |
| 195 | impl_managed& operator=(impl_managed&& other) = delete; |
| 196 | |
| 197 | ::gpiod_edge_event* get_event_ptr() const noexcept override; |
| 198 | ::std::shared_ptr<impl> copy(const ::std::shared_ptr<impl>& self) const override; |
| 199 | |
| 200 | edge_event_ptr event; |
| 201 | }; |
| 202 | |
| 203 | struct edge_event::impl_external final : public edge_event::impl |
| 204 | { |
| 205 | impl_external(); |
| 206 | impl_external(const impl_external& other) = delete; |
| 207 | impl_external(impl_external&& other) = delete; |
| 208 | ~impl_external() = default; |
| 209 | impl_external& operator=(const impl_external& other) = delete; |
| 210 | impl_external& operator=(impl_external&& other) = delete; |
| 211 | |
| 212 | ::gpiod_edge_event* get_event_ptr() const noexcept override; |
| 213 | ::std::shared_ptr<impl> copy(const ::std::shared_ptr<impl>& self) const override; |
| 214 | |
| 215 | ::gpiod_edge_event *event; |
| 216 | }; |
| 217 | |
| 218 | struct edge_event_buffer::impl |
| 219 | { |
| 220 | impl(unsigned int capacity); |
| 221 | impl(const impl& other) = delete; |
| 222 | impl(impl&& other) = delete; |
| 223 | impl& operator=(const impl& other) = delete; |
| 224 | impl& operator=(impl&& other) = delete; |
| 225 | |
| 226 | int read_events(const line_request_ptr& request, unsigned int max_events); |
| 227 | |
| 228 | edge_event_buffer_ptr buffer; |
| 229 | ::std::vector<edge_event> events; |
| 230 | }; |
| 231 | |
| 232 | } /* namespace gpiod */ |
| 233 | |
| 234 | #endif /* __LIBGPIOD_CXX_INTERNAL_HPP__ */ |