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