Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -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 | |
| 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 | |
| 24 | using ::android::hardware::audio::V2_0::ParameterValue; |
| 25 | using ::android::hardware::Return; |
| 26 | using ::android::hardware::Status; |
| 27 | using ::android::hardware::hidl_string; |
| 28 | using ::android::hardware::hidl_vec; |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | class ConversionHelperHidl { |
Mikhail Naganov | 9f57e3c | 2016-12-05 12:54:36 -0800 | [diff] [blame^] | 33 | public: |
| 34 | static void crashIfHalIsDead(const Status& status); |
| 35 | |
Mikhail Naganov | f558e02 | 2016-11-14 17:45:17 -0800 | [diff] [blame] | 36 | protected: |
| 37 | static status_t keysFromHal(const String8& keys, hidl_vec<hidl_string> *hidlKeys); |
| 38 | static status_t parametersFromHal(const String8& kvPairs, hidl_vec<ParameterValue> *hidlParams); |
| 39 | static void parametersToHal(const hidl_vec<ParameterValue>& parameters, String8 *values); |
| 40 | |
| 41 | ConversionHelperHidl(const char* className); |
| 42 | |
| 43 | status_t processReturn(const char* funcName, const Return<void>& ret) { |
| 44 | return processReturn(funcName, ret.getStatus()); |
| 45 | } |
| 46 | |
| 47 | template<typename R, typename T> |
| 48 | status_t processReturn(const char* funcName, const Return<R>& ret, T *retval) { |
| 49 | if (ret.getStatus().isOk()) { |
| 50 | // This way it also works for enum class to unscoped enum conversion. |
| 51 | *retval = static_cast<T>(static_cast<R>(ret)); |
| 52 | return OK; |
| 53 | } |
| 54 | return processReturn(funcName, ret.getStatus()); |
| 55 | } |
| 56 | |
| 57 | status_t processReturn(const char* funcName, const Return<hardware::audio::V2_0::Result>& ret) { |
| 58 | return processReturn(funcName, ret, ret); |
| 59 | } |
| 60 | |
| 61 | template<typename T> |
| 62 | status_t processReturn( |
| 63 | const char* funcName, const Return<T>& ret, hardware::audio::V2_0::Result retval) { |
| 64 | return processReturn(funcName, ret.getStatus(), retval); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | const char* mClassName; |
| 69 | |
| 70 | static status_t analyzeResult(const hardware::audio::V2_0::Result& result); |
| 71 | status_t processReturn(const char* funcName, const Status& status); |
| 72 | status_t processReturn( |
| 73 | const char* funcName, const Status& status, hardware::audio::V2_0::Result retval); |
| 74 | }; |
| 75 | |
| 76 | } // namespace android |
| 77 | |
| 78 | #endif // ANDROID_HARDWARE_CONVERSION_HELPER_HIDL_H |