diff --git a/frameworks/cj/src/vibrator_ffi.cpp b/frameworks/cj/src/vibrator_ffi.cpp index 0d05bba0ceb13492de1442915696f8b33b541eb1..2b4b60b8ee819284f6e15828b07e2152a46d2fc4 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 2406c37af14ff73f0339f647fea3da5725b35c71..3f5c2126262a517b87b093f74be80d36ff388f84 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;