Debian Codegraph @

bindings/cxx/line-config.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]>
3
4 #include <cstdlib>
5 #include <iterator>
6 #include <ostream>
7 #include <sstream>
8 #include <utility>
9 #include <vector>
10
11 #include "internal.hpp"
12
13 namespace gpiod {
14
15 namespace {
16
17 line_config_ptr make_line_config()
18 {
19 line_config_ptr config(::gpiod_line_config_new());
20 if (!config)
21 throw_from_errno("Unable to allocate the line config object");
22
23 return config;
24 }
25
26 } /* namespace */
27
28 line_config::impl::impl()
29 : config(make_line_config())
30 {
31
32 }
33
34 GPIOD_CXX_API line_config::line_config()
35 : _m_priv(new impl)
36 {
37
38 }
39
40 GPIOD_CXX_API line_config::line_config(line_config&& other) noexcept
41 : _m_priv(::std::move(other._m_priv))
42 {
43
44 }
45
46 GPIOD_CXX_API line_config::~line_config()
47 {
48
49 }
50
51 line_config& line_config::operator=(const line_config& other)
52 {
53 this->_m_priv = other._m_priv;
54
55 return *this;
56 }
57
58 GPIOD_CXX_API line_config& line_config::operator=(line_config&& other) noexcept
59 {
60 this->_m_priv = ::std::move(other._m_priv);
61
62 return *this;
63 }
64
65 GPIOD_CXX_API line_config& line_config::reset() noexcept
66 {
67 ::gpiod_line_config_reset(this->_m_priv->config.get());
68
69 return *this;
70 }
71
72 GPIOD_CXX_API line_config& line_config::add_line_settings(line::offset offset,
73 const line_settings& settings)
74 {
75 return this->add_line_settings(line::offsets({offset}), settings);
76 }
77
78 GPIOD_CXX_API line_config& line_config::add_line_settings(const line::offsets& offsets,
79 const line_settings& settings)
80 {
81 ::std::vector<unsigned int> raw_offsets(offsets.size());
82
83 for (unsigned int i = 0; i < offsets.size(); i++)
84 raw_offsets[i] = offsets[i];
85
86 auto ret = ::gpiod_line_config_add_line_settings(this->_m_priv->config.get(),
87 raw_offsets.data(), raw_offsets.size(),
88 settings._m_priv->settings.get());
89 if (ret)
90 throw_from_errno("unable to add line settings");
91
92 return *this;
93 }
94
95 GPIOD_CXX_API line_config& line_config::set_output_values(const line::values& values)
96 {
97 ::std::vector<::gpiod_line_value> mapped_values(values.size());
98
99 for (unsigned int i = 0; i < values.size(); i++)
100 mapped_values[i] = map_output_value(values[i]);
101
102 auto ret = ::gpiod_line_config_set_output_values(this->_m_priv->config.get(),
103 mapped_values.data(), mapped_values.size());
104 if (ret)
105 throw_from_errno("unable to set output values");
106
107 return *this;
108 }
109
110 GPIOD_CXX_API ::std::map<line::offset, line_settings> line_config::get_line_settings() const
111 {
112 ::std::size_t num_offsets = ::gpiod_line_config_get_num_configured_offsets(
113 this->_m_priv->config.get());
114 ::std::map<line::offset, line_settings> settings_map;
115 ::std::vector<unsigned int> offsets(num_offsets);
116
117 if (num_offsets == 0)
118 return settings_map;
119
120 ::gpiod_line_config_get_configured_offsets(this->_m_priv->config.get(),
121 offsets.data(), num_offsets);
122
123 for (size_t i = 0; i < num_offsets; i++) {
124 line_settings settings;
125
126 settings._m_priv->settings.reset(::gpiod_line_config_get_line_settings(
127 this->_m_priv->config.get(),
128 offsets[i]));
129 if (!settings._m_priv->settings)
130 throw_from_errno("unable to retrieve line settings");
131
132 settings_map[offsets[i]] = ::std::move(settings);
133 }
134
135 return settings_map;
136 }
137
138 GPIOD_CXX_API ::std::ostream&
139 operator<<(::std::ostream& out, const line_config& config)
140 {
141 auto settings_map = config.get_line_settings();
142 ::std::vector<::std::string> vec;
143
144 out << "gpiod::line_config(num_settings=" << settings_map.size();
145
146 if (settings_map.size() == 0) {
147 out << ")";
148 return out;
149 }
150
151 for (const auto& [offset, settings]: settings_map) {
152 ::std::stringstream str;
153
154 str << offset << ": " << settings;
155 vec.push_back(str.str());
156 }
157
158 out << ", settings=[";
159 ::std::copy(vec.begin(), ::std::prev(vec.end()),
160 ::std::ostream_iterator<::std::string>(out, ", "));
161 out << vec.back();
162 out << "])";
163
164 return out;
165 }
166
167 } /* namespace gpiod */