blob: 8823a8dc82cb920414d59905d677b9ce75c50cb0 [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 Rocard51e076a2018-02-28 14:36:53 -080017#ifndef ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_4_0_H
18#define ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_4_0_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;
jiabin9ff780e2018-03-19 18:19:52 -070026using ::android::hardware::audio::V4_0::MicrophoneInfo;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080027using ::android::hardware::Return;
28using ::android::hardware::hidl_string;
29using ::android::hardware::hidl_vec;
30
31namespace android {
Kevin Rocard51e076a2018-02-28 14:36:53 -080032namespace V4_0 {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080033
34class ConversionHelperHidl {
35 protected:
36 static status_t keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys);
37 static status_t parametersFromHal(const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams);
38 static void parametersToHal(const hidl_vec<ParameterValue>& parameters, String8 *values);
jiabin9ff780e2018-03-19 18:19:52 -070039 static void microphoneInfoToHal(const MicrophoneInfo& src,
40 audio_microphone_characteristic_t *pDst);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080041
42 ConversionHelperHidl(const char* className);
43
44 template<typename R, typename T>
45 status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) {
46 if (ret.isOk()) {
47 // This way it also works for enum class to unscoped enum conversion.
48 *retval = static_cast<T>(static_cast<R>(ret));
49 return OK;
50 }
51 return processReturn(funcName, ret);
52 }
53
54 template<typename T>
55 status_t processReturn(const char* funcName, const Return<T>& ret) {
56 if (!ret.isOk()) {
57 emitError(funcName, ret.description().c_str());
58 }
59 return ret.isOk() ? OK : FAILED_TRANSACTION;
60 }
61
Kevin Rocard51e076a2018-02-28 14:36:53 -080062 status_t processReturn(const char* funcName, const Return<hardware::audio::V4_0::Result>& ret) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080063 if (!ret.isOk()) {
64 emitError(funcName, ret.description().c_str());
65 }
66 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
67 }
68
69 template<typename T>
70 status_t processReturn(
Kevin Rocard51e076a2018-02-28 14:36:53 -080071 const char* funcName, const Return<T>& ret, hardware::audio::V4_0::Result retval) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080072 if (!ret.isOk()) {
73 emitError(funcName, ret.description().c_str());
74 }
75 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
76 }
77
78 private:
79 const char* mClassName;
80
Kevin Rocard51e076a2018-02-28 14:36:53 -080081 static status_t analyzeResult(const hardware::audio::V4_0::Result& result);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080082
83 void emitError(const char* funcName, const char* description);
84};
85
Kevin Rocard51e076a2018-02-28 14:36:53 -080086} // namespace V4_0
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080087} // namespace android
88
Kevin Rocard51e076a2018-02-28 14:36:53 -080089#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_4_0_H