• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "device_manager_service_impl.h"
17 
18 #include <functional>
19 
20 #include "dm_anonymous.h"
21 #include "dm_constants.h"
22 #include "dm_distributed_hardware_load.h"
23 #include "dm_log.h"
24 #include "multiple_user_connector.h"
25 #include "permission_manager.h"
26 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
27 #include "dm_common_event_manager.h"
28 #include "common_event_support.h"
29 using namespace OHOS::EventFwk;
30 #endif
31 
32 namespace OHOS {
33 namespace DistributedHardware {
DeviceManagerServiceImpl()34 DeviceManagerServiceImpl::DeviceManagerServiceImpl()
35 {
36     LOGI("DeviceManagerServiceImpl constructor");
37 }
38 
~DeviceManagerServiceImpl()39 DeviceManagerServiceImpl::~DeviceManagerServiceImpl()
40 {
41     LOGI("DeviceManagerServiceImpl destructor");
42 }
43 
Initialize(const std::shared_ptr<IDeviceManagerServiceListener> & listener)44 int32_t DeviceManagerServiceImpl::Initialize(const std::shared_ptr<IDeviceManagerServiceListener> &listener)
45 {
46     LOGI("DeviceManagerServiceImpl Initialize");
47     if (softbusConnector_ == nullptr) {
48         softbusConnector_ = std::make_shared<SoftbusConnector>();
49     }
50     if (hiChainConnector_ == nullptr) {
51         hiChainConnector_ = std::make_shared<HiChainConnector>();
52     }
53     if (deviceStateMgr_ == nullptr) {
54         deviceStateMgr_ = std::make_shared<DmDeviceStateManager>(softbusConnector_, listener, hiChainConnector_);
55         deviceStateMgr_->RegisterSoftbusStateCallback();
56     }
57     if (discoveryMgr_ == nullptr) {
58         discoveryMgr_ = std::make_shared<DmDiscoveryManager>(softbusConnector_, listener);
59     }
60     if (publishMgr_ == nullptr) {
61         publishMgr_ = std::make_shared<DmPublishManager>(softbusConnector_, listener);
62     }
63     if (authMgr_ == nullptr) {
64         authMgr_ = std::make_shared<DmAuthManager>(softbusConnector_, listener, hiChainConnector_);
65         softbusConnector_->GetSoftbusSession()->RegisterSessionCallback(authMgr_);
66         hiChainConnector_->RegisterHiChainCallback(authMgr_);
67     }
68     if (credentialMgr_ == nullptr) {
69         credentialMgr_ = std::make_shared<DmCredentialManager>(hiChainConnector_, listener);
70     }
71 
72     int32_t userId = MultipleUserConnector::GetCurrentAccountUserID();
73     if (userId > 0) {
74         LOGI("get current account user id success");
75         MultipleUserConnector::SetSwitchOldUserId(userId);
76     }
77 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
78     if (commonEventManager_ == nullptr) {
79         commonEventManager_ = std::make_shared<DmCommonEventManager>();
80     }
81     CommomEventCallback callback = std::bind(&DmAuthManager::UserSwitchEventCallback, *authMgr_.get(),
82         std::placeholders::_1);
83     if (commonEventManager_->SubscribeServiceEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED, callback)) {
84         LOGI("subscribe service user switch common event success");
85     }
86 #endif
87     LOGI("Init success, singleton initialized");
88     return DM_OK;
89 }
90 
Release()91 void DeviceManagerServiceImpl::Release()
92 {
93     LOGI("DeviceManagerServiceImpl Release");
94 #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE))
95     commonEventManager_ = nullptr;
96 #endif
97     softbusConnector_->GetSoftbusSession()->UnRegisterSessionCallback();
98     hiChainConnector_->UnRegisterHiChainCallback();
99     authMgr_ = nullptr;
100     deviceStateMgr_ = nullptr;
101     discoveryMgr_ = nullptr;
102     publishMgr_ = nullptr;
103     softbusConnector_ = nullptr;
104     abilityMgr_ = nullptr;
105     hiChainConnector_ = nullptr;
106 }
107 
StartDeviceDiscovery(const std::string & pkgName,const DmSubscribeInfo & subscribeInfo,const std::string & extra)108 int32_t DeviceManagerServiceImpl::StartDeviceDiscovery(const std::string &pkgName, const DmSubscribeInfo &subscribeInfo,
109     const std::string &extra)
110 {
111     if (!PermissionManager::GetInstance().CheckPermission()) {
112         LOGI("The caller does not have permission to call");
113         return ERR_DM_NO_PERMISSION;
114     }
115     if (pkgName.empty()) {
116         LOGE("StartDeviceDiscovery failed, pkgName is empty");
117         return ERR_DM_INPUT_PARA_INVALID;
118     }
119     return discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra);
120 }
121 
StopDeviceDiscovery(const std::string & pkgName,uint16_t subscribeId)122 int32_t DeviceManagerServiceImpl::StopDeviceDiscovery(const std::string &pkgName, uint16_t subscribeId)
123 {
124     if (!PermissionManager::GetInstance().CheckPermission()) {
125         LOGI("The caller does not have permission to call");
126         return ERR_DM_NO_PERMISSION;
127     }
128     if (pkgName.empty()) {
129         LOGE("StopDeviceDiscovery failed, pkgName is empty");
130         return ERR_DM_INPUT_PARA_INVALID;
131     }
132     return discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeId);
133 }
134 
PublishDeviceDiscovery(const std::string & pkgName,const DmPublishInfo & publishInfo)135 int32_t DeviceManagerServiceImpl::PublishDeviceDiscovery(const std::string &pkgName, const DmPublishInfo &publishInfo)
136 {
137     if (!PermissionManager::GetInstance().CheckPermission()) {
138         LOGI("The caller does not have permission to call");
139         return ERR_DM_NO_PERMISSION;
140     }
141     if (pkgName.empty()) {
142         LOGE("PublishDeviceDiscovery failed, pkgName is empty");
143         return ERR_DM_INPUT_PARA_INVALID;
144     }
145     return publishMgr_->PublishDeviceDiscovery(pkgName, publishInfo);
146 }
147 
UnPublishDeviceDiscovery(const std::string & pkgName,int32_t publishId)148 int32_t DeviceManagerServiceImpl::UnPublishDeviceDiscovery(const std::string &pkgName, int32_t publishId)
149 {
150     if (!PermissionManager::GetInstance().CheckPermission()) {
151         LOGI("The caller does not have permission to call");
152         return ERR_DM_NO_PERMISSION;
153     }
154     if (pkgName.empty()) {
155         LOGE("UnPublishDeviceDiscovery failed, pkgName is empty");
156         return ERR_DM_INPUT_PARA_INVALID;
157     }
158     return publishMgr_->UnPublishDeviceDiscovery(pkgName, publishId);
159 }
160 
AuthenticateDevice(const std::string & pkgName,int32_t authType,const std::string & deviceId,const std::string & extra)161 int32_t DeviceManagerServiceImpl::AuthenticateDevice(const std::string &pkgName, int32_t authType,
162     const std::string &deviceId, const std::string &extra)
163 {
164     if (!PermissionManager::GetInstance().CheckPermission()) {
165         LOGI("The caller does not have permission to call");
166         return ERR_DM_NO_PERMISSION;
167     }
168     if (pkgName.empty() || deviceId.empty()) {
169         LOGE("DeviceManagerServiceImpl::AuthenticateDevice failed, pkgName is %s, deviceId is %s, extra is %s",
170             pkgName.c_str(), GetAnonyString(deviceId).c_str(), extra.c_str());
171         return ERR_DM_INPUT_PARA_INVALID;
172     }
173     return authMgr_->AuthenticateDevice(pkgName, authType, deviceId, extra);
174 }
175 
UnAuthenticateDevice(const std::string & pkgName,const std::string & deviceId)176 int32_t DeviceManagerServiceImpl::UnAuthenticateDevice(const std::string &pkgName, const std::string &deviceId)
177 {
178     if (!PermissionManager::GetInstance().CheckPermission()) {
179         LOGI("The caller does not have permission to call");
180         return ERR_DM_NO_PERMISSION;
181     }
182     if (pkgName.empty() || deviceId.empty()) {
183         LOGE("DeviceManagerServiceImpl::AuthenticateDevice failed, pkgName is %s, deviceId is %s",
184             pkgName.c_str(), GetAnonyString(deviceId).c_str());
185         return ERR_DM_INPUT_PARA_INVALID;
186     }
187     return authMgr_->UnAuthenticateDevice(pkgName, deviceId);
188 }
189 
VerifyAuthentication(const std::string & authParam)190 int32_t DeviceManagerServiceImpl::VerifyAuthentication(const std::string &authParam)
191 {
192     if (!PermissionManager::GetInstance().CheckPermission()) {
193         LOGI("The caller does not have permission to call");
194         return ERR_DM_NO_PERMISSION;
195     }
196     return authMgr_->VerifyAuthentication(authParam);
197 }
198 
GetFaParam(std::string & pkgName,DmAuthParam & authParam)199 int32_t DeviceManagerServiceImpl::GetFaParam(std::string &pkgName, DmAuthParam &authParam)
200 {
201     if (pkgName.empty()) {
202         LOGE("GetFaParam failed, pkgName is empty");
203         return ERR_DM_INPUT_PARA_INVALID;
204     }
205     if (authMgr_ != nullptr) {
206         authMgr_->GetAuthenticationParam(authParam);
207     }
208     return DM_OK;
209 }
210 
SetUserOperation(std::string & pkgName,int32_t action,const std::string & params)211 int32_t DeviceManagerServiceImpl::SetUserOperation(std::string &pkgName, int32_t action,
212     const std::string &params)
213 {
214     if (pkgName.empty() || params.empty()) {
215         LOGE("DeviceManagerServiceImpl::SetUserOperation error: Invalid parameter, pkgName: %s, extra: %s",
216             pkgName.c_str(), params.c_str());
217         return ERR_DM_INPUT_PARA_INVALID;
218     }
219     if (authMgr_ != nullptr) {
220         authMgr_->OnUserOperation(action, params);
221     }
222     return DM_OK;
223 }
224 
RegisterDevStateCallback(const std::string & pkgName,const std::string & extra)225 int32_t DeviceManagerServiceImpl::RegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
226 {
227     if (pkgName.empty()) {
228         LOGE("DeviceManagerServiceImpl::RegisterDevStateCallback error: Invalid parameter, pkgName: %s, extra: %s",
229             pkgName.c_str(), extra.c_str());
230         return ERR_DM_INPUT_PARA_INVALID;
231     }
232     if (deviceStateMgr_ != nullptr) {
233         deviceStateMgr_->RegisterDevStateCallback(pkgName, extra);
234     }
235     return DM_OK;
236 }
237 
UnRegisterDevStateCallback(const std::string & pkgName,const std::string & extra)238 int32_t DeviceManagerServiceImpl::UnRegisterDevStateCallback(const std::string &pkgName, const std::string &extra)
239 {
240     if (pkgName.empty()) {
241         LOGE("UnRegisterDevStateCallback failed, pkgName is empty");
242         return ERR_DM_INPUT_PARA_INVALID;
243     }
244     if (deviceStateMgr_!= nullptr) {
245         deviceStateMgr_->UnRegisterDevStateCallback(pkgName, extra);
246     }
247     return DM_OK;
248 }
249 
HandleDeviceOnline(const DmDeviceInfo & info)250 void DeviceManagerServiceImpl::HandleDeviceOnline(const DmDeviceInfo &info)
251 {
252     if (softbusConnector_ != nullptr) {
253         softbusConnector_->HandleDeviceOnline(info);
254     }
255 }
256 
HandleDeviceOffline(const DmDeviceInfo & info)257 void DeviceManagerServiceImpl::HandleDeviceOffline(const DmDeviceInfo &info)
258 {
259     if (softbusConnector_ != nullptr) {
260         softbusConnector_->HandleDeviceOffline(info);
261     }
262 }
263 
OnSessionOpened(int sessionId,int result)264 int DeviceManagerServiceImpl::OnSessionOpened(int sessionId, int result)
265 {
266     return SoftbusSession::OnSessionOpened(sessionId, result);
267 }
268 
OnSessionClosed(int sessionId)269 void DeviceManagerServiceImpl::OnSessionClosed(int sessionId)
270 {
271     SoftbusSession::OnSessionClosed(sessionId);
272 }
273 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)274 void DeviceManagerServiceImpl::OnBytesReceived(int sessionId, const void *data, unsigned int dataLen)
275 {
276     SoftbusSession::OnBytesReceived(sessionId, data, dataLen);
277 }
278 
RequestCredential(const std::string & reqJsonStr,std::string & returnJsonStr)279 int32_t DeviceManagerServiceImpl::RequestCredential(const std::string &reqJsonStr, std::string &returnJsonStr)
280 {
281     if (reqJsonStr.empty()) {
282         LOGE("reqJsonStr is empty");
283         return ERR_DM_INPUT_PARA_INVALID;
284     }
285     if (credentialMgr_== nullptr) {
286         LOGE("credentialMgr_ is nullptr");
287         return ERR_DM_POINT_NULL;
288     }
289     return credentialMgr_->RequestCredential(reqJsonStr, returnJsonStr);
290 }
291 
ImportCredential(const std::string & pkgName,const std::string & credentialInfo)292 int32_t DeviceManagerServiceImpl::ImportCredential(const std::string &pkgName, const std::string &credentialInfo)
293 {
294     if (pkgName.empty() || credentialInfo.empty()) {
295         LOGE("DeviceManagerServiceImpl::ImportCredential failed, pkgName is %s, credentialInfo is %s",
296             pkgName.c_str(), credentialInfo.c_str());
297         return ERR_DM_INPUT_PARA_INVALID;
298     }
299     if (credentialMgr_== nullptr) {
300         LOGE("credentialMgr_ is nullptr");
301         return ERR_DM_POINT_NULL;
302     }
303     return credentialMgr_->ImportCredential(pkgName, credentialInfo);
304 }
305 
DeleteCredential(const std::string & pkgName,const std::string & deleteInfo)306 int32_t DeviceManagerServiceImpl::DeleteCredential(const std::string &pkgName, const std::string &deleteInfo)
307 {
308     if (pkgName.empty() || deleteInfo.empty()) {
309         LOGE("DeviceManagerServiceImpl::DeleteCredential failed, pkgName is %s, deleteInfo is %s",
310             pkgName.c_str(), deleteInfo.c_str());
311         return ERR_DM_INPUT_PARA_INVALID;
312     }
313     if (credentialMgr_== nullptr) {
314         LOGE("credentialMgr_ is nullptr");
315         return ERR_DM_POINT_NULL;
316     }
317     return credentialMgr_->DeleteCredential(pkgName, deleteInfo);
318 }
319 
RegisterCredentialCallback(const std::string & pkgName)320 int32_t DeviceManagerServiceImpl::RegisterCredentialCallback(const std::string &pkgName)
321 {
322     if (pkgName.empty()) {
323         LOGE("RegisterCredentialCallback failed, pkgName is empty");
324         return ERR_DM_INPUT_PARA_INVALID;
325     }
326     if (credentialMgr_ == nullptr) {
327         LOGE("credentialMgr_ is nullptr");
328         return ERR_DM_POINT_NULL;
329     }
330     return credentialMgr_->RegisterCredentialCallback(pkgName);
331 }
332 
UnRegisterCredentialCallback(const std::string & pkgName)333 int32_t DeviceManagerServiceImpl::UnRegisterCredentialCallback(const std::string &pkgName)
334 {
335     if (pkgName.empty()) {
336         LOGE("UnRegisterCredentialCallback failed, pkgName is empty");
337         return ERR_DM_INPUT_PARA_INVALID;
338     }
339     if (credentialMgr_== nullptr) {
340         LOGE("credentialMgr_ is nullptr");
341         return ERR_DM_POINT_NULL;
342     }
343     return credentialMgr_->UnRegisterCredentialCallback(pkgName);
344 }
345 
PraseNotifyEventJson(const std::string & event,nlohmann::json & jsonObject)346 int32_t DeviceManagerServiceImpl::PraseNotifyEventJson(const std::string &event, nlohmann::json &jsonObject)
347 {
348     jsonObject = nlohmann::json::parse(event, nullptr, false);
349     if (jsonObject.is_discarded()) {
350         LOGE("event prase error.");
351         return ERR_DM_FAILED;
352     }
353     if ((!jsonObject.contains("extra")) || (!jsonObject["extra"].is_object())) {
354         LOGE("extra error");
355         return ERR_DM_FAILED;
356     }
357     if ((!jsonObject["extra"].contains("deviceId")) || (!jsonObject["extra"]["deviceId"].is_string())) {
358         LOGE("NotifyEvent deviceId invalid");
359         return ERR_DM_FAILED;
360     }
361     return DM_OK;
362 }
363 
NotifyEvent(const std::string & pkgName,const int32_t eventId,const std::string & event)364 int32_t DeviceManagerServiceImpl::NotifyEvent(const std::string &pkgName, const int32_t eventId,
365     const std::string &event)
366 {
367     LOGI("NotifyEvent begin, pkgName : %s, eventId : %d", pkgName.c_str(), eventId);
368     if ((eventId <= DM_NOTIFY_EVENT_START) || (eventId >= DM_NOTIFY_EVENT_BUTT)) {
369         LOGE("NotifyEvent eventId invalid");
370         return ERR_DM_INPUT_PARA_INVALID;
371     }
372     if (eventId == DM_NOTIFY_EVENT_ONDEVICEREADY) {
373         nlohmann::json jsonObject;
374         if (PraseNotifyEventJson(event, jsonObject) != DM_OK) {
375             LOGE("NotifyEvent json invalid");
376             return ERR_DM_INPUT_PARA_INVALID;
377         }
378         std::string deviceId;
379         jsonObject["extra"]["deviceId"].get_to(deviceId);
380         if (deviceStateMgr_== nullptr) {
381             LOGE("deviceStateMgr_ is nullptr");
382             return ERR_DM_POINT_NULL;
383         }
384         if (deviceStateMgr_->ProcNotifyEvent(pkgName, eventId, deviceId) != DM_OK) {
385             LOGE("NotifyEvent failed");
386             return ERR_DM_INPUT_PARA_INVALID;
387         };
388     }
389     return DM_OK;
390 }
391 
LoadHardwareFwkService()392 void DeviceManagerServiceImpl::LoadHardwareFwkService()
393 {
394     DmDistributedHardwareLoad::GetInstance().LoadDistributedHardwareFwk();
395 }
396 
CreateDMServiceObject(void)397 extern "C" IDeviceManagerServiceImpl *CreateDMServiceObject(void)
398 {
399     return new DeviceManagerServiceImpl;
400 }
401 } // namespace DistributedHardware
402 } // namespace OHOS
403