blob: 1dc113c51c78b1f5167b61eed1d29b633bc29c81 [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
Kevin Rocarddf9b4202018-05-10 19:56:08 -070017#ifndef ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H
18#define ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080019
Kevin Rocard51e076a2018-02-28 14:36:53 -080020#include <android/hardware/audio/4.0/types.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080021#include <hidl/HidlSupport.h>
jiabin9ff780e2018-03-19 18:19:52 -070022#include <system/audio.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080023#include <utils/String8.h>
24
Kevin Rocard51e076a2018-02-28 14:36:53 -080025using ::android::hardware::audio::V4_0::ParameterValue;
Kevin Rocarddf9b4202018-05-10 19:56:08 -070026using CoreResult = ::android::hardware::audio::V4_0::Result;
jiabin9ff780e2018-03-19 18:19:52 -070027using ::android::hardware::audio::V4_0::MicrophoneInfo;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080028using ::android::hardware::Return;
29using ::android::hardware::hidl_string;
30using ::android::hardware::hidl_vec;
31
32namespace android {
Kevin Rocard51e076a2018-02-28 14:36:53 -080033namespace V4_0 {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080034
35class ConversionHelperHidl {
36 protected:
37 static status_t keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys);
38 static status_t parametersFromHal(const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams);
39 static void parametersToHal(const hidl_vec<ParameterValue>& parameters, String8 *values);
jiabin9ff780e2018-03-19 18:19:52 -070040 static void microphoneInfoToHal(const MicrophoneInfo& src,
41 audio_microphone_characteristic_t *pDst);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080042
43 ConversionHelperHidl(const char* className);
44
45 template<typename R, typename T>
46 status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) {
47 if (ret.isOk()) {
48 // This way it also works for enum class to unscoped enum conversion.
49 *retval = static_cast<T>(static_cast<R>(ret));
50 return OK;
51 }
52 return processReturn(funcName, ret);
53 }
54
55 template<typename T>
56 status_t processReturn(const char* funcName, const Return<T>& ret) {
57 if (!ret.isOk()) {
58 emitError(funcName, ret.description().c_str());
59 }
60 return ret.isOk() ? OK : FAILED_TRANSACTION;
61 }
62
Kevin Rocarddf9b4202018-05-10 19:56:08 -070063 status_t processReturn(const char* funcName, const Return<CoreResult>& ret) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080064 if (!ret.isOk()) {
65 emitError(funcName, ret.description().c_str());
66 }
67 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
68 }
69
70 template<typename T>
71 status_t processReturn(
Kevin Rocarddf9b4202018-05-10 19:56:08 -070072 const char* funcName, const Return<T>& ret, CoreResult retval) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080073 if (!ret.isOk()) {
74 emitError(funcName, ret.description().c_str());
75 }
76 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
77 }
78
79 private:
80 const char* mClassName;
81
Kevin Rocarddf9b4202018-05-10 19:56:08 -070082 static status_t analyzeResult(const CoreResult& result);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080083
84 void emitError(const char* funcName, const char* description);
85};
86
Kevin Rocard51e076a2018-02-28 14:36:53 -080087} // namespace V4_0
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080088} // namespace android
89
Kevin Rocarddf9b4202018-05-10 19:56:08 -070090#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H