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