File: chip.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 (202 lines) | stat: -rw-r--r-- 4,097 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
// SPDX-License-Identifier: LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <brgl@bgdev.pl>

#include <ostream>
#include <utility>

#include "internal.hpp"

namespace gpiod {

namespace {

chip_ptr open_chip(const ::std::filesystem::path& path)
{
	chip_ptr chip(::gpiod_chip_open(path.c_str()));
	if (!chip)
		throw_from_errno("unable to open the GPIO device " + path.string());

	return chip;
}

} /* namespace */

chip::impl::impl(const ::std::filesystem::path& path)
	: chip(open_chip(path))
{

}

void chip::impl::throw_if_closed() const
{
	if (!this->chip)
		throw chip_closed("GPIO chip has been closed");
}

GPIOD_CXX_API chip::chip(const ::std::filesystem::path& path)
	: _m_priv(new impl(path))
{

}

chip::chip(const chip& other)
	: _m_priv(other._m_priv)
{

}

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

}

GPIOD_CXX_API chip::~chip()
{

}

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

	return *this;
}

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

GPIOD_CXX_API void chip::close()
{
	this->_m_priv->throw_if_closed();

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

GPIOD_CXX_API ::std::filesystem::path chip::path() const
{
	this->_m_priv->throw_if_closed();

	return ::gpiod_chip_get_path(this->_m_priv->chip.get());
}

GPIOD_CXX_API chip_info chip::get_info() const
{
	this->_m_priv->throw_if_closed();

	chip_info_ptr info(::gpiod_chip_get_info(this->_m_priv->chip.get()));
	if (!info)
		throw_from_errno("failed to retrieve GPIO chip info");

	chip_info ret;

	ret._m_priv->set_info_ptr(info);

	return ret;
}

GPIOD_CXX_API line_info chip::get_line_info(line::offset offset) const
{
	this->_m_priv->throw_if_closed();

	line_info_ptr info(::gpiod_chip_get_line_info(this->_m_priv->chip.get(), offset));
	if (!info)
		throw_from_errno("unable to retrieve GPIO line info");

	line_info ret;

	ret._m_priv->set_info_ptr(info);

	return ret;
}

GPIOD_CXX_API line_info chip::watch_line_info(line::offset offset) const
{
	this->_m_priv->throw_if_closed();

	line_info_ptr info(::gpiod_chip_watch_line_info(this->_m_priv->chip.get(), offset));
	if (!info)
		throw_from_errno("unable to start watching GPIO line info changes");

	line_info ret;

	ret._m_priv->set_info_ptr(info);

	return ret;
}

GPIOD_CXX_API void chip::unwatch_line_info(line::offset offset) const
{
	this->_m_priv->throw_if_closed();

	int ret = ::gpiod_chip_unwatch_line_info(this->_m_priv->chip.get(), offset);
	if (ret)
		throw_from_errno("unable to unwatch line status changes");
}

GPIOD_CXX_API int chip::fd() const
{
	this->_m_priv->throw_if_closed();

	return ::gpiod_chip_get_fd(this->_m_priv->chip.get());
}

GPIOD_CXX_API bool chip::wait_info_event(const ::std::chrono::nanoseconds& timeout) const
{
	this->_m_priv->throw_if_closed();

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

	return ret;
}

GPIOD_CXX_API info_event chip::read_info_event() const
{
	this->_m_priv->throw_if_closed();

	info_event_ptr event(gpiod_chip_read_info_event(this->_m_priv->chip.get()));
	if (!event)
		throw_from_errno("error reading the line info event_handle");

	info_event ret;
	ret._m_priv->set_info_event_ptr(event);

	return ret;
}

GPIOD_CXX_API int chip::get_line_offset_from_name(const ::std::string& name) const
{
	this->_m_priv->throw_if_closed();

	int ret = ::gpiod_chip_get_line_offset_from_name(this->_m_priv->chip.get(), name.c_str());
	if (ret < 0) {
		if (errno == ENOENT)
			return -1;

		throw_from_errno("error looking up line by name");
	}

	return ret;
}

GPIOD_CXX_API request_builder chip::prepare_request()
{
	return request_builder(*this);
}

GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const chip& chip)
{
	if (!chip)
		out << "gpiod::chip(closed)";
	else
		out << "gpiod::chip(path=" << chip.path() <<
		       ", info=" << chip.get_info() << ")";

	return out;
}

} /* namespace gpiod */