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_IMPL_H
17 #define OHOS_DM_SERVICE_IMPL_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "access_control_profile.h"
23 #include "dm_ability_manager.h"
24 #include "dm_auth_manager.h"
25 #include "dm_common_event_manager.h"
26 #include "dm_credential_manager.h"
27 #include "dm_device_info.h"
28 #include "dm_device_state_manager.h"
29 #include "dm_discovery_manager.h"
30 #include "dm_publish_manager.h"
31 #include "idevice_manager_service_impl.h"
32 #include "dm_single_instance.h"
33 #include "softbus_connector.h"
34 #include "mine_hichain_connector.h"
35 
36 namespace OHOS {
37 namespace DistributedHardware {
38 class DeviceManagerServiceImpl : public IDeviceManagerServiceImpl {
39 public:
40     DeviceManagerServiceImpl();
41     virtual ~DeviceManagerServiceImpl();
42 
43     int32_t Initialize(const std::shared_ptr<IDeviceManagerServiceListener> &listener);
44 
45     void Release();
46 
47     int32_t StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
48                                  const std::string &extra);
49 
50     int32_t StartDeviceDiscovery(const std::string &pkgName, const uint16_t subscribeId,
51                                  const std::string &filterOptions);
52 
53     int32_t StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId);
54 
55     int32_t PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo);
56 
57     int32_t UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId);
58 
59     int32_t AuthenticateDevice(const std::string &pkgName, int32_t authType, const std::string &deviceId,
60                                const std::string &extra);
61 
62     int32_t UnAuthenticateDevice(const std::string &pkgName, const std::string &networkId);
63 
64     int32_t BindDevice(const std::string &pkgName, int32_t authType, const std::string &udidHash,
65         const std::string &bindParam);
66 
67     int32_t UnBindDevice(const std::string &pkgName, const std::string &udidHash);
68 
69     int32_t SetUserOperation(std::string &pkgName, int32_t action, const std::string &params);
70 
71     void HandleDeviceStatusChange(DmDeviceState devState, DmDeviceInfo &devInfo);
72 
73     int OnSessionOpened(int sessionId, int result);
74 
75     void OnSessionClosed(int sessionId);
76 
77     void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
78 
79     int OnPinHolderSessionOpened(int sessionId, int result);
80 
81     void OnPinHolderSessionClosed(int sessionId);
82 
83     void OnPinHolderBytesReceived(int sessionId, const void *data, unsigned int dataLen);
84 
85     int32_t RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr);
86 
87     int32_t ImportCredential(const std::string &pkgName, const std::string &credentialInfo);
88 
89     int32_t DeleteCredential(const std::string &pkgName, const std::string &deleteInfo);
90 
91     int32_t MineRequestCredential(const std::string &pkgName, std::string &returnJsonStr);
92 
93     int32_t CheckCredential(const std::string &pkgName, const std::string &reqJsonStr,
94         std::string &returnJsonStr);
95 
96     int32_t ImportCredential(const std::string &pkgName, const std::string &reqJsonStr,
97         std::string &returnJsonStr);
98 
99     int32_t DeleteCredential(const std::string &pkgName, const std::string &reqJsonStr,
100         std::string &returnJsonStr);
101 
102     int32_t RegisterCredentialCallback(const std::string &pkgName);
103 
104     int32_t UnRegisterCredentialCallback(const std::string &pkgName);
105 
106     int32_t NotifyEvent(const std::string &pkgName, const int32_t eventId, const std::string &event);
107 
108     int32_t GetGroupType(std::vector<DmDeviceInfo> &deviceList);
109 
110     int32_t GetUdidHashByNetWorkId(const char *networkId, std::string &deviceId);
111 
112     void LoadHardwareFwkService();
113 
114     int32_t RegisterUiStateCallback(const std::string &pkgName);
115 
116     int32_t UnRegisterUiStateCallback(const std::string &pkgName);
117 
118     int32_t ImportAuthCode(const std::string &pkgName, const std::string &authCode);
119 
120     int32_t ExportAuthCode(std::string &authCode);
121 
122     int32_t BindTarget(const std::string &pkgName, const PeerTargetId &targetId,
123         const std::map<std::string, std::string> &bindParam);
124 
125     std::unordered_map<std::string, DmAuthForm> GetAppTrustDeviceIdList(std::string pkgname);
126     void OnUnbindSessionOpened(int32_t socket, PeerSocketInfo info);
127     void OnUnbindSessionCloseed(int32_t socket);
128     void OnUnbindBytesReceived(int32_t socket, const void *data, uint32_t dataLen);
129 
130     int32_t DpAclAdd(const std::string &udid);
131     int32_t IsSameAccount(const std::string &udid);
132     void AccountCommonEventCallback(int32_t userId, std::string commonEventType);
133     void ScreenCommonEventCallback(std::string commonEventType);
134     int32_t CheckIsSameAccount(const DmAccessCaller &caller, const std::string &srcUdid,
135         const DmAccessCallee &callee, const std::string &sinkUdid);
136     int32_t CheckAccessControl(const DmAccessCaller &caller, const std::string &srcUdid,
137         const DmAccessCallee &callee, const std::string &sinkUdid);
138     void HandleDeviceNotTrust(const std::string &udid);
139     void HandleIdentAccountLogout(const std::string &udid, int32_t userId, const std::string &accountId);
140     void HandleUserRemoved(int32_t preUserId);
141     int32_t StopAuthenticateDevice(const std::string &pkgName);
142     void HandleDeviceScreenStatusChange(DmDeviceInfo &devInfo);
143     void HandleCredentialAuthStatus(const std::string &proofInfo, uint16_t deviceTypeId, int32_t errcode);
144 private:
145     int32_t PraseNotifyEventJson(const std::string &event, nlohmann::json &jsonObject);
146     std::string GetUdidHashByNetworkId(const std::string &networkId);
147     void HandleOffline(DmDeviceState devState, DmDeviceInfo &devInfo);
148     void HandleOnline(DmDeviceState devState, DmDeviceInfo &devInfo);
149     void PutIdenticalAccountToAcl(std::string requestDeviceId, std::string trustDeviceId);
150     std::map<std::string, int32_t> GetDeviceIdAndBindType(int32_t userId, const std::string &accountId);
151 private:
152     std::shared_ptr<DmAuthManager> authMgr_;
153     std::shared_ptr<DmDeviceStateManager> deviceStateMgr_;
154     std::shared_ptr<DmDiscoveryManager> discoveryMgr_;
155     std::shared_ptr<DmPublishManager> publishMgr_;
156     std::shared_ptr<SoftbusConnector> softbusConnector_;
157     std::shared_ptr<DmAbilityManager> abilityMgr_;
158     std::shared_ptr<HiChainConnector> hiChainConnector_;
159     std::shared_ptr<MineHiChainConnector> mineHiChainConnector_;
160     std::shared_ptr<DmCredentialManager> credentialMgr_;
161     std::shared_ptr<DmCommonEventManager> commonEventManager_;
162     std::shared_ptr<HiChainAuthConnector> hiChainAuthConnector_;
163     std::atomic<bool> isCredentialType_ = false;
164 };
165 
166 using CreateDMServiceFuncPtr = IDeviceManagerServiceImpl *(*)(void);
167 } // namespace DistributedHardware
168 } // namespace OHOS
169 #endif // OHOS_DM_SERVICE_IMPL_H
170