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