blob: f9c8c8a0ff84a0d0d46cc80e9cb4af9863247286 [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>
Steven Morelandf3034b02018-11-12 17:37:46 -080025#include <binder/IPCThreadState.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070026
Steven Moreland901c7452018-09-04 16:17:28 -070027using DeathRecipient = ::android::IBinder::DeathRecipient;
28
Steven Moreland2e87adc2018-08-20 19:47:00 -070029using ::android::IBinder;
30using ::android::Parcel;
31using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070032using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070033using ::android::String16;
34using ::android::wp;
35
Steven Moreland71cddc32018-08-30 23:39:22 -070036namespace ABBinderTag {
37
38static const void* kId = "ABBinder";
39static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070040void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070041
42static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070043 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070044}
45static bool has(const sp<IBinder>& binder) {
46 return binder != nullptr && binder->findObject(kId) == kValue;
47}
48
Steven Moreland6cf66ac2018-11-02 18:14:54 -070049} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070050
Steven Moreland94968952018-09-05 14:42:59 -070051namespace ABpBinderTag {
52
53static std::mutex gLock;
54static const void* kId = "ABpBinder";
55struct Value {
56 wp<ABpBinder> binder;
57};
58void clean(const void* id, void* obj, void* cookie) {
59 CHECK(id == kId) << id << " " << obj << " " << cookie;
60
61 delete static_cast<Value*>(obj);
62};
63
Steven Moreland6cf66ac2018-11-02 18:14:54 -070064} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070065
Steven Moreland2e87adc2018-08-20 19:47:00 -070066AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
67AIBinder::~AIBinder() {}
68
Steven Moreland71cddc32018-08-30 23:39:22 -070069bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070070 using ::android::String8;
71
Steven Moreland71cddc32018-08-30 23:39:22 -070072 if (clazz == nullptr) return false;
73 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070074
75 String8 newDescriptor(clazz->getInterfaceDescriptor());
76
77 if (mClazz != nullptr) {
78 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
79 if (newDescriptor == currentDescriptor) {
80 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
81 << "' match during associateClass, but they are different class objects. "
82 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070083 } else {
84 LOG(ERROR) << __func__
85 << ": Class cannot be associated on object which already has a class. "
86 "Trying to associate to '"
87 << newDescriptor.c_str() << "' but already set to '"
88 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070089 }
90
Steven Moreland71cddc32018-08-30 23:39:22 -070091 // always a failure because we know mClazz != clazz
92 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070093 }
94
Steven Moreland6cf66ac2018-11-02 18:14:54 -070095 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -070096
Steven Moreland2e87adc2018-08-20 19:47:00 -070097 String8 descriptor(getBinder()->getInterfaceDescriptor());
98 if (descriptor != newDescriptor) {
99 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
100 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700101 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700102 }
103
Steven Moreland71cddc32018-08-30 23:39:22 -0700104 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700105 mClazz = clazz;
106
Steven Moreland71cddc32018-08-30 23:39:22 -0700107 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700108}
109
110ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700111 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700112 CHECK(clazz != nullptr);
113}
114ABBinder::~ABBinder() {
115 getClass()->onDestroy(mUserData);
116}
117
118const String16& ABBinder::getInterfaceDescriptor() const {
119 return getClass()->getInterfaceDescriptor();
120}
121
Steven Moreland5d62e442018-09-13 15:01:02 -0700122status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
123 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700124 if (isUserCommand(code)) {
125 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700126 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700127 }
128
129 const AParcel in = AParcel::readOnly(this, &data);
130 AParcel out = AParcel(this, reply, false /*owns*/);
131
Steven Moreland5d62e442018-09-13 15:01:02 -0700132 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
133 return PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700134 } else {
135 return BBinder::onTransact(code, data, reply, flags);
136 }
137}
138
Steven Moreland71cddc32018-08-30 23:39:22 -0700139ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700140 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700141 CHECK(binder != nullptr);
142}
143ABpBinder::~ABpBinder() {}
144
Steven Moreland94968952018-09-05 14:42:59 -0700145void ABpBinder::onLastStrongRef(const void* id) {
146 {
147 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
148 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
149 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
150 // longer be able to exist at the time of this method call, there is no longer a need to
151 // recover it.
152
153 ABpBinderTag::Value* value =
154 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
155 if (value != nullptr) {
156 value->binder = nullptr;
157 }
158 }
159
160 BpRefBase::onLastStrongRef(id);
161}
162
163sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700164 if (binder == nullptr) {
165 return nullptr;
166 }
167 if (ABBinderTag::has(binder)) {
168 return static_cast<ABBinder*>(binder.get());
169 }
Steven Moreland94968952018-09-05 14:42:59 -0700170
171 // The following code ensures that for a given binder object (remote or local), if it is not an
172 // ABBinder then at most one ABpBinder object exists in a given process representing it.
173 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
174
175 ABpBinderTag::Value* value =
176 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
177 if (value == nullptr) {
178 value = new ABpBinderTag::Value;
179 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
180 ABpBinderTag::clean);
181 }
182
183 sp<ABpBinder> ret = value->binder.promote();
184 if (ret == nullptr) {
185 ret = new ABpBinder(binder);
186 value->binder = ret;
187 }
188
189 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700190}
191
Steven Moreland2e87adc2018-08-20 19:47:00 -0700192struct AIBinder_Weak {
193 wp<AIBinder> binder;
194};
195AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700196 if (binder == nullptr) {
197 return nullptr;
198 }
199
Steven Moreland2e87adc2018-08-20 19:47:00 -0700200 return new AIBinder_Weak{wp<AIBinder>(binder)};
201}
Steven Moreland9b80e282018-09-19 13:57:23 -0700202void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
203 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700204}
205AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700206 if (weakBinder == nullptr) {
207 return nullptr;
208 }
209
Steven Moreland2e87adc2018-08-20 19:47:00 -0700210 sp<AIBinder> binder = weakBinder->binder.promote();
211 AIBinder_incStrong(binder.get());
212 return binder.get();
213}
214
215AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
216 AIBinder_Class_onDestroy onDestroy,
217 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700218 : onCreate(onCreate),
219 onDestroy(onDestroy),
220 onTransact(onTransact),
221 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700222
223AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
224 AIBinder_Class_onCreate onCreate,
225 AIBinder_Class_onDestroy onDestroy,
226 AIBinder_Class_onTransact onTransact) {
227 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
228 onTransact == nullptr) {
229 return nullptr;
230 }
231
232 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
233}
234
Steven Moreland901c7452018-09-04 16:17:28 -0700235void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
236 CHECK(who == mWho);
237
238 mOnDied(mCookie);
239 mWho = nullptr;
240}
241
242AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700243 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700244 CHECK(onDied != nullptr);
245}
246
247binder_status_t AIBinder_DeathRecipient::linkToDeath(AIBinder* binder, void* cookie) {
248 CHECK(binder != nullptr);
249
250 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
251
252 sp<TransferDeathRecipient> recipient =
253 new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied);
254
Steven Moreland5d62e442018-09-13 15:01:02 -0700255 status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
256 if (status != STATUS_OK) {
257 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700258 }
259
260 mDeathRecipients.push_back(recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700261 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700262}
263
264binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) {
265 CHECK(binder != nullptr);
266
267 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
268
269 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
270 sp<TransferDeathRecipient> recipient = *it;
271
Steven Moreland5d62e442018-09-13 15:01:02 -0700272 if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) {
Steven Moreland901c7452018-09-04 16:17:28 -0700273 mDeathRecipients.erase(it.base() - 1);
274
Steven Moreland5d62e442018-09-13 15:01:02 -0700275 status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
276 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700277 LOG(ERROR) << __func__
278 << ": removed reference to death recipient but unlink failed.";
279 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700280 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700281 }
282 }
283
Steven Moreland5d62e442018-09-13 15:01:02 -0700284 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700285}
286
287// start of C-API methods
288
Steven Moreland2e87adc2018-08-20 19:47:00 -0700289AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
290 if (clazz == nullptr) {
291 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
292 return nullptr;
293 }
294
295 void* userData = clazz->onCreate(args);
296
Steven Moreland71cddc32018-08-30 23:39:22 -0700297 sp<AIBinder> ret = new ABBinder(clazz, userData);
298 ABBinderTag::attach(ret->getBinder());
299
300 AIBinder_incStrong(ret.get());
301 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700302}
303
Steven Moreland5ea54da2018-09-04 13:29:55 -0700304bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700305 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800306 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700307 }
308
309 return binder->isRemote();
310}
311
Steven Moreland65867d72018-09-04 14:22:26 -0700312bool AIBinder_isAlive(const AIBinder* binder) {
313 if (binder == nullptr) {
314 return false;
315 }
316
317 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
318}
319
320binder_status_t AIBinder_ping(AIBinder* binder) {
321 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700322 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700323 }
324
Steven Moreland5d62e442018-09-13 15:01:02 -0700325 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700326}
327
Steven Moreland901c7452018-09-04 16:17:28 -0700328binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
329 void* cookie) {
330 if (binder == nullptr || recipient == nullptr) {
331 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700332 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700333 }
334
Steven Moreland5d62e442018-09-13 15:01:02 -0700335 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700336 return recipient->linkToDeath(binder, cookie);
337}
338
339binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
340 void* cookie) {
341 if (binder == nullptr || recipient == nullptr) {
342 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700343 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700344 }
345
Steven Moreland5d62e442018-09-13 15:01:02 -0700346 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700347 return recipient->unlinkToDeath(binder, cookie);
348}
349
Steven Morelandf3034b02018-11-12 17:37:46 -0800350uid_t AIBinder_getCallingUid() {
351 return ::android::IPCThreadState::self()->getCallingUid();
352}
353
354pid_t AIBinder_getCallingPid() {
355 return ::android::IPCThreadState::self()->getCallingPid();
356}
357
Steven Moreland2e87adc2018-08-20 19:47:00 -0700358void AIBinder_incStrong(AIBinder* binder) {
359 if (binder == nullptr) {
360 LOG(ERROR) << __func__ << ": on null binder";
361 return;
362 }
363
364 binder->incStrong(nullptr);
365}
366void AIBinder_decStrong(AIBinder* binder) {
367 if (binder == nullptr) {
368 LOG(ERROR) << __func__ << ": on null binder";
369 return;
370 }
371
372 binder->decStrong(nullptr);
373}
374int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
375 if (binder == nullptr) {
376 LOG(ERROR) << __func__ << ": on null binder";
377 return -1;
378 }
379
380 return binder->getStrongCount();
381}
382
Steven Moreland71cddc32018-08-30 23:39:22 -0700383bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
384 if (binder == nullptr) {
385 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700386 }
387
Steven Moreland71cddc32018-08-30 23:39:22 -0700388 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700389}
390
391const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
392 if (binder == nullptr) {
393 return nullptr;
394 }
395
396 return binder->getClass();
397}
398
399void* AIBinder_getUserData(AIBinder* binder) {
400 if (binder == nullptr) {
401 return nullptr;
402 }
403
404 ABBinder* bBinder = binder->asABBinder();
405 if (bBinder == nullptr) {
406 return nullptr;
407 }
408
409 return bBinder->getUserData();
410}
411
412binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
413 if (binder == nullptr || in == nullptr) {
414 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700415 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700416 }
417 const AIBinder_Class* clazz = binder->getClass();
418 if (clazz == nullptr) {
419 LOG(ERROR) << __func__
420 << ": Class must be defined for a remote binder transaction. See "
421 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700422 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700423 }
424
Steven Moreland3527c2e2018-09-05 17:07:14 -0700425 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700426 LOG(WARNING) << "A binder object at " << binder
427 << " is being transacted on, however, this object is in the same process as "
428 "its proxy. Transacting with this binder is expensive compared to just "
429 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700430 }
431
Steven Moreland2e87adc2018-08-20 19:47:00 -0700432 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700433 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700434 binder_status_t ret = PruneStatusT(status);
435
436 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700437 delete *in;
438 *in = nullptr;
439 }
440
Steven Moreland5d62e442018-09-13 15:01:02 -0700441 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700442}
443
Steven Moreland9b80e282018-09-19 13:57:23 -0700444static void DestroyParcel(AParcel** parcel) {
445 delete *parcel;
446 *parcel = nullptr;
447}
448
Steven Moreland2e87adc2018-08-20 19:47:00 -0700449binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
450 AParcel** out, binder_flags_t flags) {
451 if (in == nullptr) {
452 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700453 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700454 }
455
Steven Morelandcaa776c2018-09-04 13:48:11 -0700456 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700457 // This object is the input to the transaction. This function takes ownership of it and deletes
458 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700459 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700460
461 if (!isUserCommand(code)) {
462 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700463 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700464 }
465
466 if ((flags & ~FLAG_ONEWAY) != 0) {
467 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700468 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700469 }
470
471 if (binder == nullptr || *in == nullptr || out == nullptr) {
472 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700473 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700474 }
475
476 if ((*in)->getBinder() != binder) {
477 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
478 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700479 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700480 }
481
482 *out = new AParcel(binder);
483
Steven Morelandf18615b2018-09-14 11:43:06 -0700484 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700485 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700486
Steven Moreland5d62e442018-09-13 15:01:02 -0700487 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700488 delete *out;
489 *out = nullptr;
490 }
491
Steven Moreland5d62e442018-09-13 15:01:02 -0700492 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700493}
Steven Moreland901c7452018-09-04 16:17:28 -0700494
495AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
496 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
497 if (onBinderDied == nullptr) {
498 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
499 return nullptr;
500 }
501 return new AIBinder_DeathRecipient(onBinderDied);
502}
503
Steven Moreland9b80e282018-09-19 13:57:23 -0700504void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
505 delete recipient;
Steven Moreland901c7452018-09-04 16:17:28 -0700506}