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
#![deny(missing_docs, warnings)]

use std::convert::From;
use std::error::Error;
use std::fmt;

use super::DeviceType;

// Error codes
use libftd2xx_ffi::FT_STATUS;
use libftd2xx_ffi::{
    FT_DEVICE_LIST_NOT_READY, FT_DEVICE_NOT_FOUND, FT_DEVICE_NOT_OPENED,
    FT_DEVICE_NOT_OPENED_FOR_ERASE, FT_DEVICE_NOT_OPENED_FOR_WRITE, FT_EEPROM_ERASE_FAILED,
    FT_EEPROM_NOT_PRESENT, FT_EEPROM_NOT_PROGRAMMED, FT_EEPROM_READ_FAILED, FT_EEPROM_WRITE_FAILED,
    FT_FAILED_TO_WRITE_DEVICE, FT_INSUFFICIENT_RESOURCES, FT_INVALID_ARGS, FT_INVALID_BAUD_RATE,
    FT_INVALID_HANDLE, FT_INVALID_PARAMETER, FT_IO_ERROR, FT_NOT_SUPPORTED, FT_OK, FT_OTHER_ERROR,
};

/// FTDI timeout errors.
///
/// This is used by the [`read`] and [`write`] functions.
///
/// [`read`]: trait.FtdiCommon.html#method.read
/// [`write`]: trait.FtdiCommon.html#method.read
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum TimeoutError {
    /// FTDI status errors.
    FtStatus(FtStatus),
    /// Timeout errors.
    Timeout {
        /// Number of bytes read or written.
        actual: usize,
        /// Number of bytes that attempted to read or write.
        expected: usize,
    },
}
impl Error for TimeoutError {}

impl fmt::Display for TimeoutError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            TimeoutError::FtStatus(status) => write!(f, "{}", status),
            TimeoutError::Timeout { actual, expected } => {
                write!(f, "IO Timeout {}/{} Bytes", actual, expected)
            }
        }
    }
}

impl From<FtStatus> for TimeoutError {
    fn from(value: FtStatus) -> TimeoutError {
        TimeoutError::FtStatus(value)
    }
}

#[test]
fn timeout_error_display() {
    assert_eq!(
        format!("{}", TimeoutError::FtStatus(FtStatus::IO_ERROR)),
        "FtStatus::IO_ERROR"
    );
    assert_eq!(
        format!("{:?}", TimeoutError::FtStatus(FtStatus::IO_ERROR)),
        "FtStatus(IO_ERROR)"
    );
    let to = TimeoutError::Timeout {
        expected: 1,
        actual: 0,
    };
    assert_eq!(format!("{}", to), "IO Timeout 0/1 Bytes");
    assert_eq!(format!("{:?}", to), "Timeout { actual: 0, expected: 1 }");
}

/// Device type errors.
///
/// This is the error used by `TryFrom` trait for FTDI devices.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum DeviceTypeError {
    /// FTDI status errors.
    FtStatus(FtStatus),
    /// Device type errors.
    DeviceType {
        /// Expected device type.
        expected: DeviceType,
        /// Detected device type.
        detected: DeviceType,
    },
}
impl Error for DeviceTypeError {}

impl fmt::Display for DeviceTypeError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DeviceTypeError::FtStatus(status) => write!(f, "{}", status),
            DeviceTypeError::DeviceType { expected, detected } => write!(
                f,
                "Device mismatch, expected {:?}, detected {:?}",
                expected, detected
            ),
        }
    }
}

impl From<FtStatus> for DeviceTypeError {
    fn from(value: FtStatus) -> DeviceTypeError {
        DeviceTypeError::FtStatus(value)
    }
}

#[test]
fn device_type_error_display() {
    assert_eq!(
        format!("{}", DeviceTypeError::FtStatus(FtStatus::IO_ERROR)),
        "FtStatus::IO_ERROR"
    );
    assert_eq!(
        format!("{:?}", DeviceTypeError::FtStatus(FtStatus::IO_ERROR)),
        "FtStatus(IO_ERROR)"
    );
    let dt = DeviceTypeError::DeviceType {
        expected: DeviceType::FT232H,
        detected: DeviceType::FT4232H,
    };
    assert_eq!(
        format!("{}", dt),
        "Device mismatch, expected FT232H, detected FT4232H"
    );
    assert_eq!(
        format!("{:?}", dt),
        "DeviceType { expected: FT232H, detected: FT4232H }"
    );
}

