blob: d9459b738115107c0ab8545ba2dcc684c92e2192 [file] [log] [blame]
Anthony Stange100aa5e2020-12-17 14:20:02 -05001/*
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
30namespace android {
31namespace hardware {
32namespace contexthub {
33namespace V1_X {
34namespace implementation {
35
36inline V1_0::ContextHubMsg convertToOldMsg(V1_2::ContextHubMsg msg) {
37 return msg.msg_1_0;
38}
39
40inline 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 */
55class IContextHubCallbackWrapperBase : public VirtualLightRefBase {
56 public:
Anthony Stangec3c59b32021-02-10 15:42:10 +000057 virtual Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
58 hidl_vec<hidl_string> msgContentPerms) = 0;
Anthony Stange100aa5e2020-12-17 14:20:02 -050059
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;
Anthony Stange92b14472021-02-15 22:13:26 +000067
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;
Anthony Stange100aa5e2020-12-17 14:20:02 -050072};
73
74template <typename T>
75class ContextHubCallbackWrapper : public IContextHubCallbackWrapperBase {
76 public:
77 ContextHubCallbackWrapper(sp<T> callback) : mCallback(callback){};
78
Anthony Stangec3c59b32021-02-10 15:42:10 +000079 virtual Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
80 hidl_vec<hidl_string> /* msgContentPerms */) override {
Anthony Stange100aa5e2020-12-17 14:20:02 -050081 return mCallback->handleClientMsg(convertToOldMsg(msg));
82 }
83
84 virtual Return<void> handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) override {
85 return mCallback->handleTxnResult(txnId, result);
86 }
87
88 virtual Return<void> handleHubEvent(V1_0::AsyncEventType evt) override {
89 return mCallback->handleHubEvent(evt);
90 }
91
92 virtual Return<void> handleAppAbort(uint64_t appId, uint32_t abortCode) override {
93 return mCallback->handleAppAbort(appId, abortCode);
94 }
95
96 virtual Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override {
97 return mCallback->handleAppsInfo(convertToOldAppInfo(appInfo));
98 }
99
Anthony Stange92b14472021-02-15 22:13:26 +0000100 Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t cookie) override {
101 return mCallback->linkToDeath(recipient, cookie);
102 }
103
104 Return<bool> unlinkToDeath(const sp<hidl_death_recipient>& recipient) override {
105 return mCallback->unlinkToDeath(recipient);
106 }
107
Anthony Stange100aa5e2020-12-17 14:20:02 -0500108 protected:
109 sp<T> mCallback;
110};
111
112class IContextHubCallbackWrapperV1_0 : public ContextHubCallbackWrapper<V1_0::IContexthubCallback> {
113 public:
114 IContextHubCallbackWrapperV1_0(sp<V1_0::IContexthubCallback> callback)
115 : ContextHubCallbackWrapper(callback){};
116};
117
118class IContextHubCallbackWrapperV1_2 : public ContextHubCallbackWrapper<V1_2::IContexthubCallback> {
119 public:
120 IContextHubCallbackWrapperV1_2(sp<V1_2::IContexthubCallback> callback)
121 : ContextHubCallbackWrapper(callback){};
122
Anthony Stangec3c59b32021-02-10 15:42:10 +0000123 Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
124 hidl_vec<hidl_string> msgContentPerms) override {
125 return mCallback->handleClientMsg_1_2(msg, msgContentPerms);
Anthony Stange100aa5e2020-12-17 14:20:02 -0500126 }
127
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