1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <fcntl.h>
17 #include <fstream>
18 #include <iostream>
19 #include <sstream>
20 #include <hdf_base.h>
21 #include "hdf_log.h"
22 #include "ddk_sysfs_dev_node.h"
23
24 #define HDF_LOG_TAG ddk_sysfs_dev_node
25
26 const std::filesystem::path SysfsDevNode::devDir_("/sys/bus/usb/devices");
27 const std::regex SysfsDevNode::intfPathRegex_("([0-9]+)-([^:]+):[0-9]+\\.([0-9]+)");
28
29 const int GROUP_ONE = 1;
30 const int GROUP_TWO = 2;
31 const int GROUP_THREE = 3;
32
SysfsDevNode(uint32_t busNum,uint32_t devNum,uint8_t intfNum,const std::string & prefix)33 SysfsDevNode::SysfsDevNode(uint32_t busNum, uint32_t devNum, uint8_t intfNum, const std::string& prefix) noexcept
34 {
35 busNum_ = busNum;
36 devNum_ = devNum;
37 intfNum_ = intfNum;
38 prefix_ = prefix;
39 fileNameRegex_ = std::regex("^" + prefix_ + "\\d+$");
40 }
41
FindPath(std::string & devNodePath)42 int32_t SysfsDevNode::FindPath(std::string& devNodePath)
43 {
44 std::error_code errorCode;
45 for (const auto& entry : fs::directory_iterator(devDir_, errorCode)) {
46 if (errorCode) {
47 HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), entry.path().c_str());
48 errorCode.clear();
49 continue;
50 }
51
52 if (!fs::is_directory(entry, errorCode)) {
53 if (errorCode) {
54 HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), entry.path().c_str());
55 errorCode.clear();
56 }
57 continue;
58 }
59
60 std::string intfDirName = entry.path().filename().string();
61 std::smatch match;
62 if (!std::regex_match(intfDirName, match, intfPathRegex_)) {
63 continue;
64 }
65
66 if (match.str(GROUP_ONE) != std::to_string(busNum_) || match.str(GROUP_THREE) != std::to_string(intfNum_)) {
67 continue;
68 }
69
70 auto devPath = std::filesystem::path(devDir_).append(match.str(GROUP_ONE) + "-" + match.str(GROUP_TWO))
71 .append("devnum");
72 std::optional<std::string> devNumInfo = GetContent(devPath);
73 if (!devNumInfo.has_value() || devNumInfo.value().find(std::to_string(devNum_)) == std::string::npos) {
74 continue;
75 }
76
77 for (fs::recursive_directory_iterator iter(entry, errorCode), end; iter != end; ++iter) {
78 if (errorCode) {
79 HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), iter->path().string().c_str());
80 errorCode.clear();
81 continue;
82 }
83
84 std::string filename = iter->path().filename().string();
85 if (std::regex_match(filename, fileNameRegex_)) {
86 devNodePath = "/dev/" + filename;
87 return HDF_SUCCESS;
88 }
89 }
90 }
91
92 return HDF_FAILURE;
93 }
94
GetContent(const std::string & filePath)95 std::optional<std::string> SysfsDevNode::GetContent(const std::string& filePath)
96 {
97 char path[PATH_MAX] = {'\0'};
98 if (realpath(filePath.c_str(), path) == NULL) {
99 HDF_LOGE("File %{public}s is invalid", filePath.c_str());
100 return std::nullopt;
101 }
102 std::ifstream file(path);
103 if (!file.is_open()) {
104 return std::nullopt;
105 }
106
107 std::stringstream buffer;
108 buffer << file.rdbuf();
109 file.close();
110 return buffer.str();
111 }
112