• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fstream>
17 #include <iostream>
18 #include <sstream>
19 #include <hdf_base.h>
20 #include "input_uhdf_log.h"
21 #include "ddk_sysfs_dev_node.h"
22 
23 #define HDF_LOG_TAG      ddk_sysfs_dev_node
24 
25 const std::filesystem::path SysfsDevNode::devDir_("/sys/bus/usb/devices");
26 const std::regex SysfsDevNode::intfPathRegex_("([0-9]+)-([^:]+):[0-9]+\\.([0-9]+)");
27 
28 const int GROUP_ONE = 1;
29 const int GROUP_TWO = 2;
30 const int GROUP_THREE = 3;
31 
SysfsDevNode(uint32_t busNum,uint32_t devNum,uint8_t intfNum,const std::string & prefix)32 SysfsDevNode::SysfsDevNode(uint32_t busNum, uint32_t devNum, uint8_t intfNum, const std::string& prefix) noexcept
33 {
34     busNum_ = busNum;
35     devNum_ = devNum;
36     intfNum_ = intfNum;
37     prefix_ = prefix;
38     fileNameRegex_ = std::regex("^" + prefix_ + "\\d+$");
39 }
40 
FindPath(std::string & devNodePath)41 int32_t SysfsDevNode::FindPath(std::string& devNodePath)
42 {
43     std::error_code errorCode;
44     for (const auto& entry : fs::directory_iterator(devDir_, errorCode)) {
45         if (errorCode) {
46             HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), entry.path().c_str());
47             errorCode.clear();
48             continue;
49         }
50 
51         if (!fs::is_directory(entry, errorCode)) {
52             if (errorCode) {
53                 HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), entry.path().c_str());
54                 errorCode.clear();
55             }
56             continue;
57         }
58 
59         std::string intfDirName = entry.path().filename().string();
60         std::smatch match;
61         if (!std::regex_match(intfDirName, match, intfPathRegex_)) {
62             continue;
63         }
64 
65         if (match.str(GROUP_ONE) != std::to_string(busNum_) || match.str(GROUP_THREE) != std::to_string(intfNum_)) {
66             continue;
67         }
68 
69         auto devPath = std::filesystem::path(devDir_).append(match.str(GROUP_ONE) + "-" + match.str(GROUP_TWO))
70             .append("devnum");
71         char realpathStr[PATH_MAX] = {'\0'};
72         if (realpath(devPath.c_str(), realpathStr) == nullptr) {
73             HDF_LOGE("Realpath failed, errno: %{public}s", strerror(errno));
74             continue;
75         }
76         std::optional<std::string> devNumInfo = GetContent(realpathStr);
77         if (!devNumInfo.has_value() || devNumInfo.value().find(std::to_string(devNum_)) == std::string::npos) {
78             continue;
79         }
80 
81         for (fs::recursive_directory_iterator iter(entry, errorCode), end; iter != end; ++iter) {
82             if (errorCode) {
83                 HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), iter->path().string().c_str());
84                 errorCode.clear();
85                 continue;
86             }
87 
88             std::string filename = iter->path().filename().string();
89             if (std::regex_match(filename, fileNameRegex_)) {
90                 devNodePath = "/dev/" + filename;
91                 return HDF_SUCCESS;
92             }
93         }
94     }
95 
96     return HDF_FAILURE;
97 }
98 
GetContent(const std::string & filePath)99 std::optional<std::string> SysfsDevNode::GetContent(const std::string& filePath)
100 {
101     std::ifstream file(filePath);
102     if (!file.is_open()) {
103         return std::nullopt;
104     }
105 
106     std::stringstream buffer;
107     buffer << file.rdbuf();
108     file.close();
109     return buffer.str();
110 }
111