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 #include "dm_distributed_hardware_load.h"
17
18 #include "dm_log.h"
19 #include "if_system_ability_manager.h" // for ISystemAbilityManager
20 #include "iservice_registry.h" // for SystemAbilityManagerClient
21 #include "system_ability_definition.h" // for DISTRIBUTED_HARDWARE_SA_ID
22
23 namespace OHOS {
24 namespace DistributedHardware {
25 DM_IMPLEMENT_SINGLE_INSTANCE(DmDistributedHardwareLoad);
26 constexpr uint32_t MAX_LOAD_VALUE = 3;
27 constexpr int32_t DM_OK = 0;
LoadDistributedHardwareFwk(void)28 void DmDistributedHardwareLoad::LoadDistributedHardwareFwk(void)
29 {
30 LOGI("enter DmDistributedHardwareLoad::LoadDistributedHardwareFwk");
31 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
32 if (samgr == nullptr) {
33 LOGE("failed to get system ability mgr.");
34 return;
35 }
36 distributedHardwareLoadCount_++;
37 sptr<DistributedHardwareLoadCallback> distributedHardwareLoadCallback_(new DistributedHardwareLoadCallback());
38 int32_t ret = samgr->LoadSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, distributedHardwareLoadCallback_);
39 if (ret != DM_OK) {
40 LOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
41 DISTRIBUTED_HARDWARE_SA_ID, ret);
42 }
43 return;
44 }
InitDistributedHardwareLoadCount(void)45 void DmDistributedHardwareLoad::InitDistributedHardwareLoadCount(void)
46 {
47 distributedHardwareLoadCount_ = 0;
48 }
GetDistributedHardwareLoadCount()49 uint32_t DmDistributedHardwareLoad::GetDistributedHardwareLoadCount()
50 {
51 return distributedHardwareLoadCount_;
52 }
53
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)54 void DistributedHardwareLoadCallback::OnLoadSystemAbilitySuccess(
55 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
56 {
57 LOGI("DmDistributedHardware Load SA success, systemAbilityId:%{public}d, remoteObject result:%{public}s",
58 systemAbilityId, (remoteObject != nullptr) ? "true" : "false");
59 if (remoteObject == nullptr) {
60 LOGE("remoteObject is nullptr");
61 return;
62 }
63 DmDistributedHardwareLoad::GetInstance().InitDistributedHardwareLoadCount();
64 }
OnLoadSystemAbilityFail(int32_t systemAbilityId)65 void DistributedHardwareLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
66 {
67 LOGE("DmDistributedHardware Load SA failed, systemAbilityId:%{public}d", systemAbilityId);
68
69 if (DmDistributedHardwareLoad::GetInstance().GetDistributedHardwareLoadCount() < MAX_LOAD_VALUE) {
70 DmDistributedHardwareLoad::GetInstance().LoadDistributedHardwareFwk();
71 } else {
72 DmDistributedHardwareLoad::GetInstance().InitDistributedHardwareLoadCount();
73 }
74 return;
75 }
76 } // namespace DistributedHardware
77 } // namespace OHOS
78