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 "access_manager.h"
17
18 #include <new>
19 #include <unistd.h>
20 #include <vector>
21
22 #include "device_manager.h"
23
24 #include "anonymous_string.h"
25 #include "constants.h"
26 #include "dh_context.h"
27 #include "dh_utils_tool.h"
28 #include "distributed_hardware_errno.h"
29 #include "distributed_hardware_log.h"
30 #include "distributed_hardware_manager_factory.h"
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 #undef DH_LOG_TAG
35 #define DH_LOG_TAG "AccessManager"
36
37 constexpr int32_t DH_RETRY_INIT_DM_COUNT = 6;
38 constexpr int32_t DH_RETRY_INIT_DM_INTERVAL_US = 1000 * 500;
~AccessManager()39 AccessManager::~AccessManager()
40 {
41 UnInit();
42 }
43
GetInstance()44 std::shared_ptr<AccessManager> AccessManager::GetInstance()
45 {
46 static std::shared_ptr<AccessManager> instance(new(std::nothrow) AccessManager);
47 if (instance == nullptr) {
48 DHLOGE("instance is nullptr, because applying memory fail!");
49 return nullptr;
50 }
51 return instance;
52 }
53
Init()54 int32_t AccessManager::Init()
55 {
56 DHLOGI("start");
57 if (InitDeviceManager() != DH_FWK_SUCCESS) {
58 DHLOGE("InitDeviceManager failed");
59 return ERR_DH_FWK_ACCESS_INIT_DM_FAILED;
60 }
61
62 if (RegisterDevStateCallback() != DH_FWK_SUCCESS) {
63 DHLOGE("RegisterDevStateCallback failed");
64 return ERR_DH_FWK_ACCESS_REGISTER_DM_FAILED;
65 }
66 SendTrustedDeviceOnline();
67 return DH_FWK_SUCCESS;
68 }
69
UnInit()70 int32_t AccessManager::UnInit()
71 {
72 DHLOGI("start");
73 if (UnInitDeviceManager() != DH_FWK_SUCCESS) {
74 DHLOGE("UnInitDeviceManager failed");
75 return ERR_DH_FWK_ACCESS_UNINIT_DM_FAILED;
76 }
77
78 if (UnRegisterDevStateCallback() != DH_FWK_SUCCESS) {
79 DHLOGE("UnRegisterDevStateCallback failed");
80 return ERR_DH_FWK_ACCESS_UNREGISTER_DM_FAILED;
81 }
82 return DH_FWK_SUCCESS;
83 }
84
InitDeviceManager()85 int32_t AccessManager::InitDeviceManager()
86 {
87 DHLOGI("start");
88 return DeviceManager::GetInstance().InitDeviceManager(DH_FWK_PKG_NAME, shared_from_this());
89 }
90
UnInitDeviceManager()91 int32_t AccessManager::UnInitDeviceManager()
92 {
93 DHLOGI("start");
94 return DeviceManager::GetInstance().UnInitDeviceManager(DH_FWK_PKG_NAME);
95 }
96
RegisterDevStateCallback()97 int32_t AccessManager::RegisterDevStateCallback()
98 {
99 return DeviceManager::GetInstance().RegisterDevStateCallback(DH_FWK_PKG_NAME, "", shared_from_this());
100 }
101
UnRegisterDevStateCallback()102 int32_t AccessManager::UnRegisterDevStateCallback()
103 {
104 return DeviceManager::GetInstance().UnRegisterDevStateCallback(DH_FWK_PKG_NAME);
105 }
106
OnRemoteDied()107 void AccessManager::OnRemoteDied()
108 {
109 for (int32_t tryCount = 0; tryCount < DH_RETRY_INIT_DM_COUNT; ++tryCount) {
110 usleep(DH_RETRY_INIT_DM_INTERVAL_US);
111 if (Init() == DH_FWK_SUCCESS) {
112 DHLOGI("DeviceManager onDied, try to init success, tryCount = %d", tryCount);
113 return;
114 }
115 DHLOGW("DeviceManager onDied, try to init failed, tryCount = %d", tryCount);
116 }
117 DHLOGE("DeviceManager onDied, try to init has reached the maximum, but still failed");
118 return;
119 }
120
OnDeviceOnline(const DmDeviceInfo & deviceInfo)121 void AccessManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
122 {
123 (void)deviceInfo;
124 return;
125 }
126
OnDeviceOffline(const DmDeviceInfo & deviceInfo)127 void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
128 {
129 std::lock_guard<std::mutex> lock(accessMutex_);
130 DHLOGI("start, networkId = %s, deviceName = %s, deviceTypeId = %d", GetAnonyString(deviceInfo.deviceId).c_str(),
131 GetAnonyString(deviceInfo.deviceName).c_str(), deviceInfo.deviceTypeId);
132
133 auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId
134 if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) {
135 DHLOGE("NetworkId is invalid!");
136 return;
137 }
138 auto uuid = GetUUIDBySoftBus(networkId);
139
140 // when other device restart, the device receives online and offline messages in sequence
141 // uuid is empty call by GetUUIDBySoftBus function. So, get uuid by memory cache when other device restart
142 uuid = uuid.empty() ? DHContext::GetInstance().GetUUIDByNetworkId(networkId) : uuid;
143 if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) {
144 DHLOGE("Uuid is invalid!");
145 return;
146 }
147
148 auto ret =
149 DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(networkId, uuid, deviceInfo.deviceTypeId);
150 DHLOGI("offline result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(),
151 GetAnonyString(uuid).c_str());
152 }
153
OnDeviceReady(const DmDeviceInfo & deviceInfo)154 void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo)
155 {
156 std::lock_guard<std::mutex> lock(accessMutex_);
157 DHLOGI("start, networkId = %s, deviceName = %s, deviceTypeId = %d", GetAnonyString(deviceInfo.deviceId).c_str(),
158 GetAnonyString(deviceInfo.deviceName).c_str(), deviceInfo.deviceTypeId);
159
160 auto networkId = std::string(deviceInfo.deviceId);
161 if (networkId.size() == 0 || networkId.size() > MAX_ID_LEN) {
162 DHLOGE("NetworkId is invalid!");
163 return;
164 }
165 auto uuid = GetUUIDBySoftBus(networkId);
166 if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) {
167 DHLOGE("Uuid is invalid!");
168 return;
169 }
170 auto ret =
171 DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId);
172 DHLOGI("online result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(),
173 GetAnonyString(uuid).c_str());
174 }
175
OnDeviceChanged(const DmDeviceInfo & deviceInfo)176 void AccessManager::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
177 {
178 (void)deviceInfo;
179 return;
180 }
181
SendTrustedDeviceOnline()182 void AccessManager::SendTrustedDeviceOnline()
183 {
184 std::vector<DmDeviceInfo> deviceList;
185 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
186 if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) {
187 DHLOGE("DeviceList size is invalid!");
188 return;
189 }
190 for (const auto &deviceInfo : deviceList) {
191 const auto networkId = std::string(deviceInfo.deviceId);
192 const auto uuid = GetUUIDBySoftBus(networkId);
193 DHLOGI("Send trusted device online, networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(),
194 GetAnonyString(uuid).c_str());
195 DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId);
196 }
197 }
198
Dump(const std::vector<std::string> & argsStr,std::string & result)199 int32_t AccessManager::Dump(const std::vector<std::string> &argsStr, std::string &result)
200 {
201 return DistributedHardwareManagerFactory::GetInstance().Dump(argsStr, result);
202 }
203 } // namespace DistributedHardware
204 } // namespace OHOS
205