179a732c7Sopenharmony_ci/*
279a732c7Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License.
579a732c7Sopenharmony_ci * You may obtain a copy of the License at
679a732c7Sopenharmony_ci *
779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
879a732c7Sopenharmony_ci *
979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and
1379a732c7Sopenharmony_ci * limitations under the License.
1479a732c7Sopenharmony_ci */
1579a732c7Sopenharmony_ci
1679a732c7Sopenharmony_ci#ifndef OHOS_DM_SERVICE_H
1779a732c7Sopenharmony_ci#define OHOS_DM_SERVICE_H
1879a732c7Sopenharmony_ci
1979a732c7Sopenharmony_ci#include <string>
2079a732c7Sopenharmony_ci#include <memory>
2179a732c7Sopenharmony_ci#if defined(__LITEOS_M__)
2279a732c7Sopenharmony_ci#include "dm_mutex.h"
2379a732c7Sopenharmony_ci#else
2479a732c7Sopenharmony_ci#include <mutex>
2579a732c7Sopenharmony_ci#endif
2679a732c7Sopenharmony_ci
2779a732c7Sopenharmony_ci#include "advertise_manager.h"
2879a732c7Sopenharmony_ci#include "discovery_manager.h"
2979a732c7Sopenharmony_ci#include "pin_holder.h"
3079a732c7Sopenharmony_ci#include "device_manager_service_listener.h"
3179a732c7Sopenharmony_ci#include "idevice_manager_service_impl.h"
3279a732c7Sopenharmony_ci#include "i_dm_service_impl_ext.h"
3379a732c7Sopenharmony_ci#include "dm_single_instance.h"
3479a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
3579a732c7Sopenharmony_ci#include "dm_account_common_event.h"
3679a732c7Sopenharmony_ci#include "dm_package_common_event.h"
3779a732c7Sopenharmony_ci#include "dm_screen_common_event.h"
3879a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
3979a732c7Sopenharmony_ci#include "dm_publish_common_event.h"
4079a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
4179a732c7Sopenharmony_ci#endif
4279a732c7Sopenharmony_ci
4379a732c7Sopenharmony_cinamespace OHOS {
4479a732c7Sopenharmony_cinamespace DistributedHardware {
4579a732c7Sopenharmony_ciclass DeviceManagerService {
4679a732c7Sopenharmony_ciDM_DECLARE_SINGLE_INSTANCE_BASE(DeviceManagerService);
4779a732c7Sopenharmony_cipublic:
4879a732c7Sopenharmony_ci    DeviceManagerService() {}
4979a732c7Sopenharmony_ci
5079a732c7Sopenharmony_ci    ~DeviceManagerService();
5179a732c7Sopenharmony_ci
5279a732c7Sopenharmony_ci    int32_t Init();
5379a732c7Sopenharmony_ci
5479a732c7Sopenharmony_ci    int32_t InitSoftbusListener();
5579a732c7Sopenharmony_ci
5679a732c7Sopenharmony_ci    void RegisterCallerAppId(const std::string &pkgName);
5779a732c7Sopenharmony_ci
5879a732c7Sopenharmony_ci    void UnRegisterCallerAppId(const std::string &pkgName);
5979a732c7Sopenharmony_ci
6079a732c7Sopenharmony_ci    void UninitSoftbusListener();
6179a732c7Sopenharmony_ci
6279a732c7Sopenharmony_ci    int32_t InitDMServiceListener();
6379a732c7Sopenharmony_ci
6479a732c7Sopenharmony_ci    void UninitDMServiceListener();
6579a732c7Sopenharmony_ci
6679a732c7Sopenharmony_ci    int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
6779a732c7Sopenharmony_ci                                 std::vector<DmDeviceInfo> &deviceList);
6879a732c7Sopenharmony_ci
6979a732c7Sopenharmony_ci    int32_t ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh, bool isWakeUp);
7079a732c7Sopenharmony_ci
7179a732c7Sopenharmony_ci    int32_t GetLocalDeviceInfo(DmDeviceInfo &info);
7279a732c7Sopenharmony_ci
7379a732c7Sopenharmony_ci    int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info);
7479a732c7Sopenharmony_ci
7579a732c7Sopenharmony_ci    int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid);
7679a732c7Sopenharmony_ci
7779a732c7Sopenharmony_ci    int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid);
7879a732c7Sopenharmony_ci
7979a732c7Sopenharmony_ci    int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
8079a732c7Sopenharmony_ci                                 const std::string &extra);
8179a732c7Sopenharmony_ci
8279a732c7Sopenharmony_ci    int32_t StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeInfo,
8379a732c7Sopenharmony_ci                                const std::string &filterOptions);
8479a732c7Sopenharmony_ci
8579a732c7Sopenharmony_ci    int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId);
8679a732c7Sopenharmony_ci
8779a732c7Sopenharmony_ci    int32_t PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo);
8879a732c7Sopenharmony_ci
8979a732c7Sopenharmony_ci    int32_t UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId);
9079a732c7Sopenharmony_ci
9179a732c7Sopenharmony_ci    int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
9279a732c7Sopenharmony_ci                               const std::string &extra);
9379a732c7Sopenharmony_ci
9479a732c7Sopenharmony_ci    int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId);
9579a732c7Sopenharmony_ci
9679a732c7Sopenharmony_ci    int32_t StopAuthenticateDevice(const std::string &pkgName);
9779a732c7Sopenharmony_ci
9879a732c7Sopenharmony_ci    int32_t BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
9979a732c7Sopenharmony_ci        const std::string &bindParam);
10079a732c7Sopenharmony_ci
10179a732c7Sopenharmony_ci    int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash);
10279a732c7Sopenharmony_ci
10379a732c7Sopenharmony_ci    int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string &params);
10479a732c7Sopenharmony_ci
10579a732c7Sopenharmony_ci    void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo);
10679a732c7Sopenharmony_ci
10779a732c7Sopenharmony_ci    int OnSessionOpened(int sessionId, int result);
10879a732c7Sopenharmony_ci
10979a732c7Sopenharmony_ci    void OnSessionClosed(int sessionId);
11079a732c7Sopenharmony_ci
11179a732c7Sopenharmony_ci    void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
11279a732c7Sopenharmony_ci
11379a732c7Sopenharmony_ci    int OnPinHolderSessionOpened(int sessionId, int result);
11479a732c7Sopenharmony_ci
11579a732c7Sopenharmony_ci    void OnPinHolderSessionClosed(int sessionId);
11679a732c7Sopenharmony_ci
11779a732c7Sopenharmony_ci    void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen);
11879a732c7Sopenharmony_ci
11979a732c7Sopenharmony_ci    int32_t DmHiDumper(const std::vector<std::string>& args, std::string &result);
12079a732c7Sopenharmony_ci
12179a732c7Sopenharmony_ci    int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr);
12279a732c7Sopenharmony_ci
12379a732c7Sopenharmony_ci    int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo);
12479a732c7Sopenharmony_ci
12579a732c7Sopenharmony_ci    int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo);
12679a732c7Sopenharmony_ci
12779a732c7Sopenharmony_ci    int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr);
12879a732c7Sopenharmony_ci
12979a732c7Sopenharmony_ci    int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
13079a732c7Sopenharmony_ci        std::string &returnJsonStr);
13179a732c7Sopenharmony_ci
13279a732c7Sopenharmony_ci    int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
13379a732c7Sopenharmony_ci        std::string &returnJsonStr);
13479a732c7Sopenharmony_ci
13579a732c7Sopenharmony_ci    int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
13679a732c7Sopenharmony_ci        std::string &returnJsonStr);
13779a732c7Sopenharmony_ci
13879a732c7Sopenharmony_ci    int32_t RegisterCredentialCallback(const std::string &pkgName);
13979a732c7Sopenharmony_ci
14079a732c7Sopenharmony_ci    int32_t UnRegisterCredentialCallback(const std::string &pkgName);
14179a732c7Sopenharmony_ci
14279a732c7Sopenharmony_ci    int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event);
14379a732c7Sopenharmony_ci
14479a732c7Sopenharmony_ci    int32_t CheckApiPermission(int32_t permissionLevel);
14579a732c7Sopenharmony_ci
14679a732c7Sopenharmony_ci    void LoadHardwareFwkService();
14779a732c7Sopenharmony_ci
14879a732c7Sopenharmony_ci    int32_t GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId, std::string &uuid);
14979a732c7Sopenharmony_ci
15079a732c7Sopenharmony_ci    int32_t GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid, const std::string &appId,
15179a732c7Sopenharmony_ci        std::string &encryptedUuid);
15279a732c7Sopenharmony_ci
15379a732c7Sopenharmony_ci    int32_t GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId, int32_t &networkType);
15479a732c7Sopenharmony_ci    int32_t RegisterUiStateCallback(const std::string &pkgName);
15579a732c7Sopenharmony_ci    int32_t UnRegisterUiStateCallback(const std::string &pkgName);
15679a732c7Sopenharmony_ci    int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode);
15779a732c7Sopenharmony_ci    int32_t ExportAuthCode(std::string &authCode);
15879a732c7Sopenharmony_ci
15979a732c7Sopenharmony_ci    int32_t RegisterPinHolderCallback(const std::string &pkgName);
16079a732c7Sopenharmony_ci    int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
16179a732c7Sopenharmony_ci        DmPinType pinType, const std::string &payload);
16279a732c7Sopenharmony_ci    int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
16379a732c7Sopenharmony_ci        DmPinType pinType, const std::string &payload);
16479a732c7Sopenharmony_ci
16579a732c7Sopenharmony_ci    // The following interfaces are provided since OpenHarmony 4.1 Version.
16679a732c7Sopenharmony_ci    int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
16779a732c7Sopenharmony_ci        const std::map<std::string, std::string> &filterOptions);
16879a732c7Sopenharmony_ci
16979a732c7Sopenharmony_ci    int32_t StopDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam);
17079a732c7Sopenharmony_ci
17179a732c7Sopenharmony_ci    int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
17279a732c7Sopenharmony_ci        const std::map<std::string, std::string> &filterOptions);
17379a732c7Sopenharmony_ci
17479a732c7Sopenharmony_ci    int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam);
17579a732c7Sopenharmony_ci
17679a732c7Sopenharmony_ci    int32_t StartAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam);
17779a732c7Sopenharmony_ci
17879a732c7Sopenharmony_ci    int32_t StopAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam);
17979a732c7Sopenharmony_ci
18079a732c7Sopenharmony_ci    int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
18179a732c7Sopenharmony_ci        const std::map<std::string, std::string> &bindParam);
18279a732c7Sopenharmony_ci
18379a732c7Sopenharmony_ci    int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
18479a732c7Sopenharmony_ci        const std::map<std::string, std::string> &unbindParam);
18579a732c7Sopenharmony_ci
18679a732c7Sopenharmony_ci    int32_t DpAclAdd(const std::string &udid);
18779a732c7Sopenharmony_ci
18879a732c7Sopenharmony_ci    int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType);
18979a732c7Sopenharmony_ci    int32_t IsSameAccount(const std::string &networkId);
19079a732c7Sopenharmony_ci    int32_t InitAccountInfo();
19179a732c7Sopenharmony_ci    int32_t InitScreenLockEvent();
19279a732c7Sopenharmony_ci    bool CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee);
19379a732c7Sopenharmony_ci    bool CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee);
19479a732c7Sopenharmony_ci    void HandleDeviceNotTrust(const std::string &msg);
19579a732c7Sopenharmony_ci    void HandleDeviceTrustedChange(const std::string &msg);
19679a732c7Sopenharmony_ci
19779a732c7Sopenharmony_ci    int32_t SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy);
19879a732c7Sopenharmony_ci    void ClearDiscoveryCache(const std::string &pkgName);
19979a732c7Sopenharmony_ci    void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo);
20079a732c7Sopenharmony_ci    int32_t GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
20179a732c7Sopenharmony_ci        int32_t &screenStatus);
20279a732c7Sopenharmony_ci    void SubscribePackageCommonEvent();
20379a732c7Sopenharmony_ci    int32_t GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid, std::string &networkId);
20479a732c7Sopenharmony_ci    void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode);
20579a732c7Sopenharmony_ciprivate:
20679a732c7Sopenharmony_ci    bool IsDMServiceImplReady();
20779a732c7Sopenharmony_ci    bool IsDMServiceAdapterLoad();
20879a732c7Sopenharmony_ci    bool IsDMImplSoLoaded();
20979a732c7Sopenharmony_ci    void UnloadDMServiceImplSo();
21079a732c7Sopenharmony_ci    void UnloadDMServiceAdapter();
21179a732c7Sopenharmony_ci    void SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId,
21279a732c7Sopenharmony_ci        int32_t bindLevel);
21379a732c7Sopenharmony_ci    void SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId);
21479a732c7Sopenharmony_ci    void SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId);
21579a732c7Sopenharmony_ci    void SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId);
21679a732c7Sopenharmony_ci    void SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids, const std::string &accountId,
21779a732c7Sopenharmony_ci        const std::string &accountName, int32_t userId);
21879a732c7Sopenharmony_ci
21979a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
22079a732c7Sopenharmony_ci    void SubscribeAccountCommonEvent();
22179a732c7Sopenharmony_ci    void AccountCommonEventCallback(int32_t userId, const std::string commonEventType);
22279a732c7Sopenharmony_ci    void SubscribeScreenLockEvent();
22379a732c7Sopenharmony_ci    void ScreenCommonEventCallback(std::string commonEventType);
22479a732c7Sopenharmony_ci    void ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo);
22579a732c7Sopenharmony_ci    int32_t ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &anoyDeviceId);
22679a732c7Sopenharmony_ci    int32_t GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash);
22779a732c7Sopenharmony_ci    void HandleAccountLogout(int32_t userId, const std::string &accountId);
22879a732c7Sopenharmony_ci    void HandleUserRemoved(int32_t preUserId);
22979a732c7Sopenharmony_ci    void HandleUserSwitched(int32_t switchUserId);
23079a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
23179a732c7Sopenharmony_ci    void SubscribePublishCommonEvent();
23279a732c7Sopenharmony_ci    void QueryDependsSwitchState();
23379a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
23479a732c7Sopenharmony_ci#endif
23579a732c7Sopenharmony_ci
23679a732c7Sopenharmony_ciprivate:
23779a732c7Sopenharmony_ci    bool isImplsoLoaded_ = false;
23879a732c7Sopenharmony_ci    bool isAdapterSoLoaded_ = false;
23979a732c7Sopenharmony_ci    std::mutex isImplLoadLock_;
24079a732c7Sopenharmony_ci    std::mutex isAdapterLoadLock_;
24179a732c7Sopenharmony_ci    std::shared_ptr<AdvertiseManager> advertiseMgr_;
24279a732c7Sopenharmony_ci    std::shared_ptr<DiscoveryManager> discoveryMgr_;
24379a732c7Sopenharmony_ci    std::shared_ptr<SoftbusListener> softbusListener_;
24479a732c7Sopenharmony_ci    std::shared_ptr<DeviceManagerServiceListener> listener_;
24579a732c7Sopenharmony_ci    std::shared_ptr<IDeviceManagerServiceImpl> dmServiceImpl_;
24679a732c7Sopenharmony_ci    std::shared_ptr<IDMServiceImplExt> dmServiceImplExt_;
24779a732c7Sopenharmony_ci    std::string localDeviceId_;
24879a732c7Sopenharmony_ci    std::shared_ptr<PinHolder> pinHolder_;
24979a732c7Sopenharmony_ci#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
25079a732c7Sopenharmony_ci    std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager_;
25179a732c7Sopenharmony_ci    std::shared_ptr<DmPackageCommonEventManager> packageCommonEventManager_;
25279a732c7Sopenharmony_ci    std::shared_ptr<DmScreenCommonEventManager> screenCommonEventManager_;
25379a732c7Sopenharmony_ci#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
25479a732c7Sopenharmony_ci    std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager_;
25579a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
25679a732c7Sopenharmony_ci#endif
25779a732c7Sopenharmony_ci};
25879a732c7Sopenharmony_ci} // namespace DistributedHardware
25979a732c7Sopenharmony_ci} // namespace OHOS
26079a732c7Sopenharmony_ci#endif // OHOS_DM_SERVICE_H