• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "soft_bus_device_connection_listener.h"
17 #include "remote_command_manager.h"
18 #include "soft_bus_manager.h"
19 #include "device_info_manager.h"
20 #include "softbus_bus_center.h"
21 #include "constant_common.h"
22 #include "device_manager.h"
23 #include "dm_device_info.h"
24 
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28 using OHOS::DistributedHardware::DeviceStateCallback;
29 using OHOS::DistributedHardware::DmDeviceInfo;
30 using OHOS::DistributedHardware::DmInitCallback;
31 
32 namespace {
33 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
34     LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "SoftBusDeviceConnectionListener"};
35 }
36 
37 const std::string ACCESSTOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token";
38 
SoftBusDeviceConnectionListener()39 SoftBusDeviceConnectionListener::SoftBusDeviceConnectionListener()
40 {
41     ACCESSTOKEN_LOG_DEBUG(LABEL, "SoftBusDeviceConnectionListener()");
42 }
~SoftBusDeviceConnectionListener()43 SoftBusDeviceConnectionListener::~SoftBusDeviceConnectionListener()
44 {
45     ACCESSTOKEN_LOG_DEBUG(LABEL, "~SoftBusDeviceConnectionListener()");
46 }
47 
OnDeviceOnline(const DmDeviceInfo & info)48 void SoftBusDeviceConnectionListener::OnDeviceOnline(const DmDeviceInfo &info)
49 {
50     std::string networkId = info.deviceId;
51     std::string uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId);
52     std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(networkId);
53 
54     ACCESSTOKEN_LOG_INFO(LABEL,
55         "networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
56         networkId.c_str(),
57         uuid.c_str(),
58         ConstantCommon::EncryptDevId(udid).c_str());
59 
60     if (uuid != "" && udid != "") {
61         DeviceInfoManager::GetInstance().AddDeviceInfo(
62             networkId, uuid, udid, info.deviceName, std::to_string(info.deviceTypeId));
63         RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid);
64     } else {
65         ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, online failed.");
66     }
67     // no need to load local permissions by now.
68 }
69 
OnDeviceOffline(const DmDeviceInfo & info)70 void SoftBusDeviceConnectionListener::OnDeviceOffline(const DmDeviceInfo &info)
71 {
72     std::string networkId = info.deviceId;
73     std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
74     std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
75 
76     ACCESSTOKEN_LOG_INFO(LABEL,
77         "networkId: %{public}s,  uuid: %{public}s, udid: %{public}s",
78         networkId.c_str(),
79         uuid.c_str(),
80         ConstantCommon::EncryptDevId(udid).c_str());
81 
82     if (uuid != "" && udid != "") {
83         RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
84         RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
85         DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
86 
87         std::string packageName = ACCESSTOKEN_PACKAGE_NAME;
88         std::string extra = "";
89         std::vector<DmDeviceInfo> deviceList;
90 
91         int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName,
92             extra, deviceList);
93         if (ret != Constant::SUCCESS) {
94             ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret);
95             return;
96         }
97 
98         if (deviceList.empty()) {
99             ACCESSTOKEN_LOG_INFO(LABEL, "there is no remote decice online, exit tokensync process");
100 
101             exit(0);
102         }
103     } else {
104         ACCESSTOKEN_LOG_ERROR(LABEL, "uuid or udid is empty, offline failed.");
105     }
106 }
107 
OnDeviceReady(const DmDeviceInfo & info)108 void SoftBusDeviceConnectionListener::OnDeviceReady(const DmDeviceInfo &info)
109 {
110     std::string networkId = info.deviceId;
111     ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s", networkId.c_str());
112 }
113 
OnDeviceChanged(const DmDeviceInfo & info)114 void SoftBusDeviceConnectionListener::OnDeviceChanged(const DmDeviceInfo &info)
115 {
116     std::string networkId = info.deviceId;
117     ACCESSTOKEN_LOG_INFO(LABEL, "networkId: %{public}s", networkId.c_str());
118 }
119 }  // namespace AccessToken
120 }  // namespace Security
121 }  // namespace OHOS
122