Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame^] | 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 |
| 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 | for (size_t i = 0; i < halKeys.size(); ++i) { |
| 35 | String8 key; |
| 36 | status_t status = halKeys.getAt(i, key); |
| 37 | if (status != OK) return status; |
| 38 | (*hidlKeys)[i] = key.string(); |
| 39 | } |
| 40 | return OK; |
| 41 | } |
| 42 | |
| 43 | // static |
| 44 | status_t ConversionHelperHidl::parametersFromHal( |
| 45 | const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams) { |
| 46 | AudioParameter params(kvPairs); |
| 47 | if (params.size() == 0) return BAD_VALUE; |
| 48 | hidlParams->resize(params.size()); |
| 49 | for (size_t i = 0; i < params.size(); ++i) { |
| 50 | String8 key, value; |
| 51 | status_t status = params.getAt(i, key, value); |
| 52 | if (status != OK) return status; |
| 53 | (*hidlParams)[i].key = key.string(); |
| 54 | (*hidlParams)[i].value = value.string(); |
| 55 | } |
| 56 | return OK; |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | void ConversionHelperHidl::parametersToHal( |
| 61 | const hidl_vec<ParameterValue>& parameters, String8 *values) { |
| 62 | AudioParameter params; |
| 63 | for (size_t i = 0; i < parameters.size(); ++i) { |
| 64 | params.add(String8(parameters[i].key.c_str()), String8(parameters[i].value.c_str())); |
| 65 | } |
| 66 | values->setTo(params.toString()); |
| 67 | } |
| 68 | |
| 69 | ConversionHelperHidl::ConversionHelperHidl(const char* className) |
| 70 | : mClassName(className) { |
| 71 | } |
| 72 | |
| 73 | // static |
| 74 | status_t ConversionHelperHidl::analyzeResult(const Result& result) { |
| 75 | switch (result) { |
| 76 | case Result::OK: return OK; |
| 77 | case Result::INVALID_ARGUMENTS: return BAD_VALUE; |
| 78 | case Result::INVALID_STATE: return NOT_ENOUGH_DATA; |
| 79 | case Result::NOT_INITIALIZED: return NO_INIT; |
| 80 | case Result::NOT_SUPPORTED: return INVALID_OPERATION; |
| 81 | default: return NO_INIT; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | status_t ConversionHelperHidl::processReturn(const char* funcName, const Status& status) { |
| 86 | const status_t st = status.transactionError(); |
| 87 | ALOGE_IF(st, "%s %p %s: %s (from rpc)", mClassName, this, funcName, strerror(-st)); |
| 88 | return st; |
| 89 | } |
| 90 | |
| 91 | status_t ConversionHelperHidl::processReturn( |
| 92 | const char* funcName, const Status& status, hardware::audio::V2_0::Result retval) { |
| 93 | const status_t st = status.isOk() ? analyzeResult(retval) : status.transactionError(); |
| 94 | ALOGE_IF(st, "%s %p %s: %s (from %s)", |
| 95 | mClassName, this, funcName, strerror(-st), status.isOk() ? "hal" : "rpc"); |
| 96 | return st; |
| 97 | } |
| 98 | |
| 99 | } // namespace android |