blob: 00d5b2ce9465aeb86fa3d41961c2c197cd10cfce [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -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
17#ifndef ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H
18#define ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H
19
20#include <android/hardware/audio/2.0/types.h>
21#include <hidl/HidlSupport.h>
22#include <utils/String8.h>
23
24using ::android::hardware::audio::V2_0::ParameterValue;
25using ::android::hardware::Return;
26using ::android::hardware::Status;
27using ::android::hardware::hidl_string;
28using ::android::hardware::hidl_vec;
29
30namespace android {
31
32class ConversionHelperHidl {
Mikhail Naganovf558e022016-11-14 17:45:17 -080033 protected:
34 static status_t keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys);
35 static status_t parametersFromHal(const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams);
36 static void parametersToHal(const hidl_vec<ParameterValue>& parameters, String8 *values);
37
38 ConversionHelperHidl(const char* className);
39
Mikhail Naganovf558e022016-11-14 17:45:17 -080040 template<typename R, typename T>
41 status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) {
Steven Morelande83be8a2017-01-06 11:06:33 -080042 if (ret.isOk()) {
Mikhail Naganovf558e022016-11-14 17:45:17 -080043 // This way it also works for enum class to unscoped enum conversion.
44 *retval = static_cast<T>(static_cast<R>(ret));
45 return OK;
46 }
Steven Morelande83be8a2017-01-06 11:06:33 -080047 return processReturn(funcName, ret);
48 }
49
50 template<typename T>
51 status_t processReturn(const char* funcName, const Return<T>& ret) {
52 if (!ret.isOk()) {
53 emitError(funcName, ret.description().c_str());
54 }
55 return ret.isOk() ? OK : UNKNOWN_ERROR;
Mikhail Naganovf558e022016-11-14 17:45:17 -080056 }
57
58 status_t processReturn(const char* funcName, const Return<hardware::audio::V2_0::Result>& ret) {
59 return processReturn(funcName, ret, ret);
60 }
61
62 template<typename T>
63 status_t processReturn(
64 const char* funcName, const Return<T>& ret, hardware::audio::V2_0::Result retval) {
Steven Morelande83be8a2017-01-06 11:06:33 -080065 const status_t st = ret.isOk() ? analyzeResult(retval) : UNKNOWN_ERROR;
66 if (!ret.isOk()) {
67 emitError(funcName, ret.description().c_str());
Steven Morelande83be8a2017-01-06 11:06:33 -080068 }
69 return st;
Mikhail Naganovf558e022016-11-14 17:45:17 -080070 }
71
72 private:
73 const char* mClassName;
74
75 static status_t analyzeResult(const hardware::audio::V2_0::Result& result);
Steven Morelande83be8a2017-01-06 11:06:33 -080076
77 void emitError(const char* funcName, const char* description);
Mikhail Naganovf558e022016-11-14 17:45:17 -080078};
79
80} // namespace android
81
82#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H