blob: f3fb9c37d85897014df7bfe88c1ed9cbbf79ef49 [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
2 * Copyright (C) 2018 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#include <android/binder_ibinder.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070018#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070019
20#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070021#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070022#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070023
24#include <android-base/logging.h>
25
Steven Moreland901c7452018-09-04 16:17:28 -070026using DeathRecipient = ::android::IBinder::DeathRecipient;
27
Steven Moreland2e87adc2018-08-20 19:47:00 -070028using ::android::IBinder;
29using ::android::Parcel;
30using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070031using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070032using ::android::String16;
33using ::android::wp;
34
Steven Moreland71cddc32018-08-30 23:39:22 -070035namespace ABBinderTag {
36
37static const void* kId = "ABBinder";
38static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070039void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070040
41static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070042 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070043}
44static bool has(const sp<IBinder>& binder) {
45 return binder != nullptr && binder->findObject(kId) == kValue;
46}
47
Steven Moreland6cf66ac2018-11-02 18:14:54 -070048} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070049
Steven Moreland94968952018-09-05 14:42:59 -070050namespace ABpBinderTag {
51
52static std::mutex gLock;
53static const void* kId = "ABpBinder";
54struct Value {
55 wp<ABpBinder> binder;
56};
57void clean(const void* id, void* obj, void* cookie) {
58 CHECK(id == kId) << id << " " << obj << " " << cookie;
59
60 delete static_cast<Value*>(obj);
61};
62
Steven Moreland6cf66ac2018-11-02 18:14:54 -070063} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070064
Steven Moreland2e87adc2018-08-20 19:47:00 -070065AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
66AIBinder::~AIBinder() {}
67
Steven Moreland71cddc32018-08-30 23:39:22 -070068bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070069 using ::android::String8;
70
Steven Moreland71cddc32018-08-30 23:39:22 -070071 if (clazz == nullptr) return false;
72 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070073
74 String8 newDescriptor(clazz->getInterfaceDescriptor());
75
76 if (mClazz != nullptr) {
77 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
78 if (newDescriptor == currentDescriptor) {
79 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
80 << "' match during associateClass, but they are different class objects. "
81 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070082 } else {
83 LOG(ERROR) << __func__
84 << ": Class cannot be associated on object which already has a class. "
85 "Trying to associate to '"
86 << newDescriptor.c_str() << "' but already set to '"
87 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070088 }
89
Steven Moreland71cddc32018-08-30 23:39:22 -070090 // always a failure because we know mClazz != clazz
91 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland6cf66ac2018-11-02 18:14:54 -070094 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -070095
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 String8 descriptor(getBinder()->getInterfaceDescriptor());
97 if (descriptor != newDescriptor) {
98 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
99 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700100 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700101 }
102
Steven Moreland71cddc32018-08-30 23:39:22 -0700103 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700104 mClazz = clazz;
105
Steven Moreland71cddc32018-08-30 23:39:22 -0700106 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700107}
108
109ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700110 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700111 CHECK(clazz != nullptr);
112}
113ABBinder::~ABBinder() {
114 getClass()->onDestroy(mUserData);
115}
116
117const String16& ABBinder::getInterfaceDescriptor() const {
118 return getClass()->getInterfaceDescriptor();
119}
120
Steven Moreland5d62e442018-09-13 15:01:02 -0700121status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
122 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700123 if (isUserCommand(code)) {
124 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700125 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700126 }
127
128 const AParcel in = AParcel::readOnly(this, &data);
129 AParcel out = AParcel(this, reply, false /*owns*/);
130
Steven Moreland5d62e442018-09-13 15:01:02 -0700131 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
132 return PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700133 } else {
134 return BBinder::onTransact(code, data, reply, flags);
135 }
136}
137
Steven Moreland71cddc32018-08-30 23:39:22 -0700138ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700139 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700140 CHECK(binder != nullptr);
141}
142ABpBinder::~ABpBinder() {}
143
Steven Moreland94968952018-09-05 14:42:59 -0700144void ABpBinder::onLastStrongRef(const void* id) {
145 {
146 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
147 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
148 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
149 // longer be able to exist at the time of this method call, there is no longer a need to
150 // recover it.
151
152 ABpBinderTag::Value* value =
153 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
154 if (value != nullptr) {
155 value->binder = nullptr;
156 }
157 }
158
159 BpRefBase::onLastStrongRef(id);
160}
161
162sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700163 if (binder == nullptr) {
164 return nullptr;
165 }
166 if (ABBinderTag::has(binder)) {
167 return static_cast<ABBinder*>(binder.get());
168 }
Steven Moreland94968952018-09-05 14:42:59 -0700169
170 // The following code ensures that for a given binder object (remote or local), if it is not an
171 // ABBinder then at most one ABpBinder object exists in a given process representing it.
172 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
173
174 ABpBinderTag::Value* value =
175 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
176 if (value == nullptr) {
177 value = new ABpBinderTag::Value;
178 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
179 ABpBinderTag::clean);
180 }
181
182 sp<ABpBinder> ret = value->binder.promote();
183 if (ret == nullptr) {
184 ret = new ABpBinder(binder);
185 value->binder = ret;
186 }
187
188 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700189}
190
Steven Moreland2e87adc2018-08-20 19:47:00 -0700191struct AIBinder_Weak {
192 wp<AIBinder> binder;
193};
194AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700195 if (binder == nullptr) {
196 return nullptr;
197 }
198
Steven Moreland2e87adc2018-08-20 19:47:00 -0700199 return new AIBinder_Weak{wp<AIBinder>(binder)};
200}
Steven Moreland9b80e282018-09-19 13:57:23 -0700201void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
202 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700203}
204AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700205 if (weakBinder == nullptr) {
206 return nullptr;
207 }
208
Steven Moreland2e87adc2018-08-20 19:47:00 -0700209 sp<AIBinder> binder = weakBinder->binder.promote();
210 AIBinder_incStrong(binder.get());
211 return binder.get();
212}
213
214AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
215 AIBinder_Class_onDestroy onDestroy,
216 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700217 : onCreate(onCreate),
218 onDestroy(onDestroy),
219 onTransact(onTransact),
220 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700221
222AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
223 AIBinder_Class_onCreate onCreate,
224 AIBinder_Class_onDestroy onDestroy,
225 AIBinder_Class_onTransact onTransact) {
226 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
227 onTransact == nullptr) {
228 return nullptr;
229 }
230
231 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
232}
233
Steven Moreland901c7452018-09-04 16:17:28 -0700234void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
235 CHECK(who == mWho);
236
237 mOnDied(mCookie);
238 mWho = nullptr;
239}
240
241AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700242 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700243 CHECK(onDied != nullptr);
244}
245
246binder_status_t AIBinder_DeathRecipient::linkToDeath(AIBinder* binder, void* cookie) {
247 CHECK(binder != nullptr);
248
249 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
250
251 sp<TransferDeathRecipient> recipient =
252 new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied);
253
Steven Moreland5d62e442018-09-13 15:01:02 -0700254 status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
255 if (status != STATUS_OK) {
256 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700257 }
258
259 mDeathRecipients.push_back(recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700260 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700261}
262
263binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) {
264 CHECK(binder != nullptr);
265
266 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
267
268 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
269 sp<TransferDeathRecipient> recipient = *it;
270
Steven Moreland5d62e442018-09-13 15:01:02 -0700271 if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) {
Steven Moreland901c7452018-09-04 16:17:28 -0700272 mDeathRecipients.erase(it.base() - 1);
273
Steven Moreland5d62e442018-09-13 15:01:02 -0700274 status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
275 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700276 LOG(ERROR) << __func__
277 << ": removed reference to death recipient but unlink failed.";
278 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700279 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700280 }
281 }
282
Steven Moreland5d62e442018-09-13 15:01:02 -0700283 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700284}
285
286// start of C-API methods
287
Steven Moreland2e87adc2018-08-20 19:47:00 -0700288AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
289 if (clazz == nullptr) {
290 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
291 return nullptr;
292 }
293
294 void* userData = clazz->onCreate(args);
295
Steven Moreland71cddc32018-08-30 23:39:22 -0700296 sp<AIBinder> ret = new ABBinder(clazz, userData);
297 ABBinderTag::attach(ret->getBinder());
298
299 AIBinder_incStrong(ret.get());
300 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700301}
302
Steven Moreland5ea54da2018-09-04 13:29:55 -0700303bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700304 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800305 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700306 }
307
308 return binder->isRemote();
309}
310
Steven Moreland65867d72018-09-04 14:22:26 -0700311bool AIBinder_isAlive(const AIBinder* binder) {
312 if (binder == nullptr) {
313 return false;
314 }
315
316 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
317}
318
319binder_status_t AIBinder_ping(AIBinder* binder) {
320 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700321 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700322 }
323
Steven Moreland5d62e442018-09-13 15:01:02 -0700324 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700325}
326
Steven Moreland901c7452018-09-04 16:17:28 -0700327binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
328 void* cookie) {
329 if (binder == nullptr || recipient == nullptr) {
330 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700331 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700332 }
333
Steven Moreland5d62e442018-09-13 15:01:02 -0700334 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700335 return recipient->linkToDeath(binder, cookie);
336}
337
338binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
339 void* cookie) {
340 if (binder == nullptr || recipient == nullptr) {
341 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700342 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700343 }
344
Steven Moreland5d62e442018-09-13 15:01:02 -0700345 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700346 return recipient->unlinkToDeath(binder, cookie);
347}
348
Steven Moreland2e87adc2018-08-20 19:47:00 -0700349void AIBinder_incStrong(AIBinder* binder) {
350 if (binder == nullptr) {
351 LOG(ERROR) << __func__ << ": on null binder";
352 return;
353 }
354
355 binder->incStrong(nullptr);
356}
357void AIBinder_decStrong(AIBinder* binder) {
358 if (binder == nullptr) {
359 LOG(ERROR) << __func__ << ": on null binder";
360 return;
361 }
362
363 binder->decStrong(nullptr);
364}
365int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
366 if (binder == nullptr) {
367 LOG(ERROR) << __func__ << ": on null binder";
368 return -1;
369 }
370
371 return binder->getStrongCount();
372}
373
Steven Moreland71cddc32018-08-30 23:39:22 -0700374bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
375 if (binder == nullptr) {
376 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700377 }
378
Steven Moreland71cddc32018-08-30 23:39:22 -0700379 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700380}
381
382const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
383 if (binder == nullptr) {
384 return nullptr;
385 }
386
387 return binder->getClass();
388}
389
390void* AIBinder_getUserData(AIBinder* binder) {
391 if (binder == nullptr) {
392 return nullptr;
393 }
394
395 ABBinder* bBinder = binder->asABBinder();
396 if (bBinder == nullptr) {
397 return nullptr;
398 }
399
400 return bBinder->getUserData();
401}
402
403binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
404 if (binder == nullptr || in == nullptr) {
405 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700406 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700407 }
408 const AIBinder_Class* clazz = binder->getClass();
409 if (clazz == nullptr) {
410 LOG(ERROR) << __func__
411 << ": Class must be defined for a remote binder transaction. See "
412 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700413 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700414 }
415
Steven Moreland3527c2e2018-09-05 17:07:14 -0700416 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700417 LOG(WARNING) << "A binder object at " << binder
418 << " is being transacted on, however, this object is in the same process as "
419 "its proxy. Transacting with this binder is expensive compared to just "
420 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700421 }
422
Steven Moreland2e87adc2018-08-20 19:47:00 -0700423 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700424 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700425 binder_status_t ret = PruneStatusT(status);
426
427 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700428 delete *in;
429 *in = nullptr;
430 }
431
Steven Moreland5d62e442018-09-13 15:01:02 -0700432 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700433}
434
Steven Moreland9b80e282018-09-19 13:57:23 -0700435static void DestroyParcel(AParcel** parcel) {
436 delete *parcel;
437 *parcel = nullptr;
438}
439
Steven Moreland2e87adc2018-08-20 19:47:00 -0700440binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
441 AParcel** out, binder_flags_t flags) {
442 if (in == nullptr) {
443 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700444 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700445 }
446
Steven Morelandcaa776c2018-09-04 13:48:11 -0700447 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700448 // This object is the input to the transaction. This function takes ownership of it and deletes
449 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700450 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700451
452 if (!isUserCommand(code)) {
453 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700454 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700455 }
456
457 if ((flags & ~FLAG_ONEWAY) != 0) {
458 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700459 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700460 }
461
462 if (binder == nullptr || *in == nullptr || out == nullptr) {
463 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700464 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700465 }
466
467 if ((*in)->getBinder() != binder) {
468 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
469 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700470 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700471 }
472
473 *out = new AParcel(binder);
474
Steven Morelandf18615b2018-09-14 11:43:06 -0700475 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700476 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700477
Steven Moreland5d62e442018-09-13 15:01:02 -0700478 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700479 delete *out;
480 *out = nullptr;
481 }
482
Steven Moreland5d62e442018-09-13 15:01:02 -0700483 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700484}
Steven Moreland901c7452018-09-04 16:17:28 -0700485
486AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
487 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
488 if (onBinderDied == nullptr) {
489 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
490 return nullptr;
491 }
492 return new AIBinder_DeathRecipient(onBinderDied);
493}
494
Steven Moreland9b80e282018-09-19 13:57:23 -0700495void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
496 delete recipient;
Steven Moreland901c7452018-09-04 16:17:28 -0700497}