blob: 1851870c0844cd354cf6e73e3261d19fd77342e6 [file] [log] [blame]
Yifan Hong7f97f442016-11-14 18:31:05 -08001/*
2 * Copyright (C) 2016 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_HIDL_BINDER_SUPPORT_H
18#define ANDROID_HIDL_BINDER_SUPPORT_H
19
20#include <hidl/HidlSupport.h>
21#include <hidl/MQDescriptor.h>
22#include <hwbinder/IBinder.h>
23#include <hwbinder/Parcel.h>
24
25// Defines functions for hidl_string, hidl_version, Status, hidl_vec, MQDescriptor,
26// etc. to interact with Parcel.
27
28namespace android {
29namespace hardware {
30
Martijn Coenen30791002016-12-01 15:40:46 +010031// ---------------------- hidl_memory
32
33status_t readEmbeddedFromParcel(hidl_memory *memory,
34 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
35
36status_t writeEmbeddedToParcel(const hidl_memory &memory,
37 Parcel *parcel, size_t parentHandle, size_t parentOffset);
38
Yifan Hong7f97f442016-11-14 18:31:05 -080039// ---------------------- hidl_string
40
41status_t readEmbeddedFromParcel(hidl_string *string,
42 const Parcel &parcel, size_t parentHandle, size_t parentOffset);
43
44status_t writeEmbeddedToParcel(const hidl_string &string,
45 Parcel *parcel, size_t parentHandle, size_t parentOffset);
46
47// ---------------------- hidl_version
48
49status_t writeToParcel(const hidl_version &version, android::hardware::Parcel& parcel);
50
51// Caller is responsible for freeing the returned object.
52hidl_version* readFromParcel(const android::hardware::Parcel& parcel);
53
54// ---------------------- Status
55
56// Bear in mind that if the client or service is a Java endpoint, this
57// is not the logic which will provide/interpret the data here.
58status_t readFromParcel(Status *status, const Parcel& parcel);
59status_t writeToParcel(const Status &status, Parcel* parcel);
60
61// ---------------------- hidl_vec
62
63template<typename T>
64status_t readEmbeddedFromParcel(
65 hidl_vec<T> * /*vec*/,
66 const Parcel &parcel,
67 size_t parentHandle,
68 size_t parentOffset,
69 size_t *handle) {
70 const void *ptr = parcel.readEmbeddedBuffer(
71 handle,
72 parentHandle,
73 parentOffset + hidl_vec<T>::kOffsetOfBuffer);
74
75 return ptr != NULL ? OK : UNKNOWN_ERROR;
76}
77
78template<typename T>
79status_t writeEmbeddedToParcel(
80 const hidl_vec<T> &vec,
81 Parcel *parcel,
82 size_t parentHandle,
83 size_t parentOffset,
84 size_t *handle) {
85 return parcel->writeEmbeddedBuffer(
86 vec.data(),
87 sizeof(T) * vec.size(),
88 handle,
89 parentHandle,
90 parentOffset + hidl_vec<T>::kOffsetOfBuffer);
91}
92
93template<typename T>
94status_t findInParcel(const hidl_vec<T> &vec, const Parcel &parcel, size_t *handle) {
95 return parcel.quickFindBuffer(vec.data(), handle);
96}
97
98// ---------------------- MQDescriptor
99
100template<MQFlavor flavor>
101::android::status_t readEmbeddedFromParcel(
102 MQDescriptor<flavor> *obj,
103 const ::android::hardware::Parcel &parcel,
104 size_t parentHandle,
105 size_t parentOffset) {
106 ::android::status_t _hidl_err = ::android::OK;
107
108 size_t _hidl_grantors_child;
109
110 _hidl_err = ::android::hardware::readEmbeddedFromParcel(
111 &obj->grantors(),
112 parcel,
113 parentHandle,
114 parentOffset + MQDescriptor<flavor>::kOffsetOfGrantors,
115 &_hidl_grantors_child);
116
117 if (_hidl_err != ::android::OK) { return _hidl_err; }
118
119 const native_handle_t *_hidl_mq_handle_ptr = parcel.readEmbeddedNativeHandle(
120 parentHandle,
121 parentOffset + MQDescriptor<flavor>::kOffsetOfHandle);
122
123 if (_hidl_mq_handle_ptr == nullptr) {
124 _hidl_err = ::android::UNKNOWN_ERROR;
125 return _hidl_err;
126 }
127
128 return _hidl_err;
129}
130
131template<MQFlavor flavor>
132::android::status_t writeEmbeddedToParcel(
133 const MQDescriptor<flavor> &obj,
134 ::android::hardware::Parcel *parcel,
135 size_t parentHandle,
136 size_t parentOffset) {
137 ::android::status_t _hidl_err = ::android::OK;
138
139 size_t _hidl_grantors_child;
140
141 _hidl_err = ::android::hardware::writeEmbeddedToParcel(
142 obj.grantors(),
143 parcel,
144 parentHandle,
145 parentOffset + MQDescriptor<flavor>::kOffsetOfGrantors,
146 &_hidl_grantors_child);
147
148 if (_hidl_err != ::android::OK) { return _hidl_err; }
149
150 _hidl_err = parcel->writeEmbeddedNativeHandle(
151 obj.handle(),
152 parentHandle,
153 parentOffset + MQDescriptor<flavor>::kOffsetOfHandle);
154
155 if (_hidl_err != ::android::OK) { return _hidl_err; }
156
157 return _hidl_err;
158}
159
160// ---------------------- pointers for HIDL
161
162template <typename T>
163static status_t readEmbeddedReferenceFromParcel(
164 T const* * /* bufptr */,
165 const Parcel & parcel,
166 size_t parentHandle,
167 size_t parentOffset,
168 size_t *handle,
169 bool *shouldResolveRefInBuffer
170 ) {
171 // *bufptr is ignored because, if I am embedded in some
172 // other buffer, the kernel should have fixed me up already.
173 bool isPreviouslyWritten;
174 status_t result = parcel.readEmbeddedReference(
175 nullptr, // ignored, not written to bufptr.
176 handle,
177 parentHandle,
178 parentOffset,
179 &isPreviouslyWritten);
180 // tell caller to run T::readEmbeddedToParcel and
181 // T::readEmbeddedReferenceToParcel if necessary.
182 // It is not called here because we don't know if these two are valid methods.
183 *shouldResolveRefInBuffer = !isPreviouslyWritten;
184 return result;
185}
186
187template <typename T>
188static status_t writeEmbeddedReferenceToParcel(
189 T const* buf,
190 Parcel *parcel, size_t parentHandle, size_t parentOffset,
191 size_t *handle,
192 bool *shouldResolveRefInBuffer
193 ) {
194
195 if(buf == nullptr) {
196 *shouldResolveRefInBuffer = false;
197 return parcel->writeEmbeddedNullReference(handle, parentHandle, parentOffset);
198 }
199
200 // find whether the buffer exists
201 size_t childHandle, childOffset;
202 status_t result;
203 bool found;
204
205 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
206
207 // tell caller to run T::writeEmbeddedToParcel and
208 // T::writeEmbeddedReferenceToParcel if necessary.
209 // It is not called here because we don't know if these two are valid methods.
210 *shouldResolveRefInBuffer = !found;
211
212 if(result != OK) {
213 return result; // bad pointers and length given
214 }
215 if(!found) { // did not find it.
216 return parcel->writeEmbeddedBuffer(buf, sizeof(T), handle,
217 parentHandle, parentOffset);
218 }
219 // found the buffer. easy case.
220 return parcel->writeEmbeddedReference(
221 handle,
222 childHandle,
223 childOffset,
224 parentHandle,
225 parentOffset);
226}
227
228template <typename T>
229static status_t readReferenceFromParcel(
230 T const* *bufptr,
231 const Parcel & parcel,
232 size_t *handle,
233 bool *shouldResolveRefInBuffer
234 ) {
235 bool isPreviouslyWritten;
236 status_t result = parcel.readReference(reinterpret_cast<void const* *>(bufptr),
237 handle, &isPreviouslyWritten);
238 // tell caller to run T::readEmbeddedToParcel and
239 // T::readEmbeddedReferenceToParcel if necessary.
240 // It is not called here because we don't know if these two are valid methods.
241 *shouldResolveRefInBuffer = !isPreviouslyWritten;
242 return result;
243}
244
245template <typename T>
246static status_t writeReferenceToParcel(
247 T const *buf,
248 Parcel * parcel,
249 size_t *handle,
250 bool *shouldResolveRefInBuffer
251 ) {
252
253 if(buf == nullptr) {
254 *shouldResolveRefInBuffer = false;
255 return parcel->writeNullReference(handle);
256 }
257
258 // find whether the buffer exists
259 size_t childHandle, childOffset;
260 status_t result;
261 bool found;
262
263 result = parcel->findBuffer(buf, sizeof(T), &found, &childHandle, &childOffset);
264
265 // tell caller to run T::writeEmbeddedToParcel and
266 // T::writeEmbeddedReferenceToParcel if necessary.
267 // It is not called here because we don't know if these two are valid methods.
268 *shouldResolveRefInBuffer = !found;
269
270 if(result != OK) {
271 return result; // bad pointers and length given
272 }
273 if(!found) { // did not find it.
274 return parcel->writeBuffer(buf, sizeof(T), handle);
275 }
276 // found the buffer. easy case.
277 return parcel->writeReference(handle,
278 childHandle, childOffset);
279}
280
281// ---------------------- support for casting interfaces
282
283extern std::map<std::string, std::function<sp<IBinder>(void*)>> gBnConstructorMap;
284
285// Construct a smallest possible binder from the given interface.
286// If it is remote, then its remote() will be retrieved.
287// Otherwise, the smallest possible BnChild is found where IChild is a subclass of IType
288// and iface is of class IChild. BnChild will be used to wrapped the given iface.
289// Return nullptr if iface is null or any failure.
290template <typename IType, typename IHwType>
291sp<IBinder> toBinder(sp<IType> iface) {
292 IType *ifacePtr = iface.get();
293 if (ifacePtr == nullptr) {
294 return nullptr;
295 }
296 if (ifacePtr->isRemote()) {
297 return ::android::hardware::IInterface::asBinder(static_cast<IHwType *>(ifacePtr));
298 } else {
299 std::string myDescriptor{};
300 ifacePtr->interfaceChain([&](const hidl_vec<hidl_string> &types) {
301 if (types.size() > 0) {
302 myDescriptor = types[0].c_str();
303 }
304 });
305 if (myDescriptor.empty()) {
306 // interfaceChain fails || types.size() == 0
307 return nullptr;
308 }
309 auto iter = gBnConstructorMap.find(myDescriptor);
310 if (iter == gBnConstructorMap.end()) {
311 return nullptr;
312 }
313 return sp<IBinder>((iter->second)(reinterpret_cast<void *>(ifacePtr)));
314 }
315}
316
317#define IMPLEMENT_SERVICE_MANAGER_INTERACTIONS(INTERFACE, PACKAGE) \
318 ::android::sp<I##INTERFACE> I##INTERFACE::getService( \
319 const std::string &serviceName, bool getStub) \
320 { \
321 using ::android::sp; \
322 using ::android::hardware::defaultServiceManager; \
Yifan Hong7f97f442016-11-14 18:31:05 -0800323 using ::android::hidl::manager::V1_0::IServiceManager; \
324 sp<I##INTERFACE> iface; \
325 const sp<IServiceManager> sm = defaultServiceManager(); \
326 if (sm != nullptr && !getStub) { \
Yifan Hong2cd440c2016-11-23 11:30:36 -0800327 sp<::android::hidl::base::V1_0::IBase> base; \
Yifan Hong7f97f442016-11-14 18:31:05 -0800328 ::android::hardware::Return<void> ret = \
329 sm->get(PACKAGE "::I" #INTERFACE, serviceName.c_str(), \
Yifan Hong2cd440c2016-11-23 11:30:36 -0800330 [&base](sp<::android::hidl::base::V1_0::IBase> found) { \
331 base = found; \
Yifan Hong7f97f442016-11-14 18:31:05 -0800332 }); \
333 if (ret.getStatus().isOk()) { \
Yifan Hong2cd440c2016-11-23 11:30:36 -0800334 iface = I##INTERFACE::castFrom(base); \
Yifan Hong7f97f442016-11-14 18:31:05 -0800335 if (iface != nullptr) { \
336 return iface; \
337 } \
338 } \
339 } \
340 int dlMode = RTLD_LAZY; \
341 void *handle = dlopen(HAL_LIBRARY_PATH_ODM PACKAGE "-impl.so", dlMode); \
342 if (handle == nullptr) { \
343 handle = dlopen(HAL_LIBRARY_PATH_VENDOR PACKAGE "-impl.so", dlMode); \
344 } \
345 if (handle == nullptr) { \
346 handle = dlopen(HAL_LIBRARY_PATH_SYSTEM PACKAGE "-impl.so", dlMode); \
347 } \
348 if (handle == nullptr) { \
349 return iface; \
350 } \
351 I##INTERFACE* (*generator)(const char* name); \
352 *(void **)(&generator) = dlsym(handle, "HIDL_FETCH_I"#INTERFACE); \
353 if (generator) { \
354 iface = (*generator)(serviceName.c_str()); \
355 if (iface != nullptr) { \
356 iface = new Bs##INTERFACE(iface); \
357 } \
358 } \
359 return iface; \
360 } \
361 ::android::status_t I##INTERFACE::registerAsService( \
362 const std::string &serviceName) \
363 { \
364 using ::android::sp; \
365 using ::android::hardware::defaultServiceManager; \
366 using ::android::hidl::manager::V1_0::IServiceManager; \
367 sp<Bn##INTERFACE> binderIface = new Bn##INTERFACE(this); \
368 const sp<IServiceManager> sm = defaultServiceManager(); \
369 bool success = false; \
370 ::android::hardware::Return<void> ret = \
371 this->interfaceChain( \
372 [&success, &sm, &serviceName, &binderIface](const auto &chain) { \
Steven Moreland337c3ae2016-11-22 13:37:32 -0800373 ::android::hardware::Return<bool> addRet = \
374 sm->add(chain, serviceName.c_str(), binderIface); \
Steven Moreland1cfaee72016-12-01 08:34:19 -0800375 success = addRet.isOk() && addRet; \
Yifan Hong7f97f442016-11-14 18:31:05 -0800376 }); \
377 success = success && ret.getStatus().isOk(); \
378 return success ? ::android::OK : ::android::UNKNOWN_ERROR; \
379 } \
380 bool I##INTERFACE::registerForNotifications( \
381 const std::string &serviceName, \
382 const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> \
383 &notification) \
384 { \
385 using ::android::sp; \
386 using ::android::hardware::defaultServiceManager; \
387 using ::android::hidl::manager::V1_0::IServiceManager; \
388 const sp<IServiceManager> sm = defaultServiceManager(); \
389 if (sm == nullptr) { \
390 return false; \
391 } \
Steven Moreland337c3ae2016-11-22 13:37:32 -0800392 ::android::hardware::Return<bool> success = \
393 sm->registerForNotifications(PACKAGE "::I" #INTERFACE, \
394 serviceName, \
395 notification); \
Steven Moreland1cfaee72016-12-01 08:34:19 -0800396 return success.isOk() && success; \
Yifan Hong7f97f442016-11-14 18:31:05 -0800397 }
398
399
400} // namespace hardware
401} // namespace android
402
403
404#endif // ANDROID_HIDL_BINDER_SUPPORT_H