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