blob: 6e2c8312cd151dce3d12acf43b3fdf8187f1a80d [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
Mikhail Naganov288a3432022-03-25 00:29:56 +000020#include <functional>
21
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080022#include <hidl/HidlSupport.h>
jiabin9ff780e2018-03-19 18:19:52 -070023#include <system/audio.h>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080024
25namespace android {
26
Mikhail Naganov288a3432022-03-25 00:29:56 +000027template<typename HalResult>
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080028class ConversionHelperHidl {
29 protected:
Mikhail Naganov288a3432022-03-25 00:29:56 +000030 using HalResultConverter = std::function<status_t(const HalResult&)>;
31 const std::string mClassName;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080032
Mikhail Naganov288a3432022-03-25 00:29:56 +000033 ConversionHelperHidl(std::string_view className, HalResultConverter resultConv)
34 : mClassName(className), mResultConverter(resultConv) {}
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080035
36 template<typename R, typename T>
Mikhail Naganov288a3432022-03-25 00:29:56 +000037 status_t processReturn(const char* funcName,
38 const ::android::hardware::Return<R>& ret, T *retval) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080039 if (ret.isOk()) {
40 // This way it also works for enum class to unscoped enum conversion.
41 *retval = static_cast<T>(static_cast<R>(ret));
42 return OK;
43 }
44 return processReturn(funcName, ret);
45 }
46
47 template<typename T>
Mikhail Naganov288a3432022-03-25 00:29:56 +000048 status_t processReturn(const char* funcName, const ::android::hardware::Return<T>& ret) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080049 if (!ret.isOk()) {
50 emitError(funcName, ret.description().c_str());
51 }
52 return ret.isOk() ? OK : FAILED_TRANSACTION;
53 }
54
Mikhail Naganov288a3432022-03-25 00:29:56 +000055 status_t processReturn(const char* funcName,
56 const ::android::hardware::Return<HalResult>& ret) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080057 if (!ret.isOk()) {
58 emitError(funcName, ret.description().c_str());
59 }
Mikhail Naganov288a3432022-03-25 00:29:56 +000060 return ret.isOk() ? mResultConverter(ret) : FAILED_TRANSACTION;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080061 }
62
63 template<typename T>
64 status_t processReturn(
Mikhail Naganov288a3432022-03-25 00:29:56 +000065 const char* funcName, const ::android::hardware::Return<T>& ret, HalResult retval) {
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080066 if (!ret.isOk()) {
67 emitError(funcName, ret.description().c_str());
68 }
Mikhail Naganov288a3432022-03-25 00:29:56 +000069 return ret.isOk() ? mResultConverter(retval) : FAILED_TRANSACTION;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080070 }
71
Andy Hung224f82f2022-03-22 00:00:49 -070072 const std::string& getClassName() const {
73 return mClassName;
74 }
75
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080076 private:
Mikhail Naganov288a3432022-03-25 00:29:56 +000077 HalResultConverter mResultConverter;
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080078
Mikhail Naganov288a3432022-03-25 00:29:56 +000079 void emitError(const char* funcName, const char* description) {
80 ALOGE("%s %p %s: %s (from rpc)", mClassName.c_str(), this, funcName, description);
81 }
Kevin Rocard4bcd67f2018-02-28 14:33:38 -080082};
83
84} // namespace android
85
Kevin Rocarddf9b4202018-05-10 19:56:08 -070086#endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H