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