File: line-request.cpp

package info (click to toggle)
libgpiod 2.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,108 kB
  • sloc: ansic: 26,612; sh: 7,554; cpp: 4,944; python: 2,426; makefile: 811; xml: 49
file content (241 lines) | stat: -rw-r--r-- 6,017 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include <iterator>
#include <ostream>
#include <utility>

#include "internal.hpp"

namespace gpiod {

void line_request::impl::throw_if_released() const
{
	if (!this->request)
		throw request_released("GPIO lines have been released");
}

void line_request::impl::set_request_ptr(line_request_ptr& ptr)
{
	this->request = ::std::move(ptr);
	this->offset_buf.resize(::gpiod_line_request_get_num_requested_lines(this->request.get()));
}

void line_request::impl::fill_offset_buf(const line::offsets& offsets)
{
	for (unsigned int i = 0; i < offsets.size(); i++)
		this->offset_buf[i] = offsets[i];
}

line_request::line_request()
	: _m_priv(new impl)
{

}

GPIOD_CXX_API line_request::line_request(line_request&& other) noexcept
	: _m_priv(::std::move(other._m_priv))
{

}

GPIOD_CXX_API line_request::~line_request()
{

}

GPIOD_CXX_API line_request& line_request::operator=(line_request&& other) noexcept
{
	this->_m_priv = ::std::move(other._m_priv);

	return *this;
}

GPIOD_CXX_API line_request::operator bool() const noexcept
{
	return this->_m_priv->request.get() != nullptr;
}

GPIOD_CXX_API void line_request::release()
{
	this->_m_priv->throw_if_released();

	this->_m_priv->request.reset();
}

GPIOD_CXX_API ::std::string line_request::chip_name() const
{
	this->_m_priv->throw_if_released();

	return ::gpiod_line_request_get_chip_name(this->_m_priv->request.get());
}

GPIOD_CXX_API ::std::size_t line_request::num_lines() const
{
	this->_m_priv->throw_if_released();

	return ::gpiod_line_request_get_num_requested_lines(this->_m_priv->request.get());
}

GPIOD_CXX_API line::offsets line_request::offsets() const
{
	this->_m_priv->throw_if_released();

	auto num_lines = this->num_lines();
	::std::vector<unsigned int> buf(num_lines);
	line::offsets offsets(num_lines);

	::gpiod_line_request_get_requested_offsets(this->_m_priv->request.get(), buf.data(), buf.size());

	for (unsigned int i = 0; i < num_lines; i++)
		offsets[i] = buf[i];

	return offsets;
}

GPIOD_CXX_API line::value line_request::get_value(line::offset offset)
{
	return this->get_values({ offset }).front();
}

GPIOD_CXX_API line::values
line_request::get_values(const line::offsets& offsets)
{
	line::values vals(offsets.size());

	this->get_values(offsets, vals);

	return vals;
}

GPIOD_CXX_API line::values line_request::get_values()
{
	return this->get_values(this->offsets());
}

GPIOD_CXX_API void line_request::get_values(const line::offsets& offsets, line::values& values)
{
	this->_m_priv->throw_if_released();

	if (offsets.size() != values.size())
		throw ::std::invalid_argument("values must have the same size as the offsets");

	this->_m_priv->fill_offset_buf(offsets);

	int ret = ::gpiod_line_request_get_values_subset(
					this->_m_priv->request.get(),
					offsets.size(), this->_m_priv->offset_buf.data(),
					reinterpret_cast<::gpiod_line_value*>(values.data()));
	if (ret)
		throw_from_errno("unable to retrieve line values");
}

GPIOD_CXX_API void line_request::get_values(line::values& values)
{
	this->get_values(this->offsets(), values);
}

GPIOD_CXX_API line_request&
line_request::line_request::set_value(line::offset offset, line::value value)
{
	return this->set_values({ offset }, { value });
}

GPIOD_CXX_API line_request&
line_request::set_values(const line::value_mappings& values)
{
	line::offsets offsets(values.size());
	line::values vals(values.size());

	for (unsigned int i = 0; i < values.size(); i++) {
		offsets[i] = values[i].first;
		vals[i] = values[i].second;
	}

	return this->set_values(offsets, vals);
}

GPIOD_CXX_API line_request& line_request::set_values(const line::offsets& offsets,
					    const line::values& values)
{
	this->_m_priv->throw_if_released();

	if (offsets.size() != values.size())
		throw ::std::invalid_argument("values must have the same size as the offsets");

	this->_m_priv->fill_offset_buf(offsets);

	int ret = ::gpiod_line_request_set_values_subset(
					this->_m_priv->request.get(),
					offsets.size(), this->_m_priv->offset_buf.data(),
					reinterpret_cast<const ::gpiod_line_value*>(values.data()));
	if (ret)
		throw_from_errno("unable to set line values");

	return *this;
}

GPIOD_CXX_API line_request& line_request::set_values(const line::values& values)
{
	return this->set_values(this->offsets(), values);
}

GPIOD_CXX_API line_request& line_request::reconfigure_lines(const line_config& config)
{
	this->_m_priv->throw_if_released();

	int ret = ::gpiod_line_request_reconfigure_lines(this->_m_priv->request.get(),
							 config._m_priv->config.get());
	if (ret)
		throw_from_errno("unable to reconfigure GPIO lines");

	return *this;
}

GPIOD_CXX_API int line_request::fd() const
{
	this->_m_priv->throw_if_released();

	return ::gpiod_line_request_get_fd(this->_m_priv->request.get());
}

GPIOD_CXX_API bool line_request::wait_edge_events(const ::std::chrono::nanoseconds& timeout) const
{
	this->_m_priv->throw_if_released();

	int ret = ::gpiod_line_request_wait_edge_events(this->_m_priv->request.get(),
						       timeout.count());
	if (ret < 0)
		throw_from_errno("error waiting for edge events");

	return ret;
}

GPIOD_CXX_API ::std::size_t line_request::read_edge_events(edge_event_buffer& buffer)
{
	return this->read_edge_events(buffer, buffer.capacity());
}

GPIOD_CXX_API ::std::size_t
line_request::read_edge_events(edge_event_buffer& buffer, ::std::size_t max_events)
{
	this->_m_priv->throw_if_released();

	return buffer._m_priv->read_events(this->_m_priv->request, max_events);
}

GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const line_request& request)
{
	if (!request)
		out << "gpiod::line_request(released)";
	else
		out << "gpiod::line_request(chip=\"" << request.chip_name() <<
		       "\", num_lines=" << request.num_lines() <<
		       ", line_offsets=" << request.offsets() <<
		       ", fd=" << request.fd() <<
		       ")";

	return out;
}

} /* namespace gpiod */