| 1 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 | // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <ostream> |
| 5 | #include <utility> |
| 6 | |
| 7 | #include "internal.hpp" |
| 8 | |
| 9 | namespace gpiod { |
| 10 | |
| 11 | struct request_builder::impl |
| 12 | { |
| 13 | impl(chip& parent) |
| 14 | : line_cfg(), |
| 15 | req_cfg(), |
| 16 | parent(parent) |
| 17 | { |
| 18 | |
| 19 | } |
| 20 | |
| 21 | impl(const impl& other) = delete; |
| 22 | impl(impl&& other) = delete; |
| 23 | impl& operator=(const impl& other) = delete; |
| 24 | impl& operator=(impl&& other) = delete; |
| 25 | |
| 26 | line_config line_cfg; |
| 27 | request_config req_cfg; |
| 28 | chip parent; |
| 29 | }; |
| 30 | |
| 31 | GPIOD_CXX_API request_builder::request_builder(chip& chip) |
| 32 | : _m_priv(new impl(chip)) |
| 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | GPIOD_CXX_API request_builder::request_builder(request_builder&& other) noexcept |
| 38 | : _m_priv(::std::move(other._m_priv)) |
| 39 | { |
| 40 | |
| 41 | } |
| 42 | |
| 43 | GPIOD_CXX_API request_builder::~request_builder() |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | GPIOD_CXX_API request_builder& request_builder::operator=(request_builder&& other) noexcept |
| 49 | { |
| 50 | this->_m_priv = ::std::move(other._m_priv); |
| 51 | |
| 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | GPIOD_CXX_API request_builder& request_builder::set_request_config(request_config& req_cfg) |
| 56 | { |
| 57 | this->_m_priv->req_cfg = req_cfg; |
| 58 | |
| 59 | return *this; |
| 60 | } |
| 61 | |
| 62 | GPIOD_CXX_API const request_config& request_builder::get_request_config() const noexcept |
| 63 | { |
| 64 | return this->_m_priv->req_cfg; |
| 65 | } |
| 66 | |
| 67 | GPIOD_CXX_API request_builder& |
| 68 | request_builder::set_consumer(const ::std::string& consumer) noexcept |
| 69 | { |
| 70 | this->_m_priv->req_cfg.set_consumer(consumer); |
| 71 | |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | GPIOD_CXX_API request_builder& |
| 76 | request_builder::set_event_buffer_size(::std::size_t event_buffer_size) noexcept |
| 77 | { |
| 78 | this->_m_priv->req_cfg.set_event_buffer_size(event_buffer_size); |
| 79 | |
| 80 | return *this; |
| 81 | } |
| 82 | |
| 83 | GPIOD_CXX_API request_builder& request_builder::set_line_config(line_config &line_cfg) |
| 84 | { |
| 85 | this->_m_priv->line_cfg = line_cfg; |
| 86 | |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | GPIOD_CXX_API const line_config& request_builder::get_line_config() const noexcept |
| 91 | { |
| 92 | return this->_m_priv->line_cfg; |
| 93 | } |
| 94 | |
| 95 | GPIOD_CXX_API request_builder& |
| 96 | request_builder::add_line_settings(line::offset offset, const line_settings& settings) |
| 97 | { |
| 98 | return this->add_line_settings(line::offsets({offset}), settings); |
| 99 | } |
| 100 | |
| 101 | GPIOD_CXX_API request_builder& |
| 102 | request_builder::add_line_settings(const line::offsets& offsets, const line_settings& settings) |
| 103 | { |
| 104 | this->_m_priv->line_cfg.add_line_settings(offsets, settings); |
| 105 | |
| 106 | return *this; |
| 107 | } |
| 108 | |
| 109 | GPIOD_CXX_API request_builder& |
| 110 | request_builder::set_output_values(const line::values& values) |
| 111 | { |
| 112 | this->_m_priv->line_cfg.set_output_values(values); |
| 113 | |
| 114 | return *this; |
| 115 | } |
| 116 | |
| 117 | GPIOD_CXX_API line_request request_builder::do_request() |
| 118 | { |
| 119 | line_request_ptr request(::gpiod_chip_request_lines( |
| 120 | this->_m_priv->parent._m_priv->chip.get(), |
| 121 | this->_m_priv->req_cfg._m_priv->config.get(), |
| 122 | this->_m_priv->line_cfg._m_priv->config.get())); |
| 123 | if (!request) |
| 124 | throw_from_errno("error requesting GPIO lines"); |
| 125 | |
| 126 | line_request ret; |
| 127 | ret._m_priv.get()->set_request_ptr(request); |
| 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const request_builder& builder) |
| 133 | { |
| 134 | out << "gpiod::request_builder(request_config=" << builder._m_priv->req_cfg << |
| 135 | ", line_config=" << builder._m_priv->line_cfg << |
| 136 | ", parent=" << builder._m_priv->parent << |
| 137 | ")"; |
| 138 | |
| 139 | return out; |
| 140 | } |
| 141 | |
| 142 | } /* namespace gpiod */ |