libicsneo_sys/
lib.rs

1//! Rust sys level crate providing a cbindgen API for libicsneo (https://github.com/intrepidcs/libicsneo)
2//! If unsure, don't use this crate and use libicsneo-rs instead.
3// Suppress the flurry of warnings caused by using "C" naming conventions
4#![allow(non_upper_case_globals)]
5#![allow(non_camel_case_types)]
6#![allow(non_snake_case)]
7
8include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
9
10#[cfg(test)]
11mod tests {
12    use super::*;
13    use std::ffi::CString;
14
15    #[test]
16    fn test_icsneo_findAllDevices() {
17        const DEVICE_COUNT_MAX: usize = 255;
18        let mut device_count = DEVICE_COUNT_MAX;
19        let mut devices = [neodevice_t::default(); DEVICE_COUNT_MAX];
20        
21        unsafe {
22            icsneo_findAllDevices(devices.as_mut_ptr(), &mut device_count);
23        }
24        println!("Found {} device(s)", device_count);
25
26        for i in 0..device_count as usize {
27            let device = &devices[i];
28            let success;
29            let name = unsafe {
30                // Open the device
31                success = icsneo_openDevice(device);
32
33                // Get the device type string
34                let mut name_length: usize = 0;
35                icsneo_getProductNameForType(device.type_, std::ptr::null_mut(), &mut name_length);
36                name_length += 1;
37                let mut name_buffer = vec![0 as std::os::raw::c_char; name_length as usize];
38                icsneo_getProductNameForType(
39                    device.type_,
40                    name_buffer.as_mut_ptr(),
41                    &mut name_length,
42                );
43                let name =
44                    CString::from_vec_with_nul(name_buffer.iter().map(|v| *v as u8).collect())
45                        .unwrap();
46                let name = name.to_str().unwrap();
47                format!("{} {}", name, "TODO") //device.get_serial_number())
48            };
49            if success {
50                println!("Opened {} successfully:\n{:#?}", name, device);
51                unsafe {
52                    let _ = icsneo_closeDevice(device);
53                }
54            } else {
55                println!("Failed to open {:?}!", device);
56            }
57        }
58    }
59}