1 /*
2 * Copyright (c) 2021-2023 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 "miscdevice_service_proxy.h"
17
18 #include "hisysevent.h"
19 #include "securec.h"
20
21 #include "sensors_errors.h"
22
23 #undef LOG_TAG
24 #define LOG_TAG "MiscdeviceServiceProxy"
25
26 namespace OHOS {
27 namespace Sensors {
28 using namespace OHOS::HiviewDFX;
29
30 namespace {
31 constexpr int32_t MAX_LIGHT_COUNT = 0XFF;
32 } // namespace
33
MiscdeviceServiceProxy(const sptr<IRemoteObject> & impl)34 MiscdeviceServiceProxy::MiscdeviceServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IMiscdeviceService>(impl)
35 {}
36
Vibrate(int32_t vibratorId,int32_t timeOut,int32_t usage)37 int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage)
38 {
39 MessageParcel data;
40 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
41 MISC_HILOGE("Write descriptor failed");
42 return WRITE_MSG_ERR;
43 }
44 if (!data.WriteInt32(vibratorId)) {
45 MISC_HILOGE("WriteInt32 vibratorId failed");
46 return WRITE_MSG_ERR;
47 }
48 if (!data.WriteInt32(timeOut)) {
49 MISC_HILOGE("WriteUint32 timeOut failed");
50 return WRITE_MSG_ERR;
51 }
52 if (!data.WriteInt32(usage)) {
53 MISC_HILOGE("WriteUint32 usage failed");
54 return WRITE_MSG_ERR;
55 }
56 sptr<IRemoteObject> remote = Remote();
57 CHKPR(remote, ERROR);
58 MessageParcel reply;
59 MessageOption option;
60 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::VIBRATE),
61 data, reply, option);
62 if (ret != NO_ERROR) {
63 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
64 HiSysEvent::EventType::FAULT, "PKG_NAME", "Vibrate", "ERROR_CODE", ret);
65 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
66 }
67 return ret;
68 }
69
StopVibrator(int32_t vibratorId)70 int32_t MiscdeviceServiceProxy::StopVibrator(int32_t vibratorId)
71 {
72 MessageParcel data;
73 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
74 MISC_HILOGE("Write descriptor failed");
75 return WRITE_MSG_ERR;
76 }
77 if (!data.WriteInt32(vibratorId)) {
78 MISC_HILOGE("WriteInt32 vibratorId failed");
79 return WRITE_MSG_ERR;
80 }
81 sptr<IRemoteObject> remote = Remote();
82 CHKPR(remote, ERROR);
83 MessageParcel reply;
84 MessageOption option;
85 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_ALL),
86 data, reply, option);
87 if (ret != NO_ERROR) {
88 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
89 HiSysEvent::EventType::FAULT, "PKG_NAME", "StopVibrator", "ERROR_CODE", ret);
90 MISC_HILOGD("SendRequest failed, ret:%{public}d", ret);
91 }
92 return ret;
93 }
94
PlayVibratorEffect(int32_t vibratorId,const std::string & effect,int32_t loopCount,int32_t usage)95 int32_t MiscdeviceServiceProxy::PlayVibratorEffect(int32_t vibratorId, const std::string &effect,
96 int32_t loopCount, int32_t usage)
97 {
98 MessageParcel data;
99 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
100 MISC_HILOGE("Write descriptor failed");
101 return WRITE_MSG_ERR;
102 }
103 if (!data.WriteInt32(vibratorId)) {
104 MISC_HILOGE("WriteInt32 vibratorId failed");
105 return WRITE_MSG_ERR;
106 }
107 if (!data.WriteString(effect)) {
108 MISC_HILOGE("WriteString effect failed");
109 return WRITE_MSG_ERR;
110 }
111 if (!data.WriteInt32(loopCount)) {
112 MISC_HILOGE("WriteInt32 loopCount failed");
113 return WRITE_MSG_ERR;
114 }
115 if (!data.WriteInt32(usage)) {
116 MISC_HILOGE("Writeint32 usage failed");
117 return WRITE_MSG_ERR;
118 }
119 sptr<IRemoteObject> remote = Remote();
120 CHKPR(remote, ERROR);
121 MessageParcel reply;
122 MessageOption option;
123 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_EFFECT),
124 data, reply, option);
125 if (ret != NO_ERROR) {
126 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
127 HiSysEvent::EventType::FAULT, "PKG_NAME", "PlayVibratorEffect", "ERROR_CODE", ret);
128 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
129 }
130 return ret;
131 }
132
StopVibrator(int32_t vibratorId,const std::string & mode)133 int32_t MiscdeviceServiceProxy::StopVibrator(int32_t vibratorId, const std::string &mode)
134 {
135 MessageParcel data;
136 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
137 MISC_HILOGE("Write descriptor failed");
138 return WRITE_MSG_ERR;
139 }
140 if (!data.WriteInt32(vibratorId)) {
141 MISC_HILOGE("WriteInt32 vibratorId failed");
142 return WRITE_MSG_ERR;
143 }
144 if (!data.WriteString(mode)) {
145 MISC_HILOGE("WriteString mode failed");
146 return WRITE_MSG_ERR;
147 }
148 sptr<IRemoteObject> remote = Remote();
149 CHKPR(remote, ERROR);
150 MessageParcel reply;
151 MessageOption option;
152 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_BY_MODE),
153 data, reply, option);
154 if (ret != NO_ERROR) {
155 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
156 HiSysEvent::EventType::FAULT, "PKG_NAME", "StopVibrator", "ERROR_CODE", ret);
157 MISC_HILOGD("SendRequest failed, ret:%{public}d", ret);
158 }
159 return ret;
160 }
161
IsSupportEffect(const std::string & effect,bool & state)162 int32_t MiscdeviceServiceProxy::IsSupportEffect(const std::string &effect, bool &state)
163 {
164 MessageParcel data;
165 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
166 MISC_HILOGE("Write descriptor failed");
167 return WRITE_MSG_ERR;
168 }
169 if (!data.WriteString(effect)) {
170 MISC_HILOGE("WriteString effect failed");
171 return WRITE_MSG_ERR;
172 }
173 sptr<IRemoteObject> remote = Remote();
174 CHKPR(remote, ERROR);
175 MessageParcel reply;
176 MessageOption option;
177 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::IS_SUPPORT_EFFECT),
178 data, reply, option);
179 if (ret != NO_ERROR) {
180 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
181 HiSysEvent::EventType::FAULT, "PKG_NAME", "IsSupportEffect", "ERROR_CODE", ret);
182 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
183 return ret;
184 }
185 if (!reply.ReadBool(state)) {
186 MISC_HILOGE("Parcel read state failed");
187 return READ_MSG_ERR;
188 }
189 return ret;
190 }
191
192 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
PlayVibratorCustom(int32_t vibratorId,const RawFileDescriptor & rawFd,int32_t usage,const VibrateParameter & parameter)193 int32_t MiscdeviceServiceProxy::PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage,
194 const VibrateParameter ¶meter)
195 {
196 MessageParcel data;
197 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
198 MISC_HILOGE("Write descriptor failed");
199 return WRITE_MSG_ERR;
200 }
201 if (!data.WriteInt32(vibratorId)) {
202 MISC_HILOGE("WriteInt32 vibratorId failed");
203 return WRITE_MSG_ERR;
204 }
205 if (!data.WriteInt32(usage)) {
206 MISC_HILOGE("Writeint32 usage failed");
207 return WRITE_MSG_ERR;
208 }
209 if (!parameter.Marshalling(data)) {
210 MISC_HILOGE("Write adjust parameter failed");
211 return WRITE_MSG_ERR;
212 }
213 if (!data.WriteInt64(rawFd.offset)) {
214 MISC_HILOGE("Writeint64 offset failed");
215 return WRITE_MSG_ERR;
216 }
217 if (!data.WriteInt64(rawFd.length)) {
218 MISC_HILOGE("Writeint64 length failed");
219 return WRITE_MSG_ERR;
220 }
221 if (!data.WriteFileDescriptor(rawFd.fd)) {
222 MISC_HILOGE("WriteFileDescriptor fd failed");
223 return WRITE_MSG_ERR;
224 }
225 sptr<IRemoteObject> remote = Remote();
226 CHKPR(remote, ERROR);
227 MessageParcel reply;
228 MessageOption option;
229 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_CUSTOM),
230 data, reply, option);
231 if (ret != NO_ERROR) {
232 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
233 HiSysEvent::EventType::FAULT, "PKG_NAME", "PlayVibratorCustom", "ERROR_CODE", ret);
234 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
235 }
236 return ret;
237 }
238 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
239
GetLightList()240 std::vector<LightInfoIPC> MiscdeviceServiceProxy::GetLightList()
241 {
242 MessageParcel data;
243 std::vector<LightInfoIPC> lightInfos;
244 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
245 MISC_HILOGE("WriteInterfaceToken failed");
246 return lightInfos;
247 }
248 sptr<IRemoteObject> remote = Remote();
249 if (remote == nullptr) {
250 MISC_HILOGE("remote is nullptr");
251 return lightInfos;
252 }
253 MessageParcel reply;
254 MessageOption option;
255 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::GET_LIGHT_LIST),
256 data, reply, option);
257 if (ret != NO_ERROR) {
258 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
259 return lightInfos;
260 }
261 uint32_t lightCount = 0;
262 if (!reply.ReadUint32(lightCount)) {
263 MISC_HILOGE("Parcel read failed");
264 return lightInfos;
265 }
266 if (lightCount > MAX_LIGHT_COUNT) {
267 lightCount = MAX_LIGHT_COUNT;
268 }
269 LightInfoIPC lightInfo;
270 for (uint32_t i = 0; i < lightCount; ++i) {
271 auto tmpLightInfo = lightInfo.Unmarshalling(reply);
272 CHKPC(tmpLightInfo);
273 lightInfos.push_back(*tmpLightInfo);
274 }
275 return lightInfos;
276 }
277
TurnOn(int32_t lightId,const LightColor & color,const LightAnimationIPC & animation)278 int32_t MiscdeviceServiceProxy::TurnOn(int32_t lightId, const LightColor &color, const LightAnimationIPC &animation)
279 {
280 MessageParcel data;
281 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
282 MISC_HILOGE("Write descriptor failed");
283 return WRITE_MSG_ERR;
284 }
285 if (!data.WriteInt32(lightId)) {
286 MISC_HILOGE("WriteUint32 lightId failed");
287 return WRITE_MSG_ERR;
288 }
289 if (!data.WriteInt32(color.singleColor)) {
290 MISC_HILOGE("Write color failed");
291 return WRITE_MSG_ERR;
292 }
293 if (!animation.Marshalling(data)) {
294 MISC_HILOGE("Write animation failed");
295 return WRITE_MSG_ERR;
296 }
297 sptr<IRemoteObject> remote = Remote();
298 CHKPR(remote, ERROR);
299 MessageParcel reply;
300 MessageOption option;
301 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_ON),
302 data, reply, option);
303 if (ret != NO_ERROR) {
304 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
305 HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOn", "ERROR_CODE", ret);
306 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
307 }
308 return ret;
309 }
310
TurnOff(int32_t lightId)311 int32_t MiscdeviceServiceProxy::TurnOff(int32_t lightId)
312 {
313 MessageParcel data;
314 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
315 MISC_HILOGE("Write descriptor failed");
316 return WRITE_MSG_ERR;
317 }
318 if (!data.WriteInt32(lightId)) {
319 MISC_HILOGE("WriteInt32 lightId failed");
320 return WRITE_MSG_ERR;
321 }
322 sptr<IRemoteObject> remote = Remote();
323 CHKPR(remote, ERROR);
324 MessageParcel reply;
325 MessageOption option;
326 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_OFF),
327 data, reply, option);
328 if (ret != NO_ERROR) {
329 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
330 HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOff", "ERROR_CODE", ret);
331 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
332 }
333 return ret;
334 }
335
GetDelayTime(int32_t & delayTime)336 int32_t MiscdeviceServiceProxy::GetDelayTime(int32_t &delayTime)
337 {
338 MessageParcel data;
339 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
340 MISC_HILOGE("Write descriptor failed");
341 return WRITE_MSG_ERR;
342 }
343 sptr<IRemoteObject> remote = Remote();
344 CHKPR(remote, ERROR);
345 MessageParcel reply;
346 MessageOption option;
347 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::GET_DELAY_TIME),
348 data, reply, option);
349 if (ret != NO_ERROR) {
350 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
351 HiSysEvent::EventType::FAULT, "PKG_NAME", "GetDelayTime", "ERROR_CODE", ret);
352 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
353 }
354 if (!reply.ReadInt32(delayTime)) {
355 MISC_HILOGE("Parcel read failed");
356 return ERROR;
357 }
358 return ret;
359 }
360
PlayPattern(const VibratePattern & pattern,int32_t usage,const VibrateParameter & parameter)361 int32_t MiscdeviceServiceProxy::PlayPattern(const VibratePattern &pattern, int32_t usage,
362 const VibrateParameter ¶meter)
363 {
364 MessageParcel data;
365 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
366 MISC_HILOGE("Write descriptor failed");
367 return WRITE_MSG_ERR;
368 }
369 if (!pattern.Marshalling(data)) {
370 MISC_HILOGE("Marshalling failed");
371 return WRITE_MSG_ERR;
372 }
373 if (!data.WriteInt32(usage)) {
374 MISC_HILOGE("WriteUint32 usage failed");
375 return WRITE_MSG_ERR;
376 }
377 if (!parameter.Marshalling(data)) {
378 MISC_HILOGE("Write adjust parameter failed");
379 return WRITE_MSG_ERR;
380 }
381 sptr<IRemoteObject> remote = Remote();
382 CHKPR(remote, ERROR);
383 MessageParcel reply;
384 MessageOption option;
385 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::PlAY_PATTERN),
386 data, reply, option);
387 if (ret != NO_ERROR) {
388 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
389 HiSysEvent::EventType::FAULT, "PKG_NAME", "PlayPattern", "ERROR_CODE", ret);
390 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
391 }
392 return ret;
393 }
394 } // namespace Sensors
395 } // namespace OHOS
396