1 /*
2 * Copyright (c) 2021-2024 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_stub.h"
17
18 #include <string>
19 #include <unistd.h>
20
21 #include "hisysevent.h"
22 #include "ipc_skeleton.h"
23 #include "message_parcel.h"
24 #include "securec.h"
25
26 #include "permission_util.h"
27 #include "sensors_errors.h"
28
29 #undef LOG_TAG
30 #define LOG_TAG "MiscdeviceServiceStub"
31
32 namespace OHOS {
33 namespace Sensors {
34 using namespace OHOS::HiviewDFX;
35
36 namespace {
37 const std::string VIBRATE_PERMISSION = "ohos.permission.VIBRATE";
38 const std::string LIGHT_PERMISSION = "ohos.permission.SYSTEM_LIGHT_CONTROL";
39 } // namespace
40
MiscdeviceServiceStub()41 MiscdeviceServiceStub::MiscdeviceServiceStub() {}
42
~MiscdeviceServiceStub()43 MiscdeviceServiceStub::~MiscdeviceServiceStub() {}
44
VibrateStub(MessageParcel & data,MessageParcel & reply)45 int32_t MiscdeviceServiceStub::VibrateStub(MessageParcel &data, MessageParcel &reply)
46 {
47 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
48 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
49 if (ret != PERMISSION_GRANTED) {
50 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
51 HiSysEvent::EventType::SECURITY, "PKG_NAME", "VibrateStub", "ERROR_CODE", ret);
52 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
53 return PERMISSION_DENIED;
54 }
55 int32_t vibratorId;
56 int32_t duration;
57 int32_t usage;
58 bool systemUsage;
59 if ((!data.ReadInt32(vibratorId)) || (!data.ReadInt32(duration)) || (!data.ReadInt32(usage))||
60 (!data.ReadBool(systemUsage))) {
61 MISC_HILOGE("Parcel read failed");
62 return ERROR;
63 }
64 return Vibrate(vibratorId, duration, usage, systemUsage);
65 }
66
StopVibratorAllStub(MessageParcel & data,MessageParcel & reply)67 int32_t MiscdeviceServiceStub::StopVibratorAllStub(MessageParcel &data, MessageParcel &reply)
68 {
69 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
70 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
71 if (ret != PERMISSION_GRANTED) {
72 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
73 HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorStub", "ERROR_CODE", ret);
74 MISC_HILOGE("Result:%{public}d", ret);
75 return PERMISSION_DENIED;
76 }
77 int32_t vibratorId;
78 if (!data.ReadInt32(vibratorId)) {
79 MISC_HILOGE("Parcel read failed");
80 return ERROR;
81 }
82 return StopVibrator(vibratorId);
83 }
84
PlayVibratorEffectStub(MessageParcel & data,MessageParcel & reply)85 int32_t MiscdeviceServiceStub::PlayVibratorEffectStub(MessageParcel &data, MessageParcel &reply)
86 {
87 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
88 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
89 if (ret != PERMISSION_GRANTED) {
90 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
91 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorEffectStub", "ERROR_CODE", ret);
92 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
93 return PERMISSION_DENIED;
94 }
95 int32_t vibratorId;
96 std::string effect;
97 int32_t count;
98 int32_t usage;
99 bool systemUsage;
100 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) ||
101 (!data.ReadInt32(count)) || (!data.ReadInt32(usage)) || (!data.ReadBool(systemUsage))) {
102 MISC_HILOGE("Parcel read failed");
103 return ERROR;
104 }
105 return PlayVibratorEffect(vibratorId, effect, count, usage, systemUsage);
106 }
107
StopVibratorByModeStub(MessageParcel & data,MessageParcel & reply)108 int32_t MiscdeviceServiceStub::StopVibratorByModeStub(MessageParcel &data, MessageParcel &reply)
109 {
110 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
111 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
112 if (ret != PERMISSION_GRANTED) {
113 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
114 HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorByModeStub", "ERROR_CODE", ret);
115 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
116 return PERMISSION_DENIED;
117 }
118 int32_t vibratorId;
119 std::string mode;
120 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(mode))) {
121 MISC_HILOGE("Parcel read failed");
122 return ERROR;
123 }
124 return StopVibrator(vibratorId, mode);
125 }
126
IsSupportEffectStub(MessageParcel & data,MessageParcel & reply)127 int32_t MiscdeviceServiceStub::IsSupportEffectStub(MessageParcel &data, MessageParcel &reply)
128 {
129 std::string effect;
130 if (!data.ReadString(effect)) {
131 MISC_HILOGE("Parcel read effect failed");
132 return ERROR;
133 }
134 bool state = false;
135 int32_t ret = IsSupportEffect(effect, state);
136 if (ret != NO_ERROR) {
137 MISC_HILOGE("Query support effect failed");
138 return ret;
139 }
140 if (!reply.WriteBool(state)) {
141 MISC_HILOGE("Parcel write state failed");
142 }
143 return ret;
144 }
145
146 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
PlayVibratorCustomStub(MessageParcel & data,MessageParcel & reply)147 int32_t MiscdeviceServiceStub::PlayVibratorCustomStub(MessageParcel &data, MessageParcel &reply)
148 {
149 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
150 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
151 if (ret != PERMISSION_GRANTED) {
152 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
153 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorCustomStub", "ERROR_CODE", ret);
154 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
155 return PERMISSION_DENIED;
156 }
157 int32_t vibratorId;
158 if (!data.ReadInt32(vibratorId)) {
159 MISC_HILOGE("Parcel read vibratorId failed");
160 return ERROR;
161 }
162 int32_t usage;
163 bool systemUsage;
164 if (!data.ReadInt32(usage) || !data.ReadBool(systemUsage)) {
165 MISC_HILOGE("Parcel read failed");
166 return ERROR;
167 }
168 VibrateParameter vibrateParameter;
169 auto parameter = vibrateParameter.Unmarshalling(data);
170 if (!parameter.has_value()) {
171 MISC_HILOGE("Parameter Unmarshalling failed");
172 return ERROR;
173 }
174 RawFileDescriptor rawFd;
175 if (!data.ReadInt64(rawFd.offset)) {
176 MISC_HILOGE("Parcel read offset failed");
177 return ERROR;
178 }
179 if (!data.ReadInt64(rawFd.length)) {
180 MISC_HILOGE("Parcel read length failed");
181 return ERROR;
182 }
183 rawFd.fd = data.ReadFileDescriptor();
184 if (rawFd.fd < 0) {
185 MISC_HILOGE("Parcel ReadFileDescriptor failed");
186 return ERROR;
187 }
188 ret = PlayVibratorCustom(vibratorId, rawFd, usage, systemUsage, parameter.value());
189 if (ret != ERR_OK) {
190 MISC_HILOGD("PlayVibratorCustom failed, ret:%{public}d", ret);
191 return ret;
192 }
193 return ret;
194 }
195 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
196
GetLightListStub(MessageParcel & data,MessageParcel & reply)197 int32_t MiscdeviceServiceStub::GetLightListStub(MessageParcel &data, MessageParcel &reply)
198 {
199 (void)data;
200 std::vector<LightInfoIPC> lightInfos(GetLightList());
201 size_t lightCount = lightInfos.size();
202 MISC_HILOGI("lightCount:%{public}zu", lightCount);
203 if (!reply.WriteUint32(lightCount)) {
204 MISC_HILOGE("Parcel write failed");
205 return WRITE_MSG_ERR;
206 }
207 for (size_t i = 0; i < lightCount; ++i) {
208 if (!lightInfos[i].Marshalling(reply)) {
209 MISC_HILOGE("lightInfo %{public}zu marshalling failed", i);
210 return WRITE_MSG_ERR;
211 }
212 }
213 return NO_ERROR;
214 }
215
TurnOnStub(MessageParcel & data,MessageParcel & reply)216 int32_t MiscdeviceServiceStub::TurnOnStub(MessageParcel &data, MessageParcel &reply)
217 {
218 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
219 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), LIGHT_PERMISSION);
220 if (ret != PERMISSION_GRANTED) {
221 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "LIGHT_PERMISSIONS_EXCEPTION",
222 HiSysEvent::EventType::SECURITY, "PKG_NAME", "turnOnStub", "ERROR_CODE", ret);
223 MISC_HILOGE("CheckLightPermission failed, ret:%{public}d", ret);
224 return PERMISSION_DENIED;
225 }
226 int32_t lightId = data.ReadInt32();
227 LightColor lightColor;
228 lightColor.singleColor = data.ReadInt32();
229 LightAnimationIPC lightAnimation;
230 auto tmpAnimation = lightAnimation.Unmarshalling(data);
231 CHKPR(tmpAnimation, ERROR);
232 return TurnOn(lightId, lightColor, *tmpAnimation);
233 }
234
TurnOffStub(MessageParcel & data,MessageParcel & reply)235 int32_t MiscdeviceServiceStub::TurnOffStub(MessageParcel &data, MessageParcel &reply)
236 {
237 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
238 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), LIGHT_PERMISSION);
239 if (ret != PERMISSION_GRANTED) {
240 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "LIGHT_PERMISSIONS_EXCEPTION",
241 HiSysEvent::EventType::SECURITY, "PKG_NAME", "TurnOffStub", "ERROR_CODE", ret);
242 MISC_HILOGE("CheckLightPermission failed, ret:%{public}d", ret);
243 return PERMISSION_DENIED;
244 }
245 int32_t lightId = data.ReadInt32();
246 return TurnOff(lightId);
247 }
248
ProcessRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)249 int32_t MiscdeviceServiceStub::ProcessRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
250 MessageOption &option)
251 {
252 switch (code) {
253 case static_cast<int32_t>(MiscdeviceInterfaceCode::VIBRATE): {
254 return VibrateStub(data, reply);
255 }
256 case static_cast<int32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_EFFECT): {
257 return PlayVibratorEffectStub(data, reply);
258 }
259 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
260 case static_cast<int32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_CUSTOM): {
261 return PlayVibratorCustomStub(data, reply);
262 }
263 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
264 case static_cast<int32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_ALL): {
265 return StopVibratorAllStub(data, reply);
266 }
267 case static_cast<int32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_BY_MODE): {
268 return StopVibratorByModeStub(data, reply);
269 }
270 case static_cast<int32_t>(MiscdeviceInterfaceCode::IS_SUPPORT_EFFECT): {
271 return IsSupportEffectStub(data, reply);
272 }
273 case static_cast<int32_t>(MiscdeviceInterfaceCode::GET_LIGHT_LIST): {
274 return GetLightListStub(data, reply);
275 }
276 case static_cast<int32_t>(MiscdeviceInterfaceCode::TURN_ON): {
277 return TurnOnStub(data, reply);
278 }
279 case static_cast<int32_t>(MiscdeviceInterfaceCode::TURN_OFF): {
280 return TurnOffStub(data, reply);
281 }
282 case static_cast<int32_t>(MiscdeviceInterfaceCode::PlAY_PATTERN): {
283 return PlayPatternStub(data, reply);
284 }
285 case static_cast<int32_t>(MiscdeviceInterfaceCode::GET_DELAY_TIME): {
286 return GetDelayTimeStub(data, reply);
287 }
288 case static_cast<int32_t>(MiscdeviceInterfaceCode::TRANSFER_CLIENT_REMOTE_OBJECT): {
289 return TransferClientRemoteObjectStub(data, reply);
290 }
291 case static_cast<int32_t>(MiscdeviceInterfaceCode::PLAY_PRIMITIVE_EFFECT): {
292 return PlayPrimitiveEffectStub(data, reply);
293 }
294 case static_cast<int32_t>(MiscdeviceInterfaceCode::GET_VIBRATOR_CAPACITY): {
295 return GetVibratorCapacityStub(data, reply);
296 }
297 default: {
298 MISC_HILOGD("Remoterequest no member function default process");
299 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
300 }
301 }
302 }
303
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)304 int32_t MiscdeviceServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
305 MessageOption &option)
306 {
307 MISC_HILOGD("Remoterequest begin, cmd:%{public}u", code);
308 std::u16string descriptor = MiscdeviceServiceStub::GetDescriptor();
309 std::u16string remoteDescriptor = data.ReadInterfaceToken();
310 if (descriptor != remoteDescriptor) {
311 MISC_HILOGE("Client and service descriptors are inconsistent");
312 return OBJECT_NULL;
313 }
314 return ProcessRemoteRequest(code, data, reply, option);
315 }
316
PlayPatternStub(MessageParcel & data,MessageParcel & reply)317 int32_t MiscdeviceServiceStub::PlayPatternStub(MessageParcel &data, MessageParcel &reply)
318 {
319 CALL_LOG_ENTER;
320 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
321 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
322 if (ret != PERMISSION_GRANTED) {
323 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
324 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayPatternStub", "ERROR_CODE", ret);
325 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
326 return PERMISSION_DENIED;
327 }
328 VibratePattern vibratePattern;
329 auto pattern = vibratePattern.Unmarshalling(data);
330 if (!pattern.has_value()) {
331 MISC_HILOGE("Pattern Unmarshalling failed");
332 return ERROR;
333 }
334 int32_t usage = 0;
335 if (!data.ReadInt32(usage)) {
336 MISC_HILOGE("Parcel read usage failed");
337 return ERROR;
338 }
339 bool systemUsage = false;
340 if (!data.ReadBool(systemUsage)) {
341 MISC_HILOGE("Parcel read systemUsage failed");
342 return ERROR;
343 }
344 VibrateParameter vibrateParameter;
345 auto parameter = vibrateParameter.Unmarshalling(data);
346 if (!parameter.has_value()) {
347 MISC_HILOGE("Parameter Unmarshalling failed");
348 return ERROR;
349 }
350 return PlayPattern(pattern.value(), usage, systemUsage, parameter.value());
351 }
352
GetDelayTimeStub(MessageParcel & data,MessageParcel & reply)353 int32_t MiscdeviceServiceStub::GetDelayTimeStub(MessageParcel &data, MessageParcel &reply)
354 {
355 CALL_LOG_ENTER;
356 int32_t delayTime = 0;
357 if (GetDelayTime(delayTime) != ERR_OK) {
358 MISC_HILOGE("GetDelayTime failed");
359 return ERROR;
360 }
361 if (!reply.WriteInt32(delayTime)) {
362 MISC_HILOGE("Failed, write delayTime failed");
363 return ERROR;
364 }
365 return NO_ERROR;
366 }
367
TransferClientRemoteObjectStub(MessageParcel & data,MessageParcel & reply)368 int32_t MiscdeviceServiceStub::TransferClientRemoteObjectStub(MessageParcel &data, MessageParcel &reply)
369 {
370 CALL_LOG_ENTER;
371 sptr<IRemoteObject> vibratorServiceClient = data.ReadRemoteObject();
372 if (vibratorServiceClient == nullptr) {
373 MISC_HILOGE("vibratorServiceClient is null");
374 return ERROR;
375 }
376 return TransferClientRemoteObject(vibratorServiceClient);
377 }
378
PlayPrimitiveEffectStub(MessageParcel & data,MessageParcel & reply)379 int32_t MiscdeviceServiceStub::PlayPrimitiveEffectStub(MessageParcel &data, MessageParcel &reply)
380 {
381 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
382 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
383 if (ret != PERMISSION_GRANTED) {
384 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
385 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayPrimitiveEffectStub", "ERROR_CODE", ret);
386 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
387 return PERMISSION_DENIED;
388 }
389 int32_t vibratorId = 0;
390 std::string effect;
391 int32_t intensity = 0;
392 int32_t usage = 0;
393 bool systemUsage = false;
394 int32_t count = 0;
395 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) ||
396 (!data.ReadInt32(intensity)) || (!data.ReadInt32(usage)) || (!data.ReadBool(systemUsage)) ||
397 (!data.ReadInt32(count))) {
398 MISC_HILOGE("Parcel read failed");
399 return ERROR;
400 }
401 return PlayPrimitiveEffect(vibratorId, effect, intensity, usage, systemUsage, count);
402 }
403
GetVibratorCapacityStub(MessageParcel & data,MessageParcel & reply)404 int32_t MiscdeviceServiceStub::GetVibratorCapacityStub(MessageParcel &data, MessageParcel &reply)
405 {
406 VibratorCapacity capacity;
407 int32_t ret = GetVibratorCapacity(capacity);
408 if (ret != NO_ERROR) {
409 MISC_HILOGE("Query support custom vibration failed");
410 return ret;
411 }
412 if (!capacity.Marshalling(reply)) {
413 MISC_HILOGE("VibratorCapacity marshalling failed");
414 return WRITE_MSG_ERR;
415 }
416 return ret;
417 }
418 } // namespace Sensors
419 } // namespace OHOS
420