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
// libiio-sys/src/device.rs
//
// Copyright (c) 2018, Frank Pagliughi
//
// Licensed under the MIT license:
//   <LICENSE or http://opensource.org/licenses/MIT>
// This file may not be copied, modified, or distributed except according
// to those terms.
//
//! Industrial I/O Devices
//!

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::*;

/// An Industrial I/O Device
///
/// This can not be created directly. It is obtained from a context.
pub struct Device {
    pub(crate) dev: *mut ffi::iio_device,
}

impl Device {
    /// Gets the context to which the device belongs
    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, }
    }

    /// Gets the device ID (e.g. <b><i>iio:device0</i></b>)
    pub fn id(&self) -> Option<String> {
        let pstr = unsafe { ffi::iio_device_get_id(self.dev) };
        cstring_opt(pstr)
    }

    /// Gets the name of the device
    pub fn name(&self) -> Option<String> {
        let pstr = unsafe { ffi::iio_device_get_name(self.dev) };
        cstring_opt(pstr)
    }

    /// Determines whether the device is a trigger
    pub fn is_trigger(&self) -> bool {
        unsafe { ffi::iio_device_is_trigger(self.dev) }
    }

    /// Associate a trigger for this device.
    /// `trigger` The device to be used as a trigger.
    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(())
    }

    /// Gets the number of device-specific attributes
    pub fn num_attrs(&self) -> usize {
        let n = unsafe { ffi::iio_device_get_attrs_count(self.dev) };
        n as usize
    }

    /// Gets the name of the device-specific attribute at the index
    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())
    }

    /// Reads a device-specific attribute as a boolean
    ///
    /// `attr` The name of the attribute
    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)
    }

    /// Reads a device-specific attribute as an integer (i64)
    ///
    /// `attr` The name of the attribute
    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)
    }

    /// Reads a device-specific attribute as a floating-point (f64) number
    ///
    /// `attr` The name of the attribute
    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)
    }

    /// Writes a device-specific attribute as a boolean
    ///
    /// `attr` The name of the attribute
    /// `val` The value to write
    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(())
    }

    /// Writes a device-specific attribute as an integer (i64)
    ///
    /// `attr` The name of the attribute
    /// `val` The value to write
    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(())
    }

    /// Writes a device-specific attribute as a floating-point (f64) number
    ///
    /// `attr` The name of the attribute
    /// `val` The value to write
    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(())
    }

    /// Gets the number of channels on the device
    pub fn num_channels(&self) -> usize {
        let n = unsafe { ffi::iio_device_get_channels_count(self.dev) };
        n as usize
    }

    /// Gets a channel by index
    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, })
    }

    /// Try to find a channel by its name or ID
    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, })
        }
    }

    /// Gets an iterator for the channels in the device
    pub fn channels(&self) -> ChannelIterator {
        ChannelIterator {
            dev: self,
            idx: 0,
        }
    }

    /// Creates a buffer for the device.
    ///
    /// `sample_count` The number of samples the buffer should hold
    /// `cyclic` Whether to enable cyclic mode.
    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 {
    /// Two devices are the same if they refer to the same underlying
    /// object in the library.
    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
        }
    }
}