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()13UdevDevice::UdevDevice() : device_(nullptr) {} 14 UdevDevice(udev_device * device)15UdevDevice::UdevDevice(udev_device* device) : device_(device) { 16 CHECK(device_); 17 18 udev_device_ref(device_); 19 } 20 ~UdevDevice()21UdevDevice::~UdevDevice() { 22 if (device_) { 23 udev_device_unref(device_); 24 device_ = nullptr; 25 } 26 } 27 GetParent() const28std::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) const35std::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() const44bool UdevDevice::IsInitialized() const { 45 return udev_device_get_is_initialized(device_); 46 } 47 GetMicrosecondsSinceInitialized() const48uint64_t UdevDevice::GetMicrosecondsSinceInitialized() const { 49 return udev_device_get_usec_since_initialized(device_); 50 } 51 GetSequenceNumber() const52uint64_t UdevDevice::GetSequenceNumber() const { 53 return udev_device_get_seqnum(device_); 54 } 55 GetDevicePath() const56const char* UdevDevice::GetDevicePath() const { 57 return udev_device_get_devpath(device_); 58 } 59 GetDeviceNode() const60const char* UdevDevice::GetDeviceNode() const { 61 return udev_device_get_devnode(device_); 62 } 63 GetDeviceNumber() const64dev_t UdevDevice::GetDeviceNumber() const { 65 return udev_device_get_devnum(device_); 66 } 67 GetDeviceType() const68const char* UdevDevice::GetDeviceType() const { 69 return udev_device_get_devtype(device_); 70 } 71 GetDriver() const72const char* UdevDevice::GetDriver() const { 73 return udev_device_get_driver(device_); 74 } 75 GetSubsystem() const76const char* UdevDevice::GetSubsystem() const { 77 return udev_device_get_subsystem(device_); 78 } 79 GetSysPath() const80const char* UdevDevice::GetSysPath() const { 81 return udev_device_get_syspath(device_); 82 } 83 GetSysName() const84const char* UdevDevice::GetSysName() const { 85 return udev_device_get_sysname(device_); 86 } 87 GetSysNumber() const88const char* UdevDevice::GetSysNumber() const { 89 return udev_device_get_sysnum(device_); 90 } 91 GetAction() const92const char* UdevDevice::GetAction() const { 93 return udev_device_get_action(device_); 94 } 95 GetDeviceLinksListEntry() const96std::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() const101std::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) const106const char* UdevDevice::GetPropertyValue(const char* key) const { 107 return udev_device_get_property_value(device_, key); 108 } 109 GetTagsListEntry() const110std::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() const115std::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) const120const char* UdevDevice::GetSysAttributeValue(const char* attribute) const { 121 return udev_device_get_sysattr_value(device_, attribute); 122 } 123 Clone()124std::unique_ptr<UdevDevice> UdevDevice::Clone() { 125 return std::make_unique<UdevDevice>(device_); 126 } 127 128 } // namespace brillo 129