1 /*
2 * Copyright (c) 2021-2024 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 = std::make_shared<AccessManager>();
47 return instance;
48 }
49
Init()50 int32_t AccessManager::Init()
51 {
52 DHLOGI("start");
53 if (InitDeviceManager() != DH_FWK_SUCCESS) {
54 DHLOGE("InitDeviceManager failed");
55 return ERR_DH_FWK_ACCESS_INIT_DM_FAILED;
56 }
57 if (RegisterDevStateCallback() != DH_FWK_SUCCESS) {
58 DHLOGE("RegisterDevStateCallback failed");
59 return ERR_DH_FWK_ACCESS_REGISTER_DM_FAILED;
60 }
61 return DH_FWK_SUCCESS;
62 }
63
UnInit()64 int32_t AccessManager::UnInit()
65 {
66 DHLOGI("start");
67 if (UnInitDeviceManager() != DH_FWK_SUCCESS) {
68 DHLOGE("UnInitDeviceManager failed");
69 return ERR_DH_FWK_ACCESS_UNINIT_DM_FAILED;
70 }
71
72 if (UnRegisterDevStateCallback() != DH_FWK_SUCCESS) {
73 DHLOGE("UnRegisterDevStateCallback failed");
74 return ERR_DH_FWK_ACCESS_UNREGISTER_DM_FAILED;
75 }
76 return DH_FWK_SUCCESS;
77 }
78
InitDeviceManager()79 int32_t AccessManager::InitDeviceManager()
80 {
81 DHLOGI("start");
82 return DeviceManager::GetInstance().InitDeviceManager(DH_FWK_PKG_NAME, shared_from_this());
83 }
84
UnInitDeviceManager()85 int32_t AccessManager::UnInitDeviceManager()
86 {
87 DHLOGI("start");
88 return DeviceManager::GetInstance().UnInitDeviceManager(DH_FWK_PKG_NAME);
89 }
90
RegisterDevStateCallback()91 int32_t AccessManager::RegisterDevStateCallback()
92 {
93 return DeviceManager::GetInstance().RegisterDevStateCallback(DH_FWK_PKG_NAME, "", shared_from_this());
94 }
95
UnRegisterDevStateCallback()96 int32_t AccessManager::UnRegisterDevStateCallback()
97 {
98 return DeviceManager::GetInstance().UnRegisterDevStateCallback(DH_FWK_PKG_NAME);
99 }
100
OnRemoteDied()101 void AccessManager::OnRemoteDied()
102 {
103 for (int32_t tryCount = 0; tryCount < DH_RETRY_INIT_DM_COUNT; ++tryCount) {
104 usleep(DH_RETRY_INIT_DM_INTERVAL_US);
105 if (Init() == DH_FWK_SUCCESS) {
106 DHLOGI("DeviceManager onDied, try to init success, tryCount = %{public}d", tryCount);
107 return;
108 }
109 DHLOGW("DeviceManager onDied, try to init failed, tryCount = %{public}d", tryCount);
110 }
111 DHLOGE("DeviceManager onDied, try to init has reached the maximum, but still failed");
112 return;
113 }
114
OnDeviceOnline(const DmDeviceInfo & deviceInfo)115 void AccessManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
116 {
117 (void)deviceInfo;
118 return;
119 }
120
OnDeviceOffline(const DmDeviceInfo & deviceInfo)121 void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
122 {
123 std::lock_guard<std::mutex> lock(accessMutex_);
124 DHLOGI("AccessManager offline, networkId: %{public}s, deviceName: %{public}s, deviceTypeId: %{public}d",
125 GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(),
126 deviceInfo.deviceTypeId);
127
128 auto networkId = std::string(deviceInfo.networkId);
129 if (!IsIdLengthValid(networkId)) {
130 return;
131 }
132 std::string uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId);
133 if (!IsIdLengthValid(uuid)) {
134 return;
135 }
136 std::string udid = DHContext::GetInstance().GetUDIDByNetworkId(networkId);
137 if (!IsIdLengthValid(udid)) {
138 return;
139 }
140
141 auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(networkId, uuid, udid,
142 deviceInfo.deviceTypeId);
143 DHLOGI("offline result: %{public}d, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
144 ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
145 }
146
OnDeviceReady(const DmDeviceInfo & deviceInfo)147 void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo)
148 {
149 std::lock_guard<std::mutex> lock(accessMutex_);
150 DHLOGI("AccessManager online, networkId: %{public}s, deviceName: %{public}s, deviceTypeId: %{public}d",
151 GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(),
152 deviceInfo.deviceTypeId);
153
154 auto networkId = std::string(deviceInfo.networkId);
155 if (!IsIdLengthValid(networkId)) {
156 return;
157 }
158 auto uuid = GetUUIDByDm(networkId);
159 if (!IsIdLengthValid(uuid)) {
160 return;
161 }
162 auto udid = GetUDIDByDm(networkId);
163 if (!IsIdLengthValid(udid)) {
164 return;
165 }
166 auto ret =
167 DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
168 deviceInfo.deviceTypeId);
169 DHLOGI("AccessManager online result: %{public}d, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
170 ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
171 }
172
OnDeviceChanged(const DmDeviceInfo & deviceInfo)173 void AccessManager::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
174 {
175 (void)deviceInfo;
176 return;
177 }
178
CheckTrustedDeviceOnline()179 void AccessManager::CheckTrustedDeviceOnline()
180 {
181 std::vector<DmDeviceInfo> deviceList;
182 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
183 if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) {
184 DHLOGE("DeviceList size is invalid!");
185 return;
186 }
187 for (const auto &deviceInfo : deviceList) {
188 const auto networkId = std::string(deviceInfo.networkId);
189 const auto uuid = GetUUIDByDm(networkId);
190 const auto udid = GetUDIDByDm(networkId);
191 DHLOGI("Send trusted device online, networkId = %{public}s, uuid = %{public}s, udid = %{public}s",
192 GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
193 DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
194 deviceInfo.deviceTypeId);
195 }
196 }
197
Dump(const std::vector<std::string> & argsStr,std::string & result)198 int32_t AccessManager::Dump(const std::vector<std::string> &argsStr, std::string &result)
199 {
200 return DistributedHardwareManagerFactory::GetInstance().Dump(argsStr, result);
201 }
202 } // namespace DistributedHardware
203 } // namespace OHOS
204