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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//! # Silicon Labs USB Xpress driver
//!
//! [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
//! [![Crates.io](https://img.shields.io/crates/v/silabs_usb_xpress.svg)](https://crates.io/crates/silabs_usb_xpress)
//! [![silabs_usb_xpress](https://docs.rs/silabs_usb_xpress/badge.svg)](https://docs.rs/silabs_usb_xpress)
//!
//! | OS        | Status    |
//! | ----      | ----      |
//! | Linux     | [![Linux Build Status](https://github.com/fMeow/silabs_usb_xpress/workflows/CI%20%28Linux%29/badge.svg?branch=master)](https://github.com/fMeow/silabs_usb_xpress/actions?query=workflow%3A%22CI+%28Linux%29%22+branch%3Amaster)|
//! | Windows (MSVC) | [![Windows Build Status](https://github.com/fMeow/silabs_usb_xpress/workflows/CI%20%28Windows%29/badge.svg?branch=master)](https://github.com/fMeow/silabs_usb_xpress/actions?query=workflow%3A%22CI+%28Windows%29%22+branch%3Amaster)|
//!
//! This library port API from [SiUSBXp](http://www.etheus.net/SiUSBXp_Linux_Driver),
//! which is an open source port to SiUSBXp.dll, supplied with SiLabs USBXpress.
//! The underlying USB backend is libusb, which enable the cross platform
//! compilation.
//!
//! # Usage
//!
//! Add to your `Cargo.toml`:
//!
//! ``` toml
//! [dependencies]
//! silabs_usb_xpress = "0.2"
//! ```
//!
//! This crate is compatible with Unix and Windows. For unix system,
//! `pkg-config` are required to link `libusb`. For windows, you must have [vcpkg](https://github.com/microsoft/vcpkg)
//! installed, hook up user-wide integration and install `libusb-win32` with it.
//! By default, `libusb` is linked dynamically, and set environment variable `VCPKGRS_DYNAMIC=0`
//! if you want to link statically.
//!
//! To pack a available driver in Windows, use [libusbk' inf wizard](https://osdn.net/projects/sfnet_libusb-win32/downloads/libusb-win32-releases/libusbK-inf-wizard.exe/).
//!
//! # Example
//! ```rust, ignore
//! # use silabs_usb_xpress::{UsbXpress, product_string, devices_count,
//! # ProductStringType, timeouts, set_timeouts};
//! # use std::time::Duration;
//!
//! # fn main(){
//! // get device count
//! let num = devices_count();
//!
//! // print serial number for selected devices
//! let if_sn = product_string(0, ProductStringType::SerialNumber);
//!
//! // print VID for selected devices
//! let pst = ProductStringType::VID;
//! let if_vid = product_string(0, pst);
//!
//! // get timeouts
//! let t = timeouts().unwrap();
//!
//! // set timeouts
//! set_timeouts(Duration::from_millis(500), None).unwrap();
//!
//! // open handle
//! let mut handle = UsbXpress::open(0).unwrap();
//!
//! // write to device handle
//! let v = vec![0x55, 0x80, 0x00, 0x01, 0x01, 0xAA];
//! handle.write(&v);
//!
//! // read 7 bytes from device handle
//! let read_res = handle.read(7);
//!
//! // close device
//! handle.close();
//! # }
//! ```
//!
//! # License
//! [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
use std::{error::Error, fmt, fmt::Formatter, mem::MaybeUninit, os::raw::c_char, time::Duration};

use ffi::*;

#[allow(dead_code)]
mod ffi {
    include!("bindings.rs");
}

/// Returns the number of devices connected
///
/// This function returns the number of devices connected to the host.
///
/// - Supported Devices
///
/// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
/// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
/// CP2101/2/3/4/5/8/9/
pub fn devices_count() -> Result<usize, SilabsUsbXpressError> {
    let (status, num) = unsafe {
        let mut num = MaybeUninit::uninit();
        let status = SI_GetNumDevices(num.as_mut_ptr());
        (status, num.assume_init())
    };
    match status as u32 {
        SI_SUCCESS => Ok(num as usize),
        SI_DEVICE_NOT_FOUND => Err(SilabsUsbXpressError::DeviceNotFound),
        _ => unreachable!(
            "Unreachable status code: {}. Please contact the author or submit an issue.",
            status
        ),
    }
}

