diff --git a/model/misc/vibrator/driver/chipset/vibrator_linear_driver.c b/model/misc/vibrator/driver/chipset/vibrator_linear_driver.c index 014eb3c77a72d540b46baaec244f7f74ca9b80f3..bb7859a6cefcad6909a5e53c3b01f4365f2d15a1 100644 --- a/model/misc/vibrator/driver/chipset/vibrator_linear_driver.c +++ b/model/misc/vibrator/driver/chipset/vibrator_linear_driver.c @@ -26,6 +26,7 @@ static struct VibratorLinearDriverData *GetLinearVibratorData(void) static int32_t StartLinearVibrator() { + int32_t ret; struct VibratorLinearDriverData *drvData = GetLinearVibratorData(); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); @@ -33,19 +34,25 @@ static int32_t StartLinearVibrator() HDF_LOGE("%s: vibrator bus type not gpio", __func__); return HDF_FAILURE; } - HDF_LOGE("%s: pull gpio high", __func__); + ret = GpioWrite(drvData->gpioNum, GPIO_VAL_LOW); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: pull gpio%d to %d level failed", __func__, drvData->gpioNum, GPIO_VAL_LOW); + return ret; + } return HDF_SUCCESS; } static int32_t StartEffectLinearVibrator(uint32_t effectType) { (void)effectType; + HDF_LOGE("%s: vibrator set build-in effect no support!", __func__); return HDF_SUCCESS; } static int32_t StopLinearVibrator() { + int32_t ret; struct VibratorLinearDriverData *drvData = GetLinearVibratorData(); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); @@ -53,8 +60,12 @@ static int32_t StopLinearVibrator() HDF_LOGE("%s: vibrator bus type not gpio", __func__); return HDF_FAILURE; } - HDF_LOGE("%s: pull gpio low", __func__); + ret = GpioWrite(drvData->gpioNum, GPIO_VAL_HIGH); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: pull gpio%d to %d level failed", __func__, drvData->gpioNum, GPIO_VAL_HIGH); + return ret; + } return HDF_SUCCESS; } diff --git a/model/misc/vibrator/driver/include/vibrator_driver.h b/model/misc/vibrator/driver/include/vibrator_driver.h index d6529669563ebfff843979ea88be6351a5d15f1d..d4114f797eaacce9489b3f1fe04d4f79752fa801 100644 --- a/model/misc/vibrator/driver/include/vibrator_driver.h +++ b/model/misc/vibrator/driver/include/vibrator_driver.h @@ -10,7 +10,6 @@ #define VIBRATOR_DRIVER_H #include "osal_mutex.h" -#include "osal_timer.h" #include "hdf_device_desc.h" #include "hdf_workqueue.h" #include "vibrator_driver_type.h" @@ -33,16 +32,15 @@ struct VibratorDriverData { struct HdfDeviceObject *device; HdfWorkQueue workQueue; HdfWork work; - OsalTimer timer; struct OsalMutex mutex; enum VibratorConfigMode mode; enum VibratorState state; struct VibratorOps ops; }; -int32_t StartTimeVibrator(uint32_t time); -int32_t StopVibrator(void); -int32_t SetEffectVibrator(uint32_t type); +void StartTimeVibrator(void); +void StopVibrator(void); +void SetEffectVibrator(uint32_t type); int32_t RegisterVibrator(struct VibratorOps *ops); #endif /* VIBRATOR_DRIVER_H */ diff --git a/model/misc/vibrator/driver/include/vibrator_driver_type.h b/model/misc/vibrator/driver/include/vibrator_driver_type.h index d4eeb96898b7ef209fa2f9f13624b14764b6b945..e6dbae303dee39b40d1df48ffb67e6d9f1cf964b 100644 --- a/model/misc/vibrator/driver/include/vibrator_driver_type.h +++ b/model/misc/vibrator/driver/include/vibrator_driver_type.h @@ -25,6 +25,13 @@ } \ } while (0) +#define CHECK_VIBRATOR_NULL_PTR_RETURN(ptr) do { \ + if ((ptr) == NULL) { \ + HDF_LOGE("%s:line %d pointer is null and return", __func__, __LINE__); \ + return; \ + } \ +} while (0) + enum VibratorBusType { VIBRATOR_BUS_I2C = 0, VIBRATOR_BUS_GPIO = 1, diff --git a/model/misc/vibrator/driver/include/vibrator_haptic.h b/model/misc/vibrator/driver/include/vibrator_haptic.h index ef03c13827f2abb25833fa602e24fa66cdc147fb..c6f4b10623a3bff6cbc6575ebc4504b2b5dcb24a 100644 --- a/model/misc/vibrator/driver/include/vibrator_haptic.h +++ b/model/misc/vibrator/driver/include/vibrator_haptic.h @@ -12,10 +12,12 @@ #include "hdf_device_desc.h" #include "hdf_dlist.h" #include "osal_mutex.h" -#include "osal_sem.h" -#include "osal_thread.h" +#include "osal_timer.h" #include "vibrator_driver_type.h" +#define VIBRATOR_MAX_HAPTIC_SEQ 1024 +#define VIBRATOR_MIN_WAIT_TIME 50 // unit:ms + enum VibratorEffectType { VIBRATOR_TYPE_EFFECT = 0, // Preset effect in device VIBRATOR_TYPE_TIME = 1, // Preset effect by time @@ -41,21 +43,20 @@ struct VibratorEffectCfg { }; struct VibratorHapticData { - bool supportPreset; + bool supportHaptic; struct DListHead effectSeqHead; struct OsalMutex mutex; - struct OsalSem hapticSem; - struct OsalThread hapticThread; - bool threadExitFlag; + OsalTimer timer; uint32_t duration[VIBRATOR_TIME_INDEX_BUTT]; int32_t effectType; int32_t seqCount; uint32_t *currentEffectSeq; + int32_t currentSeqIndex; }; -int32_t InitVibratorHaptic(struct HdfDeviceObject *device); +int32_t CreateVibratorHaptic(struct HdfDeviceObject *device); int32_t StartHaptic(struct VibratorEffectCfg *effectCfg); int32_t StopHaptic(void); -int32_t DestroyHaptic(void); +int32_t DestroyVibratorHaptic(void); #endif /* VIBRATOR_HAPTIC_H */ diff --git a/model/misc/vibrator/driver/src/vibrator_driver.c b/model/misc/vibrator/driver/src/vibrator_driver.c index 2b96f85a0425ab0ac718f725f831abd61804653c..dc78c3529f9b707f6c291026c8243959e5a9ce9b 100644 --- a/model/misc/vibrator/driver/src/vibrator_driver.c +++ b/model/misc/vibrator/driver/src/vibrator_driver.c @@ -28,6 +28,7 @@ static struct VibratorDriverData *GetVibratorDrvData(void) int32_t RegisterVibrator(struct VibratorOps *ops) { struct VibratorDriverData *drvData = GetVibratorDrvData(); + CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(ops, HDF_FAILURE); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); @@ -40,114 +41,62 @@ int32_t RegisterVibrator(struct VibratorOps *ops) return HDF_SUCCESS; } -void VibratorTimerEntry(uintptr_t para) +void StartTimeVibrator() { - struct VibratorDriverData *drvData = (struct VibratorDriverData *)para; + struct VibratorDriverData *drvData = GetVibratorDrvData(); - drvData->state = VIBRATOR_STATE_STOP; + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData); + drvData->state = VIBRATOR_STATE_START_TIMER; HdfAddWork(&drvData->workQueue, &drvData->work); } -int32_t StartTimeVibrator(uint32_t time) +void StopVibrator() { - int32_t ret; struct VibratorDriverData *drvData = GetVibratorDrvData(); - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData->ops.Start, HDF_FAILURE); - (void)OsalMutexLock(&drvData->mutex); - drvData->state = VIBRATOR_STATE_START_TIMER; - - if (drvData->timer.realTimer != NULL) { - ret = OsalTimerDelete(&drvData->timer); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: delete vibrator timer fail!", __func__); - (void)OsalMutexUnlock(&drvData->mutex); - return HDF_FAILURE; - } - } - - if (OsalTimerCreate(&drvData->timer, time, VibratorTimerEntry, (uintptr_t)drvData) != HDF_SUCCESS) { - HDF_LOGE("%s: create vibrator Timer fail!", __func__); - (void)OsalMutexUnlock(&drvData->mutex); - return HDF_FAILURE; - } - - if (OsalTimerStartOnce(&drvData->timer) != HDF_SUCCESS) { - HDF_LOGE("%s: start vibrator time fail!", __func__); - (void)OsalMutexUnlock(&drvData->mutex); - return HDF_FAILURE; - } + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData); - (void)OsalMutexUnlock(&drvData->mutex); + drvData->state = VIBRATOR_STATE_STOP; HdfAddWork(&drvData->workQueue, &drvData->work); - - return HDF_SUCCESS; } -int32_t StopVibrator() +void SetEffectVibrator(uint32_t type) { int32_t ret; struct VibratorDriverData *drvData = GetVibratorDrvData(); - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); - (void)OsalMutexLock(&drvData->mutex); - drvData->state = VIBRATOR_STATE_STOP; - ret = OsalTimerDelete(&drvData->timer); + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData); + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData->ops.StartEffect); + + ret = drvData->ops.StartEffect(type); if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: delete vibrator timer fail!", __func__); - (void)OsalMutexUnlock(&drvData->mutex); - return HDF_FAILURE; + HDF_LOGE("%s: start effect fail", __func__); + return; } - (void)OsalMutexUnlock(&drvData->mutex); - HdfAddWork(&drvData->workQueue, &drvData->work); - return HDF_SUCCESS; -} - -int32_t SetEffectVibrator(uint32_t type) -{ - int32_t ret; - struct VibratorDriverData *drvData = GetVibratorDrvData(); - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); - - (void)OsalMutexLock(&drvData->mutex); - if (drvData->ops.StartEffect != NULL) { - ret = drvData->ops.StartEffect(type); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: start effect fail", __func__); - (void)OsalMutexUnlock(&drvData->mutex); - return HDF_FAILURE; - } - } drvData->state = VIBRATOR_STATE_SET_EFFECT; - (void)OsalMutexUnlock(&drvData->mutex); - - return HDF_SUCCESS; } static void VibratorWorkEntry(void *para) { int32_t ret = HDF_FAILURE; struct VibratorDriverData *drvData = (struct VibratorDriverData *)para; - if (drvData == NULL) { - HDF_LOGE("%s: drvData is null", __func__); - return; - } + + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData); + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData->ops.Start); + CHECK_VIBRATOR_NULL_PTR_RETURN(drvData->ops.Stop); if (drvData->state == VIBRATOR_STATE_START_TIMER) { - if (drvData->ops.Start != NULL) { - ret = drvData->ops.Start(); - } - } else if (drvData->state == VIBRATOR_STATE_STOP) { - if (drvData->ops.Stop != NULL) { - ret = drvData->ops.Stop(); - } + ret = drvData->ops.Start(); + } + + if (drvData->state == VIBRATOR_STATE_STOP) { + ret = drvData->ops.Stop(); } if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: set vibrator fail state[%d]!", __func__, drvData->state); + HDF_LOGE("%s: add vibrator work fail! device state[%d{public}]!", __func__, drvData->state); } } @@ -162,18 +111,21 @@ static int32_t StartOnce(struct HdfSBuf *data, struct HdfSBuf *reply) CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(data, HDF_FAILURE); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); - if (drvData->mode == VIBRATOR_MODE_PRESET) { - if (StopHaptic() != HDF_SUCCESS) { - HDF_LOGE("%s: start haptic fail!", __func__); - return HDF_FAILURE; - } - } - if (!HdfSbufReadUint32(data, &duration)) { HDF_LOGE("%s: sbuf read duration failed", __func__); return HDF_FAILURE; } + if (duration == 0) { + HDF_LOGE("%s: vibrator duration invalid para", __func__); + return HDF_ERR_INVALID_PARAM; + } + + if (drvData->mode != VIBRATOR_MODE_BUTT) { + HDF_LOGI("%s: vibrater haptic is busy now, please stop first!", __func__); + return HDF_ERR_DEVICE_BUSY; + } + (void)OsalMutexLock(&drvData->mutex); drvData->mode = VIBRATOR_MODE_ONCE; (void)OsalMutexUnlock(&drvData->mutex); @@ -185,7 +137,7 @@ static int32_t StartOnce(struct HdfSBuf *data, struct HdfSBuf *reply) ret = StartHaptic(&config); if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: stop haptic fail!", __func__); + HDF_LOGE("%s: start haptic fail!", __func__); return ret; } @@ -203,16 +155,14 @@ static int32_t StartEffect(struct HdfSBuf *data, struct HdfSBuf *reply) CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(data, HDF_FAILURE); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(drvData, HDF_FAILURE); - if (drvData->mode == VIBRATOR_MODE_ONCE) { - if (StopHaptic() != HDF_SUCCESS) { - HDF_LOGE("%s: stop haptic fail!", __func__); - return HDF_FAILURE; - } - } - effect = HdfSbufReadString(data); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(effect, HDF_FAILURE); + if (drvData->mode != VIBRATOR_MODE_BUTT) { + HDF_LOGI("%s: vibrater haptic is busy now, please stop first!", __func__); + return HDF_ERR_DEVICE_BUSY; + } + (void)OsalMutexLock(&drvData->mutex); drvData->mode = VIBRATOR_MODE_PRESET; (void)OsalMutexUnlock(&drvData->mutex); @@ -251,6 +201,11 @@ static int32_t Stop(struct HdfSBuf *data, struct HdfSBuf *reply) return HDF_FAILURE; } + if (drvData->mode == VIBRATOR_MODE_BUTT) { + HDF_LOGD("%s: vibrater haptic had stopped", __func__); + return HDF_SUCCESS; + } + ret = StopHaptic(); if (ret != HDF_SUCCESS) { HDF_LOGE("%s: stop haptic fail!", __func__); @@ -326,7 +281,7 @@ int32_t InitVibratorDriver(struct HdfDeviceObject *device) return HDF_FAILURE; } - if (InitVibratorHaptic(device) != HDF_SUCCESS) { + if (CreateVibratorHaptic(device) != HDF_SUCCESS) { HDF_LOGE("%s: init workQueue fail!", __func__); return HDF_FAILURE; } @@ -342,9 +297,7 @@ void ReleaseVibratorDriver(struct HdfDeviceObject *device) return; } - StopHaptic(); - (void)DestroyHaptic(); - (void)OsalTimerDelete(&drvData->timer); + (void)DestroyVibratorHaptic(); (void)OsalMutexDestroy(&drvData->mutex); (void)OsalMemFree(drvData); g_vibratorDrvData = NULL; diff --git a/model/misc/vibrator/driver/src/vibrator_haptic.c b/model/misc/vibrator/driver/src/vibrator_haptic.c index 94279e9621c554e009e437a3dad9b50018677435..c86dba083e24c8474941e406f63499127df28352 100644 --- a/model/misc/vibrator/driver/src/vibrator_haptic.c +++ b/model/misc/vibrator/driver/src/vibrator_haptic.c @@ -15,7 +15,7 @@ #include "vibrator_haptic.h" #define HDF_LOG_TAG vibrator_haptic_c -#define VIBRATOR_HAPTIC_STACK_SIZE 0x2000 +#define VIBRATOR_HAPTIC_STACK_SIZE 0x4000 #define VIBRATOR_HAPTIC_SEQ_MAX 1024 #define VIBRATOR_HAPTIC_SEQ_SIZE 4 #define VIBRATOR_HAPTIC_SEQ_NAME_MAX 48 @@ -117,15 +117,16 @@ static int32_t ParserVibratorHapticConfig(const struct DeviceResourceNode *node) vibratorAttrNode = parser->GetChildNode(node, "vibratorAttr"); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(vibratorAttrNode, HDF_ERR_INVALID_PARAM); supportPresetFlag = parser->GetBool(vibratorAttrNode, "supportPreset"); + + (void)OsalMutexLock(&hapticData->mutex); + hapticData->supportHaptic = supportPresetFlag; + (void)OsalMutexUnlock(&hapticData->mutex); + if (!supportPresetFlag) { HDF_LOGD("%s: vibrator not support effect", __func__); return HDF_SUCCESS; } - (void)OsalMutexLock(&hapticData->mutex); - hapticData->supportPreset = supportPresetFlag; - (void)OsalMutexUnlock(&hapticData->mutex); - // malloc haptic resource vibratorHapticNode = parser->GetChildNode(node, "vibratorHapticConfig"); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(vibratorHapticNode, HDF_ERR_INVALID_PARAM); @@ -137,105 +138,91 @@ static int32_t ParserVibratorHapticConfig(const struct DeviceResourceNode *node) return HDF_SUCCESS; } -static int32_t DelayTimeForEffect(struct OsalSem *sem, uint32_t time) +static uint32_t ProcessHapticTime(struct VibratorHapticData *hapticData) { - int32_t ret; - if (time == 0) { - return HDF_SUCCESS; + uint32_t duration; + + CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_FAILURE); + if ((hapticData->currentSeqIndex < 0) || (hapticData->currentSeqIndex >= hapticData->seqCount)) { + return 0; } - ret = OsalSemWait(sem, time); - if (ret == 0) { - HDF_LOGD("%s: haptic need break", __func__); - return -1; + if (hapticData->currentSeqIndex % 2 == 0) { + StartTimeVibrator(); + } else { + StopVibrator(); } - return HDF_SUCCESS; + hapticData->currentSeqIndex++; + if (hapticData->currentSeqIndex >= hapticData->seqCount) { + return 0; + } + + duration = hapticData->currentEffectSeq[hapticData->currentSeqIndex] == 0 ? + VIBRATOR_MIN_WAIT_TIME : hapticData->currentEffectSeq[hapticData->currentSeqIndex]; + return duration; } -static int32_t HapticThreadEntry(void *para) +static uint32_t ProcessHapticEffect(struct VibratorHapticData *hapticData) { - int32_t count; - int32_t index = 0; - uint32_t delaytime; uint32_t effect; - struct VibratorHapticData *hapticData = NULL; + uint32_t duration; - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(para, HDF_FAILURE); - hapticData = (struct VibratorHapticData *)para; - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData->currentEffectSeq, HDF_FAILURE); + CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_FAILURE); - count = hapticData->seqCount; - if (count <= 1 || count > VIBRATOR_HAPTIC_SEQ_MAX) { - HDF_LOGE("%s: count para invalid", __func__); - return HDF_ERR_INVALID_PARAM; + if ((hapticData->currentSeqIndex < 0) || ((hapticData->currentSeqIndex + 1) >= hapticData->seqCount)) { + HDF_LOGE("%{public}s: seq index invalid para", __func__); + return 0; } - while (!hapticData->threadExitFlag) { - if (index + 1 >= count) { - break; - } + hapticData->currentSeqIndex++; + effect = hapticData->currentEffectSeq[hapticData->currentSeqIndex]; + SetEffectVibrator(effect); - delaytime = hapticData->currentEffectSeq[index]; - if (DelayTimeForEffect(&hapticData->hapticSem, delaytime) < 0) { - continue; - } - - index++; - effect = hapticData->currentEffectSeq[index++]; - if (hapticData->effectType == VIBRATOR_TYPE_EFFECT) { - SetEffectVibrator(effect); - } else if (hapticData->effectType == VIBRATOR_TYPE_TIME) { - StartTimeVibrator(effect); - } + hapticData->currentSeqIndex++; + if (hapticData->currentSeqIndex >= hapticData->seqCount) { + HDF_LOGE("%{public}s: seq index exceed max value", __func__); + return 0; } - (void)OsalMutexLock(&hapticData->mutex); - hapticData->threadExitFlag = true; - (void)OsalMutexUnlock(&hapticData->mutex); - - return HDF_SUCCESS; + duration = hapticData->currentEffectSeq[hapticData->currentSeqIndex] == 0 ? + VIBRATOR_MIN_WAIT_TIME : hapticData->currentEffectSeq[hapticData->currentSeqIndex]; + return duration; } -int32_t InitVibratorHaptic(struct HdfDeviceObject *device) +void HapticTimerEntry(uintptr_t para) { + int32_t ret; struct VibratorHapticData *hapticData = NULL; - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE); + uint32_t duration; - hapticData = (struct VibratorHapticData *)OsalMemCalloc(sizeof(*hapticData)); - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_ERR_MALLOC_FAIL); - g_vibratorHapticData = hapticData; - hapticData->threadExitFlag = true; - hapticData->supportPreset = false; + hapticData = (struct VibratorHapticData *)para; + CHECK_VIBRATOR_NULL_PTR_RETURN(hapticData); - if (OsalMutexInit(&hapticData->mutex) != HDF_SUCCESS) { - HDF_LOGE("%s: fail to create thread lock", __func__); - goto EXIT; + if (hapticData->effectType == VIBRATOR_TYPE_TIME) { + duration = ProcessHapticTime(hapticData); + HDF_LOGE("%{public}s:ProcessHapticTime duration[%{public}d]", __func__, duration); } - if (OsalSemInit(&hapticData->hapticSem, 0) != HDF_SUCCESS) { - HDF_LOGE("%s: fail to create thread lock", __func__); - goto EXIT; + if (hapticData->effectType == VIBRATOR_TYPE_EFFECT) { + duration = ProcessHapticEffect(hapticData); + HDF_LOGE("%{public}s:ProcessHapticEffect duration[%{public}d]", __func__, duration); } - // create thread - if (OsalThreadCreate(&hapticData->hapticThread, HapticThreadEntry, (void *)hapticData) != HDF_SUCCESS) { - HDF_LOGE("%s: create haptic thread fail!", __func__); - goto EXIT; + duration = ((duration > 0) && (duration < VIBRATOR_MIN_WAIT_TIME)) ? VIBRATOR_MIN_WAIT_TIME : duration; + if ((duration > 0) && (OsalTimerSetTimeout(&hapticData->timer, duration) == HDF_SUCCESS)) { + HDF_LOGD("%{public}s: modify haptic timer duration[%{public}d]", __func__, duration); + return; } - DListHeadInit(&hapticData->effectSeqHead); - // get haptic hcs - if (ParserVibratorHapticConfig(device->property) != HDF_SUCCESS) { - hapticData->threadExitFlag = true; - HDF_LOGE("%s: parser haptic config fail!", __func__); - goto EXIT; + if (hapticData->timer.realTimer != NULL) { + ret = OsalTimerDelete(&hapticData->timer); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%{public}s: delete haptic timer fail!", __func__); + } } - return HDF_SUCCESS; -EXIT: - OsalMemFree(hapticData); - return HDF_FAILURE; + return; } static int32_t GetHapticSeqByEffect(struct VibratorEffectCfg *effectCfg) @@ -247,52 +234,55 @@ static int32_t GetHapticSeqByEffect(struct VibratorEffectCfg *effectCfg) hapticData = GetHapticData(); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_FAILURE); + (void)OsalMutexLock(&hapticData->mutex); + hapticData->seqCount = 0; + hapticData->currentEffectSeq = NULL; + hapticData->currentSeqIndex = 0; + if (effectCfg->cfgMode == VIBRATOR_MODE_ONCE) { - (void)OsalMutexLock(&hapticData->mutex); - hapticData->duration[VIBRATOR_TIME_DELAY_INDEX] = 0; + hapticData->duration[VIBRATOR_TIME_DELAY_INDEX] = VIBRATOR_MIN_WAIT_TIME; hapticData->duration[VIBRATOR_TIME_DURATION_INDEX] = effectCfg->duration; hapticData->effectType = VIBRATOR_TYPE_TIME; hapticData->seqCount = VIBRATOR_TIME_INDEX_BUTT; - hapticData->currentEffectSeq = &hapticData->duration[0]; + hapticData->currentEffectSeq = &hapticData->duration[VIBRATOR_TIME_DELAY_INDEX]; (void)OsalMutexUnlock(&hapticData->mutex); - } else if (effectCfg->cfgMode == VIBRATOR_MODE_PRESET) { - if (effectCfg->effect == NULL) { - HDF_LOGE("%s: effect para invalid!", __func__); - return HDF_FAILURE; - } + return HDF_SUCCESS; + } - hapticData->seqCount = 0; - hapticData->currentEffectSeq = NULL; + if ((effectCfg->cfgMode == VIBRATOR_MODE_PRESET) && (effectCfg->effect != NULL)) { DLIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &hapticData->effectSeqHead, struct VibratorEffectNode, node) { if (strcmp(effectCfg->effect, pos->effect) == 0 && pos->seq != NULL) { - (void)OsalMutexLock(&hapticData->mutex); hapticData->effectType = pos->seq[0]; hapticData->seqCount = pos->num - 1; hapticData->currentEffectSeq = &(pos->seq[1]); - (void)OsalMutexUnlock(&hapticData->mutex); break; } } - if (hapticData->seqCount <= 0) { + if ((hapticData->seqCount <= 1) || (hapticData->seqCount > VIBRATOR_MAX_HAPTIC_SEQ)) { HDF_LOGE("%s: not find effect type!", __func__); - return HDF_FAILURE; + (void)OsalMutexUnlock(&hapticData->mutex); + return HDF_ERR_INVALID_PARAM; } + (void)OsalMutexUnlock(&hapticData->mutex); + return HDF_SUCCESS; } - return HDF_SUCCESS; + HDF_LOGE("%s: not support effect type!", __func__); + (void)OsalMutexUnlock(&hapticData->mutex); + return HDF_ERR_NOT_SUPPORT; } int32_t StartHaptic(struct VibratorEffectCfg *effectCfg) { int32_t ret; - struct OsalThreadParam threadCfg; + uint32_t duration; struct VibratorHapticData *hapticData = GetHapticData(); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(effectCfg, HDF_FAILURE); CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_FAILURE); - if (!hapticData->threadExitFlag) { - OsalSemPost(&hapticData->hapticSem); - return HDF_SUCCESS; + if ((effectCfg->cfgMode == VIBRATOR_MODE_PRESET) && (!hapticData->supportHaptic)) { + HDF_LOGE("%{public}s: vibrator no support haptic!", __func__); + return HDF_ERR_NOT_SUPPORT; } ret = GetHapticSeqByEffect(effectCfg); @@ -301,17 +291,17 @@ int32_t StartHaptic(struct VibratorEffectCfg *effectCfg) return ret; } - (void)OsalMutexLock(&hapticData->mutex); - hapticData->threadExitFlag = false; - (void)OsalMutexUnlock(&hapticData->mutex); + duration = hapticData->currentEffectSeq[0] < VIBRATOR_MIN_WAIT_TIME ? + VIBRATOR_MIN_WAIT_TIME : hapticData->currentEffectSeq[0]; - threadCfg.name = "vibrator_haptic"; - threadCfg.priority = OSAL_THREAD_PRI_DEFAULT; - threadCfg.stackSize = VIBRATOR_HAPTIC_STACK_SIZE; - ret = OsalThreadStart(&hapticData->hapticThread, &threadCfg); - if (ret != HDF_SUCCESS) { - HDF_LOGE("%s: start haptic thread fail!", __func__); - return ret; + if (OsalTimerCreate(&hapticData->timer, duration, HapticTimerEntry, (uintptr_t)hapticData) != HDF_SUCCESS) { + HDF_LOGE("%s: create vibrator timer fail!", __func__); + return HDF_FAILURE; + } + + if (OsalTimerStartLoop(&hapticData->timer) != HDF_SUCCESS) { + HDF_LOGE("%s: start vibrator timer fail!", __func__); + return HDF_FAILURE; } return HDF_SUCCESS; @@ -319,23 +309,56 @@ int32_t StartHaptic(struct VibratorEffectCfg *effectCfg) int32_t StopHaptic() { + int32_t ret; struct VibratorHapticData *hapticData = GetHapticData(); + CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_FAILURE); - if (hapticData->threadExitFlag) { - return HDF_SUCCESS; + if (hapticData->timer.realTimer != NULL) { + HDF_LOGE("%s: delete vibrator Timer!", __func__); + (void)OsalMutexLock(&hapticData->mutex); + ret = OsalTimerDelete(&hapticData->timer); + (void)OsalMutexUnlock(&hapticData->mutex); + if (ret != HDF_SUCCESS) { + HDF_LOGE("%s: delete vibrator timer fail!", __func__); + return ret; + } } - (void)OsalMutexLock(&hapticData->mutex); - hapticData->threadExitFlag = true; - (void)OsalMutexUnlock(&hapticData->mutex); - OsalSemPost(&hapticData->hapticSem); StopVibrator(); + return HDF_SUCCESS; +} + +int32_t CreateVibratorHaptic(struct HdfDeviceObject *device) +{ + struct VibratorHapticData *hapticData = NULL; + CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(device, HDF_FAILURE); + + hapticData = (struct VibratorHapticData *)OsalMemCalloc(sizeof(*hapticData)); + CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_ERR_MALLOC_FAIL); + g_vibratorHapticData = hapticData; + hapticData->supportHaptic = false; + + if (OsalMutexInit(&hapticData->mutex) != HDF_SUCCESS) { + HDF_LOGE("%s: fail to init mutex", __func__); + goto EXIT; + } + + DListHeadInit(&hapticData->effectSeqHead); + + // get haptic hcs + if (ParserVibratorHapticConfig(device->property) != HDF_SUCCESS) { + HDF_LOGE("%s: parser haptic config fail!", __func__); + goto EXIT; + } return HDF_SUCCESS; +EXIT: + OsalMemFree(hapticData); + return HDF_FAILURE; } -static void ReleaseHapticConfig() +static void FreeHapticConfig() { struct VibratorHapticData *hapticData = GetHapticData(); struct VibratorEffectNode *pos = NULL; @@ -358,18 +381,16 @@ static void ReleaseHapticConfig() (void)OsalMutexUnlock(&hapticData->mutex); } -int32_t DestroyHaptic() +int32_t DestroyVibratorHaptic() { struct VibratorHapticData *hapticData = GetHapticData(); - CHECK_VIBRATOR_NULL_PTR_RETURN_VALUE(hapticData, HDF_FAILURE); - if (hapticData->supportPreset) { - ReleaseHapticConfig(); + + if (hapticData->supportHaptic) { + FreeHapticConfig(); } (void)OsalMutexDestroy(&hapticData->mutex); - (void)OsalSemDestroy(&hapticData->hapticSem); - (void)OsalThreadDestroy(&hapticData->hapticThread); return HDF_SUCCESS; } diff --git a/model/sensor/driver/include/sensor_platform_if.h b/model/sensor/driver/include/sensor_platform_if.h index 349d3872ef2c9b423922cacc7cbb1963f5841023..cc49aca4558bd1d74c9b5dfeede2ef427c434039 100644 --- a/model/sensor/driver/include/sensor_platform_if.h +++ b/model/sensor/driver/include/sensor_platform_if.h @@ -40,7 +40,7 @@ #define SENSOR_ADDR_WIDTH_1_BYTE 1 // 8 bit #define SENSOR_ADDR_WIDTH_2_BYTE 2 // 16 bit -#define SENSOR_ADDR_WIDTH_4_BYTE 4 // 16 bit +#define SENSOR_ADDR_WIDTH_4_BYTE 4 // 32 bit #define SENSOR_DATA_WIDTH_8_BIT 8 // 8 bit #define SENSOR_CONVERT_UNIT 1000 #define SENSOR_1K_UNIT 1024