2 * Copyright 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <binder/IInterface.h>
20 #include <binder/Parcel.h>
21 #include <cutils/compiler.h>
23 // Set to 1 to enable CallStacks when logging errors
24 #define SI_DUMP_CALLSTACKS 0
25 #if SI_DUMP_CALLSTACKS
26 #include <utils/CallStack.h>
29 #include <utils/NativeHandle.h>
32 #include <type_traits>
35 namespace SafeInterface {
37 // ParcelHandler is responsible for writing/reading various types to/from a Parcel in a generic way
40 explicit ParcelHandler(const char* logTag) : mLogTag(logTag) {}
42 // Specializations for types with dedicated handling in Parcel
43 status_t read(const Parcel& parcel, bool* b) const {
44 return callParcel("readBool", [&]() { return parcel.readBool(b); });
46 status_t write(Parcel* parcel, bool b) const {
47 return callParcel("writeBool", [&]() { return parcel->writeBool(b); });
50 typename std::enable_if<std::is_enum<E>::value, status_t>::type read(const Parcel& parcel,
52 typename std::underlying_type<E>::type u{};
53 status_t result = read(parcel, &u);
54 *e = static_cast<E>(u);
58 typename std::enable_if<std::is_enum<E>::value, status_t>::type write(Parcel* parcel,
60 return write(parcel, static_cast<typename std::underlying_type<E>::type>(e));
63 typename std::enable_if<std::is_base_of<Flattenable<T>, T>::value, status_t>::type read(
64 const Parcel& parcel, T* t) const {
65 return callParcel("read(Flattenable)", [&]() { return parcel.read(*t); });
68 typename std::enable_if<std::is_base_of<Flattenable<T>, T>::value, status_t>::type write(
69 Parcel* parcel, const T& t) const {
70 return callParcel("write(Flattenable)", [&]() { return parcel->write(t); });
73 typename std::enable_if<std::is_base_of<Flattenable<T>, T>::value, status_t>::type read(
74 const Parcel& parcel, sp<T>* t) const {
76 return callParcel("read(sp<Flattenable>)", [&]() { return parcel.read(*(t->get())); });
79 typename std::enable_if<std::is_base_of<Flattenable<T>, T>::value, status_t>::type write(
80 Parcel* parcel, const sp<T>& t) const {
81 return callParcel("write(sp<Flattenable>)", [&]() { return parcel->write(*(t.get())); });
84 typename std::enable_if<std::is_base_of<LightFlattenable<T>, T>::value, status_t>::type read(
85 const Parcel& parcel, T* t) const {
86 return callParcel("read(LightFlattenable)", [&]() { return parcel.read(*t); });
89 typename std::enable_if<std::is_base_of<LightFlattenable<T>, T>::value, status_t>::type write(
90 Parcel* parcel, const T& t) const {
91 return callParcel("write(LightFlattenable)", [&]() { return parcel->write(t); });
93 template <typename NH>
94 typename std::enable_if<std::is_same<NH, sp<NativeHandle>>::value, status_t>::type read(
95 const Parcel& parcel, NH* nh) {
96 *nh = NativeHandle::create(parcel.readNativeHandle(), true);
99 template <typename NH>
100 typename std::enable_if<std::is_same<NH, sp<NativeHandle>>::value, status_t>::type write(
101 Parcel* parcel, const NH& nh) {
102 return callParcel("write(sp<NativeHandle>)",
103 [&]() { return parcel->writeNativeHandle(nh->handle()); });
105 template <typename T>
106 typename std::enable_if<std::is_base_of<Parcelable, T>::value, status_t>::type read(
107 const Parcel& parcel, T* t) const {
108 return callParcel("readParcelable", [&]() { return parcel.readParcelable(t); });
110 template <typename T>
111 typename std::enable_if<std::is_base_of<Parcelable, T>::value, status_t>::type write(
112 Parcel* parcel, const T& t) const {
113 return callParcel("writeParcelable", [&]() { return parcel->writeParcelable(t); });
115 status_t read(const Parcel& parcel, String8* str) const {
116 return callParcel("readString8", [&]() { return parcel.readString8(str); });
118 status_t write(Parcel* parcel, const String8& str) const {
119 return callParcel("writeString8", [&]() { return parcel->writeString8(str); });
121 template <typename T>
122 typename std::enable_if<std::is_same<IBinder, T>::value, status_t>::type read(
123 const Parcel& parcel, sp<T>* pointer) const {
124 return callParcel("readNullableStrongBinder",
125 [&]() { return parcel.readNullableStrongBinder(pointer); });
127 template <typename T>
128 typename std::enable_if<std::is_same<IBinder, T>::value, status_t>::type write(
129 Parcel* parcel, const sp<T>& pointer) const {
130 return callParcel("writeStrongBinder",
131 [&]() { return parcel->writeStrongBinder(pointer); });
133 template <typename T>
134 typename std::enable_if<std::is_base_of<IInterface, T>::value, status_t>::type read(
135 const Parcel& parcel, sp<T>* pointer) const {
136 return callParcel("readNullableStrongBinder[IInterface]",
137 [&]() { return parcel.readNullableStrongBinder(pointer); });
139 template <typename T>
140 typename std::enable_if<std::is_base_of<IInterface, T>::value, status_t>::type write(
141 Parcel* parcel, const sp<T>& interface) const {
142 return write(parcel, IInterface::asBinder(interface));
144 template <typename T>
145 typename std::enable_if<std::is_base_of<Parcelable, T>::value, status_t>::type read(
146 const Parcel& parcel, std::vector<T>* v) const {
147 return callParcel("readParcelableVector", [&]() { return parcel.readParcelableVector(v); });
149 template <typename T>
150 typename std::enable_if<std::is_base_of<Parcelable, T>::value, status_t>::type write(
151 Parcel* parcel, const std::vector<T>& v) const {
152 return callParcel("writeParcelableVector",
153 [&]() { return parcel->writeParcelableVector(v); });
156 // Templates to handle integral types. We use a struct template to require that the called
157 // function exactly matches the signedness and size of the argument (e.g., the argument isn't
158 // silently widened).
159 template <bool isSigned, size_t size, typename I>
161 template <typename I>
162 struct HandleInt<true, 4, I> {
163 static status_t read(const ParcelHandler& handler, const Parcel& parcel, I* i) {
164 return handler.callParcel("readInt32", [&]() { return parcel.readInt32(i); });
166 static status_t write(const ParcelHandler& handler, Parcel* parcel, I i) {
167 return handler.callParcel("writeInt32", [&]() { return parcel->writeInt32(i); });
170 template <typename I>
171 struct HandleInt<false, 4, I> {
172 static status_t read(const ParcelHandler& handler, const Parcel& parcel, I* i) {
173 return handler.callParcel("readUint32", [&]() { return parcel.readUint32(i); });
175 static status_t write(const ParcelHandler& handler, Parcel* parcel, I i) {
176 return handler.callParcel("writeUint32", [&]() { return parcel->writeUint32(i); });
179 template <typename I>
180 struct HandleInt<true, 8, I> {
181 static status_t read(const ParcelHandler& handler, const Parcel& parcel, I* i) {
182 return handler.callParcel("readInt64", [&]() { return parcel.readInt64(i); });
184 static status_t write(const ParcelHandler& handler, Parcel* parcel, I i) {
185 return handler.callParcel("writeInt64", [&]() { return parcel->writeInt64(i); });
188 template <typename I>
189 struct HandleInt<false, 8, I> {
190 static status_t read(const ParcelHandler& handler, const Parcel& parcel, I* i) {
191 return handler.callParcel("readUint64", [&]() { return parcel.readUint64(i); });
193 static status_t write(const ParcelHandler& handler, Parcel* parcel, I i) {
194 return handler.callParcel("writeUint64", [&]() { return parcel->writeUint64(i); });
197 template <typename I>
198 typename std::enable_if<std::is_integral<I>::value, status_t>::type read(const Parcel& parcel,
200 return HandleInt<std::is_signed<I>::value, sizeof(I), I>::read(*this, parcel, i);
202 template <typename I>
203 typename std::enable_if<std::is_integral<I>::value, status_t>::type write(Parcel* parcel,
205 return HandleInt<std::is_signed<I>::value, sizeof(I), I>::write(*this, parcel, i);
209 const char* const mLogTag;
211 // Helper to encapsulate error handling while calling the various Parcel methods
212 template <typename Function>
213 status_t callParcel(const char* name, Function f) const {
214 status_t error = f();
215 if (CC_UNLIKELY(error != NO_ERROR)) {
216 ALOG(LOG_ERROR, mLogTag, "Failed to %s, (%d: %s)", name, error, strerror(-error));
217 #if SI_DUMP_CALLSTACKS
218 CallStack callStack(mLogTag);
225 // Utility struct template which allows us to retrieve the types of the parameters of a member
227 template <typename T>
228 struct ParamExtractor;
229 template <typename Class, typename Return, typename... Params>
230 struct ParamExtractor<Return (Class::*)(Params...)> {
231 using ParamTuple = std::tuple<Params...>;
233 template <typename Class, typename Return, typename... Params>
234 struct ParamExtractor<Return (Class::*)(Params...) const> {
235 using ParamTuple = std::tuple<Params...>;
238 } // namespace SafeInterface
240 template <typename Interface>
241 class SafeBpInterface : public BpInterface<Interface> {
243 SafeBpInterface(const sp<IBinder>& impl, const char* logTag)
244 : BpInterface<Interface>(impl), mLogTag(logTag) {}
245 ~SafeBpInterface() override = default;
247 // callRemote is used to invoke a synchronous procedure call over Binder
248 template <typename Method, typename TagType, typename... Args>
249 status_t callRemote(TagType tag, Args&&... args) const {
250 static_assert(sizeof(TagType) <= sizeof(uint32_t), "Tag must fit inside uint32_t");
252 // Verify that the arguments are compatible with the parameters
253 using ParamTuple = typename SafeInterface::ParamExtractor<Method>::ParamTuple;
254 static_assert(ArgsMatchParams<std::tuple<Args...>, ParamTuple>::value,
255 "Invalid argument type");
257 // Write the input arguments to the data Parcel
259 data.writeInterfaceToken(this->getInterfaceDescriptor());
261 status_t error = writeInputs(&data, std::forward<Args>(args)...);
262 if (CC_UNLIKELY(error != NO_ERROR)) {
263 // A message will have been logged by writeInputs
267 // Send the data Parcel to the remote and retrieve the reply parcel
269 error = this->remote()->transact(static_cast<uint32_t>(tag), data, &reply);
270 if (CC_UNLIKELY(error != NO_ERROR)) {
271 ALOG(LOG_ERROR, mLogTag, "Failed to transact (%d)", error);
272 #if SI_DUMP_CALLSTACKS
273 CallStack callStack(mLogTag);
278 // Read the outputs from the reply Parcel into the output arguments
279 error = readOutputs(reply, std::forward<Args>(args)...);
280 if (CC_UNLIKELY(error != NO_ERROR)) {
281 // A message will have been logged by readOutputs
285 // Retrieve the result code from the reply Parcel
286 status_t result = NO_ERROR;
287 error = reply.readInt32(&result);
288 if (CC_UNLIKELY(error != NO_ERROR)) {
289 ALOG(LOG_ERROR, mLogTag, "Failed to obtain result");
290 #if SI_DUMP_CALLSTACKS
291 CallStack callStack(mLogTag);
298 // callRemoteAsync is used to invoke an asynchronous procedure call over Binder
299 template <typename Method, typename TagType, typename... Args>
300 void callRemoteAsync(TagType tag, Args&&... args) const {
301 static_assert(sizeof(TagType) <= sizeof(uint32_t), "Tag must fit inside uint32_t");
303 // Verify that the arguments are compatible with the parameters
304 using ParamTuple = typename SafeInterface::ParamExtractor<Method>::ParamTuple;
305 static_assert(ArgsMatchParams<std::tuple<Args...>, ParamTuple>::value,
306 "Invalid argument type");
308 // Write the input arguments to the data Parcel
310 data.writeInterfaceToken(this->getInterfaceDescriptor());
311 status_t error = writeInputs(&data, std::forward<Args>(args)...);
312 if (CC_UNLIKELY(error != NO_ERROR)) {
313 // A message will have been logged by writeInputs
317 // There will be no data in the reply Parcel since the call is one-way
319 error = this->remote()->transact(static_cast<uint32_t>(tag), data, &reply,
320 IBinder::FLAG_ONEWAY);
321 if (CC_UNLIKELY(error != NO_ERROR)) {
322 ALOG(LOG_ERROR, mLogTag, "Failed to transact (%d)", error);
323 #if SI_DUMP_CALLSTACKS
324 CallStack callStack(mLogTag);
330 const char* const mLogTag;
332 // This struct provides information on whether the decayed types of the elements at Index in the
333 // tuple types T and U (that is, the types after stripping cv-qualifiers, removing references,
334 // and a few other less common operations) are the same
335 template <size_t Index, typename T, typename U>
336 struct DecayedElementsMatch {
338 using FirstT = typename std::tuple_element<Index, T>::type;
339 using DecayedT = typename std::decay<FirstT>::type;
340 using FirstU = typename std::tuple_element<Index, U>::type;
341 using DecayedU = typename std::decay<FirstU>::type;
344 static constexpr bool value = std::is_same<DecayedT, DecayedU>::value;
347 // When comparing whether the argument types match the parameter types, we first decay them (see
348 // DecayedElementsMatch) to avoid falsely flagging, say, T&& against T even though they are
349 // equivalent enough for our purposes
350 template <typename T, typename U>
351 struct ArgsMatchParams {};
352 template <typename... Args, typename... Params>
353 struct ArgsMatchParams<std::tuple<Args...>, std::tuple<Params...>> {
354 static_assert(sizeof...(Args) <= sizeof...(Params), "Too many arguments");
355 static_assert(sizeof...(Args) >= sizeof...(Params), "Not enough arguments");
358 template <size_t Index>
359 static constexpr typename std::enable_if<(Index < sizeof...(Args)), bool>::type
361 if (!DecayedElementsMatch<Index, std::tuple<Args...>, std::tuple<Params...>>::value) {
364 return elementsMatch<Index + 1>();
366 template <size_t Index>
367 static constexpr typename std::enable_if<(Index >= sizeof...(Args)), bool>::type
373 static constexpr bool value = elementsMatch<0>();
376 // Since we assume that pointer arguments are outputs, we can use this template struct to
377 // determine whether or not a given argument is fundamentally a pointer type and thus an output
378 template <typename T>
379 struct IsPointerIfDecayed {
381 using Decayed = typename std::decay<T>::type;
384 static constexpr bool value = std::is_pointer<Decayed>::value;
387 template <typename T>
388 typename std::enable_if<!IsPointerIfDecayed<T>::value, status_t>::type writeIfInput(
389 Parcel* data, T&& t) const {
390 return SafeInterface::ParcelHandler{mLogTag}.write(data, std::forward<T>(t));
392 template <typename T>
393 typename std::enable_if<IsPointerIfDecayed<T>::value, status_t>::type writeIfInput(
394 Parcel* /*data*/, T&& /*t*/) const {
398 // This method iterates through all of the arguments, writing them to the data Parcel if they
399 // are an input (i.e., if they are not a pointer type)
400 template <typename T, typename... Remaining>
401 status_t writeInputs(Parcel* data, T&& t, Remaining&&... remaining) const {
402 status_t error = writeIfInput(data, std::forward<T>(t));
403 if (CC_UNLIKELY(error != NO_ERROR)) {
404 // A message will have been logged by writeIfInput
407 return writeInputs(data, std::forward<Remaining>(remaining)...);
409 static status_t writeInputs(Parcel* /*data*/) { return NO_ERROR; }
411 template <typename T>
412 typename std::enable_if<IsPointerIfDecayed<T>::value, status_t>::type readIfOutput(
413 const Parcel& reply, T&& t) const {
414 return SafeInterface::ParcelHandler{mLogTag}.read(reply, std::forward<T>(t));
416 template <typename T>
417 static typename std::enable_if<!IsPointerIfDecayed<T>::value, status_t>::type readIfOutput(
418 const Parcel& /*reply*/, T&& /*t*/) {
422 // Similar to writeInputs except that it reads output arguments from the reply Parcel
423 template <typename T, typename... Remaining>
424 status_t readOutputs(const Parcel& reply, T&& t, Remaining&&... remaining) const {
425 status_t error = readIfOutput(reply, std::forward<T>(t));
426 if (CC_UNLIKELY(error != NO_ERROR)) {
427 // A message will have been logged by readIfOutput
430 return readOutputs(reply, std::forward<Remaining>(remaining)...);
432 static status_t readOutputs(const Parcel& /*data*/) { return NO_ERROR; }
435 template <typename Interface>
436 class SafeBnInterface : public BnInterface<Interface> {
438 explicit SafeBnInterface(const char* logTag) : mLogTag(logTag) {}
441 template <typename Method>
442 status_t callLocal(const Parcel& data, Parcel* reply, Method method) {
443 CHECK_INTERFACE(this, data, reply);
445 // Since we need to both pass inputs into the call as well as retrieve outputs, we create a
446 // "raw" tuple, where the inputs are interleaved with actual, non-pointer versions of the
447 // outputs. When we ultimately call into the method, we will pass the addresses of the
448 // output arguments instead of their tuple members directly, but the storage will live in
450 using ParamTuple = typename SafeInterface::ParamExtractor<Method>::ParamTuple;
451 typename RawConverter<std::tuple<>, ParamTuple>::type rawArgs{};
453 // Read the inputs from the data Parcel into the argument tuple
454 status_t error = InputReader<ParamTuple>{mLogTag}.readInputs(data, &rawArgs);
455 if (CC_UNLIKELY(error != NO_ERROR)) {
456 // A message will have been logged by read
460 // Call the local method
461 status_t result = MethodCaller<ParamTuple>::call(this, method, &rawArgs);
463 // Extract the outputs from the argument tuple and write them into the reply Parcel
464 error = OutputWriter<ParamTuple>{mLogTag}.writeOutputs(reply, &rawArgs);
465 if (CC_UNLIKELY(error != NO_ERROR)) {
466 // A message will have been logged by write
470 // Return the result code in the reply Parcel
471 error = reply->writeInt32(result);
472 if (CC_UNLIKELY(error != NO_ERROR)) {
473 ALOG(LOG_ERROR, mLogTag, "Failed to write result");
474 #if SI_DUMP_CALLSTACKS
475 CallStack callStack(mLogTag);
482 template <typename Method>
483 status_t callLocalAsync(const Parcel& data, Parcel* /*reply*/, Method method) {
484 // reply is not actually used by CHECK_INTERFACE
485 CHECK_INTERFACE(this, data, reply);
487 // Since we need to both pass inputs into the call as well as retrieve outputs, we create a
488 // "raw" tuple, where the inputs are interleaved with actual, non-pointer versions of the
489 // outputs. When we ultimately call into the method, we will pass the addresses of the
490 // output arguments instead of their tuple members directly, but the storage will live in
492 using ParamTuple = typename SafeInterface::ParamExtractor<Method>::ParamTuple;
493 typename RawConverter<std::tuple<>, ParamTuple>::type rawArgs{};
495 // Read the inputs from the data Parcel into the argument tuple
496 status_t error = InputReader<ParamTuple>{mLogTag}.readInputs(data, &rawArgs);
497 if (CC_UNLIKELY(error != NO_ERROR)) {
498 // A message will have been logged by read
502 // Call the local method
503 MethodCaller<ParamTuple>::callVoid(this, method, &rawArgs);
505 // After calling, there is nothing more to do since asynchronous calls do not return a value
511 const char* const mLogTag;
513 // RemoveFirst strips the first element from a tuple.
514 // For example, given T = std::tuple<A, B, C>, RemoveFirst<T>::type = std::tuple<B, C>
515 template <typename T, typename... Args>
517 template <typename T, typename... Args>
518 struct RemoveFirst<std::tuple<T, Args...>> {
519 using type = std::tuple<Args...>;
522 // RawConverter strips a tuple down to its fundamental types, discarding both pointers and
523 // references. This allows us to allocate storage for both input (non-pointer) arguments and
524 // output (pointer) arguments in one tuple.
525 // For example, given T = std::tuple<const A&, B*>, RawConverter<T>::type = std::tuple<A, B>
526 template <typename Unconverted, typename... Converted>
528 template <typename Unconverted, typename... Converted>
529 struct RawConverter<std::tuple<Converted...>, Unconverted> {
531 using ElementType = typename std::tuple_element<0, Unconverted>::type;
532 using Decayed = typename std::decay<ElementType>::type;
533 using WithoutPointer = typename std::remove_pointer<Decayed>::type;
536 using type = typename RawConverter<std::tuple<Converted..., WithoutPointer>,
537 typename RemoveFirst<Unconverted>::type>::type;
539 template <typename... Converted>
540 struct RawConverter<std::tuple<Converted...>, std::tuple<>> {
541 using type = std::tuple<Converted...>;
544 // This provides a simple way to determine whether the indexed element of Args... is a pointer
545 template <size_t I, typename... Args>
546 struct ElementIsPointer {
548 using ElementType = typename std::tuple_element<I, std::tuple<Args...>>::type;
551 static constexpr bool value = std::is_pointer<ElementType>::value;
554 // This class iterates over the parameter types, and if a given parameter is an input
555 // (i.e., is not a pointer), reads the corresponding argument tuple element from the data Parcel
556 template <typename... Params>
558 template <typename... Params>
559 class InputReader<std::tuple<Params...>> {
561 explicit InputReader(const char* logTag) : mLogTag(logTag) {}
563 // Note that in this case (as opposed to in SafeBpInterface), we iterate using an explicit
564 // index (starting with 0 here) instead of using recursion and stripping the first element.
565 // This is because in SafeBpInterface we aren't actually operating on a real tuple, but are
566 // instead just using a tuple as a convenient container for variadic types, whereas here we
567 // can't modify the argument tuple without causing unnecessary copies or moves of the data
568 // contained therein.
569 template <typename RawTuple>
570 status_t readInputs(const Parcel& data, RawTuple* args) {
571 return dispatchArg<0>(data, args);
575 const char* const mLogTag;
577 template <std::size_t I, typename RawTuple>
578 typename std::enable_if<!ElementIsPointer<I, Params...>::value, status_t>::type readIfInput(
579 const Parcel& data, RawTuple* args) {
580 return SafeInterface::ParcelHandler{mLogTag}.read(data, &std::get<I>(*args));
582 template <std::size_t I, typename RawTuple>
583 typename std::enable_if<ElementIsPointer<I, Params...>::value, status_t>::type readIfInput(
584 const Parcel& /*data*/, RawTuple* /*args*/) {
588 // Recursively iterate through the arguments
589 template <std::size_t I, typename RawTuple>
590 typename std::enable_if<(I < sizeof...(Params)), status_t>::type dispatchArg(
591 const Parcel& data, RawTuple* args) {
592 status_t error = readIfInput<I>(data, args);
593 if (CC_UNLIKELY(error != NO_ERROR)) {
594 // A message will have been logged in read
597 return dispatchArg<I + 1>(data, args);
599 template <std::size_t I, typename RawTuple>
600 typename std::enable_if<(I >= sizeof...(Params)), status_t>::type dispatchArg(
601 const Parcel& /*data*/, RawTuple* /*args*/) {
606 // getForCall uses the types of the parameters to determine whether a given element of the
607 // argument tuple is an input, which should be passed directly into the call, or an output, for
608 // which its address should be passed into the call
609 template <size_t I, typename RawTuple, typename... Params>
610 static typename std::enable_if<
611 ElementIsPointer<I, Params...>::value,
612 typename std::tuple_element<I, std::tuple<Params...>>::type>::type
613 getForCall(RawTuple* args) {
614 return &std::get<I>(*args);
616 template <size_t I, typename RawTuple, typename... Params>
617 static typename std::enable_if<
618 !ElementIsPointer<I, Params...>::value,
619 typename std::tuple_element<I, std::tuple<Params...>>::type>::type&
620 getForCall(RawTuple* args) {
621 return std::get<I>(*args);
624 // This template class uses std::index_sequence and parameter pack expansion to call the given
625 // method using the elements of the argument tuple (after those arguments are passed through
626 // getForCall to get addresses instead of values for output arguments)
627 template <typename... Params>
629 template <typename... Params>
630 struct MethodCaller<std::tuple<Params...>> {
632 // The calls through these to the helper methods are necessary to generate the
633 // std::index_sequences used to unpack the argument tuple into the method call
634 template <typename Class, typename MemberFunction, typename RawTuple>
635 static status_t call(Class* instance, MemberFunction function, RawTuple* args) {
636 return callHelper(instance, function, args, std::index_sequence_for<Params...>{});
638 template <typename Class, typename MemberFunction, typename RawTuple>
639 static void callVoid(Class* instance, MemberFunction function, RawTuple* args) {
640 callVoidHelper(instance, function, args, std::index_sequence_for<Params...>{});
644 template <typename Class, typename MemberFunction, typename RawTuple, std::size_t... I>
645 static status_t callHelper(Class* instance, MemberFunction function, RawTuple* args,
646 std::index_sequence<I...> /*unused*/) {
647 return (instance->*function)(getForCall<I, RawTuple, Params...>(args)...);
649 template <typename Class, typename MemberFunction, typename RawTuple, std::size_t... I>
650 static void callVoidHelper(Class* instance, MemberFunction function, RawTuple* args,
651 std::index_sequence<I...> /*unused*/) {
652 (instance->*function)(getForCall<I, RawTuple, Params...>(args)...);
656 // This class iterates over the parameter types, and if a given parameter is an output
657 // (i.e., is a pointer), writes the corresponding argument tuple element into the reply Parcel
658 template <typename... Params>
660 template <typename... Params>
661 struct OutputWriter<std::tuple<Params...>> {
663 explicit OutputWriter(const char* logTag) : mLogTag(logTag) {}
665 // See the note on InputReader::readInputs for why this differs from the arguably simpler
666 // RemoveFirst approach in SafeBpInterface
667 template <typename RawTuple>
668 status_t writeOutputs(Parcel* reply, RawTuple* args) {
669 return dispatchArg<0>(reply, args);
673 const char* const mLogTag;
675 template <std::size_t I, typename RawTuple>
676 typename std::enable_if<ElementIsPointer<I, Params...>::value, status_t>::type
677 writeIfOutput(Parcel* reply, RawTuple* args) {
678 return SafeInterface::ParcelHandler{mLogTag}.write(reply, std::get<I>(*args));
680 template <std::size_t I, typename RawTuple>
681 typename std::enable_if<!ElementIsPointer<I, Params...>::value, status_t>::type
682 writeIfOutput(Parcel* /*reply*/, RawTuple* /*args*/) {
686 // Recursively iterate through the arguments
687 template <std::size_t I, typename RawTuple>
688 typename std::enable_if<(I < sizeof...(Params)), status_t>::type dispatchArg(
689 Parcel* reply, RawTuple* args) {
690 status_t error = writeIfOutput<I>(reply, args);
691 if (CC_UNLIKELY(error != NO_ERROR)) {
692 // A message will have been logged in read
695 return dispatchArg<I + 1>(reply, args);
697 template <std::size_t I, typename RawTuple>
698 typename std::enable_if<(I >= sizeof...(Params)), status_t>::type dispatchArg(
699 Parcel* /*reply*/, RawTuple* /*args*/) {
705 } // namespace android