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_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 {
43 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::VIBRATE)] =
44 &MiscdeviceServiceStub::VibrateStub;
45 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_EFFECT)] =
46 &MiscdeviceServiceStub::PlayVibratorEffectStub;
47 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
48 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_CUSTOM)] =
49 &MiscdeviceServiceStub::PlayVibratorCustomStub;
50 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
51 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_ALL)] =
52 &MiscdeviceServiceStub::StopVibratorAllStub;
53 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_BY_MODE)] =
54 &MiscdeviceServiceStub::StopVibratorByModeStub;
55 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::IS_SUPPORT_EFFECT)] =
56 &MiscdeviceServiceStub::IsSupportEffectStub;
57 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::GET_LIGHT_LIST)] =
58 &MiscdeviceServiceStub::GetLightListStub;
59 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_ON)] =
60 &MiscdeviceServiceStub::TurnOnStub;
61 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_OFF)] =
62 &MiscdeviceServiceStub::TurnOffStub;
63 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::PlAY_PATTERN)] =
64 &MiscdeviceServiceStub::PlayPatternStub;
65 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::GET_DELAY_TIME)] =
66 &MiscdeviceServiceStub::GetDelayTimeStub;
67 }
68
~MiscdeviceServiceStub()69 MiscdeviceServiceStub::~MiscdeviceServiceStub()
70 {
71 baseFuncs_.clear();
72 }
73
VibrateStub(MessageParcel & data,MessageParcel & reply)74 int32_t MiscdeviceServiceStub::VibrateStub(MessageParcel &data, MessageParcel &reply)
75 {
76 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
77 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
78 if (ret != PERMISSION_GRANTED) {
79 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
80 HiSysEvent::EventType::SECURITY, "PKG_NAME", "VibrateStub", "ERROR_CODE", ret);
81 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
82 return PERMISSION_DENIED;
83 }
84 int32_t vibratorId;
85 int32_t duration;
86 int32_t usage;
87 if ((!data.ReadInt32(vibratorId)) || (!data.ReadInt32(duration)) || (!data.ReadInt32(usage))) {
88 MISC_HILOGE("Parcel read failed");
89 return ERROR;
90 }
91 return Vibrate(vibratorId, duration, usage);
92 }
93
StopVibratorAllStub(MessageParcel & data,MessageParcel & reply)94 int32_t MiscdeviceServiceStub::StopVibratorAllStub(MessageParcel &data, MessageParcel &reply)
95 {
96 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
97 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
98 if (ret != PERMISSION_GRANTED) {
99 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
100 HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorStub", "ERROR_CODE", ret);
101 MISC_HILOGE("Result:%{public}d", ret);
102 return PERMISSION_DENIED;
103 }
104 int32_t vibratorId;
105 if (!data.ReadInt32(vibratorId)) {
106 MISC_HILOGE("Parcel read failed");
107 return ERROR;
108 }
109 return StopVibrator(vibratorId);
110 }
111
PlayVibratorEffectStub(MessageParcel & data,MessageParcel & reply)112 int32_t MiscdeviceServiceStub::PlayVibratorEffectStub(MessageParcel &data, MessageParcel &reply)
113 {
114 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
115 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
116 if (ret != PERMISSION_GRANTED) {
117 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
118 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorEffectStub", "ERROR_CODE", ret);
119 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
120 return PERMISSION_DENIED;
121 }
122 int32_t vibratorId;
123 std::string effect;
124 int32_t count;
125 int32_t usage;
126 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) ||
127 (!data.ReadInt32(count)) || (!data.ReadInt32(usage))) {
128 MISC_HILOGE("Parcel read failed");
129 return ERROR;
130 }
131 return PlayVibratorEffect(vibratorId, effect, count, usage);
132 }
133
StopVibratorByModeStub(MessageParcel & data,MessageParcel & reply)134 int32_t MiscdeviceServiceStub::StopVibratorByModeStub(MessageParcel &data, MessageParcel &reply)
135 {
136 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
137 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
138 if (ret != PERMISSION_GRANTED) {
139 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
140 HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorByModeStub", "ERROR_CODE", ret);
141 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
142 return PERMISSION_DENIED;
143 }
144 int32_t vibratorId;
145 std::string mode;
146 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(mode))) {
147 MISC_HILOGE("Parcel read failed");
148 return ERROR;
149 }
150 return StopVibrator(vibratorId, mode);
151 }
152
IsSupportEffectStub(MessageParcel & data,MessageParcel & reply)153 int32_t MiscdeviceServiceStub::IsSupportEffectStub(MessageParcel &data, MessageParcel &reply)
154 {
155 std::string effect;
156 if (!data.ReadString(effect)) {
157 MISC_HILOGE("Parcel read effect failed");
158 return ERROR;
159 }
160 bool state = false;
161 int32_t ret = IsSupportEffect(effect, state);
162 if (ret != NO_ERROR) {
163 MISC_HILOGE("Query support effect failed");
164 return ret;
165 }
166 if (!reply.WriteBool(state)) {
167 MISC_HILOGE("Parcel write state failed");
168 }
169 return ret;
170 }
171
172 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
PlayVibratorCustomStub(MessageParcel & data,MessageParcel & reply)173 int32_t MiscdeviceServiceStub::PlayVibratorCustomStub(MessageParcel &data, MessageParcel &reply)
174 {
175 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
176 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
177 if (ret != PERMISSION_GRANTED) {
178 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
179 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorCustomStub", "ERROR_CODE", ret);
180 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
181 return PERMISSION_DENIED;
182 }
183 int32_t vibratorId;
184 if (!data.ReadInt32(vibratorId)) {
185 MISC_HILOGE("Parcel read vibratorId failed");
186 return ERROR;
187 }
188 int32_t usage;
189 if (!data.ReadInt32(usage)) {
190 MISC_HILOGE("Parcel read usage failed");
191 return ERROR;
192 }
193 VibrateParameter vibrateParameter;
194 auto parameter = vibrateParameter.Unmarshalling(data);
195 if (!parameter.has_value()) {
196 MISC_HILOGE("Parameter Unmarshalling failed");
197 return ERROR;
198 }
199 RawFileDescriptor rawFd;
200 if (!data.ReadInt64(rawFd.offset)) {
201 MISC_HILOGE("Parcel read offset failed");
202 return ERROR;
203 }
204 if (!data.ReadInt64(rawFd.length)) {
205 MISC_HILOGE("Parcel read length failed");
206 return ERROR;
207 }
208 rawFd.fd = data.ReadFileDescriptor();
209 if (rawFd.fd < 0) {
210 MISC_HILOGE("Parcel ReadFileDescriptor failed");
211 return ERROR;
212 }
213 ret = PlayVibratorCustom(vibratorId, rawFd, usage, parameter.value());
214 close(rawFd.fd);
215 if (ret != ERR_OK) {
216 MISC_HILOGE("PlayVibratorCustom failed, ret:%{public}d", ret);
217 return ret;
218 }
219 return ret;
220 }
221 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
222
GetLightListStub(MessageParcel & data,MessageParcel & reply)223 int32_t MiscdeviceServiceStub::GetLightListStub(MessageParcel &data, MessageParcel &reply)
224 {
225 (void)data;
226 std::vector<LightInfoIPC> lightInfos(GetLightList());
227 size_t lightCount = lightInfos.size();
228 MISC_HILOGI("lightCount:%{public}zu", lightCount);
229 if (!reply.WriteUint32(lightCount)) {
230 MISC_HILOGE("Parcel write failed");
231 return WRITE_MSG_ERR;
232 }
233 for (size_t i = 0; i < lightCount; ++i) {
234 if (!lightInfos[i].Marshalling(reply)) {
235 MISC_HILOGE("lightInfo %{public}zu marshalling failed", i);
236 return WRITE_MSG_ERR;
237 }
238 }
239 return NO_ERROR;
240 }
241
TurnOnStub(MessageParcel & data,MessageParcel & reply)242 int32_t MiscdeviceServiceStub::TurnOnStub(MessageParcel &data, MessageParcel &reply)
243 {
244 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
245 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), LIGHT_PERMISSION);
246 if (ret != PERMISSION_GRANTED) {
247 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "LIGHT_PERMISSIONS_EXCEPTION",
248 HiSysEvent::EventType::SECURITY, "PKG_NAME", "turnOnStub", "ERROR_CODE", ret);
249 MISC_HILOGE("CheckLightPermission failed, ret:%{public}d", ret);
250 return PERMISSION_DENIED;
251 }
252 int32_t lightId = data.ReadInt32();
253 LightColor lightColor;
254 lightColor.singleColor = data.ReadInt32();
255 LightAnimationIPC lightAnimation;
256 auto tmpAnimation = lightAnimation.Unmarshalling(data);
257 CHKPR(tmpAnimation, ERROR);
258 return TurnOn(lightId, lightColor, *tmpAnimation);
259 }
260
TurnOffStub(MessageParcel & data,MessageParcel & reply)261 int32_t MiscdeviceServiceStub::TurnOffStub(MessageParcel &data, MessageParcel &reply)
262 {
263 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
264 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), LIGHT_PERMISSION);
265 if (ret != PERMISSION_GRANTED) {
266 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "LIGHT_PERMISSIONS_EXCEPTION",
267 HiSysEvent::EventType::SECURITY, "PKG_NAME", "TurnOffStub", "ERROR_CODE", ret);
268 MISC_HILOGE("CheckLightPermission failed, ret:%{public}d", ret);
269 return PERMISSION_DENIED;
270 }
271 int32_t lightId = data.ReadInt32();
272 return TurnOff(lightId);
273 }
274
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)275 int32_t MiscdeviceServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
276 MessageOption &option)
277 {
278 MISC_HILOGD("Remoterequest begin, cmd:%{public}u", code);
279 std::u16string descriptor = MiscdeviceServiceStub::GetDescriptor();
280 std::u16string remoteDescriptor = data.ReadInterfaceToken();
281 if (descriptor != remoteDescriptor) {
282 MISC_HILOGE("Client and service descriptors are inconsistent");
283 return OBJECT_NULL;
284 }
285 auto itFunc = baseFuncs_.find(code);
286 if (itFunc != baseFuncs_.end()) {
287 auto memberFunc = itFunc->second;
288 if (memberFunc != nullptr) {
289 return (this->*memberFunc)(data, reply);
290 }
291 }
292 MISC_HILOGD("Remoterequest no member function default process");
293 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
294 }
295
PlayPatternStub(MessageParcel & data,MessageParcel & reply)296 int32_t MiscdeviceServiceStub::PlayPatternStub(MessageParcel &data, MessageParcel &reply)
297 {
298 CALL_LOG_ENTER;
299 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
300 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
301 if (ret != PERMISSION_GRANTED) {
302 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
303 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayPatternStub", "ERROR_CODE", ret);
304 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
305 return PERMISSION_DENIED;
306 }
307 VibratePattern vibratePattern;
308 auto pattern = vibratePattern.Unmarshalling(data);
309 if (!pattern.has_value()) {
310 MISC_HILOGE("Pattern Unmarshalling failed");
311 return ERROR;
312 }
313 int32_t usage = 0;
314 if (!data.ReadInt32(usage)) {
315 MISC_HILOGE("Parcel read usage failed");
316 return ERROR;
317 }
318 VibrateParameter vibrateParameter;
319 auto parameter = vibrateParameter.Unmarshalling(data);
320 if (!parameter.has_value()) {
321 MISC_HILOGE("Parameter Unmarshalling failed");
322 return ERROR;
323 }
324 return PlayPattern(pattern.value(), usage, parameter.value());
325 }
326
GetDelayTimeStub(MessageParcel & data,MessageParcel & reply)327 int32_t MiscdeviceServiceStub::GetDelayTimeStub(MessageParcel &data, MessageParcel &reply)
328 {
329 CALL_LOG_ENTER;
330 int32_t delayTime = 0;
331 if (GetDelayTime(delayTime) != ERR_OK) {
332 MISC_HILOGE("GetDelayTime failed");
333 return ERROR;
334 }
335 if (!reply.WriteInt32(delayTime)) {
336 MISC_HILOGE("Failed, write delayTime failed");
337 return ERROR;
338 }
339 return NO_ERROR;
340 }
341 } // namespace Sensors
342 } // namespace OHOS
343