#[derive(Copy, Clone, Debug)]
pub enum ProductStringType {
    SerialNumber = 0,
    Description = 1,
    LinkName = 2,
    VID = 3,
    PID = 4,
}

/// Returns a descriptor for a device
///
/// This function returns a null terminated serial number (S/N) string or
/// product description string for the device specified by an index passed in
/// DeviceNum. The index for the first device is 0 and the last device is the
/// value returned by SI_GetNumDevices – 1.
///
/// - Supported Devices
///
/// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
/// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
/// CP2101/2/3/4/5/8/9
pub fn product_string(
    device_ix: usize,
    product_string_type: ProductStringType,
) -> Result<String, SilabsUsbXpressError> {
    let mut buffer: [c_char; 256] = [0; 256];
    let status = unsafe {
        SI_GetProductString(
            device_ix as i32,
            buffer.as_mut_ptr(),
            product_string_type as i32,
        )
    };
    match status as u32 {
        SI_SUCCESS => {
            let mut string = String::from_utf8(buffer.iter().map(|&c| c as u8).collect())
                .unwrap()
                .trim_end_matches("\0")
                .to_owned();
            match product_string_type {
                ProductStringType::PID | ProductStringType::VID => {
                    if string.len() < 4 {
                        for _ in 0..4 - string.len() {
                            string.insert(0, '0');
                        }
                    }
                    Ok(string.to_uppercase())
                }
                _ => Ok(string),
            }
        }
        SI_DEVICE_NOT_FOUND => Err(SilabsUsbXpressError::DeviceNotFound),
        _ => unreachable!(
            "Unreachable status code: {}. Please contact the author or submit an issue.",
            status
        ),
    }
}

pub struct UsbXpress {
    inner: *mut SiPrivate,
    device_ix: usize,
}

impl UsbXpress {
    /// Opens a device and returns a handle
    ///
    /// Opens a device (using device number as returned by SI_GetNumDevices) and
    /// returns a handle which will be used for subsequent accesses.
    ///
    /// - Supported Devices
    ///
    /// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
    /// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
    /// CP2101/2/3/4/5/8/9
    pub fn open(device_ix: usize) -> Result<Self, SilabsUsbXpressError> {
        let mut handle: MaybeUninit<*mut SiPrivate> = MaybeUninit::uninit();
        let (status, handle) = unsafe {
            let status = SI_Open(device_ix as i32, handle.as_mut_ptr());
            (status, handle.assume_init())
        };
        match status as u32 {
            SI_SUCCESS => Ok(UsbXpress {
                inner: handle,
                device_ix: device_ix,
            }),
            SI_SYSTEM_ERROR_CODE => Err(SilabsUsbXpressError::SystemErrorCode),
            SI_GLOBAL_DATA_ERROR => Err(SilabsUsbXpressError::GlobalDataError),
            _ => unreachable!(
                "Unreachable status code: {}. Please contact the author or submit an issue.",
                status
            ),
        }
    }

    /// Cancels pending IO and closes a device
    ///
    /// Closes an open device using the handle provided by SI_Open and sets the
    /// handle to INVALID_HANDLE_VALUE.
    ///
    /// - Supported Devices
    ///
    /// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
    /// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
    /// CP2101/2/3/4/5/8/9
    pub fn close(self) -> Result<(), SilabsUsbXpressError> {
        let status = unsafe { SI_Close(self.inner) };
        match status as u32 {
            SI_SUCCESS => Ok(()),
            SI_SYSTEM_ERROR_CODE => Err(SilabsUsbXpressError::SystemErrorCode),
            SI_GLOBAL_DATA_ERROR => Err(SilabsUsbXpressError::GlobalDataError),
            _ => unreachable!(
                "Unreachable status code: {}. Please contact the author or submit an issue.",
                status
            ),
        }
    }

