• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_[VIBRATE] = &MiscdeviceServiceStub::VibratePb;
41     baseFuncs_[CANCEL_VIBRATOR] = &MiscdeviceServiceStub::CancelVibratorPb;
42     baseFuncs_[PLAY_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::PlayVibratorEffectPb;
43     baseFuncs_[STOP_VIBRATOR_EFFECT] = &MiscdeviceServiceStub::StopVibratorEffectPb;
44     baseFuncs_[GET_LIGHT_LIST] = &MiscdeviceServiceStub::GetLightListPb;
45     baseFuncs_[TURN_ON] = &MiscdeviceServiceStub::TurnOnPb;
46     baseFuncs_[TURN_OFF] = &MiscdeviceServiceStub::TurnOffPb;
47 }
48 
~MiscdeviceServiceStub()49 MiscdeviceServiceStub::~MiscdeviceServiceStub()
50 {
51     baseFuncs_.clear();
52 }
53 
VibratePb(MessageParcel & data,MessageParcel & reply)54 int32_t MiscdeviceServiceStub::VibratePb(MessageParcel &data, MessageParcel &reply)
55 {
56     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
57     int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
58     if (ret != PERMISSION_GRANTED) {
59         HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
60             HiSysEvent::EventType::SECURITY, "PKG_NAME", "VibratePb", "ERROR_CODE", ret);
61         MISC_HILOGE("result: %{public}d", ret);
62         return PERMISSION_DENIED;
63     }
64     int32_t vibratorId;
65     int32_t duration;
66     int32_t usage;
67     if ((!data.ReadInt32(vibratorId)) || (!data.ReadInt32(duration)) || (!data.ReadInt32(usage))) {
68         MISC_HILOGE("Parcel read failed");
69         return ERROR;
70     }
71     return Vibrate(vibratorId, duration, usage);
72 }
73 
CancelVibratorPb(MessageParcel & data,MessageParcel & reply)74 int32_t MiscdeviceServiceStub::CancelVibratorPb(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         HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
80             HiSysEvent::EventType::SECURITY, "PKG_NAME", "CancelVibratorPb", "ERROR_CODE", ret);
81         MISC_HILOGE("result: %{public}d", ret);
82         return PERMISSION_DENIED;
83     }
84     int32_t vibratorId = data.ReadInt32();
85     return CancelVibrator(vibratorId);
86 }
87 
PlayVibratorEffectPb(MessageParcel & data,MessageParcel & reply)88 int32_t MiscdeviceServiceStub::PlayVibratorEffectPb(MessageParcel &data, MessageParcel &reply)
89 {
90     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
91     int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
92     if (ret != PERMISSION_GRANTED) {
93         HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
94             HiSysEvent::EventType::SECURITY, "PKG_NAME", "PlayVibratorEffectPb", "ERROR_CODE", ret);
95         MISC_HILOGE("result: %{public}d", ret);
96         return PERMISSION_DENIED;
97     }
98     int32_t vibratorId;
99     std::string effect;
100     int32_t count;
101     int32_t usage;
102     if ((!data.ReadInt32(vibratorId)) || (!data.ReadString(effect)) ||
103         (!data.ReadInt32(count)) || (!data.ReadInt32(usage))) {
104         MISC_HILOGE("Parcel read failed");
105         return ERROR;
106     }
107     return PlayVibratorEffect(vibratorId, effect, count, usage);
108 }
109 
StopVibratorEffectPb(MessageParcel & data,MessageParcel & reply)110 int32_t MiscdeviceServiceStub::StopVibratorEffectPb(MessageParcel &data, MessageParcel &reply)
111 {
112     PermissionUtil &permissionUtil = PermissionUtil::GetInstance();
113     int32_t ret = permissionUtil.CheckVibratePermission(this->GetCallingTokenID(), VIBRATE_PERMISSION);
114     if (ret != PERMISSION_GRANTED) {
115         HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "VIBRATOR_PERMISSIONS_EXCEPTION",
116             HiSysEvent::EventType::SECURITY, "PKG_NAME", "StopVibratorEffectPb", "ERROR_CODE", ret);
117         MISC_HILOGE("result: %{public}d", ret);
118         return PERMISSION_DENIED;
119     }
120     int32_t vibratorId = data.ReadInt32();
121     std::string effectType = data.ReadString();
122     return StopVibratorEffect(vibratorId, effectType);
123 }
124 
GetLightListPb(MessageParcel & data,MessageParcel & reply)125 int32_t MiscdeviceServiceStub::GetLightListPb(MessageParcel &data, MessageParcel &reply)
126 {
127     (void)data;
128     std::vector<LightInfo> lightInfos(GetLightList());
129     size_t lightCount = lightInfos.size();
130     MISC_HILOGE("lightCount:%{public}zu", lightCount);
131     if (!reply.WriteUint32(lightCount)) {
132         MISC_HILOGE("Parcel write failed");
133         return WRITE_MSG_ERR;
134     }
135     for (size_t i = 0; i < lightCount; ++i) {
136         if (!reply.WriteBuffer(&lightInfos[i], sizeof(LightInfo))) {
137             MISC_HILOGE("WriteBuffer failed");
138             return WRITE_MSG_ERR;
139         }
140     }
141     return NO_ERROR;
142 }
143 
TurnOnPb(MessageParcel & data,MessageParcel & reply)144 int32_t MiscdeviceServiceStub::TurnOnPb(MessageParcel &data, MessageParcel &reply)
145 {
146     int32_t lightId = data.ReadInt32();
147     const unsigned char *info = data.ReadBuffer(sizeof(LightColor));
148     CHKPR(info, ERROR);
149     LightColor lightColor;
150     if (memcpy_s(&lightColor, sizeof(LightColor), info, sizeof(LightColor)) != EOK) {
151         MISC_HILOGE("memcpy_s lightColor failed");
152         return ERROR;
153     }
154 
155     const unsigned char *buf = data.ReadBuffer(sizeof(LightAnimation));
156     CHKPR(buf, ERROR);
157     LightAnimation lightAnimation;
158     if (memcpy_s(&lightAnimation, sizeof(LightAnimation), buf, sizeof(LightAnimation)) != EOK) {
159         MISC_HILOGE("memcpy_s lightAnimation failed");
160         return ERROR;
161     }
162     return TurnOn(lightId, lightColor, lightAnimation);
163 }
164 
TurnOffPb(MessageParcel & data,MessageParcel & reply)165 int32_t MiscdeviceServiceStub::TurnOffPb(MessageParcel &data, MessageParcel &reply)
166 {
167     int32_t lightId = data.ReadInt32();
168     return TurnOff(lightId);
169 }
170 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)171 int32_t MiscdeviceServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
172                                                MessageOption &option)
173 {
174     MISC_HILOGD("remoterequest begin, cmd : %{public}u", code);
175     std::u16string descriptor = MiscdeviceServiceStub::GetDescriptor();
176     std::u16string remoteDescriptor = data.ReadInterfaceToken();
177     if (descriptor != remoteDescriptor) {
178         MISC_HILOGE("client and service descriptors are inconsistent");
179         return OBJECT_NULL;
180     }
181     auto itFunc = baseFuncs_.find(code);
182     if (itFunc != baseFuncs_.end()) {
183         auto memberFunc = itFunc->second;
184         if (memberFunc != nullptr) {
185             return (this->*memberFunc)(data, reply);
186         }
187     }
188     MISC_HILOGD("remoterequest no member function default process");
189     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
190 }
191 }  // namespace Sensors
192 }  // namespace OHOS
193