1/*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OHOS_DM_SERVICE_H
17#define OHOS_DM_SERVICE_H
18
19#include <string>
20#include <memory>
21#if defined(__LITEOS_M__)
22#include "dm_mutex.h"
23#else
24#include <mutex>
25#endif
26
27#include "advertise_manager.h"
28#include "discovery_manager.h"
29#include "pin_holder.h"
30#include "device_manager_service_listener.h"
31#include "idevice_manager_service_impl.h"
32#include "i_dm_service_impl_ext.h"
33#include "dm_single_instance.h"
34#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
35#include "dm_account_common_event.h"
36#include "dm_package_common_event.h"
37#include "dm_screen_common_event.h"
38#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
39#include "dm_publish_common_event.h"
40#endif // SUPPORT_BLUETOOTH SUPPORT_WIFI
41#endif
42
43namespace OHOS {
44namespace DistributedHardware {
45class DeviceManagerService {
46DM_DECLARE_SINGLE_INSTANCE_BASE(DeviceManagerService);
47public:
48    DeviceManagerService() {}
49
50    ~DeviceManagerService();
51
52    int32_t Init();
53
54    int32_t InitSoftbusListener();
55
56    void RegisterCallerAppId(const std::string &pkgName);
57
58    void UnRegisterCallerAppId(const std::string &pkgName);
59
60    void UninitSoftbusListener();
61
62    int32_t InitDMServiceListener();
63
64    void UninitDMServiceListener();
65
66    int32_t GetTrustedDeviceList(const std::string &pkgName, const std::string &extra,
67                                 std::vector<DmDeviceInfo> &deviceList);
68
69    int32_t ShiftLNNGear(const std::string &pkgName, const std::string &callerId, bool isRefresh, bool isWakeUp);
70
71    int32_t GetLocalDeviceInfo(DmDeviceInfo &info);
72
73    int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info);
74
75    int32_t GetUdidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &udid);
76
77    int32_t GetUuidByNetworkId(const std::string &pkgName, const std::string &netWorkId, std::string &uuid);
78
79    int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
80                                 const std::string &extra);
81
82    int32_t StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeInfo,
83                                const std::string &filterOptions);
84
85    int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId);
86
87    int32_t PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo);
88
89    int32_t UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId);
90
91    int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
92                               const std::string &extra);
93
94    int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId);
95
96    int32_t StopAuthenticateDevice(const std::string &pkgName);
97
98    int32_t BindDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
99        const std::string &bindParam);
100
101    int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash);
102
103    int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string &params);
104
105    void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo);
106
107    int OnSessionOpened(int sessionId, int result);
108
109    void OnSessionClosed(int sessionId);
110
111    void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
112
113    int OnPinHolderSessionOpened(int sessionId, int result);
114
115    void OnPinHolderSessionClosed(int sessionId);
116
117    void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen);
118
119    int32_t DmHiDumper(const std::vector<std::string>& args, std::string &result);
120
121    int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr);
122
123    int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo);
124
125    int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo);
126
127    int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr);
128
129    int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
130        std::string &returnJsonStr);
131
132    int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
133        std::string &returnJsonStr);
134
135    int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
136        std::string &returnJsonStr);
137
138    int32_t RegisterCredentialCallback(const std::string &pkgName);
139
140    int32_t UnRegisterCredentialCallback(const std::string &pkgName);
141
142    int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event);
143
144    int32_t CheckApiPermission(int32_t permissionLevel);
145
146    void LoadHardwareFwkService();
147
148    int32_t GetEncryptedUuidByNetworkId(const std::string &pkgName, const std::string &networkId, std::string &uuid);
149
150    int32_t GenerateEncryptedUuid(const std::string &pkgName, const std::string &uuid, const std::string &appId,
151        std::string &encryptedUuid);
152
153    int32_t GetNetworkTypeByNetworkId(const std::string &pkgName, const std::string &netWorkId, int32_t &networkType);
154    int32_t RegisterUiStateCallback(const std::string &pkgName);
155    int32_t UnRegisterUiStateCallback(const std::string &pkgName);
156    int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode);
157    int32_t ExportAuthCode(std::string &authCode);
158
159    int32_t RegisterPinHolderCallback(const std::string &pkgName);
160    int32_t CreatePinHolder(const std::string &pkgName, const PeerTargetId &targetId,
161        DmPinType pinType, const std::string &payload);
162    int32_t DestroyPinHolder(const std::string &pkgName, const PeerTargetId &targetId,
163        DmPinType pinType, const std::string &payload);
164
165    // The following interfaces are provided since OpenHarmony 4.1 Version.
166    int32_t StartDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
167        const std::map<std::string, std::string> &filterOptions);
168
169    int32_t StopDiscovering(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam);
170
171    int32_t EnableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &discoverParam,
172        const std::map<std::string, std::string> &filterOptions);
173
174    int32_t DisableDiscoveryListener(const std::string &pkgName, const std::map<std::string, std::string> &extraParam);
175
176    int32_t StartAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam);
177
178    int32_t StopAdvertising(const std::string &pkgName, const std::map<std::string, std::string> &advertiseParam);
179
180    int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
181        const std::map<std::string, std::string> &bindParam);
182
183    int32_t UnbindTarget(const std::string &pkgName, const PeerTargetId &targetId,
184        const std::map<std::string, std::string> &unbindParam);
185
186    int32_t DpAclAdd(const std::string &udid);
187
188    int32_t GetDeviceSecurityLevel(const std::string &pkgName, const std::string &networkId, int32_t &networkType);
189    int32_t IsSameAccount(const std::string &networkId);
190    int32_t InitAccountInfo();
191    int32_t InitScreenLockEvent();
192    bool CheckAccessControl(const DmAccessCaller &caller, const DmAccessCallee &callee);
193    bool CheckIsSameAccount(const DmAccessCaller &caller, const DmAccessCallee &callee);
194    void HandleDeviceNotTrust(const std::string &msg);
195    void HandleDeviceTrustedChange(const std::string &msg);
196
197    int32_t SetDnPolicy(const std::string &pkgName, std::map<std::string, std::string> &policy);
198    void ClearDiscoveryCache(const std::string &pkgName);
199    void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo);
200    int32_t GetDeviceScreenStatus(const std::string &pkgName, const std::string &networkId,
201        int32_t &screenStatus);
202    void SubscribePackageCommonEvent();
203    int32_t GetNetworkIdByUdid(const std::string &pkgName, const std::string &udid, std::string &networkId);
204    void HandleCredentialAuthStatus(const std::string &deviceList, uint16_t deviceTypeId, int32_t errcode);
205private:
206    bool IsDMServiceImplReady();
207    bool IsDMServiceAdapterLoad();
208    bool IsDMImplSoLoaded();
209    void UnloadDMServiceImplSo();
210    void UnloadDMServiceAdapter();
211    void SendUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId,
212        int32_t bindLevel);
213    void SendDeviceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId);
214    void SendAppUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId);
215    void SendServiceUnBindBroadCast(const std::vector<std::string> &peerUdids, int32_t userId, uint64_t tokenId);
216    void SendAccountLogoutBroadCast(const std::vector<std::string> &peerUdids, const std::string &accountId,
217        const std::string &accountName, int32_t userId);
218
219#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
220    void SubscribeAccountCommonEvent();
221    void AccountCommonEventCallback(int32_t userId, const std::string commonEventType);
222    void SubscribeScreenLockEvent();
223    void ScreenCommonEventCallback(std::string commonEventType);
224    void ConvertUdidHashToAnoyDeviceId(DmDeviceInfo &deviceInfo);
225    int32_t ConvertUdidHashToAnoyDeviceId(const std::string &udidHash, std::string &anoyDeviceId);
226    int32_t GetUdidHashByAnoyDeviceId(const std::string &anoyDeviceId, std::string &udidHash);
227    void HandleAccountLogout(int32_t userId, const std::string &accountId);
228    void HandleUserRemoved(int32_t preUserId);
229    void HandleUserSwitched(int32_t switchUserId);
230#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
231    void SubscribePublishCommonEvent();
232    void QueryDependsSwitchState();
233#endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
234#endif
235
236private:
237    bool isImplsoLoaded_ = false;
238    bool isAdapterSoLoaded_ = false;
239    std::mutex isImplLoadLock_;
240    std::mutex isAdapterLoadLock_;
241    std::shared_ptr<AdvertiseManager> advertiseMgr_;
242    std::shared_ptr<DiscoveryManager> discoveryMgr_;
243    std::shared_ptr<SoftbusListener> softbusListener_;
244    std::shared_ptr<DeviceManagerServiceListener> listener_;
245    std::shared_ptr<IDeviceManagerServiceImpl> dmServiceImpl_;
246    std::shared_ptr<IDMServiceImplExt> dmServiceImplExt_;
247    std::string localDeviceId_;
248    std::shared_ptr<PinHolder> pinHolder_;
249#if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
250    std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager_;
251    std::shared_ptr<DmPackageCommonEventManager> packageCommonEventManager_;
252    std::shared_ptr<DmScreenCommonEventManager> screenCommonEventManager_;
253#if defined(SUPPORT_BLUETOOTH) || defined(SUPPORT_WIFI)
254    std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager_;
255#endif // SUPPORT_BLUETOOTH  SUPPORT_WIFI
256#endif
257};
258} // namespace DistributedHardware
259} // namespace OHOS
260#endif // OHOS_DM_SERVICE_H