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 namespace OHOS {
30 namespace Sensors {
31 using namespace OHOS::HiviewDFX;
32
33 namespace {
34 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceServiceStub" };
35 const std::string VIBRATE_PERMISSION = "ohos.permission.VIBRATE";
36 } // namespace
37
MiscdeviceServiceStub()38 MiscdeviceServiceStub::MiscdeviceServiceStub()
39 {
40 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::VIBRATE)] =
41 &MiscdeviceServiceStub::VibrateStub;
42 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_EFFECT)] =
43 &MiscdeviceServiceStub::PlayVibratorEffectStub;
44 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
45 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::PLAY_VIBRATOR_CUSTOM)] =
46 &MiscdeviceServiceStub::PlayVibratorCustomStub;
47 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
48 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_ALL)] =
49 &MiscdeviceServiceStub::StopVibratorAllStub;
50 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::STOP_VIBRATOR_BY_MODE)] =
51 &MiscdeviceServiceStub::StopVibratorByModeStub;
52 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::IS_SUPPORT_EFFECT)] =
53 &MiscdeviceServiceStub::IsSupportEffectStub;
54 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::GET_LIGHT_LIST)] =
55 &MiscdeviceServiceStub::GetLightListStub;
56 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_ON)] =
57 &MiscdeviceServiceStub::TurnOnStub;
58 baseFuncs_[static_cast<uint32_t>(MiscdeviceInterfaceCode::TURN_OFF)] =
59 &MiscdeviceServiceStub::TurnOffStub;
60 }
61
~MiscdeviceServiceStub()62 MiscdeviceServiceStub::~MiscdeviceServiceStub()
63 {
64 baseFuncs_.clear();
65 }
66
VibrateStub(MessageParcel & data,MessageParcel & reply)67 int32_t MiscdeviceServiceStub::VibrateStub(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", "VibrateStub", "ERROR_CODE", ret);
74 MISC_HILOGE("Result:%{public}d", ret);
75 return PERMISSION_DENIED;
76 }
77 int32_t vibratorId;
78 int32_t duration;
79 int32_t usage;
80 if ((!data.ReadInt32(vibratorId)) || (!data.ReadInt32(duration)) || (!data.ReadInt32(usage))) {
81 MISC_HILOGE("Parcel read failed");
82 return ERROR;
83 }
84 return Vibrate(vibratorId, duration, usage);
85 }
86
StopVibratorAllStub(MessageParcel & data,MessageParcel & reply)87 int32_t MiscdeviceServiceStub::StopVibratorAllStub(MessageParcel &data, MessageParcel &reply)
88 {
89 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
90 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
91 if (ret != PERMISSION_GRANTED) {
92 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
93 HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorStub", "ERROR_CODE", ret);
94 MISC_HILOGE("Result:%{public}d", ret);
95 return PERMISSION_DENIED;
96 }
97 int32_t vibratorId;
98 if (!data.ReadInt32(vibratorId)) {
99 MISC_HILOGE("Parcel read failed");
100 return ERROR;
101 }
102 return StopVibrator(vibratorId);
103 }
104
PlayVibratorEffectStub(MessageParcel & data,MessageParcel & reply)105 int32_t MiscdeviceServiceStub::PlayVibratorEffectStub(MessageParcel &data, MessageParcel &reply)
106 {
107 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
108 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
109 if (ret != PERMISSION_GRANTED) {
110 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
111 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorEffectStub", "ERROR_CODE", ret);
112 MISC_HILOGE("Result:%{public}d", ret);
113 return PERMISSION_DENIED;
114 }
115 int32_t vibratorId;
116 std::string effect;
117 int32_t count;
118 int32_t usage;
119 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) ||
120 (!data.ReadInt32(count)) || (!data.ReadInt32(usage))) {
121 MISC_HILOGE("Parcel read failed");
122 return ERROR;
123 }
124 return PlayVibratorEffect(vibratorId, effect, count, usage);
125 }
126
StopVibratorByModeStub(MessageParcel & data,MessageParcel & reply)127 int32_t MiscdeviceServiceStub::StopVibratorByModeStub(MessageParcel &data, MessageParcel &reply)
128 {
129 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
130 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
131 if (ret != PERMISSION_GRANTED) {
132 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
133 HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorByModeStub", "ERROR_CODE", ret);
134 MISC_HILOGE("Result:%{public}d", ret);
135 return PERMISSION_DENIED;
136 }
137 int32_t vibratorId;
138 std::string mode;
139 if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(mode))) {
140 MISC_HILOGE("Parcel read failed");
141 return ERROR;
142 }
143 return StopVibrator(vibratorId, mode);
144 }
145
IsSupportEffectStub(MessageParcel & data,MessageParcel & reply)146 int32_t MiscdeviceServiceStub::IsSupportEffectStub(MessageParcel &data, MessageParcel &reply)
147 {
148 std::string effect;
149 if (!data.ReadString(effect)) {
150 MISC_HILOGE("Parcel read effect failed");
151 return ERROR;
152 }
153 bool state = false;
154 int32_t ret = IsSupportEffect(effect, state);
155 if (ret != NO_ERROR) {
156 MISC_HILOGE("Query support effect failed");
157 return ret;
158 }
159 if (!reply.WriteBool(state)) {
160 MISC_HILOGE("Parcel write state failed");
161 }
162 return ret;
163 }
164
165 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
PlayVibratorCustomStub(MessageParcel & data,MessageParcel & reply)166 int32_t MiscdeviceServiceStub::PlayVibratorCustomStub(MessageParcel &data, MessageParcel &reply)
167 {
168 PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
169 int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
170 if (ret != PERMISSION_GRANTED) {
171 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
172 HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorCustomStub", "ERROR_CODE", ret);
173 MISC_HILOGE("CheckVibratePermission failed, ret:%{public}d", ret);
174 return PERMISSION_DENIED;
175 }
176 int32_t vibratorId;
177 if (!data.ReadInt32(vibratorId)) {
178 MISC_HILOGE("Parcel read vibratorId failed");
179 return ERROR;
180 }
181 int32_t usage;
182 if (!data.ReadInt32(usage)) {
183 MISC_HILOGE("Parcel read usage failed");
184 return ERROR;
185 }
186 RawFileDescriptor rawFd;
187 if (!data.ReadInt64(rawFd.offset)) {
188 MISC_HILOGE("Parcel read offset failed");
189 return ERROR;
190 }
191 if (!data.ReadInt64(rawFd.length)) {
192 MISC_HILOGE("Parcel read length failed");
193 return ERROR;
194 }
195 rawFd.fd = data.ReadFileDescriptor();
196 if (rawFd.fd < 0) {
197 MISC_HILOGE("Parcel ReadFileDescriptor failed");
198 return ERROR;
199 }
200 ret = PlayVibratorCustom(vibratorId, rawFd, usage);
201 close(rawFd.fd);
202 if (ret != ERR_OK) {
203 MISC_HILOGE("PlayVibratorCustom failed, ret:%{public}d", ret);
204 return ret;
205 }
206 return ret;
207 }
208 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
209
GetLightListStub(MessageParcel & data,MessageParcel & reply)210 int32_t MiscdeviceServiceStub::GetLightListStub(MessageParcel &data, MessageParcel &reply)
211 {
212 (void)data;
213 std::vector<LightInfo> lightInfos(GetLightList());
214 size_t lightCount = lightInfos.size();
215 MISC_HILOGE("lightCount:%{public}zu", lightCount);
216 if (!reply.WriteUint32(lightCount)) {
217 MISC_HILOGE("Parcel write failed");
218 return WRITE_MSG_ERR;
219 }
220 for (size_t i = 0; i < lightCount; ++i) {
221 if (!reply.WriteBuffer(&lightInfos[i], sizeof(LightInfo))) {
222 MISC_HILOGE("WriteBuffer failed");
223 return WRITE_MSG_ERR;
224 }
225 }
226 return NO_ERROR;
227 }
228
TurnOnStub(MessageParcel & data,MessageParcel & reply)229 int32_t MiscdeviceServiceStub::TurnOnStub(MessageParcel &data, MessageParcel &reply)
230 {
231 int32_t lightId = data.ReadInt32();
232 const unsigned char *info = data.ReadBuffer(sizeof(LightColor));
233 CHKPR(info, ERROR);
234 LightColor lightColor;
235 if (memcpy_s(&lightColor, sizeof(LightColor), info, sizeof(LightColor)) != EOK) {
236 MISC_HILOGE("memcpy_s lightColor failed");
237 return ERROR;
238 }
239
240 const unsigned char *buf = data.ReadBuffer(sizeof(LightAnimation));
241 CHKPR(buf, ERROR);
242 LightAnimation lightAnimation;
243 if (memcpy_s(&lightAnimation, sizeof(LightAnimation), buf, sizeof(LightAnimation)) != EOK) {
244 MISC_HILOGE("memcpy_s lightAnimation failed");
245 return ERROR;
246 }
247 return TurnOn(lightId, lightColor, lightAnimation);
248 }
249
TurnOffStub(MessageParcel & data,MessageParcel & reply)250 int32_t MiscdeviceServiceStub::TurnOffStub(MessageParcel &data, MessageParcel &reply)
251 {
252 int32_t lightId = data.ReadInt32();
253 return TurnOff(lightId);
254 }
255
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)256 int32_t MiscdeviceServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
257 MessageOption &option)
258 {
259 MISC_HILOGD("Remoterequest begin, cmd:%{public}u", code);
260 std::u16string descriptor = MiscdeviceServiceStub::GetDescriptor();
261 std::u16string remoteDescriptor = data.ReadInterfaceToken();
262 if (descriptor != remoteDescriptor) {
263 MISC_HILOGE("Client and service descriptors are inconsistent");
264 return OBJECT_NULL;
265 }
266 auto itFunc = baseFuncs_.find(code);
267 if (itFunc != baseFuncs_.end()) {
268 auto memberFunc = itFunc->second;
269 if (memberFunc != nullptr) {
270 return (this->*memberFunc)(data, reply);
271 }
272 }
273 MISC_HILOGD("remoterequest no member function default process");
274 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
275 }
276 } // namespace Sensors
277 } // namespace OHOS
278