Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #pragma once |
| 18 | |
| 19 | #include <string> |
| 20 | #include <string_view> |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 21 | #include <variant> |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 24 | #include <aidl/android/hardware/audio/core/IModule.h> |
| 25 | #include <aidl/android/hardware/audio/core/IStreamCommon.h> |
| 26 | #include <aidl/android/media/audio/IHalAdapterVendorExtension.h> |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 27 | #include <android-base/expected.h> |
| 28 | #include <error/Result.h> |
| 29 | #include <media/AudioParameter.h> |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 30 | #include <utils/String16.h> |
| 31 | #include <utils/Vector.h> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 35 | class Args { |
| 36 | public: |
| 37 | explicit Args(const Vector<String16>& args) |
| 38 | : mValues(args.size()), mPtrs(args.size()) { |
| 39 | for (size_t i = 0; i < args.size(); ++i) { |
| 40 | mValues[i] = std::string(String8(args[i])); |
| 41 | mPtrs[i] = mValues[i].c_str(); |
| 42 | } |
| 43 | } |
| 44 | const char** args() { return mPtrs.data(); } |
| 45 | private: |
| 46 | std::vector<std::string> mValues; |
| 47 | std::vector<const char*> mPtrs; |
| 48 | }; |
| 49 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 50 | class ConversionHelperAidl { |
| 51 | protected: |
| 52 | ConversionHelperAidl(std::string_view className) : mClassName(className) {} |
| 53 | |
| 54 | const std::string& getClassName() const { |
| 55 | return mClassName; |
| 56 | } |
| 57 | |
| 58 | const std::string mClassName; |
| 59 | }; |
| 60 | |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 61 | // 'action' must accept a value of type 'T' and return 'status_t'. |
| 62 | // The function returns 'true' if the parameter was found, and the action has succeeded. |
| 63 | // The function returns 'false' if the parameter was not found. |
| 64 | // Any errors get propagated, if there are errors it means the parameter was found. |
| 65 | template<typename T, typename F> |
| 66 | error::Result<bool> filterOutAndProcessParameter( |
| 67 | AudioParameter& parameters, const String8& key, const F& action) { |
| 68 | if (parameters.containsKey(key)) { |
| 69 | T value; |
| 70 | status_t status = parameters.get(key, value); |
| 71 | if (status == OK) { |
| 72 | parameters.remove(key); |
| 73 | status = action(value); |
| 74 | if (status == OK) return true; |
| 75 | } |
| 76 | return base::unexpected(status); |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 81 | // Must use the same order of elements as IHalAdapterVendorExtension::ParameterScope. |
| 82 | using VendorParametersRecipient = std::variant< |
| 83 | std::shared_ptr<::aidl::android::hardware::audio::core::IModule>, |
| 84 | std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>>; |
| 85 | status_t parseAndGetVendorParameters( |
| 86 | std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> vendorExt, |
| 87 | const VendorParametersRecipient& recipient, |
| 88 | const AudioParameter& parameterKeys, |
| 89 | String8* values); |
| 90 | status_t parseAndSetVendorParameters( |
| 91 | std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> vendorExt, |
| 92 | const VendorParametersRecipient& recipient, |
| 93 | const AudioParameter& parameters); |
| 94 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 95 | } // namespace android |