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