Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 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 | |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H |
| 18 | #define ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 19 | |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 20 | #include <functional> |
| 21 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 22 | #include <hidl/HidlSupport.h> |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 23 | #include <system/audio.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 27 | template<typename HalResult> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 28 | class ConversionHelperHidl { |
| 29 | protected: |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 30 | using HalResultConverter = std::function<status_t(const HalResult&)>; |
| 31 | const std::string mClassName; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 32 | |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 33 | ConversionHelperHidl(std::string_view className, HalResultConverter resultConv) |
| 34 | : mClassName(className), mResultConverter(resultConv) {} |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 35 | |
| 36 | template<typename R, typename T> |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 37 | status_t processReturn(const char* funcName, |
| 38 | const ::android::hardware::Return<R>& ret, T *retval) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 39 | 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 Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 48 | status_t processReturn(const char* funcName, const ::android::hardware::Return<T>& ret) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 49 | if (!ret.isOk()) { |
| 50 | emitError(funcName, ret.description().c_str()); |
| 51 | } |
| 52 | return ret.isOk() ? OK : FAILED_TRANSACTION; |
| 53 | } |
| 54 | |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 55 | status_t processReturn(const char* funcName, |
| 56 | const ::android::hardware::Return<HalResult>& ret) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 57 | if (!ret.isOk()) { |
| 58 | emitError(funcName, ret.description().c_str()); |
| 59 | } |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 60 | return ret.isOk() ? mResultConverter(ret) : FAILED_TRANSACTION; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | template<typename T> |
| 64 | status_t processReturn( |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 65 | const char* funcName, const ::android::hardware::Return<T>& ret, HalResult retval) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 66 | if (!ret.isOk()) { |
| 67 | emitError(funcName, ret.description().c_str()); |
| 68 | } |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 69 | return ret.isOk() ? mResultConverter(retval) : FAILED_TRANSACTION; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 72 | const std::string& getClassName() const { |
| 73 | return mClassName; |
| 74 | } |
| 75 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 76 | private: |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 77 | HalResultConverter mResultConverter; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 78 | |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 79 | void emitError(const char* funcName, const char* description) { |
| 80 | ALOGE("%s %p %s: %s (from rpc)", mClassName.c_str(), this, funcName, description); |
| 81 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace android |
| 85 | |
Kevin Rocard | df9b420 | 2018-05-10 19:56:08 -0700 | [diff] [blame] | 86 | #endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H |