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_srv_proxy.h"
17
18 #include "errors.h"
19 #include "ithermal_temp_callback.h"
20 #include "thermal_common.h"
21 #include "thermal_log.h"
22 #include "thermal_srv_ipc_interface_code.h"
23 #include <message_option.h>
24 #include <message_parcel.h>
25
26 namespace OHOS {
27 namespace PowerMgr {
SubscribeThermalTempCallback(const std::vector<std::string> & typeList,const sptr<IThermalTempCallback> & callback)28 bool ThermalSrvProxy::SubscribeThermalTempCallback(
29 const std::vector<std::string>& typeList, const sptr<IThermalTempCallback>& callback)
30 {
31 THERMAL_HILOGD(COMP_FWK, "Enter");
32 sptr<IRemoteObject> remote = Remote();
33 THERMAL_RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
34
35 MessageParcel data;
36 MessageParcel reply;
37 MessageOption option;
38
39 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
40 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
41 return false;
42 }
43
44 THERMAL_WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
45 THERMAL_WRITE_PARCEL_WITH_RET(data, StringVector, typeList, false);
46
47 int ret = remote->SendRequest(
48 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::REG_THERMAL_TEMP_CALLBACK),
49 data, reply, option);
50 if (ret != ERR_OK) {
51 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
52 return false;
53 }
54 return true;
55 }
56
UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback> & callback)57 bool ThermalSrvProxy::UnSubscribeThermalTempCallback(const sptr<IThermalTempCallback>& callback)
58 {
59 THERMAL_HILOGD(COMP_FWK, "Enter");
60 sptr<IRemoteObject> remote = Remote();
61 THERMAL_RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
62
63 MessageParcel data;
64 MessageParcel reply;
65 MessageOption option;
66
67 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
68 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
69 return false;
70 }
71
72 THERMAL_WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
73
74 int ret = remote->SendRequest(
75 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UNREG_THERMAL_TEMP_CALLBACK),
76 data, reply, option);
77 if (ret != ERR_OK) {
78 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
79 return false;
80 }
81 return true;
82 }
83
SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)84 bool ThermalSrvProxy::SubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
85 {
86 THERMAL_HILOGD(COMP_FWK, "Enter");
87 sptr<IRemoteObject> remote = Remote();
88 THERMAL_RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
89
90 MessageParcel data;
91 MessageParcel reply;
92 MessageOption option;
93
94 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
95 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
96 return false;
97 }
98
99 THERMAL_WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
100
101 int ret = remote->SendRequest(
102 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::REG_THERMAL_LEVEL_CALLBACK),
103 data, reply, option);
104 if (ret != ERR_OK) {
105 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
106 return false;
107 }
108 return true;
109 }
110
UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback> & callback)111 bool ThermalSrvProxy::UnSubscribeThermalLevelCallback(const sptr<IThermalLevelCallback>& callback)
112 {
113 THERMAL_HILOGD(COMP_FWK, "Enter");
114 sptr<IRemoteObject> remote = Remote();
115 THERMAL_RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
116
117 MessageParcel data;
118 MessageParcel reply;
119 MessageOption option;
120
121 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
122 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
123 return false;
124 }
125
126 THERMAL_WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
127
128 int ret = remote->SendRequest(
129 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UNREG_THERMAL_LEVEL_CALLBACK),
130 data, reply, option);
131 if (ret != ERR_OK) {
132 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
133 return false;
134 }
135 return true;
136 }
137
SubscribeThermalActionCallback(const std::vector<std::string> & actionList,const std::string & desc,const sptr<IThermalActionCallback> & callback)138 bool ThermalSrvProxy::SubscribeThermalActionCallback(
139 const std::vector<std::string>& actionList, const std::string& desc, const sptr<IThermalActionCallback>& callback)
140 {
141 THERMAL_HILOGD(COMP_FWK, "Enter");
142 sptr<IRemoteObject> remote = Remote();
143 THERMAL_RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
144
145 MessageParcel data;
146 MessageParcel reply;
147 MessageOption option;
148
149 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
150 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
151 return false;
152 }
153
154 THERMAL_WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
155 THERMAL_WRITE_PARCEL_WITH_RET(data, StringVector, actionList, false);
156 THERMAL_WRITE_PARCEL_WITH_RET(data, String, desc, false);
157
158 int ret = remote->SendRequest(
159 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::REG_THERMAL_ACTION_CALLBACK),
160 data, reply, option);
161 if (ret != ERR_OK) {
162 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
163 return false;
164 }
165 return true;
166 }
167
UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback> & callback)168 bool ThermalSrvProxy::UnSubscribeThermalActionCallback(const sptr<IThermalActionCallback>& callback)
169 {
170 THERMAL_HILOGD(COMP_FWK, "Enter");
171 sptr<IRemoteObject> remote = Remote();
172 THERMAL_RETURN_IF_WITH_RET((remote == nullptr) || (callback == nullptr), false);
173
174 MessageParcel data;
175 MessageParcel reply;
176 MessageOption option;
177
178 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
179 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
180 return false;
181 }
182
183 THERMAL_WRITE_PARCEL_WITH_RET(data, RemoteObject, callback->AsObject(), false);
184
185 int ret = remote->SendRequest(
186 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::UNREG_THERMAL_ACTION_CALLBACK),
187 data, reply, option);
188 if (ret != ERR_OK) {
189 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
190 return false;
191 }
192 return true;
193 }
194
GetThermalSrvSensorInfo(const SensorType & type,ThermalSrvSensorInfo & sensorInfo)195 bool ThermalSrvProxy::GetThermalSrvSensorInfo(const SensorType& type, ThermalSrvSensorInfo& sensorInfo)
196 {
197 THERMAL_HILOGD(COMP_FWK, "Enter");
198 sptr<IRemoteObject> remote = Remote();
199 if (remote == nullptr) {
200 return false;
201 }
202 MessageParcel data;
203 MessageParcel reply;
204 MessageOption option;
205
206 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
207 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
208 return false;
209 }
210
211 THERMAL_WRITE_PARCEL_WITH_RET(data, Uint32, static_cast<uint32_t>(type), false);
212
213 int ret = remote->SendRequest(
214 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::GET_SENSOR_INFO), data, reply, option);
215 if (ret != ERR_OK) {
216 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
217 return false;
218 }
219
220 std::unique_ptr<ThermalSrvSensorInfo> info(reply.ReadParcelable<ThermalSrvSensorInfo>());
221 if (!info) {
222 return false;
223 }
224 sensorInfo = *info;
225 return true;
226 }
227
GetThermalLevel(ThermalLevel & level)228 bool ThermalSrvProxy::GetThermalLevel(ThermalLevel& level)
229 {
230 THERMAL_HILOGD(COMP_FWK, "Enter");
231 sptr<IRemoteObject> remote = Remote();
232 if (remote == nullptr) {
233 return false;
234 }
235
236 MessageParcel data;
237 MessageParcel reply;
238 MessageOption option;
239
240 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
241 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
242 return false;
243 }
244
245 int ret = remote->SendRequest(
246 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::GET_TEMP_LEVEL), data, reply, option);
247 if (ret != ERR_OK) {
248 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
249 return false;
250 }
251 uint32_t thermalLevel;
252 THERMAL_READ_PARCEL_WITH_RET(reply, Uint32, thermalLevel, false);
253 level = static_cast<ThermalLevel>(thermalLevel);
254 return true;
255 }
256
GetThermalInfo()257 bool ThermalSrvProxy::GetThermalInfo()
258 {
259 sptr<IRemoteObject> remote = Remote();
260 if (remote == nullptr) {
261 return false;
262 }
263
264 bool result = false;
265 MessageParcel data;
266 MessageParcel reply;
267 MessageOption option;
268
269 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
270 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
271 return result;
272 }
273
274 int ret = remote->SendRequest(
275 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::GET_THERMAL_INFO), data, reply, option);
276 if (ret != ERR_OK) {
277 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
278 return result;
279 }
280 if (!reply.ReadBool(result)) {
281 THERMAL_HILOGE(COMP_FWK, "ReadBool fail");
282 }
283 return true;
284 }
285
SetScene(const std::string & scene)286 bool ThermalSrvProxy::SetScene(const std::string& scene)
287 {
288 THERMAL_HILOGD(COMP_FWK, "Enter");
289 sptr<IRemoteObject> remote = Remote();
290 if (remote == nullptr) {
291 return false;
292 }
293
294 MessageParcel data;
295 MessageParcel reply;
296 MessageOption option;
297
298 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
299 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
300 return false;
301 }
302
303 THERMAL_WRITE_PARCEL_WITH_RET(data, String, scene, false);
304
305 int ret = remote->SendRequest(
306 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::SET_SCENE), data, reply, option);
307 if (ret != ERR_OK) {
308 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
309 return false;
310 }
311 return true;
312 }
313
ShellDump(const std::vector<std::string> & args,uint32_t argc)314 std::string ThermalSrvProxy::ShellDump(const std::vector<std::string>& args, uint32_t argc)
315 {
316 sptr<IRemoteObject> remote = Remote();
317 std::string result = "remote error";
318 THERMAL_RETURN_IF_WITH_RET(remote == nullptr, result);
319
320 MessageParcel data;
321 MessageParcel reply;
322 MessageOption option;
323
324 if (!data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor())) {
325 THERMAL_HILOGE(COMP_FWK, "write descriptor failed!");
326 return 0;
327 }
328
329 data.WriteUint32(argc);
330 for (uint32_t i = 0; i < argc; i++) {
331 data.WriteString(args[i]);
332 }
333 int ret = remote->SendRequest(
334 static_cast<int>(PowerMgr::ThermalMgrInterfaceCode::SHELL_DUMP), data, reply, option);
335 if (ret != ERR_OK) {
336 THERMAL_HILOGE(COMP_FWK, "SendRequest is failed, error code: %{public}d", ret);
337 return result;
338 }
339 result = reply.ReadString();
340 return result;
341 }
342 } // namespace PowerMgr
343 } // namespace OHOS
344