• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <mutex>
19 #include <optional>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 #include <unistd.h>
25 
26 #include <android-base/unique_fd.h>
27 #include <binder/IInterface.h>
28 #include <binder/IPCThreadState.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/ProcessState.h>
31 #include <binder/Status.h>
32 #include <utils/Errors.h>
33 #include <utils/Log.h>
34 #include <utils/Looper.h>
35 #include <utils/String8.h>
36 #include <utils/StrongPointer.h>
37 
38 #include "android/aidl/tests/BackendType.h"
39 #include "android/aidl/tests/BnTestService.h"
40 #include "android/aidl/tests/ITestService.h"
41 
42 #include "android/aidl/tests/BnNamedCallback.h"
43 #include "android/aidl/tests/INamedCallback.h"
44 
45 #include "android/aidl/versioned/tests/BnFooInterface.h"
46 #include "android/aidl/versioned/tests/IFooInterface.h"
47 
48 #include "android/aidl/tests/BnNewName.h"
49 #include "android/aidl/tests/BnOldName.h"
50 
51 #include "android/aidl/tests/BnCppJavaTests.h"
52 #include "android/aidl/tests/ICppJavaTests.h"
53 
54 #include "android/aidl/tests/Union.h"
55 #include "android/aidl/tests/extension/MyExt.h"
56 #include "android/aidl/tests/extension/MyExt2.h"
57 
58 #include "android/aidl/tests/nested/BnNestedService.h"
59 
60 #include "android/aidl/tests/BnCircular.h"
61 #include "android/aidl/tests/ICircular.h"
62 
63 #include "android/aidl/loggable/BnLoggableInterface.h"
64 #include "android/aidl/loggable/Data.h"
65 
66 #include "android/aidl/fixedsizearray/FixedSizeArrayExample.h"
67 
68 // Used implicitly.
69 #undef LOG_TAG
70 #define LOG_TAG "aidl_native_service"
71 
72 // libbase
73 using android::base::unique_fd;
74 
75 // libutils:
76 using android::Looper;
77 using android::LooperCallback;
78 using android::OK;
79 using android::sp;
80 using android::String16;
81 using android::String8;
82 
83 // libbinder:
84 using android::BnInterface;
85 using android::defaultServiceManager;
86 using android::IInterface;
87 using android::IPCThreadState;
88 using android::Parcel;
89 using android::ProcessState;
90 using android::binder::Status;
91 
92 // Generated code:
93 using android::aidl::tests::BackendType;
94 using android::aidl::tests::BadParcelable;
95 using android::aidl::tests::BnCircular;
96 using android::aidl::tests::BnCppJavaTests;
97 using android::aidl::tests::BnNamedCallback;
98 using android::aidl::tests::BnNewName;
99 using android::aidl::tests::BnOldName;
100 using android::aidl::tests::BnTestService;
101 using android::aidl::tests::ByteEnum;
102 using android::aidl::tests::CircularParcelable;
103 using android::aidl::tests::ConstantExpressionEnum;
104 using android::aidl::tests::GenericStructuredParcelable;
105 using android::aidl::tests::ICircular;
106 using android::aidl::tests::ICppJavaTests;
107 using android::aidl::tests::INamedCallback;
108 using android::aidl::tests::INewName;
109 using android::aidl::tests::IntEnum;
110 using android::aidl::tests::IOldName;
111 using android::aidl::tests::ITestService;
112 using android::aidl::tests::LongEnum;
113 using android::aidl::tests::RecursiveList;
114 using android::aidl::tests::SimpleParcelable;
115 using android::aidl::tests::StructuredParcelable;
116 using android::aidl::tests::Union;
117 using android::os::ParcelFileDescriptor;
118 using android::os::PersistableBundle;
119 
120 // Standard library
121 using std::map;
122 using std::optional;
123 using std::string;
124 using std::vector;
125 
126 namespace {
127 
128 class BinderCallback : public LooperCallback {
129  public:
BinderCallback()130   BinderCallback() {}
~BinderCallback()131   ~BinderCallback() override {}
132 
handleEvent(int,int,void *)133   int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
134     IPCThreadState::self()->handlePolledCommands();
135     return 1;  // Continue receiving callbacks.
136   }
137 };
138 
139 class NamedCallback : public BnNamedCallback {
140  public:
NamedCallback(String16 name)141   explicit NamedCallback(String16 name) : name_(name) {}
142 
GetName(String16 * ret)143   Status GetName(String16* ret) {
144     *ret = name_;
145     return Status::ok();
146   }
147 
148  private:
149   String16 name_;
150 };
151 
152 class OldName : public BnOldName {
153  public:
154   OldName() = default;
155   ~OldName() = default;
156 
RealName(String16 * output)157   Status RealName(String16* output) override {
158     *output = String16("OldName");
159     return Status::ok();
160   }
161 };
162 
163 class NewName : public BnNewName {
164  public:
165   NewName() = default;
166   ~NewName() = default;
167 
RealName(String16 * output)168   Status RealName(String16* output) override {
169     *output = String16("NewName");
170     return Status::ok();
171   }
172 };
173 
174 class Circular : public BnCircular {
175  public:
Circular(sp<ITestService> srv)176   Circular(sp<ITestService> srv) : mSrv(srv) {}
177   ~Circular() = default;
178 
GetTestService(sp<ITestService> * _aidl_return)179   Status GetTestService(sp<ITestService>* _aidl_return) override {
180     *_aidl_return = mSrv;
181     return Status::ok();
182   }
183 
184  private:
185   sp<ITestService> mSrv;
186 };
187 
188 template <typename T>
ReverseArray(const vector<T> & input,vector<T> * repeated,vector<T> * _aidl_return)189 Status ReverseArray(const vector<T>& input, vector<T>* repeated, vector<T>* _aidl_return) {
190   ALOGI("Reversing array of length %zu", input.size());
191   *repeated = input;
192   *_aidl_return = input;
193   std::reverse(_aidl_return->begin(), _aidl_return->end());
194   return Status::ok();
195 }
196 
197 template <typename T>
RepeatNullable(const optional<T> & input,optional<T> * _aidl_return)198 Status RepeatNullable(const optional<T>& input, optional<T>* _aidl_return) {
199   ALOGI("Repeating nullable value");
200   *_aidl_return = input;
201   return Status::ok();
202 }
203 
204 class CppJavaTests : public BnCppJavaTests {
205  public:
206   CppJavaTests() = default;
207   ~CppJavaTests() = default;
208 
RepeatBadParcelable(const BadParcelable & input,BadParcelable * _aidl_return)209   Status RepeatBadParcelable(const BadParcelable& input, BadParcelable* _aidl_return) override {
210     *_aidl_return = input;
211     return Status::ok();
212   }
213 
RepeatSimpleParcelable(const SimpleParcelable & input,SimpleParcelable * repeat,SimpleParcelable * _aidl_return)214   Status RepeatSimpleParcelable(const SimpleParcelable& input, SimpleParcelable* repeat,
215                                 SimpleParcelable* _aidl_return) override {
216     ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
217     *repeat = input;
218     *_aidl_return = input;
219     return Status::ok();
220   }
221 
RepeatGenericParcelable(const GenericStructuredParcelable<int32_t,StructuredParcelable,IntEnum> & input,GenericStructuredParcelable<int32_t,StructuredParcelable,IntEnum> * repeat,GenericStructuredParcelable<int32_t,StructuredParcelable,IntEnum> * _aidl_return)222   Status RepeatGenericParcelable(
223       const GenericStructuredParcelable<int32_t, StructuredParcelable, IntEnum>& input,
224       GenericStructuredParcelable<int32_t, StructuredParcelable, IntEnum>* repeat,
225       GenericStructuredParcelable<int32_t, StructuredParcelable, IntEnum>* _aidl_return) {
226     ALOGI("Repeating Generic Parcelable");
227     *repeat = input;
228     *_aidl_return = input;
229     return Status::ok();
230   }
231 
RepeatPersistableBundle(const PersistableBundle & input,PersistableBundle * _aidl_return)232   Status RepeatPersistableBundle(const PersistableBundle& input,
233                                  PersistableBundle* _aidl_return) override {
234     ALOGI("Repeated a PersistableBundle");
235     *_aidl_return = input;
236     return Status::ok();
237   }
238 
ReverseSimpleParcelables(const vector<SimpleParcelable> & input,vector<SimpleParcelable> * repeated,vector<SimpleParcelable> * _aidl_return)239   Status ReverseSimpleParcelables(const vector<SimpleParcelable>& input,
240                                   vector<SimpleParcelable>* repeated,
241                                   vector<SimpleParcelable>* _aidl_return) override {
242     return ReverseArray(input, repeated, _aidl_return);
243   }
ReversePersistableBundles(const vector<PersistableBundle> & input,vector<PersistableBundle> * repeated,vector<PersistableBundle> * _aidl_return)244   Status ReversePersistableBundles(const vector<PersistableBundle>& input,
245                                    vector<PersistableBundle>* repeated,
246                                    vector<PersistableBundle>* _aidl_return) override {
247     return ReverseArray(input, repeated, _aidl_return);
248   }
ReverseUnion(const Union & input,Union * repeated,Union * _aidl_return)249   Status ReverseUnion(const Union& input, Union* repeated, Union* _aidl_return) override {
250     ALOGI("Repeated a Union");
251     *repeated = input;
252     *_aidl_return = input;
253     auto reverse = [](auto& reversible) {
254       std::reverse(std::begin(reversible), std::end(reversible));
255     };
256     switch (input.getTag()) {
257       case Union::ns:  // int[]
258         reverse(_aidl_return->get<Union::ns>());
259         break;
260       case Union::s:  // String
261         reverse(_aidl_return->get<Union::s>());
262         break;
263       case Union::ss:  // List<String>
264         reverse(_aidl_return->get<Union::ss>());
265         break;
266       default:
267         break;
268     }
269     return Status::ok();
270   }
ReverseNamedCallbackList(const vector<sp<IBinder>> & input,vector<sp<IBinder>> * repeated,vector<sp<IBinder>> * _aidl_return)271   Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input, vector<sp<IBinder>>* repeated,
272                                   vector<sp<IBinder>>* _aidl_return) override {
273     return ReverseArray(input, repeated, _aidl_return);
274   }
275 
RepeatFileDescriptor(unique_fd read,unique_fd * _aidl_return)276   Status RepeatFileDescriptor(unique_fd read, unique_fd* _aidl_return) override {
277     ALOGE("Repeating file descriptor");
278     *_aidl_return = unique_fd(dup(read.get()));
279     return Status::ok();
280   }
281 
ReverseFileDescriptorArray(const vector<unique_fd> & input,vector<unique_fd> * repeated,vector<unique_fd> * _aidl_return)282   Status ReverseFileDescriptorArray(const vector<unique_fd>& input, vector<unique_fd>* repeated,
283                                     vector<unique_fd>* _aidl_return) override {
284     ALOGI("Reversing descriptor array of length %zu", input.size());
285     repeated->clear();
286     for (const auto& item : input) {
287       repeated->push_back(unique_fd(dup(item.get())));
288       _aidl_return->push_back(unique_fd(dup(item.get())));
289     }
290     std::reverse(_aidl_return->begin(), _aidl_return->end());
291     return Status::ok();
292   }
293 };
294 
295 class NativeService : public BnTestService {
296  public:
NativeService()297   NativeService() {}
298   virtual ~NativeService() = default;
299 
LogRepeatedStringToken(const String16 & token)300   void LogRepeatedStringToken(const String16& token) {
301     ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
302           token.size());
303   }
304 
305   template <typename T>
LogRepeatedToken(const T & token)306   void LogRepeatedToken(const T& token) {
307     std::ostringstream token_str;
308     token_str << token;
309     ALOGI("Repeating token %s", token_str.str().c_str());
310   }
311 
TestOneway()312   Status TestOneway() override { return Status::fromStatusT(android::UNKNOWN_ERROR); }
313 
Deprecated()314   Status Deprecated() override { return Status::ok(); }
315 
RepeatBoolean(bool token,bool * _aidl_return)316   Status RepeatBoolean(bool token, bool* _aidl_return) override {
317     LogRepeatedToken(token ? 1 : 0);
318     *_aidl_return = token;
319     return Status::ok();
320   }
RepeatByte(int8_t token,int8_t * _aidl_return)321   Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
322     LogRepeatedToken(token);
323     *_aidl_return = token;
324     return Status::ok();
325   }
RepeatChar(char16_t token,char16_t * _aidl_return)326   Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
327     LogRepeatedStringToken(String16(&token, 1));
328     *_aidl_return = token;
329     return Status::ok();
330   }
RepeatInt(int32_t token,int32_t * _aidl_return)331   Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
332     LogRepeatedToken(token);
333     *_aidl_return = token;
334     return Status::ok();
335   }
RepeatLong(int64_t token,int64_t * _aidl_return)336   Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
337     LogRepeatedToken(token);
338     *_aidl_return = token;
339     return Status::ok();
340   }
RepeatFloat(float token,float * _aidl_return)341   Status RepeatFloat(float token, float* _aidl_return) override {
342     LogRepeatedToken(token);
343     *_aidl_return = token;
344     return Status::ok();
345   }
RepeatDouble(double token,double * _aidl_return)346   Status RepeatDouble(double token, double* _aidl_return) override {
347     LogRepeatedToken(token);
348     *_aidl_return = token;
349     return Status::ok();
350   }
RepeatString(const String16 & token,String16 * _aidl_return)351   Status RepeatString(const String16& token, String16* _aidl_return) override {
352     LogRepeatedStringToken(token);
353     *_aidl_return = token;
354     return Status::ok();
355   }
RepeatByteEnum(ByteEnum token,ByteEnum * _aidl_return)356   Status RepeatByteEnum(ByteEnum token, ByteEnum* _aidl_return) override {
357     ALOGI("Repeating ByteEnum token %s", toString(token).c_str());
358     *_aidl_return = token;
359     return Status::ok();
360   }
RepeatIntEnum(IntEnum token,IntEnum * _aidl_return)361   Status RepeatIntEnum(IntEnum token, IntEnum* _aidl_return) override {
362     ALOGI("Repeating IntEnum token %s", toString(token).c_str());
363     *_aidl_return = token;
364     return Status::ok();
365   }
RepeatLongEnum(LongEnum token,LongEnum * _aidl_return)366   Status RepeatLongEnum(LongEnum token, LongEnum* _aidl_return) override {
367     ALOGI("Repeating LongEnum token %s", toString(token).c_str());
368     *_aidl_return = token;
369     return Status::ok();
370   }
371 
ReverseBoolean(const vector<bool> & input,vector<bool> * repeated,vector<bool> * _aidl_return)372   Status ReverseBoolean(const vector<bool>& input,
373                         vector<bool>* repeated,
374                         vector<bool>* _aidl_return) override {
375     return ReverseArray(input, repeated, _aidl_return);
376   }
ReverseByte(const vector<uint8_t> & input,vector<uint8_t> * repeated,vector<uint8_t> * _aidl_return)377   Status ReverseByte(const vector<uint8_t>& input,
378                      vector<uint8_t>* repeated,
379                      vector<uint8_t>* _aidl_return) override {
380     return ReverseArray(input, repeated, _aidl_return);
381   }
ReverseChar(const vector<char16_t> & input,vector<char16_t> * repeated,vector<char16_t> * _aidl_return)382   Status ReverseChar(const vector<char16_t>& input,
383                      vector<char16_t>* repeated,
384                      vector<char16_t>* _aidl_return) override {
385     return ReverseArray(input, repeated, _aidl_return);
386   }
ReverseInt(const vector<int32_t> & input,vector<int32_t> * repeated,vector<int32_t> * _aidl_return)387   Status ReverseInt(const vector<int32_t>& input,
388                     vector<int32_t>* repeated,
389                     vector<int32_t>* _aidl_return) override {
390     return ReverseArray(input, repeated, _aidl_return);
391   }
ReverseLong(const vector<int64_t> & input,vector<int64_t> * repeated,vector<int64_t> * _aidl_return)392   Status ReverseLong(const vector<int64_t>& input,
393                      vector<int64_t>* repeated,
394                      vector<int64_t>* _aidl_return) override {
395     return ReverseArray(input, repeated, _aidl_return);
396   }
ReverseFloat(const vector<float> & input,vector<float> * repeated,vector<float> * _aidl_return)397   Status ReverseFloat(const vector<float>& input,
398                       vector<float>* repeated,
399                       vector<float>* _aidl_return) override {
400     return ReverseArray(input, repeated, _aidl_return);
401   }
ReverseDouble(const vector<double> & input,vector<double> * repeated,vector<double> * _aidl_return)402   Status ReverseDouble(const vector<double>& input,
403                        vector<double>* repeated,
404                        vector<double>* _aidl_return) override {
405     return ReverseArray(input, repeated, _aidl_return);
406   }
ReverseString(const vector<String16> & input,vector<String16> * repeated,vector<String16> * _aidl_return)407   Status ReverseString(const vector<String16>& input,
408                        vector<String16>* repeated,
409                        vector<String16>* _aidl_return) override {
410     return ReverseArray(input, repeated, _aidl_return);
411   }
ReverseByteEnum(const vector<ByteEnum> & input,vector<ByteEnum> * repeated,vector<ByteEnum> * _aidl_return)412   Status ReverseByteEnum(const vector<ByteEnum>& input, vector<ByteEnum>* repeated,
413                          vector<ByteEnum>* _aidl_return) override {
414     return ReverseArray(input, repeated, _aidl_return);
415   }
ReverseIntEnum(const vector<IntEnum> & input,vector<IntEnum> * repeated,vector<IntEnum> * _aidl_return)416   Status ReverseIntEnum(const vector<IntEnum>& input, vector<IntEnum>* repeated,
417                         vector<IntEnum>* _aidl_return) override {
418     return ReverseArray(input, repeated, _aidl_return);
419   }
ReverseLongEnum(const vector<LongEnum> & input,vector<LongEnum> * repeated,vector<LongEnum> * _aidl_return)420   Status ReverseLongEnum(const vector<LongEnum>& input, vector<LongEnum>* repeated,
421                          vector<LongEnum>* _aidl_return) override {
422     return ReverseArray(input, repeated, _aidl_return);
423   }
424 
SetOtherTestService(const String16 & name,const sp<INamedCallback> & service,bool * _aidl_return)425   Status SetOtherTestService(const String16& name, const sp<INamedCallback>& service,
426                              bool* _aidl_return) override {
427     std::lock_guard<std::mutex> guard(service_map_mutex_);
428     const auto& existing = service_map_.find(name);
429     if (existing != service_map_.end() && existing->second == service) {
430       *_aidl_return = true;
431 
432       return Status::ok();
433     } else {
434       *_aidl_return = false;
435       service_map_[name] = service;
436 
437       return Status::ok();
438     }
439   }
440 
GetOtherTestService(const String16 & name,sp<INamedCallback> * returned_service)441   Status GetOtherTestService(const String16& name,
442                              sp<INamedCallback>* returned_service) override {
443     std::lock_guard<std::mutex> guard(service_map_mutex_);
444     if (service_map_.find(name) == service_map_.end()) {
445       sp<INamedCallback> new_item(new NamedCallback(name));
446       service_map_[name] = new_item;
447     }
448 
449     *returned_service = service_map_[name];
450     return Status::ok();
451   }
452 
VerifyName(const sp<INamedCallback> & service,const String16 & name,bool * returned_value)453   Status VerifyName(const sp<INamedCallback>& service, const String16& name,
454                     bool* returned_value) override {
455     String16 foundName;
456     Status status = service->GetName(&foundName);
457 
458     if (status.isOk()) {
459       *returned_value = foundName == name;
460     }
461 
462     return status;
463   }
464 
GetInterfaceArray(const vector<String16> & names,vector<sp<INamedCallback>> * _aidl_return)465   Status GetInterfaceArray(const vector<String16>& names,
466                            vector<sp<INamedCallback>>* _aidl_return) override {
467     vector<sp<INamedCallback>> services(names.size());
468     for (size_t i = 0; i < names.size(); i++) {
469       if (auto st = GetOtherTestService(names[i], &services[i]); !st.isOk()) {
470         return st;
471       }
472     }
473     *_aidl_return = std::move(services);
474     return Status::ok();
475   }
476 
VerifyNamesWithInterfaceArray(const vector<sp<INamedCallback>> & services,const vector<String16> & names,bool * _aidl_ret)477   Status VerifyNamesWithInterfaceArray(const vector<sp<INamedCallback>>& services,
478                                        const vector<String16>& names, bool* _aidl_ret) override {
479     if (services.size() == names.size()) {
480       for (size_t i = 0; i < services.size(); i++) {
481         if (auto st = VerifyName(services[i], names[i], _aidl_ret); !st.isOk() || !*_aidl_ret) {
482           return st;
483         }
484       }
485       *_aidl_ret = true;
486     } else {
487       *_aidl_ret = false;
488     }
489     return Status::ok();
490   }
491 
GetNullableInterfaceArray(const optional<vector<optional<String16>>> & names,optional<vector<sp<INamedCallback>>> * _aidl_ret)492   Status GetNullableInterfaceArray(const optional<vector<optional<String16>>>& names,
493                                    optional<vector<sp<INamedCallback>>>* _aidl_ret) override {
494     vector<sp<INamedCallback>> services;
495     if (names.has_value()) {
496       for (const auto& name : *names) {
497         if (name.has_value()) {
498           sp<INamedCallback> ret;
499           if (auto st = GetOtherTestService(*name, &ret); !st.isOk()) {
500             return st;
501           }
502           services.push_back(std::move(ret));
503         } else {
504           services.emplace_back();
505         }
506       }
507     }
508     *_aidl_ret = std::move(services);
509     return Status::ok();
510   }
511 
VerifyNamesWithNullableInterfaceArray(const optional<vector<sp<INamedCallback>>> & services,const optional<vector<optional<String16>>> & names,bool * _aidl_ret)512   Status VerifyNamesWithNullableInterfaceArray(const optional<vector<sp<INamedCallback>>>& services,
513                                                const optional<vector<optional<String16>>>& names,
514                                                bool* _aidl_ret) override {
515     if (services.has_value() && names.has_value()) {
516       if (services->size() == names->size()) {
517         for (size_t i = 0; i < services->size(); i++) {
518           if (services->at(i).get() && names->at(i).has_value()) {
519             if (auto st = VerifyName(services->at(i), names->at(i).value(), _aidl_ret);
520                 !st.isOk() || !*_aidl_ret) {
521               return st;
522             }
523           } else if (services->at(i).get() || names->at(i).has_value()) {
524             *_aidl_ret = false;
525             return Status::ok();
526           } else {
527             // ok if service=null && name=null
528           }
529         }
530         *_aidl_ret = true;
531       } else {
532         *_aidl_ret = false;
533       }
534     } else {
535       *_aidl_ret = services.has_value() == names.has_value();
536     }
537     return Status::ok();
538   }
539 
GetInterfaceList(const optional<vector<optional<String16>>> & names,optional<vector<sp<INamedCallback>>> * _aidl_ret)540   Status GetInterfaceList(const optional<vector<optional<String16>>>& names,
541                           optional<vector<sp<INamedCallback>>>* _aidl_ret) override {
542     return GetNullableInterfaceArray(names, _aidl_ret);
543   }
544 
VerifyNamesWithInterfaceList(const optional<vector<sp<INamedCallback>>> & services,const optional<vector<optional<String16>>> & names,bool * _aidl_ret)545   Status VerifyNamesWithInterfaceList(const optional<vector<sp<INamedCallback>>>& services,
546                                       const optional<vector<optional<String16>>>& names,
547                                       bool* _aidl_ret) override {
548     return VerifyNamesWithNullableInterfaceArray(services, names, _aidl_ret);
549   }
550 
ReverseStringList(const vector<String16> & input,vector<String16> * repeated,vector<String16> * _aidl_return)551   Status ReverseStringList(const vector<String16>& input,
552                            vector<String16>* repeated,
553                            vector<String16>* _aidl_return) override {
554     return ReverseArray(input, repeated, _aidl_return);
555   }
556 
RepeatParcelFileDescriptor(const ParcelFileDescriptor & read,ParcelFileDescriptor * _aidl_return)557   Status RepeatParcelFileDescriptor(const ParcelFileDescriptor& read,
558                                     ParcelFileDescriptor* _aidl_return) override {
559     ALOGE("Repeating parcel file descriptor");
560     _aidl_return->reset(unique_fd(dup(read.get())));
561     return Status::ok();
562   }
563 
ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor> & input,vector<ParcelFileDescriptor> * repeated,vector<ParcelFileDescriptor> * _aidl_return)564   Status ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor>& input,
565                                           vector<ParcelFileDescriptor>* repeated,
566                                           vector<ParcelFileDescriptor>* _aidl_return) override {
567     ALOGI("Reversing parcel descriptor array of length %zu", input.size());
568     for (const auto& item : input) {
569       repeated->push_back(ParcelFileDescriptor(unique_fd(dup(item.get()))));
570     }
571 
572     for (auto i = input.rbegin(); i != input.rend(); i++) {
573       _aidl_return->push_back(ParcelFileDescriptor(unique_fd(dup(i->get()))));
574     }
575     return Status::ok();
576   }
577 
ThrowServiceException(int code)578   Status ThrowServiceException(int code) override {
579     return Status::fromServiceSpecificError(code);
580   }
581 
RepeatNullableIntArray(const optional<vector<int32_t>> & input,optional<vector<int32_t>> * _aidl_return)582   Status RepeatNullableIntArray(const optional<vector<int32_t>>& input,
583                                 optional<vector<int32_t>>* _aidl_return) {
584     return RepeatNullable(input, _aidl_return);
585   }
586 
RepeatNullableByteEnumArray(const optional<vector<ByteEnum>> & input,optional<vector<ByteEnum>> * _aidl_return)587   Status RepeatNullableByteEnumArray(const optional<vector<ByteEnum>>& input,
588                                      optional<vector<ByteEnum>>* _aidl_return) {
589     return RepeatNullable(input, _aidl_return);
590   }
591 
RepeatNullableIntEnumArray(const optional<vector<IntEnum>> & input,optional<vector<IntEnum>> * _aidl_return)592   Status RepeatNullableIntEnumArray(const optional<vector<IntEnum>>& input,
593                                     optional<vector<IntEnum>>* _aidl_return) {
594     return RepeatNullable(input, _aidl_return);
595   }
596 
RepeatNullableLongEnumArray(const optional<vector<LongEnum>> & input,optional<vector<LongEnum>> * _aidl_return)597   Status RepeatNullableLongEnumArray(const optional<vector<LongEnum>>& input,
598                                      optional<vector<LongEnum>>* _aidl_return) {
599     return RepeatNullable(input, _aidl_return);
600   }
601 
RepeatNullableStringList(const optional<vector<optional<String16>>> & input,optional<vector<optional<String16>>> * _aidl_return)602   Status RepeatNullableStringList(const optional<vector<optional<String16>>>& input,
603                                   optional<vector<optional<String16>>>* _aidl_return) {
604     ALOGI("Repeating nullable string list");
605     return RepeatNullable(input, _aidl_return);
606   }
607 
RepeatNullableString(const optional<String16> & input,optional<String16> * _aidl_return)608   Status RepeatNullableString(const optional<String16>& input, optional<String16>* _aidl_return) {
609     return RepeatNullable(input, _aidl_return);
610   }
611 
RepeatNullableParcelable(const optional<ITestService::Empty> & input,optional<ITestService::Empty> * _aidl_return)612   Status RepeatNullableParcelable(const optional<ITestService::Empty>& input,
613                                   optional<ITestService::Empty>* _aidl_return) {
614     return RepeatNullable(input, _aidl_return);
615   }
616 
RepeatNullableParcelableList(const optional<vector<optional<ITestService::Empty>>> & input,optional<vector<optional<ITestService::Empty>>> * _aidl_return)617   Status RepeatNullableParcelableList(
618       const optional<vector<optional<ITestService::Empty>>>& input,
619       optional<vector<optional<ITestService::Empty>>>* _aidl_return) {
620     return RepeatNullable(input, _aidl_return);
621   }
622 
RepeatNullableParcelableArray(const optional<vector<optional<ITestService::Empty>>> & input,optional<vector<optional<ITestService::Empty>>> * _aidl_return)623   Status RepeatNullableParcelableArray(
624       const optional<vector<optional<ITestService::Empty>>>& input,
625       optional<vector<optional<ITestService::Empty>>>* _aidl_return) {
626     return RepeatNullable(input, _aidl_return);
627   }
628 
TakesAnIBinder(const sp<IBinder> & input)629   Status TakesAnIBinder(const sp<IBinder>& input) override {
630     (void)input;
631     return Status::ok();
632   }
TakesANullableIBinder(const sp<IBinder> & input)633   Status TakesANullableIBinder(const sp<IBinder>& input) {
634     (void)input;
635     return Status::ok();
636   }
TakesAnIBinderList(const vector<sp<IBinder>> & input)637   Status TakesAnIBinderList(const vector<sp<IBinder>>& input) override {
638     (void)input;
639     return Status::ok();
640   }
TakesANullableIBinderList(const optional<vector<sp<IBinder>>> & input)641   Status TakesANullableIBinderList(const optional<vector<sp<IBinder>>>& input) {
642     (void)input;
643     return Status::ok();
644   }
645 
RepeatUtf8CppString(const string & token,string * _aidl_return)646   Status RepeatUtf8CppString(const string& token,
647                              string* _aidl_return) override {
648     ALOGI("Repeating utf8 string '%s' of length=%zu", token.c_str(), token.size());
649     *_aidl_return = token;
650     return Status::ok();
651   }
652 
RepeatNullableUtf8CppString(const optional<string> & token,optional<string> * _aidl_return)653   Status RepeatNullableUtf8CppString(const optional<string>& token,
654                                      optional<string>* _aidl_return) override {
655     if (!token) {
656       ALOGI("Received null @utf8InCpp string");
657       return Status::ok();
658     }
659     ALOGI("Repeating utf8 string '%s' of length=%zu",
660           token->c_str(), token->size());
661     *_aidl_return = token;
662     return Status::ok();
663   }
664 
ReverseUtf8CppString(const vector<string> & input,vector<string> * repeated,vector<string> * _aidl_return)665   Status ReverseUtf8CppString(const vector<string>& input,
666                               vector<string>* repeated,
667                               vector<string>* _aidl_return) {
668     return ReverseArray(input, repeated, _aidl_return);
669   }
670 
ReverseNullableUtf8CppString(const optional<vector<optional<string>>> & input,optional<vector<optional<string>>> * repeated,optional<vector<optional<string>>> * _aidl_return)671   Status ReverseNullableUtf8CppString(const optional<vector<optional<string>>>& input,
672                                       optional<vector<optional<string>>>* repeated,
673                                       optional<vector<optional<string>>>* _aidl_return) {
674     return ReverseUtf8CppStringList(input, repeated, _aidl_return);
675   }
676 
ReverseUtf8CppStringList(const optional<vector<optional<string>>> & input,optional<vector<optional<string>>> * repeated,optional<vector<optional<string>>> * _aidl_return)677   Status ReverseUtf8CppStringList(const optional<vector<optional<string>>>& input,
678                                   optional<vector<optional<string>>>* repeated,
679                                   optional<vector<optional<string>>>* _aidl_return) {
680     if (!input) {
681       ALOGI("Received null list of utf8 strings");
682       return Status::ok();
683     }
684     *_aidl_return = input;
685     *repeated = input;
686     std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end());
687     return Status::ok();
688   }
689 
GetCallback(bool return_null,sp<INamedCallback> * ret)690   Status GetCallback(bool return_null, sp<INamedCallback>* ret) {
691     if (!return_null) {
692       return GetOtherTestService(String16("ABT: always be testing"), ret);
693     }
694     return Status::ok();
695   }
696 
FillOutStructuredParcelable(StructuredParcelable * parcelable)697   virtual ::android::binder::Status FillOutStructuredParcelable(StructuredParcelable* parcelable) {
698     parcelable->shouldBeJerry = "Jerry";
699     parcelable->shouldContainThreeFs = {parcelable->f, parcelable->f, parcelable->f};
700     parcelable->shouldBeByteBar = ByteEnum::BAR;
701     parcelable->shouldBeIntBar = IntEnum::BAR;
702     parcelable->shouldBeLongBar = LongEnum::BAR;
703     parcelable->shouldContainTwoByteFoos = {ByteEnum::FOO, ByteEnum::FOO};
704     parcelable->shouldContainTwoIntFoos = {IntEnum::FOO, IntEnum::FOO};
705     parcelable->shouldContainTwoLongFoos = {LongEnum::FOO, LongEnum::FOO};
706 
707     parcelable->const_exprs_1 = ConstantExpressionEnum::decInt32_1;
708     parcelable->const_exprs_2 = ConstantExpressionEnum::decInt32_2;
709     parcelable->const_exprs_3 = ConstantExpressionEnum::decInt64_1;
710     parcelable->const_exprs_4 = ConstantExpressionEnum::decInt64_2;
711     parcelable->const_exprs_5 = ConstantExpressionEnum::decInt64_3;
712     parcelable->const_exprs_6 = ConstantExpressionEnum::decInt64_4;
713     parcelable->const_exprs_7 = ConstantExpressionEnum::hexInt32_1;
714     parcelable->const_exprs_8 = ConstantExpressionEnum::hexInt32_2;
715     parcelable->const_exprs_9 = ConstantExpressionEnum::hexInt32_3;
716     parcelable->const_exprs_10 = ConstantExpressionEnum::hexInt64_1;
717 
718     parcelable->shouldSetBit0AndBit2 = StructuredParcelable::BIT0 | StructuredParcelable::BIT2;
719 
720     parcelable->u = Union::make<Union::ns>({1, 2, 3});
721     parcelable->shouldBeConstS1 = Union::S1();
722     return Status::ok();
723   }
724 
RepeatExtendableParcelable(const::android::aidl::tests::extension::ExtendableParcelable & ep,::android::aidl::tests::extension::ExtendableParcelable * ep2)725   ::android::binder::Status RepeatExtendableParcelable(
726       const ::android::aidl::tests::extension::ExtendableParcelable& ep,
727       ::android::aidl::tests::extension::ExtendableParcelable* ep2) {
728     ep2->a = ep.a;
729     ep2->b = ep.b;
730     std::shared_ptr<android::aidl::tests::extension::MyExt> myExt;
731     ep.ext.getParcelable(&myExt);
732     ep2->ext.setParcelable(myExt);
733 
734     return Status::ok();
735   }
736 
ReverseList(const RecursiveList & list,RecursiveList * ret)737   ::android::binder::Status ReverseList(const RecursiveList& list, RecursiveList* ret) override {
738     std::unique_ptr<RecursiveList> reversed;
739     const RecursiveList* cur = &list;
740     while (cur) {
741       auto node = std::make_unique<RecursiveList>();
742       node->value = cur->value;
743       node->next = std::move(reversed);
744       reversed = std::move(node);
745       cur = cur->next.get();
746     }
747     *ret = std::move(*reversed);
748     return Status::ok();
749   }
750 
ReverseIBinderArray(const vector<sp<IBinder>> & input,vector<sp<IBinder>> * repeated,vector<sp<IBinder>> * _aidl_return)751   Status ReverseIBinderArray(const vector<sp<IBinder>>& input, vector<sp<IBinder>>* repeated,
752                              vector<sp<IBinder>>* _aidl_return) override {
753     *repeated = input;
754     *_aidl_return = input;
755     std::reverse(_aidl_return->begin(), _aidl_return->end());
756     return Status::ok();
757   }
758 
ReverseNullableIBinderArray(const std::optional<vector<sp<IBinder>>> & input,std::optional<vector<sp<IBinder>>> * repeated,std::optional<vector<sp<IBinder>>> * _aidl_return)759   Status ReverseNullableIBinderArray(const std::optional<vector<sp<IBinder>>>& input,
760                                      std::optional<vector<sp<IBinder>>>* repeated,
761                                      std::optional<vector<sp<IBinder>>>* _aidl_return) override {
762     *repeated = input;
763     *_aidl_return = input;
764     if (*_aidl_return) {
765       std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end());
766     }
767     return Status::ok();
768   }
769 
UnimplementedMethod(int32_t,int32_t *)770   Status UnimplementedMethod(int32_t /* arg */, int32_t* /* _aidl_return */) override {
771     LOG_ALWAYS_FATAL("UnimplementedMethod shouldn't be called");
772   }
773 
GetOldNameInterface(sp<IOldName> * ret)774   Status GetOldNameInterface(sp<IOldName>* ret) {
775     *ret = new OldName;
776     return Status::ok();
777   }
778 
GetNewNameInterface(sp<INewName> * ret)779   Status GetNewNameInterface(sp<INewName>* ret) {
780     *ret = new NewName;
781     return Status::ok();
782   }
783 
GetUnionTags(const std::vector<Union> & input,std::vector<Union::Tag> * _aidl_return)784   Status GetUnionTags(const std::vector<Union>& input,
785                       std::vector<Union::Tag>* _aidl_return) override {
786     std::vector<Union::Tag> tags;
787     std::transform(input.begin(), input.end(), std::back_inserter(tags),
788                    std::mem_fn(&Union::getTag));
789     *_aidl_return = std::move(tags);
790     return Status::ok();
791   }
792 
GetCppJavaTests(sp<IBinder> * ret)793   Status GetCppJavaTests(sp<IBinder>* ret) {
794     *ret = new CppJavaTests;
795     return Status::ok();
796   }
797 
getBackendType(BackendType * _aidl_return)798   Status getBackendType(BackendType* _aidl_return) override {
799     *_aidl_return = BackendType::CPP;
800     return Status::ok();
801   }
802 
GetCircular(CircularParcelable * cp,sp<ICircular> * _aidl_return)803   Status GetCircular(CircularParcelable* cp, sp<ICircular>* _aidl_return) override {
804     auto srv = sp<ITestService>::fromExisting(this);
805     cp->testService = srv;
806     *_aidl_return = new Circular(srv);
807     return Status::ok();
808   }
809 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)810   android::status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
811                                uint32_t flags) override {
812     if (code == ::android::IBinder::FIRST_CALL_TRANSACTION + 0 /* UnimplementedMethod */) {
813       // pretend that UnimplementedMethod isn't implemented by this service.
814       return android::UNKNOWN_TRANSACTION;
815     } else {
816       return BnTestService::onTransact(code, data, reply, flags);
817     }
818   }
819 
820  private:
821   map<String16, sp<INamedCallback>> service_map_;
822   std::mutex service_map_mutex_;
823 };
824 
825 class VersionedService : public android::aidl::versioned::tests::BnFooInterface {
826  public:
VersionedService()827   VersionedService() {}
828   virtual ~VersionedService() = default;
829 
originalApi()830   Status originalApi() override { return Status::ok(); }
acceptUnionAndReturnString(const::android::aidl::versioned::tests::BazUnion & u,std::string * _aidl_return)831   Status acceptUnionAndReturnString(const ::android::aidl::versioned::tests::BazUnion& u,
832                                     std::string* _aidl_return) override {
833     switch (u.getTag()) {
834       case ::android::aidl::versioned::tests::BazUnion::intNum:
835         *_aidl_return =
836             std::to_string(u.get<::android::aidl::versioned::tests::BazUnion::intNum>());
837         break;
838     }
839     return Status::ok();
840   }
returnsLengthOfFooArray(const vector<::android::aidl::versioned::tests::Foo> & foos,int32_t * ret)841   Status returnsLengthOfFooArray(const vector<::android::aidl::versioned::tests::Foo>& foos,
842                                  int32_t* ret) override {
843     *ret = static_cast<int32_t>(foos.size());
844     return Status::ok();
845   }
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)846   Status ignoreParcelablesAndRepeatInt(const ::android::aidl::versioned::tests::Foo& inFoo,
847                                        ::android::aidl::versioned::tests::Foo* inoutFoo,
848                                        ::android::aidl::versioned::tests::Foo* outFoo,
849                                        int32_t value, int32_t* ret) override {
850     (void)inFoo;
851     (void)inoutFoo;
852     (void)outFoo;
853     *ret = value;
854     return Status::ok();
855   }
856 };
857 
858 class LoggableInterfaceService : public android::aidl::loggable::BnLoggableInterface {
859  public:
LoggableInterfaceService()860   LoggableInterfaceService() {}
861   virtual ~LoggableInterfaceService() = default;
862 
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)863   virtual Status LogThis(bool, vector<bool>*, int8_t, vector<uint8_t>*, char16_t, vector<char16_t>*,
864                          int32_t, vector<int32_t>*, int64_t, vector<int64_t>*, float,
865                          vector<float>*, double, vector<double>*, const String16&,
866                          vector<String16>*, vector<String16>*, const android::aidl::loggable::Data&,
867                          const sp<IBinder>&, optional<ParcelFileDescriptor>*,
868                          vector<ParcelFileDescriptor>*, vector<String16>* _aidl_return) override {
869     *_aidl_return = vector<String16>{String16("loggable")};
870     return Status::ok();
871   }
872 };
873 
874 using namespace android::aidl::tests::nested;
875 class NestedService : public BnNestedService {
876  public:
NestedService()877   NestedService() {}
878   virtual ~NestedService() = default;
879 
flipStatus(const ParcelableWithNested & p,INestedService::Result * _aidl_return)880   virtual Status flipStatus(const ParcelableWithNested& p, INestedService::Result* _aidl_return) {
881     if (p.status == ParcelableWithNested::Status::OK) {
882       _aidl_return->status = ParcelableWithNested::Status::NOT_OK;
883     } else {
884       _aidl_return->status = ParcelableWithNested::Status::OK;
885     }
886     return Status::ok();
887   }
flipStatusWithCallback(ParcelableWithNested::Status status,const sp<INestedService::ICallback> & cb)888   virtual Status flipStatusWithCallback(ParcelableWithNested::Status status,
889                                         const sp<INestedService::ICallback>& cb) {
890     if (status == ParcelableWithNested::Status::OK) {
891       return cb->done(ParcelableWithNested::Status::NOT_OK);
892     } else {
893       return cb->done(ParcelableWithNested::Status::OK);
894     }
895   }
896 };
897 
898 using android::aidl::fixedsizearray::FixedSizeArrayExample;
899 class FixedSizeArrayService : public FixedSizeArrayExample::BnRepeatFixedSizeArray {
900  public:
FixedSizeArrayService()901   FixedSizeArrayService() {}
902   virtual ~FixedSizeArrayService() = default;
903 
RepeatBytes(const std::array<uint8_t,3> & in_input,std::array<uint8_t,3> * out_repeated,std::array<uint8_t,3> * _aidl_return)904   Status RepeatBytes(const std::array<uint8_t, 3>& in_input, std::array<uint8_t, 3>* out_repeated,
905                      std::array<uint8_t, 3>* _aidl_return) override {
906     *out_repeated = in_input;
907     *_aidl_return = in_input;
908     return Status::ok();
909   }
RepeatInts(const std::array<int32_t,3> & in_input,std::array<int32_t,3> * out_repeated,std::array<int32_t,3> * _aidl_return)910   Status RepeatInts(const std::array<int32_t, 3>& in_input, std::array<int32_t, 3>* out_repeated,
911                     std::array<int32_t, 3>* _aidl_return) override {
912     *out_repeated = in_input;
913     *_aidl_return = in_input;
914     return Status::ok();
915   }
RepeatBinders(const std::array<sp<IBinder>,3> & in_input,std::array<sp<IBinder>,3> * out_repeated,std::array<sp<IBinder>,3> * _aidl_return)916   Status RepeatBinders(const std::array<sp<IBinder>, 3>& in_input,
917                        std::array<sp<IBinder>, 3>* out_repeated,
918                        std::array<sp<IBinder>, 3>* _aidl_return) override {
919     *out_repeated = in_input;
920     *_aidl_return = in_input;
921     return Status::ok();
922   }
RepeatParcelables(const std::array<FixedSizeArrayExample::IntParcelable,3> & in_input,std::array<FixedSizeArrayExample::IntParcelable,3> * out_repeated,std::array<FixedSizeArrayExample::IntParcelable,3> * _aidl_return)923   Status RepeatParcelables(
924       const std::array<FixedSizeArrayExample::IntParcelable, 3>& in_input,
925       std::array<FixedSizeArrayExample::IntParcelable, 3>* out_repeated,
926       std::array<FixedSizeArrayExample::IntParcelable, 3>* _aidl_return) override {
927     *out_repeated = in_input;
928     *_aidl_return = in_input;
929     return Status::ok();
930   }
Repeat2dBytes(const std::array<std::array<uint8_t,3>,2> & in_input,std::array<std::array<uint8_t,3>,2> * out_repeated,std::array<std::array<uint8_t,3>,2> * _aidl_return)931   Status Repeat2dBytes(const std::array<std::array<uint8_t, 3>, 2>& in_input,
932                        std::array<std::array<uint8_t, 3>, 2>* out_repeated,
933                        std::array<std::array<uint8_t, 3>, 2>* _aidl_return) override {
934     *out_repeated = in_input;
935     *_aidl_return = in_input;
936     return Status::ok();
937   }
Repeat2dInts(const std::array<std::array<int32_t,3>,2> & in_input,std::array<std::array<int32_t,3>,2> * out_repeated,std::array<std::array<int32_t,3>,2> * _aidl_return)938   Status Repeat2dInts(const std::array<std::array<int32_t, 3>, 2>& in_input,
939                       std::array<std::array<int32_t, 3>, 2>* out_repeated,
940                       std::array<std::array<int32_t, 3>, 2>* _aidl_return) override {
941     *out_repeated = in_input;
942     *_aidl_return = in_input;
943     return Status::ok();
944   }
Repeat2dBinders(const std::array<std::array<sp<IBinder>,3>,2> & in_input,std::array<std::array<sp<IBinder>,3>,2> * out_repeated,std::array<std::array<sp<IBinder>,3>,2> * _aidl_return)945   Status Repeat2dBinders(const std::array<std::array<sp<IBinder>, 3>, 2>& in_input,
946                          std::array<std::array<sp<IBinder>, 3>, 2>* out_repeated,
947                          std::array<std::array<sp<IBinder>, 3>, 2>* _aidl_return) override {
948     *out_repeated = in_input;
949     *_aidl_return = in_input;
950     return Status::ok();
951   }
Repeat2dParcelables(const std::array<std::array<FixedSizeArrayExample::IntParcelable,3>,2> & in_input,std::array<std::array<FixedSizeArrayExample::IntParcelable,3>,2> * out_repeated,std::array<std::array<FixedSizeArrayExample::IntParcelable,3>,2> * _aidl_return)952   Status Repeat2dParcelables(
953       const std::array<std::array<FixedSizeArrayExample::IntParcelable, 3>, 2>& in_input,
954       std::array<std::array<FixedSizeArrayExample::IntParcelable, 3>, 2>* out_repeated,
955       std::array<std::array<FixedSizeArrayExample::IntParcelable, 3>, 2>* _aidl_return) override {
956     *out_repeated = in_input;
957     *_aidl_return = in_input;
958     return Status::ok();
959   }
960 };
961 
Run()962 int Run() {
963   android::sp<NativeService> service = new NativeService;
964   sp<Looper> looper(Looper::prepare(0 /* opts */));
965 
966   int binder_fd = -1;
967   ProcessState::self()->setThreadPoolMaxThreadCount(0);
968   IPCThreadState::self()->disableBackgroundScheduling(true);
969   IPCThreadState::self()->setupPolling(&binder_fd);
970   ALOGI("Got binder FD %d", binder_fd);
971   if (binder_fd < 0) return -1;
972 
973   sp<BinderCallback> cb(new BinderCallback);
974   if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
975                     nullptr) != 1) {
976     ALOGE("Failed to add binder FD to Looper");
977     return -1;
978   }
979 
980   auto status = defaultServiceManager()->addService(service->getInterfaceDescriptor(), service);
981   if (status != OK) {
982     ALOGE("Failed to add service %s", String8(service->getInterfaceDescriptor()).c_str());
983     return -1;
984   }
985 
986   android::sp<VersionedService> versionedService = new VersionedService;
987   status = defaultServiceManager()->addService(versionedService->getInterfaceDescriptor(),
988                                                versionedService);
989   if (status != OK) {
990     ALOGE("Failed to add service %s", String8(versionedService->getInterfaceDescriptor()).c_str());
991     return -1;
992   }
993 
994   android::sp<LoggableInterfaceService> loggableInterfaceService = new LoggableInterfaceService;
995   status = defaultServiceManager()->addService(loggableInterfaceService->getInterfaceDescriptor(),
996                                                loggableInterfaceService);
997   if (status != OK) {
998     ALOGE("Failed to add service %s",
999           String8(loggableInterfaceService->getInterfaceDescriptor()).c_str());
1000     return -1;
1001   }
1002 
1003   android::sp<NestedService> nestedService = new NestedService;
1004   status =
1005       defaultServiceManager()->addService(nestedService->getInterfaceDescriptor(), nestedService);
1006   if (status != OK) {
1007     ALOGE("Failed to add service %s", String8(nestedService->getInterfaceDescriptor()).c_str());
1008     return -1;
1009   }
1010 
1011   android::sp<FixedSizeArrayService> fixedSizeArrayService = new FixedSizeArrayService;
1012   status = defaultServiceManager()->addService(fixedSizeArrayService->getInterfaceDescriptor(),
1013                                                fixedSizeArrayService);
1014   if (status != OK) {
1015     ALOGE("Failed to add service %s",
1016           String8(fixedSizeArrayService->getInterfaceDescriptor()).c_str());
1017     return -1;
1018   }
1019 
1020   ALOGI("Entering loop");
1021   while (true) {
1022     const int result = looper->pollAll(-1 /* timeoutMillis */);
1023     ALOGI("Looper returned %d", result);
1024   }
1025   return 0;
1026 }
1027 
1028 }  // namespace
1029 
main(int,char * [])1030 int main(int /* argc */, char* /* argv */ []) {
1031   return Run();
1032 }
1033