/*
 * Copyright (c) 2022-2026 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "device_manager_service.h"

#include "cJSON.h"
#include <dlfcn.h>
#include <functional>
#include <chrono>
#include <thread>
#include "app_manager.h"
#include "dm_anonymous.h"
#include "dm_constants.h"
#include "dm_crypto.h"
#include "dm_device_info.h"
#include "dm_hidumper.h"
#include "dm_softbus_cache.h"
#include "json_object.h"
#include "parameter.h"
#include "permission_manager.h"
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
#include "dm_constraints_manager.h"
#include "common_event_support.h"
#include "datetime_ex.h"
#include "deviceprofile_connector.h"
#include "device_name_manager.h"
#include "distributed_device_profile_client.h"
#include "dm_comm_tool.h"
#include "dm_library_manager.h"
#include "dm_random.h"
#include "dm_transport_msg.h"
#include "dm_jsonstr_handle.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "kv_adapter_manager.h"
#include "multiple_user_connector.h"
#include "relationship_sync_mgr.h"
#include "openssl/sha.h"
#include "system_ability_definition.h"
#if defined(SUPPORT_POWER_MANAGER)
#include "power_mgr_client.h"
#endif // SUPPORT_POWER_MANAGER
#if defined(SUPPORT_BLUETOOTH)
#include "softbus_publish.h"
#include "bluetooth_def.h"
#include "bluetooth_host.h"
#endif // SUPPORT_BLUETOOTH
#if defined(SUPPORT_WIFI)
#include "softbus_publish.h"
#include "wifi_device.h"
#include "wifi_msg.h"
#endif // SUPPORT_WIFI
#endif
#include "ipc_server_stub.h"

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
constexpr const char* LIB_IMPL_NAME = "libdevicemanagerserviceimpl.z.so";
using namespace OHOS::EventFwk;
#else
constexpr const char* LIB_IMPL_NAME = "libdevicemanagerserviceimpl.so";
#endif
constexpr const char* LIB_DM_ADAPTER_NAME = "libdevicemanageradapter.z.so";
constexpr const char* LIB_DM_RESIDENT_NAME = "libdevicemanagerresident.z.so";
constexpr const char* LIB_DM_DEVICE_RISK_DETECT_NAME = "libdevicemanagerriskdetect.z.so";
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG)
constexpr const char* LIB_DM_CHECK_API_WHITE_LIST_NAME = "libdm_check_api_whitelist.z.so";
#endif

namespace OHOS {
namespace DistributedHardware {
DM_IMPLEMENT_SINGLE_INSTANCE(DeviceManagerService);
namespace {
    const int32_t NORMAL = 0;
    const int32_t SYSTEM_BASIC = 1;
    const int32_t SYSTEM_CORE = 2;
    const uint32_t UNBIND_PROXY_ITEM_SIZE = 1;
    constexpr const char *ALL_PKGNAME = "";
    constexpr const char *NETWORKID = "NETWORK_ID";
    constexpr uint32_t INVALIED_BIND_LEVEL = 0;
    constexpr uint32_t DM_IDENTICAL_ACCOUNT = 1;
    constexpr uint32_t DM_SHARE = 2;
    const std::string USERID_CHECKSUM_NETWORKID_KEY = "networkId";
    const std::string USERID_CHECKSUM_DISCOVER_TYPE_KEY = "discoverType";
    const std::string DHARD_WARE_PKG_NAME = "ohos.dhardware";
    const std::string USERID_CHECKSUM_ISCHANGE_KEY = "ischange";
    constexpr const char* USER_SWITCH_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:userSwitchByWifi";
    constexpr const char* USER_STOP_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:userStopByWifi";
    constexpr const char* APP_UNINSTALL_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:appUninstallByWifi";
    constexpr const char* APP_UNBIND_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:appUnbindByWifi";
    constexpr const char* ACCOUNT_COMMON_EVENT_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:accountCommonEventByWifi";
    constexpr const char* SERVICE_UNBIND_PROXY_BY_WIFI_TIMEOUT_TASK = "deviceManagerTimer:serviceUnbindProxyByWifi";
    const int32_t USER_SWITCH_BY_WIFI_TIMEOUT_S = 2;
    const int32_t SEND_DELAY_MAX_TIME = 5;
    const int32_t SEND_DELAY_MIN_TIME = 0;
    const int32_t DELAY_TIME_SEC_CONVERSION = 1000000;      // 1000*1000
    // 2148 * 1000000 will exceed the maximum value of int32 and cause a crash
    const int32_t SEND_LIMIT_TIME = INT32_MAX / DELAY_TIME_SEC_CONVERSION;
    const int32_t RANDOM_OFF_SET = 8;
    const int32_t HEARTBEAT_DELAY_TIME_MIN = 1000;
    const int32_t HEARTBEAT_DELAY_TIME_MAX = 15000;
    constexpr int32_t DM_MIN_PINCODE_SIZE = 6;
    constexpr int32_t DM_MAX_PINCODE_SIZE = 1024;
    constexpr int32_t DM_PARAM_STRING_LENGTH_MAX = 1024;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG)
    const std::string GET_LOCAL_DEVICE_NAME_API_NAME = "GetLocalDeviceName";
#endif
    constexpr const char* LOCAL_ALL_USERID = "local_all_userId";
    constexpr const char* LOCAL_FOREGROUND_USERID = "local_foreground_userId";
    constexpr const char* LOCAL_BACKGROUND_USERID = "local_background_userId";
    constexpr int32_t GENERATE_SERVICE_ID_RETRY_TIME = 3;
    constexpr uint64_t DEFAULT_TOKEN_ID = 0;
    constexpr const char* DEFAULT_PKG_NAME = "";
    constexpr uint64_t DEFAULT_SERVICE_ID = 0;
    constexpr int32_t DM_MAX_SERVICE_TYPE = 15;
    constexpr int32_t DM_MAX_SERVICE_NAME = 64;
    constexpr int32_t DM_MIN_SERVICE_DISPLAYNAME = 7;
    constexpr int32_t DM_MAX_SERVICE_DISPLAYNAME = 128;
    constexpr int32_t DM_MAX_CUSTOMDATA = 1024;
    constexpr int32_t SYNC_FOREGROUND_ACCOUNT_BROADCAST_INTERVAL_MS = 5000;
    constexpr int32_t OS_TYPE_BLE = 16;
    constexpr int32_t OS_TYPE_BLE_BR = 17;
    constexpr size_t MAX_FOREGROUND_ACCOUNT_CACHE_SIZE = 10000;
    constexpr size_t MAX_SINGLE_CACHE_ACCOUNTS_SIZE = 100;
    constexpr int32_t FOREGROUND_ACCOUNT_CACHE_TIMEOUT_S = 60;
    constexpr const char* FOREGROUND_ACCOUNT_CACHE_TIMEOUT_TASK = "ForegroundAccountCacheTimeout";

    constexpr const char* SYNC_SERVICE_INFO_ONLINE_TASK = "SyncServiceInfoOnlineTask";
    constexpr const char* SEND_APP_UN_BIND_BROAD_CAST_TASK = "SendAppUnBindBroadCastTask";
    constexpr const char* SEND_APP_UN_INSTALL_BROAD_CAST_TASK = "SendAppUnInstallBroadCastTask";
    constexpr const char* HANDLE_ACCOUNT_LOGOUT_EVENT_CALLBACK_TASK = "HandleAccountLogoutEventCallbackTask";
    constexpr const char* HANDLE_USER_REMOVED_TASK = "HandleUserRemovedTask";
    constexpr const char* HANDLE_ACCOUNT_LOGOUT_EVENT_TASK = "HandleAccountLogoutEventTask";
    constexpr const char* HANDLE_COMMON_EVENT_BROAD_CAST_TASK = "HandleCommonEventBroadCastTask";
    constexpr const char* HANDLE_USER_IDS_BROAD_CAST_TASK = "HandleUserIdsBroadCastTask";
    constexpr const char* HANDLE_REMOTE_USER_REMOVED_TASK = "HandleRemoteUserRemovedTask";
    constexpr const char* ON_SET_LOCAL_DEVICE_NAME_RESULT_TASK = "OnSetLocalDeviceNameResultTask";
    constexpr const char* HANDLE_SERVICE_UN_REG_EVENT_TASK = "HandleServiceUnRegEventTask";
}
//LCOV_EXCL_START
DeviceManagerService::~DeviceManagerService()
{
    LOGI("destructor");
    UnloadDMServiceImplSo();
    UnloadDMServiceAdapterResident();
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG)
    UnloadDmCheckApiWhiteListSo();
#endif
}

int32_t DeviceManagerService::Init()
{
    InitSoftbusListener();
    InitDMServiceListener();
    LOGI("success, dm service single instance initialized.");

    int32_t ret = HandleProcessRestart();
    if (ret != DM_OK) {
        LOGE("HandleProcessRestart failed, ret: %{public}d", ret);
    }
    return DM_OK;
}

int32_t DeviceManagerService::HandleProcessRestart()
{
    LOGI("Handle DM Process Restart start");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("DM Serivce adapter resident not load");
        return ERR_DM_INIT_FAILED;
    }

    int32_t ret = dmServiceImplExtResident_->HandleDMRestart();
    if (ret != DM_OK) {
        LOGE("Update ServiceId Cache failed, ret: %{public}d", ret);
    }
#endif
    LOGI("Handle DM Process Restart success");
    return DM_OK;
}

int32_t DeviceManagerService::InitSoftbusListener()
{
    if (softbusListener_ == nullptr) {
        softbusListener_ = std::make_shared<SoftbusListener>();
    }
    SoftbusCache::GetInstance().UpdateDeviceInfoCache();
    std::vector<DmDeviceInfo> onlineDeviceList;
    SoftbusCache::GetInstance().GetDeviceInfoFromCache(onlineDeviceList);
    if (onlineDeviceList.size() > 0 && IsDMServiceImplReady()) {
        dmServiceImpl_->SaveOnlineDeviceInfo(onlineDeviceList);
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
    SubscribePublishCommonEvent();
    QueryDependsSwitchState();
#endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
    SubscribeDataShareCommonEvent();
#endif
    LOGI("SoftbusListener init success.");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (IsDMServiceAdapterResidentLoad()) {
        int32_t ret = dmServiceImplExtResident_->InitSoftbusServer();
        if (ret != DM_OK) {
            LOGE("resident InitSoftbusServer failed.");
        }
        ret = dmServiceImplExtResident_->SetServiceNodeKeyInfo(DM_PKG_NAME);
        if (ret != DM_OK) {
            LOGE("Set ServiceId NodeKeyInfo failed, ret: %{public}d", ret);
            return ret;
        }
    }
#endif
    return DM_OK;
}

void DeviceManagerService::InitHichainListener()
{
    LOGI("Start.");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(hichainListenerLock_);
#else
    std::lock_guard<std::mutex> lock(hichainListenerLock_);
#endif
    if (hichainListener_ == nullptr) {
        hichainListener_ = std::make_shared<HichainListener>();
    }
    hichainListener_->RegisterDataChangeCb();
    hichainListener_->RegisterCredentialCb();
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
void DeviceManagerService::StartDetectDeviceRisk()
{
    std::lock_guard<ffrt::mutex> lock(detectLock_);
    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    CHECK_NULL_VOID(samgr);
    if (samgr->CheckSystemAbility(RISK_ANALYSIS_MANAGER_SA_ID) == nullptr) {
        LOGE("%{public}d sa not start", RISK_ANALYSIS_MANAGER_SA_ID);
        return;
    }
    auto loadRet = LoadDMDeviceRiskDetect();
    if (!loadRet.first || loadRet.second == nullptr) {
        LOGE("load dm device risk detect failed.");
        return;
    }

    bool isRisk = loadRet.second->IsDeviceHasRisk();
    if (isRisk) {
        LOGI("device status is Illegal");
        DelAllRelateShip();
    }
    UnloadDMDeviceRiskDetect(loadRet.second);
    return;
}

void DeviceManagerService::DelAllRelateShip()
{
    std::lock_guard<ffrt::mutex> lock(hichainListenerLock_);
    if (hichainListener_ == nullptr) {
        hichainListener_ = std::make_shared<HichainListener>();
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
    std::vector<int32_t> currentUserIds;
    currentUserIds.push_back(userId);
    hichainListener_->DeleteAllGroup(localUdid, currentUserIds);
    std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
        DeviceProfileConnector::GetInstance().GetAllAclIncludeLnnAcl();
    for (auto &item : profiles) {
        std::string acerDeviceId = item.GetAccesser().GetAccesserDeviceId();
        std::string aceeDeviceId = item.GetAccessee().GetAccesseeDeviceId();
        if (localUdid == acerDeviceId) {
            int32_t acerUserId = item.GetAccesser().GetAccesserUserId();
            int32_t acerSkId = item.GetAccesser().GetAccesserSessionKeyId();
            DeviceProfileConnector::GetInstance().DeleteSessionKey(acerUserId, acerSkId);

            std::string acerCredId = item.GetAccesser().GetAccesserCredentialIdStr();
            hichainListener_->DeleteCredential(acerUserId, acerCredId);
        } else if (localUdid == aceeDeviceId) {
            int32_t aceeUserId = item.GetAccessee().GetAccesseeUserId();
            int32_t aceeSkId = item.GetAccessee().GetAccesseeSessionKeyId();
            DeviceProfileConnector::GetInstance().DeleteSessionKey(aceeUserId, aceeSkId);

            std::string aceeCredId = item.GetAccessee().GetAccesseeCredentialIdStr();
            hichainListener_->DeleteCredential(aceeUserId, aceeCredId);
        }
        int32_t aclId = item.GetAccessControlId();
        DeviceProfileConnector::GetInstance().DeleteAccessControlById(aclId);
    }
}
#endif

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
void DeviceManagerService::SubscribePublishCommonEvent()
{
    LOGI("start");
    PublishEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
        OHOS::DistributedHardware::PublishCommonEventCallback(arg1, arg2, arg3);
    };
    std::vector<std::string> PublishCommonEventVec;
#ifdef SUPPORT_BLUETOOTH
    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE);
#endif // SUPPORT_BLUETOOTH

#ifdef SUPPORT_WIFI
    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
#endif // SUPPORT_WIFI
    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
    PublishCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
    std::lock_guard<ffrt::mutex> lock(eventManagerLock_);
    if (publishCommonEventManager_ == nullptr) {
        publishCommonEventManager_ = std::make_shared<DmPublishCommonEventManager>();
    }
    if (publishCommonEventManager_->SubscribePublishCommonEvent(PublishCommonEventVec, callback)) {
        LOGI("subscribe ble and wifi and screen common event success");
    }
    return;
}
#endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
DM_EXPORT void DeviceManagerService::SubscribeDataShareCommonEvent()
{
    LOGI("start");
    if (dataShareCommonEventManager_ == nullptr) {
        dataShareCommonEventManager_ = std::make_shared<DmDataShareCommonEventManager>();
    }
    DataShareEventCallback callback = [=](const auto &arg1, const auto &arg2) {
        if (arg1 == CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY) {
            DeviceNameManager::GetInstance().DataShareReady();
        }
        if (arg1 == CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED) {
            DeviceNameManager::GetInstance().InitDeviceNameWhenLanguageOrRegionChanged();
        }
        if (arg1 == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE) {
            this->HandleNetworkConnected(arg2);
        }
    };
    std::vector<std::string> commonEventVec;
    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_DATA_SHARE_READY);
    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_LOCALE_CHANGED);
    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE);
    if (dataShareCommonEventManager_->SubscribeDataShareCommonEvent(commonEventVec, callback)) {
        LOGI("subscribe datashare common event success");
    }
}
#endif

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
void DeviceManagerService::QueryDependsSwitchState()
{
    std::shared_ptr<DmPublishEventSubscriber> publishSubScriber = nullptr;
    {
        std::lock_guard<ffrt::mutex> lock(eventManagerLock_);
        CHECK_NULL_VOID(publishCommonEventManager_);
        publishSubScriber = publishCommonEventManager_->GetSubscriber();
    }
    CHECK_NULL_VOID(publishSubScriber);
    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
    CHECK_NULL_VOID(samgr);
#ifdef SUPPORT_BLUETOOTH
    if (samgr->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID) == nullptr) {
        publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
    } else {
        if (Bluetooth::BluetoothHost::GetDefaultHost().IsBleEnabled()) {
            publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON));
        } else {
            publishSubScriber->SetBluetoothState(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
        }
    }
#endif // SUPPORT_BLUETOOTH

#ifdef SUPPORT_WIFI
    if (samgr->CheckSystemAbility(WIFI_DEVICE_SYS_ABILITY_ID) == nullptr) {
        publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
    } else {
        bool isWifiActive = false;
        auto wifiMgr = Wifi::WifiDevice::GetInstance(WIFI_DEVICE_ABILITY_ID);
        CHECK_NULL_VOID(wifiMgr);
        wifiMgr->IsWifiActive(isWifiActive);
        if (isWifiActive) {
            publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED));
        } else {
            publishSubScriber->SetWifiState(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
        }
    }
#endif // SUPPORT_WIFI

    ScreenState state = DM_SCREEN_ON;
#ifdef SUPPORT_POWER_MANAGER
    if (samgr->CheckSystemAbility(POWER_MANAGER_SERVICE_ID) == nullptr) {
        state = DM_SCREEN_OFF;
    } else {
        if (!OHOS::PowerMgr::PowerMgrClient::GetInstance().IsScreenOn()) {
            state = DM_SCREEN_OFF;
        }
    }
#endif // SUPPORT_POWER_MANAGER
    publishSubScriber->SetScreenState(state);
    OHOS::DistributedHardware::PublishCommonEventCallback(publishSubScriber->GetBluetoothState(),
        publishSubScriber->GetWifiState(), publishSubScriber->GetScreenState());
}
#endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
#endif

void DeviceManagerService::UninitSoftbusListener()
{
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->DeleteCacheDeviceInfo();
    softbusListener_ = nullptr;
    LOGI("SoftbusListener uninit.");
}

int32_t DeviceManagerService::InitDMServiceListener()
{
    if (listener_ == nullptr) {
        listener_ = std::make_shared<DeviceManagerServiceListener>();
    }
    if (advertiseMgr_ == nullptr) {
        advertiseMgr_ = std::make_shared<AdvertiseManager>(softbusListener_);
    }
    if (discoveryMgr_ == nullptr) {
        discoveryMgr_ = std::make_shared<DiscoveryManager>(softbusListener_, listener_);
    }
    if (pinHolder_ == nullptr) {
        pinHolder_ = std::make_shared<PinHolder>(listener_);
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (hiChainConnector_ == nullptr) {
        hiChainConnector_ = std::make_shared<DmServiceHiChainConnector>();
    }
    if (credentialMgr_ == nullptr) {
        credentialMgr_ = std::make_shared<DmCredentialManager>(hiChainConnector_, listener_);
    }
    CHECK_NULL_RETURN(DMCommTool::GetInstance(), ERR_DM_POINT_NULL);
    DMCommTool::GetInstance()->Init();
    int32_t currentUserId = MultipleUserConnector::GetFirstForegroundUserId();
    if (IsPC() && !MultipleUserConnector::IsUserUnlocked(currentUserId)) {
        HandleUserStopEvent(currentUserId);
    }
    InitTaskOfDelTimeOutAcl();
    DeviceProfileConnector::GetInstance().DeleteDpInvalidAcl();
    if (IsDMServiceAdapterResidentLoad()) {
        int32_t ret = dmServiceImplExtResident_->SubscribeAllServiceInfo();
        if (ret != DM_OK) {
            LOGE("SubscribeAllServiceInfo failed, ret: %{public}d", ret);
        }
    }
    if (dpInitedCallback_ == nullptr) {
        dpInitedCallback_ = sptr<DpInitedCallback>(new DpInitedCallback());
        DeviceProfileConnector::GetInstance().SubscribeDeviceProfileInited(dpInitedCallback_);
    }
#endif
    LOGI("Init success.");
    return DM_OK;
}

DM_EXPORT void DeviceManagerService::UninitDMServiceListener()
{
    listener_ = nullptr;
    advertiseMgr_ = nullptr;
    discoveryMgr_ = nullptr;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    DeviceNameManager::GetInstance().UnInit();
    KVAdapterManager::GetInstance().UnInit();
    dpInitedCallback_ = nullptr;
    DeviceProfileConnector::GetInstance().UnSubscribeDeviceProfileInited();
#endif
    LOGI("Uninit.");
}
//LCOV_EXCL_STOP

DM_EXPORT void DeviceManagerService::RegisterCallerAppId(const std::string &pkgName, const int32_t userId)
{
    AppManager::GetInstance().RegisterCallerAppId(pkgName, userId);
}

DM_EXPORT void DeviceManagerService::UnRegisterCallerAppId(const std::string &pkgName, const int32_t userId)
{
    AppManager::GetInstance().UnRegisterCallerAppId(pkgName, userId);
}

#ifdef CAR_DEVICE_ENABLE
int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
                                                   std::vector<DmDeviceInfo> &deviceList)
{
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    if (!PermissionManager::GetInstance().CheckAccessServicePermission() &&
        !PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call GetTrustedDeviceList.");
        return ERR_DM_NO_PERMISSION;
    }
    ProcessInfo processInfo;
    processInfo.pkgName = pkgName;
    int32_t displayId = 0;
    JsonObject jsonObject(extra);
    if (!jsonObject.IsDiscarded() && IsInt32(jsonObject, "localDisplayId")) {
        displayId = jsonObject["localDisplayId"].Get<int32_t>();
        LOGI("localDisplayId: %{public}d", displayId);
    }
    processInfo.userId = MultipleUserConnector::GetUserIdByDisplayId(displayId);
    processInfo.accountId = MultipleUserConnector::GetAccountIdByUserId(processInfo.userId);
    LOGI("pkgName: %{public}s, userId: %{public}d, accountId: %{public}s",
        processInfo.pkgName.c_str(), processInfo.userId, GetAnonyString(processInfo.accountId).c_str());
    return GetTrustedDeviceList(processInfo, deviceList);
}
#else
int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
                                                   std::vector<DmDeviceInfo> &deviceList)
{
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
#endif
    bool isOnlyShowNetworkId = !(PermissionManager::GetInstance().CheckAccessServicePermission() ||
        PermissionManager::GetInstance().CheckDataSyncPermission());
    std::vector<DmDeviceInfo> onlineDeviceList;
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    if (softbusListener_->GetTrustedDeviceList(onlineDeviceList) != DM_OK) {
        return ERR_DM_FAILED;
    }
    return HandleTrustedDeviceList(pkgName, isOnlyShowNetworkId, onlineDeviceList, deviceList);
}

int32_t DeviceManagerService::HandleTrustedDeviceList(const std::string &pkgName, bool isOnlyShowNetworkId,
    const std::vector<DmDeviceInfo> &onlineDeviceList, std::vector<DmDeviceInfo> &deviceList)
{
    if (isOnlyShowNetworkId && !onlineDeviceList.empty()) {
        for (auto item : onlineDeviceList) {
            DmDeviceInfo tempInfo;
            if (memcpy_s(tempInfo.networkId, DM_MAX_DEVICE_ID_LEN, item.networkId, sizeof(item.networkId)) != 0) {
                LOGE("get networkId: %{public}s failed", GetAnonyString(item.networkId).c_str());
                return ERR_DM_SECURITY_FUNC_FAILED;
            }
            deviceList.push_back(tempInfo);
        }
        return DM_OK;
    }
    if (!onlineDeviceList.empty() && IsDMServiceImplReady()) {
        std::unordered_map<std::string, DmAuthForm> udidMap;
        if (PermissionManager::GetInstance().CheckWhiteListSystemSA(pkgName) ||
            PermissionManager::GetInstance().CheckProcessValidOnGetTrustedDeviceList()) {
            udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(std::string(ALL_PKGNAME));
        } else {
            udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(pkgName);
        }
        for (auto item : onlineDeviceList) {
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
            ConvertUdidHashToAnoyDeviceId(item);
#endif
            std::string udid = "";
            SoftbusListener::GetUdidByNetworkId(item.networkId, udid);
            if (udidMap.find(udid) != udidMap.end()) {
                item.authForm = udidMap[udid];
                deviceList.push_back(item);
            }
        }
    }
    return DM_OK;
}
#endif

int32_t DeviceManagerService::GetAllTrustedDeviceList(const std::string &pkgName, const std::string &extra,
                                                      std::vector<DmDeviceInfo> &deviceList)
{
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
#endif
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetAllTrustedDeviceList.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetAllTrustedDeviceList(pkgName, extra, deviceList);
    if (ret != DM_OK) {
        LOGE("GetAllTrustedDeviceList failed");
        return ret;
    }
    return DM_OK;
}

int32_t DeviceManagerService::ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh,
                                           bool isWakeUp)
{
    LOGD("Begin for pkgName = %{public}s, callerId = %{public}s, isRefresh ="
        "%{public}d, isWakeUp = %{public}d", pkgName.c_str(), GetAnonyString(callerId).c_str(), isRefresh, isWakeUp);
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call ShiftLNNGear, pkgName = %{public}s", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || callerId.empty()) {
        LOGE("Invalid parameter, parameter is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (isRefresh) {
        CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
        int32_t ret = softbusListener_->ShiftLNNGear(isWakeUp, callerId);
        if (ret != DM_OK) {
            LOGE("failed ret: %{public}d", ret);
            return ret;
        }
    }
    return DM_OK;
}

int32_t DeviceManagerService::GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info)
{
    LOGI("Begin networkId %{public}s.", GetAnonyString(networkId).c_str());
    CHECK_EMPTY_RETURN(networkId, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(networkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission() &&
        !PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call GetDeviceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    std::string peerDeviceId = "";
    SoftbusListener::GetUdidByNetworkId(networkId.c_str(), peerDeviceId);
    int32_t ret = DM_OK;
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    if (localUdid == peerDeviceId) {
        ret = softbusListener_->GetDeviceInfo(networkId, info);
        if (ret != DM_OK) {
            LOGE("Get DeviceInfo By NetworkId failed, ret : %{public}d", ret);
        }
        return ret;
    }
    if (!AppManager::GetInstance().IsSystemSA()) {
        int32_t permissionRet = dmServiceImpl_->CheckDeviceInfoPermission(localUdid, peerDeviceId);
        if (permissionRet != DM_OK) {
            std::string processName = "";
            if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
                LOGE("Get caller process name failed.");
                return ret;
            }
            if (!PermissionManager::GetInstance().CheckProcessNameValidOnGetDeviceInfo(processName)) {
                LOGE("The caller: %{public}s is not in white list.", processName.c_str());
                return ret;
            }
        }
    }
    return softbusListener_->GetDeviceInfo(networkId, info);
}

int32_t DeviceManagerService::GetLocalDeviceInfo(DmDeviceInfo &info)
{
    LOGD("Begin.");
    bool isOnlyShowNetworkId = false;
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call GetLocalDeviceInfo.");
        isOnlyShowNetworkId = true;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetLocalDeviceInfo(info);
    if (ret != DM_OK) {
        LOGE("GetLocalDeviceInfo failed");
        return ret;
    }
    if (isOnlyShowNetworkId) {
        DmDeviceInfo tempInfo;
        if (memcpy_s(tempInfo.networkId, DM_MAX_DEVICE_ID_LEN, info.networkId, sizeof(info.networkId)) != 0) {
            LOGE("get networkId: %{public}s failed", GetAnonyString(info.networkId).c_str());
            return ERR_DM_FAILED;
        }
        info = tempInfo;
        return DM_OK;
    }
    if (localDeviceId_.empty()) {
        char localDeviceId[DEVICE_UUID_LENGTH] = {0};
        char udidHash[DEVICE_UUID_LENGTH] = {0};
        GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
        if (Crypto::GetUdidHash(localDeviceId, reinterpret_cast<uint8_t *>(udidHash)) == DM_OK) {
            localDeviceId_ = udidHash;
        }
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHashTemp = "";
    if (ConvertUdidHashToAnoyDeviceId(localDeviceId_, udidHashTemp) == DM_OK) {
        if (memset_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
            LOGE("memset_s failed.");
            return ERR_DM_FAILED;
        }
        if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, udidHashTemp.c_str(), udidHashTemp.length()) != 0) {
            LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHashTemp).c_str());
            return ERR_DM_FAILED;
        }
        return DM_OK;
    }
#endif
    if (memcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, localDeviceId_.c_str(), localDeviceId_.length()) != 0) {
        LOGE("get deviceId: %{public}s failed", GetAnonyString(localDeviceId_).c_str());
        return ERR_DM_FAILED;
    }
    return DM_OK;
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG)
bool DeviceManagerService::IsCallerInWhiteList()
{
    if (!IsDMAdapterCheckApiWhiteListLoaded()) {
        LOGE("DMAdapterCheckApiWhiteListLoad failed.");
        return false;
    }
    std::string callerName;
    if (AppManager::GetInstance().GetCallerProcessName(callerName) != DM_OK) {
        LOGE("GetCallerProcessName failed.");
        return false;
    }
    return dmCheckApiWhiteList_->IsCallerInWhiteList(callerName, GET_LOCAL_DEVICE_NAME_API_NAME);
}

void DeviceManagerService::UnloadDmCheckApiWhiteListSo()
{
    LOGI("Start.");
    std::lock_guard<std::mutex> lock(isAdapterCheckApiWhiteListLoadedLock_);
    if (checkApiWhiteListSoHandle_ != nullptr) {
        LOGI("checkApiWhiteListSoHandle_ is not nullptr.");
        dlclose(checkApiWhiteListSoHandle_);
        checkApiWhiteListSoHandle_ = nullptr;
    }
}

bool DeviceManagerService::IsDMAdapterCheckApiWhiteListLoaded()
{
    LOGD("Start.");
    std::lock_guard<std::mutex> lock(isAdapterCheckApiWhiteListLoadedLock_);
    if (isAdapterCheckApiWhiteListSoLoaded_ && (dmCheckApiWhiteList_ != nullptr)) {
        return true;
    }
    checkApiWhiteListSoHandle_ = dlopen(LIB_DM_CHECK_API_WHITE_LIST_NAME, RTLD_NOW | RTLD_NODELETE | RTLD_NOLOAD);
    if (checkApiWhiteListSoHandle_ == nullptr) {
        checkApiWhiteListSoHandle_ = dlopen(LIB_DM_CHECK_API_WHITE_LIST_NAME, RTLD_NOW | RTLD_NODELETE);
    }
    if (checkApiWhiteListSoHandle_ == nullptr) {
        LOGE("load dm check api white list so failed.");
        return false;
    }
    if (dlerror() != nullptr) {
        dlclose(checkApiWhiteListSoHandle_);
        checkApiWhiteListSoHandle_ = nullptr;
        LOGE("open dm check api white list so failed. err: %{public}s", dlerror());
        return false;
    }
    auto func = (CreateDMCheckApiWhiteListFuncPtr)dlsym(checkApiWhiteListSoHandle_, "CreateDMCheckApiWhiteListObject");
    if (func == nullptr || dlerror() != nullptr) {
        dlclose(checkApiWhiteListSoHandle_);
        checkApiWhiteListSoHandle_ = nullptr;
        LOGE("Create object function is not exist. err: %{public}s", (dlerror() == nullptr ? "null" : dlerror()));
        return false;
    }
    dmCheckApiWhiteList_ = std::shared_ptr<IDMCheckApiWhiteList>(func());
    isAdapterCheckApiWhiteListSoLoaded_ = true;
    LOGI("Success.");
    return true;
}
#endif

int32_t DeviceManagerService::GetLocalDeviceNameOld(std::string &deviceName)
{
    LOGD("Begin.");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) && !defined(DEVICE_MANAGER_COMMON_FLAG)
    if (!PermissionManager::GetInstance().CheckDataSyncPermission() && !IsCallerInWhiteList()) {
        LOGE("The caller does not have permission to call GetLocalDeviceName.");
        return ERR_DM_NO_PERMISSION;
    }
#else
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call GetLocalDeviceName.");
        return ERR_DM_NO_PERMISSION;
    }
#endif
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    DmDeviceInfo info;
    int32_t ret = softbusListener_->GetLocalDeviceInfo(info);
    if (ret != DM_OK) {
        LOGE("GetLocalDeviceInfo failed");
        return ret;
    }
    deviceName = info.deviceName;
    return DM_OK;
}

int32_t DeviceManagerService::GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
                                                 std::string &udid)
{
    CHECK_EMPTY_RETURN(netWorkId, ERR_DM_INPUT_PARA_INVALID);
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(netWorkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetUdidByNetworkId.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    return SoftbusListener::GetUdidByNetworkId(netWorkId.c_str(), udid);
}

int32_t DeviceManagerService::GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId,
                                                 std::string &uuid)
{
    CHECK_EMPTY_RETURN(netWorkId, ERR_DM_INPUT_PARA_INVALID);
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(netWorkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetUuidByNetworkId.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    return SoftbusListener::GetUuidByNetworkId(netWorkId.c_str(), uuid);
}

int32_t DeviceManagerService::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call PublishDeviceDiscovery.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }

    std::map<std::string, std::string> advertiseParam;
    advertiseParam.insert(std::pair<std::string, std::string>(PARAM_KEY_PUBLISH_ID,
        std::to_string(publishInfo.publishId)));
    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
    return advertiseMgr_->StartAdvertising(pkgName, advertiseParam);
}

int32_t DeviceManagerService::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call UnPublishDeviceDiscovery.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
    return advertiseMgr_->StopAdvertising(pkgName, publishId);
}

int32_t DeviceManagerService::AuthenticateDevice(const std::string &pkgName, int32_t authType,
                                                 const std::string &deviceId, const std::string &extra)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return ERR_DM_CONSTRAINT_ENABLE;
    }
#endif
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call AuthenticateDevice.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || deviceId.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName: %{public}s", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    std::string queryDeviceId = deviceId;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHash = "";
    if (GetUdidHashByAnoyDeviceId(deviceId, udidHash) == DM_OK) {
        queryDeviceId = udidHash;
    }
#endif
    PeerTargetId targetId;
    ConnectionAddrType addrType;
    int32_t ret = SoftbusListener::GetTargetInfoFromCache(queryDeviceId, targetId, addrType);
    if (ret != DM_OK) {
        LOGE("cannot get target info from cached discovered device map.");
        return ERR_DM_BIND_INPUT_PARA_INVALID;
    }
    std::map<std::string, std::string> bindParam;
    bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
    bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_BIND_EXTRA_DATA, extra));
    bindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE, std::to_string(addrType)));
    return dmServiceImpl_->BindTarget(pkgName, targetId, bindParam);
}

int32_t DeviceManagerService::UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call UnAuthenticateDevice.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Begin for pkgName = %{public}s, networkId = %{public}s",
        pkgName.c_str(), GetAnonyString(networkId).c_str());
    if (pkgName.empty() || networkId.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s",
            pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    std::string udid = "";
    if (SoftbusListener::GetUdidByNetworkId(networkId.c_str(), udid) != DM_OK) {
        LOGE("GetUdidByNetworkId error: udid: %{public}s", GetAnonyString(udid).c_str());
        return ERR_DM_FAILED;
    }
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    uint64_t tokenId = 0;
    int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
    LOGI("get bindlevel %{public}d.", bindLevel);
    if (bindLevel == INVALIED_BIND_LEVEL) {
        LOGE("Acl not contain the bindLevel %{public}d.", bindLevel);
        return ERR_DM_FAILED;
    }
    if (dmServiceImpl_->UnAuthenticateDevice(pkgName, udid, bindLevel) != DM_OK) {
        LOGE("dmServiceImpl_ UnAuthenticateDevice failed.");
        return ERR_DM_FAILED;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::vector<std::string> peerUdids;
    peerUdids.emplace_back(udid);
    SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId, bindLevel);
#endif
    return DM_OK;
}

int32_t DeviceManagerService::StopAuthenticateDevice(const std::string &pkgName)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call StopAuthenticateDevice.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s",
            pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    LOGI("Begin for pkgName = %{public}s", pkgName.c_str());
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    if (dmServiceImpl_->StopAuthenticateDevice(pkgName) != DM_OK) {
        LOGE("dmServiceImpl_ StopAuthenticateDevice failed.");
        return ERR_DM_FAILED;
    }
    return DM_OK;
}

int32_t DeviceManagerService::BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
    const std::string &bindParam)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return ERR_DM_CONSTRAINT_ENABLE;
    }
#endif
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call BindDevice.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || deviceId.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName: %{public}s", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    std::string queryDeviceId = deviceId;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHash = "";
    if (GetUdidHashByAnoyDeviceId(deviceId, udidHash) == DM_OK) {
        queryDeviceId = udidHash;
    }
#endif
    PeerTargetId targetId;
    std::map<std::string, std::string> bindParamMap;
    std::string bindParamStr = bindParam;
    int32_t actionId = 0;
    SoftbusListener::GetActionId(queryDeviceId, actionId);
    if (actionId > 0) {
        targetId.deviceId = queryDeviceId;
        AddHmlInfoToBindParam(actionId, bindParamStr);
    } else {
        ConnectionAddrType addrType;
        int32_t ret = SoftbusListener::GetTargetInfoFromCache(queryDeviceId, targetId, addrType);
        if (ret != DM_OK) {
            LOGE("cannot get target info from cached discovered device map.");
            return ERR_DM_BIND_INPUT_PARA_INVALID;
        }
        bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE, std::to_string(addrType)));
    }
    bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_AUTH_TYPE, std::to_string(authType)));
    bindParamMap.insert(std::pair<std::string, std::string>(PARAM_KEY_BIND_EXTRA_DATA, bindParamStr));
    return dmServiceImpl_->BindTarget(pkgName, targetId, bindParamMap);
}

int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash)
{
    int32_t result = ValidateUnBindDeviceParams(pkgName, udidHash, "");
    if (result != DM_OK) {
        return result;
    }
    std::string realDeviceId = udidHash;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHashTemp = "";
    if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) {
        realDeviceId = udidHashTemp;
    }
#endif
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    std::string udid = "";
    if (softbusListener_->GetUdidFromDp(realDeviceId, udid) != DM_OK) {
        LOGE("Get udid by udidhash failed.");
        return ERR_DM_FAILED;
    }
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    uint64_t tokenId = 0;
    int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
    LOGI("UnAuthenticateDevice get bindlevel %{public}d.", bindLevel);
    if (bindLevel == INVALIED_BIND_LEVEL) {
        LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel);
        return ERR_DM_FAILED;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    uint32_t callingTokenId = 0;
    MultipleUserConnector::GetTokenId(callingTokenId);
    tokenId = static_cast<int64_t>(callingTokenId);
    std::vector<std::string> peerUdids;
    peerUdids.emplace_back(udid);
    int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
    std::map<std::string, std::string> wifiDevices;
    bool isBleActive = false;
    GetNotifyRemoteUnBindAppWay(userId, tokenId, wifiDevices, isBleActive);
    if (isBleActive) {
        SendUnBindBroadCast(peerUdids, userId, tokenId, bindLevel);
    } else {
        NotifyRemoteUnBindAppByWifi(userId, tokenId, "", wifiDevices);
    }
#endif
    if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel) != DM_OK) {
        LOGE("dmServiceImpl_ UnBindDevice failed.");
        return ERR_DM_FAILED;
    }
    return DM_OK;
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
std::set<std::pair<std::string, std::string>> DeviceManagerService::GetProxyInfosByParseExtra(
    const std::string &pkgName, const std::string &extra,
    std::vector<std::pair<int64_t, int64_t>> &agentToProxyVec)
{
    std::set<std::pair<std::string, std::string>> proxyInfos;
    JsonObject jsonObject(extra);
    if (jsonObject.IsDiscarded()) {
        proxyInfos.insert(std::pair<std::string, std::string>(pkgName, extra));
        return proxyInfos;
    }
    if (IsString(jsonObject, PARAM_KEY_IS_PROXY_UNBIND) &&
        jsonObject[PARAM_KEY_IS_PROXY_UNBIND].Get<std::string>() == DM_VAL_TRUE) {
        if (!IsString(jsonObject, PARAM_KEY_SUBJECT_PROXYED_SUBJECTS)) {
            return proxyInfos;
        }
    } else {
        if (!jsonObject.Contains(PARAM_KEY_SUBJECT_PROXYED_SUBJECTS)) {
            proxyInfos.insert(std::pair<std::string, std::string>(pkgName, extra));
        }
        return proxyInfos;
    }
    if (!AppManager::GetInstance().IsSystemSA()) {
        LOGE("no proxy permission");
        return proxyInfos;
    }
    std::string subjectProxyAppsStr = jsonObject[PARAM_KEY_SUBJECT_PROXYED_SUBJECTS].Get<std::string>();
    JsonObject allProxyObj;
    if (!allProxyObj.Parse(subjectProxyAppsStr) || allProxyObj.Items().size() != UNBIND_PROXY_ITEM_SIZE) {
        return proxyInfos;
    }
    int64_t proxyTokenId = static_cast<int64_t>(IPCSkeleton::GetCallingTokenID());
    for (auto object : allProxyObj.Items()) {
        if (!object.Contains(TAG_BUNDLE_NAME) || !IsString(object, TAG_BUNDLE_NAME)) {
            continue;
        }
        if (!object.Contains(TAG_TOKENID) || !IsInt64(object, TAG_TOKENID)) {
            continue;
        }
        std::string bundleName = object[TAG_BUNDLE_NAME].Get<std::string>();
        int64_t agentTokenId = object[TAG_TOKENID].Get<int64_t>();
        for (uint32_t i = 0; i < agentToProxyVec.size(); i++) {
            if (agentTokenId == agentToProxyVec[i].first && proxyTokenId == agentToProxyVec[i].second) {
                object[PARAM_KEY_IS_PROXY_UNBIND] = DM_VAL_TRUE;
                proxyInfos.insert(std::pair<std::string, std::string>(bundleName, object.Dump()));
                break;
            }
        }
    }
    return proxyInfos;
}
#endif

int32_t DeviceManagerService::UnBindDeviceParseExtra(const std::string &pkgName, const std::string &udidHash,
    const std::string &extra)
{
    int32_t result = ValidateUnBindDeviceParams(pkgName, udidHash, extra);
    if (result != DM_OK) {
        return result;
    }
    std::string realDeviceId = udidHash;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHashTemp = "";
    if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) {
        realDeviceId = udidHashTemp;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    std::string udid = "";
    if (softbusListener_->GetUdidFromDp(realDeviceId, udid) != DM_OK) {
        LOGE("Get udid by udidhash failed.");
        return ERR_DM_FAILED;
    }
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
    std::vector<std::pair<int64_t, int64_t>> agentToProxyVec =
        DeviceProfileConnector::GetInstance().GetAgentToProxyVecFromAclByUserId(
            std::string(localUdid), udid, userId);
    std::set<std::pair<std::string, std::string>> proxyInfoSet = GetProxyInfosByParseExtra(
        pkgName, extra, agentToProxyVec);
    if (proxyInfoSet.size() != UNBIND_PROXY_ITEM_SIZE) {
        LOGE("UnBind size error.");
        return ERR_DM_FAILED;
    }
    auto proxyInfo = proxyInfoSet.begin();
    if (proxyInfo->second == "") {
        result = UnBindDevice(proxyInfo->first, udidHash);
    } else {
        result = UnBindDevice(proxyInfo->first, udidHash, proxyInfo->second);
    }
#else
    if (extra == "") {
        result = UnBindDevice(pkgName, udidHash);
    } else {
        result = UnBindDevice(pkgName, udidHash, extra);
    }
#endif
    return result;
}

int32_t DeviceManagerService::UnBindDevice(const std::string &pkgName, const std::string &udidHash,
    const std::string &extra)
{
    int32_t result = ValidateUnBindDeviceParams(pkgName, udidHash, extra);
    if (result != DM_OK) {
        return result;
    }
    std::string realDeviceId = udidHash;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHashTemp = "";
    if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) {
        realDeviceId = udidHashTemp;
    }
#endif
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    std::string udid = "";
    if (softbusListener_->GetUdidFromDp(realDeviceId, udid) != DM_OK) {
        LOGE("Get udid by udidhash failed.");
        return ERR_DM_FAILED;
    }
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    uint64_t tokenId = 0;
    int32_t bindLevel = dmServiceImpl_->GetBindLevel(pkgName, std::string(localUdid), udid, tokenId);
    LOGI("UnAuthenticateDevice get bindlevel %{public}d.", bindLevel);
    if (bindLevel == INVALIED_BIND_LEVEL) {
        LOGE("UnAuthenticateDevice failed, Acl not contain the bindLevel %{public}d.", bindLevel);
        return ERR_DM_FAILED;
    }
    [[maybe_unused]] uint64_t peerTokenId = dmServiceImpl_->GetTokenIdByNameAndDeviceId(extra, udid);
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::vector<std::string> peerUdids;
    peerUdids.emplace_back(udid);
    int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
    std::map<std::string, std::string> wifiDevices;
    bool isBleActive = false;
    GetNotifyRemoteUnBindAppWay(userId, tokenId, wifiDevices, isBleActive);
    if (isBleActive) {
        SendUnBindBroadCast(peerUdids, MultipleUserConnector::GetCurrentAccountUserID(), tokenId,
            bindLevel, peerTokenId);
    } else {
        NotifyRemoteUnBindAppByWifi(userId, tokenId, extra, wifiDevices);
    }
#endif
    if (dmServiceImpl_->UnBindDevice(pkgName, udid, bindLevel, extra) != DM_OK) {
        LOGE("dmServiceImpl_ UnBindDevice failed.");
        return ERR_DM_FAILED;
    }
    return DM_OK;
}

int32_t DeviceManagerService::ValidateUnBindDeviceParams(const std::string &pkgName, const std::string &udidHash,
    const std::string &extra)
{
    LOGI("pkgName: %{public}s, udidHash: %{public}s, extra: %{public}s",
        GetAnonyString(pkgName).c_str(), GetAnonyString(udidHash).c_str(),
        GetAnonyString(extra).c_str());
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call UnBindDevice.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || udidHash.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName: %{public}s", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("UnBindDevice failed, instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (!AppManager::GetInstance().IsSystemSA()) {
        int32_t ret = ValidateUnBindTokenId(pkgName, udidHash);
        if (ret != DM_OK) {
            LOGE("ValidateUnBindTokenId failed, ret: %{public}d", ret);
            return ret;
        }
    }
#endif
    return DM_OK;
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
int32_t DeviceManagerService::ValidateUnBindTokenId(const std::string &pkgName, const std::string &udidHash)
{
    uint32_t callingTokenId = IPCSkeleton::GetCallingTokenID();
    if (callingTokenId == 0) {
        LOGE("GetCallingTokenID error.");
        return ERR_DM_GET_TOKENID_FAILED;
    }
    std::string realDeviceId = udidHash;
    std::string udidHashTemp = "";
    if (GetUdidHashByAnoyDeviceId(udidHash, udidHashTemp) == DM_OK) {
        realDeviceId = udidHashTemp;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    std::string peerUdid = "";
    if (softbusListener_->GetUdidFromDp(realDeviceId, peerUdid) != DM_OK) {
        LOGE("Get udid by udidhash failed.");
        return ERR_DM_FAILED;
    }
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    int32_t localUserId = MultipleUserConnector::GetCurrentAccountUserID();
    std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
        DeviceProfileConnector::GetInstance().GetAclProfileByDeviceIdAndUserId(std::string(localUdid),
            localUserId, peerUdid);
    if (profiles.empty()) {
        LOGE("No ACL found for localUdid-peerUdid pair.");
        return ERR_DM_NO_PERMISSION;
    }
    for (const auto &profile : profiles) {
        const auto &accesser = profile.GetAccesser();
        const auto &accessee = profile.GetAccessee();
        if (accesser.GetAccesserDeviceId() == std::string(localUdid)) {
            if (static_cast<uint64_t>(accesser.GetAccesserTokenId()) == callingTokenId) {
                LOGI("TokenId matched as accesser.");
                return DM_OK;
            }
        }
        if (accessee.GetAccesseeDeviceId() == std::string(localUdid)) {
            if (static_cast<uint64_t>(accessee.GetAccesseeTokenId()) == callingTokenId) {
                LOGI("TokenId matched as accessee.");
                return DM_OK;
            }
        }
    }
    LOGE("TokenId validation failed: no matching ACL.");
    return ERR_DM_NO_PERMISSION;
}
#endif

int32_t DeviceManagerService::SetUserOperation(std::string &pkgName, int32_t action, const std::string &params)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call SetUserOperation.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || params.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    JsonObject paramJson;
    paramJson.Parse(params);
    if (paramJson.IsDiscarded() || !IsInt32(paramJson, PARAM_KEY_META_TYPE)) {
        LOGE("meta type not found");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    int32_t metaType = paramJson[PARAM_KEY_META_TYPE].Get<int32_t>();
    int32_t ret = ERR_DM_FAILED;
    if (metaType != PROXY_DEFAULT) {
        if (IsDMServiceAdapterSoLoaded()) {
            LOGI("metaType: %{public}d", metaType);
            ret = dmServiceImplExtResident_->ReplyUiAction(pkgName, action, params);
        }
    } else {
        if (IsDMServiceImplReady()) {
            ret =dmServiceImpl_->SetUserOperation(pkgName, action, params);
        }
    }
    return ret;
}

static std::string GetLocalDeviceUdid()
{
    char localUdidTemp[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdidTemp, DEVICE_UUID_LENGTH);
    return std::string(localUdidTemp);
}

void DeviceManagerService::HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo, const bool isOnline)
{
    LOGI("start.");
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->HandleDeviceStatusChange(devState, devInfo, isOnline);
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (IsDMServiceAdapterResidentLoad()) {
        std::string peerUdid(devInfo.deviceId);
        int32_t state = static_cast<int32_t>(devState);
        dmServiceImplExtResident_->HandleServiceStatusChange(devState, peerUdid);
    }
#endif
}

int DeviceManagerService::OnSessionOpened(int sessionId, int result)
{
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->OnSessionOpened(sessionId, result);
}

void DeviceManagerService::OnSessionClosed(int sessionId)
{
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return;
    }
    dmServiceImpl_->OnSessionClosed(sessionId);
}

void DeviceManagerService::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
{
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return;
    }
    dmServiceImpl_->OnBytesReceived(sessionId, data, dataLen);
}

int DeviceManagerService::OnPinHolderSessionOpened(int sessionId, int result)
{
    return PinHolderSession::OnSessionOpened(sessionId, result);
}

void DeviceManagerService::OnPinHolderSessionClosed(int sessionId)
{
    CHECK_NULL_VOID(pinHolder_);
    pinHolder_->OnSessionClosed(sessionId);
}

void DeviceManagerService::OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen)
{
    PinHolderSession::OnBytesReceived(sessionId, data, dataLen);
}

int32_t DeviceManagerService::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call RequestCredential.");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->RequestCredential(reqJsonStr, returnJsonStr);
}

int32_t DeviceManagerService::MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call RequestCredential.");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("RequestCredential failed, instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->MineRequestCredential(pkgName, returnJsonStr);
}

int32_t DeviceManagerService::CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
    std::string &returnJsonStr)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call CheckCredential.",
            pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->CheckCredential(pkgName, reqJsonStr, returnJsonStr);
}

int32_t DeviceManagerService::ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
    std::string &returnJsonStr)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call ImportCredential.",
            pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->ImportCredential(pkgName, reqJsonStr, returnJsonStr);
}

int32_t DeviceManagerService::DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
    std::string &returnJsonStr)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call DeleteCredential.",
            pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->DeleteCredential(pkgName, reqJsonStr, returnJsonStr);
}

int32_t DeviceManagerService::RegisterUiStateCallback(const std::string &pkgName)
{
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s",
            pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!AppManager::GetInstance().IsSystemApp()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NOT_SYSTEM_APP;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call RegisterUiStateCallback.",
            GetAnonyString(pkgName).c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->RegisterUiStateCallback(pkgName);
}

int32_t DeviceManagerService::UnRegisterUiStateCallback(const std::string &pkgName)
{
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s",
            pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!AppManager::GetInstance().IsSystemApp()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NOT_SYSTEM_APP;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call UnRegisterUiStateCallback.",
            GetAnonyString(pkgName).c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->UnRegisterUiStateCallback(pkgName);
}

bool DeviceManagerService::IsDMServiceImplReady()
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(isImplLoadLock_);
#else
    std::lock_guard<std::mutex> lock(isImplLoadLock_);
#endif
    if (isImplsoLoaded_ && (dmServiceImpl_ != nullptr)) {
        return true;
    }
    LOGI("libdevicemanagerserviceimpl start load.");
    dmServiceImplSoHandle_ = dlopen(LIB_IMPL_NAME, RTLD_NOW | RTLD_NODELETE | RTLD_NOLOAD);
    if (dmServiceImplSoHandle_ == nullptr) {
        dmServiceImplSoHandle_ = dlopen(LIB_IMPL_NAME, RTLD_NOW | RTLD_NODELETE);
    }
    if (dmServiceImplSoHandle_ == nullptr) {
        LOGE("load libdevicemanagerserviceimpl so failed, errMsg: %{public}s.", dlerror());
        return false;
    }
    dlerror();
    auto func = (CreateDMServiceFuncPtr)dlsym(dmServiceImplSoHandle_, "CreateDMServiceObject");
    if (dlerror() != nullptr || func == nullptr) {
        dlclose(dmServiceImplSoHandle_);
        dmServiceImplSoHandle_ = nullptr;
        LOGE("Create object function is not exist.");
        return false;
    }

    dmServiceImpl_ = std::shared_ptr<IDeviceManagerServiceImpl>(func());
    if (listener_ == nullptr) {
        listener_ = std::make_shared<DeviceManagerServiceListener>();
    }
    if (dmServiceImpl_->Initialize(listener_) != DM_OK) {
        dmServiceImpl_ = nullptr;
        isImplsoLoaded_ = false;
        return false;
    }
    isImplsoLoaded_ = true;
    LOGI("Sussess.");
    return true;
}

bool DeviceManagerService::IsDMImplSoLoaded()
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(isImplLoadLock_);
#else
    std::lock_guard<std::mutex> lock(isImplLoadLock_);
#endif
    return isImplsoLoaded_;
}

bool DeviceManagerService::IsDMServiceAdapterSoLoaded()
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(isAdapterResidentLoadLock_);
#else
    std::lock_guard<std::mutex> lock(isAdapterResidentLoadLock_);
#endif
    if (!isAdapterResidentSoLoaded_ || (dmServiceImplExtResident_ == nullptr)) {
        return false;
    }
    return dmServiceImplExtResident_->IsDMServiceAdapterSoLoaded();
}

int32_t DeviceManagerService::DmHiDumper(const std::vector<std::string>& args, std::string &result)
{
    LOGI("HiDump GetTrustedDeviceList");
    std::vector<HidumperFlag> dumpflag;
    HiDumpHelper::GetInstance().GetArgsType(args, dumpflag);

    for (unsigned int i = 0; i < dumpflag.size(); i++) {
        if (dumpflag[i] == HidumperFlag::HIDUMPER_GET_TRUSTED_LIST) {
            std::vector<DmDeviceInfo> deviceList;
            CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
            int32_t ret = softbusListener_->GetTrustedDeviceList(deviceList);
            if (ret != DM_OK) {
                result.append("HiDumpHelper GetTrustedDeviceList failed");
                LOGE("HiDumpHelper GetTrustedDeviceList failed");
                return ret;
            }

            for (unsigned int j = 0; j < deviceList.size(); j++) {
                HiDumpHelper::GetInstance().SetNodeInfo(deviceList[j]);
                LOGI("SetNodeInfo.");
            }
        }
    }
    HiDumpHelper::GetInstance().HiDump(args, result);
    return DM_OK;
}

int32_t DeviceManagerService::NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call NotifyEvent.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    if (eventId == DM_NOTIFY_EVENT_ON_PINHOLDER_EVENT) {
        LOGI("on pin holder event start.");
        CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
        return pinHolder_->NotifyPinHolderEvent(pkgName, event);
    }
    return dmServiceImpl_->NotifyEvent(pkgName, eventId, event);
}

int32_t DeviceManagerService::GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId,
    std::string &uuid)
{
    CHECK_EMPTY_RETURN(networkId, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(networkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetUuidByNetworkId(networkId.c_str(), uuid);
    if (ret != DM_OK) {
        LOGE("GetUuidByNetworkId failed, ret : %{public}d", ret);
        return ret;
    }

    std::string appId = Crypto::Sha256(AppManager::GetInstance().GetAppId());
    uuid = Crypto::Sha256(appId + "_" + uuid);
    LOGI("appId = %{public}s, uuid = %{public}s, encryptedUuid = %{public}s.", GetAnonyString(appId).c_str(),
        GetAnonyString(uuid).c_str(), GetAnonyString(uuid).c_str());
    return DM_OK;
}

int32_t DeviceManagerService::GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid,
    const std::string &appId, std::string &encryptedUuid)
{
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    encryptedUuid = Crypto::Sha256(appId + "_" + uuid);
    LOGI("encryptedUuid = %{public}s.", GetAnonyString(encryptedUuid).c_str());
    return DM_OK;
}

int32_t DeviceManagerService::CheckApiPermission(int32_t permissionLevel)
{
    LOGI("PermissionLevel: %{public}d", permissionLevel);
    int32_t ret = ERR_DM_NO_PERMISSION;
    switch (permissionLevel) {
        case NORMAL:
            if (PermissionManager::GetInstance().CheckDataSyncPermission()) {
                LOGI("The caller have permission to call");
                ret = DM_OK;
            }
            break;
        case SYSTEM_BASIC:
            if (PermissionManager::GetInstance().CheckAccessServicePermission()) {
                LOGI("The caller have permission to call");
                ret = DM_OK;
            }
            break;
        case SYSTEM_CORE:
            if (PermissionManager::GetInstance().CheckMonitorPermission()) {
                LOGI("The caller have permission to call");
                ret = DM_OK;
            }
            break;
        default:
            LOGE("DM have not this permissionLevel.");
            break;
    }
    return ret;
}

int32_t DeviceManagerService::GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId,
                                                        int32_t &networkType)
{
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    CHECK_EMPTY_RETURN(netWorkId, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(netWorkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetNetworkTypeByNetworkId.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    return softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
}

int32_t DeviceManagerService::GetOsTypeByNetworkId(const std::string &pkgName, const std::string &networkId,
    int32_t &osType)
{
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    CHECK_EMPTY_RETURN(networkId, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(networkId)) {
        LOGI("constraint enable is true");
        osType = -1;
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission() ||
        !PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetOsTypeByNetworkId.", pkgName.c_str());
        osType = -1;
        return ERR_DM_NO_PERMISSION;
    }
    if (!AppManager::GetInstance().IsSystemApp()) {
        LOGE("The caller: %{public}s is not system app.", pkgName.c_str());
        osType = -1;
        return ERR_DM_NOT_SYSTEM_APP;
    }
    DmDeviceInfo devInfo;
    int32_t ret = SoftbusCache::GetInstance().GetDevInfoByNetworkId(networkId, devInfo);
    if (ret != DM_OK) {
        LOGE("GetDevInfoByNetworkId failed, ret: %{public}d", ret);
        osType = -1;
        return ERR_DM_GET_OSTYPE_FAILED;
    }
    JsonObject extraJson(devInfo.extraData);
    if (extraJson.IsDiscarded() || !IsInt32(extraJson, PARAM_KEY_OS_TYPE)) {
        LOGE("Parse osType from extraData failed.");
        osType = -1;
        return ERR_DM_GET_OSTYPE_FAILED;
    }
    osType = extraJson[PARAM_KEY_OS_TYPE].Get<int32_t>();
    return DM_OK;
}

int32_t DeviceManagerService::ImportAuthCode(const std::string &pkgName, const std::string &authCode)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (authCode.empty() || pkgName.empty()) {
        LOGE("Invalid parameter, authCode: %{public}s.", GetAnonyString(authCode).c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->ImportAuthCode(pkgName, authCode);
}

int32_t DeviceManagerService::ExportAuthCode(std::string &authCode)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call ExportAuthCode.");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, processName: %{public}s.", processName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->ExportAuthCode(authCode);
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
bool DeviceManagerService::IsExportAuthInfoValid(const DmAuthInfo &dmAuthInfo)
{
    std::vector<int32_t> UserIds;
    int32_t ret = MultipleUserConnector::GetForegroundUserIds(UserIds);
    if (ret != DM_OK) {
        LOGE("Get foreground userids failed, ret: %{public}d", ret);
        return false;
    }
    if (std::find(UserIds.begin(), UserIds.end(), dmAuthInfo.userId) == UserIds.end()) {
        LOGE("userId %{public}d not found in foreground list", dmAuthInfo.userId);
        return false;
    }
    if (dmAuthInfo.pinConsumerPkgName.empty() ||
        dmAuthInfo.pinConsumerPkgName.length() > DM_PARAM_STRING_LENGTH_MAX) {
        LOGE("Invalid pkgName.");
        return false;
    }
    if (dmAuthInfo.bizSrcPkgName.length() > DM_PARAM_STRING_LENGTH_MAX ||
        dmAuthInfo.bizSinkPkgName.length() > DM_PARAM_STRING_LENGTH_MAX) {
        LOGE("bizSrcPkgName or bizSinkPkgName invalid.");
        return false;
    }
    return true;
}

bool DeviceManagerService::IsImportAuthInfoValid(const DmAuthInfo &dmAuthInfo)
{
    if (!IsExportAuthInfoValid(dmAuthInfo)) {
        return false;
    }
    if (std::string(dmAuthInfo.pinCode).length() < DM_MIN_PINCODE_SIZE ||
        std::string(dmAuthInfo.pinCode).length() > DM_MAX_PINCODE_SIZE) {
        LOGE("Invalid pinCode, len: %{public}zu.", std::string(dmAuthInfo.pinCode).length());
        return false;
    }
    if (std::string(dmAuthInfo.metaToken).length() > DM_PARAM_STRING_LENGTH_MAX) {
        LOGE("Invalid metaToken.");
        return false;
    }
    if (dmAuthInfo.authType != DMLocalServiceInfoAuthType::TRUST_ONETIME &&
        dmAuthInfo.authType != DMLocalServiceInfoAuthType::TRUST_ALWAYS) {
        LOGE("Invalid authType.");
        return false;
    }
    if (dmAuthInfo.authBoxType < DMLocalServiceInfoAuthBoxType::STATE3 ||
        dmAuthInfo.authBoxType > DMLocalServiceInfoAuthBoxType::TWO_IN1) {
        LOGE("Invalid authBoxType.");
        return false;
    }
    if (dmAuthInfo.pinExchangeType < DMLocalServiceInfoPinExchangeType::PINBOX ||
        dmAuthInfo.pinExchangeType > DMLocalServiceInfoPinExchangeType::ULTRASOUND) {
        LOGE("Invalid pinExchangeType.");
        return false;
    }
    if (dmAuthInfo.description.length() > DM_PARAM_STRING_LENGTH_MAX) {
        LOGE("Invalid description.");
        return false;
    }
    if (!IsAclLifeCycleDaysValid(dmAuthInfo)) {
        LOGE("Invalid ACL_LIFE_CYCLE_DAYS.");
        return false;
    }
    return true;
}

bool DeviceManagerService::IsAclLifeCycleDaysValid(const DmAuthInfo &dmAuthInfo)
{
    if (dmAuthInfo.extraInfo.empty()) {
        return true;
    }
    JsonObject extraObj(dmAuthInfo.extraInfo);
    if (extraObj.IsDiscarded()) {
        return false;
    }
    if (!extraObj.Contains(ACL_LIFE_CYCLE_DAYS)) {
        return true;
    }
    if (!extraObj[ACL_LIFE_CYCLE_DAYS].IsNumberInteger()) {
        return false;
    }
    int32_t days = extraObj[ACL_LIFE_CYCLE_DAYS].Get<int32_t>();
    if (days == ACL_LIFE_CYCLE_DAYS_NOT_CONFIGURED) {
        return true;
    }
    if (dmAuthInfo.authType != DMLocalServiceInfoAuthType::TRUST_ALWAYS) {
        return false;
    }
    return days >= ACL_LIFE_CYCLE_DAYS_MIN && days <= ACL_LIFE_CYCLE_DAYS_MAX;
}

int32_t DeviceManagerService::ImportAuthInfo(const DmAuthInfo &dmAuthInfo)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.",
            dmAuthInfo.pinConsumerPkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", dmAuthInfo.pinConsumerPkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
        LOGE("The caller: %{public}s is not in white list.", GetAnonyString(processName).c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsImportAuthInfoValid(dmAuthInfo)) {
        LOGE("Invalid para");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("ImportAuthCode failed, instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    int32_t ret = dmServiceImpl_->ImportAuthInfo(dmAuthInfo);
    if (ret != DM_OK) {
        LOGE("failed: %{public}d.", ret);
        return ret;
    }
#ifndef DEVICE_MANAGER_COMMON_FLAG
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->ImportAuthInfo(dmAuthInfo);
#else
    return DM_OK;
#endif
}

int32_t DeviceManagerService::ExportAuthInfo(DmAuthInfo &dmAuthInfo, uint32_t pinLength)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call ExportAuthCode.");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, processName: %{public}s.", processName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnAuthCode(processName)) {
        LOGE("The caller: %{public}s is not in white list.", GetAnonyString(processName).c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsExportAuthInfoValid(dmAuthInfo)) {
        LOGE("Invalid para");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (pinLength < DM_MIN_PINCODE_SIZE || pinLength > DM_MAX_PINCODE_SIZE) {
        LOGE("pinLength error: Invalid para");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    int32_t ret = dmServiceImpl_->ExportAuthInfo(dmAuthInfo, pinLength);
    if (ret != DM_OK) {
        LOGE("failed: %{public}d.", ret);
        return ret;
    }
#ifndef DEVICE_MANAGER_COMMON_FLAG
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->ExportAuthInfo(dmAuthInfo);
#else
    return DM_OK;
#endif
}
#endif

void DeviceManagerService::UnloadDMServiceImplSo()
{
    LOGI("Start.");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(isImplLoadLock_);
#else
    std::lock_guard<std::mutex> lock(isImplLoadLock_);
#endif
    if (dmServiceImpl_ != nullptr) {
        dmServiceImpl_->Release();
    }
    if (dmServiceImplSoHandle_ != nullptr) {
        LOGI("DeviceManagerService so_handle is not nullptr.");
        dlclose(dmServiceImplSoHandle_);
        dmServiceImplSoHandle_ = nullptr;
    }
}

bool DeviceManagerService::IsDMServiceAdapterResidentLoad()
{
    if (listener_ == nullptr) {
        listener_ = std::make_shared<DeviceManagerServiceListener>();
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(isAdapterResidentLoadLock_);
#else
    std::lock_guard<std::mutex> lock(isAdapterResidentLoadLock_);
#endif
    if (isAdapterResidentSoLoaded_ && (dmServiceImplExtResident_ != nullptr)) {
        return true;
    }
    residentSoHandle_ = dlopen(LIB_DM_RESIDENT_NAME, RTLD_NOW | RTLD_NODELETE | RTLD_NOLOAD);
    if (residentSoHandle_ == nullptr) {
        residentSoHandle_ = dlopen(LIB_DM_RESIDENT_NAME, RTLD_NOW | RTLD_NODELETE);
    }
    if (residentSoHandle_ == nullptr) {
        LOGE("load dm service resident so failed.");
        return false;
    }
    dlerror();
    auto func = (CreateDMServiceExtResidentFuncPtr)dlsym(residentSoHandle_, "CreateDMServiceExtResidentObject");
    if (dlerror() != nullptr || func == nullptr) {
        dlclose(residentSoHandle_);
        residentSoHandle_ = nullptr;
        LOGE("Create object function is not exist.");
        return false;
    }

    dmServiceImplExtResident_ = std::shared_ptr<IDMServiceImplExtResident>(func());
    if (dmServiceImplExtResident_->Initialize(listener_) != DM_OK) {
        dlclose(residentSoHandle_);
        residentSoHandle_ = nullptr;
        dmServiceImplExtResident_ = nullptr;
        isAdapterResidentSoLoaded_ = false;
        LOGE("dm service impl ext resident init failed.");
        return false;
    }
    isAdapterResidentSoLoaded_ = true;
    LOGI("Success.");
    return true;
}

void DeviceManagerService::UnloadDMServiceAdapterResident()
{
    LOGI("Start.");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::lock_guard<ffrt::mutex> lock(isAdapterResidentLoadLock_);
#else
    std::lock_guard<std::mutex> lock(isAdapterResidentLoadLock_);
#endif
    if (dmServiceImplExtResident_ != nullptr) {
        dmServiceImplExtResident_->Release();
    }
    dmServiceImplExtResident_ = nullptr;
    if (residentSoHandle_ != nullptr) {
        LOGI("dm service resident residentSoHandle_ is not nullptr.");
        dlclose(residentSoHandle_);
        residentSoHandle_ = nullptr;
    }
}

std::pair<bool, IDMDeviceRiskDetect*> DeviceManagerService::LoadDMDeviceRiskDetect()
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    LOGI("Start.");
    std::lock_guard<ffrt::mutex> lock(deviceRiskDetectSoLoadLock_);
    auto& libManager = GetLibraryManager();
    auto createRiskDetectObj = libManager.GetFunction<IDMDeviceRiskDetect*(*)()>(LIB_DM_DEVICE_RISK_DETECT_NAME,
        "CreateDMDeviceRiskDetectObject");
    if (createRiskDetectObj == nullptr) {
        LOGE("load dm device risk detect so failed");
        return {false, nullptr};
    }

    IDMDeviceRiskDetect* riskDetectPtr = createRiskDetectObj();
    if (riskDetectPtr == nullptr) {
        LOGE("get risk detect ptr failed");
        libManager.Release(LIB_DM_DEVICE_RISK_DETECT_NAME);
        return {false, nullptr};
    }

    if (riskDetectPtr->Initialize() != DM_OK) {
        libManager.Release(LIB_DM_DEVICE_RISK_DETECT_NAME);
        LOGE("dm sdevice risk detect init failed.");
        return {false, nullptr};
    }
    LOGI("Success.");
    return {true, riskDetectPtr};
#else
    return {false, nullptr};
#endif
}

void DeviceManagerService::UnloadDMDeviceRiskDetect(IDMDeviceRiskDetect* &riskDetectPtr)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    LOGI("Start.");
    std::lock_guard<ffrt::mutex> lock(deviceRiskDetectSoLoadLock_);
    if (riskDetectPtr == nullptr) {
        LOGE("The risk detect ptr is null");
        return;
    }
    riskDetectPtr->Release();
    riskDetectPtr = nullptr;
    auto& libManager = GetLibraryManager();
    libManager.Release(LIB_DM_DEVICE_RISK_DETECT_NAME);
    LOGI("Success.");
#endif
}

int32_t DeviceManagerService::StartDiscovering(const std::string &pkgName,
    const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission() &&
        !PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) {
        LOGI("input MetaType = %{public}s", (discoverParam.find(PARAM_KEY_META_TYPE)->second).c_str());
    }
    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
    return discoveryMgr_->StartDiscovering(pkgName, discoverParam, filterOptions);
}

int32_t DeviceManagerService::StopDiscovering(const std::string &pkgName,
    const std::map<std::string, std::string> &discoverParam)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission() &&
        !PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    uint16_t subscribeId = -1;
    if (discoverParam.find(PARAM_KEY_SUBSCRIBE_ID) != discoverParam.end()) {
        subscribeId = std::atoi((discoverParam.find(PARAM_KEY_SUBSCRIBE_ID)->second).c_str());
    }
    if (discoverParam.find(PARAM_KEY_META_TYPE) != discoverParam.end()) {
        LOGI("input MetaType = %{public}s", (discoverParam.find(PARAM_KEY_META_TYPE)->second).c_str());
    }
    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
    return discoveryMgr_->StopDiscovering(pkgName, subscribeId);
}

int32_t DeviceManagerService::EnableDiscoveryListener(const std::string &pkgName,
    const std::map<std::string, std::string> &discoverParam, const std::map<std::string, std::string> &filterOptions)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    SoftbusListener::SetHostPkgName(pkgName);
    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
    return discoveryMgr_->EnableDiscoveryListener(pkgName, discoverParam, filterOptions);
}

int32_t DeviceManagerService::DisableDiscoveryListener(const std::string &pkgName,
    const std::map<std::string, std::string> &extraParam)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(discoveryMgr_, ERR_DM_POINT_NULL);
    return discoveryMgr_->DisableDiscoveryListener(pkgName, extraParam);
}

int32_t DeviceManagerService::StartAdvertising(const std::string &pkgName,
    const std::map<std::string, std::string> &advertiseParam)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
    return advertiseMgr_->StartAdvertising(pkgName, advertiseParam);
}

int32_t DeviceManagerService::StopAdvertising(const std::string &pkgName,
    const std::map<std::string, std::string> &advertiseParam)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (advertiseParam.find(PARAM_KEY_META_TYPE) != advertiseParam.end()) {
        LOGI("input MetaType=%{public}s", (advertiseParam.find(PARAM_KEY_META_TYPE)->second).c_str());
    }
    int32_t publishId = -1;
    if (advertiseParam.find(PARAM_KEY_PUBLISH_ID) != advertiseParam.end()) {
        publishId = std::atoi((advertiseParam.find(PARAM_KEY_PUBLISH_ID)->second).c_str());
    }
    CHECK_NULL_RETURN(advertiseMgr_, ERR_DM_POINT_NULL);
    return advertiseMgr_->StopAdvertising(pkgName, publishId);
}

int32_t DeviceManagerService::BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
    const std::map<std::string, std::string> &bindParam)
{
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return ERR_DM_CONSTRAINT_ENABLE;
    }
#endif
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (bindParam.find(PARAM_KEY_META_TYPE) == bindParam.end()) {
        if (!IsDMServiceImplReady()) {
            LOGE("DMServiceImpl instance not init or init failed.");
            return ERR_DM_NOT_INIT;
        }
        LOGI("stardard begin.");
        if (targetId.wifiIp.empty() || targetId.wifiIp.length() > IP_STR_MAX_LEN) {
            return dmServiceImpl_->BindTarget(pkgName, targetId, bindParam);
        }
        ConnectionAddrType ipAddrType;
        std::map<std::string, std::string> &noConstBindParam =
            const_cast<std::map<std::string, std::string> &>(bindParam);
        if (SoftbusListener::GetIPAddrTypeFromCache(targetId.deviceId, targetId.wifiIp, ipAddrType) == DM_OK) {
            noConstBindParam.insert(std::pair<std::string, std::string>(PARAM_KEY_CONN_ADDR_TYPE,
                std::to_string(ipAddrType)));
        }
        const std::map<std::string, std::string> &constBindParam =
            const_cast<const std::map<std::string, std::string> &>(noConstBindParam);
        return dmServiceImpl_->BindTarget(pkgName, targetId, constBindParam);
    }
    if (!AppManager::GetInstance().IsSystemSA() && !AppManager::GetInstance().IsSystemApp()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    LOGI("unstardard begin.");
    return dmServiceImplExtResident_->BindTargetExt(pkgName, targetId, bindParam);
}

int32_t DeviceManagerService::UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
    const std::map<std::string, std::string> &unbindParam)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    if (unbindParam.find(PARAM_KEY_META_TYPE) == unbindParam.end()) {
        LOGE("input unbind parameter not contains META_TYPE, dm service adapter not supported.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!AppManager::GetInstance().IsSystemSA() && !AppManager::GetInstance().IsSystemApp()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string realDeviceId = targetId.deviceId;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string udidHashTemp = "";
    if (GetUdidHashByAnoyDeviceId(realDeviceId, udidHashTemp) == DM_OK) {
        realDeviceId = udidHashTemp;
    }
#endif
    std::map<std::string, std::string> unbindParamWithUdid(unbindParam);
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    std::string udid = "";
    if (softbusListener_->GetUdidFromDp(realDeviceId, udid) == DM_OK) {
        unbindParamWithUdid.insert(std::pair<std::string, std::string>(UN_BIND_PARAM_UDID_KEY, udid));
    }
    return dmServiceImplExtResident_->UnbindTargetExt(pkgName, targetId, unbindParamWithUdid);
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
bool DeviceManagerService::InitDPLocalServiceInfo(const DMLocalServiceInfo &serviceInfo,
    DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceInfo)
{
    dpLocalServiceInfo.SetBundleName(serviceInfo.bundleName);
    dpLocalServiceInfo.SetAuthBoxType(serviceInfo.authBoxType);
    dpLocalServiceInfo.SetAuthType(serviceInfo.authType);
    dpLocalServiceInfo.SetPinExchangeType(serviceInfo.pinExchangeType);
    dpLocalServiceInfo.SetPinCode(serviceInfo.pinCode);
    dpLocalServiceInfo.SetDescription(serviceInfo.description);
    dpLocalServiceInfo.SetExtraInfo(serviceInfo.extraInfo);
    return true;
}

void DeviceManagerService::InitServiceInfo(const DistributedDeviceProfile::LocalServiceInfo &dpLocalServiceInfo,
    DMLocalServiceInfo &serviceInfo)
{
    serviceInfo.bundleName = dpLocalServiceInfo.GetBundleName();
    serviceInfo.authBoxType = dpLocalServiceInfo.GetAuthBoxType();
    serviceInfo.authType = dpLocalServiceInfo.GetAuthType();
    serviceInfo.pinExchangeType = dpLocalServiceInfo.GetPinExchangeType();
    serviceInfo.pinCode = dpLocalServiceInfo.GetPinCode();
    serviceInfo.description = dpLocalServiceInfo.GetDescription();
    serviceInfo.extraInfo = dpLocalServiceInfo.GetExtraInfo();
}

void DeviceManagerService::InitServiceInfos(
    const std::vector<DistributedDeviceProfile::LocalServiceInfo> &dpLocalServiceInfos,
    std::vector<DMLocalServiceInfo> &serviceInfos)
{
    for (const auto &dpInfoItem : dpLocalServiceInfos) {
        DMLocalServiceInfo infoItem;
        InitServiceInfo(dpInfoItem, infoItem);
        serviceInfos.emplace_back(infoItem);
    }
}
#endif

int32_t DeviceManagerService::RegisterLocalServiceInfo(const DMLocalServiceInfo &serviceInfo)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call RegisterLocalServiceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    DistributedDeviceProfile::LocalServiceInfo dpLocalServiceInfo;
    bool success = InitDPLocalServiceInfo(serviceInfo, dpLocalServiceInfo);
    if (!success) {
        LOGE("InitDPLocalServiceInfo failed");
        return ERR_DM_FAILED;
    }
    return DeviceProfileConnector::GetInstance().PutLocalServiceInfo(dpLocalServiceInfo);
#else
    (void)serviceInfo;
    return ERR_DM_FAILED;
#endif
}

int32_t DeviceManagerService::UnRegisterLocalServiceInfo(const std::string &bundleName, int32_t pinExchangeType)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call UnRegisterLocalServiceInfo.",
            bundleName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    return DeviceProfileConnector::GetInstance().DeleteLocalServiceInfo(bundleName, pinExchangeType);
#else
    (void)bundleName;
    (void)pinExchangeType;
    return ERR_DM_FAILED;
#endif
}

int32_t DeviceManagerService::UpdateLocalServiceInfo(const DMLocalServiceInfo &serviceInfo)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call UpdateLocalServiceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    DistributedDeviceProfile::LocalServiceInfo dpLocalServiceInfo;
    bool success = InitDPLocalServiceInfo(serviceInfo, dpLocalServiceInfo);
    if (!success) {
        LOGE("InitDPLocalServiceInfo failed");
        return ERR_DM_FAILED;
    }
    return DeviceProfileConnector::GetInstance().UpdateLocalServiceInfo(dpLocalServiceInfo);
#else
    (void)serviceInfo;
    return ERR_DM_FAILED;
#endif
}

int32_t DeviceManagerService::GetLocalServiceInfoByBundleNameAndPinExchangeType(const std::string &bundleName,
    int32_t pinExchangeType, DMLocalServiceInfo &serviceInfo)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetLocalServiceInfo.",
            bundleName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    DistributedDeviceProfile::LocalServiceInfo dpLocalServiceInfo;
    int32_t ret = DeviceProfileConnector::GetInstance().GetLocalServiceInfoByBundleNameAndPinExchangeType(bundleName,
        pinExchangeType, dpLocalServiceInfo);
    if (ret == DM_OK) {
        InitServiceInfo(dpLocalServiceInfo, serviceInfo);
    }
    return ret;
#else
    (void)bundleName;
    (void)pinExchangeType;
    (void)serviceInfo;
    return ERR_DM_FAILED;
#endif
}

int32_t DeviceManagerService::RegisterPinHolderCallback(const std::string &pkgName)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call ImportAuthCode.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    LOGI("begin.");
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
    return pinHolder_->RegisterPinHolderCallback(pkgName);
}

int32_t DeviceManagerService::CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
    DmPinType pinType, const std::string &payload)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call CreatePinHolder.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
    return pinHolder_->CreatePinHolder(pkgName, targetId, pinType, payload);
}

int32_t DeviceManagerService::DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
    DmPinType pinType, const std::string &payload)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call DestroyPinHolder.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
    return pinHolder_->DestroyPinHolder(pkgName, targetId, pinType, payload);
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
int32_t DeviceManagerService::DpAclAdd(const std::string &udid, int64_t accessControlId)
{
    if (!PermissionManager::GetInstance().CheckDataSyncPermission()) {
        LOGE("The caller does not have permission to call DpAclAdd.");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start.");
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    dmServiceImpl_->DpAclAdd(udid, accessControlId);
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("SetDnPolicy failed, instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    // get peerUserId
    std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
        DeviceProfileConnector::GetInstance().GetAllAccessControlProfile();
    // filter the data by accessControlId
    for (const DistributedDeviceProfile::AccessControlProfile &profile : profiles) {
        if (profile.GetAccessControlId() == accessControlId) {
            DistributedDeviceProfile::Accessee accessee = profile.GetAccessee();
            int32_t peerUserId = accessee.GetAccesseeUserId();
            ServiceStateBindParameter bindParam = {
                DEFAULT_TOKEN_ID,
                DEFAULT_PKG_NAME,
                static_cast<int32_t>(DistributedDeviceProfile::BindType::SAME_ACCOUNT),
                udid,
                peerUserId,
                DEFAULT_SERVICE_ID
            };
            dmServiceImplExtResident_->BindServiceOnline(bindParam);
            break;
        }
    }
    LOGI("completed");
    return DM_OK;
}
#endif

int32_t DeviceManagerService::GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId,
                                                     int32_t &securityLevel)
{
    LOGI("Begin pkgName: %{public}s, networkId: %{public}s",
        pkgName.c_str(), GetAnonyString(networkId).c_str());
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    CHECK_EMPTY_RETURN(networkId, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(networkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetDeviceSecurityLevel.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || networkId.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(),
            GetAnonyString(networkId).c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetDeviceSecurityLevel(networkId.c_str(), securityLevel);
    if (ret != DM_OK) {
        LOGE("ret = %{public}d", ret);
        return ret;
    }
    return DM_OK;
}

int32_t DeviceManagerService::IsSameAccount(const std::string &networkId)
{
    LOGI("NetworkId %{public}s.", GetAnonyString(networkId).c_str());
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        return ERR_DM_NO_PERMISSION;
    }
    std::string udid = "";
    if (SoftbusListener::GetUdidByNetworkId(networkId.c_str(), udid) != DM_OK) {
        LOGE("udid: %{public}s", GetAnonyString(udid).c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->IsSameAccount(udid);
}

bool DeviceManagerService::CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission.");
        return false;
    }
    std::string srcUdid = "";
    std::string sinkUdid = "";
    if (!GetAccessUdidByNetworkId(caller.networkId.c_str(), srcUdid, callee.networkId.c_str(), sinkUdid)) {
        LOGE("GetAccessUdidByNetworkId failed.");
        return false;
    }
    return dmServiceImpl_->CheckAccessControl(caller, srcUdid, callee, sinkUdid);
}

bool DeviceManagerService::CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission.");
        return false;
    }
    std::string srcUdid = "";
    std::string sinkUdid = "";
    if (!GetAccessUdidByNetworkId(caller.networkId.c_str(), srcUdid, callee.networkId.c_str(), sinkUdid)) {
        LOGE("GetAccessUdidByNetworkId failed.");
        return false;
    }
    return dmServiceImpl_->CheckIsSameAccount(caller, srcUdid, callee, sinkUdid);
}

int32_t DeviceManagerService::InitAccountInfo()
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    SubscribeAccountCommonEvent();
    LOGI("Success.");
    DmConstrainsManager::GetInstance().SubscribeOsAccountConstraints({DM_ACCOUNT_CONSTRAINT});
#endif
    return DM_OK;
}

int32_t DeviceManagerService::InitScreenLockEvent()
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    SubscribeScreenLockEvent();
    LOGI("Success.");
#endif
    return DM_OK;
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
void DeviceManagerService::SubscribeAccountCommonEvent()
{
    LOGI("Start");
    if (accountCommonEventManager_ == nullptr) {
        accountCommonEventManager_ = std::make_shared<DmAccountCommonEventManager>();
    }
    AccountEventCallback callback = [=](const DmAccountEventInfo& eventInfo) {
        this->AccountCommonEventCallback(eventInfo);
    };
    std::vector<std::string> AccountCommonEventVec;
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGIN);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_STOPPED);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_UNLOCKED);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_FOREGROUND);
    AccountCommonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_USER_BACKGROUND);
    if (accountCommonEventManager_->SubscribeAccountCommonEvent(AccountCommonEventVec, callback)) {
        LOGI("Success");
    }
    return;
}

void DeviceManagerService::SendShareTypeUnBindBroadCast(const char *credId, const int32_t localUserId,
    const std::vector<std::string> &peerUdids)
{
    LOGI("Start.");
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::SHARE_UNBIND;
    msg.userId = static_cast<uint32_t>(localUserId);
    msg.credId = credId;
    msg.peerUdids = peerUdids;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::SubscribeScreenLockEvent()
{
    LOGI("Start");
    if (screenCommonEventManager_ == nullptr) {
        screenCommonEventManager_ = std::make_shared<DmScreenCommonEventManager>();
    }
    ScreenEventCallback callback = [=](const auto &arg1) { this->ScreenCommonEventCallback(arg1); };
    std::vector<std::string> screenEventVec;
    screenEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
    screenEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
    if (screenCommonEventManager_->SubscribeScreenCommonEvent(screenEventVec, callback)) {
        LOGI("Success");
    }
    return;
}

void DeviceManagerService::DeleteInvalidSkIdAcl()
{
    std::map<std::string, DmOfflineParam> invalidAclMap;
    DeviceProfileConnector::GetInstance().GetInvalidSkIdAcl(invalidAclMap);
    for (const auto &iter : invalidAclMap) {
        if (!IsDMServiceImplReady()) {
            LOGE("instance not init or init failed.");
            return;
        }
        dmServiceImpl_->DeleteSkCredAndAcl(iter.second.needDelAclInfos);
        if (iter.second.allLeftAppOrSvrAclInfos.empty() && iter.second.allUserAclInfos.empty()) {
            LOGI("No acl exist, clear lnn acl");
            dmServiceImpl_->DeleteSkCredAndAcl(iter.second.allLnnAclInfos);
        }
        DmOfflineParam dmParam = iter.second;
        dmServiceImpl_->NotifyDeviceOrAppOffline(dmParam, iter.first);
    }
}

DM_EXPORT void DeviceManagerService::AccountCommonEventCallback(const DmAccountEventInfo& eventInfo)
{
    MultipleUserConnector::UpdateForgroundUserId();
    const std::string& commonEventType = eventInfo.eventName;
    int32_t currentUserId = eventInfo.userId;
    int32_t beforeUserId = eventInfo.beforeUserId;

    HandleUserCommonEvent(commonEventType, currentUserId, beforeUserId);
    HandleAccountEvent(eventInfo);
}

void DeviceManagerService::HandleUserCommonEvent(const std::string& commonEventType,
    int32_t currentUserId, int32_t beforeUserId)
{
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    std::string localUdidStr(localUdid);
    if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
        HandleUserSwitchEventCallback(commonEventType, currentUserId, beforeUserId);
    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_REMOVED) {
        ffrt::submit([=]() {
            HandleUserRemoved(beforeUserId);
        },
            ffrt::task_attr().name(HANDLE_USER_REMOVED_TASK));
        MultipleUserConnector::DeleteAccountInfoByUserId(beforeUserId);
        DMAccountInfo currentDmAccountInfo = MultipleUserConnector::GetCurrentDMAccountInfo();
        MultipleUserConnector::SetAccountInfo(MultipleUserConnector::GetCurrentAccountUserID(),
            currentDmAccountInfo.subProfileId, currentDmAccountInfo);
    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED) {
        DeviceNameManager::GetInstance().InitDeviceNameWhenNickChange();
    } else if ((commonEventType == CommonEventSupport::COMMON_EVENT_USER_STOPPED && IsPC()) ||
        commonEventType == CommonEventSupport::COMMON_EVENT_USER_FOREGROUND ||
        commonEventType == CommonEventSupport::COMMON_EVENT_USER_BACKGROUND) {
        CHECK_NULL_VOID(DMCommTool::GetInstance());
        DMCommTool::GetInstance()->StartCommonEvent(commonEventType,
            [this, commonEventType]() { DeviceManagerService::HandleAccountCommonEvent(commonEventType); });
    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED && IsPC()) {
        DeviceNameManager::GetInstance().AccountSysReady(beforeUserId);
        CHECK_NULL_VOID(DMCommTool::GetInstance());
        DMCommTool::GetInstance()->StartCommonEvent(commonEventType,
            [this, commonEventType]() { DeviceManagerService::HandleAccountCommonEvent(commonEventType); });
    }
}

void DeviceManagerService::HandleAccountEvent(const DmAccountEventInfo& eventInfo)
{
    char localUdid[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localUdid, DEVICE_UUID_LENGTH);
    std::string localUdidStr(localUdid);
    
    if (eventInfo.eventName == CommonEventSupport::COMMON_EVENT_OS_ACCOUNT_SUB_PROFILE_DELETED) {
        HandleSubProfileDeletedEvent(localUdidStr, eventInfo);
    } else if (eventInfo.eventName == CommonEventSupport::COMMON_EVENT_OS_ACCOUNT_SUB_PROFILE_SWITCHED) {
        HandleSubProfileSwitchedEvent(localUdidStr, eventInfo);
    } else if (eventInfo.eventName == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_BOUND) {
        HandleDistributedAccountBoundEvent(eventInfo);
    } else if (eventInfo.eventName == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGIN) {
        HandleDistributedAccountLoginEvent(localUdidStr, eventInfo);
    } else if (eventInfo.eventName == CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT) {
        HandleDistributedAccountLogoutEvent(localUdidStr, eventInfo);
    }
}

void DeviceManagerService::HandleSubProfileDeletedEvent(const std::string& localUdid,
    const DmAccountEventInfo& eventInfo)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string accountId = MultipleUserConnector::GetAccountIdBySubProfileId(eventInfo.userId, eventInfo.subProfileId);
    LOGI("SubProfileDeleted: userId %{public}d, subProfileId %{public}d, accountId %{public}s",
        eventInfo.userId, eventInfo.subProfileId, GetAnonyString(accountId).c_str());
    if (!accountId.empty()) {
        NotifyPeerDevices(localUdid, eventInfo.userId, accountId, AccountEventType::ACCOUNT_DELETED);
        DeviceProfileConnector::GetInstance().DeleteAclByAccountId(localUdid, eventInfo.userId, accountId);
        MultipleUserConnector::DeleteAccountInfo(eventInfo.userId, eventInfo.subProfileId);
    }
#endif
}

void DeviceManagerService::HandleSubProfileSwitchedEvent(const std::string& localUdid,
    const DmAccountEventInfo& eventInfo)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
#ifdef CAR_DEVICE_ENABLE
    TriggerForegroundAccountSync();
    LOGI("SubProfileSwitched: userId %{public}d, previousSubProfileId %{public}d",
        eventInfo.userId, eventInfo.previousSubProfileId);
    DeviceProfileConnector::GetInstance().HandleSubProfileSwitched(localUdid, eventInfo.userId,
        eventInfo.subProfileId, eventInfo.previousSubProfileId);
#endif
#endif
}

void DeviceManagerService::HandleDistributedAccountBoundEvent(const DmAccountEventInfo& eventInfo)
{
    DeviceNameManager::GetInstance().InitDeviceNameWhenLogin();
    DMAccountInfo dmAccountInfo;
#ifdef CAR_DEVICE_ENABLE
    dmAccountInfo = MultipleUserConnector::GetDMAccountInfoBySubProfileId(eventInfo.userId, eventInfo.subProfileId);
    dmAccountInfo.subProfileId = eventInfo.subProfileId;
#else
    dmAccountInfo.subProfileId = 0;
    dmAccountInfo.accountId = MultipleUserConnector::GetOhosAccountId();
#endif
    dmAccountInfo.accountName = MultipleUserConnector::GetOhosAccountNameByUserId(eventInfo.userId);
    MultipleUserConnector::SetAccountInfo(eventInfo.userId, eventInfo.subProfileId, dmAccountInfo);
    LOGI("Cached account binding: userId %{public}d, accountId %{public}s, subProfileId %{public}d",
        eventInfo.userId, GetAnonyString(eventInfo.accountId).c_str(), eventInfo.subProfileId);
}

void DeviceManagerService::HandleDistributedAccountLoginEvent(const std::string& localUdid,
    const DmAccountEventInfo& eventInfo)
{
    DeviceNameManager::GetInstance().InitDeviceNameWhenLogin();
    DMAccountInfo dmAccountInfo;
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
#ifdef CAR_DEVICE_ENABLE
    dmAccountInfo = MultipleUserConnector::GetDMAccountInfoBySubProfileId(eventInfo.userId, eventInfo.subProfileId);
    dmAccountInfo.subProfileId = eventInfo.subProfileId;
#else
    dmAccountInfo.subProfileId = 0;
    dmAccountInfo.accountId = MultipleUserConnector::GetOhosAccountId();
#endif
    MultipleUserConnector::SetAccountInfo(eventInfo.userId, eventInfo.subProfileId, dmAccountInfo);
#ifdef CAR_DEVICE_ENABLE
    DeviceProfileConnector::GetInstance().HandleDistributedAccountLogin(localUdid, eventInfo.userId,
        eventInfo.accountId);
#endif
#endif
}

void DeviceManagerService::HandleDistributedAccountLogoutEvent(const std::string& localUdid,
    const DmAccountEventInfo& eventInfo)
{
#ifdef CAR_DEVICE_ENABLE
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::string accountId = MultipleUserConnector::GetAccountIdBySubProfileId(eventInfo.userId, eventInfo.subProfileId);
    LOGI("DistributedAccountLogout: userId %{public}d, subProfileId %{public}d, accountId %{public}s",
        eventInfo.userId, eventInfo.subProfileId, GetAnonyString(accountId).c_str());
    if (!accountId.empty()) {
        NotifyPeerDevices(localUdid, eventInfo.userId, accountId, AccountEventType::ACCOUNT_LOGOUT);
        DeviceProfileConnector::GetInstance().HandleDistributedAccountLogout(localUdid, eventInfo.userId, accountId);
    }
#endif
#else
    ffrt::submit([=]() {
        HandleAccountLogoutEventCallback(eventInfo.eventName, eventInfo.userId, eventInfo.beforeUserId);
    },
        ffrt::task_attr().name(HANDLE_ACCOUNT_LOGOUT_EVENT_CALLBACK_TASK));
#endif
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
void DeviceManagerService::NotifyPeerDevices(const std::string& localUdid, int32_t userId, const std::string& accountId,
    AccountEventType accountEventType)
{
    LOGI("userId %{public}d, accountId %{public}s, eventType %{public}d",
        userId, GetAnonyString(accountId).c_str(), static_cast<uint32_t>(accountEventType));
    std::vector<DistributedDeviceProfile::AccessControlProfile> profiles =
        DeviceProfileConnector::GetInstance().GetAclByAccountId(localUdid, userId, accountId);
    
    std::vector<std::string> bleUdids;
    std::map<std::string, std::string> wifiDevices;
    
    for (const auto& profile : profiles) {
        std::string peerUdid;
        if (profile.GetAccesser().GetAccesserDeviceId() == localUdid) {
            peerUdid = profile.GetAccessee().GetAccesseeDeviceId();
        } else if (profile.GetAccessee().GetAccesseeDeviceId() == localUdid) {
            peerUdid = profile.GetAccesser().GetAccesserDeviceId();
        } else {
            continue;
        }
        
        std::string networkId;
        int32_t networkType = 0;
        if (!SoftbusCache::GetInstance().GetNetworkIdFromCache(peerUdid.c_str(), networkId)) {
            LOGI("Peer %{public}s is offline, use broadcast", GetAnonyString(peerUdid).c_str());
            bleUdids.push_back(peerUdid);
            continue;
        }
        
        if (softbusListener_->GetNetworkTypeByNetworkId(networkId.c_str(), networkType) != DM_OK || networkType <= 0) {
            LOGI("Get networkType failed for %{public}s, use broadcast", GetAnonyString(peerUdid).c_str());
            bleUdids.push_back(peerUdid);
            continue;
        }
        
        uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
        if ((static_cast<uint32_t>(networkType) & addrTypeMask) != 0x0) {
            LOGI("Peer %{public}s is BLE networking, use broadcast", GetAnonyString(peerUdid).c_str());
            bleUdids.push_back(peerUdid);
        } else {
            LOGI("Peer %{public}s is WiFi networking, use connection", GetAnonyString(peerUdid).c_str());
            wifiDevices.insert(std::pair<std::string, std::string>(peerUdid, networkId));
        }
    }
    
    if (!bleUdids.empty()) {
        NotifyPeerByBroadcast(userId, accountId, bleUdids, accountEventType);
    }
    if (!wifiDevices.empty()) {
        for (const auto& it : wifiDevices) {
            NotifyPeerByConnection(it.second, userId, accountId, accountEventType);
        }
    }
}
#endif

void DeviceManagerService::NotifyPeerByConnection(const std::string& networkId, int32_t userId,
    const std::string& accountId, AccountEventType accountEventType)
{
    LOGI("networkId %{public}s, userId %{public}d, accountId %{public}s, eventType %{public}d",
        GetAnonyString(networkId).c_str(), userId, GetAnonyString(accountId).c_str(),
        static_cast<uint32_t>(accountEventType));
    CHECK_NULL_VOID(DMCommTool::GetInstance());
    
    int32_t ret = DMCommTool::GetInstance()->SendAccountEvent(networkId, accountId, userId, accountEventType);
    if (ret != DM_OK) {
        LOGE("SendAccountEvent failed, ret %{public}d", ret);
    }
}

void DeviceManagerService::NotifyPeerByBroadcast(int32_t userId, const std::string& accountId,
    const std::vector<std::string>& peerUdids, AccountEventType accountEventType)
{
    LOGI("NotifyPeerByBroadcast: userId %{public}d, accountId %{public}s, peerCount %{public}zu, eventType %{public}d",
        userId, GetAnonyString(accountId).c_str(), peerUdids.size(), static_cast<uint32_t>(accountEventType));
    
    char accountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
    if (Crypto::GetAccountIdHash7(accountId, reinterpret_cast<uint8_t *>(accountIdHash)) != DM_OK) {
        LOGE("GetAccountIdHash7 failed, userId %{public}d, accountId %{public}s",
            userId, GetAnonyString(accountId).c_str());
        return;
    }
    
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::ACCOUNT_EVENT;
    msg.accountEventType = accountEventType;
    msg.userId = userId;
    msg.accountId = std::string(accountIdHash);
    msg.peerUdids = peerUdids;
    msg.syncUserIdFlag = false;
    
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    if (broadCastMsg.empty()) {
        LOGE("SyncTrustRelationShip failed");
        return;
    }
    
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::GetLocalUserIdFromDataBase(std::vector<int32_t> &foregroundUsers,
    std::vector<int32_t> &backgroundUsers)
{
    std::string userIdStr;
    KVAdapterManager::GetInstance().GetLocalUserIdData(LOCAL_ALL_USERID, userIdStr);
    if (userIdStr.empty()) {
        LOGE("result is empty");
        return;
    }
    JsonObject userIdJson(userIdStr);
    if (userIdJson.IsDiscarded()) {
        LOGE("userIdJson parse failed");
        return;
    }
    if (IsArray(userIdJson, LOCAL_FOREGROUND_USERID)) {
        userIdJson[LOCAL_FOREGROUND_USERID].Get(foregroundUsers);
    }
    if (IsArray(userIdJson, LOCAL_BACKGROUND_USERID)) {
        userIdJson[LOCAL_BACKGROUND_USERID].Get(backgroundUsers);
    }
}

void DeviceManagerService::PutLocalUserIdToDataBase(const std::vector<int32_t> &foregroundUsers,
    const std::vector<int32_t> &backgroundUsers)
{
    JsonObject jsonObj;
    jsonObj[LOCAL_FOREGROUND_USERID] = foregroundUsers;
    jsonObj[LOCAL_BACKGROUND_USERID] = backgroundUsers;
    std::string localUserIdStr = jsonObj.Dump();
    KVAdapterManager::GetInstance().PutLocalUserIdData(LOCAL_ALL_USERID, localUserIdStr);
}

bool DeviceManagerService::IsUserStatusChanged(std::vector<int32_t> foregroundUserVec,
    std::vector<int32_t> backgroundUserVec)
{
    LOGI("foregroundUserVec: %{public}s, backgroundUserVec: %{public}s",
        GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str());
    std::vector<int32_t> dBForegroundUserIds;
    std::vector<int32_t> dBBackgroundUserIds;
    GetLocalUserIdFromDataBase(dBForegroundUserIds, dBBackgroundUserIds);
    LOGI("dBForegroundUserIds: %{public}s, dBBackgroundUserIds: %{public}s",
        GetIntegerList(dBForegroundUserIds).c_str(), GetIntegerList(dBBackgroundUserIds).c_str());
    std::sort(foregroundUserVec.begin(), foregroundUserVec.end());
    std::sort(backgroundUserVec.begin(), backgroundUserVec.end());
    std::sort(dBForegroundUserIds.begin(), dBForegroundUserIds.end());
    std::sort(dBBackgroundUserIds.begin(), dBBackgroundUserIds.end());
    if (foregroundUserVec == dBForegroundUserIds && backgroundUserVec == dBBackgroundUserIds) {
        LOGI("User status has not changed.");
        return false;
    }
    dBForegroundUserIds = foregroundUserVec;
    dBBackgroundUserIds = backgroundUserVec;
    PutLocalUserIdToDataBase(dBForegroundUserIds, dBBackgroundUserIds);
    return true;
}

void DeviceManagerService::HandleAccountCommonEvent(const std::string commonEventType)
{
    LOGI("commonEventType: %{public}s.", commonEventType.c_str());
    std::vector<int32_t> foregroundUserVec;
    int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec);
    std::vector<int32_t> backgroundUserVec;
    int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec);
    MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec);
    if (retFront != DM_OK || retBack != DM_OK) {
        LOGE("retFront: %{public}d, retBack: %{public}d, frontuserids: %{public}s, backuserids: %{public}s",
            retFront, retBack, GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str());
        return;
    }
    if (!IsUserStatusChanged(foregroundUserVec, backgroundUserVec)) {
        LOGI("User status has not changed.");
        return;
    }
    std::string localUdid = GetLocalDeviceUdid();
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    if (!discoveryMgr_->GetCommonDependencyObj()->CheckAclStatusAndForegroundNotMatch(localUdid, foregroundUserVec,
        backgroundUserVec)) {
        LOGI("no unreasonable data.");
        return;
    }
    std::map<std::string, int32_t> curUserDeviceMap =
        discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(foregroundUserVec, localUdid);
    std::map<std::string, int32_t> preUserDeviceMap =
        discoveryMgr_->GetCommonDependencyObj()->GetDeviceIdAndBindLevel(backgroundUserVec, localUdid);
    std::vector<std::string> peerUdids;
    PushPeerUdids(curUserDeviceMap, preUserDeviceMap, peerUdids);
    if (peerUdids.empty()) {
        LOGE("peerUdids is empty");
        return;
    }
    NotifyRemoteAccountCommonEvent(commonEventType, localUdid, peerUdids, foregroundUserVec, backgroundUserVec);
}

void DeviceManagerService::TriggerForegroundAccountSync()
{
    LOGI("start.");
    std::string localUdid = GetLocalDeviceUdid();
    
    std::vector<int32_t> foregroundUserIds;
    int32_t ret = MultipleUserConnector::GetForegroundUserIds(foregroundUserIds);
    if (ret != DM_OK || foregroundUserIds.empty()) {
        LOGE("GetForegroundUserIds failed or empty, ret: %{public}d", ret);
        return;
    }
    
    std::vector<ForegroundAccountInfo> foregroundAccounts;
    for (const auto &userId : foregroundUserIds) {
        DMAccountInfo dmAccountInfo;
        int32_t subProfileId = 0;
#ifdef CAR_DEVICE_ENABLE
        subProfileId = MultipleUserConnector::GetSubProfileIdByUserId(userId);
        if (subProfileId < 0) {
            LOGI("User %{public}d has no valid subProfileId, skip", userId);
            continue;
        }
        dmAccountInfo = MultipleUserConnector::GetDMAccountInfoBySubProfileId(userId, subProfileId);
#else
        dmAccountInfo.accountId = MultipleUserConnector::GetOhosAccountId();
#endif
        foregroundAccounts.push_back(ForegroundAccountInfo(userId, dmAccountInfo.accountId));
        LOGI("foreground userId: %{public}d, subProfileId: %{public}d, accountId: %{public}s",
            userId, subProfileId, GetAnonyString(dmAccountInfo.accountId).c_str());
    }
    
    std::map<std::string, std::string> wifiDevices;
    std::vector<std::string> bleUdids;
    GetWifiAndBleDevices(wifiDevices, bleUdids);
    
    if (!bleUdids.empty()) {
        SendForegroundAccountBroadcast(bleUdids, foregroundAccounts, true);
    }
    
    if (!wifiDevices.empty()) {
        for (const auto &device : wifiDevices) {
            DMCommTool::GetInstance()->SendForegroundAccount(device.second, foregroundAccounts);
        }
    }
    LOGI("completed.");
}

void DeviceManagerService::GetWifiAndBleDevices(std::map<std::string, std::string> &wifiDevices,
    std::vector<std::string> &bleUdids)
{
    if (!IsDMServiceImplReady()) {
        LOGE("dmServiceImpl not ready");
        return;
    }
    
    std::string localUdid = GetLocalDeviceUdid();
    
    std::vector<int32_t> foregroundUserIds;
    int32_t ret = MultipleUserConnector::GetForegroundUserIds(foregroundUserIds);
    if (ret != DM_OK || foregroundUserIds.empty()) {
        LOGE("GetForegroundUserIds failed or empty, ret: %{public}d", ret);
        return;
    }
    
    std::map<std::string, int32_t> allDeviceMap;
    for (const auto &userId : foregroundUserIds) {
        std::map<std::string, int32_t> userDeviceMap = dmServiceImpl_->GetDeviceIdAndBindLevel(userId);
        for (const auto &device : userDeviceMap) {
            allDeviceMap[device.first] = device.second;
        }
    }
    
    std::vector<std::string> peerUdids;
    for (const auto &item : allDeviceMap) {
        peerUdids.push_back(item.first);
    }
    
    if (peerUdids.empty()) {
        LOGI("peerUdids is empty");
        return;
    }
    
    DivideNotifyMethod(peerUdids, bleUdids, wifiDevices);
    LOGI("bleUdids size: %{public}zu, wifiDevices size: %{public}zu", bleUdids.size(), wifiDevices.size());
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
void DeviceManagerService::PushPeerUdids(const std::map<std::string, int32_t> &curUserDeviceMap,
    const std::map<std::string, int32_t> &preUserDeviceMap, std::vector<std::string> &peerUdids)
{
    for (const auto &item : curUserDeviceMap) {
        peerUdids.push_back(item.first);
    }
    for (const auto &item : preUserDeviceMap) {
        if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) {
            peerUdids.push_back(item.first);
        }
    }
}

void DeviceManagerService::SendCommonEventBroadCast(const std::string commonEventType,
    std::vector<std::string> &bleUdids, const std::vector<int32_t> &foregroundUserIds,
    const std::vector<int32_t> &backgroundUserIds)
{
    if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_UNLOCKED ||
        commonEventType == CommonEventSupport::COMMON_EVENT_USER_SWITCHED ||
        commonEventType == CommonEventSupport::COMMON_EVENT_USER_FOREGROUND ||
        commonEventType == CommonEventSupport::COMMON_EVENT_USER_BACKGROUND) {
        SendCommonEventBroadCast(bleUdids, foregroundUserIds, backgroundUserIds, true);
    } else if (commonEventType == CommonEventSupport::COMMON_EVENT_USER_STOPPED) {
        SendCommonEventBroadCast(bleUdids, foregroundUserIds, backgroundUserIds, false);
    } else {
        LOGE("commonEventType not match");
        return;
    }
}

void DeviceManagerService::NotifyRemoteAccountCommonEvent(const std::string commonEventType,
    const std::string &localUdid, const std::vector<std::string> &peerUdids,
    const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds)
{
    LOGI("foregroundUserIds: %{public}s, backgroundUserIds: %{public}s",
        GetIntegerList<int32_t>(foregroundUserIds).c_str(), GetIntegerList<int32_t>(backgroundUserIds).c_str());
    if (peerUdids.empty()) {
        return;
    }
    if (softbusListener_ == nullptr) {
        UpdateAcl(localUdid, peerUdids, foregroundUserIds, backgroundUserIds);
        LOGE("softbusListener_ is null");
        return;
    }
    std::vector<std::string> bleUdids;
    std::map<std::string, std::string> wifiDevices;
    for (const auto &udid : peerUdids) {
        std::string netWorkId = "";
        SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId);
        if (netWorkId.empty()) {
            LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        int32_t networkType = 0;
        if (softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType) != DM_OK || networkType <= 0) {
            LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
        if ((static_cast<uint32_t>(networkType) & addrTypeMask) != 0x0) {
            bleUdids.push_back(udid);
        } else {
            wifiDevices.insert(std::pair<std::string, std::string>(udid, netWorkId));
        }
    }
    if (!bleUdids.empty()) {
        UpdateAcl(localUdid, bleUdids, foregroundUserIds, backgroundUserIds);
        SendCommonEventBroadCast(commonEventType, bleUdids, foregroundUserIds, backgroundUserIds);
    }
    if (!wifiDevices.empty()) {
        NotifyRemoteAccountCommonEventByWifi(localUdid, wifiDevices, foregroundUserIds, backgroundUserIds);
    }
}

void DeviceManagerService::NotifyRemoteAccountCommonEventByWifi(const std::string &localUdid,
    const std::map<std::string, std::string> &wifiDevices, const std::vector<int32_t> &foregroundUserIds,
    const std::vector<int32_t> &backgroundUserIds)
{
    for (const auto &it : wifiDevices) {
        int32_t result = SendAccountCommonEventByWifi(it.second, foregroundUserIds, backgroundUserIds);
        if (result != DM_OK) {
            LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str());
            std::vector<std::string> updateUdids;
            updateUdids.push_back(it.first);
            UpdateAcl(localUdid, updateUdids, foregroundUserIds, backgroundUserIds);
            continue;
        }
        std::lock_guard<std::mutex> autoLock(timerLocks_);
        if (timer_ == nullptr) {
            timer_ = std::make_shared<DmTimer>();
        }
        std::string udid = it.first;
        timer_->StartTimer(std::string(ACCOUNT_COMMON_EVENT_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid),
            USER_SWITCH_BY_WIFI_TIMEOUT_S,
            [this, localUdid, foregroundUserIds, backgroundUserIds, udid] (std::string name) {
                DeviceManagerService::HandleCommonEventTimeout(localUdid, foregroundUserIds, backgroundUserIds, udid);
            });
    }
}

int32_t DeviceManagerService::SendAccountCommonEventByWifi(const std::string &networkId,
    const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds)
{
    LOGI("start");
    std::vector<uint32_t> foregroundUserIdsUInt;
    for (auto const &u : foregroundUserIds) {
        foregroundUserIdsUInt.push_back(static_cast<uint32_t>(u));
    }
    std::vector<uint32_t> backgroundUserIdsUInt;
    for (auto const &u : backgroundUserIds) {
        backgroundUserIdsUInt.push_back(static_cast<uint32_t>(u));
    }
    CHECK_NULL_RETURN(DMCommTool::GetInstance(), ERR_DM_POINT_NULL);
    return DMCommTool::GetInstance()->SendUserIds(networkId, foregroundUserIdsUInt, backgroundUserIdsUInt);
}

void DeviceManagerService::HandleCommonEventTimeout(const std::string &localUdid,
    const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds,
    const std::string &udid)
{
    LOGI("start udid: %{public}s", GetAnonyString(udid).c_str());
    std::vector<std::string> updateUdids;
    updateUdids.push_back(udid);
    UpdateAcl(localUdid, updateUdids, foregroundUserIds, backgroundUserIds);
}

void DeviceManagerService::UpdateAcl(const std::string &localUdid,
    const std::vector<std::string> &peerUdids, const std::vector<int32_t> &foregroundUserIds,
    const std::vector<int32_t> &backgroundUserIds)
{
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    std::vector<DmUserRemovedServiceInfo> serviceInfos;
    discoveryMgr_->GetCommonDependencyObj()->HandleAccountCommonEvent(localUdid, peerUdids, foregroundUserIds,
        backgroundUserIds, serviceInfos);
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("IsDMServiceAdapterResidentLoad failed.");
        return;
    }
    for (auto &item : serviceInfos) {
        if (item.isActive == true) {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                ServiceStateBindParameter bindParam = {
                    item.localTokenId,
                    item.localPkgName,
                    item.bindType,
                    item.peerUdid,
                    item.peerUserId,
                    item.serviceIds[i]
                };
                dmServiceImplExtResident_->BindServiceOnline(bindParam);
            }
        } else {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                DistributedDeviceProfile::ServiceInfo dpServiceInfo;
                DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                    item.peerUdid, item.serviceIds[i], dpServiceInfo);
                dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                    item.bindType, item.peerUdid, dpServiceInfo);
            }
        }
    }
}

void DeviceManagerService::UpdateAclAndDeleteGroup(const std::string &localUdid,
    const std::vector<std::string> &deviceVec, const std::vector<int32_t> &foregroundUserIds,
    const std::vector<int32_t> &backgroundUserIds)
{
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    discoveryMgr_->GetCommonDependencyObj()->HandleUserSwitched(localUdid, deviceVec,
        foregroundUserIds, backgroundUserIds);
}
#endif

void DeviceManagerService::HandleAccountLogout(int32_t userId, const std::string &accountId,
    const std::string &accountName)
{
    LOGI("UserId: %{public}d, accountId: %{public}s, accountName: %{public}s", userId,
        GetAnonyString(accountId).c_str(), GetAnonyString(accountName).c_str());

    if (IsDMServiceAdapterResidentLoad()) {
        dmServiceImplExtResident_->ClearCacheWhenLogout(userId, accountId);
    } else {
        LOGW("ClearCacheWhenLogout fail, adapter instance not init or init failed.");
    }
    if (!IsDMServiceImplReady()) {
        LOGE("Init impl failed.");
        return;
    }

    std::string localUdid = GetLocalDeviceUdid();
    auto deviceMap = dmServiceImpl_->GetDeviceIdAndUserId(userId, accountId);

    std::vector<std::string> peerHOUdids;
    GetHoOsTypeUdids(peerHOUdids);
    std::vector<std::string> peerUdids;
    std::vector<std::string> dualPeerUdids;
    for (const auto &item : deviceMap) {
        if (find(peerHOUdids.begin(), peerHOUdids.end(), item.first) != peerHOUdids.end()) {
            LOGI("dualUdid: %{public}s", GetAnonyString(item.first).c_str());
            dualPeerUdids.emplace_back(item.first);
        } else {
            peerUdids.emplace_back(item.first);
        }
    }

    if (!dualPeerUdids.empty() && IsDMServiceAdapterResidentLoad()) {
        LOGI("logout notify userId: %{public}d, accountId: %{public}s", userId, GetAnonyString(accountId).c_str());
        dmServiceImplExtResident_->AccountIdLogout(userId, accountId, dualPeerUdids);
    }
    HandleRegularPeerLogout(userId, accountId, accountName, peerUdids);
    ProcessDeviceMapForLogout(userId, deviceMap, localUdid, accountId);
}

void DeviceManagerService::HandleRegularPeerLogout(int32_t userId, const std::string &accountId,
    const std::string &accountName, const std::vector<std::string>& peerUdids)
{
    if (!peerUdids.empty()) {
        char accountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
        if (Crypto::GetAccountIdHash(accountId, reinterpret_cast<uint8_t *>(accountIdHash)) != DM_OK) {
            LOGE("GetAccountHash failed, userId: %{public}d, accountId: %{public}s, accountName: %{public}s",
                userId, GetAnonyString(accountId).c_str(), GetAnonyString(accountName).c_str());
            return;
        }
        NotifyRemoteLocalLogout(peerUdids, std::string(accountIdHash), accountName, userId);
    }
}

void DeviceManagerService::ProcessDeviceMapForLogout(int32_t userId,
    const std::multimap<std::string, int32_t> &deviceMap, const std::string &localUdid,
    const std::string &accountId)
{
    for (const auto &item : deviceMap) {
        DMAclQuadInfo info = {localUdid, userId, item.first, item.second};
        std::vector<DmUserRemovedServiceInfo> serviceInfos;
        dmServiceImpl_->HandleIdentAccountLogout(info, accountId, serviceInfos);
        if (!IsDMServiceAdapterResidentLoad()) {
            LOGE("IsDMServiceAdapterResidentLoad failed.");
            return;
        }
        LogoutProcessServiceInfos(serviceInfos, localUdid);
    }
}

void DeviceManagerService::LogoutProcessServiceInfos(const std::vector<DmUserRemovedServiceInfo> &serviceInfos,
    const std::string& localUdid)
{
    for (const auto &item : serviceInfos) {
        for (size_t i = 0; i < item.serviceIds.size(); i++) {
            DistributedDeviceProfile::ServiceInfo dpServiceInfo;
            DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                item.peerUdid, item.serviceIds[i], dpServiceInfo);
            dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                item.bindType, item.peerUdid, dpServiceInfo);
            dmServiceImplExtResident_->DeleteServiceInfoForAccountEvent(item.peerUserId, item.peerUdid,
                localUdid);
        }
    }
}

void DeviceManagerService::HandleUserRemoved(int32_t removedUserId)
{
    LOGI("PreUserId %{public}d.", removedUserId);
    if (!IsDMServiceImplReady()) {
        LOGE("Init impl failed.");
        return;
    }
    std::multimap<std::string, int32_t> deviceMap = dmServiceImpl_->GetDeviceIdAndUserId(removedUserId);
    std::vector<std::string> peerUdids;
    for (const auto &item : deviceMap) {
        if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) {
            peerUdids.emplace_back(item.first);
        }
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    if (!peerUdids.empty()) {
        // Send UserId Removed broadcast
        SendUserRemovedBroadCast(peerUdids, removedUserId);
        std::vector<DmUserRemovedServiceInfo> serviceInfos;
        dmServiceImpl_->HandleUserRemoved(peerUdids, removedUserId, serviceInfos);
        if (!IsDMServiceAdapterResidentLoad()) {
            LOGE("IsDMServiceAdapterResidentLoad failed.");
            return;
        }
        for (auto &item : serviceInfos) {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                DistributedDeviceProfile::ServiceInfo dpServiceInfo;
                DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                    item.peerUdid, item.serviceIds[i], dpServiceInfo);
                dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                    item.bindType, item.peerUdid, dpServiceInfo);
                dmServiceImplExtResident_->DeleteServiceInfoForAccountEvent(item.peerUserId, item.peerUdid,
                    localUdid);
            }
        }
    }
    IpcServerStub::GetInstance().HandleUserRemoved(removedUserId);
}

void DeviceManagerService::SendUserRemovedBroadCast(const std::vector<std::string> &peerUdids, int32_t userId)
{
    LOGI("peerUdids: %{public}s, userId %{public}d.", GetAnonyStringList(peerUdids).c_str(), userId);
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::DEL_USER;
    msg.userId = static_cast<uint32_t>(userId);
    msg.peerUdids = peerUdids;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids,
    const std::string &accountId, const std::string &accountName, int32_t userId)
{
    LOGI("accountId %{public}s, accountName %{public}s, userId %{public}d.", GetAnonyString(accountId).c_str(),
        GetAnonyString(accountName).c_str(), userId);
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::ACCOUNT_LOGOUT;
    msg.userId = static_cast<uint32_t>(userId);
    msg.peerUdids = peerUdids;
    msg.accountId = accountId;
    msg.accountName = accountName;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::SendUserIdsBroadCast(const std::vector<std::string> &peerUdids,
    const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds, bool isNeedResponse)
{
    LOGI("peerUdids: %{public}s, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s, isNeedRsp: %{public}s",
        GetAnonyStringList(peerUdids).c_str(), GetIntegerList<int32_t>(foregroundUserIds).c_str(),
        GetIntegerList<int32_t>(backgroundUserIds).c_str(), isNeedResponse ? "true" : "false");
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::SYNC_USERID;
    msg.peerUdids = peerUdids;
    msg.syncUserIdFlag = isNeedResponse;
    for (const auto &userId : foregroundUserIds) {
        msg.userIdInfos.push_back({ true, static_cast<uint16_t>(userId) });
    }
    for (auto const &userId : backgroundUserIds) {
        msg.userIdInfos.push_back({ false, static_cast<uint16_t>(userId) });
    }
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::HandleUserIdsBroadCast(const std::vector<UserIdInfo> &remoteUserIdInfos,
    const std::string &remoteUdid, bool isNeedResponse)
{
    LOGI("rmtUdid: %{public}s, rmtUserIds: %{public}s, isNeedResponse: %{public}s,",
        GetAnonyString(remoteUdid).c_str(), GetUserIdInfoList(remoteUserIdInfos).c_str(),
        isNeedResponse ? "true" : "false");

    if (isNeedResponse) {
        std::vector<int32_t> foregroundUserVec;
        std::vector<int32_t> backgroundUserVec;
        int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec);
        int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec);

        if (IsPC()) {
            MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec);
        }

        if (retFront != DM_OK || retBack != DM_OK) {
            LOGE("Get userid failed, retFront: %{public}d, retBack: %{public}d, frontUserNum:%{public}d,"
                "backUserNum: %{public}d", retFront, retBack,
                static_cast<int32_t>(foregroundUserVec.size()),
                static_cast<int32_t>(backgroundUserVec.size()));
        } else {
            LOGI("Send back local frontuserids: %{public}s, backuserids: %{public}s",
                GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str());
            std::vector<std::string> remoteUdids = { remoteUdid };
            SendUserIdsBroadCast(remoteUdids, foregroundUserVec, backgroundUserVec, false);
        }
    }

    ProcessReceivedUserIds(remoteUserIdInfos, remoteUdid);
    ProcessServiceBindings(remoteUserIdInfos, remoteUdid);
}

static std::vector<uint32_t> ConvertToUserIdVector(const std::vector<UserIdInfo> &userIdInfos)
{
    std::vector<uint32_t> userIds;
    for (const auto &u : userIdInfos) {
        userIds.push_back(static_cast<uint32_t>(u.userId));
    }
    return userIds;
}

void DeviceManagerService::ProcessReceivedUserIds(const std::vector<UserIdInfo> &remoteUserIdInfos,
    const std::string &remoteUdid)
{
    std::vector<UserIdInfo> foregroundUserIdInfos;
    std::vector<UserIdInfo> backgroundUserIdInfos;
    GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);

    LOGI("process foreground and background userids");

    std::vector<uint32_t> foregroundUserIds = ConvertToUserIdVector(foregroundUserIdInfos);
    std::vector<uint32_t> backgroundUserIds = ConvertToUserIdVector(backgroundUserIdInfos);

    if (softbusListener_ != nullptr) {
        softbusListener_->SetForegroundUserIdsToDSoftBus(remoteUdid, foregroundUserIds);
    }
}

void DeviceManagerService::ProcessServiceBindings(const std::vector<UserIdInfo> &remoteUserIdInfos,
    const std::string &remoteUdid)
{
    if (!IsDMServiceImplReady()) {
        return;
    }

    std::vector<UserIdInfo> foregroundUserIdInfos;
    std::vector<UserIdInfo> backgroundUserIdInfos;
    GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);

    std::vector<uint32_t> foregroundUserIds = ConvertToUserIdVector(foregroundUserIdInfos);
    std::vector<uint32_t> backgroundUserIds = ConvertToUserIdVector(backgroundUserIdInfos);

    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);

    std::vector<DmUserRemovedServiceInfo> serviceInfos;
    dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, IsPC(), serviceInfos);

    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("IsDMServiceAdapterResidentLoad failed.");
        return;
    }
    for (auto &item : serviceInfos) {
        if (item.isActive) {
            ProcessActiveServices(item);
        } else {
            ProcessInactiveServices(item);
        }
    }
}

void DeviceManagerService::ProcessActiveServices(const DmUserRemovedServiceInfo &serviceInfo)
{
    for (size_t i = 0; i < serviceInfo.serviceIds.size(); i++) {
        ServiceStateBindParameter bindParam = {
            serviceInfo.localTokenId,
            serviceInfo.localPkgName,
            serviceInfo.bindType,
            serviceInfo.peerUdid,
            serviceInfo.peerUserId,
            serviceInfo.serviceIds[i]
        };
        dmServiceImplExtResident_->BindServiceOnline(bindParam);
    }
}

void DeviceManagerService::ProcessInactiveServices(const DmUserRemovedServiceInfo &serviceInfo)
{
    for (size_t i = 0; i < serviceInfo.serviceIds.size(); i++) {
        DistributedDeviceProfile::ServiceInfo dpServiceInfo;
        DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(serviceInfo.peerUdid,
            serviceInfo.serviceIds[i], dpServiceInfo);
        dmServiceImplExtResident_->BindServiceOffline(
            serviceInfo.localTokenId,
            serviceInfo.localPkgName,
            serviceInfo.bindType,
            serviceInfo.peerUdid,
            dpServiceInfo);
    }
}

void DeviceManagerService::ProcessSyncUserIdsInner(std::vector<DmUserRemovedServiceInfo> &serviceInfos)
{
    for (auto &item : serviceInfos) {
        if (item.isActive == true) {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                ServiceStateBindParameter bindParam = {
                    item.localTokenId,
                    item.localPkgName,
                    item.bindType,
                    item.peerUdid,
                    item.peerUserId,
                    item.serviceIds[i]
                };
                dmServiceImplExtResident_->BindServiceOnline(bindParam);
            }
        } else {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                DistributedDeviceProfile::ServiceInfo dpServiceInfo;
                DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                    item.peerUdid, item.serviceIds[i], dpServiceInfo);
                dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                    item.bindType, item.peerUdid, dpServiceInfo);
            }
        }
    }
}

void DeviceManagerService::ProcessSyncUserIds(const std::vector<uint32_t> &foregroundUserIds,
    const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid)
{
    LOGI("process sync foregroundUserIds: %{public}s, backgroundUserIds: %{public}s, remote udid: %{public}s",
        GetIntegerList<uint32_t>(foregroundUserIds).c_str(), GetIntegerList<uint32_t>(backgroundUserIds).c_str(),
        GetAnonyString(remoteUdid).c_str());

    if (softbusListener_ != nullptr) {
        softbusListener_->SetForegroundUserIdsToDSoftBus(remoteUdid, foregroundUserIds);
    }
    std::lock_guard<std::mutex> autoLock(timerLocks_);
    if (timer_ != nullptr) {
        timer_->DeleteTimer(std::string(USER_SWITCH_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid));
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    if (IsDMServiceImplReady()) {
        std::vector<DmUserRemovedServiceInfo> serviceInfos;
        dmServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, IsPC(), serviceInfos);
        if (!IsDMServiceAdapterResidentLoad()) {
            LOGE("IsDMServiceAdapterResidentLoad failed.");
            return;
        }
        // user online&offline
        ProcessSyncUserIdsInner(serviceInfos);
    }
}

void DeviceManagerService::ProcessUninstApp(int32_t userId, int32_t tokenId)
{
    LOGI("userId: %{public}s, tokenId: %{public}s",
        GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str());
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->ProcessAppUninstall(userId, tokenId);
    }
}

void DeviceManagerService::ProcessUnBindApp(int32_t userId, int32_t tokenId, const std::string &extra,
    const std::string &udid)
{
    LOGI("userId: %{public}s, tokenId: %{public}s, udid: %{public}s",
        GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(udid).c_str());
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->ProcessUnBindApp(userId, tokenId, extra, udid);
    }
}

void DeviceManagerService::SendCommonEventBroadCast(const std::vector<std::string> &peerUdids,
    const std::vector<int32_t> &foregroundUserIds, const std::vector<int32_t> &backgroundUserIds, bool isNeedResponse)
{
    LOGI("peerUdids: %{public}s, foregroundUserIds: %{public}s, backgroundUserIds: %{public}s, isNeedRsp: %{public}s",
        GetAnonyStringList(peerUdids).c_str(), GetIntegerList<int32_t>(foregroundUserIds).c_str(),
        GetIntegerList<int32_t>(backgroundUserIds).c_str(), isNeedResponse ? "true" : "false");
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::SYNC_USERID;
    msg.peerUdids = peerUdids;
    msg.syncUserIdFlag = isNeedResponse;
    msg.isNewEvent = true;
    for (const auto &userId : foregroundUserIds) {
        msg.userIdInfos.push_back({ true, static_cast<uint16_t>(userId) });
    }
    for (auto const &userId : backgroundUserIds) {
        msg.userIdInfos.push_back({ false, static_cast<uint16_t>(userId) });
    }
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::SendForegroundAccountBroadcast(const std::vector<std::string> &peerUdids,
    const std::vector<ForegroundAccountInfo> &foregroundAccounts, bool isNeedResponse)
{
    LOGI("peerUdids %{public}s, accounts count %{public}zu, isNeedRsp %{public}s",
        GetAnonyStringList(peerUdids).c_str(), foregroundAccounts.size(), isNeedResponse ? "true" : "false");
    if (peerUdids.empty() || foregroundAccounts.empty()) {
        LOGE("peerUdids or foregroundAccounts is empty.");
        return;
    }

    uint8_t broadCastId = 0;
    for (size_t i = 0; i < foregroundAccounts.size(); i++) {
        char accountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
        if (Crypto::GetAccountIdHash7(foregroundAccounts[i].accountId,
            reinterpret_cast<uint8_t *>(accountIdHash)) != DM_OK) {
            LOGE("GetAccountIdHash7 failed, userId %{public}d, accountId %{public}s",
                foregroundAccounts[i].userId, GetAnonyString(foregroundAccounts[i].accountId).c_str());
            continue;
        }
        
        RelationShipChangeMsg msg;
        msg.type = RelationShipChangeType::ACCOUNT_EVENT;
        msg.accountEventType = isNeedResponse ? AccountEventType::SEND_ACCOUNT : AccountEventType::RESP_ACCOUNT;
        msg.peerUdids = peerUdids;
        msg.syncUserIdFlag = isNeedResponse;
        msg.userId = foregroundAccounts[i].userId;
        msg.accountId = std::string(accountIdHash);
        msg.isLastBatch = (i == foregroundAccounts.size() - 1);
        std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg, broadCastId);
        CHECK_NULL_VOID(softbusListener_);
        softbusListener_->SendAclChangedBroadcast(broadCastMsg);
        if (i != foregroundAccounts.size() - 1) {
            std::this_thread::sleep_for(std::chrono::milliseconds(SYNC_FOREGROUND_ACCOUNT_BROADCAST_INTERVAL_MS));
        }
    }
    LOGI("completed.");
}

void DeviceManagerService::HandleForegroundAccountBroadCast(
    const ForegroundAccountInfo &foregroundAccount, const std::string &remoteUdid,
    bool isNeedResponse, uint8_t broadCastId, bool isLastBatch)
{
    LOGI("rmtUdid %{public}s, isNeedResponse %{public}s, broadCastId %{public}d, isLastBatch %{public}s",
        GetAnonyString(remoteUdid).c_str(), isNeedResponse ? "true" : "false", broadCastId,
        isLastBatch ? "true" : "false");
    
    std::string cacheKey = remoteUdid + "_" + std::to_string(broadCastId);
    std::vector<ForegroundAccountInfo> accountsToProcess;
    
    if (!CacheForegroundAccount(cacheKey, foregroundAccount)) {
        return;
    }
    
    if (!isLastBatch) {
        StartForegroundAccountCacheTimer(cacheKey);
        return;
    }
    
    accountsToProcess = GetAndClearCachedAccounts(cacheKey);
    DeleteForegroundAccountCacheTimer(cacheKey);
    
    ProcessSyncForegroundAccount(accountsToProcess, remoteUdid);

    if (isNeedResponse) {
        SendForegroundAccountResponse(remoteUdid);
    }
}

bool DeviceManagerService::CacheForegroundAccount(const std::string &cacheKey,
    const ForegroundAccountInfo &foregroundAccount)
{
    std::lock_guard<std::mutex> lock(foregroundAccountCacheLock_);
    
    if (foregroundAccountCache_.size() >= MAX_FOREGROUND_ACCOUNT_CACHE_SIZE &&
        foregroundAccountCache_.find(cacheKey) == foregroundAccountCache_.end()) {
        LOGE("Foreground account cache size exceeded limit %{public}zu", MAX_FOREGROUND_ACCOUNT_CACHE_SIZE);
        return false;
    }
    
    auto &cachedAccounts = foregroundAccountCache_[cacheKey];
    if (cachedAccounts.size() >= MAX_SINGLE_CACHE_ACCOUNTS_SIZE) {
        LOGE("Single cache accounts size exceeded limit %{public}zu", MAX_SINGLE_CACHE_ACCOUNTS_SIZE);
        return false;
    }
    
    bool exists = false;
    for (const auto &cached : cachedAccounts) {
        if (cached.userId == foregroundAccount.userId && cached.accountId == foregroundAccount.accountId) {
            exists = true;
            break;
        }
    }
    if (!exists) {
        cachedAccounts.push_back(foregroundAccount);
    }
    return true;
}

void DeviceManagerService::StartForegroundAccountCacheTimer(const std::string &cacheKey)
{
    std::lock_guard<std::mutex> timerLock(timerLocks_);
    if (timer_ == nullptr) {
        timer_ = std::make_shared<DmTimer>();
    }
    std::string taskName = std::string(FOREGROUND_ACCOUNT_CACHE_TIMEOUT_TASK) + Crypto::Sha256(cacheKey);
    timer_->DeleteTimer(taskName);
    timer_->StartTimer(taskName, FOREGROUND_ACCOUNT_CACHE_TIMEOUT_S, [this, cacheKey] (std::string name) {
        std::lock_guard<std::mutex> cacheLock(foregroundAccountCacheLock_);
        foregroundAccountCache_.erase(cacheKey);
        LOGI("Foreground account cache timeout cleared");
    });
    LOGI("Started cache timer for key %{public}s", GetAnonyString(cacheKey).c_str());
}

std::vector<ForegroundAccountInfo> DeviceManagerService::GetAndClearCachedAccounts(const std::string &cacheKey)
{
    std::lock_guard<std::mutex> lock(foregroundAccountCacheLock_);
    std::vector<ForegroundAccountInfo> accounts;
    if (foregroundAccountCache_.find(cacheKey) != foregroundAccountCache_.end()) {
        accounts = foregroundAccountCache_[cacheKey];
        foregroundAccountCache_.erase(cacheKey);
    }
    return accounts;
}

void DeviceManagerService::DeleteForegroundAccountCacheTimer(const std::string &cacheKey)
{
    std::lock_guard<std::mutex> timerLock(timerLocks_);
    if (timer_ != nullptr) {
        std::string taskName = std::string(FOREGROUND_ACCOUNT_CACHE_TIMEOUT_TASK) + Crypto::Sha256(cacheKey);
        timer_->DeleteTimer(taskName);
    }
}

void DeviceManagerService::SendForegroundAccountResponse(const std::string &remoteUdid)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    std::vector<int32_t> foregroundUserIds;
    int32_t ret = MultipleUserConnector::GetForegroundUserIds(foregroundUserIds);
    if (ret != DM_OK) {
        LOGE("GetForegroundUserIds failed, ret %{public}d", ret);
        return;
    }
    
    std::vector<std::string> remoteUdids = {remoteUdid};
    std::vector<ForegroundAccountInfo> localAccounts;
    for (const auto &userId : foregroundUserIds) {
        DMAccountInfo dmAccountInfo = MultipleUserConnector::GetDMAccountInfoByUserId(userId);
        if (!dmAccountInfo.accountId.empty()) {
            localAccounts.push_back(ForegroundAccountInfo(userId, dmAccountInfo.accountId));
        }
    }
    SendForegroundAccountBroadcast(remoteUdids, localAccounts, false);
#ifdef CAR_DEVICE_ENABLE
    TriggerForegroundAccountSync();
#endif
#endif
}

void DeviceManagerService::HandleCommonEventBroadCast(const std::vector<UserIdInfo> &remoteUserIdInfos,
    const std::string &remoteUdid, bool isNeedResponse)
{
    LOGI("rmtUdid: %{public}s, rmtUserIds: %{public}s, isNeedResponse: %{public}s,",
        GetAnonyString(remoteUdid).c_str(), GetUserIdInfoList(remoteUserIdInfos).c_str(),
        isNeedResponse ? "true" : "false");

    if (isNeedResponse) {
        HandleResponseRequiredCase(remoteUdid);
    }

    ProcessUserIdsAndServiceInfo(remoteUserIdInfos, remoteUdid);
}

void DeviceManagerService::HandleResponseRequiredCase(const std::string &remoteUdid)
{
    std::vector<int32_t> foregroundUserVec;
    std::vector<int32_t> backgroundUserVec;
    int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec);
    int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec);
    MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec);

    if (retFront != DM_OK || retBack != DM_OK) {
        LOGE("retFront: %{public}d, retBack: %{public}d, frontuserids: %{public}s, backuserids: %{public}s",
            retFront, retBack, GetIntegerList(foregroundUserVec).c_str(),
            GetIntegerList(backgroundUserVec).c_str());
        return;
    }

    LOGE("Send back local frontuserids: %{public}s, backuserids: %{public}s",
        GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str());
    std::vector<std::string> remoteUdids = {remoteUdid};
    SendCommonEventBroadCast(remoteUdids, foregroundUserVec, backgroundUserVec, false);
}

static std::vector<uint32_t> ConvertUserIdInfosToUint32Vector(const std::vector<UserIdInfo> &userIdInfos)
{
    std::vector<uint32_t> userIds;
    for (const auto& u : userIdInfos) {
        userIds.push_back(static_cast<uint32_t>(u.userId));
    }
    return userIds;
}

void DeviceManagerService::ProcessUserIdsAndServiceInfo(const std::vector<UserIdInfo> &remoteUserIdInfos,
    const std::string &remoteUdid)
{
    std::vector<UserIdInfo> foregroundUserIdInfos;
    std::vector<UserIdInfo> backgroundUserIdInfos;
    GetFrontAndBackUserIdInfos(remoteUserIdInfos, foregroundUserIdInfos, backgroundUserIdInfos);
    LOGI("process foreground and background userids");

    auto foregroundUserIds = ConvertUserIdInfosToUint32Vector(foregroundUserIdInfos);
    auto backgroundUserIds = ConvertUserIdInfosToUint32Vector(backgroundUserIdInfos);

    if (!IsDMServiceImplReady()) {
        return;
    }

    std::vector<DmUserRemovedServiceInfo> serviceInfos;
    dmServiceImpl_->HandleCommonEventBroadCast(foregroundUserIds, backgroundUserIds, remoteUdid, serviceInfos);

    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("IsDMServiceAdapterResidentLoad failed.");
        return;
    }

    for (const auto& item : serviceInfos) {
        if (item.isActive) {
            ProcessOnlineServices(item);
        } else {
            ProcessOfflineServices(item);
        }
    }
}

void DeviceManagerService::ProcessOnlineServices(const DmUserRemovedServiceInfo &item)
{
    for (size_t i = 0; i < item.serviceIds.size(); i++) {
        ServiceStateBindParameter bindParam = {
            item.localTokenId,
            item.localPkgName,
            item.bindType,
            item.peerUdid,
            item.peerUserId,
            item.serviceIds[i]
        };
        dmServiceImplExtResident_->BindServiceOnline(bindParam);
    }
}

void DeviceManagerService::ProcessOfflineServices(const DmUserRemovedServiceInfo &item)
{
    for (size_t i = 0; i < item.serviceIds.size(); i++) {
        DistributedDeviceProfile::ServiceInfo dpServiceInfo;
        DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
            item.peerUdid, item.serviceIds[i], dpServiceInfo);
        dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
            item.bindType, item.peerUdid, dpServiceInfo);
    }
}

void DeviceManagerService::ProcessCommonUserStatusEventInner(std::vector<DmUserRemovedServiceInfo> &serviceInfos)
{
    for (auto &item : serviceInfos) {
        if (item.isActive == true) {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                ServiceStateBindParameter bindParam = {
                    item.localTokenId,
                    item.localPkgName,
                    item.bindType,
                    item.peerUdid,
                    item.peerUserId,
                    item.serviceIds[i]
                };
                dmServiceImplExtResident_->BindServiceOnline(bindParam);
            }
        } else {
            for (size_t i = 0; i < item.serviceIds.size(); i++) {
                DistributedDeviceProfile::ServiceInfo dpServiceInfo;
                DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                    item.peerUdid, item.serviceIds[i], dpServiceInfo);
                dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                    item.bindType, item.peerUdid, dpServiceInfo);
            }
        }
    }
}

void DeviceManagerService::ProcessCommonUserStatusEvent(const std::vector<uint32_t> &foregroundUserIds,
    const std::vector<uint32_t> &backgroundUserIds, const std::string &remoteUdid)
{
    LOGI("foregroundUserIds: %{public}s, backgroundUserIds: %{public}s, remoteUdid: %{public}s",
        GetIntegerList<uint32_t>(foregroundUserIds).c_str(), GetIntegerList<uint32_t>(backgroundUserIds).c_str(),
        GetAnonyString(remoteUdid).c_str());

    {
        std::lock_guard<std::mutex> autoLock(timerLocks_);
        if (timer_ != nullptr) {
            timer_->DeleteTimer(std::string(ACCOUNT_COMMON_EVENT_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid));
        }
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    if (IsDMServiceImplReady()) {
        std::vector<DmUserRemovedServiceInfo> serviceInfos;
        dmServiceImpl_->HandleCommonEventBroadCast(foregroundUserIds, backgroundUserIds, remoteUdid, serviceInfos);
        if (!IsDMServiceAdapterResidentLoad()) {
            LOGE("IsDMServiceAdapterResidentLoad failed.");
            return;
        }
        // user online&offline
        ProcessCommonUserStatusEventInner(serviceInfos);
    }
}

void DeviceManagerService::ScreenCommonEventCallback(std::string commonEventType)
{
    if (IsDMServiceAdapterResidentLoad()) {
        bool isLock = commonEventType == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED;
        dmServiceImplExtResident_->HandleScreenLockEvent(isLock);
    } else {
        LOGE("dmServiceImplExtResident not init or init failed.");
    }
    if (!IsDMImplSoLoaded()) {
        LOGE("instance not init or init failed.");
        return;
    }
    dmServiceImpl_->ScreenCommonEventCallback(commonEventType);
}
#endif

void DeviceManagerService::HandleDeviceNotTrust(const std::string &msg)
{
    LOGI("Start.");
    if (msg.empty()) {
        LOGE("msg is empty.");
        return;
    }
    JsonObject msgJsonObj(msg);
    if (msgJsonObj.IsDiscarded()) {
        LOGE("msg prase error.");
        return;
    }
    if (!IsString(msgJsonObj, NETWORKID)) {
        LOGE("msg not contain networkId.");
        return;
    }
    std::string networkId = msgJsonObj[NETWORKID].Get<std::string>();
    std::string udid = "";
    SoftbusCache::GetInstance().GetUdidFromCache(networkId.c_str(), udid);
    LOGI("NetworkId: %{public}s, udid: %{public}s.",
        GetAnonyString(networkId).c_str(), GetAnonyString(udid).c_str());
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->HandleDeviceNotTrust(udid);
    }
    if (IsDMServiceAdapterResidentLoad()) {
        dmServiceImplExtResident_->HandleDeviceNotTrust(udid);
    }
    return;
}

int32_t DeviceManagerService::SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnSetDnPolicy(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    auto policyStrategyIter = policy.find(PARAM_KEY_POLICY_STRATEGY_FOR_BLE);
    if (policyStrategyIter == policy.end()) {
        LOGE("Invalid parameter, DM_POLICY_STRATEGY_FOR_BLE is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    auto timeOutIter = policy.find(PARAM_KEY_POLICY_TIME_OUT);
    if (timeOutIter == policy.end()) {
        LOGE("Invalid parameter, DM_POLICY_TIMEOUT is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsNumberString(policyStrategyIter->second)) {
        LOGE("Invalid parameter, DM_POLICY_STRATEGY_FOR_BLE is not number.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsNumberString(timeOutIter->second)) {
        LOGE("Invalid parameter, DM_POLICY_TIMEOUT is not number.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    int32_t timeOut = std::atoi(timeOutIter->second.c_str());
    if (timeOut < HEARTBEAT_DELAY_TIME_MIN || timeOut > HEARTBEAT_DELAY_TIME_MAX) {
        LOGE("Invalid parameter, DM_POLICY_TIMEOUT is out of range.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    int32_t policyStrategy = std::atoi(policyStrategyIter->second.c_str());
    LOGD("strategy: %{public}d, timeOut: %{public}d", policyStrategy, timeOut);
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->SetDnPolicy(policyStrategy, timeOut);
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
DM_EXPORT void DeviceManagerService::ConvertUdidHashToAnoyDeviceId(
    DmDeviceInfo &deviceInfo)
{
    std::string udidHashTemp = "";
    if (ConvertUdidHashToAnoyDeviceId(deviceInfo.deviceId, udidHashTemp) == DM_OK) {
        if (memset_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, 0, DM_MAX_DEVICE_ID_LEN) != DM_OK) {
            LOGE("memset_s failed.");
            return;
        }
        if (memcpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, udidHashTemp.c_str(), udidHashTemp.length()) != 0) {
            LOGE("get deviceId: %{public}s failed", GetAnonyString(udidHashTemp).c_str());
            return;
        }
    }
}

DM_EXPORT int32_t DeviceManagerService::ConvertUdidHashToAnoyDeviceId(
    const std::string &udidHash, std::string &result)
{
    std::string appId = AppManager::GetInstance().GetAppId();
    if (appId.empty()) {
        LOGD("GetAppId failed");
        return ERR_DM_FAILED;
    }
    DmKVValue kvValue;
    int32_t ret = Crypto::ConvertUdidHashToAnoyAndSave(appId, udidHash, kvValue);
    if (ret != DM_OK) {
        return ERR_DM_FAILED;
    }
    result = kvValue.anoyDeviceId;
    return DM_OK;
}

DM_EXPORT int32_t DeviceManagerService::GetUdidHashByAnoyDeviceId(
    const std::string &anoyDeviceId, std::string &udidHash)
{
    LOGI("anoyDeviceId %{public}s.", GetAnonyString(anoyDeviceId).c_str());
    DmKVValue kvValue;
    if (KVAdapterManager::GetInstance().Get(anoyDeviceId, kvValue) != DM_OK) {
        LOGD("Get kv value from DB failed");
        return ERR_DM_FAILED;
    }
    udidHash = kvValue.udidHash;
    LOGI("udidHash %{public}s.", GetAnonyString(udidHash).c_str());
    return DM_OK;
}

void DeviceManagerService::SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId, int32_t bindLevel)
{
    LOGI("TokenId %{public}s, bindLevel %{public}d, userId %{public}d.", GetAnonyInt32(tokenId).c_str(),
        bindLevel, userId);
    if (static_cast<uint32_t>(bindLevel) == USER) {
        SendDeviceUnBindBroadCast(peerUdids, userId, tokenId);
        return;
    }
    if (static_cast<uint32_t>(bindLevel) == APP) {
        SendAppUnBindBroadCast(peerUdids, userId, tokenId);
        return;
    }
    if (static_cast<uint32_t>(bindLevel) == SERVICE) {
        SendServiceUnBindBroadCast(peerUdids, userId, tokenId);
        return;
    }
}

void DeviceManagerService::SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId, int32_t bindLevel, uint64_t peerTokenId)
{
    if (static_cast<uint32_t>(bindLevel) == USER) {
        SendDeviceUnBindBroadCast(peerUdids, userId, tokenId);
        return;
    }
    if (static_cast<uint32_t>(bindLevel) == APP) {
        SendAppUnBindBroadCast(peerUdids, userId, tokenId, peerTokenId);
        return;
    }
    if (static_cast<uint32_t>(bindLevel) == SERVICE) {
        SendServiceUnBindBroadCast(peerUdids, userId, tokenId);
        return;
    }
}

void DeviceManagerService::SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId)
{
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::DEVICE_UNBIND;
    msg.userId = static_cast<uint32_t>(userId);
    msg.peerUdids = peerUdids;
    msg.tokenId = tokenId;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::SendUnRegServiceBroadCast(const std::vector<std::string> &peerUdids,
    int32_t localUserId, int64_t serviceId)
{
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::SERVICEINFO_UNREGISTER;
    msg.userId = static_cast<uint32_t>(localUserId);
    msg.peerUdids = peerUdids;
    msg.serviceId = serviceId;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

int32_t DeviceManagerService::CalculateBroadCastDelayTime()
{
    int64_t timeDiff = 0;
    int32_t delayTime = 0;
    int64_t currentTime =
        std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
    {
        std::lock_guard<std::mutex> lock(broadCastLock_);
        if (SendLastBroadCastTime_ == 0) {
            SendLastBroadCastTime_ = currentTime;
        }
        timeDiff = currentTime - SendLastBroadCastTime_;
        delayTime = SEND_DELAY_MAX_TIME - timeDiff + lastDelayTime_;
        if (delayTime < SEND_DELAY_MIN_TIME || delayTime == SEND_DELAY_MAX_TIME || delayTime > SEND_LIMIT_TIME) {
            delayTime = SEND_DELAY_MIN_TIME;
        }
        SendLastBroadCastTime_ = currentTime;
        lastDelayTime_ = delayTime;
    }
    return delayTime;
}

void DeviceManagerService::SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId)
{
    int32_t delayTime = CalculateBroadCastDelayTime();
    std::function<void()> task = [=]() {
        LOGI("Start.");
        RelationShipChangeMsg msg;
        msg.type = RelationShipChangeType::APP_UNBIND;
        msg.userId = static_cast<uint32_t>(userId);
        msg.peerUdids = peerUdids;
        msg.tokenId = tokenId;
        std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
        CHECK_NULL_VOID(softbusListener_);
        softbusListener_->SendAclChangedBroadcast(broadCastMsg);
    };
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    ffrt::submit(task,
        ffrt::task_attr().name(SEND_APP_UN_BIND_BROAD_CAST_TASK).delay(delayTime * DELAY_TIME_SEC_CONVERSION));
#else
    std::thread(task, ffrt::task_attr().delay(delayTime * DELAY_TIME_SEC_CONVERSION)).detach();
#endif
}

void DeviceManagerService::SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId, uint64_t peerTokenId)
{
    int32_t delayTime = CalculateBroadCastDelayTime();
    std::function<void()> task = [=]() {
        LOGI("Start.");
        RelationShipChangeMsg msg;
        msg.type = RelationShipChangeType::APP_UNBIND;
        msg.userId = static_cast<uint32_t>(userId);
        msg.peerUdids = peerUdids;
        msg.tokenId = tokenId;
        msg.peerTokenId = peerTokenId;
        std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
        CHECK_NULL_VOID(softbusListener_);
        softbusListener_->SendAclChangedBroadcast(broadCastMsg);
    };
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    ffrt::submit(task,
        ffrt::task_attr().name(SEND_APP_UN_BIND_BROAD_CAST_TASK).delay(delayTime * DELAY_TIME_SEC_CONVERSION));
#else
    std::thread(task, ffrt::task_attr().delay(delayTime * DELAY_TIME_SEC_CONVERSION)).detach();
#endif
}

void DeviceManagerService::SendAppUnInstallBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId)
{
    int32_t delayTime = CalculateBroadCastDelayTime();
    std::function<void()> task = [=]() {
        LOGI("Start.");
        RelationShipChangeMsg msg;
        msg.type = RelationShipChangeType::APP_UNINSTALL;
        msg.userId = static_cast<uint32_t>(userId);
        msg.peerUdids = peerUdids;
        msg.tokenId = tokenId;
        std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
        CHECK_NULL_VOID(softbusListener_);
        softbusListener_->SendAclChangedBroadcast(broadCastMsg);
    };
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    ffrt::submit(task,
        ffrt::task_attr().name(SEND_APP_UN_INSTALL_BROAD_CAST_TASK).delay(delayTime * DELAY_TIME_SEC_CONVERSION));
#else
    std::thread(task, ffrt::task_attr().delay(delayTime * DELAY_TIME_SEC_CONVERSION)).detach();
#endif
}

void DeviceManagerService::SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId,
    uint64_t tokenId)
{
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::SERVICE_UNBIND;
    msg.userId = static_cast<uint32_t>(userId);
    msg.peerUdids = peerUdids;
    msg.tokenId = tokenId;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::HandleCredentialDeleted(const char *credId, const char *credInfo)
{
    LOGI("start.");
    if (credId == nullptr || credInfo == nullptr) {
        LOGE("credId or credInfo is nullptr.");
        return;
    }
    std::string localUdid = GetLocalDeviceUdid();
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return;
    }
    std::string remoteUdid = "";
    bool isSendBroadCast = false;
    dmServiceImpl_->HandleCredentialDeleted(credId, credInfo, localUdid, remoteUdid, isSendBroadCast);
    if (remoteUdid.empty()) {
        LOGE("remoteUdid is empty.");
        return;
    }
    if (!isSendBroadCast) {
        LOGI("not need to send broadcast.");
        return;
    }
    std::vector<std::string> peerUdids;
    peerUdids.emplace_back(remoteUdid);
    SendShareTypeUnBindBroadCast(credId, MultipleUserConnector::GetCurrentAccountUserID(), peerUdids);
    return;
}

void DeviceManagerService::HandleDeviceTrustedChange(const std::string &msg)
{
    if (!IsMsgEmptyAndDMServiceImplReady(msg)) {
        return;
    }
    RelationShipChangeMsg relationShipMsg = ReleationShipSyncMgr::GetInstance().ParseTrustRelationShipChange(msg);
    LOGI("Receive trust change msg: %{public}s", relationShipMsg.ToString().c_str());
    if (!ReleationShipSyncMgr::GetInstance().IsNewBroadCastId(relationShipMsg)) {
        LOGI("is not new broadcast");
        return;
    }
    bool ret = ParseRelationShipChangeType(relationShipMsg);
    if (!ret) {
        LOGI("ParseRelationShipChangeType failed");
        return;
    }
    return;
}

void DeviceManagerService::HandleAccountLogoutEvent(int32_t userId, const std::string &accountId,
    const std::string &peerUdid)
{
    if (!IsDMServiceImplReady()) {
        LOGE("Imp instance not init or init failed.");
        return;
    }
    std::vector<DmUserRemovedServiceInfo> serviceInfos;
    dmServiceImpl_->HandleAccountLogoutEvent(userId, accountId, peerUdid, serviceInfos);
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("IsDMServiceAdapterResidentLoad failed.");
        return;
    }
    std::string localUdid = GetLocalDeviceUdid();
    for (auto &item : serviceInfos) {
        for (size_t i = 0; i < item.serviceIds.size(); i++) {
            DistributedDeviceProfile::ServiceInfo dpServiceInfo;
            DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                item.peerUdid, item.serviceIds[i], dpServiceInfo);
            dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                item.bindType, item.peerUdid, dpServiceInfo);
            dmServiceImplExtResident_->DeleteServiceInfoForAccountEvent(item.peerUserId, item.peerUdid,
                localUdid);
        }
    }
}

bool DeviceManagerService::ParseRelationShipChangeType(const RelationShipChangeMsg &relationShipMsg)
{
    switch (relationShipMsg.type) {
        case RelationShipChangeType::ACCOUNT_LOGOUT:
            ffrt::submit([=]() {
                HandleAccountLogoutEvent(relationShipMsg.userId, relationShipMsg.accountId,
                    relationShipMsg.peerUdid);
            },
                ffrt::task_attr().name(HANDLE_ACCOUNT_LOGOUT_EVENT_TASK));
            break;
        case RelationShipChangeType::DEVICE_UNBIND:
            dmServiceImpl_->HandleDevUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid,
                static_cast<int32_t>(relationShipMsg.tokenId));
            break;
        case RelationShipChangeType::APP_UNBIND:
            ParseAppUnBindRelationShip(relationShipMsg);
            break;
        case RelationShipChangeType::SERVICE_UNBIND:
            dmServiceImpl_->HandleServiceUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid,
                static_cast<int32_t>(relationShipMsg.tokenId));
            break;
        case RelationShipChangeType::SYNC_USERID:
            if (relationShipMsg.isNewEvent) {
                ffrt::submit([=]() {
                    HandleCommonEventBroadCast(relationShipMsg.userIdInfos,
                        relationShipMsg.peerUdid, relationShipMsg.syncUserIdFlag);
                },
                    ffrt::task_attr().name(HANDLE_COMMON_EVENT_BROAD_CAST_TASK));
            } else {
                ffrt::submit([=]() {
                    HandleUserIdsBroadCast(relationShipMsg.userIdInfos,
                        relationShipMsg.peerUdid, relationShipMsg.syncUserIdFlag);
                },
                    ffrt::task_attr().name(HANDLE_USER_IDS_BROAD_CAST_TASK));
            }
            break;
        default:
            return ParseRelationShipChangeTypeTwo(relationShipMsg);
    }
    return true;
}

bool DeviceManagerService::ParseRelationShipChangeTypeTwo(const RelationShipChangeMsg &relationShipMsg)
{
    switch (relationShipMsg.type) {
        case RelationShipChangeType::DEL_USER:
            ffrt::submit([=]() {
                HandleRemoteUserRemoved(relationShipMsg.userId, relationShipMsg.peerUdid);
            },
                ffrt::task_attr().name(HANDLE_REMOTE_USER_REMOVED_TASK));
            break;
        case RelationShipChangeType::STOP_USER:
            HandleUserStopBroadCast(relationShipMsg.userId, relationShipMsg.peerUdid);
            break;
        case RelationShipChangeType::SHARE_UNBIND:
            HandleShareUnbindBroadCast(relationShipMsg.userId, relationShipMsg.credId);
            break;
        case RelationShipChangeType::APP_UNINSTALL:
            ProcessUninstApp(relationShipMsg.userId, static_cast<int32_t>(relationShipMsg.tokenId));
            break;
        case RelationShipChangeType::SERVICEINFO_UNREGISTER:
            HandleServiceUnRegEvent(relationShipMsg.peerUdid, relationShipMsg.userId, relationShipMsg.serviceId);
            break;
        case RelationShipChangeType::ACCOUNT_EVENT:
            HandleAccountEventBroadCast(relationShipMsg);
            break;
        default:
            LOGI("Dm have not this event type.");
            return false;
    }
    return true;
}

void DeviceManagerService::HandleRemoteUserRemoved(int32_t userId, const std::string &remoteUdid)
{
    std::vector<DmUserRemovedServiceInfo> serviceInfos;
    dmServiceImpl_->HandleRemoteUserRemoved(userId, remoteUdid, serviceInfos);
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("IsDMServiceAdapterResidentLoad failed.");
        return;
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    for (auto &item : serviceInfos) {
        for (size_t i = 0; i < item.serviceIds.size(); i++) {
            DistributedDeviceProfile::ServiceInfo dpServiceInfo;
            DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                item.peerUdid, item.serviceIds[i], dpServiceInfo);
            dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                item.bindType, item.peerUdid, dpServiceInfo);
            dmServiceImplExtResident_->DeleteServiceInfoForAccountEvent(item.peerUserId, item.peerUdid,
                localUdid);
        }
    }
}

void DeviceManagerService::ParseAppUnBindRelationShip(const RelationShipChangeMsg &relationShipMsg)
{
    if (relationShipMsg.peerTokenId != 0) {
        dmServiceImpl_->HandleAppUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid,
            static_cast<int32_t>(relationShipMsg.peerTokenId),
            static_cast<int32_t>(relationShipMsg.tokenId));
    } else {
        dmServiceImpl_->HandleAppUnBindEvent(relationShipMsg.userId, relationShipMsg.peerUdid,
            static_cast<int32_t>(relationShipMsg.tokenId));
    }
}

bool DeviceManagerService::IsMsgEmptyAndDMServiceImplReady(const std::string &msg)
{
    if (msg.empty()) {
        LOGE("Msg is empty.");
        return false;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("Imp instance not init or init failed.");
        return false;
    }
    return true;
}

void DeviceManagerService::HandleShareUnbindBroadCast(const int32_t userId, const std::string &credId)
{
    LOGI("start.");
    if (credId == "") {
        LOGE("credId is null.");
        return;
    }
    std::string localUdid = GetLocalDeviceUdid();
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->HandleShareUnbindBroadCast(credId, userId, localUdid);
    }
    return;
}

int32_t DeviceManagerService::ParseCheckSumMsg(const std::string &msg, std::string &networkId, uint32_t &discoveryType,
    bool &isChange)
{
    JsonObject msgJsonObj(msg);
    if (msgJsonObj.IsDiscarded()) {
        LOGE("msg prase error.");
        return ERR_DM_FAILED;
    }
    if (!IsString(msgJsonObj, USERID_CHECKSUM_NETWORKID_KEY)) {
        LOGE("msg not contain networkId.");
        return ERR_DM_FAILED;
    }
    if (!IsUint32(msgJsonObj, USERID_CHECKSUM_DISCOVER_TYPE_KEY)) {
        LOGE("msg not contain discoveryType.");
        return ERR_DM_FAILED;
    }
    if (!IsBool(msgJsonObj, USERID_CHECKSUM_ISCHANGE_KEY)) {
        LOGE("msg not contain ischange.");
        return ERR_DM_FAILED;
    }
    networkId = msgJsonObj[USERID_CHECKSUM_NETWORKID_KEY].Get<std::string>();
    discoveryType = msgJsonObj[USERID_CHECKSUM_DISCOVER_TYPE_KEY].Get<uint32_t>();
    isChange = msgJsonObj[USERID_CHECKSUM_ISCHANGE_KEY].Get<bool>();
    return DM_OK;
}

void DeviceManagerService::ProcessCheckSumByWifi(std::string networkId, std::vector<int32_t> foregroundUserIds,
    std::vector<int32_t> backgroundUserIds)
{
    if (localNetWorkId_ == "") {
        DmDeviceInfo deviceInfo;
        SoftbusCache::GetInstance().GetLocalDeviceInfo(deviceInfo);
        localNetWorkId_ = std::string(deviceInfo.networkId);
    }
    if (localNetWorkId_ >= networkId) {
        LOGI("Local networkid big than remote, no need begin req");
        return;
    }
    // use connection to exchange foreground/background userid
    std::vector<uint32_t> foregroundUserIdsUInt;
    for (auto const &u : foregroundUserIds) {
        foregroundUserIdsUInt.push_back(static_cast<uint32_t>(u));
    }
    std::vector<uint32_t> backgroundUserIdsUInt;
    for (auto const &u : backgroundUserIds) {
        backgroundUserIdsUInt.push_back(static_cast<uint32_t>(u));
    }
    CHECK_NULL_VOID(DMCommTool::GetInstance());
    DMCommTool::GetInstance()->SendUserIds(networkId, foregroundUserIdsUInt, backgroundUserIdsUInt);
}

void DeviceManagerService::ProcessCheckSumByBT(std::string networkId, std::vector<int32_t> foregroundUserIds,
    std::vector<int32_t> backgroundUserIds)
{
    LOGI("Try send brodcast to exchange foreground userid");
    std::string udid = "";
    SoftbusCache::GetInstance().GetUdidFromCache(networkId.c_str(), udid);
    if (udid.empty()) {
        LOGE("Can not get udid for networkid: %{public}s", GetAnonyString(networkId).c_str());
        return;
    }

    std::vector<std::string> peerUdids = { udid };
    if (!foregroundUserIds.empty()) {
        LOGI("Send local foreground and background userids");
        SendUserIdsBroadCast(peerUdids, foregroundUserIds, backgroundUserIds, true);
    } else {
        LOGE("local foreground userids empty");
    }
}

void DeviceManagerService::HandleUserIdCheckSumChange(const std::string &msg)
{
    if (msg.empty()) {
        LOGE("Msg is empty.");
        return;
    }
    LOGI("handle user trust change, msg: %{public}s", GetAnonyString(msg).c_str());
    std::string remoteNetworkId = "";
    uint32_t discoveryType = 0;
    bool isPeerUserIdChanged = true;
    int32_t ret = ParseCheckSumMsg(msg, remoteNetworkId, discoveryType, isPeerUserIdChanged);
    if (ret != DM_OK) {
        LOGE("Parse checksum msg error");
        return;
    }
    if (!isPeerUserIdChanged) {
        LOGI("Peer foreground userId not change.");
        return;
    }
    std::vector<int32_t> foregroundUserIds;
    ret = MultipleUserConnector::GetForegroundUserIds(foregroundUserIds);
    if (ret != DM_OK || foregroundUserIds.empty()) {
        LOGE("Get foreground userids failed, ret: %{public}d", ret);
        return;
    }

    std::vector<int32_t> backgroundUserIds;
    ret = MultipleUserConnector::GetBackgroundUserIds(backgroundUserIds);
    if (ret != DM_OK || backgroundUserIds.empty()) {
        LOGI("Can not get background userids, ret: %{public}d, background userid num: %{public}d",
            ret, static_cast<int32_t>(backgroundUserIds.size()));
    }
    uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
    if ((discoveryType & addrTypeMask) != 0x0) {
        ProcessCheckSumByBT(remoteNetworkId, foregroundUserIds, backgroundUserIds);
    } else {
        ProcessCheckSumByWifi(remoteNetworkId, foregroundUserIds, backgroundUserIds);
    }
#ifdef CAR_DEVICE_ENABLE
    TriggerForegroundAccountSync();
#endif
}
#endif

void DeviceManagerService::ClearDiscoveryCache(const ProcessInfo &processInfo)
{
    LOGI("PkgName: %{public}s, userId: %{public}d", processInfo.pkgName.c_str(), processInfo.userId);
    CHECK_NULL_VOID(discoveryMgr_);
    discoveryMgr_->ClearDiscoveryCache(processInfo);
}

void DeviceManagerService::HandleDeviceScreenStatusChange(DmDeviceInfo &deviceInfo)
{
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->HandleDeviceScreenStatusChange(deviceInfo);
    }
}

int32_t DeviceManagerService::GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
    int32_t &screenStatus)
{
    LOGI("Begin pkgName: %{public}s, networkId: %{public}s", pkgName.c_str(), GetAnonyString(networkId).c_str());
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
    CHECK_EMPTY_RETURN(networkId, ERR_DM_INPUT_PARA_INVALID);
    if (CheckConstraintEnabledByNetworkId(networkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetDeviceScreenStatus.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetDeviceScreenStatus(networkId.c_str(), screenStatus);
    if (ret != DM_OK) {
        LOGE("ret = %{public}d", ret);
        return ret;
    }
    return DM_OK;
}

int32_t DeviceManagerService::GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid,
                                                 std::string &networkId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call GetNetworkIdByUdid.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || udid.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s, udid: %{public}s", pkgName.c_str(), GetAnonyString(udid).c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    return SoftbusListener::GetNetworkIdByUdid(udid, networkId);
}

void DeviceManagerService::SubscribePackageCommonEvent()
{
    LOGI("Start");
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (packageCommonEventManager_ == nullptr) {
        packageCommonEventManager_ = std::make_shared<DmPackageCommonEventManager>();
    }
    PackageEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
        if (!DeviceProfileConnector::GetInstance().CheckAccessControlProfileByTokenId(arg3)) {
            return;
        }
        int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
        NotifyRemoteUninstallApp(userId, arg3);
        if (IsDMServiceImplReady()) {
            dmServiceImpl_->ProcessAppUnintall(arg1, arg3);
        }
        KVAdapterManager::GetInstance().AppUninstall(arg1);
    };
    std::vector<std::string> commonEventVec;
    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
    commonEventVec.emplace_back(CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
    if (packageCommonEventManager_->SubscribePackageCommonEvent(commonEventVec, callback)) {
        LOGI("Success");
    }
#endif
}

int32_t DeviceManagerService::SyncLocalAclListProcess(const DevUserInfo &localDevUserInfo,
    const DevUserInfo &remoteDevUserInfo, std::string remoteAclList, bool isDelImmediately)
{
    if (IsDMServiceImplReady()) {
        return dmServiceImpl_->SyncLocalAclListProcess(localDevUserInfo, remoteDevUserInfo,
            remoteAclList, isDelImmediately);
    }
    return ERR_DM_FAILED;
}

int32_t DeviceManagerService::GetAclListHash(const DevUserInfo &localDevUserInfo,
    const DevUserInfo &remoteDevUserInfo, std::string &aclList)
{
    if (IsDMServiceImplReady()) {
        return dmServiceImpl_->GetAclListHash(localDevUserInfo, remoteDevUserInfo, aclList);
    }
    return ERR_DM_FAILED;
}

void DeviceManagerService::HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId,
                                                      int32_t errcode)
{
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->HandleCredentialAuthStatus(deviceList, deviceTypeId, errcode);
    }
}

void DeviceManagerService::RemoveNotifyRecord(const ProcessInfo &processInfo)
{
    LOGI("start");
    CHECK_NULL_VOID(listener_);
    listener_->OnProcessRemove(processInfo);
}

#ifdef CAR_DEVICE_ENABLE
int32_t DeviceManagerService::RegDevStateCallbackToService(const std::string &pkgName, ProcessInfo processInfo)
{
    if (AppManager::GetInstance().IsSystemSA()) {
        int32_t displayId = 0;
        processInfo.userId = MultipleUserConnector::GetUserIdByDisplayId(displayId);
        processInfo.accountId = MultipleUserConnector::GetAccountIdByUserId(processInfo.userId);
    } else {
        processInfo.accountId = MultipleUserConnector::GetAccountIdByUserId(processInfo.userId);
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    CHECK_NULL_RETURN(listener_, ERR_DM_POINT_NULL);
    std::vector<DmDeviceInfo> deviceList;
    GetTrustedDeviceList(processInfo, deviceList);
    if (deviceList.size() == 0) {
        LOGI("deviceList not exist");
        return DM_OK;
    }
    listener_->OnDevStateCallbackAdd(processInfo, deviceList);
    if (PermissionManager::GetInstance().CheckOnReadyRetrospectiveNotificationBlackList()) {
        return DM_OK;
    }
    std::vector<DmDeviceInfo> readyDeviceList;
    CHECK_NULL_RETURN(dmServiceImpl_, ERR_DM_POINT_NULL);
    dmServiceImpl_->GetNotifyEventInfos(readyDeviceList);
    if (readyDeviceList.size() == 0) {
        return DM_OK;
    }
    listener_->OnDevDbReadyCallbackAdd(processInfo, readyDeviceList);
#else
    (void)pkgName;
#endif
    return DM_OK;
}
#endif

int32_t DeviceManagerService::RegDevStateCallbackToService(const std::string &pkgName)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    CHECK_NULL_RETURN(listener_, ERR_DM_POINT_NULL);
    std::vector<DmDeviceInfo> deviceList;
    GetTrustedDeviceList(pkgName, deviceList);
    if (deviceList.size() == 0) {
        return DM_OK;
    }
    int32_t userId = -1;
    MultipleUserConnector::GetCallerUserId(userId);
    ProcessInfo processInfo;
    processInfo.pkgName = pkgName;
    processInfo.userId = userId;
    processInfo.tokenId = IPCSkeleton::GetCallingTokenID();
    listener_->OnDevStateCallbackAdd(processInfo, deviceList);
    if (PermissionManager::GetInstance().CheckOnReadyRetrospectiveNotificationBlackList()) {
        return DM_OK;
    }
    std::vector<DmDeviceInfo> readyDeviceList;
    CHECK_NULL_RETURN(dmServiceImpl_, ERR_DM_POINT_NULL);
    dmServiceImpl_->GetNotifyEventInfos(readyDeviceList);
    if (readyDeviceList.size() == 0) {
        return DM_OK;
    }
    listener_->OnDevDbReadyCallbackAdd(processInfo, readyDeviceList);
#else
    (void)pkgName;
#endif
    return DM_OK;
}

#ifdef CAR_DEVICE_ENABLE
int32_t DeviceManagerService::GetTrustedDeviceList(ProcessInfo processInfo, std::vector<DmDeviceInfo> &deviceList)
{
    LOGI("pkgName: %{public}s, userId: %{public}d, accountId: %{public}s",
        processInfo.pkgName.c_str(), processInfo.userId, GetAnonyString(processInfo.accountId).c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
#endif
    if (processInfo.pkgName.empty() || processInfo.pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    std::vector<DmDeviceInfo> onlineDeviceList;
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetTrustedDeviceList(onlineDeviceList);
    if (ret != DM_OK) {
        LOGE("GetTrustedDeviceList failed");
        return ret;
    }
    if (onlineDeviceList.empty() || !IsDMServiceImplReady()) {
        return DM_OK;
    }

    dmServiceImpl_->DeleteAlwaysAllowTimeOut();
    std::string pkgName = PermissionManager::GetInstance().CheckWhiteListSystemSA(processInfo.pkgName) ?
        std::string(ALL_PKGNAME) : processInfo.pkgName;
    std::unordered_map<PeerDevInfo, DmAuthForm, PeerDevInfoHash> udidMap =
        dmServiceImpl_->GetAppTrustDeviceIdList(pkgName, processInfo);
    for (auto item : onlineDeviceList) {
        std::string peerUdid = "";
        SoftbusListener::GetUdidByNetworkId(item.networkId, peerUdid);
        for (const auto &mapItem : udidMap) {
            if (mapItem.first.deviceId == peerUdid) {
                item.authForm = mapItem.second;
                deviceList.push_back(item);
            }
        }
    }
    return DM_OK;
}
#endif

int32_t DeviceManagerService::GetTrustedDeviceList(const std::string &pkgName, std::vector<DmDeviceInfo> &deviceList)
{
    LOGI("Begin for pkgName = %{public}s.", pkgName.c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
#endif
    if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    std::vector<DmDeviceInfo> onlineDeviceList;
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->GetTrustedDeviceList(onlineDeviceList);
    if (ret != DM_OK) {
        LOGE("GetTrustedDeviceList failed");
        return ret;
    }
    if (!onlineDeviceList.empty() && IsDMServiceImplReady()) {
        dmServiceImpl_->DeleteAlwaysAllowTimeOut();
        std::unordered_map<std::string, DmAuthForm> udidMap;
        if (PermissionManager::GetInstance().CheckWhiteListSystemSA(pkgName)) {
            udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(std::string(ALL_PKGNAME));
        } else {
            udidMap = dmServiceImpl_->GetAppTrustDeviceIdList(pkgName);
        }
        for (auto item : onlineDeviceList) {
            std::string udid = "";
            SoftbusListener::GetUdidByNetworkId(item.networkId, udid);
            if (udidMap.find(udid) != udidMap.end()) {
                item.authForm = udidMap[udid];
                deviceList.push_back(item);
            }
        }
    }
    return DM_OK;
}

void DeviceManagerService::HandleDeviceUnBind(const char *peerUdid, const GroupInformation &groupInfo)
{
    LOGI("start.");
    std::string localUdid = GetLocalDeviceUdid();
    if (IsDMServiceImplReady()) {
        dmServiceImpl_->HandleDeviceUnBind(groupInfo.groupType, std::string(peerUdid),
            localUdid, groupInfo.userId, groupInfo.osAccountId);
    }
    return;
}

int32_t DeviceManagerService::GetAnonyLocalUdid(const std::string &pkgName, std::string &anonyUdid)
{
    (void) pkgName;
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call GetAnonyLocalUdid.");
        return ERR_DM_NO_PERMISSION;
    }
    std::string udid = DmRadarHelper::GetInstance().GetAnonyLocalUdid();
    if (udid.empty()) {
        LOGE("Anony local udid is empty.");
        return ERR_DM_FAILED;
    }
    anonyUdid = udid;
    return DM_OK;
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))

void DeviceManagerService::NotifyRemoteUninstallApp(int32_t userId, int32_t tokenId)
{
    LOGI("userId: %{public}s, tokenId: %{public}s",
        GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str());
    std::vector<std::string> peerUdids;
    int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID();
    if (IsDMServiceImplReady()) {
        peerUdids = dmServiceImpl_->GetDeviceIdByUserIdAndTokenId(currentUserId, tokenId);
    }
    if (peerUdids.empty()) {
        LOGE("peerUdids is empty");
        return;
    }
    if (softbusListener_ == nullptr) {
        LOGE("softbusListener_ is null");
        return;
    }
    std::vector<std::string> bleUdids;
    std::map<std::string, std::string> wifiDevices;
    for (const auto &udid : peerUdids) {
        std::string netWorkId = "";
        SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId);
        if (netWorkId.empty()) {
            LOGE("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        int32_t networkType = 0;
        int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
        if (ret != DM_OK || networkType <= 0) {
            LOGE("get networkType failed: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
        if ((static_cast<uint32_t>(networkType) & addrTypeMask) != 0x0) {
            bleUdids.push_back(udid);
        } else {
            wifiDevices.insert(std::pair<std::string, std::string>(udid, netWorkId));
        }
    }
    if (!bleUdids.empty()) {
        SendAppUnInstallBroadCast(bleUdids, userId, tokenId);
    }
    if (!wifiDevices.empty()) {
        NotifyRemoteUninstallAppByWifi(userId, tokenId, wifiDevices);
    }
}


void DeviceManagerService::NotifyRemoteUninstallAppByWifi(int32_t userId, int32_t tokenId,
    const std::map<std::string, std::string> &wifiDevices)
{
    LOGI("userId: %{public}s, tokenId: %{public}s",
        GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str());
    for (const auto &it : wifiDevices) {
        int32_t result = SendUninstAppByWifi(userId, tokenId, it.second);
        if (result != DM_OK) {
            LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str());
            continue;
        }
        std::lock_guard<std::mutex> autoLock(timerLocks_);
        if (timer_ == nullptr) {
            timer_ = std::make_shared<DmTimer>();
        }
        std::string udid = it.first;
        std::string networkId = it.second;
        timer_->StartTimer(std::string(APP_UNINSTALL_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid),
            USER_SWITCH_BY_WIFI_TIMEOUT_S, [this, networkId] (std::string name) {
                DMCommTool::GetInstance()->StopSocket(networkId);
            });
    }
}

void DeviceManagerService::NotifyRemoteUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra,
    const std::map<std::string, std::string> &wifiDevices)
{
    LOGI("userId: %{public}s, tokenId: %{public}s, extra: %{public}s",
        GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str(), GetAnonyString(extra).c_str());
    for (const auto &it : wifiDevices) {
        std::string localUdid = GetLocalDeviceUdid();
        int32_t result = SendUnBindAppByWifi(userId, tokenId, extra, it.second, localUdid);
        if (result != DM_OK) {
            LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str());
            continue;
        }
        std::lock_guard<std::mutex> autoLock(timerLocks_);
        if (timer_ == nullptr) {
            timer_ = std::make_shared<DmTimer>();
        }
        std::string udid = it.first;
        std::string networkId = it.second;
        timer_->StartTimer(std::string(APP_UNBIND_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid),
            USER_SWITCH_BY_WIFI_TIMEOUT_S, [this, networkId] (std::string name) {
                DMCommTool::GetInstance()->StopSocket(networkId);
            });
    }
}

void DeviceManagerService::ProcessReceiveRspAppUninstall(const std::string &remoteUdid)
{
    LOGI("remoteUdid: %{public}s", GetAnonyString(remoteUdid).c_str());
    std::lock_guard<std::mutex> autoLock(timerLocks_);
    if (timer_ != nullptr && remoteUdid != "") {
        timer_->DeleteTimer(std::string(APP_UNINSTALL_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid));
    }
}

void DeviceManagerService::ProcessReceiveRspAppUnbind(const std::string &remoteUdid)
{
    LOGI("remoteUdid: %{public}s", GetAnonyString(remoteUdid).c_str());
    std::lock_guard<std::mutex> autoLock(timerLocks_);
    if (timer_ != nullptr && remoteUdid != "") {
        timer_->DeleteTimer(std::string(APP_UNBIND_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid));
    }
}

void DeviceManagerService::ProcessReceiveRspSvcUnbindProxy(const std::string &remoteUdid)
{
    LOGI("remoteUdid: %{public}s", GetAnonyString(remoteUdid).c_str());
    std::lock_guard<std::mutex> autoLock(timerLocks_);
    if (timer_ != nullptr && remoteUdid != "") {
        timer_->DeleteTimer(std::string(SERVICE_UNBIND_PROXY_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(remoteUdid));
    }
}

void DeviceManagerService::ProcessUnBindServiceProxy(const UnbindServiceProxyParam &param)
{
    LOGI("start");
    UnbindServiceProxyParam reverseParam;
    reverseParam.userId = param.userId;
    reverseParam.localTokenId = param.localTokenId;
    reverseParam.subjectTokenId = param.subjectTokenId;
    reverseParam.peerUdid = param.localUdid;
    reverseParam.serviceId = param.serviceId;
    reverseParam.isProxyUnbind = param.isProxyUnbind;
    if (IsDMServiceAdapterResidentLoad()) {
        dmServiceImplExtResident_->HandleProcessUnBindServiceProxy(reverseParam);
    }
}

int32_t DeviceManagerService::SendUninstAppByWifi(int32_t userId, int32_t tokenId, const std::string &networkId)
{
    LOGE("userId: %{public}s, tokenId: %{public}s",
        GetAnonyInt32(userId).c_str(), GetAnonyInt32(tokenId).c_str());
    CHECK_NULL_RETURN(DMCommTool::GetInstance(), ERR_DM_POINT_NULL);
    return DMCommTool::GetInstance()->SendUninstAppObj(userId, tokenId, networkId);
}

int32_t DeviceManagerService::SendUnBindAppByWifi(int32_t userId, int32_t tokenId, std::string extra,
    const std::string &networkId, const std::string &udid)
{
    LOGE("start");
    CHECK_NULL_RETURN(DMCommTool::GetInstance(), ERR_DM_POINT_NULL);
    return DMCommTool::GetInstance()->SendUnBindAppObj(userId, tokenId, extra, networkId, udid);
}

void DeviceManagerService::GetNotifyRemoteUnBindAppWay(int32_t userId, int32_t tokenId,
    std::map<std::string, std::string> &wifiDevices, bool &isBleWay)
{
    std::vector<std::string> peerUdids;
    int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID();
    std::map<std::string, int32_t> deviceMap = dmServiceImpl_->GetDeviceIdAndBindLevel(currentUserId);
    for (const auto &item : deviceMap) {
        peerUdids.push_back(item.first);
    }
    if (peerUdids.empty()) {
        LOGE("peerUdids is empty");
        return;
    }
    if (softbusListener_ == nullptr) {
        LOGE("softbusListener_ is null");
        return;
    }

    std::vector<std::string> bleUdids;
    for (const auto &udid : peerUdids) {
        std::string netWorkId = "";
        SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId);
        if (netWorkId.empty()) {
            LOGE("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        int32_t networkType = 0;
        int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
        if (ret != DM_OK || networkType <= 0) {
            LOGE("get networkType failed: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
        if ((static_cast<uint32_t>(networkType) & addrTypeMask) != 0x0) {
            bleUdids.push_back(udid);
        } else {
            wifiDevices.insert(std::pair<std::string, std::string>(udid, netWorkId));
        }
    }

    if (!bleUdids.empty()) {
        isBleWay = true;
    } else {
        isBleWay = false;
    }
}

void DeviceManagerService::HandleUserStopEvent(int32_t stopUserId)
{
    LOGI("stopUserId %{public}s.", GetAnonyInt32(stopUserId).c_str());
    std::vector<int32_t> foregroundUserVec;
    int32_t retFront = MultipleUserConnector::GetForegroundUserIds(foregroundUserVec);
    std::vector<int32_t> backgroundUserVec;
    int32_t retBack = MultipleUserConnector::GetBackgroundUserIds(backgroundUserVec);
    MultipleUserConnector::ClearLockedUser(foregroundUserVec, backgroundUserVec);
    if (retFront != DM_OK || retBack != DM_OK) {
        LOGE("retFront: %{public}d, retBack: %{public}d, frontuserids: %{public}s, backuserids: %{public}s",
            retFront, retBack, GetIntegerList(foregroundUserVec).c_str(), GetIntegerList(backgroundUserVec).c_str());
        return;
    }
    if (!IsUserStatusChanged(foregroundUserVec, backgroundUserVec)) {
        LOGI("User status has not changed.");
        return;
    }
    std::vector<int32_t> stopUserVec;
    stopUserVec.push_back(stopUserId);
    std::string localUdid = GetLocalDeviceUdid();
    std::map<std::string, int32_t> stopUserDeviceMap;
    std::vector<std::string> peerUdids;
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    stopUserDeviceMap = discoveryMgr_->GetCommonDependencyObj()->
        GetDeviceIdAndBindLevel(stopUserVec, localUdid);
    for (const auto &item : stopUserDeviceMap) {
        peerUdids.push_back(item.first);
    }
    if (peerUdids.empty()) {
        LOGI("no data to be stoped.");
        return;
    }
    NotifyRemoteLocalUserStop(localUdid, peerUdids, stopUserId);
}

void DeviceManagerService::DivideNotifyMethod(const std::vector<std::string> &peerUdids,
    std::vector<std::string> &bleUdids, std::map<std::string, std::string> &wifiDevices)
{
    if (peerUdids.empty()) {
        return;
    }
    if (softbusListener_ == nullptr) {
        bleUdids = peerUdids;
        LOGI("softbusListener_ is null");
        return;
    }
    for (const auto &udid : peerUdids) {
        std::string netWorkId = "";
        SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId);
        if (netWorkId.empty()) {
            LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        int32_t networkType = 0;
        int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
        if (ret != DM_OK || networkType <= 0) {
            LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
        if ((static_cast<uint32_t>(networkType) & addrTypeMask) != 0x0) {
            bleUdids.push_back(udid);
        } else {
            wifiDevices.insert(std::pair<std::string, std::string>(udid, netWorkId));
        }
    }
}

void DeviceManagerService::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid,
    const std::vector<std::string> &acceptEventUdids)
{
    {
        std::lock_guard<std::mutex> autoLock(timerLocks_);
        if (timer_ != nullptr) {
            for (const auto &udid : acceptEventUdids) {
                timer_->DeleteTimer(std::string(USER_STOP_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid));
            }
        }
    }
    if (MultipleUserConnector::IsUserUnlocked(stopUserId)) {
        LOGE("user has unlocked %{public}s.", GetAnonyInt32(stopUserId).c_str());
        return;
    }
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    discoveryMgr_->GetCommonDependencyObj()->HandleUserStop(stopUserId, stopEventUdid, acceptEventUdids);
}

void DeviceManagerService::HandleUserStop(int32_t stopUserId, const std::string &stopEventUdid)
{
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    discoveryMgr_->GetCommonDependencyObj()->HandleUserStop(stopUserId, stopEventUdid);
}

void DeviceManagerService::NotifyRemoteLocalUserStop(const std::string &localUdid,
    const std::vector<std::string> &peerUdids, int32_t stopUserId)
{
    std::vector<std::string> bleUdids;
    std::map<std::string, std::string> wifiDevices;
    DivideNotifyMethod(peerUdids, bleUdids, wifiDevices);
    if (!bleUdids.empty()) {
        HandleUserStop(stopUserId, localUdid, bleUdids);
        SendUserStopBroadCast(bleUdids, stopUserId);
    }
    if (!wifiDevices.empty()) {
        NotifyRemoteLocalUserStopByWifi(localUdid, wifiDevices, stopUserId);
    }
}

void DeviceManagerService::SendUserStopBroadCast(const std::vector<std::string> &peerUdids, int32_t stopUserId)
{
    LOGI("peerUdids: %{public}s", GetAnonyStringList(peerUdids).c_str());
    RelationShipChangeMsg msg;
    msg.type = RelationShipChangeType::STOP_USER;
    msg.userId = static_cast<uint32_t>(stopUserId);
    msg.peerUdids = peerUdids;
    std::string broadCastMsg = ReleationShipSyncMgr::GetInstance().SyncTrustRelationShip(msg);
    CHECK_NULL_VOID(softbusListener_);
    softbusListener_->SendAclChangedBroadcast(broadCastMsg);
}

void DeviceManagerService::HandleUserStopBroadCast(int32_t stopUserId, const std::string &remoteUdid)
{
    LOGI("start");
    HandleUserStop(stopUserId, remoteUdid);
}

void DeviceManagerService::NotifyRemoteLocalUserStopByWifi(const std::string &localUdid,
    const std::map<std::string, std::string> &wifiDevices, int32_t stopUserId)
{
    for (const auto &it : wifiDevices) {
        std::vector<std::string> updateUdids;
        updateUdids.push_back(it.first);
        CHECK_NULL_VOID(DMCommTool::GetInstance());
        int32_t result = DMCommTool::GetInstance()->SendUserStop(it.second, stopUserId);
        if (result != DM_OK) {
            LOGE("by wifi failed: %{public}s", GetAnonyString(it.first).c_str());
            HandleUserStop(stopUserId, localUdid, updateUdids);
            continue;
        }
        std::lock_guard<std::mutex> autoLock(timerLocks_);
        if (timer_ == nullptr) {
            timer_ = std::make_shared<DmTimer>();
        }
        std::string udid = it.first;
        timer_->StartTimer(std::string(USER_STOP_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid),
            USER_SWITCH_BY_WIFI_TIMEOUT_S,
            [this, stopUserId, localUdid, updateUdids] (std::string name) {
                DeviceManagerService::HandleUserStop(stopUserId, localUdid, updateUdids);
            });
    }
}
#endif

int32_t DeviceManagerService::RegisterAuthenticationType(const std::string &pkgName,
    const std::map<std::string, std::string> &authParam)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    auto authTypeIter = authParam.find(DM_AUTHENTICATION_TYPE);
    if (authTypeIter == authParam.end()) {
        LOGE("Invalid parameter, DM_AUTHENTICATION_TYPE is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsNumberString(authTypeIter->second)) {
        LOGE("Invalid parameter, DM_AUTHENTICATION_TYPE is not number.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    int32_t authenticationType = std::atoi(authTypeIter->second.c_str());

    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_INIT_FAILED;
    }
    return dmServiceImpl_->RegisterAuthenticationType(authenticationType);
}

int32_t DeviceManagerService::GetDeviceProfileInfoList(const std::string &pkgName,
    DmDeviceProfileInfoFilterOptions &filterOptions)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->GetDeviceProfileInfoList(pkgName, filterOptions);
}

int32_t DeviceManagerService::GetDeviceIconInfo(const std::string &pkgName,
    DmDeviceIconInfoFilterOptions &filterOptions)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->GetDeviceIconInfo(pkgName, filterOptions);
}

int32_t DeviceManagerService::PutDeviceProfileInfoList(const std::string &pkgName,
    std::vector<DmDeviceProfileInfo> &deviceProfileInfoList)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidPutDeviceProfileInfoList(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("GetDeviceProfileInfoList failed, adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->PutDeviceProfileInfoList(pkgName, deviceProfileInfoList);
}

int32_t DeviceManagerService::SetLocalDisplayNameToSoftbus(const std::string &displayName)
{
    LOGI("DeviceManagerService Start SetLocalDisplayName!");
    CHECK_NULL_RETURN(softbusListener_, ERR_DM_POINT_NULL);
    int32_t ret = softbusListener_->SetLocalDisplayName(displayName);
    if (ret != DM_OK) {
        LOGE("SetLocalDisplayName error, failed ret: %{public}d", ret);
        return ret;
    }
    return DM_OK;
}

int32_t DeviceManagerService::GetLocalDisplayDeviceName(const std::string &pkgName, int32_t maxNameLength,
    std::string &displayName)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNameLength, displayName);
#endif
    (void) maxNameLength;
    (void) displayName;
    return DM_OK;
}

int32_t DeviceManagerService::SetLocalDeviceName(const std::string &pkgName, const std::string &deviceName)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidModifyLocalDeviceName(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
#if !defined(DEVICE_MANAGER_COMMON_FLAG)
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->SetLocalDeviceName(pkgName, deviceName);
#else
    CHECK_NULL_RETURN(listener_, ERR_DM_POINT_NULL);
    ProcessInfo processInfo = {.pkgName = pkgName};
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    MultipleUserConnector::GetCallerUserId(processInfo.userId);
    processInfo.tokenId = IPCSkeleton::GetCallingTokenID();
    ffrt::submit([listener = listener_, deviceName = deviceName, processInfo = processInfo]() {
        CHECK_NULL_VOID(listener);
        listener->OnSetLocalDeviceNameResult(processInfo, deviceName, DM_OK);
    },
        ffrt::task_attr().name(ON_SET_LOCAL_DEVICE_NAME_RESULT_TASK));
#else
    std::thread([listener = listener_, deviceName = deviceName, processInfo = processInfo]() {
        CHECK_NULL_VOID(listener);
        listener->OnSetLocalDeviceNameResult(processInfo, deviceName, DM_OK);
    }).detach();
#endif
    return DM_OK;
#endif
}

int32_t DeviceManagerService::SetRemoteDeviceName(const std::string &pkgName,
    const std::string &deviceId, const std::string &deviceName)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidModifyRemoteDeviceName(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->SetRemoteDeviceName(pkgName, deviceId, deviceName);
}

std::vector<std::string> DeviceManagerService::GetDeviceNamePrefixs()
{
    LOGI("In");
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return {};
    }
    return dmServiceImplExtResident_->GetDeviceNamePrefixs();
}

void DeviceManagerService::HandleNetworkConnected(int32_t networkStatus)
{
    LOGI("In");
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return;
    }
    dmServiceImplExtResident_->HandleNetworkConnected(networkStatus);
}

void DeviceManagerService::AddHmlInfoToBindParam(int32_t actionId, std::string &bindParam)
{
    cJSON *bindParamObj = cJSON_Parse(bindParam.c_str());
    if (bindParamObj == NULL) {
        bindParamObj = cJSON_CreateObject();
        if (bindParamObj == NULL) {
            LOGE("Create bindParamObj object failed.");
            return;
        }
    }
    cJSON_AddStringToObject(bindParamObj, PARAM_KEY_CONN_SESSIONTYPE, CONN_SESSION_TYPE_HML);
    cJSON_AddStringToObject(bindParamObj, PARAM_KEY_HML_ACTIONID, std::to_string(actionId).c_str());
    char *str = cJSON_PrintUnformatted(bindParamObj);
    if (str == nullptr) {
        cJSON_Delete(bindParamObj);
        return;
    }
    bindParam = std::string(str);
    cJSON_free(str);
    cJSON_Delete(bindParamObj);
}

int32_t DeviceManagerService::RestoreLocalDeviceName(const std::string &pkgName)
{
    LOGI("In");
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidModifyLocalDeviceName(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (IsDMServiceAdapterResidentLoad()) {
        dmServiceImplExtResident_->RestoreLocalDeviceName();
    } else {
        LOGW("fail, adapter instance not init or init failed.");
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    return DeviceNameManager::GetInstance().RestoreLocalDeviceName();
#endif
    return DM_OK;
}

void DeviceManagerService::ClearPublishIdCache(const ProcessInfo &processInfo)
{
    CHECK_NULL_VOID(advertiseMgr_);
    advertiseMgr_->ClearPublishIdCache(processInfo);
}

bool DeviceManagerService::IsPC()
{
    if (softbusListener_ == nullptr) {
        LOGE("softbusListener_ is null.");
        return false;
    }
    DmDeviceInfo info;
    GetLocalDeviceInfo(info);
    return (info.deviceTypeId == DmDeviceType::DEVICE_TYPE_PC || info.deviceTypeId == DmDeviceType::DEVICE_TYPE_2IN1);
}

int32_t DeviceManagerService::GetDeviceNetworkIdList(const std::string &pkgName,
    const NetworkIdQueryFilter &queryFilter, std::vector<std::string> &networkIds)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("GetDeviceProfileInfoList failed, adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    std::vector<DmDeviceProfileInfo> dmDeviceProfileInfos;
    int32_t ret = dmServiceImplExtResident_->GetDeviceProfileInfosFromLocalCache(queryFilter, dmDeviceProfileInfos);
    if (ret != DM_OK) {
        LOGW("GetDeviceProfileInfosFromLocalCache failed, ret = %{public}d.", ret);
        return ret;
    }
    for (const auto &item : dmDeviceProfileInfos) {
        std::string networkId = "";
        SoftbusCache::GetInstance().GetNetworkIdFromCache(item.deviceId, networkId);
        if (!networkId.empty()) {
            networkIds.emplace_back(networkId);
        }
    }
    if (networkIds.empty()) {
        LOGW("networkIds is empty");
        return ERR_DM_FIND_NETWORKID_LIST_EMPTY;
    }
    return DM_OK;
}

void DeviceManagerService::NotifyRemoteLocalLogout(const std::vector<std::string> &peerUdids,
    const std::string &accountIdHash, const std::string &accountName, int32_t userId)
{
    LOGI("Start.");
    std::vector<std::string> bleUdids;
    std::vector<std::string> wifiDevices;
    for (const auto &udid : peerUdids) {
        std::string netWorkId = "";
        SoftbusCache::GetInstance().GetNetworkIdFromCache(udid, netWorkId);
        if (netWorkId.empty()) {
            LOGI("netWorkId is empty: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        int32_t networkType = 0;
        int32_t ret = softbusListener_->GetNetworkTypeByNetworkId(netWorkId.c_str(), networkType);
        if (ret != DM_OK || networkType <= 0) {
            LOGI("get networkType failed: %{public}s", GetAnonyString(udid).c_str());
            bleUdids.push_back(udid);
            continue;
        }
        uint32_t addrTypeMask = 1 << NetworkType::BIT_NETWORK_TYPE_BLE;
        if ((static_cast<uint32_t>(networkType) & addrTypeMask) != 0x0) {
            bleUdids.push_back(udid);
        } else {
            wifiDevices.push_back(netWorkId);
        }
    }
    if (!bleUdids.empty()) {
        SendAccountLogoutBroadCast(bleUdids, accountIdHash, accountName, userId);
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    for (const auto &it : wifiDevices) {
        CHECK_NULL_VOID(DMCommTool::GetInstance());
        int32_t ret = DMCommTool::GetInstance()->SendLogoutAccountInfo(it, accountIdHash, userId);
        if (ret != DM_OK) {
            LOGE("Send LogoutAccount Info error, ret = %{public}d", ret);
        }
    }
#endif
}

void DeviceManagerService::ProcessSyncAccountLogout(const std::string &accountId, const std::string &peerUdid,
    int32_t userId)
{
    LOGI("Start. process udid: %{public}s", GetAnonyString(peerUdid).c_str());
    if (!IsDMServiceImplReady()) {
        LOGE("Imp instance not init or init failed.");
        return;
    }
    std::vector<DmUserRemovedServiceInfo> serviceInfos;
    dmServiceImpl_->HandleAccountLogoutEvent(userId, accountId, peerUdid, serviceInfos);
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("IsDMServiceAdapterResidentLoad failed.");
        return;
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    for (auto &item : serviceInfos) {
        for (size_t i = 0; i < item.serviceIds.size(); i++) {
            DistributedDeviceProfile::ServiceInfo dpServiceInfo;
            DeviceProfileConnector::GetInstance().GetServiceInfoByUdidAndServiceId(
                item.peerUdid, item.serviceIds[i], dpServiceInfo);
            dmServiceImplExtResident_->BindServiceOffline(item.localTokenId, item.localPkgName,
                item.bindType, item.peerUdid, dpServiceInfo);
        }
    }
#endif
}

void DeviceManagerService::ProcessSyncAccountDeleted(const std::string &accountId, const std::string &peerUdid,
    int32_t userId)
{
    LOGI("udid %{public}s, userId %{public}d, accountId %{public}s",
        GetAnonyString(peerUdid).c_str(), userId, GetAnonyString(accountId).c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (!IsDMServiceImplReady()) {
        LOGE("Imp instance not init or init failed.");
        return;
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    
    DeviceProfileConnector::GetInstance().DeleteAclByAccountId(localUdid, userId, accountId, peerUdid);
#endif
    LOGI("ProcessSyncAccountDeleted completed.");
}

void DeviceManagerService::ProcessSyncAccountUnbound(const std::string &accountId, const std::string &peerUdid,
    int32_t userId)
{
    LOGI("udid %{public}s, userId %{public}d, accountId %{public}s",
        GetAnonyString(peerUdid).c_str(), userId, GetAnonyString(accountId).c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (!IsDMServiceImplReady()) {
        LOGE("Imp instance not init or init failed.");
        return;
    }
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    
    DeviceProfileConnector::GetInstance().DeleteAclByAccountId(localUdid, userId, accountId, peerUdid);
#endif
    LOGI("ProcessSyncAccountUnbound completed.");
}

void DeviceManagerService::ProcessMultAccountLogout(const std::string &accountId, const std::string &peerUdid,
    int32_t userId)
{
    LOGI("udid %{public}s, userId %{public}d, accountId %{public}s",
        GetAnonyString(peerUdid).c_str(), userId, GetAnonyString(accountId).c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);
    DeviceProfileConnector::GetInstance().HandleDistributedAccountLogout(
        localUdid, userId, accountId, peerUdid);
#endif
}

void DeviceManagerService::ProcessSyncForegroundAccount(
    const std::vector<ForegroundAccountInfo> &foregroundAccounts, const std::string &peerUdid)
{
    LOGI("udid %{public}s, accounts count %{public}zu", GetAnonyString(peerUdid).c_str(),
        foregroundAccounts.size());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);

    std::vector<int32_t> localForegroundUserIds;
    int32_t ret = MultipleUserConnector::GetForegroundUserIds(localForegroundUserIds);
    if (ret != DM_OK) {
        LOGE("GetForegroundUserIds failed, ret %{public}d", ret);
        return;
    }
    
    std::vector<ForegroundAccountInfo> localForegroundAccounts;
    for (const auto &userId : localForegroundUserIds) {
        DMAccountInfo dmAccountInfo = MultipleUserConnector::GetDMAccountInfoByUserId(userId);
        if (!dmAccountInfo.accountId.empty()) {
            char accountIdHash[DM_MAX_DEVICE_ID_LEN] = {0};
            if (Crypto::GetAccountIdHash7(dmAccountInfo.accountId,
                reinterpret_cast<uint8_t *>(accountIdHash)) != DM_OK) {
                LOGE("GetAccountIdHash7 failed for local userId %{public}d", userId);
                continue;
            }
            localForegroundAccounts.push_back(ForegroundAccountInfo(userId, std::string(accountIdHash)));
        }
    }
    
    DeviceProfileConnector::GetInstance().UpdateAclByDualForegroundAccountHash(
        localUdid, peerUdid, localForegroundAccounts, foregroundAccounts);
#endif
    LOGI("completed.");
}

int32_t DeviceManagerService::OpenAuthSessionWithPara(const std::string &deviceId, int32_t actionId, bool isEnable160m)
{
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->OpenAuthSessionWithPara(deviceId, actionId, isEnable160m);
}

int32_t DeviceManagerService::UnRegisterPinHolderCallback(const std::string &pkgName)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call UnRegisterPinHolderCallback.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    std::string processName = "";
    if (PermissionManager::GetInstance().GetCallerProcessName(processName) != DM_OK) {
        LOGE("Get caller process name failed, pkgname: %{public}s.", pkgName.c_str());
        return ERR_DM_FAILED;
    }
    if (!PermissionManager::GetInstance().CheckProcessNameValidOnPinHolder(processName)) {
        LOGE("The caller: %{public}s is not in white list.", processName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    LOGI("begin.");
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName: %{public}s.", pkgName.c_str());
        return ERR_DM_INPUT_PARA_INVALID;
    }
    CHECK_NULL_RETURN(pinHolder_, ERR_DM_POINT_NULL);
    return pinHolder_->UnRegisterPinHolderCallback(pkgName);
}

int32_t DeviceManagerService::GetLocalDeviceName(std::string &deviceName)
{
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (PermissionManager::GetInstance().CheckReadLocalDeviceName()) {
        return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, deviceName);
    } else {
        deviceName = DeviceNameManager::GetInstance().GetLocalMarketName();
        return DM_OK;
    }
#endif
    (void) deviceName;
    return DM_OK;
}

bool DeviceManagerService::GetAccessUdidByNetworkId(const std::string &srcNetWorkId, std::string &srcUdid,
    const std::string &sinkNetWorkId, std::string &sinkUdid)
{
    LOGI("start srcNetWorkId %{public}s, sinkNetWorkId %{public}s.", GetAnonyString(srcNetWorkId).c_str(),
        GetAnonyString(sinkNetWorkId).c_str());
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller not have permission to call GetAccessUdidByNetworkId.");
        return false;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return false;
    }
    SoftbusListener::GetUdidByNetworkId(srcNetWorkId.c_str(), srcUdid);
    SoftbusListener::GetUdidByNetworkId(sinkNetWorkId.c_str(), sinkUdid);
    return true;
}

bool DeviceManagerService::CheckSrcAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission.");
        return false;
    }
    std::string srcUdid = "";
    std::string sinkUdid = "";
    if (!GetAccessUdidByNetworkId(caller.networkId.c_str(), srcUdid, callee.networkId.c_str(), sinkUdid)) {
        LOGE("The caller %{public}s GetAccessUdidByNetworkId failed.", caller.pkgName.c_str());
        return false;
    }
    return dmServiceImpl_->CheckSrcAccessControl(caller, srcUdid, callee, sinkUdid);
}

bool DeviceManagerService::CheckSinkAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission.");
        return false;
    }
    std::string srcUdid = "";
    std::string sinkUdid = "";
    if (!GetAccessUdidByNetworkId(caller.networkId.c_str(), srcUdid, callee.networkId.c_str(), sinkUdid)) {
        LOGE("The caller %{public}s GetAccessUdidByNetworkId failed.", caller.pkgName.c_str());
        return false;
    }
    return dmServiceImpl_->CheckSinkAccessControl(caller, srcUdid, callee, sinkUdid);
}

bool DeviceManagerService::CheckSrcIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission.");
        return false;
    }
    std::string srcUdid = "";
    std::string sinkUdid = "";
    if (!GetAccessUdidByNetworkId(caller.networkId.c_str(), srcUdid, callee.networkId.c_str(), sinkUdid)) {
        LOGE("The caller %{public}s GetAccessUdidByNetworkId failed.", caller.pkgName.c_str());
        return false;
    }
    return dmServiceImpl_->CheckSrcIsSameAccount(caller, srcUdid, callee, sinkUdid);
}

bool DeviceManagerService::CheckSinkIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission.");
        return false;
    }
    std::string srcUdid = "";
    std::string sinkUdid = "";
    if (!GetAccessUdidByNetworkId(caller.networkId.c_str(), srcUdid, callee.networkId.c_str(), sinkUdid)) {
        LOGE("The caller %{public}s GetAccessUdidByNetworkId failed.", caller.pkgName.c_str());
        return false;
    }
    return dmServiceImpl_->CheckSinkIsSameAccount(caller, srcUdid, callee, sinkUdid);
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
int32_t DeviceManagerService::GetIdentificationByDeviceIds(const std::string &pkgName,
    const std::vector<std::string> deviceIdList,
    std::map<std::string, std::string> &deviceIdentificationMap)
{
    if (pkgName.empty() || deviceIdList.empty()) {
        LOGE("Invalid parameter, pkgName is empty or deviceIdList is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    LOGI("pkgName = %{public}s.", pkgName.c_str());
    if (!AppManager::GetInstance().IsSystemApp()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NOT_SYSTEM_APP;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission() ||
        !PermissionManager::GetInstance().CheckDataSyncPermission() ||
        !PermissionManager::GetInstance().CheckAccessUdidPermission()) {
        LOGE("The caller does not have permission to call GetIdentificationByDeviceIds.");
        return ERR_DM_NO_PERMISSION;
    }
    for (auto deviceId : deviceIdList) {
        LOGI("deviceId = %{public}s.", GetAnonyString(deviceId).c_str());
        if (deviceIdentificationMap.find(deviceId) == deviceIdentificationMap.end()) {
            std::string udidHash = "";
            std::string udid = "";
            GetUdidHashByAnoyDeviceId(deviceId, udidHash);
            SoftbusCache::GetInstance().GetUdidByUdidHash(udidHash, udid);
            if (!deviceId.empty() && !udidHash.empty() && !udid.empty()) {
                deviceIdentificationMap[deviceId] = udid;
            }
        }
    }
    return DM_OK;
}
#endif

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
void DeviceManagerService::HandleUserSwitchEventCallback(const std::string &commonEventType, int32_t currentUserId,
    int32_t beforeUserId)
{
    LOGI("commonEventType %{public}s, currentUserId %{public}d, beforeUserId %{public}d.", commonEventType.c_str(),
        currentUserId, beforeUserId);
    DeviceProfileConnector::GetInstance().DeleteDpInvalidAcl();
    DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(currentUserId, beforeUserId);
    DMAccountInfo currentDmAccountInfo = MultipleUserConnector::GetCurrentDMAccountInfo();
    MultipleUserConnector::SetAccountInfo(currentUserId, currentDmAccountInfo.subProfileId, currentDmAccountInfo);
    CHECK_NULL_VOID(DMCommTool::GetInstance());
    DMCommTool::GetInstance()->StartCommonEvent(commonEventType,
        [this, commonEventType] () {
            DeviceManagerService::HandleAccountCommonEvent(commonEventType);
    });
#ifdef CAR_DEVICE_ENABLE
    TriggerForegroundAccountSync();
#endif
    if (IsDMServiceAdapterResidentLoad()) {
        dmServiceImplExtResident_->HandleUserSwitchEvent(currentUserId, beforeUserId);
    } else {
        LOGW("HandleUserSwitchEvent fail, adapter instance not init or init failed.");
    }
}

void DeviceManagerService::GetHoOsTypeUdids(std::vector<std::string> &peerUdids)
{
    std::vector<std::string> osTypeStrs;
    if (KVAdapterManager::GetInstance().GetAllOstypeData(osTypeStrs) != DM_OK) {
        LOGE("Get all ostype failed.");
        return;
    }
    for (const auto &item : osTypeStrs) {
        JsonObject osTypeObj(item);
        if (osTypeObj.IsDiscarded()) {
            LOGE("msg prase error.");
            continue;
        }
        if (!IsString(osTypeObj, PEER_UDID) || !IsInt32(osTypeObj, PEER_OSTYPE)) {
            LOGE("osTypeObj value invalid.");
            continue;
        }
        if (osTypeObj[PEER_OSTYPE].Get<int32_t>() == OLD_DM_HO_OSTYPE ||
            osTypeObj[PEER_OSTYPE].Get<int32_t>() == NEW_DM_HO_OSTYPE) {
            peerUdids.push_back(osTypeObj[PEER_UDID].Get<std::string>());
        }
    }
}

void DeviceManagerService::DeleteHoDevice(const std::vector<int32_t> &foreGroundUserIds,
    const std::vector<int32_t> &backGroundUserIds)
{
    if (foreGroundUserIds.empty() || backGroundUserIds.empty()) {
        LOGE("backGroundUserIds %{public}s, foreGroundUserIds %{public}s.", GetIntegerList(backGroundUserIds).c_str(),
            GetIntegerList(foreGroundUserIds).c_str());
        return;
    }
    std::vector<std::string> peerUdids;
    GetHoOsTypeUdids(peerUdids);
    if (peerUdids.empty()) {
        LOGE("peerUdids empty.");
        return;
    }
    if (IsDMServiceAdapterResidentLoad()) {
        dmServiceImplExtResident_->AccountUserSwitched(MultipleUserConnector::GetCurrentAccountUserID(),
            MultipleUserConnector::GetOhosAccountId());
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance init failed.");
        return;
    }
    for (const auto &item : peerUdids) {
        dmServiceImpl_->DeleteHoDevice(item, foreGroundUserIds, backGroundUserIds);
    }
}

void DeviceManagerService::HandleAccountLogoutEventCallback(const std::string &commonEventType, int32_t currentUserId,
    int32_t beforeUserId)
{
    LOGI("commonEventType %{public}s, currentUserId %{public}d, beforeUserId %{public}d.", commonEventType.c_str(),
        currentUserId, beforeUserId);
    DeviceProfileConnector::GetInstance().DeleteDpInvalidAcl();
    DeviceNameManager::GetInstance().InitDeviceNameWhenLogout();
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    DMAccountInfo dmAccountInfo;
    dmAccountInfo.accountId = MultipleUserConnector::GetAccountIdBySubProfileId(beforeUserId, 0);
    HandleAccountLogout(currentUserId, dmAccountInfo.accountId, dmAccountInfo.accountName);
    MultipleUserConnector::DeleteAccountInfoByUserId(currentUserId);
    DMAccountInfo currentDmAccountInfo = MultipleUserConnector::GetCurrentDMAccountInfo();
    MultipleUserConnector::SetAccountInfo(MultipleUserConnector::GetCurrentAccountUserID(),
        currentDmAccountInfo.subProfileId, currentDmAccountInfo);
    IpcServerStub::GetInstance().HandleAccountLogoutEvent(currentUserId, dmAccountInfo.accountId);
#endif
}

void DeviceManagerService::InitTaskOfDelTimeOutAcl()
{
    CHECK_NULL_VOID(discoveryMgr_);
    if (!discoveryMgr_->IsCommonDependencyReady() || discoveryMgr_->GetCommonDependencyObj() == nullptr) {
        LOGE("IsCommonDependencyReady failed or GetCommonDependencyObj() is nullptr.");
        return;
    }
    std::unordered_set<AuthOnceAclInfo, AuthOnceAclInfoHash> aclInfos;
    discoveryMgr_->GetCommonDependencyObj()->GetAllAuthOnceAclInfos(aclInfos);
    if (aclInfos.empty()) {
        LOGI("no auth once data.");
        return;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return;
    }
    for (const auto &acl : aclInfos) {
        dmServiceImpl_->InitTaskOfDelTimeOutAcl(acl.peerUdid, acl.peerUserId, acl.localUserId);
    }
}

int32_t DeviceManagerService::BindServiceTarget(const std::string &pkgName, const PeerTargetId &targetId,
    const std::map<std::string, std::string> &bindParam)
{
    LOGI("Start for pkgName = %{public}s", pkgName.c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return ERR_DM_CONSTRAINT_ENABLE;
    }
#endif
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || pkgName == std::string(DM_PKG_NAME)) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (targetId.serviceId == 0) {
        LOGE("Invalid parameter, service id is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("DMServiceImpl instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->BindServiceTarget(pkgName, targetId, bindParam);
}

int32_t DeviceManagerService::LeaveLNN(const std::string &pkgName, const std::string &networkId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call LeaveLNN.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || networkId.empty()) {
        LOGE("Invalid parameter.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceImplReady()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImpl_->LeaveLNN(pkgName, networkId);
}

int32_t DeviceManagerService::OpenAuthSessionWithPara(int64_t serviceId)
{
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->OpenAuthSessionWithPara(serviceId);
}
#endif

int32_t DeviceManagerService::GetAuthTypeByUdidHash(const std::string &udidHash, const std::string &pkgName,
    DMLocalServiceInfoAuthType &authType)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call LeaveLNN.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!AppManager::GetInstance().IsSystemSA()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || udidHash.empty()) {
        LOGE("Invalid parameter.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    DeviceProfileConnector::GetInstance().GetAuthTypeByUdidHash(udidHash, pkgName, authType);
#endif
    return DM_OK;
}

#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
int32_t DeviceManagerService::RegisterServiceInfo(const DmRegisterServiceInfo& regServiceInfo, int64_t &serviceId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The callerdoes not have permission to cal1 RegisterServiceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
    int32_t ret = ValidateDmRegisterServiceInfo(regServiceInfo);
    if (ret != DM_OK) {
        LOGE("Invalid parameter.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("StartPublishService failed, adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->RegisterServiceInfo(regServiceInfo, serviceId);
}

int32_t DeviceManagerService::ValidateDmRegisterServiceInfo(const DmRegisterServiceInfo& regServiceInfo)
{
    if (regServiceInfo.serviceOwnerPkgName.empty() ||
        regServiceInfo.serviceType.empty() ||
        regServiceInfo.serviceType.size() > DM_MAX_SERVICE_TYPE ||
        regServiceInfo.serviceName.empty() ||
        regServiceInfo.serviceName.size() > DM_MAX_SERVICE_NAME ||
        regServiceInfo.serviceDisplayName.size() < DM_MIN_SERVICE_DISPLAYNAME ||
        regServiceInfo.serviceDisplayName.size() > DM_MAX_SERVICE_DISPLAYNAME ||
        regServiceInfo.customData.size() > DM_MAX_CUSTOMDATA ||
        regServiceInfo.timeStamp < 0 ||
        regServiceInfo.serviceCode.empty() ||
        regServiceInfo.customData.empty()) {
        return ERR_DM_INPUT_PARA_INVALID;
    }
    return DM_OK;
}

int32_t DeviceManagerService::UnRegisterServiceInfo(int64_t serviceId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The callerdoes not have permission to cal1 RegisterServiceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }

    char localdeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localdeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = std::string(localdeviceId);
    int32_t currentUserId = MultipleUserConnector::GetCurrentAccountUserID();
    std::multimap<std::string, int32_t> deviceMap = DeviceProfileConnector::GetInstance().
        GetDeviceIdAndUserId(localUdid, currentUserId);
    std::vector<std::string> peerUdids;
    LOGI("deviceMap, size: %{public}zu.", deviceMap.size());
    for (const auto &item : deviceMap) {
        if (find(peerUdids.begin(), peerUdids.end(), item.first) == peerUdids.end()) {
            peerUdids.emplace_back(item.first);
        }
    }
    if (!peerUdids.empty()) {
        LOGI("SendUnRegServiceBroadCast start");
        SendUnRegServiceBroadCast(peerUdids, currentUserId, serviceId);
    }

    int32_t ret = dmServiceImplExtResident_->UnRegisterServiceInfo(serviceId);
    if (ret != DM_OK) {
        LOGE("ret: %{public}d", ret);
        return ret;
    }
    return DM_OK;
}

int32_t DeviceManagerService::StartPublishService(const std::string &pkgName, int64_t serviceId,
    const DmPublishServiceParam &publishServiceParam)
{
    LOGI("start, pkgName: %{public}s", pkgName.c_str());
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call StantPublishService.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("instance not init or init failed.");
        return ERR_DM_NOT_INIT;
    }
    return dmServiceImplExtResident_->StartPublishService(pkgName, serviceId, publishServiceParam);
}

int32_t DeviceManagerService::StopPublishService(const std::string &pkgName, int64_t serviceId)
{
    LOGI("start, pkgName: %{public}s, serviceId: %{public}" PRId64,
        pkgName.c_str(), serviceId);
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call StantPublishService.");
        return ERR_DM_NO_PERMISSION;
    }
    ProcessInfo processInfo;
    processInfo.pkgName = pkgName;
    MultipleUserConnector::GetCallerUserId(processInfo.userId);
    processInfo.tokenId = IPCSkeleton::GetCallingTokenID();
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->StopPublishService(processInfo, serviceId);
}

int32_t DeviceManagerService::StartDiscoveryService(const std::string &pkgName, const DmDiscoveryServiceParam &disParam)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call StantPublishService.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("StartDiscovering failed, adapter instance not init or init failed");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    ProcessInfo processInfo;
    processInfo.pkgName = pkgName;
    MultipleUserConnector::GetCallerUserId(processInfo.userId);
    processInfo.tokenId = IPCSkeleton::GetCallingTokenID();
    return dmServiceImplExtResident_->StartDiscoveryService(processInfo, disParam);
}

int32_t DeviceManagerService::StopDiscoveryService(const std::string &pkgName,
    const DmDiscoveryServiceParam &discParam)
{
    LOGI("start, pkgName: %{public}s", pkgName.c_str());
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call StantPublishService.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    ProcessInfo processInfo;
    processInfo.pkgName = pkgName;
    MultipleUserConnector::GetCallerUserId(processInfo.userId);
    processInfo.tokenId = IPCSkeleton::GetCallingTokenID();
    return dmServiceImplExtResident_->StopDiscoveryService(processInfo, discParam);
}

int32_t DeviceManagerService::RegServiceStateCallback(const std::string &pkgName, int64_t serviceId)
{
    LOGI("called.");
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call GetDeviceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (serviceId < 0) {
        LOGE("Invalid parameter, serviceId is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    int32_t result = dmServiceImplExtResident_->RegisterServiceStateCallback(pkgName, serviceId);
    LOGI("RegisterServiceStateCallback result: %{public}d", result);
    return result;
}

int32_t DeviceManagerService::ClearServiceStateCallback(const std::string &pkgName, int32_t userId)
{
    LOGI("called.");
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    int32_t result = dmServiceImplExtResident_->ClearServiceStateCallback(pkgName, userId);
    LOGI("result: %{public}d", result);
    return result;
}

int32_t DeviceManagerService::UnRegServiceStateCallback(const std::string &pkgName, int64_t serviceId)
{
    LOGI("called.");
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call GetDeviceInfo.");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty()) {
        LOGE("Invalid parameter, pkgName is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (serviceId < 0) {
        LOGE("Invalid parameter, serviceId is empty.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    int32_t result = dmServiceImplExtResident_->UnRegisterServiceStateCallback(pkgName, serviceId);
    LOGI("UnRegisterServiceStateCallback result: %{public}d", result);
    return result;
}

int32_t DeviceManagerService::SyncServiceInfoByServiceId(const std::string &pkgName, int32_t localUserId,
    const std::string &networkId, int64_t serviceId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call LeaveLNN.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!AppManager::GetInstance().IsSystemSA()) {
        LOGE("no proxy permission");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || networkId.empty()) {
        LOGE("Invalid parameter.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->SyncServiceInfoByServiceId(pkgName, localUserId, networkId, serviceId);
}

int32_t DeviceManagerService::SyncAllServiceInfo(const std::string &pkgName, int32_t localUserId,
    const std::string &networkId)
{
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller: %{public}s does not have permission to call LeaveLNN.", pkgName.c_str());
        return ERR_DM_NO_PERMISSION;
    }
    if (!AppManager::GetInstance().IsSystemSA()) {
        LOGE("no proxy permission");
        return ERR_DM_NO_PERMISSION;
    }
    if (pkgName.empty() || networkId.empty()) {
        LOGE("Invalid parameter.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->SyncAllServiceInfo(pkgName, localUserId, networkId);
}

int32_t DeviceManagerService::GetLocalServiceInfoByServiceId(int64_t serviceId, DmRegisterServiceInfo &serviceInfo)
{
    LOGI("serviceId: %{public}" PRId64, serviceId);
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string pkgName = "";
    if (!PermissionManager::GetInstance().CheckSystemSA(pkgName)) {
        LOGE("The caller does not have system SA permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (serviceId <= 0) {
        LOGE("Invalid serviceId");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGW("fail, adapter instance not init or init failed.");
        return ERR_DM_FAILED;
    }
    int32_t ret = dmServiceImplExtResident_->GetLocalServiceInfoByServiceId(serviceId, serviceInfo);
    if (ret != DM_OK) {
        LOGE("from resident failed, ret: %{public}d", ret);
        return ret;
    }
    LOGI("success");
    return DM_OK;
}

int32_t DeviceManagerService::GetTrustServiceInfo(const std::string &pkgName,
    const std::map<std::string, std::string> &paramMap, std::vector<DmServiceInfo> &serviceList)
{
    LOGI("Start pkgName %{public}s.", pkgName.c_str());
    CHECK_EMPTY_RETURN(pkgName, ERR_DM_INPUT_PARA_INVALID);
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    if (DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
#endif
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (!PermissionManager::GetInstance().CheckSystemSA(pkgName)) {
        LOGE("The caller does not have system SA permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGW("fail, adapter instance not init or init failed.");
        return ERR_DM_FAILED;
    }
    int32_t ret = dmServiceImplExtResident_->GetTrustServiceInfo(pkgName, paramMap, serviceList);
    if (ret != DM_OK) {
        LOGE("from resident failed, ret: %{public}d", ret);
        return ret;
    }
    LOGI("success");
    return DM_OK;
}

int32_t DeviceManagerService::GetRegisterServiceInfo(const std::map<std::string, std::string> &param,
    std::vector<DmRegisterServiceInfo> &regServiceInfos)
{
    LOGI("Start");
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string pkgName = "";
    if (!PermissionManager::GetInstance().CheckSystemSA(pkgName)) {
        LOGE("The caller does not have system SA permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGW("fail, adapter instance not init or init failed.");
        return ERR_DM_FAILED;
    }
    int32_t ret = dmServiceImplExtResident_->GetRegisterServiceInfo(param, regServiceInfos);
    if (ret != DM_OK) {
        LOGE("from resident failed, ret: %{public}d", ret);
        return ret;
    }
    LOGI("success");
    return DM_OK;
}

int32_t DeviceManagerService::UpdateServiceInfo(int64_t serviceId, const DmRegisterServiceInfo &regServiceInfo)
{
    LOGI("start.");
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (serviceId <= 0) {
        LOGE("serviceId is invalid.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (IsDMServiceAdapterResidentLoad()) {
        return dmServiceImplExtResident_->UpdateServiceInfo(serviceId, regServiceInfo);
    }
    return ERR_DM_FAILED;
}

int32_t DeviceManagerService::GetPeerServiceInfoByServiceId(const std::string &networkId, int64_t serviceId,
    DmRegisterServiceInfo &serviceInfo)
{
    LOGI("serviceId: %{public}" PRId64, serviceId);
    if (CheckConstraintEnabledByNetworkId(networkId)) {
        LOGI("contraint enable is true");
        return DM_OK;
    }
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    std::string pkgName = "";
    if (!PermissionManager::GetInstance().CheckSystemSA(pkgName)) {
        LOGE("The caller does not have system SA permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (serviceId <= 0 || networkId == "") {
        LOGE("Invalid serviceId or networkId.");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGW("fail, adapter instance not init or init failed.");
        return ERR_DM_FAILED;
    }
    int32_t ret = dmServiceImplExtResident_->GetPeerServiceInfoByServiceId(networkId, serviceId, serviceInfo);
    if (ret != DM_OK) {
        LOGE("from resident failed, ret: %{public}d", ret);
        return ret;
    }
    LOGI("success");
    return DM_OK;
}

void DeviceManagerService::NotifyRemoteUnbindService(const std::map<std::string, std::string> &unbindParam,
    const std::string &netWorkId, int64_t serviceId)
{
    uint64_t localTokenId = OHOS::IPCSkeleton::GetCallingTokenID();
    bool isProxyUnbind = false;
    auto it = unbindParam.find(PARAM_KEY_IS_PROXY_UNBIND);
    if (it != unbindParam.end()) {
        isProxyUnbind = (it->second == "true");
    }
    auto item = unbindParam.find(PARAM_KEY_LOCAL_USER_ID);
    int32_t localUserId = 0;
    if (item != unbindParam.end()) {
        localUserId = std::atoi(it->second.c_str());
    }
    uint64_t subjectTokenId = 0;
    if (isProxyUnbind) {
        LOGI("isProxyUnbind is true.");
        int32_t ret = GetSubjectTokenId(unbindParam, subjectTokenId);
        if (ret != DM_OK) {
            LOGE("input unbind parameter not contains subjectTokenId");
        }
    }
    NotifyRemoteUnBindServiceByWifi(localUserId, localTokenId, subjectTokenId, serviceId, netWorkId, isProxyUnbind);
}

void DeviceManagerService::NotifyRemoteUnBindServiceByWifi(int32_t userId, uint64_t localTokenId,
    uint64_t subjectTokenId, int64_t serviceId, const std::string &netWorkId, bool isProxyUnbind)
{
    LOGI("start.");
    std::string localUdid = GetLocalDeviceUdid();
    UnbindServiceProxyParam param;
    param.userId = userId;
    param.localTokenId = localTokenId;
    param.subjectTokenId = subjectTokenId;
    param.localUdid = localUdid;
    param.isProxyUnbind = isProxyUnbind;
    param.peerNetworkId = netWorkId;
    param.serviceId = serviceId;
    CHECK_NULL_VOID(DMCommTool::GetInstance());
    int32_t res = DMCommTool::GetInstance()->SendUnBindServiceProxyObj(param);
    if (res != DM_OK) {
        LOGE("failed: %{public}s", GetAnonyString(netWorkId).c_str());
        return;
    }
    std::lock_guard<std::mutex> autoLock(timerLocks_);
    if (timer_ == nullptr) {
        timer_ = std::make_shared<DmTimer>();
    }
    std::string udid = "";
    SoftbusListener::GetUdidByNetworkId(netWorkId.c_str(), udid);
    timer_->StartTimer(std::string(SERVICE_UNBIND_PROXY_BY_WIFI_TIMEOUT_TASK) + Crypto::Sha256(udid),
        USER_SWITCH_BY_WIFI_TIMEOUT_S, [this, netWorkId] (std::string name) {
        DMCommTool::GetInstance()->StopSocket(netWorkId);
    });
}

int32_t DeviceManagerService::UnbindServiceTarget(const std::string &pkgName,
    const std::map<std::string, std::string> &unbindParam, const std::string &netWorkId,
    int64_t serviceId)
{
    LOGI("start");
    if (!PermissionManager::GetInstance().CheckAccessServicePermission()) {
        LOGE("The caller does not have permission to call");
        return ERR_DM_NO_PERMISSION;
    }
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    NotifyRemoteUnbindService(unbindParam, netWorkId, serviceId);
    int32_t ret = dmServiceImplExtResident_->UnbindServiceTarget(pkgName, unbindParam,
        netWorkId, serviceId);
    LOGI("UnAuthenticateDevice 11 get ret %{public}d.", ret);
    if (ret != DM_OK) {
        LOGE("failed, %{public}d.", ret);
        return ERR_DM_FAILED;
    }
    return DM_OK;
}

int32_t DeviceManagerService::GetSubjectTokenId(const std::map<std::string, std::string> &unbindParam,
    uint64_t &subjectTokenId)
{
    LOGI("start");
    auto it = unbindParam.find(PARAM_KEY_SUBJECT_SERVICE_ONES);
    if (it == unbindParam.end()) {
        LOGE("input unbind parameter not contains isProxyUnbind");
        return ERR_DM_INPUT_PARA_INVALID;
    }
    JsonObject allProxyObj;
    allProxyObj.Parse(it->second);
    if (allProxyObj.IsDiscarded()) {
        LOGE("WZF event prase error.");
        return ERR_DM_FAILED;
    }
    std::string tokenIdStr = TAG_TOKENID;
    int64_t subjectTokenIdTemp;
    for (auto const &item : allProxyObj.Items()) {
        if (!item.Contains(tokenIdStr) || !IsInt64(item, tokenIdStr)) {
            LOGE("tokenId invalid");
            return ERR_DM_INPUT_PARA_INVALID;
        }
        subjectTokenIdTemp = item[tokenIdStr].Get<int64_t>();
    }
    subjectTokenId = static_cast<uint64_t>(subjectTokenIdTemp);
    return DM_OK;
}

void DeviceManagerService::HandleServiceUnRegEvent(const std::string &peerUdid, int32_t userId,
    int64_t serviceId)
{
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return;
    }

    ffrt::submit([dmServiceImplExtResident = dmServiceImplExtResident_, peerUdid, serviceId]() {
        dmServiceImplExtResident->HandleServiceUnRegEvent(peerUdid, serviceId);
    },
        ffrt::task_attr().name(HANDLE_SERVICE_UN_REG_EVENT_TASK));
    return;
}

void DeviceManagerService::HandleAccountEventBroadCast(const RelationShipChangeMsg &relationShipMsg)
{
    LOGI("userId %{public}d, accountIdHash %{public}s, eventType %{public}d, peerUdid %{public}s",
        relationShipMsg.userId, GetAnonyString(relationShipMsg.accountId).c_str(),
        static_cast<uint32_t>(relationShipMsg.accountEventType), GetAnonyString(relationShipMsg.peerUdid).c_str());
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    char localDeviceId[DEVICE_UUID_LENGTH] = {0};
    GetDevUdid(localDeviceId, DEVICE_UUID_LENGTH);
    std::string localUdid = static_cast<std::string>(localDeviceId);

    switch (relationShipMsg.accountEventType) {
        case AccountEventType::ACCOUNT_DELETED:
            DeviceProfileConnector::GetInstance().DeleteAclByAccountIdHash(localUdid,
                relationShipMsg.userId, relationShipMsg.accountId, relationShipMsg.peerUdid);
            LOGI("Handle ACCOUNT_DELETED broadcast completed");
            break;
        case AccountEventType::ACCOUNT_UNBOUND:
            DeviceProfileConnector::GetInstance().DeleteAclByAccountIdHash(localUdid,
                relationShipMsg.userId, relationShipMsg.accountId, relationShipMsg.peerUdid);
            LOGI("Handle ACCOUNT_UNBOUND broadcast completed");
            break;
        case AccountEventType::ACCOUNT_LOGOUT:
            DeviceProfileConnector::GetInstance().UpdateAclStatusByAccountIdHash(localUdid,
                relationShipMsg.userId, relationShipMsg.accountId, INACTIVE, relationShipMsg.peerUdid);
            LOGI("Handle ACCOUNT_LOGOUT broadcast completed");
            break;
        case AccountEventType::SEND_ACCOUNT:
            {
                ForegroundAccountInfo foregroundAccount(relationShipMsg.userId, relationShipMsg.accountId);
                HandleForegroundAccountBroadCast(foregroundAccount, relationShipMsg.peerUdid,
                    relationShipMsg.syncUserIdFlag, relationShipMsg.broadCastId, relationShipMsg.isLastBatch);
                LOGI("Handle SEND_ACCOUNT broadcast completed");
            }
            break;
        case AccountEventType::RESP_ACCOUNT:
            {
                ForegroundAccountInfo foregroundAccount(relationShipMsg.userId, relationShipMsg.accountId);
                HandleForegroundAccountBroadCast(foregroundAccount, relationShipMsg.peerUdid,
                    false, relationShipMsg.broadCastId, relationShipMsg.isLastBatch);
                LOGI("Handle RESP_ACCOUNT broadcast completed");
            }
            break;
        default:
            LOGE("Unknown account event type: %{public}d", static_cast<uint32_t>(relationShipMsg.accountEventType));
            break;
    }
#endif
}

int32_t DeviceManagerService::BindServiceOnline(const ServiceStateBindParameter &bindParam)
{
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->BindServiceOnline(bindParam);
}

int32_t DeviceManagerService::BindServiceOffline(uint64_t tokenId, const std::string &pkgName, int32_t bindType,
    const std::string &peerUdid, const DistributedDeviceProfile::ServiceInfo &serviceInfo)
{
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    return dmServiceImplExtResident_->BindServiceOffline(tokenId, pkgName, bindType, peerUdid, serviceInfo);
}

int32_t DeviceManagerService::HandleRemoteDied(const ProcessInfo &processInfo)
{
    LOGI("called, pkgName: %{public}s, userId: %{public}d",
        processInfo.pkgName.c_str(), processInfo.userId);
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    int32_t result = dmServiceImplExtResident_->HandleRemoteDied(processInfo);
    LOGI("result: %{public}d", result);
    return result;
}

int32_t DeviceManagerService::HandleSoftbusRestart()
{
    LOGI("start");
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    int32_t result = dmServiceImplExtResident_->HandleSoftbusRestart();
    LOGI("result: %{public}d", result);
    return result;
}

int32_t DeviceManagerService::HandleServiceStatusChange(DmDeviceState devState, const std::string &peerUdid)
{
    LOGI("start");
    if (!IsDMServiceAdapterResidentLoad()) {
        LOGE("adapter instance not init or init failed.");
        return ERR_DM_UNSUPPORTED_METHOD;
    }
    int32_t result = dmServiceImplExtResident_->HandleServiceStatusChange(devState, peerUdid);
    LOGI("result: %{public}d", result);
    return result;
}
#endif

bool DeviceManagerService::CheckConstraintEnabledByNetworkId(const std::string &networkId)
{
    DmDeviceInfo deviceInfo;
    SoftbusCache::GetInstance().GetLocalDeviceInfo(deviceInfo);
    if (networkId == std::string(deviceInfo.networkId)) {
        LOGI("get local deviceinfo, networkId %{public}s.", GetAnonyString(networkId).c_str());
        return false;
    }
#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
    return DmConstrainsManager::GetInstance().CheckOsAccountConstraintEnabled(
        MultipleUserConnector::GetForgroundUserId(), DM_ACCOUNT_CONSTRAINT);
#endif
    return false;
}
} // namespace DistributedHardware
} // namespace OHOS