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 "device_manager_service.h"
17
18 #include <dlfcn.h>
19 #include <functional>
20
21 #include "app_manager.h"
22 #include "dm_anonymous.h"
23 #include "dm_constants.h"
24 #include "dm_crypto.h"
25 #include "dm_hidumper.h"
26 #include "dm_log.h"
27 #include "dm_softbus_cache.h"
28 #include "parameter.h"
29 #include "permission_manager.h"
30 #include "relationship_sync_mgr.h"
31 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
32 #include "common_event_support.h"
33 #include "datetime_ex.h"
34 #include "iservice_registry.h"
35 #include "kv_adapter_manager.h"
36 #include "multiple_user_connector.h"
37 #if defined(SUPPORT_POWER_MANAGER)
38 #include "power_mgr_client.h"
39 #endif // SUPPORT_POWER_MANAGER
40 #if defined(SUPPORT_BLUETOOTH)
41 #include "softbus_publish.h"
42 #include "bluetooth_def.h"
43 #include "bluetooth_host.h"
44 #endif // SUPPORT_BLUETOOTH
45 #if defined(SUPPORT_WIFI)
46 #include "softbus_publish.h"
47 #include "wifi_device.h"
48 #include "wifi_msg.h"
49 #endif // SUPPORT_WIFI
50 #endif
51
52 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
53 constexpr const char* LIB_IMPL_NAME = "libdevicemanagerserviceimpl.z.so";
54 using namespace OHOS::EventFwk;
55 #else
56 constexpr const char* LIB_IMPL_NAME = "libdevicemanagerserviceimpl.so";
57 #endif
58 constexpr const char* LIB_DM_ADAPTER_NAME = "libdevicemanageradapter.z.so";
59
60 namespace OHOS {
61 namespace DistributedHardware {
62 DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerService);
63 const int32_t NORMAL = 0;
64 const int32_t SYSTEM_BASIC = 1;
65 const int32_t SYSTEM_CORE = 2;
66 constexpr const char* ALL_PKGNAME = "";
67 constexpr const char* NETWORKID = "NETWORK_ID";
68 constexpr uint32_t INVALIED_BIND_LEVEL = 0;
69 constexpr uint32_t DM_IDENTICAL_ACCOUNT = 1;
~DeviceManagerService()70 DeviceManagerService::~DeviceManagerService()
71 {
72 LOGI("DeviceManagerService destructor");
73 UnloadDMServiceImplSo();
74 UnloadDMServiceAdapter();
75 }
76
Init()77 int32_t DeviceManagerService::Init()
78 {
79 InitSoftbusListener();
80 InitDMServiceListener();
81 LOGI("Init success, dm service single instance initialized.");
82 return DM_OK;
83 }
84
InitSoftbusListener()85 int32_t DeviceManagerService::InitSoftbusListener()
86 {
87 if (softbusListener_ == nullptr) {
88 softbusListener_ = std::make_shared<SoftbusListener>();
89 }
90 SoftbusCache::GetInstance().UpdateDeviceInfoCache();
91 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
92 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
93 SubscribePublishCommonEvent();
94 QueryDependsSwitchState();
95 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
96 #endif
97 LOGI("SoftbusListener init success.");
98 return DM_OK;
99 }
100
101 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
102 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
SubscribePublishCommonEvent()103 void DeviceManagerService::SubscribePublishCommonEvent()
104 {
105 LOGI("DeviceManagerServiceImpl::SubscribeCommonEvent");
106 if (publshCommonEventManager_ == nullptr) {
107 publshCommonEventManager_ = std::make_shared<DmPublishCommonEventManager>();
108 }
109 PublishEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
110 OHOS::DistributedHardware::PublishCommonEventCallback(arg1, arg2, arg3);
111 };
112 std::vector<std::string> PublishCommonEventVec;
113 #ifdef SUPPORT_BLUETOOTH
114 PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE);
115 #endif // SUPPORT_BLUETOOTH
116
117 #ifdef SUPPORT_WIFI
118 PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
119 #endif // SUPPORT_WIFI
120 PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
121 PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
122 if (publshCommonEventManager_->SubscribePublishCommonEvent(PublishCommonEventVec, callback)) {
123 LOGI("subscribe ble and wifi and screen common event success");
124 }
125 return;
126 }
127 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
128 #endif
129
130 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
131 #if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
QueryDependsSwitchState()132 void DeviceManagerService::QueryDependsSwitchState()
133 {
134 LOGI("DeviceManagerService::QueryDependsSwitchState start.");
135 std::shared_ptr<DmPublishEventSubscriber> publishSubScriber = publshCommonEventManager_->GetSubscriber();
136 CHECK_NULL_VOID(publishSubScriber);
137 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
138 CHECK_NULL_VOID(samgr);
139 #ifdef SUPPORT_BLUETOOTH
140 if (samgr->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID) == nullptr) {
141 publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
142 } else {
143 if (Bluetooth::BluetoothHost::GetDefaultHost().IsBleEnabled()) {
144 publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON));
145 } else {
146 publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
147 }
148 }
149 #endif // SUPPORT_BLUETOOTH
150
151 #ifdef SUPPORT_WIFI
152 if (samgr->CheckSystemAbility(WIFI_DEVICE_SYS_ABILITY_ID) == nullptr) {
153 publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
154 } else {
155 bool isWifiActive = false;
156 auto wifiMgr = Wifi::WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
157 CHECK_NULL_VOID(wifiMgr);
158 wifiMgr->IsWifiActive(isWifiActive);
159 if (isWifiActive) {
160 publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED));
161 } else {
162 publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
163 }
164 }
165 #endif // SUPPORT_WIFI
166
167 #ifdef SUPPORT_POWER_MANAGER
168 if (samgr->CheckSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr) {
169 publishSubScriber->SetScreenState(DM_SCREEN_OFF);
170 } else {
171 if (OHOS::PowerMgr::PowerMgrClient::GetInstance().IsScreenOn()) {
172 publishSubScriber->SetScreenState(DM_SCREEN_ON);
173 } else {
174 publishSubScriber->SetScreenState(DM_SCREEN_OFF);
175 }
176 }
177 #else
178 publishSubScriber->SetScreenState(DM_SCREEN_ON);
179 #endif // SUPPORT_POWER_MANAGER
180 OHOS::DistributedHardware::PublishCommonEventCallback(publishSubScriber->GetBluetoothState(),
181 publishSubScriber->GetWifiState(), publishSubScriber->GetScreenState());
182 }
183 #endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
184 #endif
185
UninitSoftbusListener()186 void DeviceManagerService::UninitSoftbusListener()
187 {
188 softbusListener_ = nullptr;
189 LOGI("SoftbusListener uninit.");
190 }
191
InitDMServiceListener()192 int32_t DeviceManagerService::InitDMServiceListener()
193 {
194 if (listener_ == nullptr) {
195 listener_ = std::make_shared<DeviceManagerServiceListener>();
196 }
197 if (advertiseMgr_ == nullptr) {
198 advertiseMgr_ = std::make_shared<AdvertiseManager>(softbusListener_);
199 }
200 if (discoveryMgr_ == nullptr) {
201 discoveryMgr_ = std::make_shared<DiscoveryManager>(softbusListener_, listener_);
202 }
203 if (pinHolder_ == nullptr) {
204 pinHolder_ = std::make_shared<PinHolder>(listener_);
205 }
206 LOGI("Init success.");
207 return DM_OK;
208 }
209
UninitDMServiceListener()210 void DeviceManagerService::UninitDMServiceListener()
211 {
212 listener_ = nullptr;
213 advertiseMgr_ = nullptr;
214 discoveryMgr_ = nullptr;
215 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
216 KVAdapterManager::GetInstance().UnInit();
217 #endif
218 LOGI("Uninit.");
219 }
220
RegisterCallerAppId(const std::string &pkgName)221 void DeviceManagerService::RegisterCallerAppId(const std::string &pkgName)
222 {
223 AppManager::GetInstance().RegisterCallerAppId(pkgName);
224 }
225
UnRegisterCallerAppId(const std::string &pkgName)226 void DeviceManagerService::UnRegisterCallerAppId(const std::string &pkgName)
227 {
228 AppManager::GetInstance().UnRegisterCallerAppId(pkgName);
229 }
230
GetTrustedDeviceList(const std::string &pkgName, const std::string &extra, std::vector<DmDeviceInfo> &deviceList)231 int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
232 std::vector<DmDeviceInfo> &deviceList)
233 {
234 LOGI("Begin for pkgName = %{public}s.", pkgName.c_str());
235 if (pkgName.empty()) {
236 LOGE("Invalid parameter, pkgName is empty.");
237 return ERR_DM_INPUT_PARA_INVALID;
238 }
239 bool isOnlyShowNetworkId = false;
240 if (!PermissionManager::GetInstance().CheckNewPermission()) {
241 LOGE("The caller: %{public}s does not have permission to call GetTrustedDeviceList.", pkgName.c_str());
242 isOnlyShowNetworkId = true;
243 }
244 std::vector<DmDeviceInfo> onlineDeviceList;
245 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
246 int32_t ret = softbusListener_->GetTrustedDeviceList(onlineDeviceList);
247 if (ret != DM_OK) {
248 LOGE("GetTrustedDeviceList failed");
249 return ret;
250 }
251 if (isOnlyShowNetworkId && !onlineDeviceList.empty()) {
252 for (auto item : onlineDeviceList) {
253 DmDeviceInfo tempInfo;
254 if (memcpy_s(tempInfo.networkId, DM_MAX_DEVICE_ID_LEN, item.networkId, sizeof(item.networkId)) != 0) {
255 LOGE("get networkId: %{public}s failed", GetAnonyString(item.networkId).c_str());
256 }
257 deviceList.push_back(tempInfo);
258 }
259 return DM_OK;
260 }
261 if (onlineDeviceList.size() > 0 && IsDMServiceImplReady()) {
262 std::unordered_map<std::string, DmAuthForm> udidMap;
263 if (PermissionManager::GetInstance().CheckSystemSA(pkgName)) {
264 udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(std::string(ALL_PKGNAME));
265 } else {
266 udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(pkgName);
267 }
268 for (auto item : onlineDeviceList) {
269 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
270 ConvertUdidHashToAnoyDeviceId(item);
271 #endif
272 std::string udid = "";
273 SoftbusListener::GetUdidByNetworkId(item.networkId, udid);
274 if (udidMap.find(udid) != udidMap.end()) {
275 item.authForm = udidMap[udid];
276 deviceList.push_back(item);
277 }
278 }
279 }
280 return DM_OK;
281 }
282
ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh, bool isWakeUp)283 int32_t DeviceManagerService::ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh,
284 bool isWakeUp)
285 {
286 LOGD("Begin for pkgName = %{public}s, callerId = %{public}s, isRefresh ="
287 "%{public}d, isWakeUp = %{public}d", pkgName.c_str(), GetAnonyString(callerId).c_str(), isRefresh, isWakeUp);
288 if (!PermissionManager::GetInstance().CheckNewPermission()) {
289 LOGE("The caller does not have permission to call ShiftLNNGear, pkgName = %{public}s", pkgName.c_str());
290 return ERR_DM_NO_PERMISSION;
291 }
292 if (pkgName.empty() || callerId.empty()) {
293 LOGE("Invalid parameter, parameter is empty.");
294 return ERR_DM_INPUT_PARA_INVALID;
295 }
296 if (isRefresh) {
297 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
298 int32_t ret = softbusListener_->ShiftLNNGear(isWakeUp, callerId);
299 if (ret != DM_OK) {
300 LOGE("ShiftLNNGear error, failed ret: %{public}d", ret);
301 return ret;
302 }
303 }
304 return DM_OK;
305 }
306
GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info)307 int32_t DeviceManagerService::GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info)
308 {
309 LOGI("Begin networkId %{public}s.", GetAnonyString(networkId).c_str());
310 if (!PermissionManager::GetInstance().CheckPermission() &&
311 !PermissionManager::GetInstance().CheckNewPermission()) {
312 LOGE("The caller does not have permission to call GetDeviceInfo.");
313 return ERR_DM_NO_PERMISSION;
314 }
315 if (networkId.empty()) {
316 LOGE("Invalid parameter, networkId is empty.");
317 return ERR_DM_INPUT_PARA_INVALID;
318 }
319 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
320 int32_t ret = softbusListener_->GetDeviceInfo(networkId, info);
321 if (ret != DM_OK) {
322 LOGE("Get DeviceInfo By NetworkId failed, ret : %{public}d", ret);
323 }
324 return ret;
325 }
326
GetLocalDeviceInfo(DmDeviceInfo &info)327 int32_t DeviceManagerService::GetLocalDeviceInfo(DmDeviceInfo &info)
328 {
329 LOGI("Begin.");
330 bool isOnlyShowNetworkId = false;
331 if (!PermissionManager::GetInstance().CheckNewPermission()) {
332 LOGE("The caller does not have permission to call GetLocalDeviceInfo.");
333 isOnlyShowNetworkId = true;
334 }
335 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
336 int32_t ret = softbusListener_->GetLocalDeviceInfo(info);
337 if (ret != DM_OK) {
338 LOGE("GetLocalDeviceInfo failed");
339 return ret;
340 }
341 if (isOnlyShowNetworkId) {
342 DmDeviceInfo tempInfo;
343 if (memcpy_s(tempInfo.networkId, DM_MAX_DEVICE_ID_LEN, info.networkId, sizeof(info.networkId)) != 0) {
344 LOGE("get networkId: %{public}s failed", GetAnonyString(info.networkId).c_str());
345 }
346 info = tempInfo;
347 return DM_OK;
348 }
349 if (localDeviceId_.empty()) {
350 char localDeviceId[DEVICE_UUID_LENGTH] = {0};
351 char udidHash[DEVICE_UUID_LENGTH] = {0};
352 GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
353 if (Crypto::GetUdidHash(localDeviceId, reinterpret_cast<uint8_t *>(udidHash)) == DM_OK) {
354 localDeviceId_ = udidHash;
355 }
356 }
357 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
358 std::string udidHashTemp = "";
359 if (ConvertUdidHashToAnoyDeviceId(localDeviceId_, udidHashTemp) == DM_OK) {
360 if (memset_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
361 LOGE("GetLocalDeviceInfo memset_s failed.");
362 return ERR_DM_FAILED;
363 }
364 if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, udidHashTemp.c_str(), udidHashTemp.length()) != 0) {
365 LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHashTemp).c_str());
366 return ERR_DM_FAILED;
367 }
368 return DM_OK;
369 }
370 #endif
371 if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, localDeviceId_.c_str(), localDeviceId_.length()) != 0) {
372 LOGE("get deviceId: %{public}s failed", GetAnonyString(localDeviceId_).c_str());
373 }
374 return DM_OK;
375 }
376
GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid)377 int32_t DeviceManagerService::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
378 std::string &udid)
379 {
380 if (!PermissionManager::GetInstance().CheckPermission()) {
381 LOGE("The caller: %{public}s does not have permission to call GetUdidByNetworkId.", pkgName.c_str());
382 return ERR_DM_NO_PERMISSION;
383 }
384 if (pkgName.empty() || netWorkId.empty()) {
385 LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
386 GetAnonyString(netWorkId).c_str());
387 return ERR_DM_INPUT_PARA_INVALID;
388 }
389 return SoftbusListener::GetUdidByNetworkId(netWorkId.c_str(), udid);
390 }
391
GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid)392 int32_t DeviceManagerService::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
393 std::string &uuid)
394 {
395 if (!PermissionManager::GetInstance().CheckPermission()) {
396 LOGE("The caller: %{public}s does not have permission to call GetUuidByNetworkId.", pkgName.c_str());
397 return ERR_DM_NO_PERMISSION;
398 }
399 if (pkgName.empty() || netWorkId.empty()) {
400 LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
401 GetAnonyString(netWorkId).c_str());
402 return ERR_DM_INPUT_PARA_INVALID;
403 }
404 return SoftbusListener::GetUuidByNetworkId(netWorkId.c_str(), uuid);
405 }
406
StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo, const std::string &extra)407 int32_t DeviceManagerService::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
408 const std::string &extra)
409 {
410 if (!PermissionManager::GetInstance().CheckPermission()) {
411 LOGE("The caller: %{public}s does not have permission to call StartDeviceDiscovery.", pkgName.c_str());
412 return ERR_DM_NO_PERMISSION;
413 }
414 LOGI("Begin for pkgName = %{public}s, extra = %{public}s",
415 pkgName.c_str(), extra.c_str());
416 if (pkgName.empty()) {
417 LOGE("Invalid parameter, pkgName is empty.");
418 return ERR_DM_INPUT_PARA_INVALID;
419 }
420
421 SoftbusListener::SetHostPkgName(pkgName);
422 std::map<std::string, std::string> discParam;
423 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID,
424 std::to_string(subscribeInfo.subscribeId)));
425 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_DISC_MEDIUM, std::to_string(subscribeInfo.medium)));
426
427 std::map<std::string, std::string> filterOps;
428 filterOps.insert(std::pair<std::string, std::string>(PARAM_KEY_FILTER_OPTIONS, extra));
429 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
430 return discoveryMgr_->StartDiscovering(pkgName, discParam, filterOps);
431 }
432
StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId, const std::string &filterOptions)433 int32_t DeviceManagerService::StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId,
434 const std::string &filterOptions)
435 {
436 if (!PermissionManager::GetInstance().CheckNewPermission()) {
437 LOGE("The caller: %{public}s does not have permission to call StartDeviceDiscovery.", pkgName.c_str());
438 return ERR_DM_NO_PERMISSION;
439 }
440 LOGI("Begin for pkgName = %{public}s, filterOptions = %{public}s, subscribeId = %{public}d",
441 pkgName.c_str(), filterOptions.c_str(), subscribeId);
442 if (pkgName.empty()) {
443 LOGE("Invalid parameter, pkgName is empty.");
444 return ERR_DM_INPUT_PARA_INVALID;
445 }
446
447 SoftbusListener::SetHostPkgName(pkgName);
448 std::map<std::string, std::string> discParam;
449 discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId)));
450
451 std::map<std::string, std::string> filterOps;
452 filterOps.insert(std::pair<std::string, std::string>(PARAM_KEY_FILTER_OPTIONS, filterOptions));
453 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
454 return discoveryMgr_->StartDiscovering(pkgName, discParam, filterOps);
455 }
456
StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)457 int32_t DeviceManagerService::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
458 {
459 if (!PermissionManager::GetInstance().CheckPermission() &&
460 !PermissionManager::GetInstance().CheckNewPermission()) {
461 LOGE("The caller: %{public}s does not have permission to call StopDeviceDiscovery.", pkgName.c_str());
462 return ERR_DM_NO_PERMISSION;
463 }
464 LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
465 if (pkgName.empty()) {
466 LOGE("Invalid parameter, pkgName is empty.");
467 return ERR_DM_INPUT_PARA_INVALID;
468 }
469 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
470 return discoveryMgr_->StopDiscovering(pkgName, subscribeId);
471 }
472
PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)473 int32_t DeviceManagerService::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
474 {
475 if (!PermissionManager::GetInstance().CheckPermission()) {
476 LOGE("The caller: %{public}s does not have permission to call PublishDeviceDiscovery.", pkgName.c_str());
477 return ERR_DM_NO_PERMISSION;
478 }
479 LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
480 if (pkgName.empty()) {
481 LOGE("Invalid parameter, pkgName is empty.");
482 return ERR_DM_INPUT_PARA_INVALID;
483 }
484
485 std::map<std::string, std::string> advertiseParam;
486 advertiseParam.insert(std::pair<std::string, std::string>(PARAM_KEY_PUBLISH_ID,
487 std::to_string(publishInfo.publishId)));
488 CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
489 return advertiseMgr_->StartAdvertising(pkgName, advertiseParam);
490 }
491
UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)492 int32_t DeviceManagerService::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
493 {
494 if (!PermissionManager::GetInstance().CheckPermission()) {
495 LOGE("The caller: %{public}s does not have permission to call UnPublishDeviceDiscovery.", pkgName.c_str());
496 return ERR_DM_NO_PERMISSION;
497 }
498 if (pkgName.empty()) {
499 LOGE("Invalid parameter, pkgName is empty.");
500 return ERR_DM_INPUT_PARA_INVALID;
501 }
502 CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
503 return advertiseMgr_->StopAdvertising(pkgName, publishId);
504 }
505
AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &extra)506 int32_t DeviceManagerService::AuthenticateDevice(const std::string &pkgName, int32_t authType,
507 const std::string &deviceId, const std::string &extra)
508 {
509 if (!PermissionManager::GetInstance().CheckPermission()) {
510 LOGE("The caller: %{public}s does not have permission to call AuthenticateDevice.", pkgName.c_str());
511 return ERR_DM_NO_PERMISSION;
512 }
513 if (pkgName.empty() || deviceId.empty()) {
514 LOGE("DeviceManagerService::AuthenticateDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
515 return ERR_DM_INPUT_PARA_INVALID;
516 }
517 if (!IsDMServiceImplReady()) {
518 LOGE("AuthenticateDevice failed, instance not init or init failed.");
519 return ERR_DM_NOT_INIT;
520 }
521 std::string queryDeviceId = deviceId;
522 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
523 std::string udidHash = "";
524 if (GetUdidHashByAnoyDeviceId(deviceId, udidHash) == DM_OK) {
525 queryDeviceId = udidHash;
526 }
527 #endif
528 PeerTargetId targetId;
529 ConnectionAddrType addrType;
530 int32_t ret = SoftbusListener::GetTargetInfoFromCache(queryDeviceId, targetId, addrType);
531 if (ret != DM_OK) {
532 LOGE("AuthenticateDevice failed, cannot get target info from cached discovered device map.");
533 return ERR_DM_BIND_INPUT_PARA_INVALID;
534 }
535 std::map<std::string, std::string> bindParam;
536 bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
537 bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_BIND_EXTRA_DATA, extra));
538 bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE, std::to_string(addrType)));
539 return dmServiceImpl_->BindTarget(pkgName, targetId, bindParam);
540 }
541
UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId)542 int32_t DeviceManagerService::UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId)
543 {
544 if (!PermissionManager::GetInstance().CheckPermission()) {
545 LOGE("The caller: %{public}s does not have permission to call UnAuthenticateDevice.", pkgName.c_str());
546 return ERR_DM_NO_PERMISSION;
547 }
548 LOGI("Begin for pkgName = %{public}s, networkId = %{public}s",
549 pkgName.c_str(), GetAnonyString(networkId).c_str());
550 if (pkgName.empty() || networkId.empty()) {
551 LOGE("DeviceManagerService::UnAuthenticateDevice error: Invalid parameter, pkgName: %{public}s",
552 pkgName.c_str());
553 return ERR_DM_INPUT_PARA_INVALID;
554 }
555 std::string udid = "";
556 if (SoftbusListener::GetUdidByNetworkId(networkId.c_str(), udid) != DM_OK) {
557 LOGE("UnAuthenticateDevice GetUdidByNetworkId error: udid: %{public}s", GetAnonyString(udid).c_str());
558 return ERR_DM_FAILED;
559 }
560 char localUdid[DEVICE_UUID_LENGTH] = {0};
561 GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
562 if (!IsDMServiceImplReady()) {
563 LOGE("UnAuthenticateDevice failed, instance not init or init failed.");
564 return ERR_DM_NOT_INIT;
565 }
566 uint64_t tokenId = 0;
567 int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
568 LOGI("UnAuthenticateDevice get bindlevel %{public}d.", bindLevel);
569 if (bindLevel == INVALIED_BIND_LEVEL) {
570 LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel);
571 return ERR_DM_FAILED;
572 }
573 if (dmServiceImpl_->UnAuthenticateDevice(pkgName, udid, bindLevel) != DM_OK) {
574 LOGE("dmServiceImpl_ UnAuthenticateDevice failed.");
575 return ERR_DM_FAILED;
576 }
577 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
578 std::vector<std::string> peerUdids;
579 peerUdids.emplace_back(udid);
580 SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, bindLevel);
581 #endif
582 return DM_OK;
583 }
584
StopAuthenticateDevice(const std::string &pkgName)585 int32_t DeviceManagerService::StopAuthenticateDevice(const std::string &pkgName)
586 {
587 if (!PermissionManager::GetInstance().CheckPermission()) {
588 LOGE("The caller: %{public}s does not have permission to call StopAuthenticateDevice.", pkgName.c_str());
589 return ERR_DM_NO_PERMISSION;
590 }
591 if (pkgName.empty()) {
592 LOGE("DeviceManagerService::StopAuthenticateDevice error: Invalid parameter, pkgName: %{public}s",
593 pkgName.c_str());
594 return ERR_DM_INPUT_PARA_INVALID;
595 }
596 LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
597 if (!IsDMServiceImplReady()) {
598 LOGE("StopAuthenticateDevice failed, instance not init or init failed.");
599 return ERR_DM_NOT_INIT;
600 }
601 if (dmServiceImpl_->StopAuthenticateDevice(pkgName) != DM_OK) {
602 LOGE("dmServiceImpl_ StopAuthenticateDevice failed.");
603 return ERR_DM_FAILED;
604 }
605 return DM_OK;
606 }
607
BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId, const std::string &bindParam)608 int32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
609 const std::string &bindParam)
610 {
611 if (!PermissionManager::GetInstance().CheckNewPermission()) {
612 LOGE("The caller does not have permission to call BindDevice.");
613 return ERR_DM_NO_PERMISSION;
614 }
615 if (pkgName.empty() || deviceId.empty()) {
616 LOGE("DeviceManagerService::BindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
617 return ERR_DM_INPUT_PARA_INVALID;
618 }
619 if (!IsDMServiceImplReady()) {
620 LOGE("BindDevice failed, instance not init or init failed.");
621 return ERR_DM_NOT_INIT;
622 }
623 std::string queryDeviceId = deviceId;
624 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
625 std::string udidHash = "";
626 if (GetUdidHashByAnoyDeviceId(deviceId, udidHash) == DM_OK) {
627 queryDeviceId = udidHash;
628 }
629 #endif
630 PeerTargetId targetId;
631 ConnectionAddrType addrType;
632 int32_t ret = SoftbusListener::GetTargetInfoFromCache(queryDeviceId, targetId, addrType);
633 if (ret != DM_OK) {
634 LOGE("BindDevice failed, cannot get target info from cached discovered device map.");
635 return ERR_DM_BIND_INPUT_PARA_INVALID;
636 }
637 std::map<std::string, std::string> bindParamMap;
638 bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
639 bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_BIND_EXTRA_DATA, bindParam));
640 bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE, std::to_string(addrType)));
641 return dmServiceImpl_->BindTarget(pkgName, targetId, bindParamMap);
642 }
643
UnBindDevice(const std::string &pkgName, const std::string &udidHash)644 int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash)
645 {
646 if (!PermissionManager::GetInstance().CheckNewPermission()) {
647 LOGE("The caller does not have permission to call UnBindDevice.");
648 return ERR_DM_NO_PERMISSION;
649 }
650 LOGI("Begin for pkgName = %{public}s, udidHash = %{public}s", pkgName.c_str(), GetAnonyString(udidHash).c_str());
651 if (pkgName.empty() || udidHash.empty()) {
652 LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
653 return ERR_DM_INPUT_PARA_INVALID;
654 }
655 if (!IsDMServiceImplReady()) {
656 LOGE("UnBindDevice failed, instance not init or init failed.");
657 return ERR_DM_NOT_INIT;
658 }
659 std::string realDeviceId = udidHash;
660 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
661 std::string udidHashTemp = "";
662 if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) {
663 realDeviceId = udidHashTemp;
664 }
665 #endif
666 std::string udid = "";
667 if (SoftbusCache::GetInstance().GetUdidByUdidHash(realDeviceId, udid) != DM_OK) {
668 LOGE("Get udid by udidhash failed.");
669 return ERR_DM_FAILED;
670 }
671 char localUdid[DEVICE_UUID_LENGTH] = {0};
672 GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
673 uint64_t tokenId = 0;
674 int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
675 LOGI("UnAuthenticateDevice get bindlevel %{public}d.", bindLevel);
676 if (bindLevel == INVALIED_BIND_LEVEL) {
677 LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel);
678 return ERR_DM_FAILED;
679 }
680 if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel) != DM_OK) {
681 LOGE("dmServiceImpl_ UnBindDevice failed.");
682 return ERR_DM_FAILED;
683 }
684 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
685 std::vector<std::string> peerUdids;
686 peerUdids.emplace_back(udid);
687 SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, bindLevel);
688 #endif
689 return DM_OK;
690 }
691
SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms)692 int32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t action, const std::string ¶ms)
693 {
694 if (!PermissionManager::GetInstance().CheckPermission()) {
695 LOGE("The caller: %{public}s does not have permission to call SetUserOperation.", pkgName.c_str());
696 return ERR_DM_NO_PERMISSION;
697 }
698 if (pkgName.empty() || params.empty()) {
699 LOGE("DeviceManagerService::SetUserOperation error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
700 return ERR_DM_INPUT_PARA_INVALID;
701 }
702 if (IsDMServiceAdapterLoad()) {
703 dmServiceImplExt_->ReplyUiAction(pkgName, action, params);
704 }
705 if (!IsDMServiceImplReady()) {
706 LOGE("SetUserOperation failed, instance not init or init failed.");
707 return ERR_DM_NOT_INIT;
708 }
709 return dmServiceImpl_->SetUserOperation(pkgName, action, params);
710 }
711
HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo)712 void DeviceManagerService::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo)
713 {
714 if (IsDMServiceImplReady()) {
715 dmServiceImpl_->HandleDeviceStatusChange(devState, devInfo);
716 }
717 if (IsDMServiceAdapterLoad()) {
718 dmServiceImplExt_->HandleDeviceStatusChange(devState, devInfo);
719 }
720 }
721
OnSessionOpened(int sessionId, int result)722 int DeviceManagerService::OnSessionOpened(int sessionId, int result)
723 {
724 if (!IsDMServiceImplReady()) {
725 LOGE("OnSessionOpened failed, instance not init or init failed.");
726 return ERR_DM_NOT_INIT;
727 }
728 return dmServiceImpl_->OnSessionOpened(sessionId, result);
729 }
730
OnSessionClosed(int sessionId)731 void DeviceManagerService::OnSessionClosed(int sessionId)
732 {
733 if (!IsDMServiceImplReady()) {
734 LOGE("OnSessionClosed failed, instance not init or init failed.");
735 return;
736 }
737 dmServiceImpl_->OnSessionClosed(sessionId);
738 }
739
OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)740 void DeviceManagerService::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
741 {
742 if (!IsDMServiceImplReady()) {
743 LOGE("OnBytesReceived failed, instance not init or init failed.");
744 return;
745 }
746 dmServiceImpl_->OnBytesReceived(sessionId, data, dataLen);
747 }
748
OnPinHolderSessionOpened(int sessionId, int result)749 int DeviceManagerService::OnPinHolderSessionOpened(int sessionId, int result)
750 {
751 LOGI("In");
752 return PinHolderSession::OnSessionOpened(sessionId, result);
753 }
754
OnPinHolderSessionClosed(int sessionId)755 void DeviceManagerService::OnPinHolderSessionClosed(int sessionId)
756 {
757 LOGI("In");
758 PinHolderSession::OnSessionClosed(sessionId);
759 }
760
OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen)761 void DeviceManagerService::OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen)
762 {
763 LOGI("In");
764 PinHolderSession::OnBytesReceived(sessionId, data, dataLen);
765 }
766
RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)767 int32_t DeviceManagerService::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
768 {
769 if (!PermissionManager::GetInstance().CheckPermission()) {
770 LOGE("The caller does not have permission to call RequestCredential.");
771 return ERR_DM_NO_PERMISSION;
772 }
773 if (!IsDMServiceImplReady()) {
774 LOGE("RequestCredential failed, instance not init or init failed.");
775 return ERR_DM_NOT_INIT;
776 }
777 return dmServiceImpl_->RequestCredential(reqJsonStr, returnJsonStr);
778 }
779
ImportCredential(const std::string &pkgName, const std::string &credentialInfo)780 int32_t DeviceManagerService::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
781 {
782 if (!PermissionManager::GetInstance().CheckPermission()) {
783 LOGE("The caller: %{public}s does not have permission to call ImportCredential.",
784 pkgName.c_str());
785 return ERR_DM_NO_PERMISSION;
786 }
787 if (!IsDMServiceImplReady()) {
788 LOGE("ImportCredential failed, instance not init or init failed.");
789 return ERR_DM_NOT_INIT;
790 }
791 return dmServiceImpl_->ImportCredential(pkgName, credentialInfo);
792 }
793
DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)794 int32_t DeviceManagerService::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
795 {
796 if (!PermissionManager::GetInstance().CheckPermission()) {
797 LOGE("The caller: %{public}s does not have permission to call DeleteCredential.",
798 pkgName.c_str());
799 return ERR_DM_NO_PERMISSION;
800 }
801 if (!IsDMServiceImplReady()) {
802 LOGE("DeleteCredential failed, instance not init or init failed.");
803 return ERR_DM_NOT_INIT;
804 }
805 return dmServiceImpl_->DeleteCredential(pkgName, deleteInfo);
806 }
807
MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr)808 int32_t DeviceManagerService::MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr)
809 {
810 if (!PermissionManager::GetInstance().CheckPermission()) {
811 LOGE("The caller does not have permission to call RequestCredential.");
812 return ERR_DM_NO_PERMISSION;
813 }
814 if (!IsDMServiceImplReady()) {
815 LOGE("RequestCredential failed, instance not init or init failed.");
816 return ERR_DM_NOT_INIT;
817 }
818 return dmServiceImpl_->MineRequestCredential(pkgName, returnJsonStr);
819 }
820
CheckCredential(const std::string &pkgName, const std::string &reqJsonStr, std::string &returnJsonStr)821 int32_t DeviceManagerService::CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
822 std::string &returnJsonStr)
823 {
824 if (!PermissionManager::GetInstance().CheckPermission()) {
825 LOGE("The caller: %{public}s does not have permission to call CheckCredential.",
826 pkgName.c_str());
827 return ERR_DM_NO_PERMISSION;
828 }
829 if (!IsDMServiceImplReady()) {
830 LOGE("CheckCredential failed, instance not init or init failed.");
831 return ERR_DM_NOT_INIT;
832 }
833 return dmServiceImpl_->CheckCredential(pkgName, reqJsonStr, returnJsonStr);
834 }
835
ImportCredential(const std::string &pkgName, const std::string &reqJsonStr, std::string &returnJsonStr)836 int32_t DeviceManagerService::ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
837 std::string &returnJsonStr)
838 {
839 if (!PermissionManager::GetInstance().CheckPermission()) {
840 LOGE("The caller: %{public}s does not have permission to call ImportCredential.",
841 pkgName.c_str());
842 return ERR_DM_NO_PERMISSION;
843 }
844 if (!IsDMServiceImplReady()) {
845 LOGE("ImportCredential failed, instance not init or init failed.");
846 return ERR_DM_NOT_INIT;
847 }
848 return dmServiceImpl_->ImportCredential(pkgName, reqJsonStr, returnJsonStr);
849 }
850
DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr, std::string &returnJsonStr)851 int32_t DeviceManagerService::DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
852 std::string &returnJsonStr)
853 {
854 if (!PermissionManager::GetInstance().CheckPermission()) {
855 LOGE("The caller: %{public}s does not have permission to call DeleteCredential.",
856 pkgName.c_str());
857 return ERR_DM_NO_PERMISSION;
858 }
859 if (!IsDMServiceImplReady()) {
860 LOGE("DeleteCredential failed, instance not init or init failed.");
861 return ERR_DM_NOT_INIT;
862 }
863 return dmServiceImpl_->DeleteCredential(pkgName, reqJsonStr, returnJsonStr);
864 }
865
RegisterCredentialCallback(const std::string &pkgName)866 int32_t DeviceManagerService::RegisterCredentialCallback(const std::string &pkgName)
867 {
868 if (!PermissionManager::GetInstance().CheckPermission()) {
869 LOGE("The caller: %{public}s does not have permission to call RegisterCredentialCallback.", pkgName.c_str());
870 return ERR_DM_NO_PERMISSION;
871 }
872 if (!IsDMServiceImplReady()) {
873 LOGE("RegisterCredentialCallback failed, instance not init or init failed.");
874 return ERR_DM_NOT_INIT;
875 }
876 return dmServiceImpl_->RegisterCredentialCallback(pkgName);
877 }
878
UnRegisterCredentialCallback(const std::string &pkgName)879 int32_t DeviceManagerService::UnRegisterCredentialCallback(const std::string &pkgName)
880 {
881 if (!PermissionManager::GetInstance().CheckPermission()) {
882 LOGE("The caller: %{public}s does not have permission to call UnRegisterCredentialCallback.",
883 pkgName.c_str());
884 return ERR_DM_NO_PERMISSION;
885 }
886 if (!IsDMServiceImplReady()) {
887 LOGE("UnRegisterCredentialCallback failed, instance not init or init failed.");
888 return ERR_DM_NOT_INIT;
889 }
890 return dmServiceImpl_->UnRegisterCredentialCallback(pkgName);
891 }
892
RegisterUiStateCallback(const std::string &pkgName)893 int32_t DeviceManagerService::RegisterUiStateCallback(const std::string &pkgName)
894 {
895 if (pkgName.empty()) {
896 LOGE("DeviceManagerService::RegisterUiStateCallback error: Invalid parameter, pkgName: %{public}s",
897 pkgName.c_str());
898 return ERR_DM_INPUT_PARA_INVALID;
899 }
900 if (!PermissionManager::GetInstance().CheckPermission()) {
901 LOGE("The caller: %{public}s does not have permission to call RegisterUiStateCallback.",
902 GetAnonyString(pkgName).c_str());
903 return ERR_DM_NO_PERMISSION;
904 }
905 if (!IsDMServiceImplReady()) {
906 LOGE("RegisterUiStateCallback failed, instance not init or init failed.");
907 return ERR_DM_NOT_INIT;
908 }
909 return dmServiceImpl_->RegisterUiStateCallback(pkgName);
910 }
911
UnRegisterUiStateCallback(const std::string &pkgName)912 int32_t DeviceManagerService::UnRegisterUiStateCallback(const std::string &pkgName)
913 {
914 if (pkgName.empty()) {
915 LOGE("DeviceManagerService::UnRegisterUiStateCallback error: Invalid parameter, pkgName: %{public}s",
916 pkgName.c_str());
917 return ERR_DM_INPUT_PARA_INVALID;
918 }
919 if (!PermissionManager::GetInstance().CheckPermission()) {
920 LOGE("The caller: %{public}s does not have permission to call UnRegisterUiStateCallback.",
921 GetAnonyString(pkgName).c_str());
922 return ERR_DM_NO_PERMISSION;
923 }
924 if (!IsDMServiceImplReady()) {
925 LOGE("UnRegisterUiStateCallback failed, instance not init or init failed.");
926 return ERR_DM_NOT_INIT;
927 }
928 return dmServiceImpl_->UnRegisterUiStateCallback(pkgName);
929 }
930
IsDMServiceImplReady()931 bool DeviceManagerService::IsDMServiceImplReady()
932 {
933 std::lock_guard<std::mutex> lock(isImplLoadLock_);
934 if (isImplsoLoaded_ && (dmServiceImpl_ != nullptr)) {
935 return true;
936 }
937 void *so_handle = dlopen(LIB_IMPL_NAME, RTLD_NOW | RTLD_NODELETE);
938 if (so_handle == nullptr) {
939 LOGE("load libdevicemanagerserviceimpl so failed, errMsg: %{public}s.", dlerror());
940 return false;
941 }
942 dlerror();
943 auto func = (CreateDMServiceFuncPtr)dlsym(so_handle, "CreateDMServiceObject");
944 if (dlerror() != nullptr || func == nullptr) {
945 dlclose(so_handle);
946 LOGE("Create object function is not exist.");
947 return false;
948 }
949
950 dmServiceImpl_ = std::shared_ptr<IDeviceManagerServiceImpl>(func());
951 if (listener_ == nullptr) {
952 listener_ = std::make_shared<DeviceManagerServiceListener>();
953 }
954 if (dmServiceImpl_->Initialize(listener_) != DM_OK) {
955 dlclose(so_handle);
956 dmServiceImpl_ = nullptr;
957 isImplsoLoaded_ = false;
958 return false;
959 }
960 isImplsoLoaded_ = true;
961 return true;
962 }
963
IsDMImplSoLoaded()964 bool DeviceManagerService::IsDMImplSoLoaded()
965 {
966 LOGI("In");
967 std::lock_guard<std::mutex> lock(isImplLoadLock_);
968 return isImplsoLoaded_;
969 }
970
DmHiDumper(const std::vector<std::string>& args, std::string &result)971 int32_t DeviceManagerService::DmHiDumper(const std::vector<std::string>& args, std::string &result)
972 {
973 LOGI("HiDump GetTrustedDeviceList");
974 std::vector<HidumperFlag> dumpflag;
975 HiDumpHelper::GetInstance().GetArgsType(args, dumpflag);
976
977 for (unsigned int i = 0; i < dumpflag.size(); i++) {
978 if (dumpflag[i] == HidumperFlag::HIDUMPER_GET_TRUSTED_LIST) {
979 std::vector<DmDeviceInfo> deviceList;
980 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
981 int32_t ret = softbusListener_->GetTrustedDeviceList(deviceList);
982 if (ret != DM_OK) {
983 result.append("HiDumpHelper GetTrustedDeviceList failed");
984 LOGE("HiDumpHelper GetTrustedDeviceList failed");
985 return ret;
986 }
987
988 for (unsigned int j = 0; j < deviceList.size(); j++) {
989 HiDumpHelper::GetInstance().SetNodeInfo(deviceList[j]);
990 LOGI("DeviceManagerService::DmHiDumper SetNodeInfo.");
991 }
992 }
993 }
994 HiDumpHelper::GetInstance().HiDump(args, result);
995 return DM_OK;
996 }
997
NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event)998 int32_t DeviceManagerService::NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event)
999 {
1000 if (!PermissionManager::GetInstance().CheckPermission()) {
1001 LOGE("The caller: %{public}s does not have permission to call NotifyEvent.", pkgName.c_str());
1002 return ERR_DM_NO_PERMISSION;
1003 }
1004 if (!IsDMServiceImplReady()) {
1005 LOGE("NotifyEvent failed, instance not init or init failed.");
1006 return ERR_DM_NOT_INIT;
1007 }
1008 if (eventId == DM_NOTIFY_EVENT_ON_PINHOLDER_EVENT) {
1009 LOGI("NotifyEvent on pin holder event start.");
1010 CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
1011 return pinHolder_->NotifyPinHolderEvent(pkgName, event);
1012 }
1013 return dmServiceImpl_->NotifyEvent(pkgName, eventId, event);
1014 }
1015
LoadHardwareFwkService()1016 void DeviceManagerService::LoadHardwareFwkService()
1017 {
1018 std::string extra;
1019 std::vector<DmDeviceInfo> deviceList;
1020 int32_t ret = GetTrustedDeviceList(DM_PKG_NAME, extra, deviceList);
1021 if (ret != DM_OK) {
1022 LOGE("LoadHardwareFwkService failed, get trusted devicelist failed.");
1023 return;
1024 }
1025 if (deviceList.size() > 0) {
1026 dmServiceImpl_->LoadHardwareFwkService();
1027 }
1028 }
1029
GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId, std::string &uuid)1030 int32_t DeviceManagerService::GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId,
1031 std::string &uuid)
1032 {
1033 if (pkgName.empty()) {
1034 LOGE("Invalid parameter, pkgName is empty.");
1035 return ERR_DM_INPUT_PARA_INVALID;
1036 }
1037 LOGI("PkgName = %{public}s", pkgName.c_str());
1038 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
1039 int32_t ret = softbusListener_->GetUuidByNetworkId(networkId.c_str(), uuid);
1040 if (ret != DM_OK) {
1041 LOGE("GetUuidByNetworkId failed, ret : %{public}d", ret);
1042 return ret;
1043 }
1044
1045 std::string appId = Crypto::Sha256(AppManager::GetInstance().GetAppId());
1046 LOGI("appId = %{public}s, uuid = %{public}s.", GetAnonyString(appId).c_str(), GetAnonyString(uuid).c_str());
1047 uuid = Crypto::Sha256(appId + "_" + uuid);
1048 LOGI("encryptedUuid = %{public}s.", GetAnonyString(uuid).c_str());
1049 return DM_OK;
1050 }
1051
GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid, const std::string &appId, std::string &encryptedUuid)1052 int32_t DeviceManagerService::GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid,
1053 const std::string &appId, std::string &encryptedUuid)
1054 {
1055 if (pkgName.empty()) {
1056 LOGE("Invalid parameter, pkgName is empty.");
1057 return ERR_DM_INPUT_PARA_INVALID;
1058 }
1059 encryptedUuid = Crypto::Sha256(appId + "_" + uuid);
1060 LOGI("encryptedUuid = %{public}s.", GetAnonyString(encryptedUuid).c_str());
1061 return DM_OK;
1062 }
1063
CheckApiPermission(int32_t permissionLevel)1064 int32_t DeviceManagerService::CheckApiPermission(int32_t permissionLevel)
1065 {
1066 LOGI("PermissionLevel: %{public}d", permissionLevel);
1067 int32_t ret = ERR_DM_NO_PERMISSION;
1068 switch (permissionLevel) {
1069 case NORMAL:
1070 if (PermissionManager::GetInstance().CheckNewPermission()) {
1071 LOGI("The caller have permission to call");
1072 ret = DM_OK;
1073 }
1074 break;
1075 case SYSTEM_BASIC:
1076 if (PermissionManager::GetInstance().CheckPermission()) {
1077 LOGI("The caller have permission to call");
1078 ret = DM_OK;
1079 }
1080 break;
1081 case SYSTEM_CORE:
1082 if (PermissionManager::GetInstance().CheckMonitorPermission()) {
1083 LOGI("The caller have permission to call");
1084 ret = DM_OK;
1085 }
1086 break;
1087 default:
1088 LOGE("DM have not this permissionLevel.");
1089 break;
1090 }
1091 return ret;
1092 }
1093
GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId, int32_t &networkType)1094 int32_t DeviceManagerService::GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId,
1095 int32_t &networkType)
1096 {
1097 if (!PermissionManager::GetInstance().CheckPermission()) {
1098 LOGE("The caller: %{public}s does not have permission to call GetNetworkTypeByNetworkId.", pkgName.c_str());
1099 return ERR_DM_NO_PERMISSION;
1100 }
1101 LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
1102 if (pkgName.empty() || netWorkId.empty()) {
1103 LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
1104 GetAnonyString(netWorkId).c_str());
1105 return ERR_DM_INPUT_PARA_INVALID;
1106 }
1107 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
1108 return softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
1109 }
1110
ImportAuthCode(const std::string &pkgName, const std::string &authCode)1111 int32_t DeviceManagerService::ImportAuthCode(const std::string &pkgName, const std::string &authCode)
1112 {
1113 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1114 LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.", pkgName.c_str());
1115 return ERR_DM_NO_PERMISSION;
1116 }
1117 std::string processName = "";
1118 if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
1119 LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
1120 return ERR_DM_FAILED;
1121 }
1122 if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
1123 LOGE("The caller: %{public}s is not in white list.", processName.c_str());
1124 return ERR_DM_INPUT_PARA_INVALID;
1125 }
1126 LOGI("DeviceManagerService::ImportAuthCode begin.");
1127 if (authCode.empty() || pkgName.empty()) {
1128 LOGE("Invalid parameter, authCode: %{public}s.", GetAnonyString(authCode).c_str());
1129 return ERR_DM_INPUT_PARA_INVALID;
1130 }
1131 if (!IsDMServiceImplReady()) {
1132 LOGE("ImportAuthCode failed, instance not init or init failed.");
1133 return ERR_DM_NOT_INIT;
1134 }
1135 return dmServiceImpl_->ImportAuthCode(pkgName, authCode);
1136 }
1137
ExportAuthCode(std::string &authCode)1138 int32_t DeviceManagerService::ExportAuthCode(std::string &authCode)
1139 {
1140 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1141 LOGE("The caller does not have permission to call ExportAuthCode.");
1142 return ERR_DM_NO_PERMISSION;
1143 }
1144 std::string processName = "";
1145 if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
1146 LOGE("Get caller process name failed, processName: %{public}s.", processName.c_str());
1147 return ERR_DM_FAILED;
1148 }
1149 if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
1150 LOGE("The caller: %{public}s is not in white list.", processName.c_str());
1151 return ERR_DM_INPUT_PARA_INVALID;
1152 }
1153 if (!IsDMServiceImplReady()) {
1154 LOGE("ExportAuthCode failed, instance not init or init failed.");
1155 return ERR_DM_NOT_INIT;
1156 }
1157 LOGI("DeviceManagerService::ExportAuthCode begin.");
1158 return dmServiceImpl_->ExportAuthCode(authCode);
1159 }
1160
UnloadDMServiceImplSo()1161 void DeviceManagerService::UnloadDMServiceImplSo()
1162 {
1163 LOGI("Start.");
1164 std::lock_guard<std::mutex> lock(isImplLoadLock_);
1165 if (dmServiceImpl_ != nullptr) {
1166 dmServiceImpl_->Release();
1167 }
1168 void *so_handle = dlopen(LIB_IMPL_NAME, RTLD_NOW | RTLD_NOLOAD);
1169 if (so_handle != nullptr) {
1170 LOGI("DeviceManagerService so_handle is not nullptr.");
1171 dlclose(so_handle);
1172 }
1173 }
1174
IsDMServiceAdapterLoad()1175 bool DeviceManagerService::IsDMServiceAdapterLoad()
1176 {
1177 LOGI("Start.");
1178 if (listener_ == nullptr) {
1179 LOGE("Dm service is not init.");
1180 return false;
1181 }
1182 std::lock_guard<std::mutex> lock(isAdapterLoadLock_);
1183 if (isAdapterSoLoaded_ && (dmServiceImplExt_ != nullptr)) {
1184 return true;
1185 }
1186
1187 void *so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NODELETE);
1188 if (so_handle == nullptr) {
1189 LOGE("load dm service adapter so failed.");
1190 return false;
1191 }
1192 dlerror();
1193 auto func = (CreateDMServiceImplExtFuncPtr)dlsym(so_handle, "CreateDMServiceImplExtObject");
1194 if (dlerror() != nullptr || func == nullptr) {
1195 dlclose(so_handle);
1196 LOGE("Create object function is not exist.");
1197 return false;
1198 }
1199
1200 dmServiceImplExt_ = std::shared_ptr<IDMServiceImplExt>(func());
1201 if (dmServiceImplExt_->Initialize(listener_) != DM_OK) {
1202 dlclose(so_handle);
1203 dmServiceImplExt_ = nullptr;
1204 isAdapterSoLoaded_ = false;
1205 LOGE("dm service adapter impl ext init failed.");
1206 return false;
1207 }
1208 isAdapterSoLoaded_ = true;
1209 LOGI("Success.");
1210 return true;
1211 }
1212
UnloadDMServiceAdapter()1213 void DeviceManagerService::UnloadDMServiceAdapter()
1214 {
1215 LOGI("Start.");
1216 std::lock_guard<std::mutex> lock(isAdapterLoadLock_);
1217 if (dmServiceImplExt_ != nullptr) {
1218 dmServiceImplExt_->Release();
1219 }
1220 dmServiceImplExt_ = nullptr;
1221
1222 void *so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NOLOAD);
1223 if (so_handle != nullptr) {
1224 LOGI("dm service adapter so_handle is not nullptr.");
1225 dlclose(so_handle);
1226 }
1227 }
1228
StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)1229 int32_t DeviceManagerService::StartDiscovering(const std::string &pkgName,
1230 const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)
1231 {
1232 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1233 LOGE("The caller does not have permission to call");
1234 return ERR_DM_NO_PERMISSION;
1235 }
1236 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1237 if (pkgName.empty()) {
1238 LOGE("Invalid parameter, pkgName is empty.");
1239 return ERR_DM_INPUT_PARA_INVALID;
1240 }
1241 if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) {
1242 LOGI("StartDiscovering input MetaType = %{public}s", (discoverParam.find(PARAM_KEY_META_TYPE)->second).c_str());
1243 }
1244 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
1245 return discoveryMgr_->StartDiscovering(pkgName, discoverParam, filterOptions);
1246 }
1247
StopDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam)1248 int32_t DeviceManagerService::StopDiscovering(const std::string &pkgName,
1249 const std::map<std::string, std::string> &discoverParam)
1250 {
1251 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1252 LOGE("The caller does not have permission to call");
1253 return ERR_DM_NO_PERMISSION;
1254 }
1255 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1256 if (pkgName.empty()) {
1257 LOGE("Invalid parameter, pkgName is empty.");
1258 return ERR_DM_INPUT_PARA_INVALID;
1259 }
1260 uint16_t subscribeId = -1;
1261 if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) {
1262 subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str());
1263 }
1264 if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) {
1265 LOGI("StopDiscovering input MetaType = %{public}s", (discoverParam.find(PARAM_KEY_META_TYPE)->second).c_str());
1266 }
1267 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
1268 return discoveryMgr_->StopDiscovering(pkgName, subscribeId);
1269 }
1270
EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)1271 int32_t DeviceManagerService::EnableDiscoveryListener(const std::string &pkgName,
1272 const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)
1273 {
1274 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1275 LOGE("The caller does not have permission to call");
1276 return ERR_DM_NO_PERMISSION;
1277 }
1278 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1279 if (pkgName.empty()) {
1280 LOGE("Invalid parameter, pkgName is empty.");
1281 return ERR_DM_INPUT_PARA_INVALID;
1282 }
1283 SoftbusListener::SetHostPkgName(pkgName);
1284 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
1285 return discoveryMgr_->EnableDiscoveryListener(pkgName, discoverParam, filterOptions);
1286 }
1287
DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam)1288 int32_t DeviceManagerService::DisableDiscoveryListener(const std::string &pkgName,
1289 const std::map<std::string, std::string> &extraParam)
1290 {
1291 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1292 LOGE("The caller does not have permission to call");
1293 return ERR_DM_NO_PERMISSION;
1294 }
1295 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1296 if (pkgName.empty()) {
1297 LOGE("Invalid parameter, pkgName is empty.");
1298 return ERR_DM_INPUT_PARA_INVALID;
1299 }
1300 CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
1301 return discoveryMgr_->DisableDiscoveryListener(pkgName, extraParam);
1302 }
1303
StartAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam)1304 int32_t DeviceManagerService::StartAdvertising(const std::string &pkgName,
1305 const std::map<std::string, std::string> &advertiseParam)
1306 {
1307 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1308 LOGE("The caller does not have permission to call");
1309 return ERR_DM_NO_PERMISSION;
1310 }
1311 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1312 if (pkgName.empty()) {
1313 LOGE("Invalid parameter, pkgName is empty.");
1314 return ERR_DM_INPUT_PARA_INVALID;
1315 }
1316 CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
1317 return advertiseMgr_->StartAdvertising(pkgName, advertiseParam);
1318 }
1319
StopAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam)1320 int32_t DeviceManagerService::StopAdvertising(const std::string &pkgName,
1321 const std::map<std::string, std::string> &advertiseParam)
1322 {
1323 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1324 LOGE("The caller does not have permission to call");
1325 return ERR_DM_NO_PERMISSION;
1326 }
1327 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1328 if (pkgName.empty()) {
1329 LOGE("Invalid parameter, pkgName is empty.");
1330 return ERR_DM_INPUT_PARA_INVALID;
1331 }
1332 if (advertiseParam.find(PARAM_KEY_META_TYPE) != advertiseParam.end()) {
1333 LOGI("StopAdvertising input MetaType=%{public}s", (advertiseParam.find(PARAM_KEY_META_TYPE)->second).c_str());
1334 }
1335 int32_t publishId = -1;
1336 if (advertiseParam.find(PARAM_KEY_PUBLISH_ID) != advertiseParam.end()) {
1337 publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str());
1338 }
1339 CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
1340 return advertiseMgr_->StopAdvertising(pkgName, publishId);
1341 }
1342
BindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map<std::string, std::string> &bindParam)1343 int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
1344 const std::map<std::string, std::string> &bindParam)
1345 {
1346 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1347 LOGE("The caller does not have permission to call");
1348 return ERR_DM_NO_PERMISSION;
1349 }
1350 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1351 if (pkgName.empty()) {
1352 LOGE("Invalid parameter, pkgName is empty.");
1353 return ERR_DM_INPUT_PARA_INVALID;
1354 }
1355 if (!IsDMServiceImplReady()) {
1356 LOGE("BindTarget failed, DMServiceImpl instance not init or init failed.");
1357 return ERR_DM_NOT_INIT;
1358 }
1359 if (bindParam.find(PARAM_KEY_META_TYPE) == bindParam.end()) {
1360 LOGI("BindTarget stardard begin.");
1361 if (targetId.wifiIp.empty() || targetId.wifiIp.length() > IP_STR_MAX_LEN) {
1362 return dmServiceImpl_->BindTarget(pkgName, targetId, bindParam);
1363 }
1364 ConnectionAddrType ipAddrType;
1365 std::map<std::string, std::string> &noConstBindParam =
1366 const_cast<std::map<std::string, std::string> &>(bindParam);
1367 if (SoftbusListener::GetIPAddrTypeFromCache(targetId.deviceId, targetId.wifiIp, ipAddrType) == DM_OK) {
1368 noConstBindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE,
1369 std::to_string(ipAddrType)));
1370 }
1371 const std::map<std::string, std::string> &constBindParam =
1372 const_cast<const std::map<std::string, std::string> &>(noConstBindParam);
1373 return dmServiceImpl_->BindTarget(pkgName, targetId, constBindParam);
1374 }
1375 if (!IsDMServiceAdapterLoad()) {
1376 LOGE("BindTarget failed, adapter instance not init or init failed.");
1377 return ERR_DM_UNSUPPORTED_METHOD;
1378 }
1379 LOGI("BindTarget unstardard begin.");
1380 return dmServiceImplExt_->BindTargetExt(pkgName, targetId, bindParam);
1381 }
1382
UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId, const std::map<std::string, std::string> &unbindParam)1383 int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
1384 const std::map<std::string, std::string> &unbindParam)
1385 {
1386 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1387 LOGE("The caller does not have permission to call");
1388 return ERR_DM_NO_PERMISSION;
1389 }
1390 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1391 if (pkgName.empty()) {
1392 LOGE("Invalid parameter, pkgName is empty.");
1393 return ERR_DM_INPUT_PARA_INVALID;
1394 }
1395 if (!IsDMServiceAdapterLoad()) {
1396 LOGE("UnbindTarget failed, instance not init or init failed.");
1397 return ERR_DM_UNSUPPORTED_METHOD;
1398 }
1399 if (unbindParam.find(PARAM_KEY_META_TYPE) == unbindParam.end()) {
1400 LOGE("input unbind parameter not contains META_TYPE, dm service adapter not supported.");
1401 return ERR_DM_INPUT_PARA_INVALID;
1402 }
1403 return dmServiceImplExt_->UnbindTargetExt(pkgName, targetId, unbindParam);
1404 }
1405
RegisterPinHolderCallback(const std::string &pkgName)1406 int32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName)
1407 {
1408 if (!PermissionManager::GetInstance().CheckPermission()) {
1409 LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.", pkgName.c_str());
1410 return ERR_DM_NO_PERMISSION;
1411 }
1412 std::string processName = "";
1413 if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
1414 LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
1415 return ERR_DM_FAILED;
1416 }
1417 if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
1418 LOGE("The caller: %{public}s is not in white list.", processName.c_str());
1419 return ERR_DM_INPUT_PARA_INVALID;
1420 }
1421 LOGI("DeviceManagerService::RegisterPinHolderCallback begin.");
1422 if (pkgName.empty()) {
1423 LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
1424 return ERR_DM_INPUT_PARA_INVALID;
1425 }
1426 CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
1427 return pinHolder_->RegisterPinHolderCallback(pkgName);
1428 }
1429
CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId, DmPinType pinType, const std::string &payload)1430 int32_t DeviceManagerService::CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
1431 DmPinType pinType, const std::string &payload)
1432 {
1433 if (!PermissionManager::GetInstance().CheckPermission()) {
1434 LOGE("The caller: %{public}s does not have permission to call CreatePinHolder.", pkgName.c_str());
1435 return ERR_DM_NO_PERMISSION;
1436 }
1437 std::string processName = "";
1438 if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
1439 LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
1440 return ERR_DM_FAILED;
1441 }
1442 if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
1443 LOGE("The caller: %{public}s is not in white list.", processName.c_str());
1444 return ERR_DM_INPUT_PARA_INVALID;
1445 }
1446 LOGI("DeviceManagerService::CreatePinHolder begin.");
1447 if (pkgName.empty()) {
1448 LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
1449 return ERR_DM_INPUT_PARA_INVALID;
1450 }
1451 CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
1452 return pinHolder_->CreatePinHolder(pkgName, targetId, pinType, payload);
1453 }
1454
DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId, DmPinType pinType, const std::string &payload)1455 int32_t DeviceManagerService::DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
1456 DmPinType pinType, const std::string &payload)
1457 {
1458 if (!PermissionManager::GetInstance().CheckPermission()) {
1459 LOGE("The caller: %{public}s does not have permission to call DestroyPinHolder.", pkgName.c_str());
1460 return ERR_DM_NO_PERMISSION;
1461 }
1462 std::string processName = "";
1463 if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
1464 LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
1465 return ERR_DM_FAILED;
1466 }
1467 if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
1468 LOGE("The caller: %{public}s is not in white list.", processName.c_str());
1469 return ERR_DM_INPUT_PARA_INVALID;
1470 }
1471 LOGI("Begin.");
1472 if (pkgName.empty()) {
1473 LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
1474 return ERR_DM_INPUT_PARA_INVALID;
1475 }
1476 CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
1477 return pinHolder_->DestroyPinHolder(pkgName, targetId, pinType, payload);
1478 }
1479
DpAclAdd(const std::string &udid)1480 int32_t DeviceManagerService::DpAclAdd(const std::string &udid)
1481 {
1482 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1483 LOGE("The caller does not have permission to call DpAclAdd.");
1484 return ERR_DM_NO_PERMISSION;
1485 }
1486 LOGI("Start.");
1487 if (!IsDMServiceImplReady()) {
1488 LOGE("DpAclAdd failed, instance not init or init failed.");
1489 return ERR_DM_NOT_INIT;
1490 }
1491 dmServiceImpl_->DpAclAdd(udid);
1492 LOGI("DeviceManagerService::DpAclAdd completed");
1493 return DM_OK;
1494 }
1495
GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &securityLevel)1496 int32_t DeviceManagerService::GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId,
1497 int32_t &securityLevel)
1498 {
1499 LOGI("Begin pkgName: %{public}s, networkId: %{public}s",
1500 pkgName.c_str(), GetAnonyString(networkId).c_str());
1501 if (!PermissionManager::GetInstance().CheckPermission()) {
1502 LOGE("The caller: %{public}s does not have permission to call GetDeviceSecurityLevel.", pkgName.c_str());
1503 return ERR_DM_NO_PERMISSION;
1504 }
1505 if (pkgName.empty() || networkId.empty()) {
1506 LOGE("Invalid parameter, pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
1507 GetAnonyString(networkId).c_str());
1508 return ERR_DM_INPUT_PARA_INVALID;
1509 }
1510 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
1511 int32_t ret = softbusListener_->GetDeviceSecurityLevel(networkId.c_str(), securityLevel);
1512 if (ret != DM_OK) {
1513 LOGE("GetDeviceSecurityLevel failed, ret = %{public}d", ret);
1514 return ret;
1515 }
1516 return DM_OK;
1517 }
1518
IsSameAccount(const std::string &networkId)1519 int32_t DeviceManagerService::IsSameAccount(const std::string &networkId)
1520 {
1521 LOGI("NetworkId %{public}s.", GetAnonyString(networkId).c_str());
1522 if (!PermissionManager::GetInstance().CheckPermission()) {
1523 return ERR_DM_NO_PERMISSION;
1524 }
1525 std::string udid = "";
1526 if (SoftbusListener::GetUdidByNetworkId(networkId.c_str(), udid) != DM_OK) {
1527 LOGE("DeviceManagerService::IsSameAccount error: udid: %{public}s", GetAnonyString(udid).c_str());
1528 return ERR_DM_INPUT_PARA_INVALID;
1529 }
1530 if (!IsDMServiceImplReady()) {
1531 LOGE("IsSameAccount failed, instance not init or init failed.");
1532 return ERR_DM_NOT_INIT;
1533 }
1534 return dmServiceImpl_->IsSameAccount(udid);
1535 }
1536
CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)1537 bool DeviceManagerService::CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
1538 {
1539 if (!PermissionManager::GetInstance().CheckPermission()) {
1540 LOGE("The caller: %{public}s does not have permission to call CheckAccessControl.", caller.pkgName.c_str());
1541 return false;
1542 }
1543 if (!IsDMServiceImplReady()) {
1544 LOGE("CheckAccessControl failed, instance not init or init failed.");
1545 return false;
1546 }
1547 std::string srcUdid = "";
1548 SoftbusListener::GetUdidByNetworkId(caller.networkId.c_str(), srcUdid);
1549 std::string sinkUdid = "";
1550 SoftbusListener::GetUdidByNetworkId(callee.networkId.c_str(), sinkUdid);
1551 return dmServiceImpl_->CheckAccessControl(caller, srcUdid, callee, sinkUdid);
1552 }
1553
CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)1554 bool DeviceManagerService::CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
1555 {
1556 if (!PermissionManager::GetInstance().CheckPermission()) {
1557 LOGE("The caller: %{public}s does not have permission to call CheckIsSameAccount.", caller.pkgName.c_str());
1558 return false;
1559 }
1560 if (!IsDMServiceImplReady()) {
1561 LOGE("CheckIsSameAccount failed, instance not init or init failed.");
1562 return false;
1563 }
1564 std::string srcUdid = "";
1565 SoftbusListener::GetUdidByNetworkId(caller.networkId.c_str(), srcUdid);
1566 std::string sinkUdid = "";
1567 SoftbusListener::GetUdidByNetworkId(callee.networkId.c_str(), sinkUdid);
1568 return dmServiceImpl_->CheckIsSameAccount(caller, srcUdid, callee, sinkUdid);
1569 }
1570
InitAccountInfo()1571 int32_t DeviceManagerService::InitAccountInfo()
1572 {
1573 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1574 SubscribeAccountCommonEvent();
1575 LOGI("Success.");
1576 #endif
1577 return DM_OK;
1578 }
1579
InitScreenLockEvent()1580 int32_t DeviceManagerService::InitScreenLockEvent()
1581 {
1582 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1583 SubscribeScreenLockEvent();
1584 LOGI("Success.");
1585 #endif
1586 return DM_OK;
1587 }
1588
1589 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
SubscribeAccountCommonEvent()1590 void DeviceManagerService::SubscribeAccountCommonEvent()
1591 {
1592 LOGI("Start");
1593 if (accountCommonEventManager_ == nullptr) {
1594 accountCommonEventManager_ = std::make_shared<DmAccountCommonEventManager>();
1595 }
1596 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
1597 this->AccountCommonEventCallback(arg1, arg2);
1598 };
1599 std::vector<std::string> AccountCommonEventVec;
1600 AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
1601 AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
1602 AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
1603 AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
1604 if (accountCommonEventManager_->SubscribeAccountCommonEvent(AccountCommonEventVec, callback)) {
1605 LOGI("Success");
1606 }
1607 return;
1608 }
1609
SubscribeScreenLockEvent()1610 void DeviceManagerService::SubscribeScreenLockEvent()
1611 {
1612 LOGI("Start");
1613 if (screenCommonEventManager_ == nullptr) {
1614 screenCommonEventManager_ = std::make_shared<DmScreenCommonEventManager>();
1615 }
1616 ScreenEventCallback callback = [=](const auto &arg1) { this->ScreenCommonEventCallback(arg1); };
1617 std::vector<std::string> screenEventVec;
1618 screenEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
1619 if (screenCommonEventManager_->SubscribeScreenCommonEvent(screenEventVec, callback)) {
1620 LOGI("Success");
1621 }
1622 return;
1623 }
1624
AccountCommonEventCallback(int32_t userId, const std::string commonEventType)1625 void DeviceManagerService::AccountCommonEventCallback(int32_t userId, const std::string commonEventType)
1626 {
1627 LOGI("CommonEventType: %{public}s, userId: %{public}d", commonEventType.c_str(), userId);
1628 if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
1629 if (MultipleUserConnector::GetSwitchOldUserId() > 0 &&
1630 (userId != MultipleUserConnector::GetSwitchOldUserId())) {
1631 HandleUserSwitched(MultipleUserConnector::GetSwitchOldUserId());
1632 }
1633 MultipleUserConnector::SetSwitchOldUserId(userId);
1634 MultipleUserConnector::SetSwitchOldAccountId(MultipleUserConnector::GetOhosAccountId());
1635 MultipleUserConnector::SetSwitchOldAccountName(MultipleUserConnector::GetOhosAccountName());
1636 if (IsDMServiceAdapterLoad()) {
1637 dmServiceImplExt_->AccountUserSwitched(userId, MultipleUserConnector::GetOhosAccountId());
1638 }
1639 } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
1640 MultipleUserConnector::SetSwitchOldAccountId(MultipleUserConnector::GetOhosAccountId());
1641 MultipleUserConnector::SetSwitchOldAccountName(MultipleUserConnector::GetOhosAccountName());
1642 } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
1643 HandleAccountLogout(MultipleUserConnector::GetCurrentAccountUserID(),
1644 MultipleUserConnector::GetSwitchOldAccountId());
1645 } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
1646 HandleUserRemoved(userId);
1647 } else {
1648 LOGE("Invalied account common event.");
1649 }
1650 return;
1651 }
1652
HandleAccountLogout(int32_t userId, const std::string &accountId)1653 void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string &accountId)
1654 {
1655 LOGI("UserId %{public}d, accountId %{public}s.", userId, GetAnonyString(accountId).c_str());
1656 if (IsDMServiceAdapterLoad()) {
1657 dmServiceImplExt_->AccountIdLogout(userId, accountId);
1658 }
1659 if (!IsDMServiceImplReady()) {
1660 LOGE("Init impl failed.");
1661 return;
1662 }
1663 std::map<std::string, int32_t> deviceMap;
1664 std::vector<std::string> peerUdids;
1665 deviceMap = dmServiceImpl_->GetDeviceIdAndBindType(userId, accountId);
1666 for (const auto &item : deviceMap) {
1667 if (item.second == DM_IDENTICAL_ACCOUNT) {
1668 dmServiceImpl_->HandleIdentAccountLogout(item.first, userId, accountId);
1669 peerUdids.emplace_back(item.first);
1670 }
1671 }
1672 if (!peerUdids.empty()) {
1673 char accountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
1674 if (Crypto::GetAccountIdHash(accountId, reinterpret_cast<uint8_t *>(accountIdHash)) != DM_OK) {
1675 LOGE("GetAccountHash failed.");
1676 return;
1677 }
1678 std::string accountName = MultipleUserConnector::GetSwitchOldAccountName();
1679 SendAccountLogoutBroadCast(peerUdids, std::string(accountIdHash), accountName, userId);
1680 }
1681 }
1682
HandleUserSwitched(int32_t switchUserId)1683 void DeviceManagerService::HandleUserSwitched(int32_t switchUserId)
1684 {
1685 LOGI("switchUserId: %{public}d.", switchUserId);
1686 if (IsDMServiceImplReady()) {
1687 dmServiceImpl_->HandleUserSwitched(switchUserId);
1688 }
1689 }
1690
HandleUserRemoved(int32_t preUserId)1691 void DeviceManagerService::HandleUserRemoved(int32_t preUserId)
1692 {
1693 LOGI("PreUserId %{public}d.", preUserId);
1694 if (IsDMServiceImplReady()) {
1695 dmServiceImpl_->HandleUserRemoved(preUserId);
1696 }
1697 }
1698
SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids, const std::string &accountId, const std::string &accountName, int32_t userId)1699 void DeviceManagerService::SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids,
1700 const std::string &accountId, const std::string &accountName, int32_t userId)
1701 {
1702 LOGI("accountId %{public}s, accountName %{public}s, userId %{public}d.", GetAnonyString(accountId).c_str(),
1703 GetAnonyString(accountName).c_str(), userId);
1704 RelationShipChangeMsg msg;
1705 msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
1706 msg.userId = static_cast<uint32_t>(userId);
1707 msg.peerUdids = peerUdids;
1708 msg.accountId = accountId;
1709 msg.accountName = accountName;
1710 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
1711 CHECK_NULL_VOID(softbusListener_);
1712 softbusListener_->SendAclChangedBroadcast(broadCastMsg);
1713 }
1714
ScreenCommonEventCallback(std::string commonEventType)1715 void DeviceManagerService::ScreenCommonEventCallback(std::string commonEventType)
1716 {
1717 if (!IsDMImplSoLoaded()) {
1718 LOGE("ScreenCommonEventCallback failed, instance not init or init failed.");
1719 return;
1720 }
1721 dmServiceImpl_->ScreenCommonEventCallback(commonEventType);
1722 }
1723 #endif
1724
HandleDeviceNotTrust(const std::string &msg)1725 void DeviceManagerService::HandleDeviceNotTrust(const std::string &msg)
1726 {
1727 LOGI("Start.");
1728 if (msg.empty()) {
1729 LOGE("DeviceManagerService::HandleDeviceNotTrust msg is empty.");
1730 return;
1731 }
1732 nlohmann::json msgJsonObj = nlohmann::json::parse(msg, nullptr, false);
1733 if (msgJsonObj.is_discarded()) {
1734 LOGE("HandleDeviceNotTrust msg prase error.");
1735 return;
1736 }
1737 if (!IsString(msgJsonObj, NETWORKID)) {
1738 LOGE("HandleDeviceNotTrust msg not contain networkId.");
1739 return;
1740 }
1741 std::string networkId = msgJsonObj[NETWORKID].get<std::string>();
1742 std::string udid = "";
1743 SoftbusCache::GetInstance().GetUdidFromCache(networkId.c_str(), udid);
1744 LOGI("NetworkId: %{public}s, udid: %{public}s.",
1745 GetAnonyString(networkId).c_str(), GetAnonyString(udid).c_str());
1746 if (IsDMServiceImplReady()) {
1747 dmServiceImpl_->HandleDeviceNotTrust(udid);
1748 }
1749 if (IsDMServiceAdapterLoad()) {
1750 dmServiceImplExt_->HandleDeviceNotTrust(udid);
1751 }
1752 return;
1753 }
1754
SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy)1755 int32_t DeviceManagerService::SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy)
1756 {
1757 if (!PermissionManager::GetInstance().CheckNewPermission()) {
1758 LOGE("The caller does not have permission to call");
1759 return ERR_DM_NO_PERMISSION;
1760 }
1761 LOGI("Start for pkgName = %{public}s", pkgName.c_str());
1762 if (pkgName.empty()) {
1763 LOGE("Invalid parameter, pkgName is empty.");
1764 return ERR_DM_INPUT_PARA_INVALID;
1765 }
1766 auto policyStrategyIter = policy.find(PARAM_KEY_POLICY_STRATEGY_FOR_BLE);
1767 if (policyStrategyIter == policy.end()) {
1768 LOGE("Invalid parameter, DM_POLICY_STRATEGY_FOR_BLE is empty.");
1769 return ERR_DM_INPUT_PARA_INVALID;
1770 }
1771 auto timeOutIter = policy.find(PARAM_KEY_POLICY_TIME_OUT);
1772 if (timeOutIter == policy.end()) {
1773 LOGE("Invalid parameter, DM_POLICY_TIMEOUT is empty.");
1774 return ERR_DM_INPUT_PARA_INVALID;
1775 }
1776 if (!IsNumberString(policyStrategyIter->second)) {
1777 LOGE("Invalid parameter, DM_POLICY_STRATEGY_FOR_BLE is not number.");
1778 return ERR_DM_INPUT_PARA_INVALID;
1779 }
1780 if (!IsNumberString(timeOutIter->second)) {
1781 LOGE("Invalid parameter, DM_POLICY_TIMEOUT is not number.");
1782 return ERR_DM_INPUT_PARA_INVALID;
1783 }
1784 int32_t policyStrategy = std::atoi(policyStrategyIter->second.c_str());
1785 int32_t timeOut = std::atoi(timeOutIter->second.c_str());
1786 LOGD("strategy: %{public}d, timeOut: %{public}d", policyStrategy, timeOut);
1787 if (!IsDMServiceAdapterLoad()) {
1788 LOGE("SetDnPolicy failed, instance not init or init failed.");
1789 return ERR_DM_UNSUPPORTED_METHOD;
1790 }
1791 return dmServiceImplExt_->SetDnPolicy(policyStrategy, timeOut);
1792 }
1793
1794 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo)1795 void DeviceManagerService::ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo)
1796 {
1797 std::string udidHashTemp = "";
1798 if (ConvertUdidHashToAnoyDeviceId(deviceInfo.deviceId, udidHashTemp) == DM_OK) {
1799 if (memset_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
1800 LOGE("ConvertUdidHashToAnoyDeviceId memset_s failed.");
1801 return;
1802 }
1803 if (memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udidHashTemp.c_str(), udidHashTemp.length()) != 0) {
1804 LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHashTemp).c_str());
1805 }
1806 }
1807 }
1808
ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &result)1809 int32_t DeviceManagerService::ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &result)
1810 {
1811 LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str());
1812 std::string appId = AppManager::GetInstance().GetAppId();
1813 if (appId.empty()) {
1814 LOGD("GetAppId failed");
1815 return ERR_DM_FAILED;
1816 }
1817 DmKVValue kvValue;
1818 int32_t ret = Crypto::ConvertUdidHashToAnoyAndSave(appId, udidHash, kvValue);
1819 if (ret != DM_OK) {
1820 return ERR_DM_FAILED;
1821 }
1822 result = kvValue.anoyDeviceId;
1823 return DM_OK;
1824 }
1825
GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash)1826 int32_t DeviceManagerService::GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash)
1827 {
1828 LOGI("anoyDeviceId %{public}s.", GetAnonyString(anoyDeviceId).c_str());
1829 DmKVValue kvValue;
1830 if (KVAdapterManager::GetInstance().Get(anoyDeviceId, kvValue) != DM_OK) {
1831 LOGD("Get kv value from DB failed");
1832 return ERR_DM_FAILED;
1833 }
1834 udidHash = kvValue.udidHash;
1835 LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str());
1836 return DM_OK;
1837 }
1838
SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId, int32_t bindLevel)1839 void DeviceManagerService::SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
1840 uint64_t tokenId, int32_t bindLevel)
1841 {
1842 LOGI("TokenId %{public}" PRId64", bindLevel %{public}d, userId %{public}d.", tokenId, bindLevel, userId);
1843 if (bindLevel == DEVICE) {
1844 SendDeviceUnBindBroadCast(peerUdids, userId);
1845 return;
1846 }
1847 if (bindLevel == APP) {
1848 SendAppUnBindBroadCast(peerUdids, userId, tokenId);
1849 return;
1850 }
1851 if (bindLevel == SERVICE) {
1852 SendServiceUnBindBroadCast(peerUdids, userId, tokenId);
1853 return;
1854 }
1855 }
1856
SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId)1857 void DeviceManagerService::SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId)
1858 {
1859 RelationShipChangeMsg msg;
1860 msg.type = RelationShipChangeType::DEVICE_UNBIND;
1861 msg.userId = static_cast<uint32_t>(userId);
1862 msg.peerUdids = peerUdids;
1863 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
1864 CHECK_NULL_VOID(softbusListener_);
1865 softbusListener_->SendAclChangedBroadcast(broadCastMsg);
1866 }
1867
SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId)1868 void DeviceManagerService::SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
1869 uint64_t tokenId)
1870 {
1871 RelationShipChangeMsg msg;
1872 msg.type = RelationShipChangeType::APP_UNBIND;
1873 msg.userId = static_cast<uint32_t>(userId);
1874 msg.peerUdids = peerUdids;
1875 msg.tokenId = tokenId;
1876 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
1877 CHECK_NULL_VOID(softbusListener_);
1878 softbusListener_->SendAclChangedBroadcast(broadCastMsg);
1879 }
1880
SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId)1881 void DeviceManagerService::SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
1882 uint64_t tokenId)
1883 {
1884 RelationShipChangeMsg msg;
1885 msg.type = RelationShipChangeType::SERVICE_UNBIND;
1886 msg.userId = static_cast<uint32_t>(userId);
1887 msg.peerUdids = peerUdids;
1888 msg.tokenId = tokenId;
1889 std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
1890 CHECK_NULL_VOID(softbusListener_);
1891 softbusListener_->SendAclChangedBroadcast(broadCastMsg);
1892 }
1893 #endif
1894
HandleDeviceTrustedChange(const std::string &msg)1895 void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg)
1896 {
1897 if (msg.empty()) {
1898 LOGE("Msg is empty.");
1899 return;
1900 }
1901 RelationShipChangeMsg relationShipMsg =
1902 ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
1903 std::string tokenIdStr = std::to_string(relationShipMsg.tokenId);
1904 LOGI("EventType %{public}d, userId %{public}d, accountId %{public}s, tokenId %{public}s,"
1905 "peerUdid %{public}s, accountName %{public}s.", relationShipMsg.type, relationShipMsg.userId,
1906 GetAnonyString(relationShipMsg.accountId).c_str(), GetAnonyString(tokenIdStr).c_str(),
1907 GetAnonyString(relationShipMsg.peerUdid).c_str(), GetAnonyString(relationShipMsg.accountName).c_str());
1908 if (!IsDMServiceImplReady()) {
1909 LOGE("Imp instance not init or init failed.");
1910 return;
1911 }
1912 switch (relationShipMsg.type) {
1913 case RelationShipChangeType::ACCOUNT_LOGOUT:
1914 dmServiceImpl_->HandleAccountLogoutEvent(relationShipMsg.userId, relationShipMsg.accountId,
1915 relationShipMsg.peerUdid);
1916 break;
1917 case RelationShipChangeType::DEVICE_UNBIND:
1918 dmServiceImpl_->HandleDevUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid);
1919 break;
1920 case RelationShipChangeType::APP_UNBIND:
1921 dmServiceImpl_->HandleAppUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid,
1922 static_cast<int32_t>(relationShipMsg.tokenId));
1923 break;
1924 default:
1925 LOGI("Dm have not this event type.");
1926 break;
1927 }
1928 return;
1929 }
1930
ClearDiscoveryCache(const std::string &pkgName)1931 void DeviceManagerService::ClearDiscoveryCache(const std::string &pkgName)
1932 {
1933 LOGI("PkgName %{public}s.", pkgName.c_str());
1934 CHECK_NULL_VOID(discoveryMgr_);
1935 discoveryMgr_->ClearDiscoveryCache(pkgName);
1936 }
1937
HandleDeviceScreenStatusChange(DmDeviceInfo &deviceInfo)1938 void DeviceManagerService::HandleDeviceScreenStatusChange(DmDeviceInfo &deviceInfo)
1939 {
1940 if (IsDMServiceImplReady()) {
1941 dmServiceImpl_->HandleDeviceScreenStatusChange(deviceInfo);
1942 }
1943 }
1944
GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId, int32_t &screenStatus)1945 int32_t DeviceManagerService::GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
1946 int32_t &screenStatus)
1947 {
1948 LOGI("Begin pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(), GetAnonyString(networkId).c_str());
1949 if (!PermissionManager::GetInstance().CheckPermission()) {
1950 LOGE("The caller: %{public}s does not have permission to call GetDeviceScreenStatus.", pkgName.c_str());
1951 return ERR_DM_NO_PERMISSION;
1952 }
1953 if (pkgName.empty() || networkId.empty()) {
1954 LOGE("Invalid parameter, pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
1955 GetAnonyString(networkId).c_str());
1956 return ERR_DM_INPUT_PARA_INVALID;
1957 }
1958 CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
1959 int32_t ret = softbusListener_->GetDeviceScreenStatus(networkId.c_str(), screenStatus);
1960 if (ret != DM_OK) {
1961 LOGE("GetDeviceScreenStatus failed, ret = %{public}d", ret);
1962 return ret;
1963 }
1964 return DM_OK;
1965 }
1966
GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid, std::string &networkId)1967 int32_t DeviceManagerService::GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid,
1968 std::string &networkId)
1969 {
1970 if (!PermissionManager::GetInstance().CheckPermission()) {
1971 LOGE("The caller: %{public}s does not have permission to call GetNetworkIdByUdid.", pkgName.c_str());
1972 return ERR_DM_NO_PERMISSION;
1973 }
1974 if (pkgName.empty() || udid.empty()) {
1975 LOGE("Invalid parameter, pkgName: %{public}s, udid: %{public}s", pkgName.c_str(), GetAnonyString(udid).c_str());
1976 return ERR_DM_INPUT_PARA_INVALID;
1977 }
1978 return SoftbusListener::GetNetworkIdByUdid(udid, networkId);
1979 }
1980
SubscribePackageCommonEvent()1981 void DeviceManagerService::SubscribePackageCommonEvent()
1982 {
1983 LOGI("Start");
1984 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
1985 if (packageCommonEventManager_ == nullptr) {
1986 packageCommonEventManager_ = std::make_shared<DmPackageCommonEventManager>();
1987 }
1988 PackageEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
1989 if (IsDMServiceImplReady()) {
1990 dmServiceImpl_->ProcessAppUnintall(arg1, arg3);
1991 }
1992 KVAdapterManager::GetInstance().AppUnintall(arg1);
1993 };
1994 std::vector<std::string> commonEventVec;
1995 commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
1996 commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
1997 if (packageCommonEventManager_->SubscribePackageCommonEvent(commonEventVec, callback)) {
1998 LOGI("Success");
1999 }
2000 #endif
2001 }
2002
HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode)2003 void DeviceManagerService::HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId,
2004 int32_t errcode)
2005 {
2006 if (IsDMServiceImplReady()) {
2007 dmServiceImpl_->HandleCredentialAuthStatus(deviceList, deviceTypeId, errcode);
2008 }
2009 }
2010 } // namespace DistributedHardware
2011 } // namespace OHOS