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 "distributed_hardware_manager.h"
17
18 #include "anonymous_string.h"
19 #include "capability_info_manager.h"
20 #include "component_loader.h"
21 #include "component_manager.h"
22 #include "dh_context.h"
23 #include "dh_utils_tool.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "local_hardware_manager.h"
27 #include "task_board.h"
28 #include "task_executor.h"
29 #include "task_factory.h"
30 #include "version_manager.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 #undef DH_LOG_TAG
35 #define DH_LOG_TAG "DistributedHardwareManager"
36
37 #ifdef __cplusplus
38 #define EXTERNC extern "C"
39 #else
40 #define EXTERNC
41 #endif
42
GetDistributedHardwareManagerInstance()43 EXTERNC __attribute__((visibility("default"))) IDistributedHardwareManager *GetDistributedHardwareManagerInstance()
44 {
45 return &DistributedHardwareManager::GetInstance();
46 }
47
48 IMPLEMENT_SINGLE_INSTANCE(DistributedHardwareManager);
49
Initialize()50 int32_t DistributedHardwareManager::Initialize()
51 {
52 DHLOGI("start");
53 CapabilityInfoManager::GetInstance()->Init();
54
55 ComponentLoader::GetInstance().Init();
56
57 LocalHardwareManager::GetInstance().Init();
58
59 VersionManager::GetInstance().Init();
60
61 ComponentManager::GetInstance().Init();
62
63 return DH_FWK_SUCCESS;
64 }
65
Release()66 int32_t DistributedHardwareManager::Release()
67 {
68 DHLOGI("start");
69 TaskBoard::GetInstance().WaitForALLTaskFinish();
70
71 ComponentManager::GetInstance().UnInit();
72
73 VersionManager::GetInstance().UnInit();
74
75 LocalHardwareManager::GetInstance().UnInit();
76
77 ComponentLoader::GetInstance().UnInit();
78
79 CapabilityInfoManager::GetInstance()->UnInit();
80
81 return DH_FWK_SUCCESS;
82 }
83
SendOnLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)84 int32_t DistributedHardwareManager::SendOnLineEvent(const std::string &networkId, const std::string &uuid,
85 uint16_t deviceType)
86 {
87 (void)deviceType;
88
89 if (networkId.empty()) {
90 DHLOGE("networkId is empty");
91 return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY;
92 }
93 if (uuid.empty()) {
94 DHLOGE("uuid is empty, networkId = %s", GetAnonyString(networkId).c_str());
95 return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY;
96 }
97
98 DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str());
99
100 if (DHContext::GetInstance().IsDeviceOnline(uuid)) {
101 DHLOGW("device is already online, uuid = %s", GetAnonyString(uuid).c_str());
102 return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE;
103 }
104
105 auto task = TaskFactory::GetInstance().CreateTask(TaskType::ON_LINE, networkId, uuid, "", nullptr);
106 TaskExecutor::GetInstance().PushTask(task);
107 DHContext::GetInstance().AddOnlineDevice(uuid, networkId);
108 CapabilityInfoManager::GetInstance()->CreateManualSyncCount(GetDeviceIdByUUID(uuid));
109
110 return DH_FWK_SUCCESS;
111 }
112
SendOffLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)113 int32_t DistributedHardwareManager::SendOffLineEvent(const std::string &networkId, const std::string &uuid,
114 uint16_t deviceType)
115 {
116 (void)deviceType;
117
118 if (networkId.empty()) {
119 DHLOGE("networkId is empty");
120 return ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY;
121 }
122
123 // when other device restart, the device receives online and offline messages in sequence
124 // So, make the cache device handle offline event when other device restart
125 std::string cacheUUID = DHContext::GetInstance().GetUUIDByNetworkId(networkId);
126 if (uuid.empty() && cacheUUID.empty()) {
127 DHLOGE("uuid is empty, networkId = %s", GetAnonyString(networkId).c_str());
128 return ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY;
129 }
130
131 std::string realUUID = uuid.empty() ? cacheUUID : uuid;
132
133 DHLOGI("networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(), GetAnonyString(realUUID).c_str());
134
135 if (!DHContext::GetInstance().IsDeviceOnline(realUUID)) {
136 DHLOGW("device is already offline, uuid = %s", GetAnonyString(realUUID).c_str());
137 return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE;
138 }
139
140 auto task = TaskFactory::GetInstance().CreateTask(TaskType::OFF_LINE, networkId, realUUID, "", nullptr);
141 TaskExecutor::GetInstance().PushTask(task);
142
143 DHContext::GetInstance().RemoveOnlineDevice(realUUID);
144 CapabilityInfoManager::GetInstance()->RemoveManualSyncCount(GetDeviceIdByUUID(realUUID));
145
146 return DH_FWK_SUCCESS;
147 }
148
GetOnLineCount()149 size_t DistributedHardwareManager::GetOnLineCount()
150 {
151 return DHContext::GetInstance().GetOnlineCount();
152 }
153
GetComponentVersion(std::unordered_map<DHType,std::string> & versionMap)154 int32_t DistributedHardwareManager::GetComponentVersion(std::unordered_map<DHType, std::string> &versionMap)
155 {
156 DHLOGI("start");
157 DHVersion dhVersion;
158 int32_t ret = ComponentLoader::GetInstance().GetLocalDHVersion(dhVersion);
159 if (ret != DH_FWK_SUCCESS) {
160 DHLOGE("GetLocalDHVersion fail, errCode = %d", ret);
161 return ret;
162 }
163
164 for (auto iter = dhVersion.compVersions.cbegin(); iter != dhVersion.compVersions.cend(); ++iter) {
165 versionMap.emplace(iter->first, iter->second.sinkVersion);
166 }
167 return DH_FWK_SUCCESS;
168 }
169 }
170 }
171