// These get around an annoyance with bindgen generating different types for
// preprocessor macros on Linux vs Windows.
const OK: FT_STATUS = FT_OK as FT_STATUS;
const INVALID_HANDLE: FT_STATUS = FT_INVALID_HANDLE as FT_STATUS;
const DEVICE_NOT_FOUND: FT_STATUS = FT_DEVICE_NOT_FOUND as FT_STATUS;
const DEVICE_NOT_OPENED: FT_STATUS = FT_DEVICE_NOT_OPENED as FT_STATUS;
const IO_ERROR: FT_STATUS = FT_IO_ERROR as FT_STATUS;
const INSUFFICIENT_RESOURCES: FT_STATUS = FT_INSUFFICIENT_RESOURCES as FT_STATUS;
const INVALID_PARAMETER: FT_STATUS = FT_INVALID_PARAMETER as FT_STATUS;
const INVALID_BAUD_RATE: FT_STATUS = FT_INVALID_BAUD_RATE as FT_STATUS;
const DEVICE_NOT_OPENED_FOR_ERASE: FT_STATUS = FT_DEVICE_NOT_OPENED_FOR_ERASE as FT_STATUS;
const DEVICE_NOT_OPENED_FOR_WRITE: FT_STATUS = FT_DEVICE_NOT_OPENED_FOR_WRITE as FT_STATUS;
const FAILED_TO_WRITE_DEVICE: FT_STATUS = FT_FAILED_TO_WRITE_DEVICE as FT_STATUS;
const EEPROM_READ_FAILED: FT_STATUS = FT_EEPROM_READ_FAILED as FT_STATUS;
const EEPROM_WRITE_FAILED: FT_STATUS = FT_EEPROM_WRITE_FAILED as FT_STATUS;
const EEPROM_ERASE_FAILED: FT_STATUS = FT_EEPROM_ERASE_FAILED as FT_STATUS;
const EEPROM_NOT_PRESENT: FT_STATUS = FT_EEPROM_NOT_PRESENT as FT_STATUS;
const EEPROM_NOT_PROGRAMMED: FT_STATUS = FT_EEPROM_NOT_PROGRAMMED as FT_STATUS;
const INVALID_ARGS: FT_STATUS = FT_INVALID_ARGS as FT_STATUS;
const NOT_SUPPORTED: FT_STATUS = FT_NOT_SUPPORTED as FT_STATUS;
const OTHER_ERROR: FT_STATUS = FT_OTHER_ERROR as FT_STATUS;
const DEVICE_LIST_NOT_READY: FT_STATUS = FT_DEVICE_LIST_NOT_READY as FT_STATUS;

/// These are the C API error codes.
///
/// Unfortunately there are provided in the C API as self documenting,
/// which they are for the most part.
///
/// This is the most common error in this crate, majority of functions and
/// methods will return this in the `Result`.
///
/// This is also used in the [`TimeoutError`], and [`DeviceTypeError`]
/// enumerations.
///
/// [`DeviceTypeError`]: ./enum.DeviceTypeError.html
/// [`TimeoutError`]: ./enum.TimeoutError.html
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[allow(non_camel_case_types, missing_docs)]
#[repr(u32)]
pub enum FtStatus {
    INVALID_HANDLE = INVALID_HANDLE,
    DEVICE_NOT_FOUND = DEVICE_NOT_FOUND,
    DEVICE_NOT_OPENED = DEVICE_NOT_OPENED,
    IO_ERROR = IO_ERROR,
    INSUFFICIENT_RESOURCES = INSUFFICIENT_RESOURCES,
    INVALID_PARAMETER = INVALID_PARAMETER,
    INVALID_BAUD_RATE = INVALID_BAUD_RATE,
    DEVICE_NOT_OPENED_FOR_ERASE = DEVICE_NOT_OPENED_FOR_ERASE,
    DEVICE_NOT_OPENED_FOR_WRITE = DEVICE_NOT_OPENED_FOR_WRITE,
    FAILED_TO_WRITE_DEVICE = FAILED_TO_WRITE_DEVICE,
    EEPROM_READ_FAILED = EEPROM_READ_FAILED,
    EEPROM_WRITE_FAILED = EEPROM_WRITE_FAILED,
    EEPROM_ERASE_FAILED = EEPROM_ERASE_FAILED,
    EEPROM_NOT_PRESENT = EEPROM_NOT_PRESENT,
    EEPROM_NOT_PROGRAMMED = EEPROM_NOT_PROGRAMMED,
    INVALID_ARGS = INVALID_ARGS,
    NOT_SUPPORTED = NOT_SUPPORTED,
    OTHER_ERROR = OTHER_ERROR,
    DEVICE_LIST_NOT_READY = DEVICE_LIST_NOT_READY,
}

