From 15d5202fa7705146eaa373390d247d707e697fce Mon Sep 17 00:00:00 2001 From: "Harpreet \"Eli\" Sangha" Date: Wed, 10 Apr 2019 16:38:12 +0900 Subject: [PATCH] vibrator: example: Enforce API-Specific Effect Range Bug: 130265348 Test: vts-tradefed run singleCommand vts-hal -m VtsHalVibratorV1_2Target Change-Id: I45cc2ad64295d6a63ef3280d4d5b869e19786ce3 Signed-off-by: Harpreet "Eli" Sangha --- vibrator/1.3/example/Vibrator.cpp | 25 +++++++++++++++++++++---- vibrator/1.3/example/Vibrator.h | 3 +++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/vibrator/1.3/example/Vibrator.cpp b/vibrator/1.3/example/Vibrator.cpp index 0cb37e6a36..b529437108 100644 --- a/vibrator/1.3/example/Vibrator.cpp +++ b/vibrator/1.3/example/Vibrator.cpp @@ -56,27 +56,30 @@ Return Vibrator::supportsAmplitudeControl() { } Return Vibrator::setAmplitude(uint8_t amplitude) { + if (!amplitude) { + return Status::BAD_VALUE; + } ALOGI("Amplitude: %u -> %u\n", mAmplitude, amplitude); mAmplitude = amplitude; return Status::OK; } Return Vibrator::perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb) { - return perform_1_1(static_cast(effect), strength, _hidl_cb); + return perform(effect, strength, _hidl_cb); } // Methods from ::android::hardware::vibrator::V1_1::IVibrator follow. Return Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength, perform_cb _hidl_cb) { - return perform_1_2(static_cast(effect), strength, _hidl_cb); + return perform(effect, strength, _hidl_cb); } // Methods from ::android::hardware::vibrator::V1_2::IVibrator follow. Return Vibrator::perform_1_2(V1_2::Effect effect, EffectStrength strength, perform_cb _hidl_cb) { - return perform_1_3(static_cast(effect), strength, _hidl_cb); + return perform(effect, strength, _hidl_cb); } // Methods from ::android::hardware::vibrator::V1_3::IVibrator follow. @@ -98,6 +101,12 @@ Return Vibrator::setExternalControl(bool enabled) { } Return Vibrator::perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) { + return perform(effect, strength, _hidl_cb); +} + +// Private methods follow. + +Return Vibrator::perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb) { uint8_t amplitude; uint32_t ms; Status status = Status::OK; @@ -123,7 +132,15 @@ Return Vibrator::perform_1_3(Effect effect, EffectStrength strength, perfo return Void(); } -// Private methods follow. +template +Return Vibrator::perform(T effect, EffectStrength strength, perform_cb _hidl_cb) { + auto validRange = hidl_enum_range(); + if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) { + _hidl_cb(Status::UNSUPPORTED_OPERATION, 0); + return Void(); + } + return perform(static_cast(effect), strength, _hidl_cb); +} Status Vibrator::enable(bool enabled) { if (mExternalControl) { diff --git a/vibrator/1.3/example/Vibrator.h b/vibrator/1.3/example/Vibrator.h index 64e8e1b76d..5180774552 100644 --- a/vibrator/1.3/example/Vibrator.h +++ b/vibrator/1.3/example/Vibrator.h @@ -54,6 +54,9 @@ class Vibrator : public IVibrator { Return perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) override; private: + Return perform(Effect effect, EffectStrength strength, perform_cb _hidl_cb); + template + Return perform(T effect, EffectStrength strength, perform_cb _hidl_cb); Status enable(bool enabled); Status activate(uint32_t ms); void timeout();