| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <map> |
| 5 | #include <ostream> |
| 6 | |
| 7 | #include "internal.hpp" |
| 8 | |
| 9 | namespace gpiod { |
| 10 | |
| 11 | namespace { |
| 12 | |
| 13 | template<class cxx_enum_type, class c_enum_type> |
| 14 | ::std::map<c_enum_type, cxx_enum_type> |
| 15 | make_reverse_maping(const ::std::map<cxx_enum_type, c_enum_type>& mapping) |
| 16 | { |
| 17 | ::std::map<c_enum_type, cxx_enum_type> ret; |
| 18 | |
| 19 | for (const auto &item: mapping) |
| 20 | ret[item.second] = item.first; |
| 21 | |
| 22 | return ret; |
| 23 | } |
| 24 | |
| 25 | const ::std::map<line::direction, ::gpiod_line_direction> direction_mapping = { |
| 26 | { line::direction::AS_IS, GPIOD_LINE_DIRECTION_AS_IS }, |
| 27 | { line::direction::INPUT, GPIOD_LINE_DIRECTION_INPUT }, |
| 28 | { line::direction::OUTPUT, GPIOD_LINE_DIRECTION_OUTPUT }, |
| 29 | }; |
| 30 | |
| 31 | const ::std::map<::gpiod_line_direction, line::direction> |
| 32 | reverse_direction_mapping = make_reverse_maping(direction_mapping); |
| 33 | |
| 34 | const ::std::map<line::edge, ::gpiod_line_edge> edge_mapping = { |
| 35 | { line::edge::NONE, GPIOD_LINE_EDGE_NONE }, |
| 36 | { line::edge::FALLING, GPIOD_LINE_EDGE_FALLING }, |
| 37 | { line::edge::RISING, GPIOD_LINE_EDGE_RISING }, |
| 38 | { line::edge::BOTH, GPIOD_LINE_EDGE_BOTH }, |
| 39 | }; |
| 40 | |
| 41 | const ::std::map<::gpiod_line_edge, line::edge> |
| 42 | reverse_edge_mapping = make_reverse_maping(edge_mapping); |
| 43 | |
| 44 | const ::std::map<line::bias, ::gpiod_line_bias> bias_mapping = { |
| 45 | { line::bias::AS_IS, GPIOD_LINE_BIAS_AS_IS }, |
| 46 | { line::bias::DISABLED, GPIOD_LINE_BIAS_DISABLED }, |
| 47 | { line::bias::PULL_UP, GPIOD_LINE_BIAS_PULL_UP }, |
| 48 | { line::bias::PULL_DOWN, GPIOD_LINE_BIAS_PULL_DOWN }, |
| 49 | }; |
| 50 | |
| 51 | const ::std::map<::gpiod_line_bias, line::bias> |
| 52 | reverse_bias_mapping = make_reverse_maping(bias_mapping); |
| 53 | |
| 54 | const ::std::map<line::drive, ::gpiod_line_drive> drive_mapping = { |
| 55 | { line::drive::PUSH_PULL, GPIOD_LINE_DRIVE_PUSH_PULL }, |
| 56 | { line::drive::OPEN_DRAIN, GPIOD_LINE_DRIVE_OPEN_DRAIN }, |
| 57 | { line::drive::OPEN_SOURCE, GPIOD_LINE_DRIVE_OPEN_SOURCE }, |
| 58 | }; |
| 59 | |
| 60 | const ::std::map<::gpiod_line_drive, line::drive> |
| 61 | reverse_drive_mapping = make_reverse_maping(drive_mapping); |
| 62 | |
| 63 | const ::std::map<line::clock, ::gpiod_line_clock> clock_mapping = { |
| 64 | { line::clock::MONOTONIC, GPIOD_LINE_CLOCK_MONOTONIC }, |
| 65 | { line::clock::REALTIME, GPIOD_LINE_CLOCK_REALTIME }, |
| 66 | { line::clock::HTE, GPIOD_LINE_CLOCK_HTE }, |
| 67 | }; |
| 68 | |
| 69 | const ::std::map<::gpiod_line_clock, line::clock> |
| 70 | reverse_clock_mapping = make_reverse_maping(clock_mapping); |
| 71 | |
| 72 | const ::std::map<line::value, ::gpiod_line_value> value_mapping = { |
| 73 | { line::value::INACTIVE, GPIOD_LINE_VALUE_INACTIVE }, |
| 74 | { line::value::ACTIVE, GPIOD_LINE_VALUE_ACTIVE }, |
| 75 | }; |
| 76 | |
| 77 | const ::std::map<::gpiod_line_value, line::value> |
| 78 | reverse_value_mapping = make_reverse_maping(value_mapping); |
| 79 | |
| 80 | line_settings_ptr make_line_settings() |
| 81 | { |
| 82 | line_settings_ptr settings(::gpiod_line_settings_new()); |
| 83 | if (!settings) |
| 84 | throw_from_errno("Unable to allocate the line settings object"); |
| 85 | |
| 86 | return settings; |
| 87 | } |
| 88 | |
| 89 | line_settings_ptr copy_line_settings(const line_settings_ptr& ptr) |
| 90 | { |
| 91 | line_settings_ptr settings(::gpiod_line_settings_copy(ptr.get())); |
| 92 | if (!settings) |
| 93 | throw_from_errno("Unable to copy the line settings object"); |
| 94 | |
| 95 | return settings; |
| 96 | } |
| 97 | |
| 98 | template<class cxx_enum_type, class c_enum_type> |
| 99 | c_enum_type do_map_value(cxx_enum_type value, const ::std::map<cxx_enum_type, c_enum_type>& mapping) |
| 100 | try { |
| 101 | return get_mapped_value(value, mapping); |
| 102 | } catch (const bad_mapping& ex) { |
| 103 | throw ::std::invalid_argument(ex.what()); |
| 104 | } |
| 105 | |
| 106 | template<class cxx_enum_type, class c_enum_type, int set_func(::gpiod_line_settings*, c_enum_type)> |
| 107 | void set_mapped_prop(::gpiod_line_settings* settings, cxx_enum_type value, |
| 108 | const ::std::map<cxx_enum_type, c_enum_type>& mapping) |
| 109 | { |
| 110 | c_enum_type mapped_val = do_map_value(value, mapping); |
| 111 | |
| 112 | auto ret = set_func(settings, mapped_val); |
| 113 | if (ret) |
| 114 | throw_from_errno("unable to set property"); |
| 115 | } |
| 116 | |
| 117 | template<class cxx_enum_type, class c_enum_type, c_enum_type get_func(::gpiod_line_settings*)> |
| 118 | cxx_enum_type get_mapped_prop(::gpiod_line_settings* settings, |
| 119 | const ::std::map<c_enum_type, cxx_enum_type>& mapping) |
| 120 | { |
| 121 | auto mapped_val = get_func(settings); |
| 122 | |
| 123 | return get_mapped_value(mapped_val, mapping); |
| 124 | } |
| 125 | |
| 126 | } /* namespace */ |
| 127 | |
| 128 | ::gpiod_line_value map_output_value(line::value value) |
| 129 | { |
| 130 | return do_map_value(value, value_mapping); |
| 131 | } |
| 132 | |
| 133 | line_settings::impl::impl() |
| 134 | : settings(make_line_settings()) |
| 135 | { |
| 136 | |
| 137 | } |
| 138 | |
| 139 | line_settings::impl::impl(const impl& other) |
| 140 | : settings(copy_line_settings(other.settings)) |
| 141 | { |
| 142 | |
| 143 | } |
| 144 | |
| 145 | GPIOD_CXX_API line_settings::line_settings() |
| 146 | : _m_priv(new impl) |
| 147 | { |
| 148 | |
| 149 | } |
| 150 | |
| 151 | GPIOD_CXX_API line_settings::line_settings(const line_settings& other) |
| 152 | : _m_priv(new impl(*other._m_priv)) |
| 153 | { |
| 154 | |
| 155 | } |
| 156 | |
| 157 | GPIOD_CXX_API line_settings::line_settings(line_settings&& other) noexcept |
| 158 | : _m_priv(::std::move(other._m_priv)) |
| 159 | { |
| 160 | |
| 161 | } |
| 162 | |
| 163 | GPIOD_CXX_API line_settings::~line_settings() |
| 164 | { |
| 165 | |
| 166 | } |
| 167 | |
| 168 | GPIOD_CXX_API line_settings& line_settings::operator=(const line_settings& other) |
| 169 | { |
| 170 | this->_m_priv.reset(new impl(*other._m_priv)); |
| 171 | |
| 172 | return *this; |
| 173 | } |
| 174 | |
| 175 | GPIOD_CXX_API line_settings& line_settings::operator=(line_settings&& other) |
| 176 | { |
| 177 | this->_m_priv = ::std::move(other._m_priv); |
| 178 | |
| 179 | return *this; |
| 180 | } |
| 181 | |
| 182 | GPIOD_CXX_API line_settings& line_settings::reset() noexcept |
| 183 | { |
| 184 | ::gpiod_line_settings_reset(this->_m_priv->settings.get()); |
| 185 | |
| 186 | return *this; |
| 187 | } |
| 188 | |
| 189 | GPIOD_CXX_API line_settings& line_settings::set_direction(line::direction direction) |
| 190 | { |
| 191 | set_mapped_prop<line::direction, ::gpiod_line_direction, |
| 192 | ::gpiod_line_settings_set_direction>(this->_m_priv->settings.get(), |
| 193 | direction, direction_mapping); |
| 194 | |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | GPIOD_CXX_API line::direction line_settings::direction() const |
| 199 | { |
| 200 | return get_mapped_prop<line::direction, ::gpiod_line_direction, |
| 201 | ::gpiod_line_settings_get_direction>( |
| 202 | this->_m_priv->settings.get(), |
| 203 | reverse_direction_mapping); |
| 204 | } |
| 205 | |
| 206 | GPIOD_CXX_API line_settings& line_settings::set_edge_detection(line::edge edge) |
| 207 | { |
| 208 | set_mapped_prop<line::edge, ::gpiod_line_edge, |
| 209 | ::gpiod_line_settings_set_edge_detection>(this->_m_priv->settings.get(), |
| 210 | edge, edge_mapping); |
| 211 | |
| 212 | return *this; |
| 213 | } |
| 214 | |
| 215 | GPIOD_CXX_API line::edge line_settings::edge_detection() const |
| 216 | { |
| 217 | return get_mapped_prop<line::edge, ::gpiod_line_edge, |
| 218 | ::gpiod_line_settings_get_edge_detection>( |
| 219 | this->_m_priv->settings.get(), |
| 220 | reverse_edge_mapping); |
| 221 | } |
| 222 | |
| 223 | GPIOD_CXX_API line_settings& line_settings::set_bias(line::bias bias) |
| 224 | { |
| 225 | set_mapped_prop<line::bias, ::gpiod_line_bias, |
| 226 | ::gpiod_line_settings_set_bias>(this->_m_priv->settings.get(), |
| 227 | bias, bias_mapping); |
| 228 | |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | GPIOD_CXX_API line::bias line_settings::bias() const |
| 233 | { |
| 234 | return get_mapped_prop<line::bias, ::gpiod_line_bias, |
| 235 | ::gpiod_line_settings_get_bias>(this->_m_priv->settings.get(), |
| 236 | reverse_bias_mapping); |
| 237 | } |
| 238 | |
| 239 | GPIOD_CXX_API line_settings& line_settings::set_drive(line::drive drive) |
| 240 | { |
| 241 | set_mapped_prop<line::drive, ::gpiod_line_drive, |
| 242 | ::gpiod_line_settings_set_drive>(this->_m_priv->settings.get(), |
| 243 | drive, drive_mapping); |
| 244 | |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | GPIOD_CXX_API line::drive line_settings::drive() const |
| 249 | { |
| 250 | return get_mapped_prop<line::drive, ::gpiod_line_drive, |
| 251 | ::gpiod_line_settings_get_drive>(this->_m_priv->settings.get(), |
| 252 | reverse_drive_mapping); |
| 253 | } |
| 254 | |
| 255 | GPIOD_CXX_API line_settings& line_settings::set_active_low(bool active_low) |
| 256 | { |
| 257 | ::gpiod_line_settings_set_active_low(this->_m_priv->settings.get(), active_low); |
| 258 | |
| 259 | return *this; |
| 260 | } |
| 261 | |
| 262 | GPIOD_CXX_API bool line_settings::active_low() const noexcept |
| 263 | { |
| 264 | return ::gpiod_line_settings_get_active_low(this->_m_priv->settings.get()); |
| 265 | } |
| 266 | |
| 267 | GPIOD_CXX_API line_settings& |
| 268 | line_settings::set_debounce_period(const ::std::chrono::microseconds& period) |
| 269 | { |
| 270 | ::gpiod_line_settings_set_debounce_period_us(this->_m_priv->settings.get(), period.count()); |
| 271 | |
| 272 | return *this; |
| 273 | } |
| 274 | |
| 275 | GPIOD_CXX_API ::std::chrono::microseconds line_settings::debounce_period() const noexcept |
| 276 | { |
| 277 | return ::std::chrono::microseconds( |
| 278 | ::gpiod_line_settings_get_debounce_period_us(this->_m_priv->settings.get())); |
| 279 | } |
| 280 | |
| 281 | GPIOD_CXX_API line_settings& line_settings::set_event_clock(line::clock event_clock) |
| 282 | { |
| 283 | set_mapped_prop<line::clock, ::gpiod_line_clock, |
| 284 | ::gpiod_line_settings_set_event_clock>(this->_m_priv->settings.get(), |
| 285 | event_clock, clock_mapping); |
| 286 | |
| 287 | return *this; |
| 288 | } |
| 289 | |
| 290 | GPIOD_CXX_API line::clock line_settings::event_clock() const |
| 291 | { |
| 292 | return get_mapped_prop<line::clock, ::gpiod_line_clock, |
| 293 | ::gpiod_line_settings_get_event_clock>( |
| 294 | this->_m_priv->settings.get(), |
| 295 | reverse_clock_mapping); |
| 296 | } |
| 297 | |
| 298 | GPIOD_CXX_API line_settings& line_settings::set_output_value(line::value value) |
| 299 | { |
| 300 | set_mapped_prop<line::value, ::gpiod_line_value, |
| 301 | ::gpiod_line_settings_set_output_value>(this->_m_priv->settings.get(), |
| 302 | value, value_mapping); |
| 303 | |
| 304 | return *this; |
| 305 | } |
| 306 | |
| 307 | GPIOD_CXX_API line::value line_settings::output_value() const |
| 308 | { |
| 309 | return get_mapped_prop<line::value, ::gpiod_line_value, |
| 310 | ::gpiod_line_settings_get_output_value>( |
| 311 | this->_m_priv->settings.get(), |
| 312 | reverse_value_mapping); |
| 313 | } |
| 314 | |
| 315 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line_settings& settings) |
| 316 | { |
| 317 | out << "gpiod::line_settings(direction=" << settings.direction() << |
| 318 | ", edge_detection=" << settings.edge_detection() << |
| 319 | ", bias=" << settings.bias() << |
| 320 | ", drive=" << settings.drive() << |
| 321 | ", " << (settings.active_low() ? "active-low" : "active-high") << |
| 322 | ", debounce_period=" << settings.debounce_period().count() << |
| 323 | ", event_clock=" << settings.event_clock() << |
| 324 | ", output_value=" << settings.output_value() << |
| 325 | ")"; |
| 326 | |
| 327 | return out; |
| 328 | } |
| 329 | |
| 330 | } /* namespace gpiod */ |