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 <functional>
21 
22 #include <hidl/HidlSupport.h>
23 #include <system/audio.h>
24 
25 namespace android {
26 
27 template<typename HalResult>
28 class ConversionHelperHidl {
29   protected:
30     using HalResultConverter = std::function<status_t(const HalResult&)>;
31     const std::string mClassName;
32 
ConversionHelperHidl(std::string_view className,HalResultConverter resultConv)33     ConversionHelperHidl(std::string_view className, HalResultConverter resultConv)
34             : mClassName(className), mResultConverter(resultConv) {}
35 
36     template<typename R, typename T>
processReturn(const char * funcName,const::android::hardware::Return<R> & ret,T * retval)37     status_t processReturn(const char* funcName,
38             const ::android::hardware::Return<R>& ret, T *retval) {
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>
processReturn(const char * funcName,const::android::hardware::Return<T> & ret)48     status_t processReturn(const char* funcName, const ::android::hardware::Return<T>& ret) {
49         if (!ret.isOk()) {
50             emitError(funcName, ret.description().c_str());
51         }
52         return ret.isOk() ? OK : FAILED_TRANSACTION;
53     }
54 
processReturn(const char * funcName,const::android::hardware::Return<HalResult> & ret)55     status_t processReturn(const char* funcName,
56             const ::android::hardware::Return<HalResult>& ret) {
57         if (!ret.isOk()) {
58             emitError(funcName, ret.description().c_str());
59         }
60         return ret.isOk() ? mResultConverter(ret) : FAILED_TRANSACTION;
61     }
62 
63     template<typename T>
processReturn(const char * funcName,const::android::hardware::Return<T> & ret,HalResult retval)64     status_t processReturn(
65             const char* funcName, const ::android::hardware::Return<T>& ret, HalResult retval) {
66         if (!ret.isOk()) {
67             emitError(funcName, ret.description().c_str());
68         }
69         return ret.isOk() ? mResultConverter(retval) : FAILED_TRANSACTION;
70     }
71 
getClassName()72     const std::string& getClassName() const {
73         return mClassName;
74     }
75 
76   private:
77     HalResultConverter mResultConverter;
78 
emitError(const char * funcName,const char * description)79     void emitError(const char* funcName, const char* description) {
80         ALOGE("%s %p %s: %s (from rpc)", mClassName.c_str(), this, funcName, description);
81     }
82 };
83 
84 }  // namespace android
85 
86 #endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H
87