    /// Reads a block of data from a device
    ///
    /// Reads the available number of bytes into the supplied buffer and
    /// retrieves the number of bytes that were read (this can be less than
    /// the number of bytes requested). This function returns synchronously
    /// if the overlapped object is set to NULL (this happens by default)
    /// but will not block system execution. If an initialized OVERLAPPED
    /// object is passed then the function returns immediately. If the read
    /// completed then the status will be SI_SUCCESS but if I/O is still
    /// pending then it will return STATUS_IO_PENDING. If STATUS_IO_PENDING
    /// is returned, the OVERLAPPED object can then be waited on using
    /// WaitForSingleObject(), and retrieve data or cancel using
    /// GetOverlappedResult() (as documented on MSDN by Microsoft) or
    /// SI_CancelIo() respectively. This functionality allows for multiple reads
    /// to be issued and waited on at a time. If any data is available when
    /// SI_Read is called it will return so check NumBytesReturned to
    /// determine if all requested data was returned. To make sure that
    /// SI_Read returns the requested number of bytes use SI_CheckRxQueue().
    ///
    /// - Supported Devices
    ///
    /// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
    /// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
    /// CP2101/2/3/4/5/8/9
    pub fn read(&mut self, bytes_to_read: usize) -> Result<Vec<u8>, SilabsUsbXpressError> {
        let mut buffer = Vec::with_capacity(bytes_to_read);
        // let mut buffer: [i8;256] = [0;256];
        let status = unsafe {
            let mut bytes_returned = MaybeUninit::uninit();
            let status = SI_Read(
                self.inner,
                buffer.as_mut_slice().as_mut_ptr(),
                bytes_to_read as i32,
                bytes_returned.as_mut_ptr(),
                MaybeUninit::uninit().as_mut_ptr(),
            );
            buffer.set_len(bytes_returned.assume_init() as usize);
            status
        };
        match status as u32 {
            SI_SUCCESS => Ok(buffer.iter().map(|&c| c as u8).collect()),
            SI_READ_ERROR => Err(SilabsUsbXpressError::ReadError),
            SI_READ_TIMED_OUT => Err(SilabsUsbXpressError::ReadTimeOut),
            SI_IO_PENDING => Err(SilabsUsbXpressError::IoPending),
            SI_SYSTEM_ERROR_CODE => Err(SilabsUsbXpressError::SystemErrorCode),
            SI_INVALID_REQUEST_LENGTH => Err(SilabsUsbXpressError::InvalidRequestLength),
            SI_DEVICE_IO_FAILED => Err(SilabsUsbXpressError::DeviceIoFailed),
            _ => unreachable!(
                "Unreachable status code: {}. Please contact the author or submit an issue.",
                status
            ),
        }
    }

