• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     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 ThermalMgrClient::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 ThermalMgrClient::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     ThermalMgrClient::GetInstance().ResetProxy(remote);
93     THERMAL_HILOGI(COMP_FWK, "ThermalMgrDeathRecipient::Recv death notice.");
94 }
95 
SubscribeThermalTempCallback(const std::vector<std::string> & typeList,const sptr<IThermalTempCallback> & callback)96 bool ThermalMgrClient::SubscribeThermalTempCallback(
97     const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback)
98 {
99     THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
100     THERMAL_HILOGD(COMP_FWK, "Enter");
101     thermalSrv_->SubscribeThermalTempCallback(typeList, callback);
102     return true;
103 }
104 
UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback> & callback)105 bool ThermalMgrClient::UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback)
106 {
107     THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
108     THERMAL_HILOGD(COMP_FWK, "Enter");
109     thermalSrv_->UnSubscribeThermalTempCallback(callback);
110     return true;
111 }
112 
SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)113 bool ThermalMgrClient::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
114 {
115     THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
116     THERMAL_HILOGD(COMP_FWK, "Enter");
117     thermalSrv_->SubscribeThermalLevelCallback(callback);
118     return true;
119 }
120 
UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)121 bool ThermalMgrClient::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
122 {
123     THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
124     THERMAL_HILOGD(COMP_FWK, "Enter");
125     thermalSrv_->UnSubscribeThermalLevelCallback(callback);
126     return true;
127 }
128 
SubscribeThermalActionCallback(const std::vector<std::string> & actionList,const std::string & desc,const sptr<IThermalActionCallback> & callback)129 bool ThermalMgrClient::SubscribeThermalActionCallback(
130     const std::vector<std::string>& actionList, const std::string& desc, const sptr<IThermalActionCallback>& callback)
131 {
132     THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
133     THERMAL_HILOGD(COMP_FWK, "Enter");
134     thermalSrv_->SubscribeThermalActionCallback(actionList, desc, callback);
135     return true;
136 }
137 
UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback> & callback)138 bool ThermalMgrClient::UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback)
139 {
140     THERMAL_RETURN_IF_WITH_RET((callback == nullptr) || (Connect() != ERR_OK), false);
141     THERMAL_HILOGD(COMP_FWK, "Enter");
142     thermalSrv_->UnSubscribeThermalActionCallback(callback);
143     return true;
144 }
145 
GetThermalSrvSensorInfo(const SensorType & type,ThermalSrvSensorInfo & sensorInfo)146 bool ThermalMgrClient::GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo)
147 {
148     if (Connect() != ERR_OK) {
149         return false;
150     }
151     THERMAL_HILOGD(COMP_FWK, "Enter");
152     bool ret = thermalSrv_->GetThermalSrvSensorInfo(type, sensorInfo);
153     return ret;
154 }
155 
GetThermalSensorTemp(const SensorType type)156 int32_t ThermalMgrClient::GetThermalSensorTemp(const SensorType type)
157 {
158     ThermalSrvSensorInfo info;
159     bool ret = GetThermalSrvSensorInfo(type, info);
160     if (!ret) {
161         THERMAL_HILOGI(COMP_FWK, "failed  to Get sensor info");
162     }
163     return info.GetTemp();
164 }
165 
GetLevel(ThermalLevel & level)166 void ThermalMgrClient::GetLevel(ThermalLevel& level)
167 {
168     THERMAL_HILOGD(COMP_FWK, "Enter");
169     THERMAL_RETURN_IF(Connect() != ERR_OK);
170     thermalSrv_->GetThermalLevel(level);
171 }
172 
SetScene(const std::string & scene)173 bool ThermalMgrClient::SetScene(const std::string& scene)
174 {
175     THERMAL_HILOGD(COMP_FWK, "Enter");
176     THERMAL_RETURN_IF_WITH_RET(Connect() != ERR_OK, false);
177     return thermalSrv_->SetScene(scene);
178 }
179 
GetThermalLevel()180 ThermalLevel ThermalMgrClient::GetThermalLevel()
181 {
182     THERMAL_HILOGD(COMP_FWK, "Enter");
183     ThermalLevel level = ThermalLevel::COOL;
184     GetLevel(level);
185     return level;
186 }
187 
Dump(const std::vector<std::string> & args)188 std::string ThermalMgrClient::Dump(const std::vector<std::string>& args)
189 {
190     std::string error = "can't connect service";
191     THERMAL_RETURN_IF_WITH_RET(Connect() != ERR_OK, error);
192     THERMAL_HILOGD(COMP_FWK, "Enter");
193     return thermalSrv_->ShellDump(args, args.size());
194 }
195 } // namespace PowerMgr
196 } // namespace OHOS
197