1 /*
2  * Copyright (c) 2022-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 "soft_bus_device_connection_listener.h"
17 
18 #include "remote_command_manager.h"
19 #include "soft_bus_manager.h"
20 #include "device_info_manager.h"
21 #include "device_manager.h"
22 #include "iservice_registry.h"
23 #include "soft_bus_socket_listener.h"
24 #include "system_ability_definition.h"
25 #include "constant_common.h"
26 #include "device_manager.h"
27 #include "dm_device_info.h"
28 
29 namespace OHOS {
30 namespace Security {
31 namespace AccessToken {
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 DistributedHardware::DmDeviceInfo & info)48 void SoftBusDeviceConnectionListener::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &info)
49 {
50     std::string networkId = std::string(info.networkId);
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         ConstantCommon::EncryptDevId(networkId).c_str(),
57         ConstantCommon::EncryptDevId(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 
UnloadTokensyncService()70 void SoftBusDeviceConnectionListener::UnloadTokensyncService()
71 {
72     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
73     if (samgr == nullptr) {
74         ACCESSTOKEN_LOG_ERROR(LABEL, "Get samgr failed.");
75         return ;
76     }
77     int32_t ret = samgr->UnloadSystemAbility(TOKEN_SYNC_MANAGER_SERVICE_ID);
78     if (ret != ERR_OK) {
79         ACCESSTOKEN_LOG_ERROR(LABEL, "Remove system ability failed.");
80     }
81 }
82 
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & info)83 void SoftBusDeviceConnectionListener::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info)
84 {
85     std::string networkId = std::string(info.networkId);
86     std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
87     std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
88     if ((uuid == "") || (udid == "")) {
89         ACCESSTOKEN_LOG_ERROR(LABEL, "Uuid or udid is empty, offline failed.");
90         return;
91     }
92 
93     ACCESSTOKEN_LOG_INFO(LABEL, "NetworkId: %{public}s,  uuid: %{public}s, udid: %{public}s.",
94         ConstantCommon::EncryptDevId(networkId).c_str(),
95         ConstantCommon::EncryptDevId(uuid).c_str(),
96         ConstantCommon::EncryptDevId(udid).c_str());
97 
98     RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
99     RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
100     DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
101 
102     std::string packageName = ACCESSTOKEN_PACKAGE_NAME;
103     std::string extra = "";
104     std::vector<DistributedHardware::DmDeviceInfo> deviceList;
105 
106     int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName,
107         extra, deviceList);
108     if (ret != Constant::SUCCESS) {
109         ACCESSTOKEN_LOG_ERROR(LABEL, "GetTrustedDeviceList error, result: %{public}d", ret);
110         return;
111     }
112 
113     if (deviceList.empty()) {
114         ACCESSTOKEN_LOG_INFO(LABEL, "There is no remote decice online, exit tokensync process");
115 
116         UnloadTokensyncService();
117     }
118 }
119 
OnDeviceReady(const DistributedHardware::DmDeviceInfo & info)120 void SoftBusDeviceConnectionListener::OnDeviceReady(const DistributedHardware::DmDeviceInfo &info)
121 {
122     std::string networkId = std::string(info.networkId);
123     ACCESSTOKEN_LOG_INFO(LABEL, "NetworkId: %{public}s", ConstantCommon::EncryptDevId(networkId).c_str());
124 }
125 
OnDeviceChanged(const DistributedHardware::DmDeviceInfo & info)126 void SoftBusDeviceConnectionListener::OnDeviceChanged(const DistributedHardware::DmDeviceInfo &info)
127 {
128     std::string networkId = std::string(info.networkId);
129     ACCESSTOKEN_LOG_INFO(LABEL, "NetworkId: %{public}s", ConstantCommon::EncryptDevId(networkId).c_str());
130 }
131 }  // namespace AccessToken
132 }  // namespace Security
133 }  // namespace OHOS
134