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 <unistd.h>
19 #include <vector>
20
21 #include "anonymous_string.h"
22 #include "constants.h"
23 #include "dh_utils_tool.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "distributed_hardware_manager_factory.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "AccessManager"
32
33 constexpr int32_t DH_RETRY_INIT_DM_COUNT = 6;
34 constexpr int32_t DH_RETRY_INIT_DM_INTERVAL_US = 1000 * 500;
~AccessManager()35 AccessManager::~AccessManager()
36 {
37 UnInit();
38 }
39
GetInstance()40 std::shared_ptr<AccessManager> AccessManager::GetInstance()
41 {
42 static std::shared_ptr<AccessManager> instance(new AccessManager);
43 return instance;
44 }
45
Init()46 int32_t AccessManager::Init()
47 {
48 DHLOGI("start");
49 if (InitDeviceManager() != DH_FWK_SUCCESS) {
50 DHLOGE("InitDeviceManager failed");
51 return ERR_DH_FWK_ACCESS_INIT_DM_FAILED;
52 }
53
54 if (RegisterDevStateCallback() != DH_FWK_SUCCESS) {
55 DHLOGE("RegisterDevStateCallback failed");
56 return ERR_DH_FWK_ACCESS_REGISTER_DM_FAILED;
57 }
58 SendTrustedDeviceOnline();
59 return DH_FWK_SUCCESS;
60 }
61
UnInit()62 int32_t AccessManager::UnInit()
63 {
64 DHLOGI("start");
65 if (UnInitDeviceManager() != DH_FWK_SUCCESS) {
66 DHLOGE("UnInitDeviceManager failed");
67 return ERR_DH_FWK_ACCESS_UNINIT_DM_FAILED;
68 }
69
70 if (UnRegisterDevStateCallback() != DH_FWK_SUCCESS) {
71 DHLOGE("UnRegisterDevStateCallback failed");
72 return ERR_DH_FWK_ACCESS_UNREGISTER_DM_FAILED;
73 }
74 return DH_FWK_SUCCESS;
75 }
76
InitDeviceManager()77 int32_t AccessManager::InitDeviceManager()
78 {
79 DHLOGI("start");
80 return DeviceManager::GetInstance().InitDeviceManager(DH_FWK_PKG_NAME, shared_from_this());
81 }
82
UnInitDeviceManager()83 int32_t AccessManager::UnInitDeviceManager()
84 {
85 DHLOGI("start");
86 return DeviceManager::GetInstance().UnInitDeviceManager(DH_FWK_PKG_NAME);
87 }
88
RegisterDevStateCallback()89 int32_t AccessManager::RegisterDevStateCallback()
90 {
91 return DeviceManager::GetInstance().RegisterDevStateCallback(DH_FWK_PKG_NAME, "", shared_from_this());
92 }
93
UnRegisterDevStateCallback()94 int32_t AccessManager::UnRegisterDevStateCallback()
95 {
96 return DeviceManager::GetInstance().UnRegisterDevStateCallback(DH_FWK_PKG_NAME);
97 }
98
OnRemoteDied()99 void AccessManager::OnRemoteDied()
100 {
101 for (int32_t tryCount = 0; tryCount < DH_RETRY_INIT_DM_COUNT; ++tryCount) {
102 usleep(DH_RETRY_INIT_DM_INTERVAL_US);
103 if (Init() == DH_FWK_SUCCESS) {
104 DHLOGI("DeviceManager onDied, try to init success, tryCount = %d", tryCount);
105 return;
106 }
107 DHLOGW("DeviceManager onDied, try to init failed, tryCount = %d", tryCount);
108 }
109 DHLOGE("DeviceManager onDied, try to init has reached the maximum, but still failed");
110 return;
111 }
112
OnDeviceOnline(const DmDeviceInfo & deviceInfo)113 void AccessManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
114 {
115 std::lock_guard<std::mutex> lock(accessMutex_);
116 DHLOGI("start, networkId = %s, deviceName = %s, deviceTypeId = %d", GetAnonyString(deviceInfo.deviceId).c_str(),
117 deviceInfo.deviceName, deviceInfo.deviceTypeId);
118
119 auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId
120 auto uuid = GetUUIDBySoftBus(networkId);
121 auto ret =
122 DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId);
123 DHLOGI("online result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(),
124 GetAnonyString(uuid).c_str());
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 deviceInfo.deviceName, deviceInfo.deviceTypeId);
132
133 auto networkId = std::string(deviceInfo.deviceId); // deviceId of DM actually is networkId
134 auto uuid = GetUUIDBySoftBus(networkId);
135 auto ret =
136 DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(networkId, uuid, deviceInfo.deviceTypeId);
137 DHLOGI("offline result = %d, networkId = %s, uuid = %s", ret, GetAnonyString(networkId).c_str(),
138 GetAnonyString(uuid).c_str());
139 }
140
OnDeviceReady(const DmDeviceInfo & deviceInfo)141 void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo)
142 {
143 (void)deviceInfo;
144 return;
145 }
146
OnDeviceChanged(const DmDeviceInfo & deviceInfo)147 void AccessManager::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
148 {
149 (void)deviceInfo;
150 return;
151 }
152
SendTrustedDeviceOnline()153 void AccessManager::SendTrustedDeviceOnline()
154 {
155 std::vector<DmDeviceInfo> deviceList;
156 DeviceManager::GetInstance().GetTrustedDeviceList(DH_FWK_PKG_NAME, "", deviceList);
157 for (const auto &deviceInfo : deviceList) {
158 const auto networkId = std::string(deviceInfo.deviceId);
159 const auto uuid = GetUUIDBySoftBus(networkId);
160 DHLOGI("Send trusted device online, networkId = %s, uuid = %s", GetAnonyString(networkId).c_str(),
161 GetAnonyString(uuid).c_str());
162 DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, deviceInfo.deviceTypeId);
163 }
164 }
165 }
166 }
167