• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "device_param_mgr.h"
29 #include "distributed_hardware_errno.h"
30 #include "distributed_hardware_log.h"
31 #include "distributed_hardware_manager_factory.h"
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 #undef DH_LOG_TAG
36 #define DH_LOG_TAG "AccessManager"
37 
38 constexpr int32_t DH_RETRY_INIT_DM_COUNT = 6;
39 constexpr int32_t DH_RETRY_INIT_DM_INTERVAL_US = 1000 * 500;
40 
~AccessManager()41 AccessManager::~AccessManager()
42 {
43     UnInit();
44 }
45 
GetInstance()46 std::shared_ptr<AccessManager> AccessManager::GetInstance()
47 {
48     static std::shared_ptr<AccessManager> instance = std::make_shared<AccessManager>();
49     return instance;
50 }
51 
Init()52 int32_t AccessManager::Init()
53 {
54     DHLOGI("start");
55     if (InitDeviceManager() != DH_FWK_SUCCESS) {
56         DHLOGE("InitDeviceManager failed");
57         return ERR_DH_FWK_ACCESS_INIT_DM_FAILED;
58     }
59     if (RegisterDevStateCallback() != DH_FWK_SUCCESS) {
60         DHLOGE("RegisterDevStateCallback failed");
61         return ERR_DH_FWK_ACCESS_REGISTER_DM_FAILED;
62     }
63     return DH_FWK_SUCCESS;
64 }
65 
UnInit()66 int32_t AccessManager::UnInit()
67 {
68     DHLOGI("start");
69     if (UnInitDeviceManager() != DH_FWK_SUCCESS) {
70         DHLOGE("UnInitDeviceManager failed");
71         return ERR_DH_FWK_ACCESS_UNINIT_DM_FAILED;
72     }
73 
74     if (UnRegisterDevStateCallback() != DH_FWK_SUCCESS) {
75         DHLOGE("UnRegisterDevStateCallback failed");
76         return ERR_DH_FWK_ACCESS_UNREGISTER_DM_FAILED;
77     }
78     return DH_FWK_SUCCESS;
79 }
80 
InitDeviceManager()81 int32_t AccessManager::InitDeviceManager()
82 {
83     DHLOGI("start");
84     return DeviceManager::GetInstance().InitDeviceManager(DH_FWK_PKG_NAME, shared_from_this());
85 }
86 
UnInitDeviceManager()87 int32_t AccessManager::UnInitDeviceManager()
88 {
89     DHLOGI("start");
90     return DeviceManager::GetInstance().UnInitDeviceManager(DH_FWK_PKG_NAME);
91 }
92 
RegisterDevStateCallback()93 int32_t AccessManager::RegisterDevStateCallback()
94 {
95     return DeviceManager::GetInstance().RegisterDevStateCallback(DH_FWK_PKG_NAME, "", shared_from_this());
96 }
97 
UnRegisterDevStateCallback()98 int32_t AccessManager::UnRegisterDevStateCallback()
99 {
100     return DeviceManager::GetInstance().UnRegisterDevStateCallback(DH_FWK_PKG_NAME);
101 }
102 
OnRemoteDied()103 void AccessManager::OnRemoteDied()
104 {
105     for (int32_t tryCount = 0; tryCount < DH_RETRY_INIT_DM_COUNT; ++tryCount) {
106         usleep(DH_RETRY_INIT_DM_INTERVAL_US);
107         if (Init() == DH_FWK_SUCCESS) {
108             DHLOGI("DeviceManager onDied, try to init success, tryCount = %{public}d", tryCount);
109             return;
110         }
111         DHLOGW("DeviceManager onDied, try to init failed, tryCount = %{public}d", tryCount);
112     }
113     DHLOGE("DeviceManager onDied, try to init has reached the maximum, but still failed");
114     return;
115 }
116 
OnDeviceOnline(const DmDeviceInfo & deviceInfo)117 void AccessManager::OnDeviceOnline(const DmDeviceInfo &deviceInfo)
118 {
119     std::lock_guard<std::mutex> lock(accessMutex_);
120     DHLOGI("AccessManager online, networkId: %{public}s, deviceName: %{public}s, deviceTypeId: %{public}d",
121         GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(),
122         deviceInfo.deviceTypeId);
123 
124     auto networkId = std::string(deviceInfo.networkId);
125     if (!IsIdLengthValid(networkId)) {
126         return;
127     }
128     auto uuid = GetUUIDByDm(networkId);
129     if (!IsIdLengthValid(uuid)) {
130         return;
131     }
132     auto udid = GetUDIDByDm(networkId);
133     if (!IsIdLengthValid(udid)) {
134         return;
135     }
136     int32_t osType = GetDeviceSystemType(deviceInfo.extraData);
137     auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
138         deviceInfo.deviceTypeId, osType);
139     DHLOGI("AccessManager online result: %{public}d, networkId: %{public}s, uuid: %{public}s, udid: %{public}s,"
140         "osType = %{public}d", ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(),
141         GetAnonyString(udid).c_str(), osType);
142 }
143 
OnDeviceOffline(const DmDeviceInfo & deviceInfo)144 void AccessManager::OnDeviceOffline(const DmDeviceInfo &deviceInfo)
145 {
146     std::lock_guard<std::mutex> lock(accessMutex_);
147     DHLOGI("AccessManager offline, networkId: %{public}s, deviceName: %{public}s, deviceTypeId: %{public}d",
148         GetAnonyString(deviceInfo.networkId).c_str(), GetAnonyString(deviceInfo.deviceName).c_str(),
149         deviceInfo.deviceTypeId);
150 
151     auto networkId = std::string(deviceInfo.networkId);
152     if (!IsIdLengthValid(networkId)) {
153         return;
154     }
155     std::string uuid = DHContext::GetInstance().GetUUIDByNetworkId(networkId);
156     if (!IsIdLengthValid(uuid)) {
157         return;
158     }
159     std::string udid = DHContext::GetInstance().GetUDIDByNetworkId(networkId);
160     if (!IsIdLengthValid(udid)) {
161         return;
162     }
163 
164     auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(networkId, uuid, udid,
165         deviceInfo.deviceTypeId);
166     DHLOGI("offline result: %{public}d, networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
167         ret, GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(), GetAnonyString(udid).c_str());
168 }
169 
OnDeviceReady(const DmDeviceInfo & deviceInfo)170 void AccessManager::OnDeviceReady(const DmDeviceInfo &deviceInfo)
171 {
172     (void)deviceInfo;
173     return;
174 }
175 
OnDeviceChanged(const DmDeviceInfo & deviceInfo)176 void AccessManager::OnDeviceChanged(const DmDeviceInfo &deviceInfo)
177 {
178     (void)deviceInfo;
179     return;
180 }
181 
CheckTrustedDeviceOnline()182 void AccessManager::CheckTrustedDeviceOnline()
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.networkId);
192         const auto uuid = GetUUIDByDm(networkId);
193         const auto udid = GetUDIDByDm(networkId);
194         int32_t osType = GetDeviceSystemType(deviceInfo.extraData);
195         DHLOGI("Send trusted device online, networkId = %{public}s, uuid = %{public}s, udid = %{public}s,"
196             "osType = %{public}d", GetAnonyString(networkId).c_str(), GetAnonyString(uuid).c_str(),
197             GetAnonyString(udid).c_str(), osType);
198         DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(networkId, uuid, udid,
199             deviceInfo.deviceTypeId, osType);
200     }
201 }
202 
Dump(const std::vector<std::string> & argsStr,std::string & result)203 int32_t AccessManager::Dump(const std::vector<std::string> &argsStr, std::string &result)
204 {
205     return DistributedHardwareManagerFactory::GetInstance().Dump(argsStr, result);
206 }
207 } // namespace DistributedHardware
208 } // namespace OHOS
209