Effect AIDL: Refactor effect capability with Range implementation

Bug: 258124419
Test: atest --test-mapping hardware/interfaces/audio/aidl/vts:presubmit
Change-Id: Ie76270e91471a30f7dda1883c510cd318745855f
diff --git a/audio/aidl/default/envReverb/EnvReverbSw.cpp b/audio/aidl/default/envReverb/EnvReverbSw.cpp
index 905dba4..46d9016 100644
--- a/audio/aidl/default/envReverb/EnvReverbSw.cpp
+++ b/audio/aidl/default/envReverb/EnvReverbSw.cpp
@@ -60,18 +60,20 @@
 namespace aidl::android::hardware::audio::effect {
 
 const std::string EnvReverbSw::kEffectName = "EnvReverbSw";
-const EnvironmentalReverb::Capability EnvReverbSw::kCapability = {.minRoomLevelMb = -6000,
-                                                                  .maxRoomLevelMb = 0,
-                                                                  .minRoomHfLevelMb = -4000,
-                                                                  .maxRoomHfLevelMb = 0,
-                                                                  .maxDecayTimeMs = 7000,
-                                                                  .minDecayHfRatioPm = 100,
-                                                                  .maxDecayHfRatioPm = 2000,
-                                                                  .minLevelMb = -6000,
-                                                                  .maxLevelMb = 0,
-                                                                  .maxDelayMs = 65,
-                                                                  .maxDiffusionPm = 1000,
-                                                                  .maxDensityPm = 1000};
+
+const std::vector<Range::EnvironmentalReverbRange> EnvReverbSw::kRanges = {
+        MAKE_RANGE(EnvironmentalReverb, roomLevelMb, -6000, 0),
+        MAKE_RANGE(EnvironmentalReverb, roomHfLevelMb, -4000, 0),
+        MAKE_RANGE(EnvironmentalReverb, decayTimeMs, 0, 7000),
+        MAKE_RANGE(EnvironmentalReverb, decayHfRatioPm, 100, 2000),
+        MAKE_RANGE(EnvironmentalReverb, levelMb, -6000, 0),
+        MAKE_RANGE(EnvironmentalReverb, delayMs, 0, 65),
+        MAKE_RANGE(EnvironmentalReverb, diffusionPm, 0, 1000),
+        MAKE_RANGE(EnvironmentalReverb, densityPm, 0, 1000)};
+
+const Capability EnvReverbSw::kCapability = {
+        .range = Range::make<Range::environmentalReverb>(EnvReverbSw::kRanges)};
+
 const Descriptor EnvReverbSw::kDescriptor = {
         .common = {.id = {.type = kEnvReverbTypeUUID,
                           .uuid = kEnvReverbSwImplUUID,
@@ -81,7 +83,7 @@
                              .volume = Flags::Volume::CTRL},
                    .name = EnvReverbSw::kEffectName,
                    .implementor = "The Android Open Source Project"},
-        .capability = Capability::make<Capability::environmentalReverb>(EnvReverbSw::kCapability)};
+        .capability = EnvReverbSw::kCapability};
 
 ndk::ScopedAStatus EnvReverbSw::getDescriptor(Descriptor* _aidl_return) {
     LOG(DEBUG) << __func__ << kDescriptor.toString();
@@ -94,8 +96,8 @@
               "EffectNotSupported");
 
     auto& erParam = specific.get<Parameter::Specific::environmentalReverb>();
+    RETURN_IF(!inRange(erParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
     auto tag = erParam.getTag();
-
     switch (tag) {
         case EnvironmentalReverb::roomLevelMb: {
             RETURN_IF(mContext->setErRoomLevel(erParam.get<EnvironmentalReverb::roomLevelMb>()) !=
@@ -262,85 +264,41 @@
 }
 
 RetCode EnvReverbSwContext::setErRoomLevel(int roomLevel) {
-    if (roomLevel < EnvReverbSw::kCapability.minRoomLevelMb ||
-        roomLevel > EnvReverbSw::kCapability.maxRoomLevelMb) {
-        LOG(ERROR) << __func__ << " invalid roomLevel: " << roomLevel;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new room level
     mRoomLevel = roomLevel;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErRoomHfLevel(int roomHfLevel) {
-    if (roomHfLevel < EnvReverbSw::kCapability.minRoomHfLevelMb ||
-        roomHfLevel > EnvReverbSw::kCapability.maxRoomHfLevelMb) {
-        LOG(ERROR) << __func__ << " invalid roomHfLevel: " << roomHfLevel;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new room HF level
     mRoomHfLevel = roomHfLevel;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErDecayTime(int decayTime) {
-    if (decayTime < 0 || decayTime > EnvReverbSw::kCapability.maxDecayTimeMs) {
-        LOG(ERROR) << __func__ << " invalid decayTime: " << decayTime;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new decay time
     mDecayTime = decayTime;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErDecayHfRatio(int decayHfRatio) {
-    if (decayHfRatio < EnvReverbSw::kCapability.minDecayHfRatioPm ||
-        decayHfRatio > EnvReverbSw::kCapability.maxDecayHfRatioPm) {
-        LOG(ERROR) << __func__ << " invalid decayHfRatio: " << decayHfRatio;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new decay HF ratio
     mDecayHfRatio = decayHfRatio;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErLevel(int level) {
-    if (level < EnvReverbSw::kCapability.minLevelMb ||
-        level > EnvReverbSw::kCapability.maxLevelMb) {
-        LOG(ERROR) << __func__ << " invalid level: " << level;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new level
     mLevel = level;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErDelay(int delay) {
-    if (delay < 0 || delay > EnvReverbSw::kCapability.maxDelayMs) {
-        LOG(ERROR) << __func__ << " invalid delay: " << delay;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new delay
     mDelay = delay;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErDiffusion(int diffusion) {
-    if (diffusion < 0 || diffusion > EnvReverbSw::kCapability.maxDiffusionPm) {
-        LOG(ERROR) << __func__ << " invalid diffusion: " << diffusion;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new diffusion
     mDiffusion = diffusion;
     return RetCode::SUCCESS;
 }
 
 RetCode EnvReverbSwContext::setErDensity(int density) {
-    if (density < 0 || density > EnvReverbSw::kCapability.maxDensityPm) {
-        LOG(ERROR) << __func__ << " invalid density: " << density;
-        return RetCode::ERROR_ILLEGAL_PARAMETER;
-    }
-    // TODO : Add implementation to apply new density
     mDensity = density;
     return RetCode::SUCCESS;
 }