• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "dm_device_info.h"
27 #ifdef MEMORY_MANAGER_ENABLE
28 #include "mem_mgr_client.h"
29 #endif
30 
31 namespace OHOS {
32 namespace Security {
33 namespace AccessToken {
34 
35 const std::string ACCESSTOKEN_PACKAGE_NAME = "ohos.security.distributed_access_token";
36 
SoftBusDeviceConnectionListener()37 SoftBusDeviceConnectionListener::SoftBusDeviceConnectionListener()
38 {
39     LOGD(ATM_DOMAIN, ATM_TAG, "SoftBusDeviceConnectionListener()");
40 }
~SoftBusDeviceConnectionListener()41 SoftBusDeviceConnectionListener::~SoftBusDeviceConnectionListener()
42 {
43     LOGD(ATM_DOMAIN, ATM_TAG, "~SoftBusDeviceConnectionListener()");
44 }
45 
OnDeviceOnline(const DistributedHardware::DmDeviceInfo & info)46 void SoftBusDeviceConnectionListener::OnDeviceOnline(const DistributedHardware::DmDeviceInfo &info)
47 {
48 #ifdef MEMORY_MANAGER_ENABLE
49     int32_t pid = getpid();
50     Memory::MemMgrClient::GetInstance().SetCritical(pid, true, TOKEN_SYNC_MANAGER_SERVICE_ID);
51 #endif
52     std::string networkId = std::string(info.networkId);
53     std::string uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId);
54     std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(networkId);
55 
56     LOGI(ATM_DOMAIN, ATM_TAG,
57         "networkId: %{public}s, uuid: %{public}s, udid: %{public}s",
58         ConstantCommon::EncryptDevId(networkId).c_str(),
59         ConstantCommon::EncryptDevId(uuid).c_str(),
60         ConstantCommon::EncryptDevId(udid).c_str());
61 
62     if (uuid != "" && udid != "") {
63         DeviceInfoManager::GetInstance().AddDeviceInfo(
64             networkId, uuid, udid, info.deviceName, std::to_string(info.deviceTypeId));
65         RemoteCommandManager::GetInstance().NotifyDeviceOnline(udid);
66     } else {
67         LOGE(ATM_DOMAIN, ATM_TAG, "Uuid or udid is empty, online failed.");
68     }
69     // no need to load local permissions by now.
70 }
71 
UnloadTokensyncService()72 void SoftBusDeviceConnectionListener::UnloadTokensyncService()
73 {
74     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
75     if (samgr == nullptr) {
76         LOGE(ATM_DOMAIN, ATM_TAG, "Get samgr failed.");
77         return ;
78     }
79     int32_t ret = samgr->UnloadSystemAbility(TOKEN_SYNC_MANAGER_SERVICE_ID);
80     if (ret != ERR_OK) {
81         LOGE(ATM_DOMAIN, ATM_TAG, "Remove system ability failed.");
82     }
83 }
84 
OnDeviceOffline(const DistributedHardware::DmDeviceInfo & info)85 void SoftBusDeviceConnectionListener::OnDeviceOffline(const DistributedHardware::DmDeviceInfo &info)
86 {
87     std::string networkId = std::string(info.networkId);
88     std::string uuid = DeviceInfoManager::GetInstance().ConvertToUniversallyUniqueIdOrFetch(networkId);
89     if (uuid.empty()) {
90         uuid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(networkId);
91     }
92     std::string udid = DeviceInfoManager::GetInstance().ConvertToUniqueDeviceIdOrFetch(networkId);
93     if ((uuid == "") || (udid == "")) {
94         LOGE(ATM_DOMAIN, ATM_TAG, "Uuid or udid is empty, offline failed.");
95         return;
96     }
97 
98     LOGI(ATM_DOMAIN, ATM_TAG, "NetworkId: %{public}s,  uuid: %{public}s, udid: %{public}s.",
99         ConstantCommon::EncryptDevId(networkId).c_str(),
100         ConstantCommon::EncryptDevId(uuid).c_str(),
101         ConstantCommon::EncryptDevId(udid).c_str());
102 
103     RemoteCommandManager::GetInstance().NotifyDeviceOffline(uuid);
104     RemoteCommandManager::GetInstance().NotifyDeviceOffline(udid);
105     DeviceInfoManager::GetInstance().RemoveRemoteDeviceInfo(networkId, DeviceIdType::NETWORK_ID);
106 
107     std::string packageName = ACCESSTOKEN_PACKAGE_NAME;
108     std::string extra = "";
109     std::vector<DistributedHardware::DmDeviceInfo> deviceList;
110 
111     int32_t ret = DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(packageName,
112         extra, deviceList);
113     if (ret != Constant::SUCCESS) {
114         LOGE(ATM_DOMAIN, ATM_TAG, "GetTrustedDeviceList error, result: %{public}d", ret);
115         return;
116     }
117 
118     if (deviceList.empty()) {
119         LOGI(ATM_DOMAIN, ATM_TAG, "There is no remote decice online, exit tokensync process");
120 
121         UnloadTokensyncService();
122 #ifdef MEMORY_MANAGER_ENABLE
123         int32_t pid = getpid();
124         Memory::MemMgrClient::GetInstance().SetCritical(pid, false, TOKEN_SYNC_MANAGER_SERVICE_ID);
125 #endif
126     }
127 }
128 
OnDeviceReady(const DistributedHardware::DmDeviceInfo & info)129 void SoftBusDeviceConnectionListener::OnDeviceReady(const DistributedHardware::DmDeviceInfo &info)
130 {
131     std::string networkId = std::string(info.networkId);
132     LOGI(ATM_DOMAIN, ATM_TAG, "NetworkId: %{public}s", ConstantCommon::EncryptDevId(networkId).c_str());
133 }
134 
OnDeviceChanged(const DistributedHardware::DmDeviceInfo & info)135 void SoftBusDeviceConnectionListener::OnDeviceChanged(const DistributedHardware::DmDeviceInfo &info)
136 {
137     std::string networkId = std::string(info.networkId);
138     LOGI(ATM_DOMAIN, ATM_TAG, "NetworkId: %{public}s", ConstantCommon::EncryptDevId(networkId).c_str());
139 }
140 }  // namespace AccessToken
141 }  // namespace Security
142 }  // namespace OHOS
143