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