File: line-config.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 (167 lines) | stat: -rw-r--r-- 3,884 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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include <cstdlib>
#include <iterator>
#include <ostream>
#include <sstream>
#include <utility>
#include <vector>

#include "internal.hpp"

namespace gpiod {

namespace {

line_config_ptr make_line_config()
{
	line_config_ptr config(::gpiod_line_config_new());
	if (!config)
		throw_from_errno("Unable to allocate the line config object");

	return config;
}

} /* namespace */

line_config::impl::impl()
	: config(make_line_config())
{

}

GPIOD_CXX_API line_config::line_config()
	: _m_priv(new impl)
{

}

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

}

GPIOD_CXX_API line_config::~line_config()
{

}

line_config& line_config::operator=(const line_config& other)
{
	this->_m_priv = other._m_priv;

	return *this;
}

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

	return *this;
}

GPIOD_CXX_API line_config& line_config::reset() noexcept
{
	::gpiod_line_config_reset(this->_m_priv->config.get());

	return *this;
}

GPIOD_CXX_API line_config& line_config::add_line_settings(line::offset offset,
							  const line_settings& settings)
{
	return this->add_line_settings(line::offsets({offset}), settings);
}

GPIOD_CXX_API line_config& line_config::add_line_settings(const line::offsets& offsets,
							  const line_settings& settings)
{
	::std::vector<unsigned int> raw_offsets(offsets.size());

	for (unsigned int i = 0; i < offsets.size(); i++)
		raw_offsets[i] = offsets[i];

	auto ret = ::gpiod_line_config_add_line_settings(this->_m_priv->config.get(),
							 raw_offsets.data(), raw_offsets.size(),
							 settings._m_priv->settings.get());
	if (ret)
		throw_from_errno("unable to add line settings");

	return *this;
}

GPIOD_CXX_API line_config& line_config::set_output_values(const line::values& values)
{
	::std::vector<::gpiod_line_value> mapped_values(values.size());

	for (unsigned int i = 0; i < values.size(); i++)
		mapped_values[i] = map_output_value(values[i]);

	auto ret = ::gpiod_line_config_set_output_values(this->_m_priv->config.get(),
							 mapped_values.data(), mapped_values.size());
	if (ret)
		throw_from_errno("unable to set output values");

	return *this;
}

GPIOD_CXX_API ::std::map<line::offset, line_settings> line_config::get_line_settings() const
{
	::std::size_t num_offsets = ::gpiod_line_config_get_num_configured_offsets(
								this->_m_priv->config.get());
	::std::map<line::offset, line_settings> settings_map;
	::std::vector<unsigned int> offsets(num_offsets);

	if (num_offsets == 0)
		return settings_map;

	::gpiod_line_config_get_configured_offsets(this->_m_priv->config.get(),
					offsets.data(), num_offsets);

	for (size_t i = 0; i < num_offsets; i++) {
		line_settings settings;

		settings._m_priv->settings.reset(::gpiod_line_config_get_line_settings(
							this->_m_priv->config.get(),
							offsets[i]));
		if (!settings._m_priv->settings)
			throw_from_errno("unable to retrieve line settings");

		settings_map[offsets[i]] = ::std::move(settings);
	}

	return settings_map;
}

GPIOD_CXX_API ::std::ostream&
operator<<(::std::ostream& out, const line_config& config)
{
	auto settings_map = config.get_line_settings();
	::std::vector<::std::string> vec;

	out << "gpiod::line_config(num_settings=" << settings_map.size();

	if (settings_map.size() == 0) {
		out << ")";
		return out;
	}

	for (const auto& [offset, settings]: settings_map) {
		::std::stringstream str;

		str << offset << ": " << settings;
		vec.push_back(str.str());
	}

	out << ", settings=[";
	::std::copy(vec.begin(), ::std::prev(vec.end()),
		    ::std::ostream_iterator<::std::string>(out, ", "));
	out << vec.back();
	out << "])";

	return out;
}

} /* namespace gpiod */