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
use std::borrow::Cow;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem;

use ::libc::c_void;

/// Types of ports.
#[derive(Debug,PartialEq,Eq,Clone,Copy,Hash)]
pub enum PortType {
    /// Serial port.
    Serial,

    /// USB port.
    USB,

    /// Disk or local mountpoint.
    Disk,

    /// PTP or IP connection.
    PTPIP,

    /// Direct I/O on a USB mass storage device.
    Direct,

    /// USB mass storage raw SCSI port.
    SCSI,

    /// Unknown port type.
    Other,
}

/// A structure describing a port.
///
/// ## Example
///
/// A `Port` object can be used to report information about a camera's connection:
///
/// ```no_run
/// let mut context = gphoto::Context::new().unwrap();
/// let mut camera = gphoto::Camera::autodetect(&mut context).unwrap();
/// let port = camera.port();
///
/// println!("port type = {:?}", port.port_type());
/// println!("port name = {:?}", port.name());
/// println!("port path = {:?}", port.path());
/// ```
///
/// The above example may print something like the following:
///
/// ```text
/// port type = USB
/// port name = "Universal Serial Bus"
/// port path = "usb:020,007"
/// ```
pub struct Port<'a> {
    // GPPortInfo is a typedef for a pointer. Lifetime is needed because it borrows data owned by
    // the Camera struct.
    inner: ::gphoto2::GPPortInfo,
    __phantom: PhantomData<&'a c_void>,
}

impl<'a> Port<'a> {
    /// Returns the type of the port.
    pub fn port_type(&self) -> PortType {
        let mut port_type = unsafe { mem::uninitialized() };

        unsafe {
            assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_port_info_get_type(self.inner, &mut port_type));
        }

        match port_type {
            ::gphoto2::GP_PORT_SERIAL          => PortType::Serial,
            ::gphoto2::GP_PORT_USB             => PortType::USB,
            ::gphoto2::GP_PORT_DISK            => PortType::Disk,
            ::gphoto2::GP_PORT_PTPIP           => PortType::PTPIP,
            ::gphoto2::GP_PORT_USB_DISK_DIRECT => PortType::Direct,
            ::gphoto2::GP_PORT_USB_SCSI        => PortType::SCSI,
            ::gphoto2::GP_PORT_NONE | _        => PortType::Other,
        }
    }

    /// Returns the name of the port.
    pub fn name(&self) -> Cow<str> {
        let mut name = unsafe { mem::uninitialized() };

        unsafe {
            assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_port_info_get_name(self.inner, &mut name));
            String::from_utf8_lossy(CStr::from_ptr(name).to_bytes())
        }
    }

    /// Returns the path of the port.
    pub fn path(&self) -> Cow<str> {
        let mut path = unsafe { mem::uninitialized() };

        unsafe {
            assert_eq!(::gphoto2::GP_OK, ::gphoto2::gp_port_info_get_path(self.inner, &mut path));
            String::from_utf8_lossy(CStr::from_ptr(path).to_bytes())
        }
    }
}

#[doc(hidden)]
pub fn from_libgphoto2<'a>(_camera: &'a ::camera::Camera, ptr: ::gphoto2::GPPortInfo) -> Port<'a> {
    Port {
        inner: ptr,
        __phantom: PhantomData,
    }
}