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/AudioParameter.h>
21 #include <utils/Log.h>
22 
23 #include "ConversionHelperHidl.h"
24 
25 using ::android::hardware::audio::V2_0::Result;
26 
27 namespace android {
28 
29 // static
keysFromHal(const String8 & keys,hidl_vec<hidl_string> * hidlKeys)30 status_t ConversionHelperHidl::keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys) {
31     AudioParameter halKeys(keys);
32     if (halKeys.size() == 0) return BAD_VALUE;
33     hidlKeys->resize(halKeys.size());
34     //FIXME:  keyStreamSupportedChannels and keyStreamSupportedSamplingRates come with a
35     // "keyFormat=<value>" pair. We need to transform it into a single key string so that it is
36     // carried over to the legacy HAL via HIDL.
37     String8 value;
38     bool keepFormatValue = halKeys.size() == 2 &&
39          (halKeys.get(String8(AudioParameter::keyStreamSupportedChannels), value) == NO_ERROR ||
40          halKeys.get(String8(AudioParameter::keyStreamSupportedSamplingRates), value) == NO_ERROR);
41 
42     for (size_t i = 0; i < halKeys.size(); ++i) {
43         String8 key;
44         status_t status = halKeys.getAt(i, key);
45         if (status != OK) return status;
46         if (keepFormatValue && key == AudioParameter::keyFormat) {
47             AudioParameter formatParam;
48             halKeys.getAt(i, key, value);
49             formatParam.add(key, value);
50             key = formatParam.toString();
51         }
52         (*hidlKeys)[i] = key.string();
53     }
54     return OK;
55 }
56 
57 // static
parametersFromHal(const String8 & kvPairs,hidl_vec<ParameterValue> * hidlParams)58 status_t ConversionHelperHidl::parametersFromHal(
59         const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) {
60     AudioParameter params(kvPairs);
61     if (params.size() == 0) return BAD_VALUE;
62     hidlParams->resize(params.size());
63     for (size_t i = 0; i < params.size(); ++i) {
64         String8 key, value;
65         status_t status = params.getAt(i, key, value);
66         if (status != OK) return status;
67         (*hidlParams)[i].key = key.string();
68         (*hidlParams)[i].value = value.string();
69     }
70     return OK;
71 }
72 
73 // static
parametersToHal(const hidl_vec<ParameterValue> & parameters,String8 * values)74 void ConversionHelperHidl::parametersToHal(
75         const hidl_vec<ParameterValue>& parameters, String8 *values) {
76     AudioParameter params;
77     for (size_t i = 0; i < parameters.size(); ++i) {
78         params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str()));
79     }
80     values->setTo(params.toString());
81 }
82 
ConversionHelperHidl(const char * className)83 ConversionHelperHidl::ConversionHelperHidl(const char* className)
84         : mClassName(className) {
85 }
86 
87 // static
analyzeResult(const Result & result)88 status_t ConversionHelperHidl::analyzeResult(const Result& result) {
89     switch (result) {
90         case Result::OK: return OK;
91         case Result::INVALID_ARGUMENTS: return BAD_VALUE;
92         case Result::INVALID_STATE: return NOT_ENOUGH_DATA;
93         case Result::NOT_INITIALIZED: return NO_INIT;
94         case Result::NOT_SUPPORTED: return INVALID_OPERATION;
95         default: return NO_INIT;
96     }
97 }
98 
emitError(const char * funcName,const char * description)99 void ConversionHelperHidl::emitError(const char* funcName, const char* description) {
100     ALOGE("%s %p %s: %s (from rpc)", mClassName, this, funcName, description);
101 }
102 
103 }  // namespace android
104