1 /*
2 * Copyright (c) 2021 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 "capability_info_manager.h"
21 #include "component_loader.h"
22 #include "constants.h"
23 #include "device_type.h"
24 #include "dh_context.h"
25 #include "dh_utils_hitrace.h"
26 #include "distributed_hardware_errno.h"
27 #include "plugin_listener_impl.h"
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 #undef DH_LOG_TAG
32 #define DH_LOG_TAG "LocalHardwareManager"
33
IMPLEMENT_SINGLE_INSTANCE(LocalHardwareManager)34 IMPLEMENT_SINGLE_INSTANCE(LocalHardwareManager)
35
36 LocalHardwareManager::LocalHardwareManager() {}
~LocalHardwareManager()37 LocalHardwareManager::~LocalHardwareManager() {}
38
Init()39 void LocalHardwareManager::Init()
40 {
41 DHLOGI("start");
42 std::vector<DHType> allCompTypes = ComponentLoader::GetInstance().GetAllCompTypes();
43 for (auto dhType : allCompTypes) {
44 IHardwareHandler *hardwareHandler = nullptr;
45 int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandler);
46 if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) {
47 DHLOGE("GetHardwareHandler %#X failed", dhType);
48 continue;
49 }
50 if (hardwareHandler->Initialize() != DH_FWK_SUCCESS) {
51 DHLOGE("Initialize %#X failed", dhType);
52 continue;
53 }
54
55 DHQueryTraceStart(dhType);
56 QueryLocalHardware(dhType, hardwareHandler);
57 DHTraceEnd();
58 if (!hardwareHandler->IsSupportPlugin()) {
59 DHLOGI("hardwareHandler is not support hot swap plugin, release!");
60 ComponentLoader::GetInstance().ReleaseHardwareHandler(dhType);
61 hardwareHandler = nullptr;
62 } else {
63 compToolFuncsMap_[dhType] = hardwareHandler;
64 std::shared_ptr<PluginListener> listener = std::make_shared<PluginListenerImpl>(dhType);
65 pluginListenerMap_[dhType] = listener;
66 hardwareHandler->RegisterPluginListener(listener);
67 }
68 }
69 }
70
UnInit()71 void LocalHardwareManager::UnInit()
72 {
73 DHLOGI("start");
74 compToolFuncsMap_.clear();
75 pluginListenerMap_.clear();
76 }
77
QueryLocalHardware(const DHType dhType,IHardwareHandler * hardwareHandler)78 void LocalHardwareManager::QueryLocalHardware(const DHType dhType, IHardwareHandler *hardwareHandler)
79 {
80 std::vector<DHItem> dhItems;
81 int32_t retryTimes = QUERY_RETRY_MAX_TIMES;
82 while (retryTimes > 0) {
83 DHLOGI("Query hardwareHandler retry times left: %d, dhType: %#X", retryTimes, dhType);
84 dhItems = hardwareHandler->Query();
85 if (dhItems.empty()) {
86 DHLOGE("Query hardwareHandler and obtain empty, dhType: %#X", dhType);
87 usleep(QUERY_INTERVAL_TIME);
88 } else {
89 DHLOGI("Query hardwareHandler success, dhType: %#X!", dhType);
90
91 /*
92 * Failed to delete data when the device restarts or other exception situation.
93 * So check and remove the non-exist local capabilityInfo.
94 */
95 CheckNonExistCapabilityInfo(dhItems, dhType);
96 AddLocalCapabilityInfo(dhItems, dhType);
97 break;
98 }
99 retryTimes--;
100 }
101 }
102
AddLocalCapabilityInfo(const std::vector<DHItem> & dhItems,const DHType dhType)103 void LocalHardwareManager::AddLocalCapabilityInfo(const std::vector<DHItem> &dhItems, const DHType dhType)
104 {
105 DHLOGI("start!");
106 std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
107 std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
108 std::string devName = DHContext::GetInstance().GetDeviceInfo().deviceName;
109 uint16_t devType = DHContext::GetInstance().GetDeviceInfo().deviceType;
110 for (auto dhItem : dhItems) {
111 std::shared_ptr<CapabilityInfo> dhCapabilityInfo =
112 std::make_shared<CapabilityInfo>(dhItem.dhId, deviceId, devName, devType, dhType, dhItem.attrs);
113 capabilityInfos.push_back(dhCapabilityInfo);
114 }
115 CapabilityInfoManager::GetInstance()->AddCapability(capabilityInfos);
116 }
117
CheckNonExistCapabilityInfo(const std::vector<DHItem> & dhItems,const DHType dhType)118 void LocalHardwareManager::CheckNonExistCapabilityInfo(const std::vector<DHItem> &dhItems, const DHType dhType)
119 {
120 DHLOGI("start");
121 if (dhType != DHType::INPUT) {
122 DHLOGI("This dhType is not input and no need check!");
123 return;
124 }
125 CapabilityInfoMap allLocalCapabilityInfos;
126 GetLocalCapabilityMapByPrefix(dhType, allLocalCapabilityInfos);
127 for (auto capabilityInfo : allLocalCapabilityInfos) {
128 std::shared_ptr<CapabilityInfo> capabilityValue = capabilityInfo.second;
129 if (capabilityValue == nullptr) {
130 DHLOGE("capabilityInfo value is nullptr");
131 continue;
132 }
133 DHLOGI("The key in allLocalCapabilityInfos is %s", capabilityValue->GetAnonymousKey().c_str());
134 bool isExist = false;
135 for (auto dhItem : dhItems) {
136 DHLOGI("This data key is: %s, dhItem: %s", capabilityValue->GetAnonymousKey().c_str(),
137 GetAnonyString(dhItem.dhId).c_str());
138 if (capabilityValue->GetDHId() == dhItem.dhId) {
139 DHLOGI("This data is exist, no need removed key: %s", capabilityValue->GetAnonymousKey().c_str());
140 isExist = true;
141 break;
142 }
143 }
144 if (!isExist) {
145 DHLOGI("This data is non-exist, it should be removed, key: %s", capabilityValue->GetAnonymousKey().c_str());
146 CapabilityInfoManager::GetInstance()->RemoveCapabilityInfoByKey(capabilityValue->GetKey());
147 }
148 }
149 DHLOGI("end");
150 }
151
GetLocalCapabilityMapByPrefix(const DHType dhType,CapabilityInfoMap & capabilityInfoMap)152 void LocalHardwareManager::GetLocalCapabilityMapByPrefix(const DHType dhType, CapabilityInfoMap &capabilityInfoMap)
153 {
154 std::string localDeviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
155 if (localDeviceId.size() == 0 || localDeviceId.size() > MAX_ID_LEN) {
156 DHLOGE("LocalDeviceId is invalid");
157 return;
158 }
159 if (DHTypePrefixMap.find(dhType) == DHTypePrefixMap.end()) {
160 DHLOGE("DHTypePrefixMap can not find dhType: %#X", dhType);
161 return;
162 }
163 std::string prefix = DHTypePrefixMap.find(dhType)->second;
164 std::string localCapabilityPrefix = localDeviceId + RESOURCE_SEPARATOR + prefix;
165 CapabilityInfoManager::GetInstance()->GetDataByKeyPrefix(localCapabilityPrefix, capabilityInfoMap);
166 }
167 } // namespace DistributedHardware
168 } // namespace OHOS
169