1 /*
2  * Copyright (c) 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 #include "dm_service_load.h"
17 
18 #include "dm_constants.h"
19 #include "dm_log.h"
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 DM_IMPLEMENT_SINGLE_INSTANCE(DmServiceLoad);
27 
LoadDMService(void)28 int32_t DmServiceLoad::LoadDMService(void)
29 {
30     LOGI("LoadDMService start");
31     if (isDMServiceLoading_) {
32         LOGI("DM service is loading.");
33         return DM_OK;
34     }
35     isDMServiceLoading_ = true;
36     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37     if (samgr == nullptr) {
38         isDMServiceLoading_ = false;
39         LOGE("failed to get system ability mgr.");
40         return ERR_DM_POINT_NULL;
41     }
42     sptr<DMLoadCallback> dmLoadCallback_(new DMLoadCallback());
43     int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, dmLoadCallback_);
44     if (ret != DM_OK) {
45         isDMServiceLoading_ = false;
46         LOGE("Failed to Load DM service, ret code:%{public}d", ret);
47         return ret;
48     }
49     return DM_OK;
50 }
51 
SetLoadFinish(void)52 void DmServiceLoad::SetLoadFinish(void)
53 {
54     isDMServiceLoading_ = false;
55 }
56 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)57 void DMLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
58 {
59     LOGI("Load DM service success remoteObject result:%{public}s", (remoteObject != nullptr) ? "true" : "false");
60     DmServiceLoad::GetInstance().SetLoadFinish();
61     if (remoteObject == nullptr) {
62         LOGE("remoteObject is nullptr");
63         return;
64     }
65 }
66 
OnLoadSystemAbilityFail(int32_t systemAbilityId)67 void DMLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
68 {
69     LOGE("Load DM service failed.");
70     DmServiceLoad::GetInstance().SetLoadFinish();
71 }
72 } // namespace DistributedHardware
73 } // namespace OHOS
74