• 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 "usb_driver_manager.h"
17 #include <cstdint>
18 #include <sstream>
19 #include "hdf_log.h"
20 #include "ipc_skeleton.h"
21 #include "usbd_wrapper.h"
22 
23 #define HDF_LOG_TAG usb_driver_manager
24 
25 namespace OHOS {
26 namespace HDI {
27 namespace Usb {
28 namespace Ddk {
29 namespace V1_1 {
GetInstance(void)30 UsbDriverManager& UsbDriverManager::GetInstance(void)
31 {
32     static UsbDriverManager instance;
33     return instance;
34 }
35 
ConvertDriverUid2TokenId(const std::string & driverUid,uint32_t & tokenId)36 static bool ConvertDriverUid2TokenId(const std::string &driverUid, uint32_t &tokenId)
37 {
38     std::stringstream ss(driverUid);
39     std::string part;
40 
41     if (std::getline(ss, part, '-')) {
42         if (!(ss >> tokenId)) {
43             HDF_LOGE("%{public}s: Failed to extract a valid uint32_t after the delimiter", __func__);
44             return false;
45         }
46     } else {
47         HDF_LOGE("%{public}s: Delimiter '-' not found in the string.", __func__);
48         return false;
49     }
50     return true;
51 }
52 
UpdateDriverInfo(const DriverAbilityInfo & driverInfo)53 bool UsbDriverManager::UpdateDriverInfo(const DriverAbilityInfo &driverInfo)
54 {
55     uint32_t tokenId;
56     if (!ConvertDriverUid2TokenId(driverInfo.driverUid, tokenId)) {
57         HDF_LOGE("%{public}s: convert failed, driverUid:%{public}s", __func__, driverInfo.driverUid.c_str());
58         return false;
59     }
60 
61     std::lock_guard<std::mutex> lock(mutex_);
62     driverMap_[tokenId] = std::make_unique<DriverAbilityInfo>(driverInfo);
63     return true;
64 }
65 
RemoveDriverInfo(const std::string & driverUid)66 bool UsbDriverManager::RemoveDriverInfo(const std::string &driverUid)
67 {
68     uint32_t tokenId;
69     if (!ConvertDriverUid2TokenId(driverUid, tokenId)) {
70         HDF_LOGE("%{public}s: convert failed, driverUid:%{public}s", __func__, driverUid.c_str());
71         return false;
72     }
73 
74     std::lock_guard<std::mutex> lock(mutex_);
75     auto it = driverMap_.find(tokenId);
76     if (it != driverMap_.end()) {
77         driverMap_.erase(tokenId);
78     }
79     return true;
80 }
81 
QueryDriverInfo(uint32_t tokenId,DriverAbilityInfo & driverInfo)82 bool UsbDriverManager::QueryDriverInfo(uint32_t tokenId, DriverAbilityInfo &driverInfo)
83 {
84     std::lock_guard<std::mutex> lock(mutex_);
85     auto it = driverMap_.find(tokenId);
86     if (it != driverMap_.end() && it->second != nullptr) {
87         driverInfo = *(it->second);
88         return true;
89     }
90     return false;
91 }
92 } // namespace V1_1
93 } // namespace Ddk
94 } // namespace Usb
95 } // namespace HDI
96 } // namespace OHOS