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
242
243
244
245
246
247
248
249
250
251
use std::ffi::{CString, CStr};
use std::os::raw::{c_uint, c_longlong};
use nix::errno::{Errno};
use nix::Error::Sys as SysError;
use ffi;
use super::*;
use errors::*;
use channel::*;
pub struct Device {
pub(crate) dev: *mut ffi::iio_device,
}
impl Device {
pub fn context(&self) -> Context {
let ctx = unsafe { ffi::iio_device_get_context(self.dev) as *mut ffi::iio_context };
if ctx.is_null() { panic!("Unexpected NULL context"); }
Context { ctx, }
}
pub fn id(&self) -> Option<String> {
let pstr = unsafe { ffi::iio_device_get_id(self.dev) };
cstring_opt(pstr)
}
pub fn name(&self) -> Option<String> {
let pstr = unsafe { ffi::iio_device_get_name(self.dev) };
cstring_opt(pstr)
}
pub fn is_trigger(&self) -> bool {
unsafe { ffi::iio_device_is_trigger(self.dev) }
}
pub fn set_trigger(&mut self, trigger: &Device) -> Result<()> {
let ret = unsafe { ffi::iio_device_set_trigger(self.dev, trigger.dev) };
if ret < 0 { bail!(SysError(Errno::last())); }
Ok(())
}
pub fn num_attrs(&self) -> usize {
let n = unsafe { ffi::iio_device_get_attrs_count(self.dev) };
n as usize
}
pub fn get_attr(&self, idx: usize) -> Result<String> {
let pstr = unsafe { ffi::iio_device_get_attr(self.dev, idx as c_uint) };
cstring_opt(pstr).ok_or("Invalid index".into())
}
pub fn attr_read_bool(&self, attr: &str) -> Result<bool> {
let mut val: bool = false;
let attr = CString::new(attr).unwrap();
unsafe {
if ffi::iio_device_attr_read_bool(self.dev, attr.as_ptr(), &mut val) < 0 {
bail!(SysError(Errno::last()));
}
}
Ok(val)
}
pub fn attr_read_int(&self, attr: &str) -> Result<i64> {
let mut val: c_longlong = 0;
let attr = CString::new(attr).unwrap();
unsafe {
if ffi::iio_device_attr_read_longlong(self.dev, attr.as_ptr(), &mut val) < 0 {
bail!(SysError(Errno::last()));
}
}
Ok(val as i64)
}
pub fn attr_read_float(&self, attr: &str) -> Result<f64> {
let mut val: f64 = 0.0;
let attr = CString::new(attr).unwrap();
unsafe {
if ffi::iio_device_attr_read_double(self.dev, attr.as_ptr(), &mut val) < 0 {
bail!(SysError(Errno::last()));
}
}
Ok(val)
}
pub fn attr_write_bool(&self, attr: &str, val: bool) -> Result<()> {
let attr = CString::new(attr).unwrap();
unsafe {
if ffi::iio_device_attr_write_bool(self.dev, attr.as_ptr(), val) < 0 {
bail!(SysError(Errno::last()));
}
}
Ok(())
}
pub fn attr_write_int(&self, attr: &str, val: i64) -> Result<()> {
let attr = CString::new(attr).unwrap();
unsafe {
if ffi::iio_device_attr_write_longlong(self.dev, attr.as_ptr(), val) < 0 {
bail!(SysError(Errno::last()));
}
}
Ok(())
}
pub fn attr_write_float(&self, attr: &str, val: f64) -> Result<()> {
let attr = CString::new(attr).unwrap();
unsafe {
if ffi::iio_device_attr_write_double(self.dev, attr.as_ptr(), val) < 0 {
bail!(SysError(Errno::last()));
}
}
Ok(())
}
pub fn num_channels(&self) -> usize {
let n = unsafe { ffi::iio_device_get_channels_count(self.dev) };
n as usize
}
pub fn get_channel(&self, idx: usize) -> Result<Channel> {
let chan = unsafe { ffi::iio_device_get_channel(self.dev, idx as c_uint) };
if chan.is_null() { bail!("Index out of range"); }
Ok(Channel { chan, })
}
pub fn find_channel(&self, name: &str, chan_type: ChannelType) -> Option<Channel> {
let name = CString::new(name).unwrap();
let is_output = chan_type == ChannelType::Output;
let chan = unsafe { ffi::iio_device_find_channel(self.dev, name.as_ptr(), is_output) };
if chan.is_null() {
None
}
else {
Some(Channel { chan, })
}
}
pub fn channels(&self) -> ChannelIterator {
ChannelIterator {
dev: self,
idx: 0,
}
}
pub fn create_buffer(&self, sample_count: usize, cyclic: bool) -> Result<Buffer> {
let buf = unsafe { ffi::iio_device_create_buffer(self.dev, sample_count, cyclic) };
if buf.is_null() { bail!(SysError(Errno::last())); }
Ok(Buffer { buf, })
}
}
impl PartialEq for Device {
fn eq(&self, other: &Device) -> bool {
self.dev == other.dev
}
}
pub struct ChannelIterator<'a> {
dev: &'a Device,
idx: usize,
}
impl<'a> Iterator for ChannelIterator<'a> {
type Item = Channel;
fn next(&mut self) -> Option<Self::Item> {
match self.dev.get_channel(self.idx) {
Ok(chan) => {
self.idx += 1;
Some(chan)
},
Err(_) => None
}
}
}
pub struct AttrIterator<'a> {
dev: &'a Device,
idx: usize,
}
impl<'a> Iterator for AttrIterator<'a> {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
match self.dev.get_attr(self.idx) {
Ok(name) => {
self.idx += 1;
Some(name)
},
Err(_) => None
}
}
}