1 /*
2 * Copyright (c) 2021 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 "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 {
ThermalMgrClient()27 ThermalMgrClient::ThermalMgrClient() {};
~ThermalMgrClient()28 ThermalMgrClient::~ThermalMgrClient()
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 ThermalMgrClient::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 sptr<IRemoteObject::DeathRecipient> drt = new(std::nothrow) ThermalMgrDeathRecipient(*this);
58 if (drt == nullptr) {
59 THERMAL_HILOGE(COMP_FWK, "Failed to create ThermalMgrDeathRecipient!");
60 return ERR_NO_MEMORY;
61 }
62
63 if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(drt))) {
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 deathRecipient_=drt;
70 THERMAL_HILOGI(COMP_FWK, "Connecting ThermalMgrService success.");
71 return ERR_OK;
72 }
73
OnRemoteDied(const wptr<IRemoteObject> & remote)74 void ThermalMgrClient::ThermalMgrDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
75 {
76 THERMAL_HILOGW(COMP_FWK, "ThermalMgrDeathRecipient::Recv death notice.");
77 client_.ResetProxy(remote);
78 }
79
ResetProxy(const wptr<IRemoteObject> & remote)80 void ThermalMgrClient::ResetProxy(const wptr<IRemoteObject>& remote)
81 {
82 if (remote == nullptr) {
83 THERMAL_HILOGE(COMP_FWK, "ThermalMgrDeathRecipient::OnRemoteDied failed, remote is nullptr.");
84 return;
85 }
86
87 std::lock_guard<std::mutex> lock(mutex_);
88 THERMAL_RETURN_IF(thermalSrv_ == nullptr);
89
90 auto serviceRemote = thermalSrv_->AsObject();
91 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
92 serviceRemote->RemoveDeathRecipient(deathRecipient_);
93 thermalSrv_ = nullptr;
94 }
95 }
96
SubscribeThermalTempCallback(const std::vector<std::string> & typeList,const sptr<IThermalTempCallback> & callback)97 bool ThermalMgrClient::SubscribeThermalTempCallback(
98 const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback)
99 {
100 THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
101 THERMAL_HILOGD(COMP_FWK, "Enter");
102 thermalSrv_->SubscribeThermalTempCallback(typeList, callback);
103 return true;
104 }
105
UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback> & callback)106 bool ThermalMgrClient::UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback)
107 {
108 THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
109 THERMAL_HILOGD(COMP_FWK, "Enter");
110 thermalSrv_->UnSubscribeThermalTempCallback(callback);
111 return true;
112 }
113
SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)114 bool ThermalMgrClient::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
115 {
116 THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
117 THERMAL_HILOGD(COMP_FWK, "Enter");
118 thermalSrv_->SubscribeThermalLevelCallback(callback);
119 return true;
120 }
121
UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)122 bool ThermalMgrClient::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
123 {
124 THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
125 THERMAL_HILOGD(COMP_FWK, "Enter");
126 thermalSrv_->UnSubscribeThermalLevelCallback(callback);
127 return true;
128 }
129
SubscribeThermalActionCallback(const std::vector<std::string> & actionList,const std::string & desc,const sptr<IThermalActionCallback> & callback)130 bool ThermalMgrClient::SubscribeThermalActionCallback(
131 const std::vector<std::string>& actionList, const std::string& desc, const sptr<IThermalActionCallback>& callback)
132 {
133 THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
134 THERMAL_HILOGD(COMP_FWK, "Enter");
135 thermalSrv_->SubscribeThermalActionCallback(actionList, desc, callback);
136 return true;
137 }
138
UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback> & callback)139 bool ThermalMgrClient::UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback)
140 {
141 THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
142 THERMAL_HILOGD(COMP_FWK, "Enter");
143 thermalSrv_->UnSubscribeThermalActionCallback(callback);
144 return true;
145 }
146
GetThermalSrvSensorInfo(const SensorType & type,ThermalSrvSensorInfo & sensorInfo)147 bool ThermalMgrClient::GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo)
148 {
149 if (Connect() != ERR_OK) {
150 return false;
151 }
152 THERMAL_HILOGD(COMP_FWK, "Enter");
153 if (thermalSrv_ == nullptr) {
154 THERMAL_HILOGI(COMP_FWK, "the thermal pointer is null");
155 return false;
156 }
157 bool ret = thermalSrv_->GetThermalSrvSensorInfo(type, sensorInfo);
158 return ret;
159 }
160
GetThermalSensorTemp(const SensorType type)161 int32_t ThermalMgrClient::GetThermalSensorTemp(const SensorType type)
162 {
163 ThermalSrvSensorInfo info;
164 bool ret = GetThermalSrvSensorInfo(type, info);
165 if (!ret) {
166 THERMAL_HILOGI(COMP_FWK, "failed to Get sensor info");
167 }
168 return info.GetTemp();
169 }
170
GetLevel(ThermalLevel & level)171 void ThermalMgrClient::GetLevel(ThermalLevel& level)
172 {
173 THERMAL_HILOGD(COMP_FWK, "Enter");
174 THERMAL_RETURN_IF(Connect() != ERR_OK);
175 thermalSrv_->GetThermalLevel(level);
176 }
177
SetScene(const std::string & scene)178 bool ThermalMgrClient::SetScene(const std::string& scene)
179 {
180 THERMAL_HILOGD(COMP_FWK, "Enter");
181 THERMAL_RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
182 return thermalSrv_->SetScene(scene);
183 }
184
UpdateThermalState(const std::string & tag,const std::string & val,bool isImmed)185 bool ThermalMgrClient::UpdateThermalState(const std::string& tag, const std::string& val, bool isImmed)
186 {
187 THERMAL_HILOGD(COMP_FWK, "Enter");
188 THERMAL_RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
189 return thermalSrv_->UpdateThermalState(tag, val, isImmed);
190 }
191
GetThermalLevel()192 ThermalLevel ThermalMgrClient::GetThermalLevel()
193 {
194 THERMAL_HILOGD(COMP_FWK, "Enter");
195 ThermalLevel level = ThermalLevel::COOL;
196 GetLevel(level);
197 return level;
198 }
199
Dump(const std::vector<std::string> & args)200 std::string ThermalMgrClient::Dump(const std::vector<std::string>& args)
201 {
202 std::string error = "can't connect service";
203 THERMAL_RETURN_IF_WITH_RET(Connect() != ERR_OK, error);
204 THERMAL_HILOGD(COMP_FWK, "Enter");
205 return thermalSrv_->ShellDump(args, args.size());
206 }
207 } // namespace PowerMgr
208 } // namespace OHOS
209