179a732c7Sopenharmony_ci/*
279a732c7Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License.
579a732c7Sopenharmony_ci * You may obtain a copy of the License at
679a732c7Sopenharmony_ci *
779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
879a732c7Sopenharmony_ci *
979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and
1379a732c7Sopenharmony_ci * limitations under the License.
1479a732c7Sopenharmony_ci */
1579a732c7Sopenharmony_ci
1679a732c7Sopenharmony_ci#include "device_manager_service.h"
1779a732c7Sopenharmony_ci
1879a732c7Sopenharmony_ci#include <dlfcn.h>
1979a732c7Sopenharmony_ci#include <functional>
2079a732c7Sopenharmony_ci
2179a732c7Sopenharmony_ci#include "app_manager.h"
2279a732c7Sopenharmony_ci#include "dm_anonymous.h"
2379a732c7Sopenharmony_ci#include "dm_constants.h"
2479a732c7Sopenharmony_ci#include "dm_crypto.h"
2579a732c7Sopenharmony_ci#include "dm_hidumper.h"
2679a732c7Sopenharmony_ci#include "dm_log.h"
2779a732c7Sopenharmony_ci#include "dm_softbus_cache.h"
2879a732c7Sopenharmony_ci#include "parameter.h"
2979a732c7Sopenharmony_ci#include "permission_manager.h"
3079a732c7Sopenharmony_ci#include "relationship_sync_mgr.h"
3179a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
3279a732c7Sopenharmony_ci#include "common_event_support.h"
3379a732c7Sopenharmony_ci#include "datetime_ex.h"
3479a732c7Sopenharmony_ci#include "iservice_registry.h"
3579a732c7Sopenharmony_ci#include "kv_adapter_manager.h"
3679a732c7Sopenharmony_ci#include "multiple_user_connector.h"
3779a732c7Sopenharmony_ci#if defined(SUPPORT_POWER_MANAGER)
3879a732c7Sopenharmony_ci#include "power_mgr_client.h"
3979a732c7Sopenharmony_ci#endif // SUPPORT_POWER_MANAGER
4079a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH)
4179a732c7Sopenharmony_ci#include "softbus_publish.h"
4279a732c7Sopenharmony_ci#include "bluetooth_def.h"
4379a732c7Sopenharmony_ci#include "bluetooth_host.h"
4479a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH
4579a732c7Sopenharmony_ci#if defined(SUPPORT_WIFI)
4679a732c7Sopenharmony_ci#include "softbus_publish.h"
4779a732c7Sopenharmony_ci#include "wifi_device.h"
4879a732c7Sopenharmony_ci#include "wifi_msg.h"
4979a732c7Sopenharmony_ci#endif // SUPPORT_WIFI
5079a732c7Sopenharmony_ci#endif
5179a732c7Sopenharmony_ci
5279a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
5379a732c7Sopenharmony_ciconstexpr const char* LIB_IMPL_NAME = "libdevicemanagerserviceimpl.z.so";
5479a732c7Sopenharmony_ciusing namespace OHOS::EventFwk;
5579a732c7Sopenharmony_ci#else
5679a732c7Sopenharmony_ciconstexpr const char* LIB_IMPL_NAME = "libdevicemanagerserviceimpl.so";
5779a732c7Sopenharmony_ci#endif
5879a732c7Sopenharmony_ciconstexpr const char* LIB_DM_ADAPTER_NAME = "libdevicemanageradapter.z.so";
5979a732c7Sopenharmony_ci
6079a732c7Sopenharmony_cinamespace OHOS {
6179a732c7Sopenharmony_cinamespace DistributedHardware {
6279a732c7Sopenharmony_ciDM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerService);
6379a732c7Sopenharmony_ciconst int32_t NORMAL = 0;
6479a732c7Sopenharmony_ciconst int32_t SYSTEM_BASIC = 1;
6579a732c7Sopenharmony_ciconst int32_t SYSTEM_CORE = 2;
6679a732c7Sopenharmony_ciconstexpr const char* ALL_PKGNAME = "";
6779a732c7Sopenharmony_ciconstexpr const char* NETWORKID = "NETWORK_ID";
6879a732c7Sopenharmony_ciconstexpr uint32_t INVALIED_BIND_LEVEL = 0;
6979a732c7Sopenharmony_ciconstexpr uint32_t DM_IDENTICAL_ACCOUNT = 1;
7079a732c7Sopenharmony_ciDeviceManagerService::~DeviceManagerService()
7179a732c7Sopenharmony_ci{
7279a732c7Sopenharmony_ci    LOGI("DeviceManagerService destructor");
7379a732c7Sopenharmony_ci    UnloadDMServiceImplSo();
7479a732c7Sopenharmony_ci    UnloadDMServiceAdapter();
7579a732c7Sopenharmony_ci}
7679a732c7Sopenharmony_ci
7779a732c7Sopenharmony_ciint32_t DeviceManagerService::Init()
7879a732c7Sopenharmony_ci{
7979a732c7Sopenharmony_ci    InitSoftbusListener();
8079a732c7Sopenharmony_ci    InitDMServiceListener();
8179a732c7Sopenharmony_ci    LOGI("Init success, dm service single instance initialized.");
8279a732c7Sopenharmony_ci    return DM_OK;
8379a732c7Sopenharmony_ci}
8479a732c7Sopenharmony_ci
8579a732c7Sopenharmony_ciint32_t DeviceManagerService::InitSoftbusListener()
8679a732c7Sopenharmony_ci{
8779a732c7Sopenharmony_ci    if (softbusListener_ == nullptr) {
8879a732c7Sopenharmony_ci        softbusListener_ = std::make_shared<SoftbusListener>();
8979a732c7Sopenharmony_ci    }
9079a732c7Sopenharmony_ci    SoftbusCache::GetInstance().UpdateDeviceInfoCache();
9179a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
9279a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
9379a732c7Sopenharmony_ci    SubscribePublishCommonEvent();
9479a732c7Sopenharmony_ci    QueryDependsSwitchState();
9579a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
9679a732c7Sopenharmony_ci#endif
9779a732c7Sopenharmony_ci    LOGI("SoftbusListener init success.");
9879a732c7Sopenharmony_ci    return DM_OK;
9979a732c7Sopenharmony_ci}
10079a732c7Sopenharmony_ci
10179a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
10279a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
10379a732c7Sopenharmony_civoid DeviceManagerService::SubscribePublishCommonEvent()
10479a732c7Sopenharmony_ci{
10579a732c7Sopenharmony_ci    LOGI("DeviceManagerServiceImpl::SubscribeCommonEvent");
10679a732c7Sopenharmony_ci    if (publshCommonEventManager_ == nullptr) {
10779a732c7Sopenharmony_ci        publshCommonEventManager_ = std::make_shared<DmPublishCommonEventManager>();
10879a732c7Sopenharmony_ci    }
10979a732c7Sopenharmony_ci    PublishEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
11079a732c7Sopenharmony_ci        OHOS::DistributedHardware::PublishCommonEventCallback(arg1, arg2, arg3);
11179a732c7Sopenharmony_ci    };
11279a732c7Sopenharmony_ci    std::vector<std::string> PublishCommonEventVec;
11379a732c7Sopenharmony_ci#ifdef SUPPORT_BLUETOOTH
11479a732c7Sopenharmony_ci    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE);
11579a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH
11679a732c7Sopenharmony_ci
11779a732c7Sopenharmony_ci#ifdef SUPPORT_WIFI
11879a732c7Sopenharmony_ci    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
11979a732c7Sopenharmony_ci#endif // SUPPORT_WIFI
12079a732c7Sopenharmony_ci    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
12179a732c7Sopenharmony_ci    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
12279a732c7Sopenharmony_ci    if (publshCommonEventManager_->SubscribePublishCommonEvent(PublishCommonEventVec, callback)) {
12379a732c7Sopenharmony_ci        LOGI("subscribe ble and wifi and screen common event success");
12479a732c7Sopenharmony_ci    }
12579a732c7Sopenharmony_ci    return;
12679a732c7Sopenharmony_ci}
12779a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
12879a732c7Sopenharmony_ci#endif
12979a732c7Sopenharmony_ci
13079a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
13179a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
13279a732c7Sopenharmony_civoid DeviceManagerService::QueryDependsSwitchState()
13379a732c7Sopenharmony_ci{
13479a732c7Sopenharmony_ci    LOGI("DeviceManagerService::QueryDependsSwitchState start.");
13579a732c7Sopenharmony_ci    std::shared_ptr<DmPublishEventSubscriber> publishSubScriber = publshCommonEventManager_->GetSubscriber();
13679a732c7Sopenharmony_ci    CHECK_NULL_VOID(publishSubScriber);
13779a732c7Sopenharmony_ci    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
13879a732c7Sopenharmony_ci    CHECK_NULL_VOID(samgr);
13979a732c7Sopenharmony_ci#ifdef SUPPORT_BLUETOOTH
14079a732c7Sopenharmony_ci    if (samgr->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID) == nullptr) {
14179a732c7Sopenharmony_ci        publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
14279a732c7Sopenharmony_ci    } else {
14379a732c7Sopenharmony_ci        if (Bluetooth::BluetoothHost::GetDefaultHost().IsBleEnabled()) {
14479a732c7Sopenharmony_ci            publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON));
14579a732c7Sopenharmony_ci        } else {
14679a732c7Sopenharmony_ci            publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
14779a732c7Sopenharmony_ci        }
14879a732c7Sopenharmony_ci    }
14979a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH
15079a732c7Sopenharmony_ci
15179a732c7Sopenharmony_ci#ifdef SUPPORT_WIFI
15279a732c7Sopenharmony_ci    if (samgr->CheckSystemAbility(WIFI_DEVICE_SYS_ABILITY_ID) == nullptr) {
15379a732c7Sopenharmony_ci        publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
15479a732c7Sopenharmony_ci    } else {
15579a732c7Sopenharmony_ci        bool isWifiActive = false;
15679a732c7Sopenharmony_ci        auto wifiMgr = Wifi::WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
15779a732c7Sopenharmony_ci        CHECK_NULL_VOID(wifiMgr);
15879a732c7Sopenharmony_ci        wifiMgr->IsWifiActive(isWifiActive);
15979a732c7Sopenharmony_ci        if (isWifiActive) {
16079a732c7Sopenharmony_ci            publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED));
16179a732c7Sopenharmony_ci        } else {
16279a732c7Sopenharmony_ci            publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
16379a732c7Sopenharmony_ci        }
16479a732c7Sopenharmony_ci    }
16579a732c7Sopenharmony_ci#endif // SUPPORT_WIFI
16679a732c7Sopenharmony_ci
16779a732c7Sopenharmony_ci#ifdef SUPPORT_POWER_MANAGER
16879a732c7Sopenharmony_ci    if (samgr->CheckSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr) {
16979a732c7Sopenharmony_ci        publishSubScriber->SetScreenState(DM_SCREEN_OFF);
17079a732c7Sopenharmony_ci    } else {
17179a732c7Sopenharmony_ci        if (OHOS::PowerMgr::PowerMgrClient::GetInstance().IsScreenOn()) {
17279a732c7Sopenharmony_ci            publishSubScriber->SetScreenState(DM_SCREEN_ON);
17379a732c7Sopenharmony_ci        } else {
17479a732c7Sopenharmony_ci            publishSubScriber->SetScreenState(DM_SCREEN_OFF);
17579a732c7Sopenharmony_ci        }
17679a732c7Sopenharmony_ci    }
17779a732c7Sopenharmony_ci#else
17879a732c7Sopenharmony_ci    publishSubScriber->SetScreenState(DM_SCREEN_ON);
17979a732c7Sopenharmony_ci#endif // SUPPORT_POWER_MANAGER
18079a732c7Sopenharmony_ci    OHOS::DistributedHardware::PublishCommonEventCallback(publishSubScriber->GetBluetoothState(),
18179a732c7Sopenharmony_ci        publishSubScriber->GetWifiState(), publishSubScriber->GetScreenState());
18279a732c7Sopenharmony_ci}
18379a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
18479a732c7Sopenharmony_ci#endif
18579a732c7Sopenharmony_ci
18679a732c7Sopenharmony_civoid DeviceManagerService::UninitSoftbusListener()
18779a732c7Sopenharmony_ci{
18879a732c7Sopenharmony_ci    softbusListener_ = nullptr;
18979a732c7Sopenharmony_ci    LOGI("SoftbusListener uninit.");
19079a732c7Sopenharmony_ci}
19179a732c7Sopenharmony_ci
19279a732c7Sopenharmony_ciint32_t DeviceManagerService::InitDMServiceListener()
19379a732c7Sopenharmony_ci{
19479a732c7Sopenharmony_ci    if (listener_ == nullptr) {
19579a732c7Sopenharmony_ci        listener_ = std::make_shared<DeviceManagerServiceListener>();
19679a732c7Sopenharmony_ci    }
19779a732c7Sopenharmony_ci    if (advertiseMgr_ == nullptr) {
19879a732c7Sopenharmony_ci        advertiseMgr_ = std::make_shared<AdvertiseManager>(softbusListener_);
19979a732c7Sopenharmony_ci    }
20079a732c7Sopenharmony_ci    if (discoveryMgr_ == nullptr) {
20179a732c7Sopenharmony_ci        discoveryMgr_ = std::make_shared<DiscoveryManager>(softbusListener_, listener_);
20279a732c7Sopenharmony_ci    }
20379a732c7Sopenharmony_ci    if (pinHolder_ == nullptr) {
20479a732c7Sopenharmony_ci        pinHolder_ = std::make_shared<PinHolder>(listener_);
20579a732c7Sopenharmony_ci    }
20679a732c7Sopenharmony_ci    LOGI("Init success.");
20779a732c7Sopenharmony_ci    return DM_OK;
20879a732c7Sopenharmony_ci}
20979a732c7Sopenharmony_ci
21079a732c7Sopenharmony_civoid DeviceManagerService::UninitDMServiceListener()
21179a732c7Sopenharmony_ci{
21279a732c7Sopenharmony_ci    listener_ = nullptr;
21379a732c7Sopenharmony_ci    advertiseMgr_ = nullptr;
21479a732c7Sopenharmony_ci    discoveryMgr_ = nullptr;
21579a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
21679a732c7Sopenharmony_ci    KVAdapterManager::GetInstance().UnInit();
21779a732c7Sopenharmony_ci#endif
21879a732c7Sopenharmony_ci    LOGI("Uninit.");
21979a732c7Sopenharmony_ci}
22079a732c7Sopenharmony_ci
22179a732c7Sopenharmony_civoid DeviceManagerService::RegisterCallerAppId(const std::string &pkgName)
22279a732c7Sopenharmony_ci{
22379a732c7Sopenharmony_ci    AppManager::GetInstance().RegisterCallerAppId(pkgName);
22479a732c7Sopenharmony_ci}
22579a732c7Sopenharmony_ci
22679a732c7Sopenharmony_civoid DeviceManagerService::UnRegisterCallerAppId(const std::string &pkgName)
22779a732c7Sopenharmony_ci{
22879a732c7Sopenharmony_ci    AppManager::GetInstance().UnRegisterCallerAppId(pkgName);
22979a732c7Sopenharmony_ci}
23079a732c7Sopenharmony_ci
23179a732c7Sopenharmony_ciint32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
23279a732c7Sopenharmony_ci                                                   std::vector<DmDeviceInfo> &deviceList)
23379a732c7Sopenharmony_ci{
23479a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s.", pkgName.c_str());
23579a732c7Sopenharmony_ci    if (pkgName.empty()) {
23679a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
23779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
23879a732c7Sopenharmony_ci    }
23979a732c7Sopenharmony_ci    bool isOnlyShowNetworkId = false;
24079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
24179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetTrustedDeviceList.", pkgName.c_str());
24279a732c7Sopenharmony_ci        isOnlyShowNetworkId = true;
24379a732c7Sopenharmony_ci    }
24479a732c7Sopenharmony_ci    std::vector<DmDeviceInfo> onlineDeviceList;
24579a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
24679a732c7Sopenharmony_ci    int32_t ret = softbusListener_->GetTrustedDeviceList(onlineDeviceList);
24779a732c7Sopenharmony_ci    if (ret != DM_OK) {
24879a732c7Sopenharmony_ci        LOGE("GetTrustedDeviceList failed");
24979a732c7Sopenharmony_ci        return ret;
25079a732c7Sopenharmony_ci    }
25179a732c7Sopenharmony_ci    if (isOnlyShowNetworkId && !onlineDeviceList.empty()) {
25279a732c7Sopenharmony_ci        for (auto item : onlineDeviceList) {
25379a732c7Sopenharmony_ci            DmDeviceInfo tempInfo;
25479a732c7Sopenharmony_ci            if (memcpy_s(tempInfo.networkId, DM_MAX_DEVICE_ID_LEN, item.networkId, sizeof(item.networkId)) != 0) {
25579a732c7Sopenharmony_ci                LOGE("get networkId: %{public}s failed", GetAnonyString(item.networkId).c_str());
25679a732c7Sopenharmony_ci            }
25779a732c7Sopenharmony_ci            deviceList.push_back(tempInfo);
25879a732c7Sopenharmony_ci        }
25979a732c7Sopenharmony_ci        return DM_OK;
26079a732c7Sopenharmony_ci    }
26179a732c7Sopenharmony_ci    if (onlineDeviceList.size() > 0 && IsDMServiceImplReady()) {
26279a732c7Sopenharmony_ci        std::unordered_map<std::string, DmAuthForm> udidMap;
26379a732c7Sopenharmony_ci        if (PermissionManager::GetInstance().CheckSystemSA(pkgName)) {
26479a732c7Sopenharmony_ci            udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(std::string(ALL_PKGNAME));
26579a732c7Sopenharmony_ci        } else {
26679a732c7Sopenharmony_ci            udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(pkgName);
26779a732c7Sopenharmony_ci        }
26879a732c7Sopenharmony_ci        for (auto item : onlineDeviceList) {
26979a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
27079a732c7Sopenharmony_ci            ConvertUdidHashToAnoyDeviceId(item);
27179a732c7Sopenharmony_ci#endif
27279a732c7Sopenharmony_ci            std::string udid = "";
27379a732c7Sopenharmony_ci            SoftbusListener::GetUdidByNetworkId(item.networkId, udid);
27479a732c7Sopenharmony_ci            if (udidMap.find(udid) != udidMap.end()) {
27579a732c7Sopenharmony_ci                item.authForm = udidMap[udid];
27679a732c7Sopenharmony_ci                deviceList.push_back(item);
27779a732c7Sopenharmony_ci            }
27879a732c7Sopenharmony_ci        }
27979a732c7Sopenharmony_ci    }
28079a732c7Sopenharmony_ci    return DM_OK;
28179a732c7Sopenharmony_ci}
28279a732c7Sopenharmony_ci
28379a732c7Sopenharmony_ciint32_t DeviceManagerService::ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh,
28479a732c7Sopenharmony_ci                                           bool isWakeUp)
28579a732c7Sopenharmony_ci{
28679a732c7Sopenharmony_ci    LOGD("Begin for pkgName = %{public}s, callerId = %{public}s, isRefresh ="
28779a732c7Sopenharmony_ci        "%{public}d, isWakeUp = %{public}d", pkgName.c_str(), GetAnonyString(callerId).c_str(), isRefresh, isWakeUp);
28879a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
28979a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call ShiftLNNGear, pkgName = %{public}s", pkgName.c_str());
29079a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
29179a732c7Sopenharmony_ci    }
29279a732c7Sopenharmony_ci    if (pkgName.empty() || callerId.empty()) {
29379a732c7Sopenharmony_ci        LOGE("Invalid parameter, parameter is empty.");
29479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
29579a732c7Sopenharmony_ci    }
29679a732c7Sopenharmony_ci    if (isRefresh) {
29779a732c7Sopenharmony_ci        CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
29879a732c7Sopenharmony_ci        int32_t ret = softbusListener_->ShiftLNNGear(isWakeUp, callerId);
29979a732c7Sopenharmony_ci        if (ret != DM_OK) {
30079a732c7Sopenharmony_ci            LOGE("ShiftLNNGear error, failed ret: %{public}d", ret);
30179a732c7Sopenharmony_ci            return ret;
30279a732c7Sopenharmony_ci        }
30379a732c7Sopenharmony_ci    }
30479a732c7Sopenharmony_ci    return DM_OK;
30579a732c7Sopenharmony_ci}
30679a732c7Sopenharmony_ci
30779a732c7Sopenharmony_ciint32_t DeviceManagerService::GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info)
30879a732c7Sopenharmony_ci{
30979a732c7Sopenharmony_ci    LOGI("Begin networkId %{public}s.", GetAnonyString(networkId).c_str());
31079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission() &&
31179a732c7Sopenharmony_ci        !PermissionManager::GetInstance().CheckNewPermission()) {
31279a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call GetDeviceInfo.");
31379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
31479a732c7Sopenharmony_ci    }
31579a732c7Sopenharmony_ci    if (networkId.empty()) {
31679a732c7Sopenharmony_ci        LOGE("Invalid parameter, networkId is empty.");
31779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
31879a732c7Sopenharmony_ci    }
31979a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
32079a732c7Sopenharmony_ci    int32_t ret = softbusListener_->GetDeviceInfo(networkId, info);
32179a732c7Sopenharmony_ci    if (ret != DM_OK) {
32279a732c7Sopenharmony_ci        LOGE("Get DeviceInfo By NetworkId failed, ret : %{public}d", ret);
32379a732c7Sopenharmony_ci    }
32479a732c7Sopenharmony_ci    return ret;
32579a732c7Sopenharmony_ci}
32679a732c7Sopenharmony_ci
32779a732c7Sopenharmony_ciint32_t DeviceManagerService::GetLocalDeviceInfo(DmDeviceInfo &info)
32879a732c7Sopenharmony_ci{
32979a732c7Sopenharmony_ci    LOGI("Begin.");
33079a732c7Sopenharmony_ci    bool isOnlyShowNetworkId = false;
33179a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
33279a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call GetLocalDeviceInfo.");
33379a732c7Sopenharmony_ci        isOnlyShowNetworkId = true;
33479a732c7Sopenharmony_ci    }
33579a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
33679a732c7Sopenharmony_ci    int32_t ret = softbusListener_->GetLocalDeviceInfo(info);
33779a732c7Sopenharmony_ci    if (ret != DM_OK) {
33879a732c7Sopenharmony_ci        LOGE("GetLocalDeviceInfo failed");
33979a732c7Sopenharmony_ci        return ret;
34079a732c7Sopenharmony_ci    }
34179a732c7Sopenharmony_ci    if (isOnlyShowNetworkId) {
34279a732c7Sopenharmony_ci        DmDeviceInfo tempInfo;
34379a732c7Sopenharmony_ci        if (memcpy_s(tempInfo.networkId, DM_MAX_DEVICE_ID_LEN, info.networkId, sizeof(info.networkId)) != 0) {
34479a732c7Sopenharmony_ci            LOGE("get networkId: %{public}s failed", GetAnonyString(info.networkId).c_str());
34579a732c7Sopenharmony_ci        }
34679a732c7Sopenharmony_ci        info = tempInfo;
34779a732c7Sopenharmony_ci        return DM_OK;
34879a732c7Sopenharmony_ci    }
34979a732c7Sopenharmony_ci    if (localDeviceId_.empty()) {
35079a732c7Sopenharmony_ci        char localDeviceId[DEVICE_UUID_LENGTH] = {0};
35179a732c7Sopenharmony_ci        char udidHash[DEVICE_UUID_LENGTH] = {0};
35279a732c7Sopenharmony_ci        GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
35379a732c7Sopenharmony_ci        if (Crypto::GetUdidHash(localDeviceId, reinterpret_cast<uint8_t *>(udidHash)) == DM_OK) {
35479a732c7Sopenharmony_ci            localDeviceId_ = udidHash;
35579a732c7Sopenharmony_ci        }
35679a732c7Sopenharmony_ci    }
35779a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
35879a732c7Sopenharmony_ci    std::string udidHashTemp = "";
35979a732c7Sopenharmony_ci    if (ConvertUdidHashToAnoyDeviceId(localDeviceId_, udidHashTemp) == DM_OK) {
36079a732c7Sopenharmony_ci        if (memset_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
36179a732c7Sopenharmony_ci            LOGE("GetLocalDeviceInfo memset_s failed.");
36279a732c7Sopenharmony_ci            return ERR_DM_FAILED;
36379a732c7Sopenharmony_ci        }
36479a732c7Sopenharmony_ci        if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, udidHashTemp.c_str(), udidHashTemp.length()) != 0) {
36579a732c7Sopenharmony_ci            LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHashTemp).c_str());
36679a732c7Sopenharmony_ci            return ERR_DM_FAILED;
36779a732c7Sopenharmony_ci        }
36879a732c7Sopenharmony_ci        return DM_OK;
36979a732c7Sopenharmony_ci    }
37079a732c7Sopenharmony_ci#endif
37179a732c7Sopenharmony_ci    if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, localDeviceId_.c_str(), localDeviceId_.length()) != 0) {
37279a732c7Sopenharmony_ci        LOGE("get deviceId: %{public}s failed", GetAnonyString(localDeviceId_).c_str());
37379a732c7Sopenharmony_ci    }
37479a732c7Sopenharmony_ci    return DM_OK;
37579a732c7Sopenharmony_ci}
37679a732c7Sopenharmony_ci
37779a732c7Sopenharmony_ciint32_t DeviceManagerService::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
37879a732c7Sopenharmony_ci                                                 std::string &udid)
37979a732c7Sopenharmony_ci{
38079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
38179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetUdidByNetworkId.", pkgName.c_str());
38279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
38379a732c7Sopenharmony_ci    }
38479a732c7Sopenharmony_ci    if (pkgName.empty() || netWorkId.empty()) {
38579a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
38679a732c7Sopenharmony_ci            GetAnonyString(netWorkId).c_str());
38779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
38879a732c7Sopenharmony_ci    }
38979a732c7Sopenharmony_ci    return SoftbusListener::GetUdidByNetworkId(netWorkId.c_str(), udid);
39079a732c7Sopenharmony_ci}
39179a732c7Sopenharmony_ci
39279a732c7Sopenharmony_ciint32_t DeviceManagerService::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
39379a732c7Sopenharmony_ci                                                 std::string &uuid)
39479a732c7Sopenharmony_ci{
39579a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
39679a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetUuidByNetworkId.", pkgName.c_str());
39779a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
39879a732c7Sopenharmony_ci    }
39979a732c7Sopenharmony_ci    if (pkgName.empty() || netWorkId.empty()) {
40079a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
40179a732c7Sopenharmony_ci            GetAnonyString(netWorkId).c_str());
40279a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
40379a732c7Sopenharmony_ci    }
40479a732c7Sopenharmony_ci    return SoftbusListener::GetUuidByNetworkId(netWorkId.c_str(), uuid);
40579a732c7Sopenharmony_ci}
40679a732c7Sopenharmony_ci
40779a732c7Sopenharmony_ciint32_t DeviceManagerService::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
40879a732c7Sopenharmony_ci    const std::string &extra)
40979a732c7Sopenharmony_ci{
41079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
41179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call StartDeviceDiscovery.", pkgName.c_str());
41279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
41379a732c7Sopenharmony_ci    }
41479a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s, extra = %{public}s",
41579a732c7Sopenharmony_ci        pkgName.c_str(), extra.c_str());
41679a732c7Sopenharmony_ci    if (pkgName.empty()) {
41779a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
41879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
41979a732c7Sopenharmony_ci    }
42079a732c7Sopenharmony_ci
42179a732c7Sopenharmony_ci    SoftbusListener::SetHostPkgName(pkgName);
42279a732c7Sopenharmony_ci    std::map<std::string, std::string> discParam;
42379a732c7Sopenharmony_ci    discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID,
42479a732c7Sopenharmony_ci        std::to_string(subscribeInfo.subscribeId)));
42579a732c7Sopenharmony_ci    discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_DISC_MEDIUM, std::to_string(subscribeInfo.medium)));
42679a732c7Sopenharmony_ci
42779a732c7Sopenharmony_ci    std::map<std::string, std::string> filterOps;
42879a732c7Sopenharmony_ci    filterOps.insert(std::pair<std::string, std::string>(PARAM_KEY_FILTER_OPTIONS, extra));
42979a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
43079a732c7Sopenharmony_ci    return discoveryMgr_->StartDiscovering(pkgName, discParam, filterOps);
43179a732c7Sopenharmony_ci}
43279a732c7Sopenharmony_ci
43379a732c7Sopenharmony_ciint32_t DeviceManagerService::StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId,
43479a732c7Sopenharmony_ci                                                   const std::string &filterOptions)
43579a732c7Sopenharmony_ci{
43679a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
43779a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call StartDeviceDiscovery.", pkgName.c_str());
43879a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
43979a732c7Sopenharmony_ci    }
44079a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s, filterOptions = %{public}s, subscribeId = %{public}d",
44179a732c7Sopenharmony_ci        pkgName.c_str(), filterOptions.c_str(), subscribeId);
44279a732c7Sopenharmony_ci    if (pkgName.empty()) {
44379a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
44479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
44579a732c7Sopenharmony_ci    }
44679a732c7Sopenharmony_ci
44779a732c7Sopenharmony_ci    SoftbusListener::SetHostPkgName(pkgName);
44879a732c7Sopenharmony_ci    std::map<std::string, std::string> discParam;
44979a732c7Sopenharmony_ci    discParam.insert(std::pair<std::string, std::string>(PARAM_KEY_SUBSCRIBE_ID, std::to_string(subscribeId)));
45079a732c7Sopenharmony_ci
45179a732c7Sopenharmony_ci    std::map<std::string, std::string> filterOps;
45279a732c7Sopenharmony_ci    filterOps.insert(std::pair<std::string, std::string>(PARAM_KEY_FILTER_OPTIONS, filterOptions));
45379a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
45479a732c7Sopenharmony_ci    return discoveryMgr_->StartDiscovering(pkgName, discParam, filterOps);
45579a732c7Sopenharmony_ci}
45679a732c7Sopenharmony_ci
45779a732c7Sopenharmony_ciint32_t DeviceManagerService::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
45879a732c7Sopenharmony_ci{
45979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission() &&
46079a732c7Sopenharmony_ci        !PermissionManager::GetInstance().CheckNewPermission()) {
46179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call StopDeviceDiscovery.", pkgName.c_str());
46279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
46379a732c7Sopenharmony_ci    }
46479a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
46579a732c7Sopenharmony_ci    if (pkgName.empty()) {
46679a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
46779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
46879a732c7Sopenharmony_ci    }
46979a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
47079a732c7Sopenharmony_ci    return discoveryMgr_->StopDiscovering(pkgName, subscribeId);
47179a732c7Sopenharmony_ci}
47279a732c7Sopenharmony_ci
47379a732c7Sopenharmony_ciint32_t DeviceManagerService::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
47479a732c7Sopenharmony_ci{
47579a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
47679a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call PublishDeviceDiscovery.", pkgName.c_str());
47779a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
47879a732c7Sopenharmony_ci    }
47979a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
48079a732c7Sopenharmony_ci    if (pkgName.empty()) {
48179a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
48279a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
48379a732c7Sopenharmony_ci    }
48479a732c7Sopenharmony_ci
48579a732c7Sopenharmony_ci    std::map<std::string, std::string> advertiseParam;
48679a732c7Sopenharmony_ci    advertiseParam.insert(std::pair<std::string, std::string>(PARAM_KEY_PUBLISH_ID,
48779a732c7Sopenharmony_ci        std::to_string(publishInfo.publishId)));
48879a732c7Sopenharmony_ci    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
48979a732c7Sopenharmony_ci    return advertiseMgr_->StartAdvertising(pkgName, advertiseParam);
49079a732c7Sopenharmony_ci}
49179a732c7Sopenharmony_ci
49279a732c7Sopenharmony_ciint32_t DeviceManagerService::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
49379a732c7Sopenharmony_ci{
49479a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
49579a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call UnPublishDeviceDiscovery.", pkgName.c_str());
49679a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
49779a732c7Sopenharmony_ci    }
49879a732c7Sopenharmony_ci    if (pkgName.empty()) {
49979a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
50079a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
50179a732c7Sopenharmony_ci    }
50279a732c7Sopenharmony_ci    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
50379a732c7Sopenharmony_ci    return advertiseMgr_->StopAdvertising(pkgName, publishId);
50479a732c7Sopenharmony_ci}
50579a732c7Sopenharmony_ci
50679a732c7Sopenharmony_ciint32_t DeviceManagerService::AuthenticateDevice(const std::string &pkgName, int32_t authType,
50779a732c7Sopenharmony_ci                                                 const std::string &deviceId, const std::string &extra)
50879a732c7Sopenharmony_ci{
50979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
51079a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call AuthenticateDevice.", pkgName.c_str());
51179a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
51279a732c7Sopenharmony_ci    }
51379a732c7Sopenharmony_ci    if (pkgName.empty() || deviceId.empty()) {
51479a732c7Sopenharmony_ci        LOGE("DeviceManagerService::AuthenticateDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
51579a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
51679a732c7Sopenharmony_ci    }
51779a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
51879a732c7Sopenharmony_ci        LOGE("AuthenticateDevice failed, instance not init or init failed.");
51979a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
52079a732c7Sopenharmony_ci    }
52179a732c7Sopenharmony_ci    std::string queryDeviceId = deviceId;
52279a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
52379a732c7Sopenharmony_ci    std::string udidHash = "";
52479a732c7Sopenharmony_ci    if (GetUdidHashByAnoyDeviceId(deviceId, udidHash) == DM_OK) {
52579a732c7Sopenharmony_ci        queryDeviceId = udidHash;
52679a732c7Sopenharmony_ci    }
52779a732c7Sopenharmony_ci#endif
52879a732c7Sopenharmony_ci    PeerTargetId targetId;
52979a732c7Sopenharmony_ci    ConnectionAddrType addrType;
53079a732c7Sopenharmony_ci    int32_t ret = SoftbusListener::GetTargetInfoFromCache(queryDeviceId, targetId, addrType);
53179a732c7Sopenharmony_ci    if (ret != DM_OK) {
53279a732c7Sopenharmony_ci        LOGE("AuthenticateDevice failed, cannot get target info from cached discovered device map.");
53379a732c7Sopenharmony_ci        return ERR_DM_BIND_INPUT_PARA_INVALID;
53479a732c7Sopenharmony_ci    }
53579a732c7Sopenharmony_ci    std::map<std::string, std::string> bindParam;
53679a732c7Sopenharmony_ci    bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
53779a732c7Sopenharmony_ci    bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_BIND_EXTRA_DATA, extra));
53879a732c7Sopenharmony_ci    bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE, std::to_string(addrType)));
53979a732c7Sopenharmony_ci    return dmServiceImpl_->BindTarget(pkgName, targetId, bindParam);
54079a732c7Sopenharmony_ci}
54179a732c7Sopenharmony_ci
54279a732c7Sopenharmony_ciint32_t DeviceManagerService::UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId)
54379a732c7Sopenharmony_ci{
54479a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
54579a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call UnAuthenticateDevice.", pkgName.c_str());
54679a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
54779a732c7Sopenharmony_ci    }
54879a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s, networkId = %{public}s",
54979a732c7Sopenharmony_ci        pkgName.c_str(), GetAnonyString(networkId).c_str());
55079a732c7Sopenharmony_ci    if (pkgName.empty() || networkId.empty()) {
55179a732c7Sopenharmony_ci        LOGE("DeviceManagerService::UnAuthenticateDevice error: Invalid parameter, pkgName: %{public}s",
55279a732c7Sopenharmony_ci            pkgName.c_str());
55379a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
55479a732c7Sopenharmony_ci    }
55579a732c7Sopenharmony_ci    std::string udid = "";
55679a732c7Sopenharmony_ci    if (SoftbusListener::GetUdidByNetworkId(networkId.c_str(), udid) != DM_OK) {
55779a732c7Sopenharmony_ci        LOGE("UnAuthenticateDevice GetUdidByNetworkId error: udid: %{public}s", GetAnonyString(udid).c_str());
55879a732c7Sopenharmony_ci        return ERR_DM_FAILED;
55979a732c7Sopenharmony_ci    }
56079a732c7Sopenharmony_ci    char localUdid[DEVICE_UUID_LENGTH] = {0};
56179a732c7Sopenharmony_ci    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
56279a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
56379a732c7Sopenharmony_ci        LOGE("UnAuthenticateDevice failed, instance not init or init failed.");
56479a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
56579a732c7Sopenharmony_ci    }
56679a732c7Sopenharmony_ci    uint64_t tokenId = 0;
56779a732c7Sopenharmony_ci    int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
56879a732c7Sopenharmony_ci    LOGI("UnAuthenticateDevice get bindlevel %{public}d.", bindLevel);
56979a732c7Sopenharmony_ci    if (bindLevel == INVALIED_BIND_LEVEL) {
57079a732c7Sopenharmony_ci        LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel);
57179a732c7Sopenharmony_ci        return ERR_DM_FAILED;
57279a732c7Sopenharmony_ci    }
57379a732c7Sopenharmony_ci    if (dmServiceImpl_->UnAuthenticateDevice(pkgName, udid, bindLevel) != DM_OK) {
57479a732c7Sopenharmony_ci        LOGE("dmServiceImpl_ UnAuthenticateDevice failed.");
57579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
57679a732c7Sopenharmony_ci    }
57779a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
57879a732c7Sopenharmony_ci    std::vector<std::string> peerUdids;
57979a732c7Sopenharmony_ci    peerUdids.emplace_back(udid);
58079a732c7Sopenharmony_ci    SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, bindLevel);
58179a732c7Sopenharmony_ci#endif
58279a732c7Sopenharmony_ci    return DM_OK;
58379a732c7Sopenharmony_ci}
58479a732c7Sopenharmony_ci
58579a732c7Sopenharmony_ciint32_t DeviceManagerService::StopAuthenticateDevice(const std::string &pkgName)
58679a732c7Sopenharmony_ci{
58779a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
58879a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call StopAuthenticateDevice.", pkgName.c_str());
58979a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
59079a732c7Sopenharmony_ci    }
59179a732c7Sopenharmony_ci    if (pkgName.empty()) {
59279a732c7Sopenharmony_ci        LOGE("DeviceManagerService::StopAuthenticateDevice error: Invalid parameter, pkgName: %{public}s",
59379a732c7Sopenharmony_ci            pkgName.c_str());
59479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
59579a732c7Sopenharmony_ci    }
59679a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
59779a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
59879a732c7Sopenharmony_ci        LOGE("StopAuthenticateDevice failed, instance not init or init failed.");
59979a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
60079a732c7Sopenharmony_ci    }
60179a732c7Sopenharmony_ci    if (dmServiceImpl_->StopAuthenticateDevice(pkgName) != DM_OK) {
60279a732c7Sopenharmony_ci        LOGE("dmServiceImpl_ StopAuthenticateDevice failed.");
60379a732c7Sopenharmony_ci        return ERR_DM_FAILED;
60479a732c7Sopenharmony_ci    }
60579a732c7Sopenharmony_ci    return DM_OK;
60679a732c7Sopenharmony_ci}
60779a732c7Sopenharmony_ci
60879a732c7Sopenharmony_ciint32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
60979a732c7Sopenharmony_ci    const std::string &bindParam)
61079a732c7Sopenharmony_ci{
61179a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
61279a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call BindDevice.");
61379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
61479a732c7Sopenharmony_ci    }
61579a732c7Sopenharmony_ci    if (pkgName.empty() || deviceId.empty()) {
61679a732c7Sopenharmony_ci        LOGE("DeviceManagerService::BindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
61779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
61879a732c7Sopenharmony_ci    }
61979a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
62079a732c7Sopenharmony_ci        LOGE("BindDevice failed, instance not init or init failed.");
62179a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
62279a732c7Sopenharmony_ci    }
62379a732c7Sopenharmony_ci    std::string queryDeviceId = deviceId;
62479a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
62579a732c7Sopenharmony_ci    std::string udidHash = "";
62679a732c7Sopenharmony_ci    if (GetUdidHashByAnoyDeviceId(deviceId, udidHash) == DM_OK) {
62779a732c7Sopenharmony_ci        queryDeviceId = udidHash;
62879a732c7Sopenharmony_ci    }
62979a732c7Sopenharmony_ci#endif
63079a732c7Sopenharmony_ci    PeerTargetId targetId;
63179a732c7Sopenharmony_ci    ConnectionAddrType addrType;
63279a732c7Sopenharmony_ci    int32_t ret = SoftbusListener::GetTargetInfoFromCache(queryDeviceId, targetId, addrType);
63379a732c7Sopenharmony_ci    if (ret != DM_OK) {
63479a732c7Sopenharmony_ci        LOGE("BindDevice failed, cannot get target info from cached discovered device map.");
63579a732c7Sopenharmony_ci        return ERR_DM_BIND_INPUT_PARA_INVALID;
63679a732c7Sopenharmony_ci    }
63779a732c7Sopenharmony_ci    std::map<std::string, std::string> bindParamMap;
63879a732c7Sopenharmony_ci    bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
63979a732c7Sopenharmony_ci    bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_BIND_EXTRA_DATA, bindParam));
64079a732c7Sopenharmony_ci    bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE, std::to_string(addrType)));
64179a732c7Sopenharmony_ci    return dmServiceImpl_->BindTarget(pkgName, targetId, bindParamMap);
64279a732c7Sopenharmony_ci}
64379a732c7Sopenharmony_ci
64479a732c7Sopenharmony_ciint32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash)
64579a732c7Sopenharmony_ci{
64679a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
64779a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call UnBindDevice.");
64879a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
64979a732c7Sopenharmony_ci    }
65079a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s, udidHash = %{public}s", pkgName.c_str(), GetAnonyString(udidHash).c_str());
65179a732c7Sopenharmony_ci    if (pkgName.empty() || udidHash.empty()) {
65279a732c7Sopenharmony_ci        LOGE("DeviceManagerService::UnBindDevice error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
65379a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
65479a732c7Sopenharmony_ci    }
65579a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
65679a732c7Sopenharmony_ci        LOGE("UnBindDevice failed, instance not init or init failed.");
65779a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
65879a732c7Sopenharmony_ci    }
65979a732c7Sopenharmony_ci    std::string realDeviceId = udidHash;
66079a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
66179a732c7Sopenharmony_ci    std::string udidHashTemp = "";
66279a732c7Sopenharmony_ci    if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) {
66379a732c7Sopenharmony_ci        realDeviceId = udidHashTemp;
66479a732c7Sopenharmony_ci    }
66579a732c7Sopenharmony_ci#endif
66679a732c7Sopenharmony_ci    std::string udid = "";
66779a732c7Sopenharmony_ci    if (SoftbusCache::GetInstance().GetUdidByUdidHash(realDeviceId, udid) != DM_OK) {
66879a732c7Sopenharmony_ci        LOGE("Get udid by udidhash failed.");
66979a732c7Sopenharmony_ci        return ERR_DM_FAILED;
67079a732c7Sopenharmony_ci    }
67179a732c7Sopenharmony_ci    char localUdid[DEVICE_UUID_LENGTH] = {0};
67279a732c7Sopenharmony_ci    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
67379a732c7Sopenharmony_ci    uint64_t tokenId = 0;
67479a732c7Sopenharmony_ci    int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
67579a732c7Sopenharmony_ci    LOGI("UnAuthenticateDevice get bindlevel %{public}d.", bindLevel);
67679a732c7Sopenharmony_ci    if (bindLevel == INVALIED_BIND_LEVEL) {
67779a732c7Sopenharmony_ci        LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel);
67879a732c7Sopenharmony_ci        return ERR_DM_FAILED;
67979a732c7Sopenharmony_ci    }
68079a732c7Sopenharmony_ci    if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel) != DM_OK) {
68179a732c7Sopenharmony_ci        LOGE("dmServiceImpl_ UnBindDevice failed.");
68279a732c7Sopenharmony_ci        return ERR_DM_FAILED;
68379a732c7Sopenharmony_ci    }
68479a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
68579a732c7Sopenharmony_ci    std::vector<std::string> peerUdids;
68679a732c7Sopenharmony_ci    peerUdids.emplace_back(udid);
68779a732c7Sopenharmony_ci    SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, bindLevel);
68879a732c7Sopenharmony_ci#endif
68979a732c7Sopenharmony_ci    return DM_OK;
69079a732c7Sopenharmony_ci}
69179a732c7Sopenharmony_ci
69279a732c7Sopenharmony_ciint32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t action, const std::string &params)
69379a732c7Sopenharmony_ci{
69479a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
69579a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call SetUserOperation.", pkgName.c_str());
69679a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
69779a732c7Sopenharmony_ci    }
69879a732c7Sopenharmony_ci    if (pkgName.empty() || params.empty()) {
69979a732c7Sopenharmony_ci        LOGE("DeviceManagerService::SetUserOperation error: Invalid parameter, pkgName: %{public}s", pkgName.c_str());
70079a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
70179a732c7Sopenharmony_ci    }
70279a732c7Sopenharmony_ci    if (IsDMServiceAdapterLoad()) {
70379a732c7Sopenharmony_ci        dmServiceImplExt_->ReplyUiAction(pkgName, action, params);
70479a732c7Sopenharmony_ci    }
70579a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
70679a732c7Sopenharmony_ci        LOGE("SetUserOperation failed, instance not init or init failed.");
70779a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
70879a732c7Sopenharmony_ci    }
70979a732c7Sopenharmony_ci    return dmServiceImpl_->SetUserOperation(pkgName, action, params);
71079a732c7Sopenharmony_ci}
71179a732c7Sopenharmony_ci
71279a732c7Sopenharmony_civoid DeviceManagerService::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo)
71379a732c7Sopenharmony_ci{
71479a732c7Sopenharmony_ci    if (IsDMServiceImplReady()) {
71579a732c7Sopenharmony_ci        dmServiceImpl_->HandleDeviceStatusChange(devState, devInfo);
71679a732c7Sopenharmony_ci    }
71779a732c7Sopenharmony_ci    if (IsDMServiceAdapterLoad()) {
71879a732c7Sopenharmony_ci        dmServiceImplExt_->HandleDeviceStatusChange(devState, devInfo);
71979a732c7Sopenharmony_ci    }
72079a732c7Sopenharmony_ci}
72179a732c7Sopenharmony_ci
72279a732c7Sopenharmony_ciint DeviceManagerService::OnSessionOpened(int sessionId, int result)
72379a732c7Sopenharmony_ci{
72479a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
72579a732c7Sopenharmony_ci        LOGE("OnSessionOpened failed, instance not init or init failed.");
72679a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
72779a732c7Sopenharmony_ci    }
72879a732c7Sopenharmony_ci    return dmServiceImpl_->OnSessionOpened(sessionId, result);
72979a732c7Sopenharmony_ci}
73079a732c7Sopenharmony_ci
73179a732c7Sopenharmony_civoid DeviceManagerService::OnSessionClosed(int sessionId)
73279a732c7Sopenharmony_ci{
73379a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
73479a732c7Sopenharmony_ci        LOGE("OnSessionClosed failed, instance not init or init failed.");
73579a732c7Sopenharmony_ci        return;
73679a732c7Sopenharmony_ci    }
73779a732c7Sopenharmony_ci    dmServiceImpl_->OnSessionClosed(sessionId);
73879a732c7Sopenharmony_ci}
73979a732c7Sopenharmony_ci
74079a732c7Sopenharmony_civoid DeviceManagerService::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
74179a732c7Sopenharmony_ci{
74279a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
74379a732c7Sopenharmony_ci        LOGE("OnBytesReceived failed, instance not init or init failed.");
74479a732c7Sopenharmony_ci        return;
74579a732c7Sopenharmony_ci    }
74679a732c7Sopenharmony_ci    dmServiceImpl_->OnBytesReceived(sessionId, data, dataLen);
74779a732c7Sopenharmony_ci}
74879a732c7Sopenharmony_ci
74979a732c7Sopenharmony_ciint DeviceManagerService::OnPinHolderSessionOpened(int sessionId, int result)
75079a732c7Sopenharmony_ci{
75179a732c7Sopenharmony_ci    LOGI("In");
75279a732c7Sopenharmony_ci    return PinHolderSession::OnSessionOpened(sessionId, result);
75379a732c7Sopenharmony_ci}
75479a732c7Sopenharmony_ci
75579a732c7Sopenharmony_civoid DeviceManagerService::OnPinHolderSessionClosed(int sessionId)
75679a732c7Sopenharmony_ci{
75779a732c7Sopenharmony_ci    LOGI("In");
75879a732c7Sopenharmony_ci    PinHolderSession::OnSessionClosed(sessionId);
75979a732c7Sopenharmony_ci}
76079a732c7Sopenharmony_ci
76179a732c7Sopenharmony_civoid DeviceManagerService::OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen)
76279a732c7Sopenharmony_ci{
76379a732c7Sopenharmony_ci    LOGI("In");
76479a732c7Sopenharmony_ci    PinHolderSession::OnBytesReceived(sessionId, data, dataLen);
76579a732c7Sopenharmony_ci}
76679a732c7Sopenharmony_ci
76779a732c7Sopenharmony_ciint32_t DeviceManagerService::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
76879a732c7Sopenharmony_ci{
76979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
77079a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call RequestCredential.");
77179a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
77279a732c7Sopenharmony_ci    }
77379a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
77479a732c7Sopenharmony_ci        LOGE("RequestCredential failed, instance not init or init failed.");
77579a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
77679a732c7Sopenharmony_ci    }
77779a732c7Sopenharmony_ci    return dmServiceImpl_->RequestCredential(reqJsonStr, returnJsonStr);
77879a732c7Sopenharmony_ci}
77979a732c7Sopenharmony_ci
78079a732c7Sopenharmony_ciint32_t DeviceManagerService::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
78179a732c7Sopenharmony_ci{
78279a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
78379a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call ImportCredential.",
78479a732c7Sopenharmony_ci            pkgName.c_str());
78579a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
78679a732c7Sopenharmony_ci    }
78779a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
78879a732c7Sopenharmony_ci        LOGE("ImportCredential failed, instance not init or init failed.");
78979a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
79079a732c7Sopenharmony_ci    }
79179a732c7Sopenharmony_ci    return dmServiceImpl_->ImportCredential(pkgName, credentialInfo);
79279a732c7Sopenharmony_ci}
79379a732c7Sopenharmony_ci
79479a732c7Sopenharmony_ciint32_t DeviceManagerService::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
79579a732c7Sopenharmony_ci{
79679a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
79779a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call DeleteCredential.",
79879a732c7Sopenharmony_ci            pkgName.c_str());
79979a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
80079a732c7Sopenharmony_ci    }
80179a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
80279a732c7Sopenharmony_ci        LOGE("DeleteCredential failed, instance not init or init failed.");
80379a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
80479a732c7Sopenharmony_ci    }
80579a732c7Sopenharmony_ci    return dmServiceImpl_->DeleteCredential(pkgName, deleteInfo);
80679a732c7Sopenharmony_ci}
80779a732c7Sopenharmony_ci
80879a732c7Sopenharmony_ciint32_t DeviceManagerService::MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr)
80979a732c7Sopenharmony_ci{
81079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
81179a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call RequestCredential.");
81279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
81379a732c7Sopenharmony_ci    }
81479a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
81579a732c7Sopenharmony_ci        LOGE("RequestCredential failed, instance not init or init failed.");
81679a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
81779a732c7Sopenharmony_ci    }
81879a732c7Sopenharmony_ci    return dmServiceImpl_->MineRequestCredential(pkgName, returnJsonStr);
81979a732c7Sopenharmony_ci}
82079a732c7Sopenharmony_ci
82179a732c7Sopenharmony_ciint32_t DeviceManagerService::CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
82279a732c7Sopenharmony_ci    std::string &returnJsonStr)
82379a732c7Sopenharmony_ci{
82479a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
82579a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call CheckCredential.",
82679a732c7Sopenharmony_ci            pkgName.c_str());
82779a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
82879a732c7Sopenharmony_ci    }
82979a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
83079a732c7Sopenharmony_ci        LOGE("CheckCredential failed, instance not init or init failed.");
83179a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
83279a732c7Sopenharmony_ci    }
83379a732c7Sopenharmony_ci    return dmServiceImpl_->CheckCredential(pkgName, reqJsonStr, returnJsonStr);
83479a732c7Sopenharmony_ci}
83579a732c7Sopenharmony_ci
83679a732c7Sopenharmony_ciint32_t DeviceManagerService::ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
83779a732c7Sopenharmony_ci    std::string &returnJsonStr)
83879a732c7Sopenharmony_ci{
83979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
84079a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call ImportCredential.",
84179a732c7Sopenharmony_ci            pkgName.c_str());
84279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
84379a732c7Sopenharmony_ci    }
84479a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
84579a732c7Sopenharmony_ci        LOGE("ImportCredential failed, instance not init or init failed.");
84679a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
84779a732c7Sopenharmony_ci    }
84879a732c7Sopenharmony_ci    return dmServiceImpl_->ImportCredential(pkgName, reqJsonStr, returnJsonStr);
84979a732c7Sopenharmony_ci}
85079a732c7Sopenharmony_ci
85179a732c7Sopenharmony_ciint32_t DeviceManagerService::DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
85279a732c7Sopenharmony_ci    std::string &returnJsonStr)
85379a732c7Sopenharmony_ci{
85479a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
85579a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call DeleteCredential.",
85679a732c7Sopenharmony_ci            pkgName.c_str());
85779a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
85879a732c7Sopenharmony_ci    }
85979a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
86079a732c7Sopenharmony_ci        LOGE("DeleteCredential failed, instance not init or init failed.");
86179a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
86279a732c7Sopenharmony_ci    }
86379a732c7Sopenharmony_ci    return dmServiceImpl_->DeleteCredential(pkgName, reqJsonStr, returnJsonStr);
86479a732c7Sopenharmony_ci}
86579a732c7Sopenharmony_ci
86679a732c7Sopenharmony_ciint32_t DeviceManagerService::RegisterCredentialCallback(const std::string &pkgName)
86779a732c7Sopenharmony_ci{
86879a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
86979a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call RegisterCredentialCallback.", pkgName.c_str());
87079a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
87179a732c7Sopenharmony_ci    }
87279a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
87379a732c7Sopenharmony_ci        LOGE("RegisterCredentialCallback failed, instance not init or init failed.");
87479a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
87579a732c7Sopenharmony_ci    }
87679a732c7Sopenharmony_ci    return dmServiceImpl_->RegisterCredentialCallback(pkgName);
87779a732c7Sopenharmony_ci}
87879a732c7Sopenharmony_ci
87979a732c7Sopenharmony_ciint32_t DeviceManagerService::UnRegisterCredentialCallback(const std::string &pkgName)
88079a732c7Sopenharmony_ci{
88179a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
88279a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call UnRegisterCredentialCallback.",
88379a732c7Sopenharmony_ci            pkgName.c_str());
88479a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
88579a732c7Sopenharmony_ci    }
88679a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
88779a732c7Sopenharmony_ci        LOGE("UnRegisterCredentialCallback failed, instance not init or init failed.");
88879a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
88979a732c7Sopenharmony_ci    }
89079a732c7Sopenharmony_ci    return dmServiceImpl_->UnRegisterCredentialCallback(pkgName);
89179a732c7Sopenharmony_ci}
89279a732c7Sopenharmony_ci
89379a732c7Sopenharmony_ciint32_t DeviceManagerService::RegisterUiStateCallback(const std::string &pkgName)
89479a732c7Sopenharmony_ci{
89579a732c7Sopenharmony_ci    if (pkgName.empty()) {
89679a732c7Sopenharmony_ci        LOGE("DeviceManagerService::RegisterUiStateCallback error: Invalid parameter, pkgName: %{public}s",
89779a732c7Sopenharmony_ci            pkgName.c_str());
89879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
89979a732c7Sopenharmony_ci    }
90079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
90179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call RegisterUiStateCallback.",
90279a732c7Sopenharmony_ci            GetAnonyString(pkgName).c_str());
90379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
90479a732c7Sopenharmony_ci    }
90579a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
90679a732c7Sopenharmony_ci        LOGE("RegisterUiStateCallback failed, instance not init or init failed.");
90779a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
90879a732c7Sopenharmony_ci    }
90979a732c7Sopenharmony_ci    return dmServiceImpl_->RegisterUiStateCallback(pkgName);
91079a732c7Sopenharmony_ci}
91179a732c7Sopenharmony_ci
91279a732c7Sopenharmony_ciint32_t DeviceManagerService::UnRegisterUiStateCallback(const std::string &pkgName)
91379a732c7Sopenharmony_ci{
91479a732c7Sopenharmony_ci    if (pkgName.empty()) {
91579a732c7Sopenharmony_ci        LOGE("DeviceManagerService::UnRegisterUiStateCallback error: Invalid parameter, pkgName: %{public}s",
91679a732c7Sopenharmony_ci            pkgName.c_str());
91779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
91879a732c7Sopenharmony_ci    }
91979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
92079a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call UnRegisterUiStateCallback.",
92179a732c7Sopenharmony_ci            GetAnonyString(pkgName).c_str());
92279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
92379a732c7Sopenharmony_ci    }
92479a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
92579a732c7Sopenharmony_ci        LOGE("UnRegisterUiStateCallback failed, instance not init or init failed.");
92679a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
92779a732c7Sopenharmony_ci    }
92879a732c7Sopenharmony_ci    return dmServiceImpl_->UnRegisterUiStateCallback(pkgName);
92979a732c7Sopenharmony_ci}
93079a732c7Sopenharmony_ci
93179a732c7Sopenharmony_cibool DeviceManagerService::IsDMServiceImplReady()
93279a732c7Sopenharmony_ci{
93379a732c7Sopenharmony_ci    std::lock_guard<std::mutex> lock(isImplLoadLock_);
93479a732c7Sopenharmony_ci    if (isImplsoLoaded_ && (dmServiceImpl_ != nullptr)) {
93579a732c7Sopenharmony_ci        return true;
93679a732c7Sopenharmony_ci    }
93779a732c7Sopenharmony_ci    void *so_handle = dlopen(LIB_IMPL_NAME, RTLD_NOW | RTLD_NODELETE);
93879a732c7Sopenharmony_ci    if (so_handle == nullptr) {
93979a732c7Sopenharmony_ci        LOGE("load libdevicemanagerserviceimpl so failed, errMsg: %{public}s.", dlerror());
94079a732c7Sopenharmony_ci        return false;
94179a732c7Sopenharmony_ci    }
94279a732c7Sopenharmony_ci    dlerror();
94379a732c7Sopenharmony_ci    auto func = (CreateDMServiceFuncPtr)dlsym(so_handle, "CreateDMServiceObject");
94479a732c7Sopenharmony_ci    if (dlerror() != nullptr || func == nullptr) {
94579a732c7Sopenharmony_ci        dlclose(so_handle);
94679a732c7Sopenharmony_ci        LOGE("Create object function is not exist.");
94779a732c7Sopenharmony_ci        return false;
94879a732c7Sopenharmony_ci    }
94979a732c7Sopenharmony_ci
95079a732c7Sopenharmony_ci    dmServiceImpl_ = std::shared_ptr<IDeviceManagerServiceImpl>(func());
95179a732c7Sopenharmony_ci    if (listener_ == nullptr) {
95279a732c7Sopenharmony_ci        listener_ = std::make_shared<DeviceManagerServiceListener>();
95379a732c7Sopenharmony_ci    }
95479a732c7Sopenharmony_ci    if (dmServiceImpl_->Initialize(listener_) != DM_OK) {
95579a732c7Sopenharmony_ci        dlclose(so_handle);
95679a732c7Sopenharmony_ci        dmServiceImpl_ = nullptr;
95779a732c7Sopenharmony_ci        isImplsoLoaded_ = false;
95879a732c7Sopenharmony_ci        return false;
95979a732c7Sopenharmony_ci    }
96079a732c7Sopenharmony_ci    isImplsoLoaded_ = true;
96179a732c7Sopenharmony_ci    return true;
96279a732c7Sopenharmony_ci}
96379a732c7Sopenharmony_ci
96479a732c7Sopenharmony_cibool DeviceManagerService::IsDMImplSoLoaded()
96579a732c7Sopenharmony_ci{
96679a732c7Sopenharmony_ci    LOGI("In");
96779a732c7Sopenharmony_ci    std::lock_guard<std::mutex> lock(isImplLoadLock_);
96879a732c7Sopenharmony_ci    return isImplsoLoaded_;
96979a732c7Sopenharmony_ci}
97079a732c7Sopenharmony_ci
97179a732c7Sopenharmony_ciint32_t DeviceManagerService::DmHiDumper(const std::vector<std::string>& args, std::string &result)
97279a732c7Sopenharmony_ci{
97379a732c7Sopenharmony_ci    LOGI("HiDump GetTrustedDeviceList");
97479a732c7Sopenharmony_ci    std::vector<HidumperFlag> dumpflag;
97579a732c7Sopenharmony_ci    HiDumpHelper::GetInstance().GetArgsType(args, dumpflag);
97679a732c7Sopenharmony_ci
97779a732c7Sopenharmony_ci    for (unsigned int i = 0; i < dumpflag.size(); i++) {
97879a732c7Sopenharmony_ci        if (dumpflag[i] == HidumperFlag::HIDUMPER_GET_TRUSTED_LIST) {
97979a732c7Sopenharmony_ci            std::vector<DmDeviceInfo> deviceList;
98079a732c7Sopenharmony_ci            CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
98179a732c7Sopenharmony_ci            int32_t ret = softbusListener_->GetTrustedDeviceList(deviceList);
98279a732c7Sopenharmony_ci            if (ret != DM_OK) {
98379a732c7Sopenharmony_ci                result.append("HiDumpHelper GetTrustedDeviceList failed");
98479a732c7Sopenharmony_ci                LOGE("HiDumpHelper GetTrustedDeviceList failed");
98579a732c7Sopenharmony_ci                return ret;
98679a732c7Sopenharmony_ci            }
98779a732c7Sopenharmony_ci
98879a732c7Sopenharmony_ci            for (unsigned int j = 0; j < deviceList.size(); j++) {
98979a732c7Sopenharmony_ci                HiDumpHelper::GetInstance().SetNodeInfo(deviceList[j]);
99079a732c7Sopenharmony_ci                LOGI("DeviceManagerService::DmHiDumper SetNodeInfo.");
99179a732c7Sopenharmony_ci            }
99279a732c7Sopenharmony_ci        }
99379a732c7Sopenharmony_ci    }
99479a732c7Sopenharmony_ci    HiDumpHelper::GetInstance().HiDump(args, result);
99579a732c7Sopenharmony_ci    return DM_OK;
99679a732c7Sopenharmony_ci}
99779a732c7Sopenharmony_ci
99879a732c7Sopenharmony_ciint32_t DeviceManagerService::NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event)
99979a732c7Sopenharmony_ci{
100079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
100179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call NotifyEvent.", pkgName.c_str());
100279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
100379a732c7Sopenharmony_ci    }
100479a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
100579a732c7Sopenharmony_ci        LOGE("NotifyEvent failed, instance not init or init failed.");
100679a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
100779a732c7Sopenharmony_ci    }
100879a732c7Sopenharmony_ci    if (eventId == DM_NOTIFY_EVENT_ON_PINHOLDER_EVENT) {
100979a732c7Sopenharmony_ci        LOGI("NotifyEvent on pin holder event start.");
101079a732c7Sopenharmony_ci        CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
101179a732c7Sopenharmony_ci        return pinHolder_->NotifyPinHolderEvent(pkgName, event);
101279a732c7Sopenharmony_ci    }
101379a732c7Sopenharmony_ci    return dmServiceImpl_->NotifyEvent(pkgName, eventId, event);
101479a732c7Sopenharmony_ci}
101579a732c7Sopenharmony_ci
101679a732c7Sopenharmony_civoid DeviceManagerService::LoadHardwareFwkService()
101779a732c7Sopenharmony_ci{
101879a732c7Sopenharmony_ci    std::string extra;
101979a732c7Sopenharmony_ci    std::vector<DmDeviceInfo> deviceList;
102079a732c7Sopenharmony_ci    int32_t ret = GetTrustedDeviceList(DM_PKG_NAME, extra, deviceList);
102179a732c7Sopenharmony_ci    if (ret != DM_OK) {
102279a732c7Sopenharmony_ci        LOGE("LoadHardwareFwkService failed, get trusted devicelist failed.");
102379a732c7Sopenharmony_ci        return;
102479a732c7Sopenharmony_ci    }
102579a732c7Sopenharmony_ci    if (deviceList.size() > 0) {
102679a732c7Sopenharmony_ci        dmServiceImpl_->LoadHardwareFwkService();
102779a732c7Sopenharmony_ci    }
102879a732c7Sopenharmony_ci}
102979a732c7Sopenharmony_ci
103079a732c7Sopenharmony_ciint32_t DeviceManagerService::GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId,
103179a732c7Sopenharmony_ci    std::string &uuid)
103279a732c7Sopenharmony_ci{
103379a732c7Sopenharmony_ci    if (pkgName.empty()) {
103479a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
103579a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
103679a732c7Sopenharmony_ci    }
103779a732c7Sopenharmony_ci    LOGI("PkgName = %{public}s", pkgName.c_str());
103879a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
103979a732c7Sopenharmony_ci    int32_t ret = softbusListener_->GetUuidByNetworkId(networkId.c_str(), uuid);
104079a732c7Sopenharmony_ci    if (ret != DM_OK) {
104179a732c7Sopenharmony_ci        LOGE("GetUuidByNetworkId failed, ret : %{public}d", ret);
104279a732c7Sopenharmony_ci        return ret;
104379a732c7Sopenharmony_ci    }
104479a732c7Sopenharmony_ci
104579a732c7Sopenharmony_ci    std::string appId = Crypto::Sha256(AppManager::GetInstance().GetAppId());
104679a732c7Sopenharmony_ci    LOGI("appId = %{public}s, uuid = %{public}s.", GetAnonyString(appId).c_str(), GetAnonyString(uuid).c_str());
104779a732c7Sopenharmony_ci    uuid = Crypto::Sha256(appId + "_" + uuid);
104879a732c7Sopenharmony_ci    LOGI("encryptedUuid = %{public}s.", GetAnonyString(uuid).c_str());
104979a732c7Sopenharmony_ci    return DM_OK;
105079a732c7Sopenharmony_ci}
105179a732c7Sopenharmony_ci
105279a732c7Sopenharmony_ciint32_t DeviceManagerService::GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid,
105379a732c7Sopenharmony_ci    const std::string &appId, std::string &encryptedUuid)
105479a732c7Sopenharmony_ci{
105579a732c7Sopenharmony_ci    if (pkgName.empty()) {
105679a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
105779a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
105879a732c7Sopenharmony_ci    }
105979a732c7Sopenharmony_ci    encryptedUuid = Crypto::Sha256(appId + "_" + uuid);
106079a732c7Sopenharmony_ci    LOGI("encryptedUuid = %{public}s.", GetAnonyString(encryptedUuid).c_str());
106179a732c7Sopenharmony_ci    return DM_OK;
106279a732c7Sopenharmony_ci}
106379a732c7Sopenharmony_ci
106479a732c7Sopenharmony_ciint32_t DeviceManagerService::CheckApiPermission(int32_t permissionLevel)
106579a732c7Sopenharmony_ci{
106679a732c7Sopenharmony_ci    LOGI("PermissionLevel: %{public}d", permissionLevel);
106779a732c7Sopenharmony_ci    int32_t ret = ERR_DM_NO_PERMISSION;
106879a732c7Sopenharmony_ci    switch (permissionLevel) {
106979a732c7Sopenharmony_ci        case NORMAL:
107079a732c7Sopenharmony_ci            if (PermissionManager::GetInstance().CheckNewPermission()) {
107179a732c7Sopenharmony_ci                LOGI("The caller have permission to call");
107279a732c7Sopenharmony_ci                ret = DM_OK;
107379a732c7Sopenharmony_ci            }
107479a732c7Sopenharmony_ci            break;
107579a732c7Sopenharmony_ci        case SYSTEM_BASIC:
107679a732c7Sopenharmony_ci            if (PermissionManager::GetInstance().CheckPermission()) {
107779a732c7Sopenharmony_ci                LOGI("The caller have permission to call");
107879a732c7Sopenharmony_ci                ret = DM_OK;
107979a732c7Sopenharmony_ci            }
108079a732c7Sopenharmony_ci            break;
108179a732c7Sopenharmony_ci        case SYSTEM_CORE:
108279a732c7Sopenharmony_ci            if (PermissionManager::GetInstance().CheckMonitorPermission()) {
108379a732c7Sopenharmony_ci                LOGI("The caller have permission to call");
108479a732c7Sopenharmony_ci                ret = DM_OK;
108579a732c7Sopenharmony_ci            }
108679a732c7Sopenharmony_ci            break;
108779a732c7Sopenharmony_ci        default:
108879a732c7Sopenharmony_ci            LOGE("DM have not this permissionLevel.");
108979a732c7Sopenharmony_ci            break;
109079a732c7Sopenharmony_ci    }
109179a732c7Sopenharmony_ci    return ret;
109279a732c7Sopenharmony_ci}
109379a732c7Sopenharmony_ci
109479a732c7Sopenharmony_ciint32_t DeviceManagerService::GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId,
109579a732c7Sopenharmony_ci                                                        int32_t &networkType)
109679a732c7Sopenharmony_ci{
109779a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
109879a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetNetworkTypeByNetworkId.", pkgName.c_str());
109979a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
110079a732c7Sopenharmony_ci    }
110179a732c7Sopenharmony_ci    LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
110279a732c7Sopenharmony_ci    if (pkgName.empty() || netWorkId.empty()) {
110379a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s, netWorkId: %{public}s", pkgName.c_str(),
110479a732c7Sopenharmony_ci            GetAnonyString(netWorkId).c_str());
110579a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
110679a732c7Sopenharmony_ci    }
110779a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
110879a732c7Sopenharmony_ci    return softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
110979a732c7Sopenharmony_ci}
111079a732c7Sopenharmony_ci
111179a732c7Sopenharmony_ciint32_t DeviceManagerService::ImportAuthCode(const std::string &pkgName, const std::string &authCode)
111279a732c7Sopenharmony_ci{
111379a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
111479a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.", pkgName.c_str());
111579a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
111679a732c7Sopenharmony_ci    }
111779a732c7Sopenharmony_ci    std::string processName = "";
111879a732c7Sopenharmony_ci    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
111979a732c7Sopenharmony_ci        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
112079a732c7Sopenharmony_ci        return ERR_DM_FAILED;
112179a732c7Sopenharmony_ci    }
112279a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
112379a732c7Sopenharmony_ci        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
112479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
112579a732c7Sopenharmony_ci    }
112679a732c7Sopenharmony_ci    LOGI("DeviceManagerService::ImportAuthCode begin.");
112779a732c7Sopenharmony_ci    if (authCode.empty() || pkgName.empty()) {
112879a732c7Sopenharmony_ci        LOGE("Invalid parameter, authCode: %{public}s.", GetAnonyString(authCode).c_str());
112979a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
113079a732c7Sopenharmony_ci    }
113179a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
113279a732c7Sopenharmony_ci        LOGE("ImportAuthCode failed, instance not init or init failed.");
113379a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
113479a732c7Sopenharmony_ci    }
113579a732c7Sopenharmony_ci    return dmServiceImpl_->ImportAuthCode(pkgName, authCode);
113679a732c7Sopenharmony_ci}
113779a732c7Sopenharmony_ci
113879a732c7Sopenharmony_ciint32_t DeviceManagerService::ExportAuthCode(std::string &authCode)
113979a732c7Sopenharmony_ci{
114079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
114179a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call ExportAuthCode.");
114279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
114379a732c7Sopenharmony_ci    }
114479a732c7Sopenharmony_ci    std::string processName = "";
114579a732c7Sopenharmony_ci    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
114679a732c7Sopenharmony_ci        LOGE("Get caller process name failed, processName: %{public}s.", processName.c_str());
114779a732c7Sopenharmony_ci        return ERR_DM_FAILED;
114879a732c7Sopenharmony_ci    }
114979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
115079a732c7Sopenharmony_ci        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
115179a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
115279a732c7Sopenharmony_ci    }
115379a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
115479a732c7Sopenharmony_ci        LOGE("ExportAuthCode failed, instance not init or init failed.");
115579a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
115679a732c7Sopenharmony_ci    }
115779a732c7Sopenharmony_ci    LOGI("DeviceManagerService::ExportAuthCode begin.");
115879a732c7Sopenharmony_ci    return dmServiceImpl_->ExportAuthCode(authCode);
115979a732c7Sopenharmony_ci}
116079a732c7Sopenharmony_ci
116179a732c7Sopenharmony_civoid DeviceManagerService::UnloadDMServiceImplSo()
116279a732c7Sopenharmony_ci{
116379a732c7Sopenharmony_ci    LOGI("Start.");
116479a732c7Sopenharmony_ci    std::lock_guard<std::mutex> lock(isImplLoadLock_);
116579a732c7Sopenharmony_ci    if (dmServiceImpl_ != nullptr) {
116679a732c7Sopenharmony_ci        dmServiceImpl_->Release();
116779a732c7Sopenharmony_ci    }
116879a732c7Sopenharmony_ci    void *so_handle = dlopen(LIB_IMPL_NAME, RTLD_NOW | RTLD_NOLOAD);
116979a732c7Sopenharmony_ci    if (so_handle != nullptr) {
117079a732c7Sopenharmony_ci        LOGI("DeviceManagerService so_handle is not nullptr.");
117179a732c7Sopenharmony_ci        dlclose(so_handle);
117279a732c7Sopenharmony_ci    }
117379a732c7Sopenharmony_ci}
117479a732c7Sopenharmony_ci
117579a732c7Sopenharmony_cibool DeviceManagerService::IsDMServiceAdapterLoad()
117679a732c7Sopenharmony_ci{
117779a732c7Sopenharmony_ci    LOGI("Start.");
117879a732c7Sopenharmony_ci    if (listener_ == nullptr) {
117979a732c7Sopenharmony_ci        LOGE("Dm service is not init.");
118079a732c7Sopenharmony_ci        return false;
118179a732c7Sopenharmony_ci    }
118279a732c7Sopenharmony_ci    std::lock_guard<std::mutex> lock(isAdapterLoadLock_);
118379a732c7Sopenharmony_ci    if (isAdapterSoLoaded_ && (dmServiceImplExt_ != nullptr)) {
118479a732c7Sopenharmony_ci        return true;
118579a732c7Sopenharmony_ci    }
118679a732c7Sopenharmony_ci
118779a732c7Sopenharmony_ci    void *so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NODELETE);
118879a732c7Sopenharmony_ci    if (so_handle == nullptr) {
118979a732c7Sopenharmony_ci        LOGE("load dm service adapter so failed.");
119079a732c7Sopenharmony_ci        return false;
119179a732c7Sopenharmony_ci    }
119279a732c7Sopenharmony_ci    dlerror();
119379a732c7Sopenharmony_ci    auto func = (CreateDMServiceImplExtFuncPtr)dlsym(so_handle, "CreateDMServiceImplExtObject");
119479a732c7Sopenharmony_ci    if (dlerror() != nullptr || func == nullptr) {
119579a732c7Sopenharmony_ci        dlclose(so_handle);
119679a732c7Sopenharmony_ci        LOGE("Create object function is not exist.");
119779a732c7Sopenharmony_ci        return false;
119879a732c7Sopenharmony_ci    }
119979a732c7Sopenharmony_ci
120079a732c7Sopenharmony_ci    dmServiceImplExt_ = std::shared_ptr<IDMServiceImplExt>(func());
120179a732c7Sopenharmony_ci    if (dmServiceImplExt_->Initialize(listener_) != DM_OK) {
120279a732c7Sopenharmony_ci        dlclose(so_handle);
120379a732c7Sopenharmony_ci        dmServiceImplExt_ = nullptr;
120479a732c7Sopenharmony_ci        isAdapterSoLoaded_ = false;
120579a732c7Sopenharmony_ci        LOGE("dm service adapter impl ext init failed.");
120679a732c7Sopenharmony_ci        return false;
120779a732c7Sopenharmony_ci    }
120879a732c7Sopenharmony_ci    isAdapterSoLoaded_ = true;
120979a732c7Sopenharmony_ci    LOGI("Success.");
121079a732c7Sopenharmony_ci    return true;
121179a732c7Sopenharmony_ci}
121279a732c7Sopenharmony_ci
121379a732c7Sopenharmony_civoid DeviceManagerService::UnloadDMServiceAdapter()
121479a732c7Sopenharmony_ci{
121579a732c7Sopenharmony_ci    LOGI("Start.");
121679a732c7Sopenharmony_ci    std::lock_guard<std::mutex> lock(isAdapterLoadLock_);
121779a732c7Sopenharmony_ci    if (dmServiceImplExt_ != nullptr) {
121879a732c7Sopenharmony_ci        dmServiceImplExt_->Release();
121979a732c7Sopenharmony_ci    }
122079a732c7Sopenharmony_ci    dmServiceImplExt_ = nullptr;
122179a732c7Sopenharmony_ci
122279a732c7Sopenharmony_ci    void *so_handle = dlopen(LIB_DM_ADAPTER_NAME, RTLD_NOW | RTLD_NOLOAD);
122379a732c7Sopenharmony_ci    if (so_handle != nullptr) {
122479a732c7Sopenharmony_ci        LOGI("dm service adapter so_handle is not nullptr.");
122579a732c7Sopenharmony_ci        dlclose(so_handle);
122679a732c7Sopenharmony_ci    }
122779a732c7Sopenharmony_ci}
122879a732c7Sopenharmony_ci
122979a732c7Sopenharmony_ciint32_t DeviceManagerService::StartDiscovering(const std::string &pkgName,
123079a732c7Sopenharmony_ci    const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)
123179a732c7Sopenharmony_ci{
123279a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
123379a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
123479a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
123579a732c7Sopenharmony_ci    }
123679a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
123779a732c7Sopenharmony_ci    if (pkgName.empty()) {
123879a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
123979a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
124079a732c7Sopenharmony_ci    }
124179a732c7Sopenharmony_ci    if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) {
124279a732c7Sopenharmony_ci        LOGI("StartDiscovering input MetaType = %{public}s", (discoverParam.find(PARAM_KEY_META_TYPE)->second).c_str());
124379a732c7Sopenharmony_ci    }
124479a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
124579a732c7Sopenharmony_ci    return discoveryMgr_->StartDiscovering(pkgName, discoverParam, filterOptions);
124679a732c7Sopenharmony_ci}
124779a732c7Sopenharmony_ci
124879a732c7Sopenharmony_ciint32_t DeviceManagerService::StopDiscovering(const std::string &pkgName,
124979a732c7Sopenharmony_ci    const std::map<std::string, std::string> &discoverParam)
125079a732c7Sopenharmony_ci{
125179a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
125279a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
125379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
125479a732c7Sopenharmony_ci    }
125579a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
125679a732c7Sopenharmony_ci    if (pkgName.empty()) {
125779a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
125879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
125979a732c7Sopenharmony_ci    }
126079a732c7Sopenharmony_ci    uint16_t subscribeId = -1;
126179a732c7Sopenharmony_ci    if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) {
126279a732c7Sopenharmony_ci        subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str());
126379a732c7Sopenharmony_ci    }
126479a732c7Sopenharmony_ci    if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) {
126579a732c7Sopenharmony_ci        LOGI("StopDiscovering input MetaType = %{public}s", (discoverParam.find(PARAM_KEY_META_TYPE)->second).c_str());
126679a732c7Sopenharmony_ci    }
126779a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
126879a732c7Sopenharmony_ci    return discoveryMgr_->StopDiscovering(pkgName, subscribeId);
126979a732c7Sopenharmony_ci}
127079a732c7Sopenharmony_ci
127179a732c7Sopenharmony_ciint32_t DeviceManagerService::EnableDiscoveryListener(const std::string &pkgName,
127279a732c7Sopenharmony_ci    const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)
127379a732c7Sopenharmony_ci{
127479a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
127579a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
127679a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
127779a732c7Sopenharmony_ci    }
127879a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
127979a732c7Sopenharmony_ci    if (pkgName.empty()) {
128079a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
128179a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
128279a732c7Sopenharmony_ci    }
128379a732c7Sopenharmony_ci    SoftbusListener::SetHostPkgName(pkgName);
128479a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
128579a732c7Sopenharmony_ci    return discoveryMgr_->EnableDiscoveryListener(pkgName, discoverParam, filterOptions);
128679a732c7Sopenharmony_ci}
128779a732c7Sopenharmony_ci
128879a732c7Sopenharmony_ciint32_t DeviceManagerService::DisableDiscoveryListener(const std::string &pkgName,
128979a732c7Sopenharmony_ci    const std::map<std::string, std::string> &extraParam)
129079a732c7Sopenharmony_ci{
129179a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
129279a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
129379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
129479a732c7Sopenharmony_ci    }
129579a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
129679a732c7Sopenharmony_ci    if (pkgName.empty()) {
129779a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
129879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
129979a732c7Sopenharmony_ci    }
130079a732c7Sopenharmony_ci    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
130179a732c7Sopenharmony_ci    return discoveryMgr_->DisableDiscoveryListener(pkgName, extraParam);
130279a732c7Sopenharmony_ci}
130379a732c7Sopenharmony_ci
130479a732c7Sopenharmony_ciint32_t DeviceManagerService::StartAdvertising(const std::string &pkgName,
130579a732c7Sopenharmony_ci    const std::map<std::string, std::string> &advertiseParam)
130679a732c7Sopenharmony_ci{
130779a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
130879a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
130979a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
131079a732c7Sopenharmony_ci    }
131179a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
131279a732c7Sopenharmony_ci    if (pkgName.empty()) {
131379a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
131479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
131579a732c7Sopenharmony_ci    }
131679a732c7Sopenharmony_ci    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
131779a732c7Sopenharmony_ci    return advertiseMgr_->StartAdvertising(pkgName, advertiseParam);
131879a732c7Sopenharmony_ci}
131979a732c7Sopenharmony_ci
132079a732c7Sopenharmony_ciint32_t DeviceManagerService::StopAdvertising(const std::string &pkgName,
132179a732c7Sopenharmony_ci    const std::map<std::string, std::string> &advertiseParam)
132279a732c7Sopenharmony_ci{
132379a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
132479a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
132579a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
132679a732c7Sopenharmony_ci    }
132779a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
132879a732c7Sopenharmony_ci    if (pkgName.empty()) {
132979a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
133079a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
133179a732c7Sopenharmony_ci    }
133279a732c7Sopenharmony_ci    if (advertiseParam.find(PARAM_KEY_META_TYPE) != advertiseParam.end()) {
133379a732c7Sopenharmony_ci        LOGI("StopAdvertising input MetaType=%{public}s", (advertiseParam.find(PARAM_KEY_META_TYPE)->second).c_str());
133479a732c7Sopenharmony_ci    }
133579a732c7Sopenharmony_ci    int32_t publishId = -1;
133679a732c7Sopenharmony_ci    if (advertiseParam.find(PARAM_KEY_PUBLISH_ID) != advertiseParam.end()) {
133779a732c7Sopenharmony_ci        publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str());
133879a732c7Sopenharmony_ci    }
133979a732c7Sopenharmony_ci    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
134079a732c7Sopenharmony_ci    return advertiseMgr_->StopAdvertising(pkgName, publishId);
134179a732c7Sopenharmony_ci}
134279a732c7Sopenharmony_ci
134379a732c7Sopenharmony_ciint32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
134479a732c7Sopenharmony_ci    const std::map<std::string, std::string> &bindParam)
134579a732c7Sopenharmony_ci{
134679a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
134779a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
134879a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
134979a732c7Sopenharmony_ci    }
135079a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
135179a732c7Sopenharmony_ci    if (pkgName.empty()) {
135279a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
135379a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
135479a732c7Sopenharmony_ci    }
135579a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
135679a732c7Sopenharmony_ci        LOGE("BindTarget failed, DMServiceImpl instance not init or init failed.");
135779a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
135879a732c7Sopenharmony_ci    }
135979a732c7Sopenharmony_ci    if (bindParam.find(PARAM_KEY_META_TYPE) == bindParam.end()) {
136079a732c7Sopenharmony_ci        LOGI("BindTarget stardard begin.");
136179a732c7Sopenharmony_ci        if (targetId.wifiIp.empty() || targetId.wifiIp.length() > IP_STR_MAX_LEN) {
136279a732c7Sopenharmony_ci            return dmServiceImpl_->BindTarget(pkgName, targetId, bindParam);
136379a732c7Sopenharmony_ci        }
136479a732c7Sopenharmony_ci        ConnectionAddrType ipAddrType;
136579a732c7Sopenharmony_ci        std::map<std::string, std::string> &noConstBindParam =
136679a732c7Sopenharmony_ci            const_cast<std::map<std::string, std::string> &>(bindParam);
136779a732c7Sopenharmony_ci        if (SoftbusListener::GetIPAddrTypeFromCache(targetId.deviceId, targetId.wifiIp, ipAddrType) == DM_OK) {
136879a732c7Sopenharmony_ci            noConstBindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE,
136979a732c7Sopenharmony_ci                std::to_string(ipAddrType)));
137079a732c7Sopenharmony_ci        }
137179a732c7Sopenharmony_ci        const std::map<std::string, std::string> &constBindParam =
137279a732c7Sopenharmony_ci            const_cast<const std::map<std::string, std::string> &>(noConstBindParam);
137379a732c7Sopenharmony_ci        return dmServiceImpl_->BindTarget(pkgName, targetId, constBindParam);
137479a732c7Sopenharmony_ci    }
137579a732c7Sopenharmony_ci    if (!IsDMServiceAdapterLoad()) {
137679a732c7Sopenharmony_ci        LOGE("BindTarget failed, adapter instance not init or init failed.");
137779a732c7Sopenharmony_ci        return ERR_DM_UNSUPPORTED_METHOD;
137879a732c7Sopenharmony_ci    }
137979a732c7Sopenharmony_ci    LOGI("BindTarget unstardard begin.");
138079a732c7Sopenharmony_ci    return dmServiceImplExt_->BindTargetExt(pkgName, targetId, bindParam);
138179a732c7Sopenharmony_ci}
138279a732c7Sopenharmony_ci
138379a732c7Sopenharmony_ciint32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
138479a732c7Sopenharmony_ci    const std::map<std::string, std::string> &unbindParam)
138579a732c7Sopenharmony_ci{
138679a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
138779a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
138879a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
138979a732c7Sopenharmony_ci    }
139079a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
139179a732c7Sopenharmony_ci    if (pkgName.empty()) {
139279a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
139379a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
139479a732c7Sopenharmony_ci    }
139579a732c7Sopenharmony_ci    if (!IsDMServiceAdapterLoad()) {
139679a732c7Sopenharmony_ci        LOGE("UnbindTarget failed, instance not init or init failed.");
139779a732c7Sopenharmony_ci        return ERR_DM_UNSUPPORTED_METHOD;
139879a732c7Sopenharmony_ci    }
139979a732c7Sopenharmony_ci    if (unbindParam.find(PARAM_KEY_META_TYPE) == unbindParam.end()) {
140079a732c7Sopenharmony_ci        LOGE("input unbind parameter not contains META_TYPE, dm service adapter not supported.");
140179a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
140279a732c7Sopenharmony_ci    }
140379a732c7Sopenharmony_ci    return dmServiceImplExt_->UnbindTargetExt(pkgName, targetId, unbindParam);
140479a732c7Sopenharmony_ci}
140579a732c7Sopenharmony_ci
140679a732c7Sopenharmony_ciint32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName)
140779a732c7Sopenharmony_ci{
140879a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
140979a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.", pkgName.c_str());
141079a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
141179a732c7Sopenharmony_ci    }
141279a732c7Sopenharmony_ci    std::string processName = "";
141379a732c7Sopenharmony_ci    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
141479a732c7Sopenharmony_ci        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
141579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
141679a732c7Sopenharmony_ci    }
141779a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
141879a732c7Sopenharmony_ci        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
141979a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
142079a732c7Sopenharmony_ci    }
142179a732c7Sopenharmony_ci    LOGI("DeviceManagerService::RegisterPinHolderCallback begin.");
142279a732c7Sopenharmony_ci    if (pkgName.empty()) {
142379a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
142479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
142579a732c7Sopenharmony_ci    }
142679a732c7Sopenharmony_ci    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
142779a732c7Sopenharmony_ci    return pinHolder_->RegisterPinHolderCallback(pkgName);
142879a732c7Sopenharmony_ci}
142979a732c7Sopenharmony_ci
143079a732c7Sopenharmony_ciint32_t DeviceManagerService::CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
143179a732c7Sopenharmony_ci    DmPinType pinType, const std::string &payload)
143279a732c7Sopenharmony_ci{
143379a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
143479a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call CreatePinHolder.", pkgName.c_str());
143579a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
143679a732c7Sopenharmony_ci    }
143779a732c7Sopenharmony_ci    std::string processName = "";
143879a732c7Sopenharmony_ci    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
143979a732c7Sopenharmony_ci        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
144079a732c7Sopenharmony_ci        return ERR_DM_FAILED;
144179a732c7Sopenharmony_ci    }
144279a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
144379a732c7Sopenharmony_ci        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
144479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
144579a732c7Sopenharmony_ci    }
144679a732c7Sopenharmony_ci    LOGI("DeviceManagerService::CreatePinHolder begin.");
144779a732c7Sopenharmony_ci    if (pkgName.empty()) {
144879a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
144979a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
145079a732c7Sopenharmony_ci    }
145179a732c7Sopenharmony_ci    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
145279a732c7Sopenharmony_ci    return pinHolder_->CreatePinHolder(pkgName, targetId, pinType, payload);
145379a732c7Sopenharmony_ci}
145479a732c7Sopenharmony_ci
145579a732c7Sopenharmony_ciint32_t DeviceManagerService::DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
145679a732c7Sopenharmony_ci    DmPinType pinType, const std::string &payload)
145779a732c7Sopenharmony_ci{
145879a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
145979a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call DestroyPinHolder.", pkgName.c_str());
146079a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
146179a732c7Sopenharmony_ci    }
146279a732c7Sopenharmony_ci    std::string processName = "";
146379a732c7Sopenharmony_ci    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
146479a732c7Sopenharmony_ci        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
146579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
146679a732c7Sopenharmony_ci    }
146779a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
146879a732c7Sopenharmony_ci        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
146979a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
147079a732c7Sopenharmony_ci    }
147179a732c7Sopenharmony_ci    LOGI("Begin.");
147279a732c7Sopenharmony_ci    if (pkgName.empty()) {
147379a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
147479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
147579a732c7Sopenharmony_ci    }
147679a732c7Sopenharmony_ci    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
147779a732c7Sopenharmony_ci    return pinHolder_->DestroyPinHolder(pkgName, targetId, pinType, payload);
147879a732c7Sopenharmony_ci}
147979a732c7Sopenharmony_ci
148079a732c7Sopenharmony_ciint32_t DeviceManagerService::DpAclAdd(const std::string &udid)
148179a732c7Sopenharmony_ci{
148279a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
148379a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call DpAclAdd.");
148479a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
148579a732c7Sopenharmony_ci    }
148679a732c7Sopenharmony_ci    LOGI("Start.");
148779a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
148879a732c7Sopenharmony_ci        LOGE("DpAclAdd failed, instance not init or init failed.");
148979a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
149079a732c7Sopenharmony_ci    }
149179a732c7Sopenharmony_ci    dmServiceImpl_->DpAclAdd(udid);
149279a732c7Sopenharmony_ci    LOGI("DeviceManagerService::DpAclAdd completed");
149379a732c7Sopenharmony_ci    return DM_OK;
149479a732c7Sopenharmony_ci}
149579a732c7Sopenharmony_ci
149679a732c7Sopenharmony_ciint32_t DeviceManagerService::GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId,
149779a732c7Sopenharmony_ci                                                     int32_t &securityLevel)
149879a732c7Sopenharmony_ci{
149979a732c7Sopenharmony_ci    LOGI("Begin pkgName: %{public}s, networkId: %{public}s",
150079a732c7Sopenharmony_ci        pkgName.c_str(), GetAnonyString(networkId).c_str());
150179a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
150279a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetDeviceSecurityLevel.", pkgName.c_str());
150379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
150479a732c7Sopenharmony_ci    }
150579a732c7Sopenharmony_ci    if (pkgName.empty() || networkId.empty()) {
150679a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
150779a732c7Sopenharmony_ci            GetAnonyString(networkId).c_str());
150879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
150979a732c7Sopenharmony_ci    }
151079a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
151179a732c7Sopenharmony_ci    int32_t ret = softbusListener_->GetDeviceSecurityLevel(networkId.c_str(), securityLevel);
151279a732c7Sopenharmony_ci    if (ret != DM_OK) {
151379a732c7Sopenharmony_ci        LOGE("GetDeviceSecurityLevel failed, ret = %{public}d", ret);
151479a732c7Sopenharmony_ci        return ret;
151579a732c7Sopenharmony_ci    }
151679a732c7Sopenharmony_ci    return DM_OK;
151779a732c7Sopenharmony_ci}
151879a732c7Sopenharmony_ci
151979a732c7Sopenharmony_ciint32_t DeviceManagerService::IsSameAccount(const std::string &networkId)
152079a732c7Sopenharmony_ci{
152179a732c7Sopenharmony_ci    LOGI("NetworkId %{public}s.", GetAnonyString(networkId).c_str());
152279a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
152379a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
152479a732c7Sopenharmony_ci    }
152579a732c7Sopenharmony_ci    std::string udid = "";
152679a732c7Sopenharmony_ci    if (SoftbusListener::GetUdidByNetworkId(networkId.c_str(), udid) != DM_OK) {
152779a732c7Sopenharmony_ci        LOGE("DeviceManagerService::IsSameAccount error: udid: %{public}s", GetAnonyString(udid).c_str());
152879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
152979a732c7Sopenharmony_ci    }
153079a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
153179a732c7Sopenharmony_ci        LOGE("IsSameAccount failed, instance not init or init failed.");
153279a732c7Sopenharmony_ci        return ERR_DM_NOT_INIT;
153379a732c7Sopenharmony_ci    }
153479a732c7Sopenharmony_ci    return dmServiceImpl_->IsSameAccount(udid);
153579a732c7Sopenharmony_ci}
153679a732c7Sopenharmony_ci
153779a732c7Sopenharmony_cibool DeviceManagerService::CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
153879a732c7Sopenharmony_ci{
153979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
154079a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call CheckAccessControl.", caller.pkgName.c_str());
154179a732c7Sopenharmony_ci        return false;
154279a732c7Sopenharmony_ci    }
154379a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
154479a732c7Sopenharmony_ci        LOGE("CheckAccessControl failed, instance not init or init failed.");
154579a732c7Sopenharmony_ci        return false;
154679a732c7Sopenharmony_ci    }
154779a732c7Sopenharmony_ci    std::string srcUdid = "";
154879a732c7Sopenharmony_ci    SoftbusListener::GetUdidByNetworkId(caller.networkId.c_str(), srcUdid);
154979a732c7Sopenharmony_ci    std::string sinkUdid = "";
155079a732c7Sopenharmony_ci    SoftbusListener::GetUdidByNetworkId(callee.networkId.c_str(), sinkUdid);
155179a732c7Sopenharmony_ci    return dmServiceImpl_->CheckAccessControl(caller, srcUdid, callee, sinkUdid);
155279a732c7Sopenharmony_ci}
155379a732c7Sopenharmony_ci
155479a732c7Sopenharmony_cibool DeviceManagerService::CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
155579a732c7Sopenharmony_ci{
155679a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
155779a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call CheckIsSameAccount.", caller.pkgName.c_str());
155879a732c7Sopenharmony_ci        return false;
155979a732c7Sopenharmony_ci    }
156079a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
156179a732c7Sopenharmony_ci        LOGE("CheckIsSameAccount failed, instance not init or init failed.");
156279a732c7Sopenharmony_ci        return false;
156379a732c7Sopenharmony_ci    }
156479a732c7Sopenharmony_ci    std::string srcUdid = "";
156579a732c7Sopenharmony_ci    SoftbusListener::GetUdidByNetworkId(caller.networkId.c_str(), srcUdid);
156679a732c7Sopenharmony_ci    std::string sinkUdid = "";
156779a732c7Sopenharmony_ci    SoftbusListener::GetUdidByNetworkId(callee.networkId.c_str(), sinkUdid);
156879a732c7Sopenharmony_ci    return dmServiceImpl_->CheckIsSameAccount(caller, srcUdid, callee, sinkUdid);
156979a732c7Sopenharmony_ci}
157079a732c7Sopenharmony_ci
157179a732c7Sopenharmony_ciint32_t DeviceManagerService::InitAccountInfo()
157279a732c7Sopenharmony_ci{
157379a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
157479a732c7Sopenharmony_ci    SubscribeAccountCommonEvent();
157579a732c7Sopenharmony_ci    LOGI("Success.");
157679a732c7Sopenharmony_ci#endif
157779a732c7Sopenharmony_ci    return DM_OK;
157879a732c7Sopenharmony_ci}
157979a732c7Sopenharmony_ci
158079a732c7Sopenharmony_ciint32_t DeviceManagerService::InitScreenLockEvent()
158179a732c7Sopenharmony_ci{
158279a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
158379a732c7Sopenharmony_ci    SubscribeScreenLockEvent();
158479a732c7Sopenharmony_ci    LOGI("Success.");
158579a732c7Sopenharmony_ci#endif
158679a732c7Sopenharmony_ci    return DM_OK;
158779a732c7Sopenharmony_ci}
158879a732c7Sopenharmony_ci
158979a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
159079a732c7Sopenharmony_civoid DeviceManagerService::SubscribeAccountCommonEvent()
159179a732c7Sopenharmony_ci{
159279a732c7Sopenharmony_ci    LOGI("Start");
159379a732c7Sopenharmony_ci    if (accountCommonEventManager_ == nullptr) {
159479a732c7Sopenharmony_ci        accountCommonEventManager_ = std::make_shared<DmAccountCommonEventManager>();
159579a732c7Sopenharmony_ci    }
159679a732c7Sopenharmony_ci    AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
159779a732c7Sopenharmony_ci        this->AccountCommonEventCallback(arg1, arg2);
159879a732c7Sopenharmony_ci    };
159979a732c7Sopenharmony_ci    std::vector<std::string> AccountCommonEventVec;
160079a732c7Sopenharmony_ci    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
160179a732c7Sopenharmony_ci    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
160279a732c7Sopenharmony_ci    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
160379a732c7Sopenharmony_ci    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
160479a732c7Sopenharmony_ci    if (accountCommonEventManager_->SubscribeAccountCommonEvent(AccountCommonEventVec, callback)) {
160579a732c7Sopenharmony_ci        LOGI("Success");
160679a732c7Sopenharmony_ci    }
160779a732c7Sopenharmony_ci    return;
160879a732c7Sopenharmony_ci}
160979a732c7Sopenharmony_ci
161079a732c7Sopenharmony_civoid DeviceManagerService::SubscribeScreenLockEvent()
161179a732c7Sopenharmony_ci{
161279a732c7Sopenharmony_ci    LOGI("Start");
161379a732c7Sopenharmony_ci    if (screenCommonEventManager_ == nullptr) {
161479a732c7Sopenharmony_ci        screenCommonEventManager_ = std::make_shared<DmScreenCommonEventManager>();
161579a732c7Sopenharmony_ci    }
161679a732c7Sopenharmony_ci    ScreenEventCallback callback = [=](const auto &arg1) { this->ScreenCommonEventCallback(arg1); };
161779a732c7Sopenharmony_ci    std::vector<std::string> screenEventVec;
161879a732c7Sopenharmony_ci    screenEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
161979a732c7Sopenharmony_ci    if (screenCommonEventManager_->SubscribeScreenCommonEvent(screenEventVec, callback)) {
162079a732c7Sopenharmony_ci        LOGI("Success");
162179a732c7Sopenharmony_ci    }
162279a732c7Sopenharmony_ci    return;
162379a732c7Sopenharmony_ci}
162479a732c7Sopenharmony_ci
162579a732c7Sopenharmony_civoid DeviceManagerService::AccountCommonEventCallback(int32_t userId, const std::string commonEventType)
162679a732c7Sopenharmony_ci{
162779a732c7Sopenharmony_ci    LOGI("CommonEventType: %{public}s, userId: %{public}d", commonEventType.c_str(), userId);
162879a732c7Sopenharmony_ci    if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
162979a732c7Sopenharmony_ci        if (MultipleUserConnector::GetSwitchOldUserId() > 0 &&
163079a732c7Sopenharmony_ci            (userId != MultipleUserConnector::GetSwitchOldUserId())) {
163179a732c7Sopenharmony_ci            HandleUserSwitched(MultipleUserConnector::GetSwitchOldUserId());
163279a732c7Sopenharmony_ci        }
163379a732c7Sopenharmony_ci        MultipleUserConnector::SetSwitchOldUserId(userId);
163479a732c7Sopenharmony_ci        MultipleUserConnector::SetSwitchOldAccountId(MultipleUserConnector::GetOhosAccountId());
163579a732c7Sopenharmony_ci        MultipleUserConnector::SetSwitchOldAccountName(MultipleUserConnector::GetOhosAccountName());
163679a732c7Sopenharmony_ci        if (IsDMServiceAdapterLoad()) {
163779a732c7Sopenharmony_ci            dmServiceImplExt_->AccountUserSwitched(userId, MultipleUserConnector::GetOhosAccountId());
163879a732c7Sopenharmony_ci        }
163979a732c7Sopenharmony_ci    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGIN) {
164079a732c7Sopenharmony_ci        MultipleUserConnector::SetSwitchOldAccountId(MultipleUserConnector::GetOhosAccountId());
164179a732c7Sopenharmony_ci        MultipleUserConnector::SetSwitchOldAccountName(MultipleUserConnector::GetOhosAccountName());
164279a732c7Sopenharmony_ci    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_HWID_LOGOUT) {
164379a732c7Sopenharmony_ci        HandleAccountLogout(MultipleUserConnector::GetCurrentAccountUserID(),
164479a732c7Sopenharmony_ci            MultipleUserConnector::GetSwitchOldAccountId());
164579a732c7Sopenharmony_ci    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
164679a732c7Sopenharmony_ci        HandleUserRemoved(userId);
164779a732c7Sopenharmony_ci    } else {
164879a732c7Sopenharmony_ci        LOGE("Invalied account common event.");
164979a732c7Sopenharmony_ci    }
165079a732c7Sopenharmony_ci    return;
165179a732c7Sopenharmony_ci}
165279a732c7Sopenharmony_ci
165379a732c7Sopenharmony_civoid DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string &accountId)
165479a732c7Sopenharmony_ci{
165579a732c7Sopenharmony_ci    LOGI("UserId %{public}d, accountId %{public}s.", userId, GetAnonyString(accountId).c_str());
165679a732c7Sopenharmony_ci    if (IsDMServiceAdapterLoad()) {
165779a732c7Sopenharmony_ci        dmServiceImplExt_->AccountIdLogout(userId, accountId);
165879a732c7Sopenharmony_ci    }
165979a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
166079a732c7Sopenharmony_ci        LOGE("Init impl failed.");
166179a732c7Sopenharmony_ci        return;
166279a732c7Sopenharmony_ci    }
166379a732c7Sopenharmony_ci    std::map<std::string, int32_t> deviceMap;
166479a732c7Sopenharmony_ci    std::vector<std::string> peerUdids;
166579a732c7Sopenharmony_ci    deviceMap = dmServiceImpl_->GetDeviceIdAndBindType(userId, accountId);
166679a732c7Sopenharmony_ci    for (const auto &item : deviceMap) {
166779a732c7Sopenharmony_ci        if (item.second == DM_IDENTICAL_ACCOUNT) {
166879a732c7Sopenharmony_ci            dmServiceImpl_->HandleIdentAccountLogout(item.first, userId, accountId);
166979a732c7Sopenharmony_ci            peerUdids.emplace_back(item.first);
167079a732c7Sopenharmony_ci        }
167179a732c7Sopenharmony_ci    }
167279a732c7Sopenharmony_ci    if (!peerUdids.empty()) {
167379a732c7Sopenharmony_ci        char accountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
167479a732c7Sopenharmony_ci        if (Crypto::GetAccountIdHash(accountId, reinterpret_cast<uint8_t *>(accountIdHash)) != DM_OK) {
167579a732c7Sopenharmony_ci            LOGE("GetAccountHash failed.");
167679a732c7Sopenharmony_ci            return;
167779a732c7Sopenharmony_ci        }
167879a732c7Sopenharmony_ci        std::string accountName = MultipleUserConnector::GetSwitchOldAccountName();
167979a732c7Sopenharmony_ci        SendAccountLogoutBroadCast(peerUdids, std::string(accountIdHash), accountName, userId);
168079a732c7Sopenharmony_ci    }
168179a732c7Sopenharmony_ci}
168279a732c7Sopenharmony_ci
168379a732c7Sopenharmony_civoid DeviceManagerService::HandleUserSwitched(int32_t switchUserId)
168479a732c7Sopenharmony_ci{
168579a732c7Sopenharmony_ci    LOGI("switchUserId: %{public}d.", switchUserId);
168679a732c7Sopenharmony_ci    if (IsDMServiceImplReady()) {
168779a732c7Sopenharmony_ci        dmServiceImpl_->HandleUserSwitched(switchUserId);
168879a732c7Sopenharmony_ci    }
168979a732c7Sopenharmony_ci}
169079a732c7Sopenharmony_ci
169179a732c7Sopenharmony_civoid DeviceManagerService::HandleUserRemoved(int32_t preUserId)
169279a732c7Sopenharmony_ci{
169379a732c7Sopenharmony_ci    LOGI("PreUserId %{public}d.", preUserId);
169479a732c7Sopenharmony_ci    if (IsDMServiceImplReady()) {
169579a732c7Sopenharmony_ci        dmServiceImpl_->HandleUserRemoved(preUserId);
169679a732c7Sopenharmony_ci    }
169779a732c7Sopenharmony_ci}
169879a732c7Sopenharmony_ci
169979a732c7Sopenharmony_civoid DeviceManagerService::SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids,
170079a732c7Sopenharmony_ci    const std::string &accountId, const std::string &accountName, int32_t userId)
170179a732c7Sopenharmony_ci{
170279a732c7Sopenharmony_ci    LOGI("accountId %{public}s, accountName %{public}s, userId %{public}d.", GetAnonyString(accountId).c_str(),
170379a732c7Sopenharmony_ci        GetAnonyString(accountName).c_str(), userId);
170479a732c7Sopenharmony_ci    RelationShipChangeMsg msg;
170579a732c7Sopenharmony_ci    msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
170679a732c7Sopenharmony_ci    msg.userId = static_cast<uint32_t>(userId);
170779a732c7Sopenharmony_ci    msg.peerUdids = peerUdids;
170879a732c7Sopenharmony_ci    msg.accountId = accountId;
170979a732c7Sopenharmony_ci    msg.accountName = accountName;
171079a732c7Sopenharmony_ci    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
171179a732c7Sopenharmony_ci    CHECK_NULL_VOID(softbusListener_);
171279a732c7Sopenharmony_ci    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
171379a732c7Sopenharmony_ci}
171479a732c7Sopenharmony_ci
171579a732c7Sopenharmony_civoid DeviceManagerService::ScreenCommonEventCallback(std::string commonEventType)
171679a732c7Sopenharmony_ci{
171779a732c7Sopenharmony_ci    if (!IsDMImplSoLoaded()) {
171879a732c7Sopenharmony_ci        LOGE("ScreenCommonEventCallback failed, instance not init or init failed.");
171979a732c7Sopenharmony_ci        return;
172079a732c7Sopenharmony_ci    }
172179a732c7Sopenharmony_ci    dmServiceImpl_->ScreenCommonEventCallback(commonEventType);
172279a732c7Sopenharmony_ci}
172379a732c7Sopenharmony_ci#endif
172479a732c7Sopenharmony_ci
172579a732c7Sopenharmony_civoid DeviceManagerService::HandleDeviceNotTrust(const std::string &msg)
172679a732c7Sopenharmony_ci{
172779a732c7Sopenharmony_ci    LOGI("Start.");
172879a732c7Sopenharmony_ci    if (msg.empty()) {
172979a732c7Sopenharmony_ci        LOGE("DeviceManagerService::HandleDeviceNotTrust msg is empty.");
173079a732c7Sopenharmony_ci        return;
173179a732c7Sopenharmony_ci    }
173279a732c7Sopenharmony_ci    nlohmann::json msgJsonObj = nlohmann::json::parse(msg, nullptr, false);
173379a732c7Sopenharmony_ci    if (msgJsonObj.is_discarded()) {
173479a732c7Sopenharmony_ci        LOGE("HandleDeviceNotTrust msg prase error.");
173579a732c7Sopenharmony_ci        return;
173679a732c7Sopenharmony_ci    }
173779a732c7Sopenharmony_ci    if (!IsString(msgJsonObj, NETWORKID)) {
173879a732c7Sopenharmony_ci        LOGE("HandleDeviceNotTrust msg not contain networkId.");
173979a732c7Sopenharmony_ci        return;
174079a732c7Sopenharmony_ci    }
174179a732c7Sopenharmony_ci    std::string networkId = msgJsonObj[NETWORKID].get<std::string>();
174279a732c7Sopenharmony_ci    std::string udid = "";
174379a732c7Sopenharmony_ci    SoftbusCache::GetInstance().GetUdidFromCache(networkId.c_str(), udid);
174479a732c7Sopenharmony_ci    LOGI("NetworkId: %{public}s, udid: %{public}s.",
174579a732c7Sopenharmony_ci        GetAnonyString(networkId).c_str(), GetAnonyString(udid).c_str());
174679a732c7Sopenharmony_ci    if (IsDMServiceImplReady()) {
174779a732c7Sopenharmony_ci        dmServiceImpl_->HandleDeviceNotTrust(udid);
174879a732c7Sopenharmony_ci    }
174979a732c7Sopenharmony_ci    if (IsDMServiceAdapterLoad()) {
175079a732c7Sopenharmony_ci        dmServiceImplExt_->HandleDeviceNotTrust(udid);
175179a732c7Sopenharmony_ci    }
175279a732c7Sopenharmony_ci    return;
175379a732c7Sopenharmony_ci}
175479a732c7Sopenharmony_ci
175579a732c7Sopenharmony_ciint32_t DeviceManagerService::SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy)
175679a732c7Sopenharmony_ci{
175779a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckNewPermission()) {
175879a732c7Sopenharmony_ci        LOGE("The caller does not have permission to call");
175979a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
176079a732c7Sopenharmony_ci    }
176179a732c7Sopenharmony_ci    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
176279a732c7Sopenharmony_ci    if (pkgName.empty()) {
176379a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName is empty.");
176479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
176579a732c7Sopenharmony_ci    }
176679a732c7Sopenharmony_ci    auto policyStrategyIter = policy.find(PARAM_KEY_POLICY_STRATEGY_FOR_BLE);
176779a732c7Sopenharmony_ci    if (policyStrategyIter == policy.end()) {
176879a732c7Sopenharmony_ci        LOGE("Invalid parameter, DM_POLICY_STRATEGY_FOR_BLE is empty.");
176979a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
177079a732c7Sopenharmony_ci    }
177179a732c7Sopenharmony_ci    auto timeOutIter = policy.find(PARAM_KEY_POLICY_TIME_OUT);
177279a732c7Sopenharmony_ci    if (timeOutIter == policy.end()) {
177379a732c7Sopenharmony_ci        LOGE("Invalid parameter, DM_POLICY_TIMEOUT is empty.");
177479a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
177579a732c7Sopenharmony_ci    }
177679a732c7Sopenharmony_ci    if (!IsNumberString(policyStrategyIter->second)) {
177779a732c7Sopenharmony_ci        LOGE("Invalid parameter, DM_POLICY_STRATEGY_FOR_BLE is not number.");
177879a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
177979a732c7Sopenharmony_ci    }
178079a732c7Sopenharmony_ci    if (!IsNumberString(timeOutIter->second)) {
178179a732c7Sopenharmony_ci        LOGE("Invalid parameter, DM_POLICY_TIMEOUT is not number.");
178279a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
178379a732c7Sopenharmony_ci    }
178479a732c7Sopenharmony_ci    int32_t policyStrategy = std::atoi(policyStrategyIter->second.c_str());
178579a732c7Sopenharmony_ci    int32_t timeOut = std::atoi(timeOutIter->second.c_str());
178679a732c7Sopenharmony_ci    LOGD("strategy: %{public}d, timeOut: %{public}d", policyStrategy, timeOut);
178779a732c7Sopenharmony_ci    if (!IsDMServiceAdapterLoad()) {
178879a732c7Sopenharmony_ci        LOGE("SetDnPolicy failed, instance not init or init failed.");
178979a732c7Sopenharmony_ci        return ERR_DM_UNSUPPORTED_METHOD;
179079a732c7Sopenharmony_ci    }
179179a732c7Sopenharmony_ci    return dmServiceImplExt_->SetDnPolicy(policyStrategy, timeOut);
179279a732c7Sopenharmony_ci}
179379a732c7Sopenharmony_ci
179479a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
179579a732c7Sopenharmony_civoid DeviceManagerService::ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo)
179679a732c7Sopenharmony_ci{
179779a732c7Sopenharmony_ci    std::string udidHashTemp = "";
179879a732c7Sopenharmony_ci    if (ConvertUdidHashToAnoyDeviceId(deviceInfo.deviceId, udidHashTemp) == DM_OK) {
179979a732c7Sopenharmony_ci        if (memset_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
180079a732c7Sopenharmony_ci            LOGE("ConvertUdidHashToAnoyDeviceId memset_s failed.");
180179a732c7Sopenharmony_ci            return;
180279a732c7Sopenharmony_ci        }
180379a732c7Sopenharmony_ci        if (memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udidHashTemp.c_str(), udidHashTemp.length()) != 0) {
180479a732c7Sopenharmony_ci            LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHashTemp).c_str());
180579a732c7Sopenharmony_ci        }
180679a732c7Sopenharmony_ci    }
180779a732c7Sopenharmony_ci}
180879a732c7Sopenharmony_ci
180979a732c7Sopenharmony_ciint32_t DeviceManagerService::ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &result)
181079a732c7Sopenharmony_ci{
181179a732c7Sopenharmony_ci    LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str());
181279a732c7Sopenharmony_ci    std::string appId = AppManager::GetInstance().GetAppId();
181379a732c7Sopenharmony_ci    if (appId.empty()) {
181479a732c7Sopenharmony_ci        LOGD("GetAppId failed");
181579a732c7Sopenharmony_ci        return ERR_DM_FAILED;
181679a732c7Sopenharmony_ci    }
181779a732c7Sopenharmony_ci    DmKVValue kvValue;
181879a732c7Sopenharmony_ci    int32_t ret = Crypto::ConvertUdidHashToAnoyAndSave(appId, udidHash, kvValue);
181979a732c7Sopenharmony_ci    if (ret != DM_OK) {
182079a732c7Sopenharmony_ci        return ERR_DM_FAILED;
182179a732c7Sopenharmony_ci    }
182279a732c7Sopenharmony_ci    result = kvValue.anoyDeviceId;
182379a732c7Sopenharmony_ci    return DM_OK;
182479a732c7Sopenharmony_ci}
182579a732c7Sopenharmony_ci
182679a732c7Sopenharmony_ciint32_t DeviceManagerService::GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash)
182779a732c7Sopenharmony_ci{
182879a732c7Sopenharmony_ci    LOGI("anoyDeviceId %{public}s.", GetAnonyString(anoyDeviceId).c_str());
182979a732c7Sopenharmony_ci    DmKVValue kvValue;
183079a732c7Sopenharmony_ci    if (KVAdapterManager::GetInstance().Get(anoyDeviceId, kvValue) != DM_OK) {
183179a732c7Sopenharmony_ci        LOGD("Get kv value from DB failed");
183279a732c7Sopenharmony_ci        return ERR_DM_FAILED;
183379a732c7Sopenharmony_ci    }
183479a732c7Sopenharmony_ci    udidHash = kvValue.udidHash;
183579a732c7Sopenharmony_ci    LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str());
183679a732c7Sopenharmony_ci    return DM_OK;
183779a732c7Sopenharmony_ci}
183879a732c7Sopenharmony_ci
183979a732c7Sopenharmony_civoid DeviceManagerService::SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
184079a732c7Sopenharmony_ci    uint64_t tokenId, int32_t bindLevel)
184179a732c7Sopenharmony_ci{
184279a732c7Sopenharmony_ci    LOGI("TokenId %{public}" PRId64", bindLevel %{public}d, userId %{public}d.", tokenId, bindLevel, userId);
184379a732c7Sopenharmony_ci    if (bindLevel == DEVICE) {
184479a732c7Sopenharmony_ci        SendDeviceUnBindBroadCast(peerUdids, userId);
184579a732c7Sopenharmony_ci        return;
184679a732c7Sopenharmony_ci    }
184779a732c7Sopenharmony_ci    if (bindLevel == APP) {
184879a732c7Sopenharmony_ci        SendAppUnBindBroadCast(peerUdids, userId, tokenId);
184979a732c7Sopenharmony_ci        return;
185079a732c7Sopenharmony_ci    }
185179a732c7Sopenharmony_ci    if (bindLevel == SERVICE) {
185279a732c7Sopenharmony_ci        SendServiceUnBindBroadCast(peerUdids, userId, tokenId);
185379a732c7Sopenharmony_ci        return;
185479a732c7Sopenharmony_ci    }
185579a732c7Sopenharmony_ci}
185679a732c7Sopenharmony_ci
185779a732c7Sopenharmony_civoid DeviceManagerService::SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId)
185879a732c7Sopenharmony_ci{
185979a732c7Sopenharmony_ci    RelationShipChangeMsg msg;
186079a732c7Sopenharmony_ci    msg.type = RelationShipChangeType::DEVICE_UNBIND;
186179a732c7Sopenharmony_ci    msg.userId = static_cast<uint32_t>(userId);
186279a732c7Sopenharmony_ci    msg.peerUdids = peerUdids;
186379a732c7Sopenharmony_ci    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
186479a732c7Sopenharmony_ci    CHECK_NULL_VOID(softbusListener_);
186579a732c7Sopenharmony_ci    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
186679a732c7Sopenharmony_ci}
186779a732c7Sopenharmony_ci
186879a732c7Sopenharmony_civoid DeviceManagerService::SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
186979a732c7Sopenharmony_ci    uint64_t tokenId)
187079a732c7Sopenharmony_ci{
187179a732c7Sopenharmony_ci    RelationShipChangeMsg msg;
187279a732c7Sopenharmony_ci    msg.type = RelationShipChangeType::APP_UNBIND;
187379a732c7Sopenharmony_ci    msg.userId = static_cast<uint32_t>(userId);
187479a732c7Sopenharmony_ci    msg.peerUdids = peerUdids;
187579a732c7Sopenharmony_ci    msg.tokenId = tokenId;
187679a732c7Sopenharmony_ci    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
187779a732c7Sopenharmony_ci    CHECK_NULL_VOID(softbusListener_);
187879a732c7Sopenharmony_ci    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
187979a732c7Sopenharmony_ci}
188079a732c7Sopenharmony_ci
188179a732c7Sopenharmony_civoid DeviceManagerService::SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
188279a732c7Sopenharmony_ci    uint64_t tokenId)
188379a732c7Sopenharmony_ci{
188479a732c7Sopenharmony_ci    RelationShipChangeMsg msg;
188579a732c7Sopenharmony_ci    msg.type = RelationShipChangeType::SERVICE_UNBIND;
188679a732c7Sopenharmony_ci    msg.userId = static_cast<uint32_t>(userId);
188779a732c7Sopenharmony_ci    msg.peerUdids = peerUdids;
188879a732c7Sopenharmony_ci    msg.tokenId = tokenId;
188979a732c7Sopenharmony_ci    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
189079a732c7Sopenharmony_ci    CHECK_NULL_VOID(softbusListener_);
189179a732c7Sopenharmony_ci    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
189279a732c7Sopenharmony_ci}
189379a732c7Sopenharmony_ci#endif
189479a732c7Sopenharmony_ci
189579a732c7Sopenharmony_civoid DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg)
189679a732c7Sopenharmony_ci{
189779a732c7Sopenharmony_ci    if (msg.empty()) {
189879a732c7Sopenharmony_ci        LOGE("Msg is empty.");
189979a732c7Sopenharmony_ci        return;
190079a732c7Sopenharmony_ci    }
190179a732c7Sopenharmony_ci    RelationShipChangeMsg relationShipMsg =
190279a732c7Sopenharmony_ci        ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
190379a732c7Sopenharmony_ci    std::string tokenIdStr = std::to_string(relationShipMsg.tokenId);
190479a732c7Sopenharmony_ci    LOGI("EventType %{public}d, userId %{public}d, accountId %{public}s, tokenId %{public}s,"
190579a732c7Sopenharmony_ci        "peerUdid %{public}s, accountName %{public}s.", relationShipMsg.type, relationShipMsg.userId,
190679a732c7Sopenharmony_ci        GetAnonyString(relationShipMsg.accountId).c_str(), GetAnonyString(tokenIdStr).c_str(),
190779a732c7Sopenharmony_ci        GetAnonyString(relationShipMsg.peerUdid).c_str(), GetAnonyString(relationShipMsg.accountName).c_str());
190879a732c7Sopenharmony_ci    if (!IsDMServiceImplReady()) {
190979a732c7Sopenharmony_ci        LOGE("Imp instance not init or init failed.");
191079a732c7Sopenharmony_ci        return;
191179a732c7Sopenharmony_ci    }
191279a732c7Sopenharmony_ci    switch (relationShipMsg.type) {
191379a732c7Sopenharmony_ci        case RelationShipChangeType::ACCOUNT_LOGOUT:
191479a732c7Sopenharmony_ci            dmServiceImpl_->HandleAccountLogoutEvent(relationShipMsg.userId, relationShipMsg.accountId,
191579a732c7Sopenharmony_ci                relationShipMsg.peerUdid);
191679a732c7Sopenharmony_ci            break;
191779a732c7Sopenharmony_ci        case RelationShipChangeType::DEVICE_UNBIND:
191879a732c7Sopenharmony_ci            dmServiceImpl_->HandleDevUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid);
191979a732c7Sopenharmony_ci            break;
192079a732c7Sopenharmony_ci        case RelationShipChangeType::APP_UNBIND:
192179a732c7Sopenharmony_ci            dmServiceImpl_->HandleAppUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid,
192279a732c7Sopenharmony_ci                static_cast<int32_t>(relationShipMsg.tokenId));
192379a732c7Sopenharmony_ci            break;
192479a732c7Sopenharmony_ci        default:
192579a732c7Sopenharmony_ci            LOGI("Dm have not this event type.");
192679a732c7Sopenharmony_ci            break;
192779a732c7Sopenharmony_ci    }
192879a732c7Sopenharmony_ci    return;
192979a732c7Sopenharmony_ci}
193079a732c7Sopenharmony_ci
193179a732c7Sopenharmony_civoid DeviceManagerService::ClearDiscoveryCache(const std::string &pkgName)
193279a732c7Sopenharmony_ci{
193379a732c7Sopenharmony_ci    LOGI("PkgName %{public}s.", pkgName.c_str());
193479a732c7Sopenharmony_ci    CHECK_NULL_VOID(discoveryMgr_);
193579a732c7Sopenharmony_ci    discoveryMgr_->ClearDiscoveryCache(pkgName);
193679a732c7Sopenharmony_ci}
193779a732c7Sopenharmony_ci
193879a732c7Sopenharmony_civoid DeviceManagerService::HandleDeviceScreenStatusChange(DmDeviceInfo &deviceInfo)
193979a732c7Sopenharmony_ci{
194079a732c7Sopenharmony_ci    if (IsDMServiceImplReady()) {
194179a732c7Sopenharmony_ci        dmServiceImpl_->HandleDeviceScreenStatusChange(deviceInfo);
194279a732c7Sopenharmony_ci    }
194379a732c7Sopenharmony_ci}
194479a732c7Sopenharmony_ci
194579a732c7Sopenharmony_ciint32_t DeviceManagerService::GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
194679a732c7Sopenharmony_ci    int32_t &screenStatus)
194779a732c7Sopenharmony_ci{
194879a732c7Sopenharmony_ci    LOGI("Begin pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(), GetAnonyString(networkId).c_str());
194979a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
195079a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetDeviceScreenStatus.", pkgName.c_str());
195179a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
195279a732c7Sopenharmony_ci    }
195379a732c7Sopenharmony_ci    if (pkgName.empty() || networkId.empty()) {
195479a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
195579a732c7Sopenharmony_ci            GetAnonyString(networkId).c_str());
195679a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
195779a732c7Sopenharmony_ci    }
195879a732c7Sopenharmony_ci    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
195979a732c7Sopenharmony_ci    int32_t ret = softbusListener_->GetDeviceScreenStatus(networkId.c_str(), screenStatus);
196079a732c7Sopenharmony_ci    if (ret != DM_OK) {
196179a732c7Sopenharmony_ci        LOGE("GetDeviceScreenStatus failed, ret = %{public}d", ret);
196279a732c7Sopenharmony_ci        return ret;
196379a732c7Sopenharmony_ci    }
196479a732c7Sopenharmony_ci    return DM_OK;
196579a732c7Sopenharmony_ci}
196679a732c7Sopenharmony_ci
196779a732c7Sopenharmony_ciint32_t DeviceManagerService::GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid,
196879a732c7Sopenharmony_ci                                                 std::string &networkId)
196979a732c7Sopenharmony_ci{
197079a732c7Sopenharmony_ci    if (!PermissionManager::GetInstance().CheckPermission()) {
197179a732c7Sopenharmony_ci        LOGE("The caller: %{public}s does not have permission to call GetNetworkIdByUdid.", pkgName.c_str());
197279a732c7Sopenharmony_ci        return ERR_DM_NO_PERMISSION;
197379a732c7Sopenharmony_ci    }
197479a732c7Sopenharmony_ci    if (pkgName.empty() || udid.empty()) {
197579a732c7Sopenharmony_ci        LOGE("Invalid parameter, pkgName: %{public}s, udid: %{public}s", pkgName.c_str(), GetAnonyString(udid).c_str());
197679a732c7Sopenharmony_ci        return ERR_DM_INPUT_PARA_INVALID;
197779a732c7Sopenharmony_ci    }
197879a732c7Sopenharmony_ci    return SoftbusListener::GetNetworkIdByUdid(udid, networkId);
197979a732c7Sopenharmony_ci}
198079a732c7Sopenharmony_ci
198179a732c7Sopenharmony_civoid DeviceManagerService::SubscribePackageCommonEvent()
198279a732c7Sopenharmony_ci{
198379a732c7Sopenharmony_ci    LOGI("Start");
198479a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
198579a732c7Sopenharmony_ci    if (packageCommonEventManager_ == nullptr) {
198679a732c7Sopenharmony_ci        packageCommonEventManager_ = std::make_shared<DmPackageCommonEventManager>();
198779a732c7Sopenharmony_ci    }
198879a732c7Sopenharmony_ci    PackageEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
198979a732c7Sopenharmony_ci        if (IsDMServiceImplReady()) {
199079a732c7Sopenharmony_ci            dmServiceImpl_->ProcessAppUnintall(arg1, arg3);
199179a732c7Sopenharmony_ci        }
199279a732c7Sopenharmony_ci        KVAdapterManager::GetInstance().AppUnintall(arg1);
199379a732c7Sopenharmony_ci    };
199479a732c7Sopenharmony_ci    std::vector<std::string> commonEventVec;
199579a732c7Sopenharmony_ci    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
199679a732c7Sopenharmony_ci    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
199779a732c7Sopenharmony_ci    if (packageCommonEventManager_->SubscribePackageCommonEvent(commonEventVec, callback)) {
199879a732c7Sopenharmony_ci        LOGI("Success");
199979a732c7Sopenharmony_ci    }
200079a732c7Sopenharmony_ci#endif
200179a732c7Sopenharmony_ci}
200279a732c7Sopenharmony_ci
200379a732c7Sopenharmony_civoid DeviceManagerService::HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId,
200479a732c7Sopenharmony_ci                                                      int32_t errcode)
200579a732c7Sopenharmony_ci{
200679a732c7Sopenharmony_ci    if (IsDMServiceImplReady()) {
200779a732c7Sopenharmony_ci        dmServiceImpl_->HandleCredentialAuthStatus(deviceList, deviceTypeId, errcode);
200879a732c7Sopenharmony_ci    }
200979a732c7Sopenharmony_ci}
201079a732c7Sopenharmony_ci} // namespace DistributedHardware
201179a732c7Sopenharmony_ci} // namespace OHOS