1 /*
2 * Copyright (c) 2021 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 #include "sensors_errors.h"
21
22 namespace OHOS {
23 namespace Sensors {
24 using namespace OHOS::HiviewDFX;
25
26 namespace {
27 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceServiceProxy" };
28 constexpr int32_t MAX_LIGHT_COUNT = 0XFF;
29 }
30
MiscdeviceServiceProxy(const sptr<IRemoteObject> & impl)31 MiscdeviceServiceProxy::MiscdeviceServiceProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IMiscdeviceService>(impl)
32 {}
33
Vibrate(int32_t vibratorId,int32_t timeOut,int32_t usage)34 int32_t MiscdeviceServiceProxy::Vibrate(int32_t vibratorId, int32_t timeOut, int32_t usage)
35 {
36 MessageParcel data;
37 MessageParcel reply;
38 MessageOption option;
39 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
40 MISC_HILOGE("write descriptor failed");
41 return WRITE_MSG_ERR;
42 }
43 if (!data.WriteInt32(vibratorId)) {
44 MISC_HILOGE("WriteInt32 vibratorId failed");
45 return WRITE_MSG_ERR;
46 }
47 if (!data.WriteInt32(timeOut)) {
48 MISC_HILOGE("WriteUint32 timeOut failed");
49 return WRITE_MSG_ERR;
50 }
51 if (!data.WriteInt32(usage)) {
52 MISC_HILOGE("WriteUint32 usage failed");
53 return WRITE_MSG_ERR;
54 }
55 sptr<IRemoteObject> remote = Remote();
56 CHKPR(remote, ERROR);
57 int32_t ret = remote->SendRequest(VIBRATE, data, reply, option);
58 if (ret != NO_ERROR) {
59 HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
60 HiSysEvent::EventType::FAULT, "PKG_NAME", "Vibrate", "ERROR_CODE", ret);
61 MISC_HILOGE("sendRequest ret : %{public}d", ret);
62 }
63 return ret;
64 }
65
CancelVibrator(int32_t vibratorId)66 int32_t MiscdeviceServiceProxy::CancelVibrator(int32_t vibratorId)
67 {
68 MessageParcel data;
69 MessageParcel reply;
70 MessageOption option;
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 int32_t ret = remote->SendRequest(CANCEL_VIBRATOR, data, reply, option);
82 if (ret != NO_ERROR) {
83 HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
84 HiSysEvent::EventType::FAULT, "PKG_NAME", "CancelVibrator", "ERROR_CODE", ret);
85 MISC_HILOGE("ret : %{public}d", ret);
86 }
87 return ret;
88 }
89
PlayVibratorEffect(int32_t vibratorId,const std::string & effect,int32_t loopCount,int32_t usage)90 int32_t MiscdeviceServiceProxy::PlayVibratorEffect(int32_t vibratorId, const std::string &effect,
91 int32_t loopCount, int32_t usage)
92 {
93 MessageParcel data;
94 MessageParcel reply;
95 MessageOption option;
96 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
97 MISC_HILOGE("write descriptor failed");
98 return WRITE_MSG_ERR;
99 }
100 if (!data.WriteInt32(vibratorId)) {
101 MISC_HILOGE("WriteInt32 vibratorId failed");
102 return WRITE_MSG_ERR;
103 }
104 if (!data.WriteString(effect)) {
105 MISC_HILOGE("WriteString effect failed");
106 return WRITE_MSG_ERR;
107 }
108 if (!data.WriteInt32(loopCount)) {
109 MISC_HILOGE("WriteBool effect failed");
110 return WRITE_MSG_ERR;
111 }
112 if (!data.WriteInt32(usage)) {
113 MISC_HILOGE("WriteUint32 usage failed");
114 return WRITE_MSG_ERR;
115 }
116 sptr<IRemoteObject> remote = Remote();
117 CHKPR(remote, ERROR);
118 int32_t ret = remote->SendRequest(PLAY_VIBRATOR_EFFECT, data, reply, option);
119 if (ret != NO_ERROR) {
120 HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
121 HiSysEvent::EventType::FAULT, "PKG_NAME", "PlayVibratorEffect", "ERROR_CODE", ret);
122 MISC_HILOGE("ret : %{public}d", ret);
123 }
124 return ret;
125 }
126
StopVibratorEffect(int32_t vibratorId,const std::string & effect)127 int32_t MiscdeviceServiceProxy::StopVibratorEffect(int32_t vibratorId, const std::string &effect)
128 {
129 MessageParcel data;
130 MessageParcel reply;
131 MessageOption option;
132 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
133 MISC_HILOGE("write descriptor failed");
134 return WRITE_MSG_ERR;
135 }
136 if (!data.WriteInt32(vibratorId)) {
137 MISC_HILOGE("WriteInt32 vibratorId failed");
138 return WRITE_MSG_ERR;
139 }
140 if (!data.WriteString(effect)) {
141 MISC_HILOGE("WriteString effect failed");
142 return WRITE_MSG_ERR;
143 }
144 sptr<IRemoteObject> remote = Remote();
145 CHKPR(remote, ERROR);
146 int32_t ret = remote->SendRequest(STOP_VIBRATOR_EFFECT, data, reply, option);
147 if (ret != NO_ERROR) {
148 HiSysEvent::Write(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
149 HiSysEvent::EventType::FAULT, "PKG_NAME", "StopVibratorEffect", "ERROR_CODE", ret);
150 MISC_HILOGE("ret : %{public}d", ret);
151 }
152 return ret;
153 }
GetLightList()154 std::vector<LightInfo> MiscdeviceServiceProxy::GetLightList()
155 {
156 MessageParcel data;
157 MessageParcel reply;
158 MessageOption option;
159 std::vector<LightInfo> lightInfos;
160 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
161 MISC_HILOGE("WriteInterfaceToken failed");
162 return lightInfos;
163 }
164 sptr<IRemoteObject> remote = Remote();
165 if (remote == nullptr) {
166 MISC_HILOGE("remote is null");
167 return lightInfos;
168 }
169 int32_t ret = remote->SendRequest(GET_LIGHT_LIST, data, reply, option);
170 if (ret != NO_ERROR) {
171 MISC_HILOGE("failed, ret:%{public}d", ret);
172 return lightInfos;
173 }
174 uint32_t lightCount = 0;
175 if (!reply.ReadUint32(lightCount)) {
176 MISC_HILOGE("Parcel read failed");
177 return lightInfos;
178 }
179 if (lightCount > MAX_LIGHT_COUNT) {
180 lightCount = MAX_LIGHT_COUNT;
181 }
182 for (uint32_t i = 0; i < lightCount; ++i) {
183 const uint8_t *info = reply.ReadBuffer(sizeof(LightInfo));
184 if (info == nullptr) {
185 MISC_HILOGE("ReadBuffer failed");
186 return lightInfos;
187 }
188 LightInfo lightInfo;
189 if (memcpy_s(&lightInfo, sizeof(LightInfo), info, sizeof(LightInfo)) != EOK) {
190 MISC_HILOGE("memcpy_s failed");
191 return lightInfos;
192 }
193 lightInfos.push_back(lightInfo);
194 }
195 return lightInfos;
196 }
197
TurnOn(int32_t lightId,const LightColor & color,const LightAnimation & animation)198 int32_t MiscdeviceServiceProxy::TurnOn(int32_t lightId, const LightColor &color, const LightAnimation &animation)
199 {
200 MessageParcel data;
201 MessageParcel reply;
202 MessageOption option;
203 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
204 MISC_HILOGE("write descriptor failed");
205 return WRITE_MSG_ERR;
206 }
207 if (!data.WriteInt32(lightId)) {
208 MISC_HILOGE("WriteUint32 lightId failed");
209 return WRITE_MSG_ERR;
210 }
211 if (!data.WriteBuffer(&color, sizeof(LightColor))) {
212 MISC_HILOGE("WriteBuffer color failed");
213 return WRITE_MSG_ERR;
214 }
215 if (!data.WriteBuffer(&animation, sizeof(LightAnimation))) {
216 MISC_HILOGE("WriteBuffer animation failed");
217 return WRITE_MSG_ERR;
218 }
219 sptr<IRemoteObject> remote = Remote();
220 CHKPR(remote, ERROR);
221 int32_t ret = remote->SendRequest(TURN_ON, data, reply, option);
222 if (ret != NO_ERROR) {
223 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
224 HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOn", "ERROR_CODE", ret);
225 MISC_HILOGE("sendRequest failed, ret:%{public}d", ret);
226 }
227 return ret;
228 }
229
TurnOff(int32_t lightId)230 int32_t MiscdeviceServiceProxy::TurnOff(int32_t lightId)
231 {
232 MessageParcel data;
233 MessageParcel reply;
234 MessageOption option;
235 if (!data.WriteInterfaceToken(MiscdeviceServiceProxy::GetDescriptor())) {
236 MISC_HILOGE("write descriptor failed");
237 return WRITE_MSG_ERR;
238 }
239 if (!data.WriteInt32(lightId)) {
240 MISC_HILOGE("WriteInt32 lightId failed");
241 return WRITE_MSG_ERR;
242 }
243 sptr<IRemoteObject> remote = Remote();
244 CHKPR(remote, ERROR);
245 int32_t ret = remote->SendRequest(TURN_OFF, data, reply, option);
246 if (ret != NO_ERROR) {
247 HiSysEventWrite(HiSysEvent::Domain::MISCDEVICE, "MISC_SERVICE_IPC_EXCEPTION",
248 HiSysEvent::EventType::FAULT, "PKG_NAME", "TurnOff", "ERROR_CODE", ret);
249 MISC_HILOGE("sendRequest failed, ret:%{public}d", ret);
250 }
251 return ret;
252 }
253 } // namespace Sensors
254 } // namespace OHOS
255