1 /*
2 * Copyright (c) 2021-2022 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_factory.h"
17
18 #include <cstdlib>
19 #include <dlfcn.h>
20 #include <string>
21 #include <thread>
22 #include <vector>
23
24 #include "dm_device_info.h"
25
26 #include "anonymous_string.h"
27 #include "constants.h"
28 #include "device_manager.h"
29 #include "dh_context.h"
30 #include "dh_utils_hisysevent.h"
31 #include "dh_utils_hitrace.h"
32 #include "dh_utils_tool.h"
33 #include "distributed_hardware_errno.h"
34 #include "distributed_hardware_log.h"
35 #include "distributed_hardware_manager.h"
36
37 namespace OHOS {
38 namespace DistributedHardware {
39 #undef DH_LOG_TAG
40 #define DH_LOG_TAG "DistributedHardwareManagerFactory"
41
42 IMPLEMENT_SINGLE_INSTANCE(DistributedHardwareManagerFactory);
Init()43 bool DistributedHardwareManagerFactory::Init()
44 {
45 DHLOGI("start");
46 isInit = true;
47 auto initResult = DistributedHardwareManager::GetInstance().Initialize();
48 if (initResult != DH_FWK_SUCCESS) {
49 DHLOGE("Initialize failed, errCode = %d", initResult);
50 return false;
51 }
52 DHLOGD("success");
53 return true;
54 }
55
UnInit()56 void DistributedHardwareManagerFactory::UnInit()
57 {
58 DHLOGI("start");
59 DHTraceStart(COMPONENT_UNLOAD_START);
60 HiSysEventWriteMsg(DHFWK_EXIT_BEGIN, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
61 "dhfwk sa exit begin.");
62
63 // release all the resources synchronously
64 DistributedHardwareManager::GetInstance().Release();
65 isInit = false;
66 DHTraceEnd();
67 CheckExitSAOrNot();
68 }
69
CheckExitSAOrNot()70 void DistributedHardwareManagerFactory::CheckExitSAOrNot()
71 {
72 std::vector<DmDeviceInfo> deviceList;
73 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
74 if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) {
75 DHLOGI("DM report devices offline or deviceList is over size, exit sa process");
76 HiSysEventWriteMsg(DHFWK_EXIT_END, OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
77 "dhfwk sa exit end.");
78
79 _Exit(0);
80 }
81
82 DHLOGI("After uninit, DM report devices online, reinit");
83 Init();
84 for (const auto &deviceInfo : deviceList) {
85 const auto networkId = std::string(deviceInfo.deviceId);
86 const auto uuid = GetUUIDBySoftBus(networkId);
87 DHLOGI("Send trusted device online, networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(),
88 GetAnonyString(uuid).c_str());
89 std::thread(&DistributedHardwareManagerFactory::SendOnLineEvent, this, networkId, uuid,
90 deviceInfo.deviceTypeId).detach();
91 }
92 }
93
IsInit()94 bool DistributedHardwareManagerFactory::IsInit()
95 {
96 return isInit.load();
97 }
98
SendOnLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)99 int32_t DistributedHardwareManagerFactory::SendOnLineEvent(const std::string &networkId, const std::string &uuid,
100 uint16_t deviceType)
101 {
102 if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN || uuid.size() == 0 || uuid.size() > MAX_ID_LEN) {
103 DHLOGE("NetworkId or uuid is invalid");
104 return ERR_DH_FWK_PARA_INVALID;
105 }
106
107 if (DHContext::GetInstance().IsDeviceOnline(uuid)) {
108 DHLOGW("device is already online, uuid = %s", GetAnonyString(uuid).c_str());
109 return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE;
110 }
111
112 DHContext::GetInstance().AddOnlineDevice(uuid, networkId);
113
114 if (!isInit && !Init()) {
115 DHLOGE("distributedHardwareMgr is null");
116 return ERR_DH_FWK_HARDWARE_MANAGER_INIT_FAILED;
117 }
118
119 auto onlineResult = DistributedHardwareManager::GetInstance().SendOnLineEvent(networkId, uuid, deviceType);
120 if (onlineResult != DH_FWK_SUCCESS) {
121 DHLOGE("online failed, errCode = %d", onlineResult);
122 return onlineResult;
123 }
124 return DH_FWK_SUCCESS;
125 }
126
SendOffLineEvent(const std::string & networkId,const std::string & uuid,uint16_t deviceType)127 int32_t DistributedHardwareManagerFactory::SendOffLineEvent(const std::string &networkId, const std::string &uuid,
128 uint16_t deviceType)
129 {
130 if (networkId.empty() || networkId.size() > MAX_ID_LEN || uuid.empty() || uuid.size() > MAX_ID_LEN) {
131 DHLOGE("NetworkId or uuid is invalid");
132 return ERR_DH_FWK_PARA_INVALID;
133 }
134
135 if (!isInit && !Init()) {
136 DHLOGE("distributedHardwareMgr is null");
137 return ERR_DH_FWK_HARDWARE_MANAGER_INIT_FAILED;
138 }
139
140 if (!DHContext::GetInstance().IsDeviceOnline(uuid)) {
141 DHLOGE("Device not online, networkId: %s, uuid: %s",
142 GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str());
143 return ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE;
144 }
145
146 auto offlineResult = DistributedHardwareManager::GetInstance().SendOffLineEvent(networkId, uuid, deviceType);
147 if (offlineResult != DH_FWK_SUCCESS) {
148 DHLOGE("offline failed, errCode = %d", offlineResult);
149 return offlineResult;
150 }
151
152 DHContext::GetInstance().RemoveOnlineDevice(uuid);
153 if (DistributedHardwareManager::GetInstance().GetOnLineCount() == 0) {
154 DHLOGI("all devices are offline, start to free the resource");
155 UnInit();
156 }
157 return DH_FWK_SUCCESS;
158 }
159
GetComponentVersion(std::unordered_map<DHType,std::string> & versionMap)160 int32_t DistributedHardwareManagerFactory::GetComponentVersion(std::unordered_map<DHType, std::string> &versionMap)
161 {
162 DHLOGI("start");
163 return DistributedHardwareManager::GetInstance().GetComponentVersion(versionMap);
164 }
165
Dump(const std::vector<std::string> & argsStr,std::string & result)166 int32_t DistributedHardwareManagerFactory::Dump(const std::vector<std::string> &argsStr, std::string &result)
167 {
168 return DistributedHardwareManager::GetInstance().Dump(argsStr, result);
169 }
170 } // namespace DistributedHardware
171 } // namespace OHOS
172