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 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26
27 namespace {
28 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceServiceProxy" };
29 constexpr int32_t MAX_LIGHT_COUNT = 0XFF;
30 }
31
MiscdeviceServiceProxy(const sptr<IRemoteObject> & impl)32 MiscdeviceServiceProxy::MiscdeviceServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IMiscdeviceService>(impl)
33 {}
34
Vibrate(int32_t vibratorId,int32_t timeOut,int32_t usage)35 int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage)
36 {
37 MessageParcel data;
38 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
39 MISC_HILOGE("Write descriptor failed");
40 return WRITE_MSG_ERR;
41 }
42 if (!data.WriteInt32(vibratorId)) {
43 MISC_HILOGE("WriteInt32 vibratorId failed");
44 return WRITE_MSG_ERR;
45 }
46 if (!data.WriteInt32(timeOut)) {
47 MISC_HILOGE("WriteUint32 timeOut failed");
48 return WRITE_MSG_ERR;
49 }
50 if (!data.WriteInt32(usage)) {
51 MISC_HILOGE("WriteUint32 usage failed");
52 return WRITE_MSG_ERR;
53 }
54 sptr<IRemoteObject> remote = Remote();
55 CHKPR(remote, ERROR);
56 MessageParcel reply;
57 MessageOption option;
58 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::VIBRATE),
59 data, reply, option);
60 if (ret != NO_ERROR) {
61 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
62 HiSysEvent::EventType::FAULT, "PKG_NAME", "Vibrate", "ERROR_CODE", ret);
63 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
64 }
65 return ret;
66 }
67
StopVibrator(int32_t vibratorId)68 int32_t MiscdeviceServiceProxy::StopVibrator(int32_t vibratorId)
69 {
70 MessageParcel data;
71 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
72 MISC_HILOGE("Write descriptor failed");
73 return WRITE_MSG_ERR;
74 }
75 if (!data.WriteInt32(vibratorId)) {
76 MISC_HILOGE("WriteInt32 failed");
77 return WRITE_MSG_ERR;
78 }
79 sptr<IRemoteObject> remote = Remote();
80 CHKPR(remote, ERROR);
81 MessageParcel reply;
82 MessageOption option;
83 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_ALL),
84 data, reply, option);
85 if (ret != NO_ERROR) {
86 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
87 HiSysEvent::EventType::FAULT, "PKG_NAME", "StopVibrator", "ERROR_CODE", ret);
88 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
89 }
90 return ret;
91 }
92
PlayVibratorEffect(int32_t vibratorId,const std::string & effect,int32_t loopCount,int32_t usage)93 int32_t MiscdeviceServiceProxy::PlayVibratorEffect(int32_t vibratorId, const std::string &effect,
94 int32_t loopCount, int32_t usage)
95 {
96 MessageParcel data;
97 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
98 MISC_HILOGE("Write descriptor failed");
99 return WRITE_MSG_ERR;
100 }
101 if (!data.WriteInt32(vibratorId)) {
102 MISC_HILOGE("WriteInt32 vibratorId failed");
103 return WRITE_MSG_ERR;
104 }
105 if (!data.WriteString(effect)) {
106 MISC_HILOGE("WriteString effect failed");
107 return WRITE_MSG_ERR;
108 }
109 if (!data.WriteInt32(loopCount)) {
110 MISC_HILOGE("WriteBool effect failed");
111 return WRITE_MSG_ERR;
112 }
113 if (!data.WriteInt32(usage)) {
114 MISC_HILOGE("Writeint32 usage failed");
115 return WRITE_MSG_ERR;
116 }
117 sptr<IRemoteObject> remote = Remote();
118 CHKPR(remote, ERROR);
119 MessageParcel reply;
120 MessageOption option;
121 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_EFFECT),
122 data, reply, option);
123 if (ret != NO_ERROR) {
124 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
125 HiSysEvent::EventType::FAULT, "PKG_NAME", "PlayVibratorEffect", "ERROR_CODE", ret);
126 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
127 }
128 return ret;
129 }
130
StopVibrator(int32_t vibratorId,const std::string & mode)131 int32_t MiscdeviceServiceProxy::StopVibrator(int32_t vibratorId, const std::string &mode)
132 {
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
135 MISC_HILOGE("Write descriptor failed");
136 return WRITE_MSG_ERR;
137 }
138 if (!data.WriteInt32(vibratorId)) {
139 MISC_HILOGE("WriteInt32 vibratorId failed");
140 return WRITE_MSG_ERR;
141 }
142 if (!data.WriteString(mode)) {
143 MISC_HILOGE("WriteString mode failed");
144 return WRITE_MSG_ERR;
145 }
146 sptr<IRemoteObject> remote = Remote();
147 CHKPR(remote, ERROR);
148 MessageParcel reply;
149 MessageOption option;
150 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_BY_MODE),
151 data, reply, option);
152 if (ret != NO_ERROR) {
153 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
154 HiSysEvent::EventType::FAULT, "PKG_NAME", "StopVibrator", "ERROR_CODE", ret);
155 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
156 }
157 return ret;
158 }
159
IsSupportEffect(const std::string & effect,bool & state)160 int32_t MiscdeviceServiceProxy::IsSupportEffect(const std::string &effect, bool &state)
161 {
162 MessageParcel data;
163 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
164 MISC_HILOGE("Write descriptor failed");
165 return WRITE_MSG_ERR;
166 }
167 if (!data.WriteString(effect)) {
168 MISC_HILOGE("WriteString effect failed");
169 return WRITE_MSG_ERR;
170 }
171 sptr<IRemoteObject> remote = Remote();
172 CHKPR(remote, ERROR);
173 MessageParcel reply;
174 MessageOption option;
175 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::IS_SUPPORT_EFFECT),
176 data, reply, option);
177 if (ret != NO_ERROR) {
178 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
179 HiSysEvent::EventType::FAULT, "PKG_NAME", "IsSupportEffect", "ERROR_CODE", ret);
180 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
181 return ret;
182 }
183 if (!reply.ReadBool(state)) {
184 MISC_HILOGE("Parcel read state failed");
185 return READ_MSG_ERR;
186 }
187 return ret;
188 }
189
190 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
PlayVibratorCustom(int32_t vibratorId,const RawFileDescriptor & rawFd,int32_t usage)191 int32_t MiscdeviceServiceProxy::PlayVibratorCustom(int32_t vibratorId, const RawFileDescriptor &rawFd, int32_t usage)
192 {
193 MessageParcel data;
194 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
195 MISC_HILOGE("Write descriptor failed");
196 return WRITE_MSG_ERR;
197 }
198 if (!data.WriteInt32(vibratorId)) {
199 MISC_HILOGE("WriteInt32 vibratorId failed");
200 return WRITE_MSG_ERR;
201 }
202 if (!data.WriteInt32(usage)) {
203 MISC_HILOGE("Writeint32 usage failed");
204 return WRITE_MSG_ERR;
205 }
206 if (!data.WriteInt64(rawFd.offset)) {
207 MISC_HILOGE("Writeint64 offset failed");
208 return WRITE_MSG_ERR;
209 }
210 if (!data.WriteInt64(rawFd.length)) {
211 MISC_HILOGE("Writeint64 length failed");
212 return WRITE_MSG_ERR;
213 }
214 if (!data.WriteFileDescriptor(rawFd.fd)) {
215 MISC_HILOGE("WriteFileDescriptor fd failed");
216 return WRITE_MSG_ERR;
217 }
218 sptr<IRemoteObject> remote = Remote();
219 CHKPR(remote, ERROR);
220 MessageParcel reply;
221 MessageOption option;
222 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_CUSTOM),
223 data, reply, option);
224 if (ret != NO_ERROR) {
225 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
226 HiSysEvent::EventType::FAULT, "PKG_NAME", "PlayVibratorCustom", "ERROR_CODE", ret);
227 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
228 }
229 return ret;
230 }
231 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
232
GetLightList()233 std::vector<LightInfo> MiscdeviceServiceProxy::GetLightList()
234 {
235 MessageParcel data;
236 std::vector<LightInfo> lightInfos;
237 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
238 MISC_HILOGE("WriteInterfaceToken failed");
239 return lightInfos;
240 }
241 sptr<IRemoteObject> remote = Remote();
242 if (remote == nullptr) {
243 MISC_HILOGE("remote is nullptr");
244 return lightInfos;
245 }
246 MessageParcel reply;
247 MessageOption option;
248 int32_t ret = remote->SendRequest(static_cast<uint32_t>(MiscdeviceInterfaceCode::GET_LIGHT_LIST),
249 data, reply, option);
250 if (ret != NO_ERROR) {
251 MISC_HILOGE("SendRequest failed, ret:%{public}d", ret);
252 return lightInfos;
253 }
254 uint32_t lightCount = 0;
255 if (!reply.ReadUint32(lightCount)) {
256 MISC_HILOGE("Parcel read failed");
257 return lightInfos;
258 }
259 if (lightCount > MAX_LIGHT_COUNT) {
260 lightCount = MAX_LIGHT_COUNT;
261 }
262 for (uint32_t i = 0; i < lightCount; ++i) {
263 const uint8_t *info = reply.ReadBuffer(sizeof(LightInfo));
264 if (info == nullptr) {
265 MISC_HILOGE("ReadBuffer failed");
266 return lightInfos;
267 }
268 LightInfo lightInfo;
269 if (memcpy_s(&lightInfo, sizeof(LightInfo), info, sizeof(LightInfo)) != EOK) {
270 MISC_HILOGE("memcpy_s failed");
271 return lightInfos;
272 }
273 lightInfos.push_back(lightInfo);
274 }
275 return lightInfos;
276 }
277
TurnOn(int32_t lightId,const LightColor & color,const LightAnimation & animation)278 int32_t MiscdeviceServiceProxy::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &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.WriteBuffer(&color, sizeof(LightColor))) {
290 MISC_HILOGE("WriteBuffer color failed");
291 return WRITE_MSG_ERR;
292 }
293 if (!data.WriteBuffer(&animation, sizeof(LightAnimation))) {
294 MISC_HILOGE("WriteBuffer 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 } // namespace Sensors
336 } // namespace OHOS
337