Debian Codegraph @

bindings/rust/gpiosim-sys/src/sim.rs

raw symbols SCIP
1 // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
2 // SPDX-FileCopyrightText: 2022 Linaro Ltd.
3 // SPDX-FileCopyrightText: 2022 Viresh Kumar <[email protected]>
4
5 use std::ffi::{CStr, CString};
6 use std::os::raw::c_char;
7 use std::path::PathBuf;
8 use std::str;
9
10 use libgpiod::{line::Offset, Error, OperationType, Result};
11
12 use crate::*;
13
14 /// Sim Ctx
15 #[derive(Debug)]
16 struct SimCtx {
17 ctx: *mut gpiosim_ctx,
18 }
19
20 // Safe as the pointer is guaranteed to be valid and the associated resource
21 // won't be freed until the object is dropped.
22 unsafe impl Send for SimCtx {}
23
24 impl SimCtx {
25 fn new() -> Result<Self> {
26 // SAFETY: `gpiosim_ctx` returned by gpiosim is guaranteed to live
27 // as long as the `struct SimCtx`.
28 let ctx = unsafe { gpiosim_ctx_new() };
29 if ctx.is_null() {
30 return Err(Error::OperationFailed(
31 OperationType::SimCtxNew,
32 errno::errno(),
33 ));
34 }
35
36 Ok(Self { ctx })
37 }
38 }
39
40 impl Drop for SimCtx {
41 fn drop(&mut self) {
42 // SAFETY: `gpiosim_ctx` is guaranteed to be valid here.
43 unsafe { gpiosim_ctx_unref(self.ctx) }
44 }
45 }
46
47 /// Sim Dev
48 #[derive(Debug)]
49 struct SimDev {
50 dev: *mut gpiosim_dev,
51 }
52
53 // Safe as the pointer is guaranteed to be valid and the associated resource
54 // won't be freed until the object is dropped.
55 unsafe impl Send for SimDev {}
56
57 impl SimDev {
58 fn new(ctx: &SimCtx) -> Result<Self> {
59 // SAFETY: `gpiosim_dev` returned by gpiosim is guaranteed to live
60 // as long as the `struct SimDev`.
61 let dev = unsafe { gpiosim_dev_new(ctx.ctx) };
62 if dev.is_null() {
63 return Err(Error::OperationFailed(
64 OperationType::SimDevNew,
65 errno::errno(),
66 ));
67 }
68
69 Ok(Self { dev })
70 }
71
72 fn enable(&self) -> Result<()> {
73 // SAFETY: `gpiosim_dev` is guaranteed to be valid here.
74 let ret = unsafe { gpiosim_dev_enable(self.dev) };
75
76 if ret == -1 {
77 Err(Error::OperationFailed(
78 OperationType::SimDevEnable,
79 errno::errno(),
80 ))
81 } else {
82 Ok(())
83 }
84 }
85
86 fn disable(&self) -> Result<()> {
87 // SAFETY: `gpiosim_dev` is guaranteed to be valid here.
88 let ret = unsafe { gpiosim_dev_disable(self.dev) };
89
90 if ret == -1 {
91 Err(Error::OperationFailed(
92 OperationType::SimDevDisable,
93 errno::errno(),
94 ))
95 } else {
96 Ok(())
97 }
98 }
99 }
100
101 impl Drop for SimDev {
102 fn drop(&mut self) {
103 // SAFETY: `gpiosim_dev` is guaranteed to be valid here.
104 unsafe { gpiosim_dev_unref(self.dev) }
105 }
106 }
107
108 /// Sim Bank
109 #[derive(Debug)]
110 struct SimBank {
111 bank: *mut gpiosim_bank,
112 }
113
114 // Safe as the pointer is guaranteed to be valid and the associated resource
115 // won't be freed until the object is dropped.
116 unsafe impl Send for SimBank {}
117
118 impl SimBank {
119 fn new(dev: &SimDev) -> Result<Self> {
120 // SAFETY: `gpiosim_bank` returned by gpiosim is guaranteed to live
121 // as long as the `struct SimBank`.
122 let bank = unsafe { gpiosim_bank_new(dev.dev) };
123 if bank.is_null() {
124 return Err(Error::OperationFailed(
125 OperationType::SimBankNew,
126 errno::errno(),
127 ));
128 }
129
130 Ok(Self { bank })
131 }
132
133 fn chip_name(&self) -> Result<&str> {
134 // SAFETY: The string returned by gpiosim is guaranteed to live as long
135 // as the `struct SimBank`.
136 let name = unsafe { gpiosim_bank_get_chip_name(self.bank) };
137
138 // SAFETY: The string is guaranteed to be valid here.
139 unsafe { CStr::from_ptr(name) }
140 .to_str()
141 .map_err(Error::StringNotUtf8)
142 }
143
144 fn dev_path(&self) -> Result<PathBuf> {
145 // SAFETY: The string returned by gpiosim is guaranteed to live as long
146 // as the `struct SimBank`.
147 let path = unsafe { gpiosim_bank_get_dev_path(self.bank) };
148
149 // SAFETY: The string is guaranteed to be valid here.
150 let path = unsafe { CStr::from_ptr(path) }
151 .to_str()
152 .map_err(Error::StringNotUtf8)?;
153
154 Ok(PathBuf::from(path))
155 }
156
157 fn val(&self, offset: Offset) -> Result<Value> {
158 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
159 let ret = unsafe { gpiosim_bank_get_value(self.bank, offset) };
160
161 if ret == -1 {
162 Err(Error::OperationFailed(
163 OperationType::SimBankGetVal,
164 errno::errno(),
165 ))
166 } else {
167 Value::new(ret)
168 }
169 }
170
171 fn set_label(&self, label: &str) -> Result<()> {
172 let label = CString::new(label).map_err(|_| Error::InvalidString)?;
173
174 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
175 let ret = unsafe { gpiosim_bank_set_label(self.bank, label.as_ptr() as *const c_char) };
176
177 if ret == -1 {
178 Err(Error::OperationFailed(
179 OperationType::SimBankSetLabel,
180 errno::errno(),
181 ))
182 } else {
183 Ok(())
184 }
185 }
186
187 fn set_num_lines(&self, num: usize) -> Result<()> {
188 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
189 let ret = unsafe { gpiosim_bank_set_num_lines(self.bank, num) };
190 if ret == -1 {
191 Err(Error::OperationFailed(
192 OperationType::SimBankSetNumLines,
193 errno::errno(),
194 ))
195 } else {
196 Ok(())
197 }
198 }
199
200 fn set_line_name(&self, offset: Offset, name: &str) -> Result<()> {
201 let name = CString::new(name).map_err(|_| Error::InvalidString)?;
202
203 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
204 let ret = unsafe {
205 gpiosim_bank_set_line_name(self.bank, offset, name.as_ptr() as *const c_char)
206 };
207
208 if ret == -1 {
209 Err(Error::OperationFailed(
210 OperationType::SimBankSetLineName,
211 errno::errno(),
212 ))
213 } else {
214 Ok(())
215 }
216 }
217
218 fn set_pull(&self, offset: Offset, pull: Pull) -> Result<()> {
219 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
220 let ret = unsafe { gpiosim_bank_set_pull(self.bank, offset, pull.val()) };
221
222 if ret == -1 {
223 Err(Error::OperationFailed(
224 OperationType::SimBankSetPull,
225 errno::errno(),
226 ))
227 } else {
228 Ok(())
229 }
230 }
231
232 fn hog_line(&self, offset: Offset, name: &str, dir: Direction) -> Result<()> {
233 let name = CString::new(name).map_err(|_| Error::InvalidString)?;
234
235 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
236 let ret = unsafe {
237 gpiosim_bank_hog_line(self.bank, offset, name.as_ptr() as *const c_char, dir.val())
238 };
239
240 if ret == -1 {
241 Err(Error::OperationFailed(
242 OperationType::SimBankHogLine,
243 errno::errno(),
244 ))
245 } else {
246 Ok(())
247 }
248 }
249 }
250
251 impl Drop for SimBank {
252 fn drop(&mut self) {
253 // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
254 unsafe { gpiosim_bank_unref(self.bank) }
255 }
256 }
257
258 /// GPIO SIM
259 #[derive(Debug)]
260 pub struct Sim {
261 _ctx: SimCtx,
262 dev: SimDev,
263 bank: SimBank,
264 }
265
266 impl Sim {
267 pub fn new(ngpio: Option<usize>, label: Option<&str>, enable: bool) -> Result<Self> {
268 let ctx = SimCtx::new()?;
269 let dev = SimDev::new(&ctx)?;
270 let bank = SimBank::new(&dev)?;
271
272 if let Some(ngpio) = ngpio {
273 bank.set_num_lines(ngpio)?;
274 }
275
276 if let Some(label) = label {
277 bank.set_label(label)?;
278 }
279
280 if enable {
281 dev.enable()?;
282 }
283
284 Ok(Self {
285 _ctx: ctx,
286 dev,
287 bank,
288 })
289 }
290
291 pub fn chip_name(&self) -> &str {
292 self.bank.chip_name().unwrap()
293 }
294
295 pub fn dev_path(&self) -> PathBuf {
296 self.bank.dev_path().unwrap()
297 }
298
299 pub fn val(&self, offset: Offset) -> Result<Value> {
300 self.bank.val(offset)
301 }
302
303 pub fn set_label(&self, label: &str) -> Result<()> {
304 self.bank.set_label(label)
305 }
306
307 pub fn set_num_lines(&self, num: usize) -> Result<()> {
308 self.bank.set_num_lines(num)
309 }
310
311 pub fn set_line_name(&self, offset: Offset, name: &str) -> Result<()> {
312 self.bank.set_line_name(offset, name)
313 }
314
315 pub fn set_pull(&self, offset: Offset, pull: Pull) -> Result<()> {
316 self.bank.set_pull(offset, pull)
317 }
318
319 pub fn hog_line(&self, offset: Offset, name: &str, dir: Direction) -> Result<()> {
320 self.bank.hog_line(offset, name, dir)
321 }
322
323 pub fn enable(&self) -> Result<()> {
324 self.dev.enable()
325 }
326
327 pub fn disable(&self) -> Result<()> {
328 self.dev.disable()
329 }
330 }
331
332 impl Drop for Sim {
333 fn drop(&mut self) {
334 self.disable().unwrap()
335 }
336 }