1/*
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::hidl_string;
27using ::android::hardware::hidl_vec;
28
29namespace android {
30
31class ConversionHelperHidl {
32  protected:
33    static status_t keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys);
34    static status_t parametersFromHal(const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams);
35    static void parametersToHal(const hidl_vec<ParameterValue>& parameters, String8 *values);
36
37    ConversionHelperHidl(const char* className);
38
39    template<typename R, typename T>
40    status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) {
41        if (ret.isOk()) {
42            // This way it also works for enum class to unscoped enum conversion.
43            *retval = static_cast<T>(static_cast<R>(ret));
44            return OK;
45        }
46        return processReturn(funcName, ret);
47    }
48
49    template<typename T>
50    status_t processReturn(const char* funcName, const Return<T>& ret) {
51        if (!ret.isOk()) {
52            emitError(funcName, ret.description().c_str());
53        }
54        return ret.isOk() ? OK : FAILED_TRANSACTION;
55    }
56
57    status_t processReturn(const char* funcName, const Return<hardware::audio::V2_0::Result>& ret) {
58        if (!ret.isOk()) {
59            emitError(funcName, ret.description().c_str());
60        }
61        return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
62    }
63
64    template<typename T>
65    status_t processReturn(
66            const char* funcName, const Return<T>& ret, hardware::audio::V2_0::Result retval) {
67        if (!ret.isOk()) {
68            emitError(funcName, ret.description().c_str());
69        }
70        return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
71    }
72
73  private:
74    const char* mClassName;
75
76    static status_t analyzeResult(const hardware::audio::V2_0::Result& result);
77
78    void emitError(const char* funcName, const char* description);
79};
80
81}  // namespace android
82
83#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H
84