blob: ce97c786d9e463acc46003c1d0807843879494a0 [file] [log] [blame]
Pawin Vongmasafbffe922017-03-01 02:25:36 -08001/*
2 * Copyright (C) 2017 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_HALTOKEN_H
18#define ANDROID_HALTOKEN_H
19
20#include <binder/Parcel.h>
21#include <hidl/HidlSupport.h>
22
23/**
Pawin Vongmasa0277ce92017-03-01 02:25:36 -080024 * Hybrid Interfaces
25 * =================
26 *
27 * A hybrid interface is a binder interface that
28 * 1. is implemented both traditionally and as a wrapper around a hidl
29 * interface, and allows querying whether the underlying instance comes from
30 * a hidl interface or not; and
31 * 2. allows efficient calls to a hidl interface (if the underlying instance
32 * comes from a hidl interface) by automatically creating the wrapper in the
33 * process that calls it.
Pawin Vongmasafbffe922017-03-01 02:25:36 -080034 *
35 * Terminology:
36 * - `HalToken`: The type for a "token" of a hidl interface. This is defined to
37 * be compatible with `ITokenManager.hal`.
38 * - `HInterface`: The base type for a hidl interface. Currently, it is defined
39 * as `::android::hidl::base::V1_0::IBase`.
40 * - `HALINTERFACE`: The hidl interface that will be sent through binders.
41 * - `INTERFACE`: The binder interface that will be the wrapper of
Pawin Vongmasa0277ce92017-03-01 02:25:36 -080042 * `HALINTERFACE`. `INTERFACE` is supposed to be somewhat similar to
43 * `HALINTERFACE`.
Pawin Vongmasafbffe922017-03-01 02:25:36 -080044 *
45 * To demonstrate how this is done, here is an example. Suppose `INTERFACE` is
46 * `IFoo` and `HALINTERFACE` is `HFoo`. The required steps are:
47 * 1. Use DECLARE_HYBRID_META_INTERFACE instead of DECLARE_META_INTERFACE in the
48 * definition of `IFoo`. The usage is
49 * DECLARE_HYBRID_META_INTERFACE(IFoo, HFoo)
50 * inside the body of `IFoo`.
51 * 2. Create a converter class that derives from
52 * `H2BConverter<HFoo, IFoo, BnFoo>`. Let us call this `H2BFoo`.
Pawin Vongmasa0277ce92017-03-01 02:25:36 -080053 * 3. Add the following constructor in `H2BFoo` that call the corresponding
Pawin Vongmasafbffe922017-03-01 02:25:36 -080054 * constructors in `H2BConverter`:
55 * H2BFoo(const sp<HalInterface>& base) : CBase(base) {}
56 * Note: `CBase = H2BConverter<HFoo, IFoo, BnFoo>` and `HalInterface = HFoo`
Pawin Vongmasa0277ce92017-03-01 02:25:36 -080057 * are member typedefs of `H2BConverter<HFoo, IFoo, BnFoo>`, so the above
58 * line can be copied into `H2BFoo`.
59 * 4. Implement `IFoo` in `H2BFoo` on top of `HFoo`. `H2BConverter` provides a
60 * protected `mBase` of type `sp<HFoo>` that can be used to access the `HFoo`
Pawin Vongmasafbffe922017-03-01 02:25:36 -080061 * instance. (There is also a public function named `getHalInterface()` that
62 * returns `mBase`.)
63 * 5. Create a hardware proxy class that derives from
64 * `HpInterface<BpFoo, H2BFoo>`. Name this class `HpFoo`. (This name cannot
65 * deviate. See step 8 below.)
66 * 6. Add the following constructor to `HpFoo`:
67 * HpFoo(const sp<IBinder>& base): PBase(base) {}
68 * Note: `PBase` a member typedef of `HpInterface<BpFoo, H2BFoo>` that is
69 * equal to `HpInterface<BpFoo, H2BFoo>` itself, so the above line can be
70 * copied verbatim into `HpFoo`.
71 * 7. Delegate all functions in `HpFoo` that come from `IFoo` except
Pawin Vongmasa0277ce92017-03-01 02:25:36 -080072 * `getHalInterface` to the protected member `mBase`,
Pawin Vongmasafbffe922017-03-01 02:25:36 -080073 * which is defined in `HpInterface<BpFoo, H2BFoo>` (hence in `HpFoo`) with
74 * type `IFoo`. (There is also a public function named `getBaseInterface()`
75 * that returns `mBase`.)
76 * 8. Replace the existing `IMPLEMENT_META_INTERFACE` for INTERFACE by
77 * `IMPLEMENT_HYBRID_META_INTERFACE`. Note that this macro relies on the
78 * exact naming of `HpFoo`, where `Foo` comes from the interface name `IFoo`.
79 * An example usage is
80 * IMPLEMENT_HYBRID_META_INTERFACE(IFoo, HFoo, "example.interface.foo");
81 *
82 * `GETTOKEN` Template Argument
83 * ============================
84 *
85 * Following the instructions above, `H2BConverter` and `HpInterface` would use
86 * `transact()` to send over tokens, with `code` (the first argument of
87 * `transact()`) equal to a 4-byte value of '_GTK'. If this value clashes with
88 * other values already in use in the `Bp` class, it can be changed by supplying
89 * the last optional template argument to `H2BConverter` and `HpInterface`.
90 *
91 */
92
93namespace android {
94
95typedef uint64_t HalToken;
96typedef ::android::hidl::base::V1_0::IBase HInterface;
97
98sp<HInterface> retrieveHalInterface(const HalToken& token);
99bool createHalToken(const sp<HInterface>& interface, HalToken* token);
100bool deleteHalToken(const HalToken& token);
101
102template <
103 typename HINTERFACE,
104 typename INTERFACE,
105 typename BNINTERFACE,
106 uint32_t GETTOKEN = '_GTK'>
107class H2BConverter : public BNINTERFACE {
108public:
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800109 typedef H2BConverter<HINTERFACE, INTERFACE, BNINTERFACE, GETTOKEN> CBase; // Converter Base
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800110 typedef INTERFACE BaseInterface;
111 typedef HINTERFACE HalInterface;
112 static constexpr uint32_t GET_HAL_TOKEN = GETTOKEN;
113
114 H2BConverter(const sp<HalInterface>& base) : mBase(base) {}
115 virtual status_t onTransact(uint32_t code,
116 const Parcel& data, Parcel* reply, uint32_t flags = 0);
117 sp<HalInterface> getHalInterface() override { return mBase; }
118 HalInterface* getBaseInterface() { return mBase.get(); }
119
120protected:
121 sp<HalInterface> mBase;
122};
123
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800124template <
125 typename BPINTERFACE,
126 typename CONVERTER,
127 uint32_t GETTOKEN = '_GTK'>
128class HpInterface : public CONVERTER::BaseInterface {
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800129public:
130 typedef HpInterface<BPINTERFACE, CONVERTER, GETTOKEN> PBase; // Proxy Base
131 typedef typename CONVERTER::BaseInterface BaseInterface;
132 typedef typename CONVERTER::HalInterface HalInterface;
133 static constexpr uint32_t GET_HAL_TOKEN = GETTOKEN;
134
135 explicit HpInterface(const sp<IBinder>& impl);
136 sp<HalInterface> getHalInterface() override { return mHal; }
137 BaseInterface* getBaseInterface() { return mBase.get(); }
138
139protected:
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800140 sp<IBinder> mImpl;
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800141 sp<BaseInterface> mBase;
142 sp<HalInterface> mHal;
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800143 IBinder* onAsBinder() override { return mImpl.get(); }
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800144};
145
146// ----------------------------------------------------------------------
147
148#define DECLARE_HYBRID_META_INTERFACE(INTERFACE, HAL) \
149 static const ::android::String16 descriptor; \
150 static ::android::sp<I##INTERFACE> asInterface( \
151 const ::android::sp<::android::IBinder>& obj); \
152 virtual const ::android::String16& getInterfaceDescriptor() const; \
153 I##INTERFACE(); \
154 virtual ~I##INTERFACE(); \
155 virtual sp<HAL> getHalInterface(); \
156
157
158#define IMPLEMENT_HYBRID_META_INTERFACE(INTERFACE, HAL, NAME) \
159 const ::android::String16 I##INTERFACE::descriptor(NAME); \
160 const ::android::String16& \
161 I##INTERFACE::getInterfaceDescriptor() const { \
162 return I##INTERFACE::descriptor; \
163 } \
164 ::android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
165 const ::android::sp<::android::IBinder>& obj) \
166 { \
167 ::android::sp<I##INTERFACE> intr; \
168 if (obj != NULL) { \
169 intr = static_cast<I##INTERFACE*>( \
170 obj->queryLocalInterface( \
171 I##INTERFACE::descriptor).get()); \
172 if (intr == NULL) { \
173 intr = new Hp##INTERFACE(obj); \
174 } \
175 } \
176 return intr; \
177 } \
178 I##INTERFACE::I##INTERFACE() { } \
179 I##INTERFACE::~I##INTERFACE() { } \
180 sp<HAL> I##INTERFACE::getHalInterface() { return nullptr; } \
181
182// ----------------------------------------------------------------------
183
184template <
185 typename HINTERFACE,
186 typename INTERFACE,
187 typename BNINTERFACE,
188 uint32_t GETTOKEN>
189status_t H2BConverter<HINTERFACE, INTERFACE, BNINTERFACE, GETTOKEN>::
190 onTransact(
191 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
192 if (code == GET_HAL_TOKEN) {
193 HalToken token;
194 bool result;
195 result = createHalToken(mBase, &token);
196 if (!result) {
197 ALOGE("H2BConverter: Failed to create HAL token.");
198 }
199 reply->writeBool(result);
200 reply->writeUint64(token);
201 return NO_ERROR;
202 }
203 return BNINTERFACE::onTransact(code, data, reply, flags);
204}
205
206template <typename BPINTERFACE, typename CONVERTER, uint32_t GETTOKEN>
207HpInterface<BPINTERFACE, CONVERTER, GETTOKEN>::HpInterface(
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800208 const sp<IBinder>& impl) : mImpl(impl) {
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800209 Parcel data, reply;
210 data.writeInterfaceToken(BaseInterface::getInterfaceDescriptor());
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800211 if (impl->transact(GET_HAL_TOKEN, data, &reply) == NO_ERROR) {
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800212 bool tokenCreated = reply.readBool();
213 HalToken token = reply.readUint64();
214 if (!tokenCreated) {
215 ALOGE("HpInterface: Sender failed to create HAL token.");
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800216 mBase = new BPINTERFACE(impl);
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800217 } else {
218 sp<HInterface> hInterface = retrieveHalInterface(token);
219 deleteHalToken(token);
220 if (hInterface != nullptr) {
221 mHal = static_cast<HalInterface*>(hInterface.get());
222 mBase = new CONVERTER(mHal);
223 } else {
224 ALOGE("HpInterface: Cannot retrieve HAL interface from token.");
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800225 mBase = new BPINTERFACE(impl);
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800226 }
227 }
228 } else {
Pawin Vongmasa0277ce92017-03-01 02:25:36 -0800229 mBase = new BPINTERFACE(impl);
Pawin Vongmasafbffe922017-03-01 02:25:36 -0800230 }
231}
232
233// ----------------------------------------------------------------------
234
235}; // namespace android
236
237#endif // ANDROID_HALTOKEN_H