From 72385d8fc3cd7b484466cafb506a2d7b92f9b4e3 Mon Sep 17 00:00:00 2001 From: xdst1024 Date: Wed, 11 Jun 2025 20:18:17 +0800 Subject: [PATCH] Signed-off-by: xdst1024 Change-Id: Ie7c7f5d025177650745b25f629b8398a7a80803f Change-Id: I7c4ed92d027025debf951940758c5da34d176de5 --- frameworks/cj/src/vibrator_ffi.cpp | 119 ++++++++++++++++++++++++++++- frameworks/cj/src/vibrator_ffi.h | 6 ++ 2 files changed, 121 insertions(+), 4 deletions(-) diff --git a/frameworks/cj/src/vibrator_ffi.cpp b/frameworks/cj/src/vibrator_ffi.cpp index 0d05bba..2b4b60b 100644 --- a/frameworks/cj/src/vibrator_ffi.cpp +++ b/frameworks/cj/src/vibrator_ffi.cpp @@ -41,6 +41,22 @@ static std::map g_usageType = { {"simulateReality", USAGE_SIMULATE_REALITY}, }; +namespace { +constexpr int32_t EVENT_START_TIME_MAX = 1800000; +constexpr int32_t EVENT_NUM_MAX = 128; +constexpr int32_t INTENSITY_MIN = 0; +constexpr int32_t INTENSITY_MAX = 100; +constexpr int32_t FREQUENCY_MIN = 0; +constexpr int32_t FREQUENCY_MAX = 100; +constexpr int32_t CURVE_POINT_INTENSITY_MAX = 100; +constexpr int32_t CURVE_POINT_NUM_MIN = 4; +constexpr int32_t CURVE_POINT_NUM_MAX = 16; +constexpr int32_t CURVE_FREQUENCY_MIN = -100; +constexpr int32_t CURVE_FREQUENCY_MAX = 100; +constexpr int32_t CONTINUOUS_DURATION_MAX = 5000; +constexpr int32_t EVENT_INDEX_MAX = 2; +} + static std::set g_allowedTypes = {"time", "preset", "file", "pattern"}; struct CJVibrateInfo { std::string type; @@ -101,7 +117,7 @@ extern "C" { info.usage = strUsage; info.systemUsage = false; code = StartVibrate(info); - }; + } void FfiVibratorStartVibrationPreset(RetVibratePreset effect, RetVibrateAttribute attribute, int32_t &code) { @@ -116,7 +132,7 @@ extern "C" { info.usage = strUsage; info.systemUsage = false; code = StartVibrate(info); - }; + } void FfiVibratorStartVibrationFile(RetVibrateFromFile effect, RetVibrateAttribute attribute, int32_t &code) { @@ -130,7 +146,102 @@ extern "C" { info.usage = strUsage; info.systemUsage = false; code = StartVibrate(info); - }; + } + + static bool CheckVibratorCurvePoint(const VibratorEvent &event) + { + if ((event.pointNum < CURVE_POINT_NUM_MIN) || (event.pointNum > CURVE_POINT_NUM_MAX)) { + MISC_HILOGE("The points size is out of range, pointNum:%{public}d", event.pointNum); + return false; + } + for (int32_t j = 0; j < event.pointNum; ++j) { + if ((event.points[j].time < 0) || (event.points[j].time > event.duration)) { + MISC_HILOGE("time in points is out of range, time:%{public}d", event.points[j].time); + return false; + } + if ((event.points[j].intensity < 0) || (event.points[j].intensity > CURVE_POINT_INTENSITY_MAX)) { + MISC_HILOGE("intensity in points is out of range, intensity:%{public}d", event.points[j].intensity); + return false; + } + if ((event.points[j].frequency < CURVE_FREQUENCY_MIN) || + (event.points[j].frequency > CURVE_FREQUENCY_MAX)) { + MISC_HILOGE("frequency in points is out of range, frequency:%{public}d", event.points[j].frequency); + return false; + } + } + return true; + } + + static bool CheckVibratorEvent(const VibratorEvent &event) + { + if ((event.time < 0) || (event.time > EVENT_START_TIME_MAX)) { + MISC_HILOGE("The event time is out of range, time:%{public}d", event.time); + return false; + } + if ((event.frequency < FREQUENCY_MIN) || (event.frequency > FREQUENCY_MAX)) { + MISC_HILOGE("The event frequency is out of range, frequency:%{public}d", event.frequency); + return false; + } + if ((event.intensity < INTENSITY_MIN) || (event.intensity > INTENSITY_MAX)) { + MISC_HILOGE("The event intensity is out of range, intensity:%{public}d", event.intensity); + return false; + } + if ((event.duration <= 0) || (event.duration > CONTINUOUS_DURATION_MAX)) { + MISC_HILOGE("The event duration is out of range, duration:%{public}d", event.duration); + return false; + } + if ((event.index < 0) || (event.index > EVENT_INDEX_MAX)) { + MISC_HILOGE("The event index is out of range, index:%{public}d", event.index); + return false; + } + if ((event.type == VibratorEventType::EVENT_TYPE_CONTINUOUS) && (event.pointNum > 0)) { + if (!CheckVibratorCurvePoint(event)) { + MISC_HILOGE("CheckVibratorCurvePoint failed"); + return false; + } + } + return true; + } + + static bool CheckVibratorPatternParameter(VibratorPattern vibratorPattern) + { + CALL_LOG_ENTER; + if (vibratorPattern.events == nullptr) { + MISC_HILOGE("Events is nullptr"); + return false; + } + if ((vibratorPattern.eventNum <= 0) || (vibratorPattern.eventNum > EVENT_NUM_MAX)) { + MISC_HILOGE("The event num is out of range, eventNum:%{public}d", vibratorPattern.eventNum); + return false; + } + for (int32_t i = 0; i < vibratorPattern.eventNum; ++i) { + if (!CheckVibratorEvent(vibratorPattern.events[i])) { + MISC_HILOGE("CheckVibratorEvent failed"); + return false; + } + } + return true; + } + + void FfiVibratorStartVibrationPattern(VibrateFormPattern effect, RetVibrateAttribute attribute, int32_t &code) + { + CJVibrateInfo info; + std::string strType(effect.patternType); + info.type = strType; + info.vibratorPattern.time = effect.vibratorPattern.time; + info.vibratorPattern.eventNum = effect.vibratorPattern.eventNum; + info.vibratorPattern.events = (VibratorEvent *)effect.vibratorPattern.events; + std::string strUsage(attribute.usage); + info.usage = strUsage; + info.systemUsage = false; + + if (CheckVibratorPatternParameter(info.vibratorPattern)) { + code = PARAMETER_ERROR; + return; + } + + code = StartVibrate(info); + } void FfiVibratorStopVibration(int32_t &code) { @@ -173,4 +284,4 @@ extern "C" { } } } -} \ No newline at end of file +} diff --git a/frameworks/cj/src/vibrator_ffi.h b/frameworks/cj/src/vibrator_ffi.h index 2406c37..3f5c212 100644 --- a/frameworks/cj/src/vibrator_ffi.h +++ b/frameworks/cj/src/vibrator_ffi.h @@ -20,6 +20,7 @@ #include "cj_common_ffi.h" #include "ffi_remote_data.h" +#include "vibrator_agent_type.h" extern "C" { struct RetVibrateTime { @@ -45,6 +46,11 @@ extern "C" { RetHapticFileDescriptor hapticFd; }; + struct VibrateFormPattern { + char* patternType; + VibratorPattern vibratorPattern; + }; + struct RetVibrateAttribute { int32_t id; char* usage; -- Gitee