| 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 | /* SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> */ |
| 3 | |
| 4 | #include <map> |
| 5 | #include <system_error> |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "gpiosim.h" |
| 9 | #include "gpiosim.hpp" |
| 10 | |
| 11 | namespace gpiosim { |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | const ::std::map<chip::pull, gpiosim_pull> pull_mapping = { |
| 16 | { chip::pull::PULL_UP, GPIOSIM_PULL_UP }, |
| 17 | { chip::pull::PULL_DOWN, GPIOSIM_PULL_DOWN }, |
| 18 | }; |
| 19 | |
| 20 | const ::std::map<chip_builder::direction, gpiosim_direction> hog_dir_mapping = { |
| 21 | { chip_builder::direction::INPUT, GPIOSIM_DIRECTION_INPUT }, |
| 22 | { chip_builder::direction::OUTPUT_HIGH, GPIOSIM_DIRECTION_OUTPUT_HIGH }, |
| 23 | { chip_builder::direction::OUTPUT_LOW, GPIOSIM_DIRECTION_OUTPUT_LOW }, |
| 24 | }; |
| 25 | |
| 26 | const ::std::map<gpiosim_value, chip::value> value_mapping = { |
| 27 | { GPIOSIM_VALUE_INACTIVE, chip::value::INACTIVE }, |
| 28 | { GPIOSIM_VALUE_ACTIVE, chip::value::ACTIVE }, |
| 29 | }; |
| 30 | |
| 31 | template<class gpiosim_type, void free_func(gpiosim_type*)> struct deleter |
| 32 | { |
| 33 | void operator()(gpiosim_type* ptr) |
| 34 | { |
| 35 | free_func(ptr); |
| 36 | } |
| 37 | }; |
| 38 | |
| 39 | using ctx_deleter = deleter<::gpiosim_ctx, ::gpiosim_ctx_unref>; |
| 40 | using dev_deleter = deleter<::gpiosim_dev, ::gpiosim_dev_unref>; |
| 41 | using bank_deleter = deleter<::gpiosim_bank, ::gpiosim_bank_unref>; |
| 42 | |
| 43 | using ctx_ptr = ::std::unique_ptr<::gpiosim_ctx, ctx_deleter>; |
| 44 | using dev_ptr = ::std::unique_ptr<::gpiosim_dev, dev_deleter>; |
| 45 | using bank_ptr = ::std::unique_ptr<::gpiosim_bank, bank_deleter>; |
| 46 | |
| 47 | ctx_ptr sim_ctx; |
| 48 | |
| 49 | class sim_ctx_initializer |
| 50 | { |
| 51 | public: |
| 52 | sim_ctx_initializer() |
| 53 | { |
| 54 | sim_ctx.reset(gpiosim_ctx_new()); |
| 55 | if (!sim_ctx) |
| 56 | throw ::std::system_error(errno, ::std::system_category(), |
| 57 | "unable to create the GPIO simulator context"); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | dev_ptr make_sim_dev() |
| 62 | { |
| 63 | static sim_ctx_initializer ctx_initializer; |
| 64 | |
| 65 | dev_ptr dev(::gpiosim_dev_new(sim_ctx.get())); |
| 66 | if (!dev) |
| 67 | throw ::std::system_error(errno, ::std::system_category(), |
| 68 | "failed to create a new GPIO simulator device"); |
| 69 | |
| 70 | return dev; |
| 71 | } |
| 72 | |
| 73 | bank_ptr make_sim_bank(const dev_ptr& dev) |
| 74 | { |
| 75 | bank_ptr bank(::gpiosim_bank_new(dev.get())); |
| 76 | if (!bank) |
| 77 | throw ::std::system_error(errno, ::std::system_category(), |
| 78 | "failed to create a new GPIO simulator bank"); |
| 79 | |
| 80 | return bank; |
| 81 | } |
| 82 | |
| 83 | } /* namespace */ |
| 84 | |
| 85 | struct chip::impl |
| 86 | { |
| 87 | impl() |
| 88 | : dev(make_sim_dev()), |
| 89 | bank(make_sim_bank(this->dev)) |
| 90 | { |
| 91 | |
| 92 | } |
| 93 | |
| 94 | impl(const impl& other) = delete; |
| 95 | impl(impl&& other) = delete; |
| 96 | ~impl() = default; |
| 97 | impl& operator=(const impl& other) = delete; |
| 98 | impl& operator=(impl&& other) = delete; |
| 99 | |
| 100 | dev_ptr dev; |
| 101 | bank_ptr bank; |
| 102 | }; |
| 103 | |
| 104 | chip::chip() |
| 105 | : _m_priv(new impl) |
| 106 | { |
| 107 | |
| 108 | } |
| 109 | |
| 110 | chip::chip(chip&& other) |
| 111 | : _m_priv(::std::move(other._m_priv)) |
| 112 | { |
| 113 | |
| 114 | } |
| 115 | |
| 116 | chip::~chip() |
| 117 | { |
| 118 | ::gpiosim_dev_disable(this->_m_priv->dev.get()); |
| 119 | } |
| 120 | |
| 121 | chip& chip::operator=(chip&& other) |
| 122 | { |
| 123 | this->_m_priv = ::std::move(other._m_priv); |
| 124 | |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | ::std::filesystem::path chip::dev_path() const |
| 129 | { |
| 130 | return ::gpiosim_bank_get_dev_path(this->_m_priv->bank.get()); |
| 131 | } |
| 132 | |
| 133 | ::std::string chip::name() const |
| 134 | { |
| 135 | return ::gpiosim_bank_get_chip_name(this->_m_priv->bank.get()); |
| 136 | } |
| 137 | |
| 138 | chip::value chip::get_value(unsigned int offset) |
| 139 | { |
| 140 | auto val = ::gpiosim_bank_get_value(this->_m_priv->bank.get(), offset); |
| 141 | if (val == GPIOSIM_VALUE_ERROR) |
| 142 | throw ::std::system_error(errno, ::std::system_category(), |
| 143 | "failed to read the simulated GPIO line value"); |
| 144 | |
| 145 | return value_mapping.at(val); |
| 146 | } |
| 147 | |
| 148 | void chip::set_pull(unsigned int offset, pull pull) |
| 149 | { |
| 150 | auto ret = ::gpiosim_bank_set_pull(this->_m_priv->bank.get(), |
| 151 | offset, pull_mapping.at(pull)); |
| 152 | if (ret) |
| 153 | throw ::std::system_error(errno, ::std::system_category(), |
| 154 | "failed to set the pull of simulated GPIO line"); |
| 155 | } |
| 156 | |
| 157 | struct chip_builder::impl |
| 158 | { |
| 159 | impl() |
| 160 | : num_lines(0), |
| 161 | label(), |
| 162 | line_names(), |
| 163 | hogs() |
| 164 | { |
| 165 | |
| 166 | } |
| 167 | |
| 168 | ::std::size_t num_lines; |
| 169 | ::std::string label; |
| 170 | ::std::map<unsigned int, ::std::string> line_names; |
| 171 | ::std::map<unsigned int, ::std::pair<::std::string, direction>> hogs; |
| 172 | }; |
| 173 | |
| 174 | chip_builder::chip_builder() |
| 175 | : _m_priv(new impl) |
| 176 | { |
| 177 | |
| 178 | } |
| 179 | |
| 180 | chip_builder::chip_builder(chip_builder&& other) |
| 181 | : _m_priv(::std::move(other._m_priv)) |
| 182 | { |
| 183 | |
| 184 | } |
| 185 | |
| 186 | chip_builder::~chip_builder() |
| 187 | { |
| 188 | |
| 189 | } |
| 190 | |
| 191 | chip_builder& chip_builder::operator=(chip_builder&& other) |
| 192 | { |
| 193 | this->_m_priv = ::std::move(other._m_priv); |
| 194 | |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | chip_builder& chip_builder::set_num_lines(::std::size_t num_lines) |
| 199 | { |
| 200 | this->_m_priv->num_lines = num_lines; |
| 201 | |
| 202 | return *this; |
| 203 | } |
| 204 | |
| 205 | chip_builder& chip_builder::set_label(const ::std::string& label) |
| 206 | { |
| 207 | this->_m_priv->label = label; |
| 208 | |
| 209 | return *this; |
| 210 | } |
| 211 | |
| 212 | chip_builder& chip_builder::set_line_name(unsigned int offset, const ::std::string& name) |
| 213 | { |
| 214 | this->_m_priv->line_names[offset] = name; |
| 215 | |
| 216 | return *this; |
| 217 | } |
| 218 | |
| 219 | chip_builder& chip_builder::set_hog(unsigned int offset, const ::std::string& name, direction direction) |
| 220 | { |
| 221 | this->_m_priv->hogs[offset] = { name, direction }; |
| 222 | |
| 223 | return *this; |
| 224 | } |
| 225 | |
| 226 | chip chip_builder::build() |
| 227 | { |
| 228 | chip sim; |
| 229 | int ret; |
| 230 | |
| 231 | if (this->_m_priv->num_lines) { |
| 232 | ret = ::gpiosim_bank_set_num_lines(sim._m_priv->bank.get(), |
| 233 | this->_m_priv->num_lines); |
| 234 | if (ret) |
| 235 | throw ::std::system_error(errno, ::std::system_category(), |
| 236 | "failed to set number of lines"); |
| 237 | } |
| 238 | |
| 239 | if (!this->_m_priv->label.empty()) { |
| 240 | ret = ::gpiosim_bank_set_label(sim._m_priv->bank.get(), |
| 241 | this->_m_priv->label.c_str()); |
| 242 | if (ret) |
| 243 | throw ::std::system_error(errno, ::std::system_category(), |
| 244 | "failed to set the chip label"); |
| 245 | } |
| 246 | |
| 247 | for (const auto& name: this->_m_priv->line_names) { |
| 248 | ret = ::gpiosim_bank_set_line_name(sim._m_priv->bank.get(), |
| 249 | name.first, name.second.c_str()); |
| 250 | if (ret) |
| 251 | throw ::std::system_error(errno, ::std::system_category(), |
| 252 | "failed to set the line name"); |
| 253 | } |
| 254 | |
| 255 | for (const auto& hog: this->_m_priv->hogs) { |
| 256 | ret = ::gpiosim_bank_hog_line(sim._m_priv->bank.get(), hog.first, |
| 257 | hog.second.first.c_str(), |
| 258 | hog_dir_mapping.at(hog.second.second)); |
| 259 | if (ret) |
| 260 | throw ::std::system_error(errno, ::std::system_category(), |
| 261 | "failed to hog the line"); |
| 262 | } |
| 263 | |
| 264 | ret = ::gpiosim_dev_enable(sim._m_priv->dev.get()); |
| 265 | if (ret) |
| 266 | throw ::std::system_error(errno, ::std::system_category(), |
| 267 | "failed to enable the simulated GPIO device"); |
| 268 | |
| 269 | return sim; |
| 270 | } |
| 271 | |
| 272 | chip_builder make_sim() |
| 273 | { |
| 274 | return chip_builder(); |
| 275 | } |
| 276 | |
| 277 | } /* namespace gpiosim */ |