1 /*
2 * Copyright (C) 2015 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 <map>
18 #include <optional>
19 #include <sstream>
20 #include <string>
21 #include <vector>
22
23 #include <unistd.h>
24
25 #include <android-base/unique_fd.h>
26 #include <binder/IInterface.h>
27 #include <binder/IPCThreadState.h>
28 #include <binder/IServiceManager.h>
29 #include <binder/ProcessState.h>
30 #include <binder/Status.h>
31 #include <utils/Errors.h>
32 #include <utils/Log.h>
33 #include <utils/Looper.h>
34 #include <utils/String8.h>
35 #include <utils/StrongPointer.h>
36
37 #include "android/aidl/tests/BackendType.h"
38 #include "android/aidl/tests/BnTestService.h"
39 #include "android/aidl/tests/ITestService.h"
40
41 #include "android/aidl/tests/BnNamedCallback.h"
42 #include "android/aidl/tests/INamedCallback.h"
43
44 #include "android/aidl/versioned/tests/BnFooInterface.h"
45 #include "android/aidl/versioned/tests/IFooInterface.h"
46
47 #include "android/aidl/tests/BnNewName.h"
48 #include "android/aidl/tests/BnOldName.h"
49
50 #include "android/aidl/tests/BnCppJavaTests.h"
51 #include "android/aidl/tests/ICppJavaTests.h"
52
53 #include "android/aidl/tests/Union.h"
54 #include "android/aidl/tests/extension/MyExt.h"
55 #include "android/aidl/tests/extension/MyExt2.h"
56
57 #include "android/aidl/loggable/BnLoggableInterface.h"
58 #include "android/aidl/loggable/Data.h"
59
60 // Used implicitly.
61 #undef LOG_TAG
62 #define LOG_TAG "aidl_native_service"
63
64 // libbase
65 using android::base::unique_fd;
66
67 // libutils:
68 using android::Looper;
69 using android::LooperCallback;
70 using android::OK;
71 using android::sp;
72 using android::String16;
73 using android::String8;
74
75 // libbinder:
76 using android::BnInterface;
77 using android::defaultServiceManager;
78 using android::IInterface;
79 using android::IPCThreadState;
80 using android::Parcel;
81 using android::ProcessState;
82 using android::binder::Status;
83
84 // Generated code:
85 using android::aidl::tests::BackendType;
86 using android::aidl::tests::BnCppJavaTests;
87 using android::aidl::tests::BnNamedCallback;
88 using android::aidl::tests::BnNewName;
89 using android::aidl::tests::BnOldName;
90 using android::aidl::tests::BnTestService;
91 using android::aidl::tests::ByteEnum;
92 using android::aidl::tests::ConstantExpressionEnum;
93 using android::aidl::tests::GenericStructuredParcelable;
94 using android::aidl::tests::ICppJavaTests;
95 using android::aidl::tests::INamedCallback;
96 using android::aidl::tests::INewName;
97 using android::aidl::tests::IntEnum;
98 using android::aidl::tests::IOldName;
99 using android::aidl::tests::LongEnum;
100 using android::aidl::tests::SimpleParcelable;
101 using android::aidl::tests::StructuredParcelable;
102 using android::aidl::tests::Union;
103 using android::os::ParcelFileDescriptor;
104 using android::os::PersistableBundle;
105
106 // Standard library
107 using std::map;
108 using std::optional;
109 using std::string;
110 using std::vector;
111
112 namespace {
113
114 class BinderCallback : public LooperCallback {
115 public:
BinderCallback()116 BinderCallback() {}
~BinderCallback()117 ~BinderCallback() override {}
118
handleEvent(int,int,void *)119 int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
120 IPCThreadState::self()->handlePolledCommands();
121 return 1; // Continue receiving callbacks.
122 }
123 };
124
125 class NamedCallback : public BnNamedCallback {
126 public:
NamedCallback(String16 name)127 explicit NamedCallback(String16 name) : name_(name) {}
128
GetName(String16 * ret)129 Status GetName(String16* ret) {
130 *ret = name_;
131 return Status::ok();
132 }
133
134 private:
135 String16 name_;
136 };
137
138 class OldName : public BnOldName {
139 public:
140 OldName() = default;
141 ~OldName() = default;
142
RealName(String16 * output)143 Status RealName(String16* output) override {
144 *output = String16("OldName");
145 return Status::ok();
146 }
147 };
148
149 class NewName : public BnNewName {
150 public:
151 NewName() = default;
152 ~NewName() = default;
153
RealName(String16 * output)154 Status RealName(String16* output) override {
155 *output = String16("NewName");
156 return Status::ok();
157 }
158 };
159
160 template <typename T>
ReverseArray(const vector<T> & input,vector<T> * repeated,vector<T> * _aidl_return)161 Status ReverseArray(const vector<T>& input, vector<T>* repeated, vector<T>* _aidl_return) {
162 ALOGI("Reversing array of length %zu", input.size());
163 *repeated = input;
164 *_aidl_return = input;
165 std::reverse(_aidl_return->begin(), _aidl_return->end());
166 return Status::ok();
167 }
168
169 template <typename T>
RepeatNullable(const optional<T> & input,optional<T> * _aidl_return)170 Status RepeatNullable(const optional<T>& input, optional<T>* _aidl_return) {
171 ALOGI("Repeating nullable value");
172 *_aidl_return = input;
173 return Status::ok();
174 }
175
176 class CppJavaTests : public BnCppJavaTests {
177 public:
178 CppJavaTests() = default;
179 ~CppJavaTests() = default;
180
RepeatSimpleParcelable(const SimpleParcelable & input,SimpleParcelable * repeat,SimpleParcelable * _aidl_return)181 Status RepeatSimpleParcelable(const SimpleParcelable& input, SimpleParcelable* repeat,
182 SimpleParcelable* _aidl_return) override {
183 ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
184 *repeat = input;
185 *_aidl_return = input;
186 return Status::ok();
187 }
188
RepeatGenericParcelable(const GenericStructuredParcelable<int32_t,StructuredParcelable,IntEnum> & input,GenericStructuredParcelable<int32_t,StructuredParcelable,IntEnum> * repeat,GenericStructuredParcelable<int32_t,StructuredParcelable,IntEnum> * _aidl_return)189 Status RepeatGenericParcelable(
190 const GenericStructuredParcelable<int32_t, StructuredParcelable, IntEnum>& input,
191 GenericStructuredParcelable<int32_t, StructuredParcelable, IntEnum>* repeat,
192 GenericStructuredParcelable<int32_t, StructuredParcelable, IntEnum>* _aidl_return) {
193 ALOGI("Repeating Generic Parcelable");
194 *repeat = input;
195 *_aidl_return = input;
196 return Status::ok();
197 }
198
RepeatPersistableBundle(const PersistableBundle & input,PersistableBundle * _aidl_return)199 Status RepeatPersistableBundle(const PersistableBundle& input,
200 PersistableBundle* _aidl_return) override {
201 ALOGI("Repeated a PersistableBundle");
202 *_aidl_return = input;
203 return Status::ok();
204 }
205
ReverseSimpleParcelables(const vector<SimpleParcelable> & input,vector<SimpleParcelable> * repeated,vector<SimpleParcelable> * _aidl_return)206 Status ReverseSimpleParcelables(const vector<SimpleParcelable>& input,
207 vector<SimpleParcelable>* repeated,
208 vector<SimpleParcelable>* _aidl_return) override {
209 return ReverseArray(input, repeated, _aidl_return);
210 }
ReversePersistableBundles(const vector<PersistableBundle> & input,vector<PersistableBundle> * repeated,vector<PersistableBundle> * _aidl_return)211 Status ReversePersistableBundles(const vector<PersistableBundle>& input,
212 vector<PersistableBundle>* repeated,
213 vector<PersistableBundle>* _aidl_return) override {
214 return ReverseArray(input, repeated, _aidl_return);
215 }
ReverseUnion(const Union & input,Union * repeated,Union * _aidl_return)216 Status ReverseUnion(const Union& input, Union* repeated, Union* _aidl_return) override {
217 ALOGI("Repeated a Union");
218 *repeated = input;
219 *_aidl_return = input;
220 auto reverse = [](auto& reversible) {
221 std::reverse(std::begin(reversible), std::end(reversible));
222 };
223 switch (input.getTag()) {
224 case Union::ns: // int[]
225 reverse(_aidl_return->get<Union::ns>());
226 break;
227 case Union::s: // String
228 reverse(_aidl_return->get<Union::s>());
229 break;
230 case Union::ss: // List<String>
231 reverse(_aidl_return->get<Union::ss>());
232 break;
233 default:
234 break;
235 }
236 return Status::ok();
237 }
ReverseNamedCallbackList(const vector<sp<IBinder>> & input,vector<sp<IBinder>> * repeated,vector<sp<IBinder>> * _aidl_return)238 Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input, vector<sp<IBinder>>* repeated,
239 vector<sp<IBinder>>* _aidl_return) override {
240 return ReverseArray(input, repeated, _aidl_return);
241 }
242
RepeatFileDescriptor(unique_fd read,unique_fd * _aidl_return)243 Status RepeatFileDescriptor(unique_fd read, unique_fd* _aidl_return) override {
244 ALOGE("Repeating file descriptor");
245 *_aidl_return = unique_fd(dup(read.get()));
246 return Status::ok();
247 }
248
ReverseFileDescriptorArray(const vector<unique_fd> & input,vector<unique_fd> * repeated,vector<unique_fd> * _aidl_return)249 Status ReverseFileDescriptorArray(const vector<unique_fd>& input, vector<unique_fd>* repeated,
250 vector<unique_fd>* _aidl_return) override {
251 ALOGI("Reversing descriptor array of length %zu", input.size());
252 for (const auto& item : input) {
253 repeated->push_back(unique_fd(dup(item.get())));
254 _aidl_return->push_back(unique_fd(dup(item.get())));
255 }
256 std::reverse(_aidl_return->begin(), _aidl_return->end());
257 return Status::ok();
258 }
259
TakesAnIBinderList(const vector<sp<IBinder>> & input)260 Status TakesAnIBinderList(const vector<sp<IBinder>>& input) override {
261 (void)input;
262 return Status::ok();
263 }
TakesANullableIBinderList(const optional<vector<sp<IBinder>>> & input)264 Status TakesANullableIBinderList(const optional<vector<sp<IBinder>>>& input) {
265 (void)input;
266 return Status::ok();
267 }
268
RepeatExtendableParcelable(const::android::aidl::tests::extension::ExtendableParcelable & ep,::android::aidl::tests::extension::ExtendableParcelable * ep2)269 ::android::binder::Status RepeatExtendableParcelable(
270 const ::android::aidl::tests::extension::ExtendableParcelable& ep,
271 ::android::aidl::tests::extension::ExtendableParcelable* ep2) {
272 ep2->a = ep.a;
273 ep2->b = ep.b;
274 std::shared_ptr<android::aidl::tests::extension::MyExt> myExt;
275 ep.ext.getParcelable(&myExt);
276 ep2->ext.setParcelable(myExt);
277
278 return Status::ok();
279 }
280 };
281
282 class NativeService : public BnTestService {
283 public:
NativeService()284 NativeService() {}
285 virtual ~NativeService() = default;
286
LogRepeatedStringToken(const String16 & token)287 void LogRepeatedStringToken(const String16& token) {
288 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
289 token.size());
290 }
291
292 template <typename T>
LogRepeatedToken(const T & token)293 void LogRepeatedToken(const T& token) {
294 std::ostringstream token_str;
295 token_str << token;
296 ALOGI("Repeating token %s", token_str.str().c_str());
297 }
298
TestOneway()299 Status TestOneway() override { return Status::fromStatusT(android::UNKNOWN_ERROR); }
300
Deprecated()301 Status Deprecated() override { return Status::ok(); }
302
RepeatBoolean(bool token,bool * _aidl_return)303 Status RepeatBoolean(bool token, bool* _aidl_return) override {
304 LogRepeatedToken(token ? 1 : 0);
305 *_aidl_return = token;
306 return Status::ok();
307 }
RepeatByte(int8_t token,int8_t * _aidl_return)308 Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
309 LogRepeatedToken(token);
310 *_aidl_return = token;
311 return Status::ok();
312 }
RepeatChar(char16_t token,char16_t * _aidl_return)313 Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
314 LogRepeatedStringToken(String16(&token, 1));
315 *_aidl_return = token;
316 return Status::ok();
317 }
RepeatInt(int32_t token,int32_t * _aidl_return)318 Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
319 LogRepeatedToken(token);
320 *_aidl_return = token;
321 return Status::ok();
322 }
RepeatLong(int64_t token,int64_t * _aidl_return)323 Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
324 LogRepeatedToken(token);
325 *_aidl_return = token;
326 return Status::ok();
327 }
RepeatFloat(float token,float * _aidl_return)328 Status RepeatFloat(float token, float* _aidl_return) override {
329 LogRepeatedToken(token);
330 *_aidl_return = token;
331 return Status::ok();
332 }
RepeatDouble(double token,double * _aidl_return)333 Status RepeatDouble(double token, double* _aidl_return) override {
334 LogRepeatedToken(token);
335 *_aidl_return = token;
336 return Status::ok();
337 }
RepeatString(const String16 & token,String16 * _aidl_return)338 Status RepeatString(const String16& token, String16* _aidl_return) override {
339 LogRepeatedStringToken(token);
340 *_aidl_return = token;
341 return Status::ok();
342 }
RepeatByteEnum(ByteEnum token,ByteEnum * _aidl_return)343 Status RepeatByteEnum(ByteEnum token, ByteEnum* _aidl_return) override {
344 ALOGI("Repeating ByteEnum token %s", toString(token).c_str());
345 *_aidl_return = token;
346 return Status::ok();
347 }
RepeatIntEnum(IntEnum token,IntEnum * _aidl_return)348 Status RepeatIntEnum(IntEnum token, IntEnum* _aidl_return) override {
349 ALOGI("Repeating IntEnum token %s", toString(token).c_str());
350 *_aidl_return = token;
351 return Status::ok();
352 }
RepeatLongEnum(LongEnum token,LongEnum * _aidl_return)353 Status RepeatLongEnum(LongEnum token, LongEnum* _aidl_return) override {
354 ALOGI("Repeating LongEnum token %s", toString(token).c_str());
355 *_aidl_return = token;
356 return Status::ok();
357 }
358
ReverseBoolean(const vector<bool> & input,vector<bool> * repeated,vector<bool> * _aidl_return)359 Status ReverseBoolean(const vector<bool>& input,
360 vector<bool>* repeated,
361 vector<bool>* _aidl_return) override {
362 return ReverseArray(input, repeated, _aidl_return);
363 }
ReverseByte(const vector<uint8_t> & input,vector<uint8_t> * repeated,vector<uint8_t> * _aidl_return)364 Status ReverseByte(const vector<uint8_t>& input,
365 vector<uint8_t>* repeated,
366 vector<uint8_t>* _aidl_return) override {
367 return ReverseArray(input, repeated, _aidl_return);
368 }
ReverseChar(const vector<char16_t> & input,vector<char16_t> * repeated,vector<char16_t> * _aidl_return)369 Status ReverseChar(const vector<char16_t>& input,
370 vector<char16_t>* repeated,
371 vector<char16_t>* _aidl_return) override {
372 return ReverseArray(input, repeated, _aidl_return);
373 }
ReverseInt(const vector<int32_t> & input,vector<int32_t> * repeated,vector<int32_t> * _aidl_return)374 Status ReverseInt(const vector<int32_t>& input,
375 vector<int32_t>* repeated,
376 vector<int32_t>* _aidl_return) override {
377 return ReverseArray(input, repeated, _aidl_return);
378 }
ReverseLong(const vector<int64_t> & input,vector<int64_t> * repeated,vector<int64_t> * _aidl_return)379 Status ReverseLong(const vector<int64_t>& input,
380 vector<int64_t>* repeated,
381 vector<int64_t>* _aidl_return) override {
382 return ReverseArray(input, repeated, _aidl_return);
383 }
ReverseFloat(const vector<float> & input,vector<float> * repeated,vector<float> * _aidl_return)384 Status ReverseFloat(const vector<float>& input,
385 vector<float>* repeated,
386 vector<float>* _aidl_return) override {
387 return ReverseArray(input, repeated, _aidl_return);
388 }
ReverseDouble(const vector<double> & input,vector<double> * repeated,vector<double> * _aidl_return)389 Status ReverseDouble(const vector<double>& input,
390 vector<double>* repeated,
391 vector<double>* _aidl_return) override {
392 return ReverseArray(input, repeated, _aidl_return);
393 }
ReverseString(const vector<String16> & input,vector<String16> * repeated,vector<String16> * _aidl_return)394 Status ReverseString(const vector<String16>& input,
395 vector<String16>* repeated,
396 vector<String16>* _aidl_return) override {
397 return ReverseArray(input, repeated, _aidl_return);
398 }
ReverseByteEnum(const vector<ByteEnum> & input,vector<ByteEnum> * repeated,vector<ByteEnum> * _aidl_return)399 Status ReverseByteEnum(const vector<ByteEnum>& input, vector<ByteEnum>* repeated,
400 vector<ByteEnum>* _aidl_return) override {
401 return ReverseArray(input, repeated, _aidl_return);
402 }
ReverseIntEnum(const vector<IntEnum> & input,vector<IntEnum> * repeated,vector<IntEnum> * _aidl_return)403 Status ReverseIntEnum(const vector<IntEnum>& input, vector<IntEnum>* repeated,
404 vector<IntEnum>* _aidl_return) override {
405 return ReverseArray(input, repeated, _aidl_return);
406 }
ReverseLongEnum(const vector<LongEnum> & input,vector<LongEnum> * repeated,vector<LongEnum> * _aidl_return)407 Status ReverseLongEnum(const vector<LongEnum>& input, vector<LongEnum>* repeated,
408 vector<LongEnum>* _aidl_return) override {
409 return ReverseArray(input, repeated, _aidl_return);
410 }
411
GetOtherTestService(const String16 & name,sp<INamedCallback> * returned_service)412 Status GetOtherTestService(const String16& name,
413 sp<INamedCallback>* returned_service) override {
414 if (service_map_.find(name) == service_map_.end()) {
415 sp<INamedCallback> new_item(new NamedCallback(name));
416 service_map_[name] = new_item;
417 }
418
419 *returned_service = service_map_[name];
420 return Status::ok();
421 }
422
VerifyName(const sp<INamedCallback> & service,const String16 & name,bool * returned_value)423 Status VerifyName(const sp<INamedCallback>& service, const String16& name,
424 bool* returned_value) override {
425 String16 foundName;
426 Status status = service->GetName(&foundName);
427
428 if (status.isOk()) {
429 *returned_value = foundName == name;
430 }
431
432 return status;
433 }
434
ReverseStringList(const vector<String16> & input,vector<String16> * repeated,vector<String16> * _aidl_return)435 Status ReverseStringList(const vector<String16>& input,
436 vector<String16>* repeated,
437 vector<String16>* _aidl_return) override {
438 return ReverseArray(input, repeated, _aidl_return);
439 }
440
RepeatParcelFileDescriptor(const ParcelFileDescriptor & read,ParcelFileDescriptor * _aidl_return)441 Status RepeatParcelFileDescriptor(const ParcelFileDescriptor& read,
442 ParcelFileDescriptor* _aidl_return) override {
443 ALOGE("Repeating parcel file descriptor");
444 _aidl_return->reset(unique_fd(dup(read.get())));
445 return Status::ok();
446 }
447
ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor> & input,vector<ParcelFileDescriptor> * repeated,vector<ParcelFileDescriptor> * _aidl_return)448 Status ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor>& input,
449 vector<ParcelFileDescriptor>* repeated,
450 vector<ParcelFileDescriptor>* _aidl_return) override {
451 ALOGI("Reversing parcel descriptor array of length %zu", input.size());
452 for (const auto& item : input) {
453 repeated->push_back(ParcelFileDescriptor(unique_fd(dup(item.get()))));
454 }
455
456 for (auto i = input.rbegin(); i != input.rend(); i++) {
457 _aidl_return->push_back(ParcelFileDescriptor(unique_fd(dup(i->get()))));
458 }
459 return Status::ok();
460 }
461
ThrowServiceException(int code)462 Status ThrowServiceException(int code) override {
463 return Status::fromServiceSpecificError(code);
464 }
465
RepeatNullableIntArray(const optional<vector<int32_t>> & input,optional<vector<int32_t>> * _aidl_return)466 Status RepeatNullableIntArray(const optional<vector<int32_t>>& input,
467 optional<vector<int32_t>>* _aidl_return) {
468 return RepeatNullable(input, _aidl_return);
469 }
470
RepeatNullableByteEnumArray(const optional<vector<ByteEnum>> & input,optional<vector<ByteEnum>> * _aidl_return)471 Status RepeatNullableByteEnumArray(const optional<vector<ByteEnum>>& input,
472 optional<vector<ByteEnum>>* _aidl_return) {
473 return RepeatNullable(input, _aidl_return);
474 }
475
RepeatNullableIntEnumArray(const optional<vector<IntEnum>> & input,optional<vector<IntEnum>> * _aidl_return)476 Status RepeatNullableIntEnumArray(const optional<vector<IntEnum>>& input,
477 optional<vector<IntEnum>>* _aidl_return) {
478 return RepeatNullable(input, _aidl_return);
479 }
480
RepeatNullableLongEnumArray(const optional<vector<LongEnum>> & input,optional<vector<LongEnum>> * _aidl_return)481 Status RepeatNullableLongEnumArray(const optional<vector<LongEnum>>& input,
482 optional<vector<LongEnum>>* _aidl_return) {
483 return RepeatNullable(input, _aidl_return);
484 }
485
RepeatNullableStringList(const optional<vector<optional<String16>>> & input,optional<vector<optional<String16>>> * _aidl_return)486 Status RepeatNullableStringList(const optional<vector<optional<String16>>>& input,
487 optional<vector<optional<String16>>>* _aidl_return) {
488 ALOGI("Repeating nullable string list");
489 return RepeatNullable(input, _aidl_return);
490 }
491
RepeatNullableString(const optional<String16> & input,optional<String16> * _aidl_return)492 Status RepeatNullableString(const optional<String16>& input, optional<String16>* _aidl_return) {
493 return RepeatNullable(input, _aidl_return);
494 }
495
RepeatNullableParcelable(const optional<StructuredParcelable> & input,optional<StructuredParcelable> * _aidl_return)496 Status RepeatNullableParcelable(const optional<StructuredParcelable>& input,
497 optional<StructuredParcelable>* _aidl_return) {
498 return RepeatNullable(input, _aidl_return);
499 }
500
TakesAnIBinder(const sp<IBinder> & input)501 Status TakesAnIBinder(const sp<IBinder>& input) override {
502 (void)input;
503 return Status::ok();
504 }
TakesANullableIBinder(const sp<IBinder> & input)505 Status TakesANullableIBinder(const sp<IBinder>& input) {
506 (void)input;
507 return Status::ok();
508 }
509
RepeatUtf8CppString(const string & token,string * _aidl_return)510 Status RepeatUtf8CppString(const string& token,
511 string* _aidl_return) override {
512 ALOGI("Repeating utf8 string '%s' of length=%zu", token.c_str(), token.size());
513 *_aidl_return = token;
514 return Status::ok();
515 }
516
RepeatNullableUtf8CppString(const optional<string> & token,optional<string> * _aidl_return)517 Status RepeatNullableUtf8CppString(const optional<string>& token,
518 optional<string>* _aidl_return) override {
519 if (!token) {
520 ALOGI("Received null @utf8InCpp string");
521 return Status::ok();
522 }
523 ALOGI("Repeating utf8 string '%s' of length=%zu",
524 token->c_str(), token->size());
525 *_aidl_return = token;
526 return Status::ok();
527 }
528
ReverseUtf8CppString(const vector<string> & input,vector<string> * repeated,vector<string> * _aidl_return)529 Status ReverseUtf8CppString(const vector<string>& input,
530 vector<string>* repeated,
531 vector<string>* _aidl_return) {
532 return ReverseArray(input, repeated, _aidl_return);
533 }
534
ReverseNullableUtf8CppString(const optional<vector<optional<string>>> & input,optional<vector<optional<string>>> * repeated,optional<vector<optional<string>>> * _aidl_return)535 Status ReverseNullableUtf8CppString(const optional<vector<optional<string>>>& input,
536 optional<vector<optional<string>>>* repeated,
537 optional<vector<optional<string>>>* _aidl_return) {
538 return ReverseUtf8CppStringList(input, repeated, _aidl_return);
539 }
540
ReverseUtf8CppStringList(const optional<vector<optional<string>>> & input,optional<vector<optional<string>>> * repeated,optional<vector<optional<string>>> * _aidl_return)541 Status ReverseUtf8CppStringList(const optional<vector<optional<string>>>& input,
542 optional<vector<optional<string>>>* repeated,
543 optional<vector<optional<string>>>* _aidl_return) {
544 if (!input) {
545 ALOGI("Received null list of utf8 strings");
546 return Status::ok();
547 }
548 *_aidl_return = input;
549 *repeated = input;
550 std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end());
551 return Status::ok();
552 }
553
GetCallback(bool return_null,sp<INamedCallback> * ret)554 Status GetCallback(bool return_null, sp<INamedCallback>* ret) {
555 if (!return_null) {
556 return GetOtherTestService(String16("ABT: always be testing"), ret);
557 }
558 return Status::ok();
559 }
560
FillOutStructuredParcelable(StructuredParcelable * parcelable)561 virtual ::android::binder::Status FillOutStructuredParcelable(StructuredParcelable* parcelable) {
562 parcelable->shouldBeJerry = "Jerry";
563 parcelable->shouldContainThreeFs = {parcelable->f, parcelable->f, parcelable->f};
564 parcelable->shouldBeByteBar = ByteEnum::BAR;
565 parcelable->shouldBeIntBar = IntEnum::BAR;
566 parcelable->shouldBeLongBar = LongEnum::BAR;
567 parcelable->shouldContainTwoByteFoos = {ByteEnum::FOO, ByteEnum::FOO};
568 parcelable->shouldContainTwoIntFoos = {IntEnum::FOO, IntEnum::FOO};
569 parcelable->shouldContainTwoLongFoos = {LongEnum::FOO, LongEnum::FOO};
570
571 parcelable->const_exprs_1 = ConstantExpressionEnum::decInt32_1;
572 parcelable->const_exprs_2 = ConstantExpressionEnum::decInt32_2;
573 parcelable->const_exprs_3 = ConstantExpressionEnum::decInt64_1;
574 parcelable->const_exprs_4 = ConstantExpressionEnum::decInt64_2;
575 parcelable->const_exprs_5 = ConstantExpressionEnum::decInt64_3;
576 parcelable->const_exprs_6 = ConstantExpressionEnum::decInt64_4;
577 parcelable->const_exprs_7 = ConstantExpressionEnum::hexInt32_1;
578 parcelable->const_exprs_8 = ConstantExpressionEnum::hexInt32_2;
579 parcelable->const_exprs_9 = ConstantExpressionEnum::hexInt32_3;
580 parcelable->const_exprs_10 = ConstantExpressionEnum::hexInt64_1;
581
582 parcelable->shouldSetBit0AndBit2 = StructuredParcelable::BIT0 | StructuredParcelable::BIT2;
583
584 parcelable->u = Union::make<Union::ns>({1, 2, 3});
585 parcelable->shouldBeConstS1 = Union::S1();
586 return Status::ok();
587 }
588
UnimplementedMethod(int32_t,int32_t *)589 Status UnimplementedMethod(int32_t /* arg */, int32_t* /* _aidl_return */) override {
590 LOG_ALWAYS_FATAL("UnimplementedMethod shouldn't be called");
591 }
592
GetOldNameInterface(sp<IOldName> * ret)593 Status GetOldNameInterface(sp<IOldName>* ret) {
594 *ret = new OldName;
595 return Status::ok();
596 }
597
GetNewNameInterface(sp<INewName> * ret)598 Status GetNewNameInterface(sp<INewName>* ret) {
599 *ret = new NewName;
600 return Status::ok();
601 }
602
GetCppJavaTests(sp<IBinder> * ret)603 Status GetCppJavaTests(sp<IBinder>* ret) {
604 *ret = new CppJavaTests;
605 return Status::ok();
606 }
607
getBackendType(BackendType * _aidl_return)608 Status getBackendType(BackendType* _aidl_return) override {
609 *_aidl_return = BackendType::CPP;
610 return Status::ok();
611 }
612
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)613 android::status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
614 uint32_t flags) override {
615 if (code == ::android::IBinder::FIRST_CALL_TRANSACTION + 0 /* UnimplementedMethod */) {
616 // pretend that UnimplementedMethod isn't implemented by this service.
617 return android::UNKNOWN_TRANSACTION;
618 } else {
619 return BnTestService::onTransact(code, data, reply, flags);
620 }
621 }
622
623 private:
624 map<String16, sp<INamedCallback>> service_map_;
625 };
626
627 class VersionedService : public android::aidl::versioned::tests::BnFooInterface {
628 public:
VersionedService()629 VersionedService() {}
630 virtual ~VersionedService() = default;
631
originalApi()632 Status originalApi() override { return Status::ok(); }
acceptUnionAndReturnString(const::android::aidl::versioned::tests::BazUnion & u,std::string * _aidl_return)633 Status acceptUnionAndReturnString(const ::android::aidl::versioned::tests::BazUnion& u,
634 std::string* _aidl_return) override {
635 switch (u.getTag()) {
636 case ::android::aidl::versioned::tests::BazUnion::intNum:
637 *_aidl_return =
638 std::to_string(u.get<::android::aidl::versioned::tests::BazUnion::intNum>());
639 break;
640 }
641 return Status::ok();
642 }
returnsLengthOfFooArray(const vector<::android::aidl::versioned::tests::Foo> & foos,int32_t * ret)643 Status returnsLengthOfFooArray(const vector<::android::aidl::versioned::tests::Foo>& foos,
644 int32_t* ret) override {
645 *ret = static_cast<int32_t>(foos.size());
646 return Status::ok();
647 }
ignoreParcelablesAndRepeatInt(const::android::aidl::versioned::tests::Foo & inFoo,::android::aidl::versioned::tests::Foo * inoutFoo,::android::aidl::versioned::tests::Foo * outFoo,int32_t value,int32_t * ret)648 Status ignoreParcelablesAndRepeatInt(const ::android::aidl::versioned::tests::Foo& inFoo,
649 ::android::aidl::versioned::tests::Foo* inoutFoo,
650 ::android::aidl::versioned::tests::Foo* outFoo,
651 int32_t value, int32_t* ret) override {
652 (void)inFoo;
653 (void)inoutFoo;
654 (void)outFoo;
655 *ret = value;
656 return Status::ok();
657 }
658 };
659
660 class LoggableInterfaceService : public android::aidl::loggable::BnLoggableInterface {
661 public:
LoggableInterfaceService()662 LoggableInterfaceService() {}
663 virtual ~LoggableInterfaceService() = default;
664
LogThis(bool,vector<bool> *,int8_t,vector<uint8_t> *,char16_t,vector<char16_t> *,int32_t,vector<int32_t> *,int64_t,vector<int64_t> *,float,vector<float> *,double,vector<double> *,const String16 &,vector<String16> *,vector<String16> *,const android::aidl::loggable::Data &,const sp<IBinder> &,optional<ParcelFileDescriptor> *,vector<ParcelFileDescriptor> *,vector<String16> * _aidl_return)665 virtual Status LogThis(bool, vector<bool>*, int8_t, vector<uint8_t>*, char16_t, vector<char16_t>*,
666 int32_t, vector<int32_t>*, int64_t, vector<int64_t>*, float,
667 vector<float>*, double, vector<double>*, const String16&,
668 vector<String16>*, vector<String16>*, const android::aidl::loggable::Data&,
669 const sp<IBinder>&, optional<ParcelFileDescriptor>*,
670 vector<ParcelFileDescriptor>*, vector<String16>* _aidl_return) override {
671 *_aidl_return = vector<String16>{String16("loggable")};
672 return Status::ok();
673 }
674 };
675
Run()676 int Run() {
677 android::sp<NativeService> service = new NativeService;
678 sp<Looper> looper(Looper::prepare(0 /* opts */));
679
680 int binder_fd = -1;
681 ProcessState::self()->setThreadPoolMaxThreadCount(0);
682 IPCThreadState::self()->disableBackgroundScheduling(true);
683 IPCThreadState::self()->setupPolling(&binder_fd);
684 ALOGI("Got binder FD %d", binder_fd);
685 if (binder_fd < 0) return -1;
686
687 sp<BinderCallback> cb(new BinderCallback);
688 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
689 nullptr) != 1) {
690 ALOGE("Failed to add binder FD to Looper");
691 return -1;
692 }
693
694 auto status = defaultServiceManager()->addService(service->getInterfaceDescriptor(), service);
695 if (status != OK) {
696 ALOGE("Failed to add service %s", String8(service->getInterfaceDescriptor()).c_str());
697 return -1;
698 }
699
700 android::sp<VersionedService> versionedService = new VersionedService;
701 status = defaultServiceManager()->addService(versionedService->getInterfaceDescriptor(),
702 versionedService);
703 if (status != OK) {
704 ALOGE("Failed to add service %s", String8(versionedService->getInterfaceDescriptor()).c_str());
705 return -1;
706 }
707
708 android::sp<LoggableInterfaceService> loggableInterfaceService = new LoggableInterfaceService;
709 status = defaultServiceManager()->addService(loggableInterfaceService->getInterfaceDescriptor(),
710 loggableInterfaceService);
711 if (status != OK) {
712 ALOGE("Failed to add service %s",
713 String8(loggableInterfaceService->getInterfaceDescriptor()).c_str());
714 return -1;
715 }
716
717 ALOGI("Entering loop");
718 while (true) {
719 const int result = looper->pollAll(-1 /* timeoutMillis */);
720 ALOGI("Looper returned %d", result);
721 }
722 return 0;
723 }
724
725 } // namespace
726
main(int,char * [])727 int main(int /* argc */, char* /* argv */ []) {
728 return Run();
729 }
730