blob: d8cc37b5f2c6bd36985691ed45b1919252ed0a17 [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;
67};
68
69template <typename T>
70class ContextHubCallbackWrapper : public IContextHubCallbackWrapperBase {
71 public:
72 ContextHubCallbackWrapper(sp<T> callback) : mCallback(callback){};
73
Anthony Stangec3c59b32021-02-10 15:42:10 +000074 virtual Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
75 hidl_vec<hidl_string> /* msgContentPerms */) override {
Anthony Stange100aa5e2020-12-17 14:20:02 -050076 return mCallback->handleClientMsg(convertToOldMsg(msg));
77 }
78
79 virtual Return<void> handleTxnResult(uint32_t txnId, V1_0::TransactionResult result) override {
80 return mCallback->handleTxnResult(txnId, result);
81 }
82
83 virtual Return<void> handleHubEvent(V1_0::AsyncEventType evt) override {
84 return mCallback->handleHubEvent(evt);
85 }
86
87 virtual Return<void> handleAppAbort(uint64_t appId, uint32_t abortCode) override {
88 return mCallback->handleAppAbort(appId, abortCode);
89 }
90
91 virtual Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override {
92 return mCallback->handleAppsInfo(convertToOldAppInfo(appInfo));
93 }
94
95 protected:
96 sp<T> mCallback;
97};
98
99class IContextHubCallbackWrapperV1_0 : public ContextHubCallbackWrapper<V1_0::IContexthubCallback> {
100 public:
101 IContextHubCallbackWrapperV1_0(sp<V1_0::IContexthubCallback> callback)
102 : ContextHubCallbackWrapper(callback){};
103};
104
105class IContextHubCallbackWrapperV1_2 : public ContextHubCallbackWrapper<V1_2::IContexthubCallback> {
106 public:
107 IContextHubCallbackWrapperV1_2(sp<V1_2::IContexthubCallback> callback)
108 : ContextHubCallbackWrapper(callback){};
109
Anthony Stangec3c59b32021-02-10 15:42:10 +0000110 Return<void> handleClientMsg(V1_2::ContextHubMsg msg,
111 hidl_vec<hidl_string> msgContentPerms) override {
112 return mCallback->handleClientMsg_1_2(msg, msgContentPerms);
Anthony Stange100aa5e2020-12-17 14:20:02 -0500113 }
114
115 Return<void> handleAppsInfo(hidl_vec<V1_2::HubAppInfo> appInfo) override {
116 return mCallback->handleAppsInfo_1_2(appInfo);
117 }
118};
119
120} // namespace implementation
121} // namespace V1_X
122} // namespace contexthub
123} // namespace hardware
124} // namespace android
125
126#endif // ANDROID_HARDWARE_CONTEXTHUB_V1_X_ICONTEXTHUBCALLBACKWRAPPER_H