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 <eliptus@google.com>
diff --git a/vibrator/1.3/example/Vibrator.cpp b/vibrator/1.3/example/Vibrator.cpp
index 0cb37e6..b529437 100644
--- a/vibrator/1.3/example/Vibrator.cpp
+++ b/vibrator/1.3/example/Vibrator.cpp
@@ -56,27 +56,30 @@
 }
 
 Return<Status> Vibrator::setAmplitude(uint8_t amplitude) {
+    if (!amplitude) {
+        return Status::BAD_VALUE;
+    }
     ALOGI("Amplitude: %u -> %u\n", mAmplitude, amplitude);
     mAmplitude = amplitude;
     return Status::OK;
 }
 
 Return<void> Vibrator::perform(V1_0::Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
-    return perform_1_1(static_cast<V1_1::Effect_1_1>(effect), strength, _hidl_cb);
+    return perform<decltype(effect)>(effect, strength, _hidl_cb);
 }
 
 // Methods from ::android::hardware::vibrator::V1_1::IVibrator follow.
 
 Return<void> Vibrator::perform_1_1(V1_1::Effect_1_1 effect, EffectStrength strength,
                                    perform_cb _hidl_cb) {
-    return perform_1_2(static_cast<V1_2::Effect>(effect), strength, _hidl_cb);
+    return perform<decltype(effect)>(effect, strength, _hidl_cb);
 }
 
 // Methods from ::android::hardware::vibrator::V1_2::IVibrator follow.
 
 Return<void> Vibrator::perform_1_2(V1_2::Effect effect, EffectStrength strength,
                                    perform_cb _hidl_cb) {
-    return perform_1_3(static_cast<V1_3::Effect>(effect), strength, _hidl_cb);
+    return perform<decltype(effect)>(effect, strength, _hidl_cb);
 }
 
 // Methods from ::android::hardware::vibrator::V1_3::IVibrator follow.
@@ -98,6 +101,12 @@
 }
 
 Return<void> Vibrator::perform_1_3(Effect effect, EffectStrength strength, perform_cb _hidl_cb) {
+    return perform<decltype(effect)>(effect, strength, _hidl_cb);
+}
+
+// Private methods follow.
+
+Return<void> 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 Void();
 }
 
-// Private methods follow.
+template <typename T>
+Return<void> Vibrator::perform(T effect, EffectStrength strength, perform_cb _hidl_cb) {
+    auto validRange = hidl_enum_range<T>();
+    if (effect < *validRange.begin() || effect > *std::prev(validRange.end())) {
+        _hidl_cb(Status::UNSUPPORTED_OPERATION, 0);
+        return Void();
+    }
+    return perform(static_cast<Effect>(effect), strength, _hidl_cb);
+}
 
 Status Vibrator::enable(bool enabled) {
     if (mExternalControl) {