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 "mock_thermal_mgr_client.h"
17 #include "thermal_common.h"
18 #include <datetime_ex.h>
19 #include <if_system_ability_manager.h>
20 #include <ipc_skeleton.h>
21 #include <iservice_registry.h>
22 #include <string_ex.h>
23 #include <system_ability_definition.h>
24 
25 namespace OHOS {
26 namespace PowerMgr {
MockThermalMgrClient()27 MockThermalMgrClient::MockThermalMgrClient() {};
~MockThermalMgrClient()28 MockThermalMgrClient::~MockThermalMgrClient()
29 {
30     if (thermalSrv_ != nullptr) {
31         auto remoteObject = thermalSrv_->AsObject();
32         if (remoteObject != nullptr) {
33             remoteObject->RemoveDeathRecipient(deathRecipient_);
34         }
35     }
36 }
37 
Connect()38 ErrCode MockThermalMgrClient::Connect()
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     if (thermalSrv_ != nullptr) {
42         return ERR_OK;
43     }
44 
45     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     if (sam == nullptr) {
47         THERMAL_HILOGE(COMP_FWK, "Failed to get Registry!");
48         return E_GET_SYSTEM_ABILITY_MANAGER_FAILED_THERMAL;
49     }
50 
51     sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(POWER_MANAGER_THERMAL_SERVICE_ID);
52     if (remoteObject_ == nullptr) {
53         THERMAL_HILOGE(COMP_FWK, "GetSystemAbility failed!");
54         return E_GET_THERMAL_SERVICE_FAILED;
55     }
56 
57     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new ThermalMgrDeathRecipient());
58     if (deathRecipient_ == nullptr) {
59         THERMAL_HILOGE(COMP_FWK, "Failed to create ThermalMgrDeathRecipient!");
60         return ERR_NO_MEMORY;
61     }
62 
63     if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
64         THERMAL_HILOGE(COMP_FWK, "Add death recipient to PowerMgr service failed.");
65         return E_ADD_DEATH_RECIPIENT_FAILED_THERMAL;
66     }
67 
68     thermalSrv_ = iface_cast<IThermalSrv>(remoteObject_);
69     THERMAL_HILOGI(COMP_FWK, "Connecting ThermalMgrService success.");
70     return ERR_OK;
71 }
72 
ResetProxy(const wptr<IRemoteObject> & remote)73 void MockThermalMgrClient::ResetProxy(const wptr<IRemoteObject>& remote)
74 {
75     std::lock_guard<std::mutex> lock(mutex_);
76     THERMAL_RETURN_IF(thermalSrv_ == nullptr);
77 
78     auto serviceRemote = thermalSrv_->AsObject();
79     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
80         serviceRemote->RemoveDeathRecipient(deathRecipient_);
81         thermalSrv_ = nullptr;
82     }
83 }
84 
OnRemoteDied(const wptr<IRemoteObject> & remote)85 void MockThermalMgrClient::ThermalMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
86 {
87     if (remote == nullptr) {
88         THERMAL_HILOGE(COMP_FWK, "ThermalMgrDeathRecipient::OnRemoteDied failed, remote is nullptr.");
89         return;
90     }
91 
92     MockThermalMgrClient::GetInstance().ResetProxy(remote);
93     THERMAL_HILOGI(COMP_FWK, "ThermalMgrDeathRecipient::Recv death notice.");
94 }
95 
GetThermalInfo()96 bool MockThermalMgrClient::GetThermalInfo()
97 {
98     THERMAL_RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
99     bool ret = false;
100     ret = thermalSrv_->GetThermalInfo();
101     THERMAL_HILOGD(COMP_FWK, "Calling GetThermalInfo Success");
102     return ret;
103 }
104 } // namespace PowerMgr
105 } // namespace OHOS
106