• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "local_hardware_manager.h"
17 
18 #include <unistd.h>
19 
20 #include "anonymous_string.h"
21 #include "capability_info_manager.h"
22 #include "component_loader.h"
23 #include "constants.h"
24 #include "device_type.h"
25 #include "dh_context.h"
26 #include "dh_utils_hitrace.h"
27 #include "distributed_hardware_errno.h"
28 #include "distributed_hardware_log.h"
29 #include "dh_utils_tool.h"
30 #include "meta_info_manager.h"
31 #include "plugin_listener_impl.h"
32 #include "version_manager.h"
33 
34 namespace OHOS {
35 namespace DistributedHardware {
36 namespace {
37     constexpr int32_t QUERY_INTERVAL_TIME = 1000 * 1000; // 1s
38     constexpr int32_t QUERY_RETRY_MAX_TIMES = 3;
39 }
40 #undef DH_LOG_TAG
41 #define DH_LOG_TAG "LocalHardwareManager"
42 
IMPLEMENT_SINGLE_INSTANCE(LocalHardwareManager)43 IMPLEMENT_SINGLE_INSTANCE(LocalHardwareManager)
44 
45 LocalHardwareManager::LocalHardwareManager() {}
~LocalHardwareManager()46 LocalHardwareManager::~LocalHardwareManager() {}
47 
Init()48 void LocalHardwareManager::Init()
49 {
50     DHLOGI("start");
51     std::lock_guard<std::mutex> lock(localHardwareMgrMutex_);
52     std::vector<DHType> allCompTypes;
53     ComponentLoader::GetInstance().GetAllCompTypes(allCompTypes);
54     int64_t allQueryStartTime = GetCurrentTime();
55     for (auto dhType : allCompTypes) {
56         int64_t singleQueryStartTime = GetCurrentTime();
57         IHardwareHandler *hardwareHandler = nullptr;
58         int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandler);
59         if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) {
60             DHLOGE("GetHardwareHandler %{public}#X failed", dhType);
61             continue;
62         }
63         if (hardwareHandler->Initialize() != DH_FWK_SUCCESS) {
64             DHLOGE("Initialize %{public}#X failed", dhType);
65             continue;
66         }
67 
68         DHQueryTraceStart(dhType);
69         QueryLocalHardware(dhType, hardwareHandler);
70         DHTraceEnd();
71         if (!hardwareHandler->IsSupportPlugin()) {
72             DHLOGI("hardwareHandler is not support hot swap plugin, release!");
73             ComponentLoader::GetInstance().ReleaseHardwareHandler(dhType);
74             hardwareHandler = nullptr;
75         } else {
76             compToolFuncsMap_[dhType] = hardwareHandler;
77             std::shared_ptr<PluginListener> listener = std::make_shared<PluginListenerImpl>(dhType);
78             pluginListenerMap_[dhType] = listener;
79             hardwareHandler->RegisterPluginListener(listener);
80         }
81         int64_t singleQueryEndTime = GetCurrentTime();
82         DHLOGI("query %{public}#X hardware cost time: %{public}" PRIu64 " ms",
83             dhType, singleQueryEndTime - singleQueryStartTime);
84     }
85     int64_t allQueryEndTime = GetCurrentTime();
86     DHLOGI("query all local hardware cost time: %{public}" PRIu64 " ms", allQueryEndTime - allQueryStartTime);
87     std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
88     std::vector<std::shared_ptr<MetaCapabilityInfo>> metaCapInfos;
89     for (const auto &localDHItems : localDHItemsMap_) {
90         AddLocalCapabilityInfo(localDHItems.second, localDHItems.first, capabilityInfos);
91         AddLocalMetaCapInfo(localDHItems.second, localDHItems.first, metaCapInfos);
92     }
93     CapabilityInfoManager::GetInstance()->AddCapability(capabilityInfos);
94     MetaInfoManager::GetInstance()->AddMetaCapInfos(metaCapInfos);
95 }
96 
UnInit()97 void LocalHardwareManager::UnInit()
98 {
99     DHLOGI("start");
100     std::lock_guard<std::mutex> lock(localHardwareMgrMutex_);
101     compToolFuncsMap_.clear();
102     pluginListenerMap_.clear();
103     localDHItemsMap_.clear();
104 }
105 
QueryLocalHardware(const DHType dhType,IHardwareHandler * hardwareHandler)106 void LocalHardwareManager::QueryLocalHardware(const DHType dhType, IHardwareHandler *hardwareHandler)
107 {
108     std::vector<DHItem> dhItems;
109     int32_t retryTimes = QUERY_RETRY_MAX_TIMES;
110     while (retryTimes > 0) {
111         DHLOGI("Query hardwareHandler retry times left: %{public}d, dhType: %{public}#X", retryTimes, dhType);
112         if (hardwareHandler == nullptr) {
113             DHLOGE("hardwareHandler is null.");
114             return;
115         }
116         dhItems = hardwareHandler->Query();
117         if (dhItems.empty()) {
118             DHLOGE("Query hardwareHandler and obtain empty, dhType: %{public}#X", dhType);
119             usleep(QUERY_INTERVAL_TIME);
120         } else {
121             DHLOGI("Query hardwareHandler success, dhType: %{public}#X!, size: %{public}zu", dhType, dhItems.size());
122             /*
123              * Failed to delete data when the device restarts or other exception situation.
124              * So check and remove the non-exist local capabilityInfo.
125              */
126             CheckNonExistCapabilityInfo(dhItems, dhType);
127             localDHItemsMap_[dhType] = dhItems;
128             break;
129         }
130         retryTimes--;
131     }
132 }
133 
AddLocalCapabilityInfo(const std::vector<DHItem> & dhItems,const DHType dhType,std::vector<std::shared_ptr<CapabilityInfo>> & capabilityInfos)134 void LocalHardwareManager::AddLocalCapabilityInfo(const std::vector<DHItem> &dhItems, const DHType dhType,
135     std::vector<std::shared_ptr<CapabilityInfo>> &capabilityInfos)
136 {
137     DHLOGI("start!");
138     std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
139     std::string devName = DHContext::GetInstance().GetDeviceInfo().deviceName;
140     uint16_t devType = DHContext::GetInstance().GetDeviceInfo().deviceType;
141     for (auto dhItem : dhItems) {
142         std::shared_ptr<CapabilityInfo> dhCapabilityInfo = std::make_shared<CapabilityInfo>(
143             dhItem.dhId, deviceId, devName, devType, dhType, dhItem.attrs, dhItem.subtype);
144         capabilityInfos.push_back(dhCapabilityInfo);
145     }
146 }
147 
AddLocalMetaCapInfo(const std::vector<DHItem> & dhItems,const DHType dhType,std::vector<std::shared_ptr<MetaCapabilityInfo>> & metaCapInfos)148 void LocalHardwareManager::AddLocalMetaCapInfo(const std::vector<DHItem> &dhItems, const DHType dhType,
149     std::vector<std::shared_ptr<MetaCapabilityInfo>> &metaCapInfos)
150 {
151     DHLOGI("start!");
152     std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
153     std::string udidHash = DHContext::GetInstance().GetDeviceInfo().udidHash;
154     std::string devName = DHContext::GetInstance().GetDeviceInfo().deviceName;
155     uint16_t devType = DHContext::GetInstance().GetDeviceInfo().deviceType;
156     std::string strUUID = DHContext::GetInstance().GetDeviceInfo().uuid;
157     CompVersion compversion;
158     VersionManager::GetInstance().GetCompVersion(strUUID, dhType, compversion);
159     for (auto dhItem : dhItems) {
160         std::shared_ptr<MetaCapabilityInfo> dhMetaCapInfo = std::make_shared<MetaCapabilityInfo>(
161             dhItem.dhId, deviceId, devName, devType, dhType, dhItem.attrs, dhItem.subtype, udidHash,
162             compversion);
163         metaCapInfos.push_back(dhMetaCapInfo);
164     }
165 }
166 
CheckNonExistCapabilityInfo(const std::vector<DHItem> & dhItems,const DHType dhType)167 void LocalHardwareManager::CheckNonExistCapabilityInfo(const std::vector<DHItem> &dhItems, const DHType dhType)
168 {
169     DHLOGI("start");
170     if (dhType != DHType::INPUT) {
171         DHLOGI("This dhType is not input and no need check!");
172         return;
173     }
174     CapabilityInfoMap allLocalCapabilityInfos;
175     GetLocalCapabilityMapByPrefix(dhType, allLocalCapabilityInfos);
176     for (auto capabilityInfo : allLocalCapabilityInfos) {
177         std::shared_ptr<CapabilityInfo> capabilityValue = capabilityInfo.second;
178         if (capabilityValue == nullptr) {
179             DHLOGE("capabilityInfo value is nullptr");
180             continue;
181         }
182         DHLOGI("The key in allLocalCapabilityInfos is %{public}s", capabilityValue->GetAnonymousKey().c_str());
183         bool isExist = false;
184         for (auto dhItem : dhItems) {
185             DHLOGI("This data key is: %{public}s, dhItem: %{public}s", capabilityValue->GetAnonymousKey().c_str(),
186                 GetAnonyString(dhItem.dhId).c_str());
187             if (capabilityValue->GetDHId() == dhItem.dhId) {
188                 DHLOGI("This data is exist, no need removed key: %{public}s",
189                     capabilityValue->GetAnonymousKey().c_str());
190                 isExist = true;
191                 break;
192             }
193         }
194         if (!isExist) {
195             DHLOGI("This data is non-exist, it should be removed, key: %{public}s",
196                 capabilityValue->GetAnonymousKey().c_str());
197             CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capabilityValue->GetKey());
198         }
199     }
200     DHLOGI("end");
201 }
202 
GetLocalCapabilityMapByPrefix(const DHType dhType,CapabilityInfoMap & capabilityInfoMap)203 void LocalHardwareManager::GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap)
204 {
205     std::string localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
206     if (!IsIdLengthValid(localDeviceId)) {
207         return;
208     }
209     if (DHTypePrefixMap.find(dhType) == DHTypePrefixMap.end()) {
210         DHLOGE("DHTypePrefixMap can not find dhType: %{public}#X", dhType);
211         return;
212     }
213     std::string prefix = DHTypePrefixMap.find(dhType)->second;
214     std::string localCapabilityPrefix = localDeviceId + RESOURCE_SEPARATOR + prefix;
215     CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(localCapabilityPrefix, capabilityInfoMap);
216 }
217 } // namespace DistributedHardware
218 } // namespace OHOS
219