blob: 2909013084858b79b2e508c16dc4c8ad07c58360 [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>
Andy Hung61589a42021-06-16 09:37:53 -070024#include <utils/String16.h>
25#include <utils/Vector.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080026
Kevin Rocard070e7512018-05-22 09:29:13 -070027using ::android::hardware::audio::CPP_VERSION::ParameterValue;
28using CoreResult = ::android::hardware::audio::CPP_VERSION::Result;
29
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080030using ::android::hardware::Return;
31using ::android::hardware::hidl_string;
32using ::android::hardware::hidl_vec;
33
34namespace android {
Kevin Rocard070e7512018-05-22 09:29:13 -070035namespace CPP_VERSION {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080036
37class ConversionHelperHidl {
38 protected:
39 static status_t keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys);
40 static status_t parametersFromHal(const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams);
41 static void parametersToHal(const hidl_vec<ParameterValue>& parameters, String8 *values);
Andy Hung61589a42021-06-16 09:37:53 -070042 static void argsFromHal(const Vector<String16>& args, hidl_vec<hidl_string> *hidlArgs);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080043
44 ConversionHelperHidl(const char* className);
45
46 template<typename R, typename T>
47 status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) {
48 if (ret.isOk()) {
49 // This way it also works for enum class to unscoped enum conversion.
50 *retval = static_cast<T>(static_cast<R>(ret));
51 return OK;
52 }
53 return processReturn(funcName, ret);
54 }
55
56 template<typename T>
57 status_t processReturn(const char* funcName, const Return<T>& ret) {
58 if (!ret.isOk()) {
59 emitError(funcName, ret.description().c_str());
60 }
61 return ret.isOk() ? OK : FAILED_TRANSACTION;
62 }
63
Kevin Rocarddf9b4202018-05-10 19:56:08 -070064 status_t processReturn(const char* funcName, const Return<CoreResult>& ret) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080065 if (!ret.isOk()) {
66 emitError(funcName, ret.description().c_str());
67 }
68 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
69 }
70
71 template<typename T>
72 status_t processReturn(
Kevin Rocarddf9b4202018-05-10 19:56:08 -070073 const char* funcName, const Return<T>& ret, CoreResult retval) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080074 if (!ret.isOk()) {
75 emitError(funcName, ret.description().c_str());
76 }
77 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
78 }
79
80 private:
81 const char* mClassName;
82
Kevin Rocarddf9b4202018-05-10 19:56:08 -070083 static status_t analyzeResult(const CoreResult& result);
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080084
85 void emitError(const char* funcName, const char* description);
86};
87
Kevin Rocard070e7512018-05-22 09:29:13 -070088} // namespace CPP_VERSION
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080089} // namespace android
90
Kevin Rocarddf9b4202018-05-10 19:56:08 -070091#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H