Debian Codegraph @

bindings/cxx/chip.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 <ostream>
5 #include <utility>
6
7 #include "internal.hpp"
8
9 namespace gpiod {
10
11 namespace {
12
13 chip_ptr open_chip(const ::std::filesystem::path& path)
14 {
15 chip_ptr chip(::gpiod_chip_open(path.c_str()));
16 if (!chip)
17 throw_from_errno("unable to open the GPIO device " + path.string());
18
19 return chip;
20 }
21
22 } /* namespace */
23
24 chip::impl::impl(const ::std::filesystem::path& path)
25 : chip(open_chip(path))
26 {
27
28 }
29
30 void chip::impl::throw_if_closed() const
31 {
32 if (!this->chip)
33 throw chip_closed("GPIO chip has been closed");
34 }
35
36 GPIOD_CXX_API chip::chip(const ::std::filesystem::path& path)
37 : _m_priv(new impl(path))
38 {
39
40 }
41
42 chip::chip(const chip& other)
43 : _m_priv(other._m_priv)
44 {
45
46 }
47
48 GPIOD_CXX_API chip::chip(chip&& other) noexcept
49 : _m_priv(::std::move(other._m_priv))
50 {
51
52 }
53
54 GPIOD_CXX_API chip::~chip()
55 {
56
57 }
58
59 GPIOD_CXX_API chip& chip::operator=(chip&& other) noexcept
60 {
61 this->_m_priv = ::std::move(other._m_priv);
62
63 return *this;
64 }
65
66 GPIOD_CXX_API chip::operator bool() const noexcept
67 {
68 return this->_m_priv->chip.get() != nullptr;
69 }
70
71 GPIOD_CXX_API void chip::close()
72 {
73 this->_m_priv->throw_if_closed();
74
75 this->_m_priv->chip.reset();
76 }
77
78 GPIOD_CXX_API ::std::filesystem::path chip::path() const
79 {
80 this->_m_priv->throw_if_closed();
81
82 return ::gpiod_chip_get_path(this->_m_priv->chip.get());
83 }
84
85 GPIOD_CXX_API chip_info chip::get_info() const
86 {
87 this->_m_priv->throw_if_closed();
88
89 chip_info_ptr info(::gpiod_chip_get_info(this->_m_priv->chip.get()));
90 if (!info)
91 throw_from_errno("failed to retrieve GPIO chip info");
92
93 chip_info ret;
94
95 ret._m_priv->set_info_ptr(info);
96
97 return ret;
98 }
99
100 GPIOD_CXX_API line_info chip::get_line_info(line::offset offset) const
101 {
102 this->_m_priv->throw_if_closed();
103
104 line_info_ptr info(::gpiod_chip_get_line_info(this->_m_priv->chip.get(), offset));
105 if (!info)
106 throw_from_errno("unable to retrieve GPIO line info");
107
108 line_info ret;
109
110 ret._m_priv->set_info_ptr(info);
111
112 return ret;
113 }
114
115 GPIOD_CXX_API line_info chip::watch_line_info(line::offset offset) const
116 {
117 this->_m_priv->throw_if_closed();
118
119 line_info_ptr info(::gpiod_chip_watch_line_info(this->_m_priv->chip.get(), offset));
120 if (!info)
121 throw_from_errno("unable to start watching GPIO line info changes");
122
123 line_info ret;
124
125 ret._m_priv->set_info_ptr(info);
126
127 return ret;
128 }
129
130 GPIOD_CXX_API void chip::unwatch_line_info(line::offset offset) const
131 {
132 this->_m_priv->throw_if_closed();
133
134 int ret = ::gpiod_chip_unwatch_line_info(this->_m_priv->chip.get(), offset);
135 if (ret)
136 throw_from_errno("unable to unwatch line status changes");
137 }
138
139 GPIOD_CXX_API int chip::fd() const
140 {
141 this->_m_priv->throw_if_closed();
142
143 return ::gpiod_chip_get_fd(this->_m_priv->chip.get());
144 }
145
146 GPIOD_CXX_API bool chip::wait_info_event(const ::std::chrono::nanoseconds& timeout) const
147 {
148 this->_m_priv->throw_if_closed();
149
150 int ret = ::gpiod_chip_wait_info_event(this->_m_priv->chip.get(), timeout.count());
151 if (ret < 0)
152 throw_from_errno("error waiting for info events");
153
154 return ret;
155 }
156
157 GPIOD_CXX_API info_event chip::read_info_event() const
158 {
159 this->_m_priv->throw_if_closed();
160
161 info_event_ptr event(gpiod_chip_read_info_event(this->_m_priv->chip.get()));
162 if (!event)
163 throw_from_errno("error reading the line info event_handle");
164
165 info_event ret;
166 ret._m_priv->set_info_event_ptr(event);
167
168 return ret;
169 }
170
171 GPIOD_CXX_API int chip::get_line_offset_from_name(const ::std::string& name) const
172 {
173 this->_m_priv->throw_if_closed();
174
175 int ret = ::gpiod_chip_get_line_offset_from_name(this->_m_priv->chip.get(), name.c_str());
176 if (ret < 0) {
177 if (errno == ENOENT)
178 return -1;
179
180 throw_from_errno("error looking up line by name");
181 }
182
183 return ret;
184 }
185
186 GPIOD_CXX_API request_builder chip::prepare_request()
187 {
188 return request_builder(*this);
189 }
190
191 GPIOD_CXX_API ::std::ostream& operator<<(::std::ostream& out, const chip& chip)
192 {
193 if (!chip)
194 out << "gpiod::chip(closed)";
195 else
196 out << "gpiod::chip(path=" << chip.path() <<
197 ", info=" << chip.get_info() << ")";
198
199 return out;
200 }
201
202 } /* namespace gpiod */