blob: 76acff50bf2ed9b50f1341d9ce1158c44a487b30 [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-base/logging.h>
18#include <android/binder_manager.h>
19#include <iface/iface.h>
20
Steven Moreland507547f2018-11-06 15:58:05 -080021#include <android/binder_auto_utils.h>
22
Steven Moreland2e87adc2018-08-20 19:47:00 -070023using ::android::sp;
24using ::android::wp;
25
26const char* IFoo::kSomeInstanceName = "libbinder_ndk-test-IFoo";
Steven Moreland507547f2018-11-06 15:58:05 -080027const char* IFoo::kInstanceNameToDieFor = "libbinder_ndk-test-IFoo-to-die";
Stephen Crane8fde87f2020-11-17 15:06:11 -080028const char* IFoo::kIFooDescriptor = "my-special-IFoo-class";
Steven Moreland2e87adc2018-08-20 19:47:00 -070029
30struct IFoo_Class_Data {
31 sp<IFoo> foo;
32};
33
34void* IFoo_Class_onCreate(void* args) {
35 IFoo_Class_Data* foo = static_cast<IFoo_Class_Data*>(args);
36 // This is a foo, but we're currently not verifying that. So, the method newLocalBinder is
37 // coupled with this.
38 return static_cast<void*>(foo);
39}
40
41void IFoo_Class_onDestroy(void* userData) {
42 delete static_cast<IFoo_Class_Data*>(userData);
43}
44
45binder_status_t IFoo_Class_onTransact(AIBinder* binder, transaction_code_t code, const AParcel* in,
46 AParcel* out) {
Steven Moreland5d62e442018-09-13 15:01:02 -070047 binder_status_t stat = STATUS_FAILED_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -070048
49 sp<IFoo> foo = static_cast<IFoo_Class_Data*>(AIBinder_getUserData(binder))->foo;
50 CHECK(foo != nullptr) << "Transaction made on already deleted object";
51
52 switch (code) {
53 case IFoo::DOFOO: {
54 int32_t valueIn;
Steven Moreland507547f2018-11-06 15:58:05 -080055 int32_t valueOut;
Steven Moreland2e87adc2018-08-20 19:47:00 -070056 stat = AParcel_readInt32(in, &valueIn);
Steven Moreland5d62e442018-09-13 15:01:02 -070057 if (stat != STATUS_OK) break;
Steven Moreland507547f2018-11-06 15:58:05 -080058 stat = foo->doubleNumber(valueIn, &valueOut);
59 if (stat != STATUS_OK) break;
Steven Moreland2e87adc2018-08-20 19:47:00 -070060 stat = AParcel_writeInt32(out, valueOut);
61 break;
62 }
Steven Moreland507547f2018-11-06 15:58:05 -080063 case IFoo::DIE: {
64 stat = foo->die();
65 break;
66 }
Steven Moreland2e87adc2018-08-20 19:47:00 -070067 }
68
69 return stat;
70}
71
72AIBinder_Class* IFoo::kClass = AIBinder_Class_define(kIFooDescriptor, IFoo_Class_onCreate,
73 IFoo_Class_onDestroy, IFoo_Class_onTransact);
74
Steven Morelandfaf25a42022-12-21 02:21:36 +000075// Defines the same class. Ordinarly, you would never want to do this, but it's done here
76// to simulate what would happen when multiple linker namespaces interact.
77AIBinder_Class* IFoo::kClassDupe = AIBinder_Class_define(
78 kIFooDescriptor, IFoo_Class_onCreate, IFoo_Class_onDestroy, IFoo_Class_onTransact);
79
Steven Moreland2e87adc2018-08-20 19:47:00 -070080class BpFoo : public IFoo {
Steven Moreland6cf66ac2018-11-02 18:14:54 -070081 public:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -080082 explicit BpFoo(AIBinder* binder) : mBinder(binder) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -070083 virtual ~BpFoo() { AIBinder_decStrong(mBinder); }
84
Steven Moreland507547f2018-11-06 15:58:05 -080085 virtual binder_status_t doubleNumber(int32_t in, int32_t* out) {
86 binder_status_t stat = STATUS_OK;
87
Steven Moreland2e87adc2018-08-20 19:47:00 -070088 AParcel* parcelIn;
Steven Moreland507547f2018-11-06 15:58:05 -080089 stat = AIBinder_prepareTransaction(mBinder, &parcelIn);
90 if (stat != STATUS_OK) return stat;
Steven Moreland2e87adc2018-08-20 19:47:00 -070091
Steven Moreland507547f2018-11-06 15:58:05 -080092 stat = AParcel_writeInt32(parcelIn, in);
93 if (stat != STATUS_OK) return stat;
Steven Moreland2e87adc2018-08-20 19:47:00 -070094
Steven Moreland507547f2018-11-06 15:58:05 -080095 ::ndk::ScopedAParcel parcelOut;
96 stat = AIBinder_transact(mBinder, IFoo::DOFOO, &parcelIn, parcelOut.getR(), 0 /*flags*/);
97 if (stat != STATUS_OK) return stat;
Steven Moreland2e87adc2018-08-20 19:47:00 -070098
Steven Moreland507547f2018-11-06 15:58:05 -080099 stat = AParcel_readInt32(parcelOut.get(), out);
100 if (stat != STATUS_OK) return stat;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700101
Steven Moreland507547f2018-11-06 15:58:05 -0800102 return stat;
103 }
Steven Morelandcaa776c2018-09-04 13:48:11 -0700104
Steven Moreland507547f2018-11-06 15:58:05 -0800105 virtual binder_status_t die() {
106 binder_status_t stat = STATUS_OK;
107
108 AParcel* parcelIn;
109 stat = AIBinder_prepareTransaction(mBinder, &parcelIn);
110
111 ::ndk::ScopedAParcel parcelOut;
112 stat = AIBinder_transact(mBinder, IFoo::DIE, &parcelIn, parcelOut.getR(), 0 /*flags*/);
113
114 return stat;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700115 }
116
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700117 private:
Steven Moreland2e87adc2018-08-20 19:47:00 -0700118 // Always assumes one refcount
119 AIBinder* mBinder;
120};
121
122IFoo::~IFoo() {
Steven Moreland9b80e282018-09-19 13:57:23 -0700123 AIBinder_Weak_delete(mWeakBinder);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700124}
125
Steven Moreland88234072020-08-06 19:32:45 +0000126AIBinder* IFoo::getBinder() {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700127 AIBinder* binder = nullptr;
128
129 if (mWeakBinder != nullptr) {
130 // one strong ref count of binder
131 binder = AIBinder_Weak_promote(mWeakBinder);
132 }
133 if (binder == nullptr) {
134 // or one strong refcount here
135 binder = AIBinder_new(IFoo::kClass, static_cast<void*>(new IFoo_Class_Data{this}));
136 if (mWeakBinder != nullptr) {
Steven Moreland9b80e282018-09-19 13:57:23 -0700137 AIBinder_Weak_delete(mWeakBinder);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700138 }
139 mWeakBinder = AIBinder_Weak_new(binder);
Steven Moreland88234072020-08-06 19:32:45 +0000140
141 // WARNING: it is important that this class does not implement debug or
142 // shell functions because it does not use special C++ wrapper
143 // functions, and so this is how we test those functions.
Steven Moreland2e87adc2018-08-20 19:47:00 -0700144 }
145
Steven Moreland88234072020-08-06 19:32:45 +0000146 return binder;
147}
148
149binder_status_t IFoo::addService(const char* instance) {
150 AIBinder* binder = getBinder();
151
Steven Moreland2e87adc2018-08-20 19:47:00 -0700152 binder_status_t status = AServiceManager_addService(binder, instance);
153 // Strong references we care about kept by remote process
154 AIBinder_decStrong(binder);
155 return status;
156}
157
Steven Moreland507547f2018-11-06 15:58:05 -0800158sp<IFoo> IFoo::getService(const char* instance, AIBinder** outBinder) {
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700159 AIBinder* binder = AServiceManager_getService(instance); // maybe nullptr
Steven Moreland2e87adc2018-08-20 19:47:00 -0700160 if (binder == nullptr) {
161 return nullptr;
162 }
163
Steven Moreland71cddc32018-08-30 23:39:22 -0700164 if (!AIBinder_associateClass(binder, IFoo::kClass)) {
165 AIBinder_decStrong(binder);
166 return nullptr;
167 }
168
Steven Moreland507547f2018-11-06 15:58:05 -0800169 if (outBinder != nullptr) {
170 AIBinder_incStrong(binder);
171 *outBinder = binder;
172 }
173
Steven Moreland2e87adc2018-08-20 19:47:00 -0700174 if (AIBinder_isRemote(binder)) {
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700175 sp<IFoo> ret = new BpFoo(binder); // takes ownership of binder
Steven Moreland2e87adc2018-08-20 19:47:00 -0700176 return ret;
177 }
178
179 IFoo_Class_Data* data = static_cast<IFoo_Class_Data*>(AIBinder_getUserData(binder));
180
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700181 CHECK(data != nullptr); // always created with non-null data
Steven Moreland2e87adc2018-08-20 19:47:00 -0700182
183 sp<IFoo> ret = data->foo;
184
185 AIBinder* held = AIBinder_Weak_promote(ret->mWeakBinder);
186 CHECK(held == binder);
187 AIBinder_decStrong(held);
188
Steven Moreland2e87adc2018-08-20 19:47:00 -0700189 AIBinder_decStrong(binder);
190 return ret;
191}