    /// Writes a block of data to a device
    ///
    /// On USB MCU devices, this function flushes both the receive buffer in the
    /// USBXpress device driver and the transmit buffer in the device.
    ///
    /// **Note**: Parameter 2 and 3 of `SI_Write` have no effect and any
    /// value can be passed when used with USB MCU devices.
    ///
    /// On CP210x devices, this function operates in accordance with parameters
    /// 2 and 3. If parameter 2 (FlushTransmit) is non-zero, the CP210x
    /// device’s UART transmit buffer is flushed. If parameter 3
    /// (FlushReceive) is non-zero, the CP210x device’s UART receive buffer
    /// is flushed. If parameters 2 and 3 are both non-zero, then both the
    /// CP210x device UART transmit buffer and UART receive buffer are
    /// flushed.
    ///
    /// - Supported Devices
    ///
    /// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
    /// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
    /// CP2101/2/3/4/5/8/9
    pub fn write(&mut self, to_write: &Vec<u8>) -> Result<usize, SilabsUsbXpressError> {
        let mut buffer: Vec<c_char> = to_write.iter().map(|&c| c as c_char).collect();
        let (status, bytes_written) = unsafe {
            let mut bytes_written = MaybeUninit::uninit();
            let status = SI_Write(
                self.inner,
                buffer.as_mut_ptr(),
                to_write.len() as i32,
                bytes_written.as_mut_ptr(),
                MaybeUninit::uninit().as_mut_ptr(),
            );
            (status, bytes_written.assume_init())
        };
        match status as u32 {
            SI_SUCCESS => Ok(bytes_written as usize),
            SI_WRITE_ERROR => Err(SilabsUsbXpressError::WriteError),
            SI_INVALID_REQUEST_LENGTH => Err(SilabsUsbXpressError::InvalidRequestLength),
            SI_WRITE_TIMED_OUT => Err(SilabsUsbXpressError::WriteTimeOut),
            SI_IO_PENDING => Err(SilabsUsbXpressError::IoPending),
            SI_SYSTEM_ERROR_CODE => Err(SilabsUsbXpressError::SystemErrorCode),
            SI_DEVICE_IO_FAILED => Err(SilabsUsbXpressError::DeviceIoFailed),
            _ => unreachable!(
                "Unreachable status code: {}. Please contact the author or submit an issue.",
                status
            ),
        }
    }

    /// Allows sending low-level commands to the device driver
    ///
    /// **UNIMPLEMENTED!!!**
    ///
    /// Interface for any miscellaneous device control functions. A separate
    /// call to SI_DeviceIOControl is required for each input or output
    /// operation. A single call cannot be used to perform both an input and
    /// output operation simultaneously. Refer to DeviceIOControl function
    /// definition on MSDN Help for more details.
    ///
    /// - Supported Devices
    ///
    /// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
    /// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3
    pub fn device_io_control(&mut self) {
        unimplemented!()
    }

    /// Flushes the TX and RX buffers for a device
    ///
    /// On USB MCU devices, this function flushes both the receive buffer in the
    /// USBXpress device driver and the transmit buffer in the device.
    ///
    /// **Note**: Parameter 2 and 3 of `SI_FlushBuffers` have no effect and any
    /// value can be passed when used with USB MCU devices.
    ///
    /// On CP210x devices, this function operates in accordance with parameters
    /// 2 and 3. If parameter 2 (FlushTransmit) is non-zero, the CP210x
    /// device’s UART transmit buffer is flushed. If parameter 3
    /// (FlushReceive) is non-zero, the CP210x device’s UART receive buffer
    /// is flushed. If parameters 2 and 3 are both non-zero, then both the
    /// CP210x device UART transmit buffer and UART receive buffer are
    /// flushed.
    ///
    /// - Supported Devices
    ///
    /// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
    /// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
    /// CP2101/2/3/4/5/8/9
    pub fn flush_buffers(&mut self) -> Result<(), SilabsUsbXpressError> {
        let status = unsafe { SI_FlushBuffers(self.inner, 1 as c_char, 1 as c_char) };
        match status as u32 {
            SI_SUCCESS => Ok(()),
            SI_SYSTEM_ERROR_CODE => Err(SilabsUsbXpressError::SystemErrorCode),
            _ => unreachable!(
                "Unreachable status code: {}. Please contact the author or submit an issue.",
                status
            ),
        }
    }

    /// Returns the number of bytes in a device's RX queue
    ///
    /// Returns the number of bytes in the receive queue and a status value that
    /// indicates if an overrun (SI_QUEUE_OVERRUN) has occurred and if the RX
    /// queue is ready (SI_QUEUE_READY) for reading. Upon indication of an
    /// Overrun condition it is recommended that data transfer be stopped
    /// and all buffers be flushed using the SI_FlushBuffers command.
    pub fn check_rx_queue(&mut self) -> Result<(usize, usize), SilabsUsbXpressError> {
        let (status, num_bytes_in_queue, queue_status) = unsafe {
            let mut num_bytes_in_queue = MaybeUninit::uninit();
            let mut queue_status = MaybeUninit::uninit();
            let status = SI_CheckRXQueue(
                self.inner,
                num_bytes_in_queue.as_mut_ptr(),
                queue_status.as_mut_ptr(),
            );
            (
                status,
                num_bytes_in_queue.assume_init(),
                queue_status.assume_init(),
            )
        };
        match status as u32 {
            SI_SUCCESS => Ok((num_bytes_in_queue as usize, queue_status as usize)),
            SI_DEVICE_IO_FAILED => Err(SilabsUsbXpressError::DeviceIoFailed),
            _ => unreachable!(
                "Unreachable status code: {}. Please contact the author or submit an issue.",
                status
            ),
        }
    }
}

