• 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         std::optional<std::string> devNumInfo = GetContent(devPath);
72         if (!devNumInfo.has_value() || devNumInfo.value().find(std::to_string(devNum_)) == std::string::npos) {
73             continue;
74         }
75 
76         for (fs::recursive_directory_iterator iter(entry, errorCode), end; iter != end; ++iter) {
77             if (errorCode) {
78                 HDF_LOGE("Error: %{public}s at %{public}s", errorCode.message().c_str(), iter->path().string().c_str());
79                 errorCode.clear();
80                 continue;
81             }
82 
83             std::string filename = iter->path().filename().string();
84             if (std::regex_match(filename, fileNameRegex_)) {
85                 devNodePath = "/dev/" + filename;
86                 return HDF_SUCCESS;
87             }
88         }
89     }
90 
91     return HDF_FAILURE;
92 }
93 
GetContent(const std::string & filePath)94 std::optional<std::string> SysfsDevNode::GetContent(const std::string& filePath)
95 {
96     std::ifstream file(filePath);
97     if (!file.is_open()) {
98         return std::nullopt;
99     }
100 
101     std::stringstream buffer;
102     buffer << file.rdbuf();
103     file.close();
104     return buffer.str();
105 }
106