blob: 52a14564a714d65fa20be659656d04858ef01f6e [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 Rocard95213bf2018-11-08 17:16:57 -080020#include PATH(android/hardware/audio/FILE_VERSION/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 Rocard070e7512018-05-22 09:29:13 -070025using ::android::hardware::audio::CPP_VERSION::ParameterValue;
26using CoreResult = ::android::hardware::audio::CPP_VERSION::Result;
27
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 Rocard070e7512018-05-22 09:29:13 -070033namespace CPP_VERSION {
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);
40
41 ConversionHelperHidl(const char* className);
42
43 template<typename R, typename T>
44 status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) {
45 if (ret.isOk()) {
46 // This way it also works for enum class to unscoped enum conversion.
47 *retval = static_cast<T>(static_cast<R>(ret));
48 return OK;
49 }
50 return processReturn(funcName, ret);
51 }
52
53 template<typename T>
54 status_t processReturn(const char* funcName, const Return<T>& ret) {
55 if (!ret.isOk()) {
56 emitError(funcName, ret.description().c_str());
57 }
58 return ret.isOk() ? OK : FAILED_TRANSACTION;
59 }
60
Kevin Rocarddf9b4202018-05-10 19:56:08 -070061 status_t processReturn(const char* funcName, const Return<CoreResult>& ret) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080062 if (!ret.isOk()) {
63 emitError(funcName, ret.description().c_str());
64 }
65 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
66 }
67
68 template<typename T>
69 status_t processReturn(
Kevin Rocarddf9b4202018-05-10 19:56:08 -070070 const char* funcName, const Return<T>& ret, CoreResult retval) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080071 if (!ret.isOk()) {
72 emitError(funcName, ret.description().c_str());
73 }
74 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
75 }
76
77 private:
78 const char* mClassName;
79
Kevin Rocarddf9b4202018-05-10 19:56:08 -070080 static status_t analyzeResult(const CoreResult& result);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080081
82 void emitError(const char* funcName, const char* description);
83};
84
Kevin Rocard070e7512018-05-22 09:29:13 -070085#if MAJOR_VERSION == 4
86using ::android::hardware::audio::CPP_VERSION::MicrophoneInfo;
87void microphoneInfoToHal(const MicrophoneInfo& src,
88 audio_microphone_characteristic_t *pDst);
89#endif
90
91} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080092} // namespace android
93
Kevin Rocarddf9b4202018-05-10 19:56:08 -070094#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H