• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_error_type.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     std::lock_guard<std::mutex> autoLock(dmServiceLoadLock_);
31     if (isDMServiceLoading_.load()) {
32         LOGI("DM service is loading.");
33         return DM_OK;
34     }
35     LOGI("start");
36     isDMServiceLoading_.store(true);
37     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38     if (samgr == nullptr) {
39         isDMServiceLoading_.store(false);
40         LOGE("failed to get system ability mgr.");
41         return ERR_DM_POINT_NULL;
42     }
43     sptr<DMLoadCallback> dmLoadCallback_(new DMLoadCallback());
44     int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, dmLoadCallback_);
45     if (ret != DM_OK) {
46         isDMServiceLoading_.store(false);
47         LOGE("Failed to Load DM service, ret code:%{public}d", ret);
48         return ret;
49     }
50     return DM_OK;
51 }
52 
SetLoadFinish(void)53 void DmServiceLoad::SetLoadFinish(void)
54 {
55     std::lock_guard<std::mutex> autoLock(dmServiceLoadLock_);
56     isDMServiceLoading_.store(false);
57 }
58 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)59 void DMLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
60     const sptr<IRemoteObject> &remoteObject)
61 {
62     LOGI("Load DM service success remoteObject result:%{public}s", (remoteObject != nullptr) ? "true" : "false");
63     DmServiceLoad::GetInstance().SetLoadFinish();
64     if (remoteObject == nullptr) {
65         LOGE("remoteObject is nullptr");
66         return;
67     }
68 }
69 
OnLoadSystemAbilityFail(int32_t systemAbilityId)70 void DMLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
71 {
72     LOGE("Load DM service failed.");
73     DmServiceLoad::GetInstance().SetLoadFinish();
74 }
75 } // namespace DistributedHardware
76 } // namespace OHOS
77