impl fmt::Debug for UsbXpress {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UsbXpress")
            .field("device_ix", &self.device_ix)
            .finish()
    }
}

/// Sets read and write block timeouts
///
/// Sets the read and write timeouts. Timeouts are used for SI_Read and SI_Write
/// when called synchronously (OVERLAPPED* o is set to NULL). The default value
/// for timeouts is 1000ms.
///
/// - Supported Devices
///
/// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
/// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
/// CP2101/2/3/4/5/8/9
pub fn set_timeouts<R: Into<Option<Duration>>, W: Into<Option<Duration>>>(
    read: R,
    write: W,
) -> Result<(), SilabsUsbXpressError> {
    let status = unsafe {
        SI_SetTimeouts(
            read.into().unwrap_or(Duration::from_secs(1)).as_millis() as i32,
            write.into().unwrap_or(Duration::from_secs(1)).as_millis() as i32,
        )
    };

    match status as u32 {
        SI_SUCCESS => Ok(()),
        SI_DEVICE_IO_FAILED => Err(SilabsUsbXpressError::DeviceIoFailed),
        _ => unreachable!(
            "Unreachable status code: {}. Please contact the author or submit an issue.",
            status
        ),
    }
}

#[derive(Debug)]
pub struct Timeout {
    read: Duration,
    write: Duration,
}

impl Timeout {
    pub fn read_timeout(&self) -> Duration {
        self.read
    }
    pub fn write_timeout(&self) -> Duration {
        self.write
    }
}

/// Gets read and write block timeouts
///
/// Returns the current read and write timeouts. If a timeout value is None in
/// Rust, it has been set to wait 1000ms; otherwise the timeouts are specified
/// in milliseconds.
///
/// - Supported Devices
///
/// C8051F320/1/6/7, C8051F340/1/2/3/4/5/6/7/8/9/A/B/C/D,
/// C8051F380/1/2/3/4/5/6/7, C8051T320/1/2/3/6/7, C8051T620/1/2/3,
/// CP2101/2/3/4/5/8/9
pub fn timeouts() -> Result<Timeout, SilabsUsbXpressError> {
    let (status, read, write) = unsafe {
        let mut read = MaybeUninit::uninit();
        let mut write = MaybeUninit::uninit();
        let status = SI_GetTimeouts(read.as_mut_ptr(), write.as_mut_ptr());
        (status, read.assume_init(), write.assume_init())
    };

    match status as u32 {
        SI_SUCCESS => Ok(Timeout {
            read: Duration::from_millis(read as u64),
            write: Duration::from_millis(write as u64),
        }),
        SI_DEVICE_IO_FAILED => Err(SilabsUsbXpressError::DeviceIoFailed),
        _ => unreachable!(
            "Unreachable status code: {}. Please contact the author or submit an issue.",
            status
        ),
    }
}

#[derive(Debug)]
pub enum SilabsUsbXpressError {
    ConnectionError,
    DeviceNotFound,
    SystemErrorCode,
    GlobalDataError,
    ReadError,
    ReadTimeOut,
    IoPending,
    InvalidRequestLength,
    DeviceIoFailed,
    WriteError,
    WriteTimeOut,
}

impl fmt::Display for SilabsUsbXpressError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(&format!("{:?}", self))
    }
}

impl Error for SilabsUsbXpressError {}