Anthony Stange | 100aa5e | 2020-12-17 14:20:02 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H |
| 18 | #define ANDROID_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H |
| 19 | |
| 20 | #include "android/hardware/contexthub/1.0/IContexthub.h" |
| 21 | #include "android/hardware/contexthub/1.0/IContexthubCallback.h" |
| 22 | #include "android/hardware/contexthub/1.0/types.h" |
| 23 | #include "android/hardware/contexthub/1.2/IContexthubCallback.h" |
| 24 | #include "android/hardware/contexthub/1.2/types.h" |
| 25 | |
| 26 | #include <utils/LightRefBase.h> |
| 27 | |
| 28 | #include <cassert> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace contexthub { |
| 33 | namespace V1_X { |
| 34 | namespace implementation { |
| 35 | |
| 36 | inline V1_0::ContextHubMsg convertToOldMsg(V1_2::ContextHubMsg msg) { |
| 37 | return msg.msg_1_0; |
| 38 | } |
| 39 | |
| 40 | inline hidl_vec<V1_0::HubAppInfo> convertToOldAppInfo(hidl_vec<V1_2::HubAppInfo> appInfos) { |
| 41 | hidl_vec<V1_0::HubAppInfo> convertedInfo(appInfos.size()); |
| 42 | for (int i = 0; i < appInfos.size(); ++i) { |
| 43 | convertedInfo[i] = appInfos[i].info_1_0; |
| 44 | } |
| 45 | |
| 46 | return convertedInfo; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * The IContexthubCallback classes below abstract away the common logic between both the V1.0, and |
| 51 | * V1.2 versions of the Contexthub HAL callback interface. This allows users of these classes to |
| 52 | * only care about the HAL version at init time and then interact with either version of the |
| 53 | * callback without worrying about the class type by utilizing the base class. |
| 54 | */ |
| 55 | class IContextHubCallbackWrapperBase : public VirtualLightRefBase { |
| 56 | public: |
| 57 | virtual Return<void> handleClientMsg(V1_2::ContextHubMsg msg) = 0; |
| 58 | |
| 59 | virtual Return<void> handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) = 0; |
| 60 | |
| 61 | virtual Return<void> handleHubEvent(V1_0::AsyncEventType evt) = 0; |
| 62 | |
| 63 | virtual Return<void> handleAppAbort(uint64_t appId, uint32_t abortCode) = 0; |
| 64 | |
| 65 | virtual Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) = 0; |
| 66 | }; |
| 67 | |
| 68 | template <typename T> |
| 69 | class ContextHubCallbackWrapper : public IContextHubCallbackWrapperBase { |
| 70 | public: |
| 71 | ContextHubCallbackWrapper(sp<T> callback) : mCallback(callback){}; |
| 72 | |
| 73 | virtual Return<void> handleClientMsg(V1_2::ContextHubMsg msg) override { |
| 74 | return mCallback->handleClientMsg(convertToOldMsg(msg)); |
| 75 | } |
| 76 | |
| 77 | virtual Return<void> handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) override { |
| 78 | return mCallback->handleTxnResult(txnId, result); |
| 79 | } |
| 80 | |
| 81 | virtual Return<void> handleHubEvent(V1_0::AsyncEventType evt) override { |
| 82 | return mCallback->handleHubEvent(evt); |
| 83 | } |
| 84 | |
| 85 | virtual Return<void> handleAppAbort(uint64_t appId, uint32_t abortCode) override { |
| 86 | return mCallback->handleAppAbort(appId, abortCode); |
| 87 | } |
| 88 | |
| 89 | virtual Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override { |
| 90 | return mCallback->handleAppsInfo(convertToOldAppInfo(appInfo)); |
| 91 | } |
| 92 | |
| 93 | protected: |
| 94 | sp<T> mCallback; |
| 95 | }; |
| 96 | |
| 97 | class IContextHubCallbackWrapperV1_0 : public ContextHubCallbackWrapper<V1_0::IContexthubCallback> { |
| 98 | public: |
| 99 | IContextHubCallbackWrapperV1_0(sp<V1_0::IContexthubCallback> callback) |
| 100 | : ContextHubCallbackWrapper(callback){}; |
| 101 | }; |
| 102 | |
| 103 | class IContextHubCallbackWrapperV1_2 : public ContextHubCallbackWrapper<V1_2::IContexthubCallback> { |
| 104 | public: |
| 105 | IContextHubCallbackWrapperV1_2(sp<V1_2::IContexthubCallback> callback) |
| 106 | : ContextHubCallbackWrapper(callback){}; |
| 107 | |
| 108 | Return<void> handleClientMsg(V1_2::ContextHubMsg msg) override { |
| 109 | return mCallback->handleClientMsg_1_2(msg); |
| 110 | } |
| 111 | |
| 112 | Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override { |
| 113 | return mCallback->handleAppsInfo_1_2(appInfo); |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | } // namespace implementation |
| 118 | } // namespace V1_X |
| 119 | } // namespace contexthub |
| 120 | } // namespace hardware |
| 121 | } // namespace android |
| 122 | |
| 123 | #endif // ANDROID_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H |