• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <brillo/udev/udev_device.h>
6 
7 #include <libudev.h>
8 
9 #include <base/logging.h>
10 
11 namespace brillo {
12 
UdevDevice()13 UdevDevice::UdevDevice() : device_(nullptr) {}
14 
UdevDevice(udev_device * device)15 UdevDevice::UdevDevice(udev_device* device) : device_(device) {
16   CHECK(device_);
17 
18   udev_device_ref(device_);
19 }
20 
~UdevDevice()21 UdevDevice::~UdevDevice() {
22   if (device_) {
23     udev_device_unref(device_);
24     device_ = nullptr;
25   }
26 }
27 
GetParent() const28 std::unique_ptr<UdevDevice> UdevDevice::GetParent() const {
29   // udev_device_get_parent does not increase the reference count of the
30   // returned udev_device struct.
31   udev_device* parent_device = udev_device_get_parent(device_);
32   return parent_device ? std::make_unique<UdevDevice>(parent_device) : nullptr;
33 }
34 
GetParentWithSubsystemDeviceType(const char * subsystem,const char * device_type) const35 std::unique_ptr<UdevDevice> UdevDevice::GetParentWithSubsystemDeviceType(
36     const char* subsystem, const char* device_type) const {
37   // udev_device_get_parent_with_subsystem_devtype does not increase the
38   // reference count of the returned udev_device struct.
39   udev_device* parent_device = udev_device_get_parent_with_subsystem_devtype(
40       device_, subsystem, device_type);
41   return parent_device ? std::make_unique<UdevDevice>(parent_device) : nullptr;
42 }
43 
IsInitialized() const44 bool UdevDevice::IsInitialized() const {
45   return udev_device_get_is_initialized(device_);
46 }
47 
GetMicrosecondsSinceInitialized() const48 uint64_t UdevDevice::GetMicrosecondsSinceInitialized() const {
49   return udev_device_get_usec_since_initialized(device_);
50 }
51 
GetSequenceNumber() const52 uint64_t UdevDevice::GetSequenceNumber() const {
53   return udev_device_get_seqnum(device_);
54 }
55 
GetDevicePath() const56 const char* UdevDevice::GetDevicePath() const {
57   return udev_device_get_devpath(device_);
58 }
59 
GetDeviceNode() const60 const char* UdevDevice::GetDeviceNode() const {
61   return udev_device_get_devnode(device_);
62 }
63 
GetDeviceNumber() const64 dev_t UdevDevice::GetDeviceNumber() const {
65   return udev_device_get_devnum(device_);
66 }
67 
GetDeviceType() const68 const char* UdevDevice::GetDeviceType() const {
69   return udev_device_get_devtype(device_);
70 }
71 
GetDriver() const72 const char* UdevDevice::GetDriver() const {
73   return udev_device_get_driver(device_);
74 }
75 
GetSubsystem() const76 const char* UdevDevice::GetSubsystem() const {
77   return udev_device_get_subsystem(device_);
78 }
79 
GetSysPath() const80 const char* UdevDevice::GetSysPath() const {
81   return udev_device_get_syspath(device_);
82 }
83 
GetSysName() const84 const char* UdevDevice::GetSysName() const {
85   return udev_device_get_sysname(device_);
86 }
87 
GetSysNumber() const88 const char* UdevDevice::GetSysNumber() const {
89   return udev_device_get_sysnum(device_);
90 }
91 
GetAction() const92 const char* UdevDevice::GetAction() const {
93   return udev_device_get_action(device_);
94 }
95 
GetDeviceLinksListEntry() const96 std::unique_ptr<UdevListEntry> UdevDevice::GetDeviceLinksListEntry() const {
97   udev_list_entry* list_entry = udev_device_get_devlinks_list_entry(device_);
98   return list_entry ? std::make_unique<UdevListEntry>(list_entry) : nullptr;
99 }
100 
GetPropertiesListEntry() const101 std::unique_ptr<UdevListEntry> UdevDevice::GetPropertiesListEntry() const {
102   udev_list_entry* list_entry = udev_device_get_properties_list_entry(device_);
103   return list_entry ? std::make_unique<UdevListEntry>(list_entry) : nullptr;
104 }
105 
GetPropertyValue(const char * key) const106 const char* UdevDevice::GetPropertyValue(const char* key) const {
107   return udev_device_get_property_value(device_, key);
108 }
109 
GetTagsListEntry() const110 std::unique_ptr<UdevListEntry> UdevDevice::GetTagsListEntry() const {
111   udev_list_entry* list_entry = udev_device_get_tags_list_entry(device_);
112   return list_entry ? std::make_unique<UdevListEntry>(list_entry) : nullptr;
113 }
114 
GetSysAttributeListEntry() const115 std::unique_ptr<UdevListEntry> UdevDevice::GetSysAttributeListEntry() const {
116   udev_list_entry* list_entry = udev_device_get_sysattr_list_entry(device_);
117   return list_entry ? std::make_unique<UdevListEntry>(list_entry) : nullptr;
118 }
119 
GetSysAttributeValue(const char * attribute) const120 const char* UdevDevice::GetSysAttributeValue(const char* attribute) const {
121   return udev_device_get_sysattr_value(device_, attribute);
122 }
123 
Clone()124 std::unique_ptr<UdevDevice> UdevDevice::Clone() {
125   return std::make_unique<UdevDevice>(device_);
126 }
127 
128 }  // namespace brillo
129