• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "vibrator_controller.h"
17 #include <securec.h>
18 #include "hdf_base.h"
19 #include "vibrator_uhdf_log.h"
20 #include "osal_mem.h"
21 
22 #define HDF_LOG_TAG uhdf_vibrator_service
23 #define EFFECT_SUN 64
24 #define EFFECT_DURATION 2000
25 #define VIBRATOR_SERVICE_NAME "hdf_misc_vibrator"
26 #define DEFAULT_START_UP_TIME 20
27 
GetVibratorDevicePriv(void)28 static struct VibratorDevice *GetVibratorDevicePriv(void)
29 {
30     static struct VibratorDevice vibratorDeviceData = {
31         .initState = false,
32         .ioService = NULL,
33     };
34 
35     return &vibratorDeviceData;
36 }
37 
SendVibratorMsg(uint32_t cmd,struct HdfSBuf * msg,struct HdfSBuf * reply)38 static int32_t SendVibratorMsg(uint32_t cmd, struct HdfSBuf *msg, struct HdfSBuf *reply)
39 {
40     struct VibratorDevice *priv = GetVibratorDevicePriv();
41 
42     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
43     CHECK_NULL_PTR_RETURN_VALUE(priv->ioService, HDF_FAILURE);
44     CHECK_NULL_PTR_RETURN_VALUE(priv->ioService->dispatcher, HDF_FAILURE);
45     CHECK_NULL_PTR_RETURN_VALUE(priv->ioService->dispatcher->Dispatch, HDF_FAILURE);
46 
47     int32_t ret = priv->ioService->dispatcher->Dispatch(&priv->ioService->object, cmd, msg, reply);
48     if (ret != HDF_SUCCESS) {
49         HDF_LOGE("%{public}s: Vibrator dispatch failed", __func__);
50         return ret;
51     }
52 
53     return HDF_SUCCESS;
54 }
55 
ReadVibratorInfo(struct HdfSBuf * reply,struct VibratorDevice * priv)56 static int32_t ReadVibratorInfo(struct HdfSBuf *reply, struct VibratorDevice *priv)
57 {
58     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
59     uint32_t len;
60     struct VibratorInfo *buf = NULL;
61 
62     if (!HdfSbufReadBuffer(reply, (const void **)&buf, &len)) {
63         return HDF_FAILURE;
64     }
65 
66     if (buf == NULL || len != sizeof(struct VibratorInfo)) {
67         HDF_LOGE("%{public}s: read size is error, len = %{public}d, size = %{public}zu\n",\
68             __func__, len, sizeof(struct VibratorInfo));
69         HdfSbufRecycle(reply);
70         return HDF_FAILURE;
71     }
72 
73     if (memcpy_s(&priv->vibratorInfoEntry, sizeof(priv->vibratorInfoEntry), buf, sizeof(*buf)) != EOK) {
74         HDF_LOGE("%s: Memcpy buf failed", __func__);
75         HdfSbufRecycle(reply);
76         return HDF_FAILURE;
77     }
78 
79     return HDF_SUCCESS;
80 }
81 
GetVibratorInfo(struct VibratorInfo ** vibratorInfo)82 static int32_t GetVibratorInfo(struct VibratorInfo **vibratorInfo)
83 {
84     int32_t ret;
85     if (vibratorInfo == NULL) {
86         HDF_LOGE("%s:line:%{public}d pointer is null and return ret", __func__, __LINE__);
87         return HDF_FAILURE;
88     }
89     struct VibratorDevice *priv = GetVibratorDevicePriv();
90 
91     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
92 
93     (void)OsalMutexLock(&priv->mutex);
94     struct HdfSBuf *reply = HdfSbufObtainDefaultSize();
95     if (reply == NULL) {
96         HDF_LOGE("%s: get sbuf failed", __func__);
97         (void)OsalMutexUnlock(&priv->mutex);
98         return HDF_FAILURE;
99     }
100 
101     ret = SendVibratorMsg(VIBRATOR_IO_GET_INFO, NULL, reply);
102     if (ret != HDF_SUCCESS) {
103         HDF_LOGE("%{public}s: Vibrator send cmd failed, ret[%{public}d]", __func__, ret);
104         HdfSbufRecycle(reply);
105         (void)OsalMutexUnlock(&priv->mutex);
106         return ret;
107     }
108 
109     if (ReadVibratorInfo(reply, priv) != HDF_SUCCESS) {
110         HdfSbufRecycle(reply);
111         (void)OsalMutexUnlock(&priv->mutex);
112         return HDF_FAILURE;
113     }
114 
115     HdfSbufRecycle(reply);
116     (void)OsalMutexUnlock(&priv->mutex);
117 
118     *vibratorInfo = &priv->vibratorInfoEntry;
119 
120     return HDF_SUCCESS;
121 }
122 
ValidityJudgment(uint32_t duration,uint16_t intensity,int16_t frequency)123 static int32_t ValidityJudgment(uint32_t duration, uint16_t intensity, int16_t frequency)
124 {
125     struct VibratorDevice *priv = GetVibratorDevicePriv();
126     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
127     if (duration == 0) {
128         HDF_LOGE("%s:invalid vibration period", __func__);
129         return VIBRATOR_NOT_PERIOD;
130     }
131 
132     if ((priv->vibratorInfoEntry.isSupportIntensity == 0) || (intensity < priv->vibratorInfoEntry.intensityMinValue) ||
133         (intensity > priv->vibratorInfoEntry.intensityMaxValue)) {
134         HDF_LOGE("%s:intensity not supported", __func__);
135         return VIBRATOR_NOT_INTENSITY;
136     }
137 
138     if ((priv->vibratorInfoEntry.isSupportFrequency == 0) || (frequency < priv->vibratorInfoEntry.frequencyMinValue) ||
139         (frequency > priv->vibratorInfoEntry.frequencyMaxValue)) {
140         HDF_LOGE("%s:frequency not supported", __func__);
141         return VIBRATOR_NOT_FREQUENCY;
142     }
143 
144     return VIBRATOR_SUCCESS;
145 }
146 
EnableVibratorModulation(uint32_t duration,uint16_t intensity,int16_t frequency)147 static int32_t EnableVibratorModulation(uint32_t duration, uint16_t intensity, int16_t frequency)
148 {
149     int32_t ret;
150     struct VibratorDevice *priv = GetVibratorDevicePriv();
151 
152     ret = ValidityJudgment(duration, intensity, frequency);
153     if (ret != HDF_SUCCESS) {
154         HDF_LOGE("%{public}s: effect is false", __func__);
155         return ret;
156     }
157 
158     (void)OsalMutexLock(&priv->mutex);
159     struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
160     if (msg == NULL) {
161         HDF_LOGE("%{public}s: get sbuf failed", __func__);
162         (void)OsalMutexUnlock(&priv->mutex);
163         return HDF_FAILURE;
164     }
165 
166     if (!HdfSbufWriteUint32(msg, duration)) {
167         HDF_LOGE("%{public}s: write duration failed.", __func__);
168         HdfSbufRecycle(msg);
169         (void)OsalMutexUnlock(&priv->mutex);
170         return HDF_FAILURE;
171     }
172 
173     if (!HdfSbufWriteUint16(msg, intensity)) {
174         HDF_LOGE("%{public}s: write intensity failed.", __func__);
175         HdfSbufRecycle(msg);
176         (void)OsalMutexUnlock(&priv->mutex);
177         return HDF_FAILURE;
178     }
179 
180     if (!HdfSbufWriteInt16(msg, frequency)) {
181         HDF_LOGE("%{public}s: write frequency failed.", __func__);
182         HdfSbufRecycle(msg);
183         (void)OsalMutexUnlock(&priv->mutex);
184         return HDF_FAILURE;
185     }
186     ret = SendVibratorMsg(VIBRATOR_IO_ENABLE_MODULATION_PARAMETER, msg, NULL);
187     if (ret != HDF_SUCCESS) {
188         HDF_LOGE("%{public}s: Vibrator send cmd failed, ret[%{public}d]", __func__, ret);
189     }
190     HdfSbufRecycle(msg);
191     (void)OsalMutexUnlock(&priv->mutex);
192 
193     return ret;
194 }
195 
StartOnce(uint32_t duration)196 static int32_t StartOnce(uint32_t duration)
197 {
198     int32_t ret;
199     struct VibratorDevice *priv = GetVibratorDevicePriv();
200     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
201 
202     (void)OsalMutexLock(&priv->mutex);
203     struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
204     if (msg == NULL) {
205         HDF_LOGE("%s: get sbuf failed", __func__);
206         (void)OsalMutexUnlock(&priv->mutex);
207         return HDF_FAILURE;
208     }
209 
210     if (!HdfSbufWriteUint32(msg, duration)) {
211         HDF_LOGE("%s: write duration failed", __func__);
212         HdfSbufRecycle(msg);
213         (void)OsalMutexUnlock(&priv->mutex);
214         return HDF_FAILURE;
215     }
216 
217     ret = SendVibratorMsg(VIBRATOR_IO_START_ONCE, msg, NULL);
218     if (ret != HDF_SUCCESS) {
219         HDF_LOGE("%{public}s: Vibrator send cmd failed, ret[%{public}d]", __func__, ret);
220     }
221     HdfSbufRecycle(msg);
222     (void)OsalMutexUnlock(&priv->mutex);
223 
224     return ret;
225 }
226 
Start(const char * effect)227 static int32_t Start(const char *effect)
228 {
229     int32_t ret;
230     struct VibratorDevice *priv = GetVibratorDevicePriv();
231     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
232 
233     if (effect == NULL) {
234         HDF_LOGE("%s: start vibrator effect type invalid", __func__);
235         return HDF_ERR_INVALID_PARAM;
236     }
237 
238     (void)OsalMutexLock(&priv->mutex);
239     struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
240     if (msg == NULL) {
241         HDF_LOGE("%s: get sbuf failed", __func__);
242         HdfSbufRecycle(msg);
243         (void)OsalMutexUnlock(&priv->mutex);
244         return HDF_FAILURE;
245     }
246 
247     if (!HdfSbufWriteString(msg, effect)) {
248         HDF_LOGE("%s: write effectName failed", __func__);
249         HdfSbufRecycle(msg);
250         (void)OsalMutexUnlock(&priv->mutex);
251         return HDF_FAILURE;
252     }
253 
254     ret = SendVibratorMsg(VIBRATOR_IO_START_EFFECT, msg, NULL);
255     if (ret != HDF_SUCCESS) {
256         HDF_LOGE("%{public}s: Vibrator send cmd failed, ret[%{public}d]", __func__, ret);
257     }
258     HdfSbufRecycle(msg);
259     (void)OsalMutexUnlock(&priv->mutex);
260 
261     return ret;
262 }
263 
GetEffectInfo(const char * effect,struct EffectInfo * effectInfo)264 static int32_t GetEffectInfo(const char *effect, struct EffectInfo *effectInfo)
265 {
266     CHECK_NULL_PTR_RETURN_VALUE(effectInfo, HDF_FAILURE);
267     for (int i = 0; i < EFFECT_TYPE_MAX; i++) {
268         if (!strcmp(effect, g_effectmap[i].effectName)) {
269             effectInfo->isSupportEffect = g_effectmap[i].issupport;
270             effectInfo->duration = g_effectmap[i].duration;
271         }
272     }
273     return HDF_SUCCESS;
274 }
275 
Stop(enum VibratorMode mode)276 static int32_t Stop(enum VibratorMode mode)
277 {
278     if (mode < VIBRATOR_MODE_ONCE || mode >= VIBRATOR_MODE_BUTT) {
279         return HDF_ERR_INVALID_PARAM;
280     }
281     if (mode == VIBRATOR_MODE_HDHAPTIC) {
282         return HDF_ERR_NOT_SUPPORT;
283     }
284     int32_t ret;
285     struct VibratorDevice *priv = GetVibratorDevicePriv();
286     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
287 
288     (void)OsalMutexLock(&priv->mutex);
289     struct HdfSBuf *msg = HdfSbufObtainDefaultSize();
290     if (msg == NULL) {
291         HDF_LOGE("%s: get sbuf failed", __func__);
292         (void)OsalMutexUnlock(&priv->mutex);
293         return HDF_FAILURE;
294     }
295 
296     if (!HdfSbufWriteInt32(msg, mode)) {
297         HDF_LOGE("%s: write mode failed", __func__);
298         HdfSbufRecycle(msg);
299         (void)OsalMutexUnlock(&priv->mutex);
300         return HDF_FAILURE;
301     }
302 
303     ret = SendVibratorMsg(VIBRATOR_IO_STOP, msg, NULL);
304     if (ret != HDF_SUCCESS) {
305         HDF_LOGE("%{public}s: Vibrator send cmd failed, ret[%{public}d]", __func__, ret);
306     }
307     HdfSbufRecycle(msg);
308     (void)OsalMutexUnlock(&priv->mutex);
309 
310     return ret;
311 }
312 
PlayHapticPattern(struct HapticPaket * pkg)313 static int32_t PlayHapticPattern(struct HapticPaket *pkg)
314 {
315     (void)pkg;
316     return HDF_SUCCESS;
317 }
318 
GetHapticCapacity(struct HapticCapacity * hapticCapacity)319 static int32_t GetHapticCapacity(struct HapticCapacity *hapticCapacity)
320 {
321     (void)hapticCapacity;
322     return HDF_SUCCESS;
323 }
324 
GetHapticStartUpTime(int32_t mode,int32_t * startUpTime)325 static int32_t GetHapticStartUpTime(int32_t mode, int32_t *startUpTime)
326 {
327     *startUpTime = DEFAULT_START_UP_TIME;
328     HDF_LOGE("%{public}s: mode = %{public}d", __func__, mode);
329     HDF_LOGE("%{public}s: startUpTime = %{public}d", __func__, *startUpTime);
330     return HDF_SUCCESS;
331 }
332 
NewVibratorInterfaceInstance(void)333 const struct VibratorInterface *NewVibratorInterfaceInstance(void)
334 {
335     static struct VibratorInterface vibratorDevInstance;
336     struct VibratorDevice *priv = GetVibratorDevicePriv();
337 
338     if (priv == NULL) {
339         return &vibratorDevInstance;
340     }
341     if (priv->initState) {
342         return &vibratorDevInstance;
343     }
344 
345     OsalMutexInit(&priv->mutex);
346     vibratorDevInstance.Start = Start;
347     vibratorDevInstance.StartOnce = StartOnce;
348     vibratorDevInstance.Stop = Stop;
349     vibratorDevInstance.GetVibratorInfo = GetVibratorInfo;
350     vibratorDevInstance.GetEffectInfo = GetEffectInfo;
351     vibratorDevInstance.EnableVibratorModulation = EnableVibratorModulation;
352     vibratorDevInstance.PlayHapticPattern = PlayHapticPattern;
353     vibratorDevInstance.GetHapticCapacity = GetHapticCapacity;
354     vibratorDevInstance.GetHapticStartUpTime = GetHapticStartUpTime;
355 
356     priv->ioService = HdfIoServiceBind(VIBRATOR_SERVICE_NAME);
357     if (priv->ioService == NULL) {
358         HDF_LOGE("%s: get vibrator ioService failed", __func__);
359         OsalMutexDestroy(&priv->mutex);
360         return NULL;
361     }
362 
363     priv->initState = true;
364     HDF_LOGD("get vibrator devInstance success");
365     return &vibratorDevInstance;
366 }
367 
FreeVibratorInterfaceInstance(void)368 int32_t FreeVibratorInterfaceInstance(void)
369 {
370     struct VibratorDevice *priv = GetVibratorDevicePriv();
371 
372     CHECK_NULL_PTR_RETURN_VALUE(priv, HDF_FAILURE);
373 
374     if (!priv->initState) {
375         HDF_LOGD("%s: vibrator instance had released", __func__);
376         return HDF_SUCCESS;
377     }
378 
379     if (priv->ioService != NULL) {
380         HdfIoServiceRecycle(priv->ioService);
381     }
382 
383     OsalMutexDestroy(&priv->mutex);
384 
385     return HDF_SUCCESS;
386 }