Effect AIDL: Refactor effect capability with Range vector

Bug: 258124419
Test: m libdynamicsprocessingsw; m libaecsw
Change-Id: I7e9ff4f52cf9ba8b4b89fc9a71094597f51124aa
diff --git a/audio/include/system/audio_effects/aidl_effects_utils.h b/audio/include/system/audio_effects/aidl_effects_utils.h
new file mode 100644
index 0000000..80b3737
--- /dev/null
+++ b/audio/include/system/audio_effects/aidl_effects_utils.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <optional>
+
+#include <aidl/android/hardware/audio/effect/AcousticEchoCanceler.h>
+#include <aidl/android/hardware/audio/effect/DynamicsProcessing.h>
+#include <aidl/android/hardware/audio/effect/Parameter.h>
+#include <aidl/android/hardware/audio/effect/Range.h>
+
+namespace aidl::android::hardware::audio::effect {
+
+/**
+ * Check the target Parameter with $Parameter$Range definition in Capability.
+ * This method go through the elements in the ranges to find a matching tag for the target
+ * parameter, and check if the target parameter is inside range use the default AIDL union
+ * comparator.
+ *
+ * Absence of a corresponding range is an indication that there are no limits set on the parameter
+ * so this method return true.
+ */
+template <typename T, typename R>
+bool inRange(const T& target, const R& ranges) {
+  for (const auto& r : ranges) {
+    if (target.getTag() == r.min.getTag() &&
+        target.getTag() == r.max.getTag() &&
+        (target < r.min || target > r.max)) {
+      return false;
+    }
+  }
+  return true;
+}
+
+template <typename Range::Tag rangeTag, typename T>
+bool inRange(const T& target, const Capability& cap) {
+  if (cap.range.getTag() == rangeTag) {
+      const auto& ranges = cap.range.template get<rangeTag>();
+      return inRange(target, ranges);
+  }
+  return true;
+}
+
+template <typename T, typename R>
+bool isRangeValid(const T& tag, const R& ranges) {
+  for (const auto& r : ranges) {
+    if (tag == r.min.getTag() && tag == r.max.getTag()) {
+      return r.min <= r.max;
+    }
+  }
+
+  return true;
+}
+
+template <typename Range::Tag rangeTag, typename T>
+bool isRangeValid(const T& paramTag, const Capability& cap) {
+  if (cap.range.getTag() == rangeTag) {
+      const auto& ranges = cap.range.template get<rangeTag>();
+      return isRangeValid(paramTag, ranges);
+  }
+  return true;
+}
+
+}  // namespace aidl::android::hardware::audio::effect