1 /*
2 * Copyright (C) 2016 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 #include <string.h>
18
19 #define LOG_TAG "HalHidl"
20 #include <media/AudioContainers.h>
21 #include <media/AudioParameter.h>
22 #include <utils/Log.h>
23
24 #include "CoreConversionHelperHidl.h"
25
26 namespace android {
27
28 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
29 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
30
31 // static
keysFromHal(const String8 & keys,hidl_vec<hidl_string> * hidlKeys)32 status_t CoreConversionHelperHidl::keysFromHal(
33 const String8& keys, hidl_vec<hidl_string> *hidlKeys) {
34 AudioParameter halKeys(keys);
35 if (halKeys.size() == 0) return BAD_VALUE;
36 hidlKeys->resize(halKeys.size());
37 //FIXME: keyStreamSupportedChannels and keyStreamSupportedSamplingRates come with a
38 // "keyFormat=<value>" pair. We need to transform it into a single key string so that it is
39 // carried over to the legacy HAL via HIDL.
40 String8 value;
41 bool keepFormatValue = halKeys.size() == 2 &&
42 (halKeys.get(String8(AudioParameter::keyStreamSupportedChannels), value) == NO_ERROR ||
43 halKeys.get(String8(AudioParameter::keyStreamSupportedSamplingRates), value) == NO_ERROR);
44 // When querying encapsulation capabilities, "keyRouting=<value>" pair is used to identify
45 // the device. We need to transform it into a single key string so that it is carried over to
46 // the legacy HAL via HIDL.
47 bool keepRoutingValue =
48 halKeys.get(String8(AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_MODES),
49 value) == NO_ERROR ||
50 halKeys.get(String8(AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_METADATA_TYPES),
51 value) == NO_ERROR;
52
53 const bool keepDelayValue =
54 halKeys.get(String8(AudioParameter::keyAdditionalOutputDeviceDelay),
55 value) == NO_ERROR ||
56 halKeys.get(String8(AudioParameter::keyMaxAdditionalOutputDeviceDelay),
57 value) == NO_ERROR;
58
59 for (size_t i = 0; i < halKeys.size(); ++i) {
60 String8 key;
61 status_t status = halKeys.getAt(i, key);
62 if (status != OK) return status;
63 if ((keepFormatValue && key == AudioParameter::keyFormat) ||
64 (keepRoutingValue && key == AudioParameter::keyRouting) ||
65 (keepDelayValue && key == AudioParameter::keyAdditionalOutputDeviceDelay) ||
66 (keepDelayValue && key == AudioParameter::keyMaxAdditionalOutputDeviceDelay)) {
67 AudioParameter keepValueParam;
68 halKeys.getAt(i, key, value);
69 keepValueParam.add(key, value);
70 key = keepValueParam.toString();
71 }
72 (*hidlKeys)[i] = key.string();
73 }
74 return OK;
75 }
76
77 // static
parametersFromHal(const String8 & kvPairs,hidl_vec<ParameterValue> * hidlParams)78 status_t CoreConversionHelperHidl::parametersFromHal(
79 const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) {
80 AudioParameter params(kvPairs);
81 if (params.size() == 0) return BAD_VALUE;
82 hidlParams->resize(params.size());
83 for (size_t i = 0; i < params.size(); ++i) {
84 String8 key, value;
85 status_t status = params.getAt(i, key, value);
86 if (status != OK) return status;
87 (*hidlParams)[i].key = key.string();
88 (*hidlParams)[i].value = value.string();
89 }
90 return OK;
91 }
92
93 // static
parametersToHal(const hidl_vec<ParameterValue> & parameters,String8 * values)94 void CoreConversionHelperHidl::parametersToHal(
95 const hidl_vec<ParameterValue>& parameters, String8 *values) {
96 AudioParameter params;
97 for (size_t i = 0; i < parameters.size(); ++i) {
98 params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
99 }
100 values->setTo(params.toString());
101 }
102
CoreConversionHelperHidl(std::string_view className)103 CoreConversionHelperHidl::CoreConversionHelperHidl(std::string_view className)
104 : ConversionHelperHidl<CoreResult>(className, analyzeResult) {}
105
106 // static
argsFromHal(const Vector<String16> & args,hidl_vec<hidl_string> * hidlArgs)107 void CoreConversionHelperHidl::argsFromHal(
108 const Vector<String16>& args, hidl_vec<hidl_string> *hidlArgs) {
109 hidlArgs->resize(args.size());
110 for (size_t i = 0; i < args.size(); ++i) {
111 (*hidlArgs)[i] = String8(args[i]).c_str();
112 }
113 }
114
115 // static
analyzeResult(const CoreResult & result)116 status_t CoreConversionHelperHidl::analyzeResult(const CoreResult& result) {
117 switch (result) {
118 case Result::OK: return OK;
119 case Result::INVALID_ARGUMENTS: return BAD_VALUE;
120 case Result::INVALID_STATE: return NOT_ENOUGH_DATA;
121 case Result::NOT_INITIALIZED: return NO_INIT;
122 case Result::NOT_SUPPORTED: return INVALID_OPERATION;
123 }
124 return NO_INIT;
125 }
126
127 } // namespace android
128