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