/* * 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 #include #include #include #include #include #include #include #include #include #include #include namespace android { /* * Helper macro to add instance name, function name in logs * classes should provide getInstanceName API to use these macros. * print function names along with instance name. * * Usage: * AUGMENT_LOG(D); * AUGMENT_LOG(I, "hello!"); * AUGMENT_LOG(W, "value: %d", value); * * AUGMENT_LOG_IF(D, value < 0, "negative"); * AUGMENT_LOG_IF(E, value < 0, "bad value: %d", value); */ #define AUGMENT_LOG(level, ...) \ ALOG##level("[%s] %s" __VA_OPT__(": " __android_second(0, __VA_ARGS__, "")), \ getInstanceName().c_str(), __func__ __VA_OPT__(__android_rest(__VA_ARGS__))) #define AUGMENT_LOG_IF(level, cond, ...) \ ALOG##level##_IF(cond, "[%s] %s" __VA_OPT__(": " __android_second(0, __VA_ARGS__, "")), \ getInstanceName().c_str(), __func__ __VA_OPT__(__android_rest(__VA_ARGS__))) class Args { public: explicit Args(const Vector& args) : mValues(args.size()), mPtrs(args.size()) { for (size_t i = 0; i < args.size(); ++i) { mValues[i] = std::string(String8(args[i])); mPtrs[i] = mValues[i].c_str(); } } const char** args() { return mPtrs.data(); } private: std::vector mValues; std::vector mPtrs; }; class ConversionHelperAidl { protected: ConversionHelperAidl(std::string_view className, std::string_view instanceName) : mClassName(className), mInstanceName(instanceName) {} const std::string& getClassName() const { return mClassName; } const std::string& getInstanceName() const { return mInstanceName; } const std::string mClassName; const std::string mInstanceName; }; // 'action' must accept a value of type 'T' and return 'status_t'. // The function returns 'true' if the parameter was found, and the action has succeeded. // The function returns 'false' if the parameter was not found. // Any errors get propagated, if there are errors it means the parameter was found. template error::Result filterOutAndProcessParameter( AudioParameter& parameters, const String8& key, const F& action) { if (parameters.containsKey(key)) { T value; status_t status = parameters.get(key, value); if (status == OK) { parameters.remove(key); status = action(value); if (status == OK) return true; } return base::unexpected(status); } return false; } // Must use the same order of elements as IHalAdapterVendorExtension::ParameterScope. using VendorParametersRecipient = std::variant< std::shared_ptr<::aidl::android::hardware::audio::core::IModule>, std::shared_ptr<::aidl::android::hardware::audio::core::IStreamCommon>>; status_t parseAndGetVendorParameters( std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> vendorExt, const VendorParametersRecipient& recipient, const AudioParameter& parameterKeys, String8* values); status_t parseAndSetVendorParameters( std::shared_ptr<::aidl::android::media::audio::IHalAdapterVendorExtension> vendorExt, const VendorParametersRecipient& recipient, const AudioParameter& parameters); } // namespace android