Debian Codegraph @

third_party/rust/libudev/src/device.rs

raw
1 use std::str;
2
3 use std::ffi::{CStr,OsStr};
4 use std::path::Path;
5 use std::str::FromStr;
6
7 use libc::{c_char,dev_t};
8
9 use ::context::Context;
10 use ::handle::*;
11
12 pub fn new(context: &Context, device: *mut ::ffi::udev_device) -> Device {
13 Device {
14 _context: context,
15 device: device,
16 }
17 }
18
19 /// A structure that provides access to sysfs/kernel devices.
20 pub struct Device<'a> {
21 _context: &'a Context,
22 device: *mut ::ffi::udev_device,
23 }
24
25 impl<'a> Drop for Device<'a> {
26 fn drop(&mut self) {
27 unsafe {
28 ::ffi::udev_device_unref(self.device);
29 }
30 }
31 }
32
33 #[doc(hidden)]
34 impl<'a> Handle<::ffi::udev_device> for Device<'a> {
35 fn as_ptr(&self) -> *mut ::ffi::udev_device {
36 self.device
37 }
38 }
39
40 impl<'a> Device<'a> {
41 /// Checks whether the device has already been handled by udev.
42 ///
43 /// When a new device is connected to the system, udev initializes the device by setting
44 /// permissions, renaming network devices, and possibly other initialization routines. This
45 /// method returns `true` if udev has performed all of its work to initialize this device.
46 ///
47 /// This method only applies to devices with device nodes or network interfaces. All other
48 /// devices return `true` by default.
49 pub fn is_initialized(&self) -> bool {
50 unsafe {
51 ::ffi::udev_device_get_is_initialized(self.device) > 0
52 }
53 }
54
55 /// Gets the device's major/minor number.
56 pub fn devnum(&self) -> Option<dev_t> {
57 match unsafe { ::ffi::udev_device_get_devnum(self.device) } {
58 0 => None,
59 n => Some(n)
60 }
61 }
62
63 /// Returns the syspath of the device.
64 ///
65 /// The path is an absolute path and includes the sys mount point. For example, the syspath for
66 /// `tty0` could be `/sys/devices/virtual/tty/tty0`, which includes the sys mount point,
67 /// `/sys`.
68 pub fn syspath(&self) -> &Path {
69 Path::new(unsafe {
70 ::util::ptr_to_os_str_unchecked(::ffi::udev_device_get_syspath(self.device))
71 })
72 }
73
74 /// Returns the kernel devpath value of the device.
75 ///
76 /// The path does not contain the sys mount point, but does start with a `/`. For example, the
77 /// devpath for `tty0` could be `/devices/virtual/tty/tty0`.
78 pub fn devpath(&self) -> &OsStr {
79 unsafe {
80 ::util::ptr_to_os_str_unchecked(::ffi::udev_device_get_devpath(self.device))
81 }
82 }
83
84 /// Returns the path to the device node belonging to the device.
85 ///
86 /// The path is an absolute path and starts with the device directory. For example, the device
87 /// node for `tty0` could be `/dev/tty0`.
88 pub fn devnode(&self) -> Option<&Path> {
89 ::util::ptr_to_os_str(unsafe { ::ffi::udev_device_get_devnode(self.device) }).map(|path| {
90 Path::new(path)
91 })
92 }
93
94 /// Returns the parent of the device.
95 pub fn parent(&self) -> Option<Device> {
96 let ptr = unsafe { ::ffi::udev_device_get_parent(self.device) };
97
98 if !ptr.is_null() {
99 unsafe {
100 ::ffi::udev_device_ref(ptr);
101 }
102
103 Some(Device {
104 _context: self._context,
105 device: ptr,
106 })
107 }
108 else {
109 None
110 }
111 }
112
113 /// Returns the subsystem name of the device.
114 ///
115 /// The subsystem name is a string that indicates which kernel subsystem the device belongs to.
116 /// Examples of subsystem names are `tty`, `vtconsole`, `block`, `scsi`, and `net`.
117 pub fn subsystem(&self) -> &OsStr {
118 unsafe {
119 ::util::ptr_to_os_str_unchecked(::ffi::udev_device_get_subsystem(self.device))
120 }
121 }
122
123 /// Returns the kernel device name for the device.
124 ///
125 /// The sysname is a string that differentiates the device from others in the same subsystem.
126 /// For example, `tty0` is the sysname for a TTY device that differentiates it from others,
127 /// such as `tty1`.
128 pub fn sysname(&self) -> &OsStr {
129 unsafe {
130 ::util::ptr_to_os_str_unchecked(::ffi::udev_device_get_sysname(self.device))
131 }
132 }
133
134 /// Returns the instance number of the device.
135 ///
136 /// The instance number is used to differentiate many devices of the same type. For example,
137 /// `/dev/tty0` and `/dev/tty1` are both TTY devices but have instance numbers of 0 and 1,
138 /// respectively.
139 ///
140 /// Some devices don't have instance numbers, such as `/dev/console`, in which case the method
141 /// returns `None`.
142 pub fn sysnum(&self) -> Option<usize> {
143 let ptr = unsafe { ::ffi::udev_device_get_sysnum(self.device) };
144
145 if !ptr.is_null() {
146 match str::from_utf8(unsafe { CStr::from_ptr(ptr) }.to_bytes()) {
147 Err(_) => None,
148 Ok(s) => FromStr::from_str(s).ok()
149 }
150 }
151 else {
152 None
153 }
154 }
155
156 /// Returns the devtype name of the device.
157 pub fn devtype(&self) -> Option<&OsStr> {
158 ::util::ptr_to_os_str(unsafe { ::ffi::udev_device_get_devtype(self.device) })
159 }
160
161 /// Returns the name of the kernel driver attached to the device.
162 pub fn driver(&self) -> Option<&OsStr> {
163 ::util::ptr_to_os_str(unsafe { ::ffi::udev_device_get_driver(self.device) })
164 }
165
166 /// Retreives the value of a device property.
167 pub fn property_value<T: AsRef<OsStr>>(&self, property: T) -> Option<&OsStr> {
168 let prop = match ::util::os_str_to_cstring(property) {
169 Ok(s) => s,
170 Err(_) => return None
171 };
172
173 ::util::ptr_to_os_str(unsafe {
174 ::ffi::udev_device_get_property_value(self.device, prop.as_ptr())
175 })
176 }
177
178 /// Retreives the value of a device attribute.
179 pub fn attribute_value<T: AsRef<OsStr>>(&self, attribute: T) -> Option<&OsStr> {
180 let attr = match ::util::os_str_to_cstring(attribute) {
181 Ok(s) => s,
182 Err(_) => return None
183 };
184
185 ::util::ptr_to_os_str(unsafe {
186 ::ffi::udev_device_get_sysattr_value(self.device, attr.as_ptr())
187 })
188 }
189
190 /// Sets the value of a device attribute.
191 pub fn set_attribute_value<T: AsRef<OsStr>, U: AsRef<OsStr>>(&mut self, attribute: T, value: U) -> ::Result<()> {
192 let attribute = try!(::util::os_str_to_cstring(attribute));
193 let value = try!(::util::os_str_to_cstring(value));
194
195 ::util::errno_to_result(unsafe {
196 ::ffi::udev_device_set_sysattr_value(self.device, attribute.as_ptr(), value.as_ptr() as *mut c_char)
197 })
198 }
199
200 /// Returns an iterator over the device's properties.
201 ///
202 /// ## Example
203 ///
204 /// This example prints out all of a device's properties:
205 ///
206 /// ```no_run
207 /// # use std::path::Path;
208 /// # let mut context = libudev::Context::new().unwrap();
209 /// # let device = context.device_from_syspath(Path::new("/sys/devices/virtual/tty/tty0")).unwrap();
210 /// for property in device.properties() {
211 /// println!("{:?} = {:?}", property.name(), property.value());
212 /// }
213 /// ```
214 pub fn properties(&self) -> Properties {
215 Properties {
216 _device: self,
217 entry: unsafe { ::ffi::udev_device_get_properties_list_entry(self.device) }
218 }
219 }
220
221 /// Returns an iterator over the device's attributes.
222 ///
223 /// ## Example
224 ///
225 /// This example prints out all of a device's attributes:
226 ///
227 /// ```no_run
228 /// # use std::path::Path;
229 /// # let mut context = libudev::Context::new().unwrap();
230 /// # let device = context.device_from_syspath(Path::new("/sys/devices/virtual/tty/tty0")).unwrap();
231 /// for attribute in device.attributes() {
232 /// println!("{:?} = {:?}", attribute.name(), attribute.value());
233 /// }
234 /// ```
235 pub fn attributes(&self) -> Attributes {
236 Attributes {
237 device: self,
238 entry: unsafe { ::ffi::udev_device_get_sysattr_list_entry(self.device) }
239 }
240 }
241 }
242
243
244 /// Iterator over a device's properties.
245 pub struct Properties<'a> {
246 _device: &'a Device<'a>,
247 entry: *mut ::ffi::udev_list_entry
248 }
249
250 impl<'a> Iterator for Properties<'a> {
251 type Item = Property<'a>;
252
253 fn next(&mut self) -> Option<Property<'a>> {
254 if self.entry.is_null() {
255 None
256 }
257 else {
258 let name = unsafe { ::util::ptr_to_os_str_unchecked(::ffi::udev_list_entry_get_name(self.entry)) };
259 let value = unsafe { ::util::ptr_to_os_str_unchecked(::ffi::udev_list_entry_get_value(self.entry)) };
260
261 self.entry = unsafe { ::ffi::udev_list_entry_get_next(self.entry) };
262
263 Some(Property {
264 name: name,
265 value: value
266 })
267 }
268 }
269
270 fn size_hint(&self) -> (usize, Option<usize>) {
271 (0, None)
272 }
273 }
274
275 /// A device property.
276 pub struct Property<'a> {
277 name: &'a OsStr,
278 value: &'a OsStr
279 }
280
281 impl<'a> Property<'a> {
282 /// Returns the property name.
283 pub fn name(&self) -> &OsStr {
284 self.name
285 }
286
287 /// Returns the property value.
288 pub fn value(&self) -> &OsStr {
289 self.value
290 }
291 }
292
293
294 /// Iterator over a device's attributes.
295 pub struct Attributes<'a> {
296 device: &'a Device<'a>,
297 entry: *mut ::ffi::udev_list_entry
298 }
299
300 impl<'a> Iterator for Attributes<'a> {
301 type Item = Attribute<'a>;
302
303 fn next(&mut self) -> Option<Attribute<'a>> {
304 if !self.entry.is_null() {
305 let name = unsafe { ::util::ptr_to_os_str_unchecked(::ffi::udev_list_entry_get_name(self.entry)) };
306
307 self.entry = unsafe { ::ffi::udev_list_entry_get_next(self.entry) };
308
309 Some(Attribute {
310 device: self.device,
311 name: name
312 })
313 }
314 else {
315 None
316 }
317 }
318
319 fn size_hint(&self) -> (usize, Option<usize>) {
320 (0, None)
321 }
322 }
323
324 /// A device attribute.
325 pub struct Attribute<'a> {
326 device: &'a Device<'a>,
327 name: &'a OsStr
328 }
329
330 impl<'a> Attribute<'a> {
331 /// Returns the attribute name.
332 pub fn name(&self) -> &OsStr {
333 self.name
334 }
335
336 /// Returns the attribute value.
337 pub fn value(&self) -> Option<&OsStr> {
338 self.device.attribute_value(self.name)
339 }
340 }