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
convertToOldMsg(V1_2::ContextHubMsg msg)36 inline V1_0::ContextHubMsg convertToOldMsg(V1_2::ContextHubMsg msg) {
37 return msg.msg_1_0;
38 }
39
convertToOldAppInfo(hidl_vec<V1_2::HubAppInfo> appInfos)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,
58 hidl_vec<hidl_string> msgContentPerms) = 0;
59
60 virtual Return<void> handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) = 0;
61
62 virtual Return<void> handleHubEvent(V1_0::AsyncEventType evt) = 0;
63
64 virtual Return<void> handleAppAbort(uint64_t appId, uint32_t abortCode) = 0;
65
66 virtual Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) = 0;
67
68 virtual Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient,
69 uint64_t cookie) = 0;
70
71 virtual Return<bool> unlinkToDeath(const sp<hidl_death_recipient>& recipient) = 0;
72 };
73
74 template <typename T>
75 class ContextHubCallbackWrapper : public IContextHubCallbackWrapperBase {
76 public:
ContextHubCallbackWrapper(sp<T> callback)77 ContextHubCallbackWrapper(sp<T> callback) : mCallback(callback){};
78
handleClientMsg(V1_2::ContextHubMsg msg,hidl_vec<hidl_string>)79 virtual Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
80 hidl_vec<hidl_string> /* msgContentPerms */) override {
81 return mCallback->handleClientMsg(convertToOldMsg(msg));
82 }
83
handleTxnResult(uint32_t txnId,V1_0::TransactionResult result)84 virtual Return<void> handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) override {
85 return mCallback->handleTxnResult(txnId, result);
86 }
87
handleHubEvent(V1_0::AsyncEventType evt)88 virtual Return<void> handleHubEvent(V1_0::AsyncEventType evt) override {
89 return mCallback->handleHubEvent(evt);
90 }
91
handleAppAbort(uint64_t appId,uint32_t abortCode)92 virtual Return<void> handleAppAbort(uint64_t appId, uint32_t abortCode) override {
93 return mCallback->handleAppAbort(appId, abortCode);
94 }
95
handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo)96 virtual Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override {
97 return mCallback->handleAppsInfo(convertToOldAppInfo(appInfo));
98 }
99
linkToDeath(const sp<hidl_death_recipient> & recipient,uint64_t cookie)100 Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t cookie) override {
101 return mCallback->linkToDeath(recipient, cookie);
102 }
103
unlinkToDeath(const sp<hidl_death_recipient> & recipient)104 Return<bool> unlinkToDeath(const sp<hidl_death_recipient>& recipient) override {
105 return mCallback->unlinkToDeath(recipient);
106 }
107
108 protected:
109 sp<T> mCallback;
110 };
111
112 class IContextHubCallbackWrapperV1_0 : public ContextHubCallbackWrapper<V1_0::IContexthubCallback> {
113 public:
IContextHubCallbackWrapperV1_0(sp<V1_0::IContexthubCallback> callback)114 IContextHubCallbackWrapperV1_0(sp<V1_0::IContexthubCallback> callback)
115 : ContextHubCallbackWrapper(callback){};
116 };
117
118 class IContextHubCallbackWrapperV1_2 : public ContextHubCallbackWrapper<V1_2::IContexthubCallback> {
119 public:
IContextHubCallbackWrapperV1_2(sp<V1_2::IContexthubCallback> callback)120 IContextHubCallbackWrapperV1_2(sp<V1_2::IContexthubCallback> callback)
121 : ContextHubCallbackWrapper(callback){};
122
handleClientMsg(V1_2::ContextHubMsg msg,hidl_vec<hidl_string> msgContentPerms)123 Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
124 hidl_vec<hidl_string> msgContentPerms) override {
125 return mCallback->handleClientMsg_1_2(msg, msgContentPerms);
126 }
127
handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo)128 Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override {
129 return mCallback->handleAppsInfo_1_2(appInfo);
130 }
131 };
132
133 } // namespace implementation
134 } // namespace V1_X
135 } // namespace contexthub
136 } // namespace hardware
137 } // namespace android
138
139 #endif // ANDROID_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H