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 "device_type.h"
23 #include "dh_context.h"
24 #include "distributed_hardware_errno.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
28 #undef DH_LOG_TAG
29 #define DH_LOG_TAG "LocalHardwareManager"
30
IMPLEMENT_SINGLE_INSTANCE(LocalHardwareManager)31 IMPLEMENT_SINGLE_INSTANCE(LocalHardwareManager)
32
33 LocalHardwareManager::LocalHardwareManager() {}
~LocalHardwareManager()34 LocalHardwareManager::~LocalHardwareManager() {}
35
Init()36 void LocalHardwareManager::Init()
37 {
38 std::vector<DHType> allCompTypes = ComponentLoader::GetInstance().GetAllCompTypes();
39 for (auto dhType : allCompTypes) {
40 IHardwareHandler *hardwareHandler = nullptr;
41 int32_t status = ComponentLoader::GetInstance().GetHardwareHandler(dhType, hardwareHandler);
42 if (status != DH_FWK_SUCCESS || hardwareHandler == nullptr) {
43 DHLOGE("GetHardwareHandler %#X failed", dhType);
44 continue;
45 }
46 if (hardwareHandler->Initialize() != DH_FWK_SUCCESS) {
47 DHLOGE("Initialize %#X failed", dhType);
48 continue;
49 }
50 QueryLocalHardware(dhType, hardwareHandler);
51 if (!hardwareHandler->IsSupportPlugin()) {
52 DHLOGI("hardwareHandler is not support hot swap plugin, release!");
53 ComponentLoader::GetInstance().ReleaseHardwareHandler(dhType);
54 hardwareHandler = nullptr;
55 } else {
56 compToolFuncsMap_[dhType] = hardwareHandler;
57 }
58 }
59 }
60
UnInit()61 void LocalHardwareManager::UnInit()
62 {
63 DHLOGI("start");
64 compToolFuncsMap_.clear();
65 }
66
QueryLocalHardware(const DHType dhType,IHardwareHandler * hardwareHandler)67 void LocalHardwareManager::QueryLocalHardware(const DHType dhType, IHardwareHandler *hardwareHandler)
68 {
69 std::vector<DHItem> dhItems;
70 int32_t retryTimes = QUERY_RETRY_MAX_TIMES;
71 while (retryTimes > 0) {
72 DHLOGI("Query hardwareHandler retry times left: %d, dhType: %#X", retryTimes, dhType);
73 dhItems = hardwareHandler->Query();
74 if (dhItems.empty()) {
75 DHLOGE("Query hardwareHandler and obtain empty, dhType: %#X", dhType);
76 usleep(QUERY_INTERVAL_TIME);
77 } else {
78 DHLOGI("Query hardwareHandler success, dhType: %#X!", dhType);
79 AddLocalCapabilityInfo(dhItems, dhType);
80 break;
81 }
82 retryTimes--;
83 }
84 }
85
AddLocalCapabilityInfo(const std::vector<DHItem> & dhItems,const DHType dhType)86 void LocalHardwareManager::AddLocalCapabilityInfo(const std::vector<DHItem> &dhItems, const DHType dhType)
87 {
88 DHLOGI("start!");
89 std::vector<std::shared_ptr<CapabilityInfo>> capabilityInfos;
90 std::string deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
91 std::string devName = DHContext::GetInstance().GetDeviceInfo().deviceName;
92 uint16_t devType = DHContext::GetInstance().GetDeviceInfo().deviceType;
93 for (auto dhItem : dhItems) {
94 std::shared_ptr<CapabilityInfo> dhCapabilityInfo =
95 std::make_shared<CapabilityInfo>(dhItem.dhId, deviceId, devName, devType, dhType, dhItem.attrs);
96 capabilityInfos.push_back(dhCapabilityInfo);
97 }
98 CapabilityInfoManager::GetInstance()->AddCapability(capabilityInfos);
99 }
100 }
101 }
102