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_enumerate.h>
6
7 #include <libudev.h>
8
9 #include <base/logging.h>
10 #include <base/strings/stringprintf.h>
11 #include <brillo/udev/udev_device.h>
12
13 using base::StringPrintf;
14
15 namespace brillo {
16
UdevEnumerate()17 UdevEnumerate::UdevEnumerate() : enumerate_(nullptr) {}
18
UdevEnumerate(udev_enumerate * enumerate)19 UdevEnumerate::UdevEnumerate(udev_enumerate* enumerate)
20 : enumerate_(enumerate) {
21 CHECK(enumerate_);
22
23 udev_enumerate_ref(enumerate_);
24 }
25
~UdevEnumerate()26 UdevEnumerate::~UdevEnumerate() {
27 if (enumerate_) {
28 udev_enumerate_unref(enumerate_);
29 enumerate_ = nullptr;
30 }
31 }
32
AddMatchSubsystem(const char * subsystem)33 bool UdevEnumerate::AddMatchSubsystem(const char* subsystem) {
34 int result = udev_enumerate_add_match_subsystem(enumerate_, subsystem);
35 if (result == 0)
36 return true;
37
38 VLOG(2) << StringPrintf(
39 "udev_enumerate_add_match_subsystem (%p, \"%s\") returned %d.",
40 enumerate_, subsystem, result);
41 return false;
42 }
43
AddNoMatchSubsystem(const char * subsystem)44 bool UdevEnumerate::AddNoMatchSubsystem(const char* subsystem) {
45 int result = udev_enumerate_add_nomatch_subsystem(enumerate_, subsystem);
46 if (result == 0)
47 return true;
48
49 VLOG(2) << StringPrintf(
50 "udev_enumerate_add_nomatch_subsystem (%p, \"%s\") returned %d.",
51 enumerate_, subsystem, result);
52 return false;
53 }
54
AddMatchSysAttribute(const char * attribute,const char * value)55 bool UdevEnumerate::AddMatchSysAttribute(const char* attribute,
56 const char* value) {
57 int result = udev_enumerate_add_match_sysattr(enumerate_, attribute, value);
58 if (result == 0)
59 return true;
60
61 VLOG(2) << StringPrintf(
62 "udev_enumerate_add_match_sysattr (%p, \"%s\", \"%s\") returned %d.",
63 enumerate_, attribute, value, result);
64 return false;
65 }
66
AddNoMatchSysAttribute(const char * attribute,const char * value)67 bool UdevEnumerate::AddNoMatchSysAttribute(const char* attribute,
68 const char* value) {
69 int result = udev_enumerate_add_nomatch_sysattr(enumerate_, attribute, value);
70 if (result == 0)
71 return true;
72
73 VLOG(2) << StringPrintf(
74 "udev_enumerate_add_nomatch_sysattr (%p, \"%s\", \"%s\") returned %d.",
75 enumerate_, attribute, value, result);
76 return false;
77 }
78
AddMatchProperty(const char * property,const char * value)79 bool UdevEnumerate::AddMatchProperty(const char* property, const char* value) {
80 int result = udev_enumerate_add_match_property(enumerate_, property, value);
81 if (result == 0)
82 return true;
83
84 VLOG(2) << StringPrintf(
85 "udev_enumerate_add_match_property (%p, \"%s\", \"%s\") returned %d.",
86 enumerate_, property, value, result);
87 return false;
88 }
89
AddMatchSysName(const char * sys_name)90 bool UdevEnumerate::AddMatchSysName(const char* sys_name) {
91 int result = udev_enumerate_add_match_sysname(enumerate_, sys_name);
92 if (result == 0)
93 return true;
94
95 VLOG(2) << StringPrintf(
96 "udev_enumerate_add_match_sysname (%p, \"%s\") returned %d.", enumerate_,
97 sys_name, result);
98 return false;
99 }
100
AddMatchTag(const char * tag)101 bool UdevEnumerate::AddMatchTag(const char* tag) {
102 int result = udev_enumerate_add_match_tag(enumerate_, tag);
103 if (result == 0)
104 return true;
105
106 VLOG(2) << StringPrintf(
107 "udev_enumerate_add_match_tag (%p, \"%s\") returned %d.", enumerate_, tag,
108 result);
109 return false;
110 }
111
AddMatchIsInitialized()112 bool UdevEnumerate::AddMatchIsInitialized() {
113 int result = udev_enumerate_add_match_is_initialized(enumerate_);
114 if (result == 0)
115 return true;
116
117 VLOG(2) << StringPrintf(
118 "udev_enumerate_add_match_is_initialized (%p) returned %d.", enumerate_,
119 result);
120 return false;
121 }
122
AddSysPath(const char * sys_path)123 bool UdevEnumerate::AddSysPath(const char* sys_path) {
124 int result = udev_enumerate_add_syspath(enumerate_, sys_path);
125 if (result == 0)
126 return true;
127
128 VLOG(2) << StringPrintf("udev_enumerate_add_syspath(%p, \"%s\") returned %d.",
129 enumerate_, sys_path, result);
130 return false;
131 }
132
ScanDevices()133 bool UdevEnumerate::ScanDevices() {
134 int result = udev_enumerate_scan_devices(enumerate_);
135 if (result == 0)
136 return true;
137
138 VLOG(2) << StringPrintf("udev_enumerate_scan_devices(%p) returned %d.",
139 enumerate_, result);
140 return false;
141 }
142
ScanSubsystems()143 bool UdevEnumerate::ScanSubsystems() {
144 int result = udev_enumerate_scan_subsystems(enumerate_);
145 if (result == 0)
146 return true;
147
148 VLOG(2) << StringPrintf("udev_enumerate_scan_subsystems(%p) returned %d.",
149 enumerate_, result);
150 return false;
151 }
152
GetListEntry() const153 std::unique_ptr<UdevListEntry> UdevEnumerate::GetListEntry() const {
154 udev_list_entry* list_entry = udev_enumerate_get_list_entry(enumerate_);
155 return list_entry ? std::make_unique<UdevListEntry>(list_entry) : nullptr;
156 }
157
158 } // namespace brillo
159