impl From<FT_STATUS> for FtStatus {
    fn from(x: FT_STATUS) -> FtStatus {
        match x {
            OK => panic!("OK is not an error status"),
            INVALID_HANDLE => FtStatus::INVALID_HANDLE,
            DEVICE_NOT_FOUND => FtStatus::DEVICE_NOT_FOUND,
            DEVICE_NOT_OPENED => FtStatus::DEVICE_NOT_OPENED,
            IO_ERROR => FtStatus::IO_ERROR,
            INSUFFICIENT_RESOURCES => FtStatus::INSUFFICIENT_RESOURCES,
            INVALID_PARAMETER => FtStatus::INVALID_PARAMETER,
            INVALID_BAUD_RATE => FtStatus::INVALID_BAUD_RATE,
            DEVICE_NOT_OPENED_FOR_ERASE => FtStatus::DEVICE_NOT_OPENED_FOR_ERASE,
            DEVICE_NOT_OPENED_FOR_WRITE => FtStatus::DEVICE_NOT_OPENED_FOR_WRITE,
            FAILED_TO_WRITE_DEVICE => FtStatus::FAILED_TO_WRITE_DEVICE,
            EEPROM_READ_FAILED => FtStatus::EEPROM_READ_FAILED,
            EEPROM_WRITE_FAILED => FtStatus::EEPROM_WRITE_FAILED,
            EEPROM_ERASE_FAILED => FtStatus::EEPROM_ERASE_FAILED,
            EEPROM_NOT_PRESENT => FtStatus::EEPROM_NOT_PRESENT,
            EEPROM_NOT_PROGRAMMED => FtStatus::EEPROM_NOT_PROGRAMMED,
            INVALID_ARGS => FtStatus::INVALID_ARGS,
            NOT_SUPPORTED => FtStatus::NOT_SUPPORTED,
            OTHER_ERROR => FtStatus::OTHER_ERROR,
            DEVICE_LIST_NOT_READY => FtStatus::DEVICE_LIST_NOT_READY,
            _ => panic!("invalid FT_STATUS value: {}", x),
        }
    }
}

impl fmt::Display for FtStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "FtStatus::{:?}", self)
    }
}

impl Error for FtStatus {}

#[test]
fn ft_status_display() {
    assert_eq!(format!("{}", FtStatus::IO_ERROR), "FtStatus::IO_ERROR");
    assert_eq!(format!("{:?}", FtStatus::IO_ERROR), "IO_ERROR");
}

/// EEPROM value error.
///
/// This is used in the `TryFrom` trait implementation for these EEPROM enums:
///
/// * [`Cbus232h`]
/// * [`Cbus232r`]
/// * [`CbusX`]
/// * [`DriveCurrent`]
/// * [`DriverType`]
///
/// Some EEPROM values, such as the [`DriveCurrent`] have a fixed range of valid
/// values.  However, the EEPROM may not be programmed with valid values.
///
/// [`Cbus232h`]: ./enum.Cbus232h.html
/// [`Cbus232r`]: ./enum.Cbus232r.html
/// [`CbusX`]: ./enum.CbusX.html
/// [`DriveCurrent`]: ./enum.DriveCurrent.html
/// [`DriverType`]: ./enum.DriverType.html
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct EepromValueError {
    /// Invalid value.
    pub value: u8,
}

impl EepromValueError {
    /// Create a new `EepromValueError`.
    pub fn new(value: u8) -> EepromValueError {
        EepromValueError { value }
    }
}

impl fmt::Display for EepromValueError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Invalid value for this EEPROM field: {}", self.value)
    }
}

impl Error for EepromValueError {}

#[test]
fn drive_current_error_display() {
    assert_eq!(
        format!("{}", EepromValueError::new(1)),
        "Invalid value for this EEPROM field: 1"
    );
    assert_eq!(
        format!("{:?}", EepromValueError::new(1)),
        "EepromValueError { value: 1 }"
    );
}

/// EEPROM strings error.
///
/// This error is used by `set_manufacturer`, `set_manufacturer_id`,
/// `set_description`, and `set_serial_number` methods on EEPROM structures when
/// the length of these strings exceeds the maximums.
///
/// There are two limits to these strings:
///
/// * Less than or equal to 64 characters for each individual string.
/// * The total length of the `manufacturer`, `manufacturer_id`,
///   `description`, and `serial_number` strings can not exceed
///   96 characters.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
pub struct EepromStringsError {
    /// Manufacturer string length.
    pub manufacturer: usize,
    /// Manufacturer ID string length.
    pub manufacturer_id: usize,
    /// Description string length.
    pub description: usize,
    /// Serial number string length.
    pub serial_number: usize,
}

impl EepromStringsError {
    /// Maximum length per string.
    pub const MAX_LEN: usize = 64;
    /// Maximum total string length.
    pub const MAX_TOTAL_LEN: usize = 96;
    /// Total length of all the strings.
    pub fn total_len(self) -> usize {
        self.manufacturer + self.manufacturer_id + self.description + self.serial_number
    }
}

impl fmt::Display for EepromStringsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "EEPROM strings exceed limits \
        manufacturer {}/{} \
        manufacturer_id {}/{} \
        description {}/{} \
        serial_number {}/{} \
        total {}/{}",
            self.manufacturer,
            EepromStringsError::MAX_LEN,
            self.manufacturer_id,
            EepromStringsError::MAX_LEN,
            self.description,
            EepromStringsError::MAX_LEN,
            self.serial_number,
            EepromStringsError::MAX_LEN,
            self.total_len(),
            EepromStringsError::MAX_TOTAL_LEN,
        )
    }
}