• 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 <sstream>
19 #include <string>
20 #include <vector>
21 
22 #include <unistd.h>
23 
24 #include <android-base/unique_fd.h>
25 #include <binder/IInterface.h>
26 #include <binder/IPCThreadState.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/ProcessState.h>
29 #include <binder/Status.h>
30 #include <binder/Value.h>
31 #include <utils/Errors.h>
32 #include <utils/Log.h>
33 #include <utils/Looper.h>
34 #include <utils/StrongPointer.h>
35 
36 #include "android/aidl/tests/BnTestService.h"
37 #include "android/aidl/tests/ITestService.h"
38 
39 #include "android/aidl/tests/BnNamedCallback.h"
40 #include "android/aidl/tests/INamedCallback.h"
41 
42 // Used implicitly.
43 #undef LOG_TAG
44 #define LOG_TAG "aidl_native_service"
45 
46 // libbase
47 using android::base::unique_fd;
48 
49 // libutils:
50 using android::Looper;
51 using android::LooperCallback;
52 using android::OK;
53 using android::sp;
54 using android::String16;
55 
56 // libbinder:
57 using android::BnInterface;
58 using android::defaultServiceManager;
59 using android::IInterface;
60 using android::IPCThreadState;
61 using android::Parcel;
62 using android::ProcessState;
63 using android::binder::Status;
64 
65 // Generated code:
66 using android::aidl::tests::BnNamedCallback;
67 using android::aidl::tests::BnTestService;
68 using android::aidl::tests::INamedCallback;
69 using android::aidl::tests::SimpleParcelable;
70 using android::binder::Map;
71 using android::os::ParcelFileDescriptor;
72 using android::os::PersistableBundle;
73 
74 // Standard library
75 using std::map;
76 using std::string;
77 using std::unique_ptr;
78 using std::vector;
79 
80 namespace {
81 
82 class BinderCallback : public LooperCallback {
83  public:
BinderCallback()84   BinderCallback() {}
~BinderCallback()85   ~BinderCallback() override {}
86 
handleEvent(int,int,void *)87   int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
88     IPCThreadState::self()->handlePolledCommands();
89     return 1;  // Continue receiving callbacks.
90   }
91 };
92 
93 class NamedCallback : public BnNamedCallback {
94  public:
NamedCallback(String16 name)95   explicit NamedCallback(String16 name) : name_(name) {}
96 
GetName(String16 * ret)97   Status GetName(String16* ret) {
98     *ret = name_;
99     return Status::ok();
100   }
101 
102  private:
103   String16 name_;
104 };
105 
106 class NativeService : public BnTestService {
107  public:
NativeService()108   NativeService() {}
109   virtual ~NativeService() = default;
110 
LogRepeatedStringToken(const String16 & token)111   void LogRepeatedStringToken(const String16& token) {
112     ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
113           token.size());
114   }
115 
116   template <typename T>
LogRepeatedToken(const T & token)117   void LogRepeatedToken(const T& token) {
118     std::ostringstream token_str;
119     token_str << token;
120     ALOGI("Repeating token %s", token_str.str().c_str());
121   }
122 
LogRepeatedMapToken(const Map & token)123   void LogRepeatedMapToken(const Map& token) {
124     ALOGI("Repeating Map with %d elements", (int)token.size());
125   }
126 
RepeatBoolean(bool token,bool * _aidl_return)127   Status RepeatBoolean(bool token, bool* _aidl_return) override {
128     LogRepeatedToken(token ? 1 : 0);
129     *_aidl_return = token;
130     return Status::ok();
131   }
RepeatByte(int8_t token,int8_t * _aidl_return)132   Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
133     LogRepeatedToken(token);
134     *_aidl_return = token;
135     return Status::ok();
136   }
RepeatChar(char16_t token,char16_t * _aidl_return)137   Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
138     LogRepeatedStringToken(String16(&token, 1));
139     *_aidl_return = token;
140     return Status::ok();
141   }
RepeatInt(int32_t token,int32_t * _aidl_return)142   Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
143     LogRepeatedToken(token);
144     *_aidl_return = token;
145     return Status::ok();
146   }
RepeatLong(int64_t token,int64_t * _aidl_return)147   Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
148     LogRepeatedToken(token);
149     *_aidl_return = token;
150     return Status::ok();
151   }
RepeatFloat(float token,float * _aidl_return)152   Status RepeatFloat(float token, float* _aidl_return) override {
153     LogRepeatedToken(token);
154     *_aidl_return = token;
155     return Status::ok();
156   }
RepeatDouble(double token,double * _aidl_return)157   Status RepeatDouble(double token, double* _aidl_return) override {
158     LogRepeatedToken(token);
159     *_aidl_return = token;
160     return Status::ok();
161   }
RepeatString(const String16 & token,String16 * _aidl_return)162   Status RepeatString(const String16& token, String16* _aidl_return) override {
163     LogRepeatedStringToken(token);
164     *_aidl_return = token;
165     return Status::ok();
166   }
RepeatMap(const Map & token,Map * _aidl_return)167   Status RepeatMap(const Map& token, Map* _aidl_return) override {
168     LogRepeatedMapToken(token);
169     *_aidl_return = token;
170     return Status::ok();
171   }
172 
RepeatSimpleParcelable(const SimpleParcelable & input,SimpleParcelable * repeat,SimpleParcelable * _aidl_return)173   Status RepeatSimpleParcelable(const SimpleParcelable& input,
174                                 SimpleParcelable* repeat,
175                                 SimpleParcelable* _aidl_return) override {
176     ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
177     *repeat = input;
178     *_aidl_return = input;
179     return Status::ok();
180   }
181 
RepeatPersistableBundle(const PersistableBundle & input,PersistableBundle * _aidl_return)182   Status RepeatPersistableBundle(const PersistableBundle& input,
183                                  PersistableBundle* _aidl_return) override {
184     ALOGI("Repeated a PersistableBundle");
185     *_aidl_return = input;
186     return Status::ok();
187   }
188 
189   template <typename T>
ReverseArray(const vector<T> & input,vector<T> * repeated,vector<T> * _aidl_return)190   Status ReverseArray(const vector<T>& input, vector<T>* repeated,
191                       vector<T>* _aidl_return) {
192     ALOGI("Reversing array of length %zu", input.size());
193     *repeated = input;
194     *_aidl_return = input;
195     std::reverse(_aidl_return->begin(), _aidl_return->end());
196     return Status::ok();
197   }
198 
199   template<typename T>
RepeatNullable(const unique_ptr<T> & input,unique_ptr<T> * _aidl_return)200   Status RepeatNullable(const unique_ptr<T>& input,
201                         unique_ptr<T>* _aidl_return) {
202     ALOGI("Repeating nullable value");
203 
204     _aidl_return->reset();
205     if (input) {
206       _aidl_return->reset(new T(*input));
207     }
208 
209     return Status::ok();
210   }
211 
ReverseBoolean(const vector<bool> & input,vector<bool> * repeated,vector<bool> * _aidl_return)212   Status ReverseBoolean(const vector<bool>& input,
213                         vector<bool>* repeated,
214                         vector<bool>* _aidl_return) override {
215     return ReverseArray(input, repeated, _aidl_return);
216   }
ReverseByte(const vector<uint8_t> & input,vector<uint8_t> * repeated,vector<uint8_t> * _aidl_return)217   Status ReverseByte(const vector<uint8_t>& input,
218                      vector<uint8_t>* repeated,
219                      vector<uint8_t>* _aidl_return) override {
220     return ReverseArray(input, repeated, _aidl_return);
221   }
ReverseChar(const vector<char16_t> & input,vector<char16_t> * repeated,vector<char16_t> * _aidl_return)222   Status ReverseChar(const vector<char16_t>& input,
223                      vector<char16_t>* repeated,
224                      vector<char16_t>* _aidl_return) override {
225     return ReverseArray(input, repeated, _aidl_return);
226   }
ReverseInt(const vector<int32_t> & input,vector<int32_t> * repeated,vector<int32_t> * _aidl_return)227   Status ReverseInt(const vector<int32_t>& input,
228                     vector<int32_t>* repeated,
229                     vector<int32_t>* _aidl_return) override {
230     return ReverseArray(input, repeated, _aidl_return);
231   }
ReverseLong(const vector<int64_t> & input,vector<int64_t> * repeated,vector<int64_t> * _aidl_return)232   Status ReverseLong(const vector<int64_t>& input,
233                      vector<int64_t>* repeated,
234                      vector<int64_t>* _aidl_return) override {
235     return ReverseArray(input, repeated, _aidl_return);
236   }
ReverseFloat(const vector<float> & input,vector<float> * repeated,vector<float> * _aidl_return)237   Status ReverseFloat(const vector<float>& input,
238                       vector<float>* repeated,
239                       vector<float>* _aidl_return) override {
240     return ReverseArray(input, repeated, _aidl_return);
241   }
ReverseDouble(const vector<double> & input,vector<double> * repeated,vector<double> * _aidl_return)242   Status ReverseDouble(const vector<double>& input,
243                        vector<double>* repeated,
244                        vector<double>* _aidl_return) override {
245     return ReverseArray(input, repeated, _aidl_return);
246   }
ReverseString(const vector<String16> & input,vector<String16> * repeated,vector<String16> * _aidl_return)247   Status ReverseString(const vector<String16>& input,
248                        vector<String16>* repeated,
249                        vector<String16>* _aidl_return) override {
250     return ReverseArray(input, repeated, _aidl_return);
251   }
ReverseSimpleParcelables(const vector<SimpleParcelable> & input,vector<SimpleParcelable> * repeated,vector<SimpleParcelable> * _aidl_return)252   Status ReverseSimpleParcelables(
253       const vector<SimpleParcelable>& input,
254       vector<SimpleParcelable>* repeated,
255       vector<SimpleParcelable>* _aidl_return) override {
256     return ReverseArray(input, repeated, _aidl_return);
257   }
ReversePersistableBundles(const vector<PersistableBundle> & input,vector<PersistableBundle> * repeated,vector<PersistableBundle> * _aidl_return)258   Status ReversePersistableBundles(
259       const vector<PersistableBundle>& input,
260       vector<PersistableBundle>* repeated,
261       vector<PersistableBundle>* _aidl_return) override {
262     return ReverseArray(input, repeated, _aidl_return);
263   }
264 
GetOtherTestService(const String16 & name,sp<INamedCallback> * returned_service)265   Status GetOtherTestService(const String16& name,
266                              sp<INamedCallback>* returned_service) override {
267     if (service_map_.find(name) == service_map_.end()) {
268       sp<INamedCallback> new_item(new NamedCallback(name));
269       service_map_[name] = new_item;
270     }
271 
272     *returned_service = service_map_[name];
273     return Status::ok();
274   }
275 
VerifyName(const sp<INamedCallback> & service,const String16 & name,bool * returned_value)276   Status VerifyName(const sp<INamedCallback>& service, const String16& name,
277                     bool* returned_value) override {
278     String16 foundName;
279     Status status = service->GetName(&foundName);
280 
281     if (status.isOk()) {
282       *returned_value = foundName == name;
283     }
284 
285     return status;
286   }
287 
ReverseStringList(const vector<String16> & input,vector<String16> * repeated,vector<String16> * _aidl_return)288   Status ReverseStringList(const vector<String16>& input,
289                            vector<String16>* repeated,
290                            vector<String16>* _aidl_return) override {
291     return ReverseArray(input, repeated, _aidl_return);
292   }
293 
ReverseNamedCallbackList(const vector<sp<IBinder>> & input,vector<sp<IBinder>> * repeated,vector<sp<IBinder>> * _aidl_return)294   Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
295                                   vector<sp<IBinder>>* repeated,
296                                   vector<sp<IBinder>>* _aidl_return) override {
297     return ReverseArray(input, repeated, _aidl_return);
298   }
299 
RepeatFileDescriptor(const unique_fd & read,unique_fd * _aidl_return)300   Status RepeatFileDescriptor(const unique_fd& read,
301                               unique_fd* _aidl_return) override {
302     ALOGE("Repeating file descriptor");
303     *_aidl_return = unique_fd(dup(read.get()));
304     return Status::ok();
305   }
306 
ReverseFileDescriptorArray(const vector<unique_fd> & input,vector<unique_fd> * repeated,vector<unique_fd> * _aidl_return)307   Status ReverseFileDescriptorArray(const vector<unique_fd>& input,
308                                     vector<unique_fd>* repeated,
309                                     vector<unique_fd>* _aidl_return) override {
310     ALOGI("Reversing descriptor array of length %zu", input.size());
311     for (const auto& item : input) {
312       repeated->push_back(unique_fd(dup(item.get())));
313       _aidl_return->push_back(unique_fd(dup(item.get())));
314     }
315     std::reverse(_aidl_return->begin(), _aidl_return->end());
316     return Status::ok();
317   }
318 
RepeatParcelFileDescriptor(const ParcelFileDescriptor & read,ParcelFileDescriptor * _aidl_return)319   Status RepeatParcelFileDescriptor(const ParcelFileDescriptor& read,
320                                     ParcelFileDescriptor* _aidl_return) override {
321     ALOGE("Repeating parcel file descriptor");
322     _aidl_return->reset(unique_fd(dup(read.get())));
323     return Status::ok();
324   }
325 
ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor> & input,vector<ParcelFileDescriptor> * repeated,vector<ParcelFileDescriptor> * _aidl_return)326   Status ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor>& input,
327                                           vector<ParcelFileDescriptor>* repeated,
328                                           vector<ParcelFileDescriptor>* _aidl_return) override {
329     ALOGI("Reversing parcel descriptor array of length %zu", input.size());
330     for (const auto& item : input) {
331       repeated->push_back(ParcelFileDescriptor(unique_fd(dup(item.get()))));
332     }
333 
334     for (auto i = input.rbegin(); i != input.rend(); i++) {
335       _aidl_return->push_back(ParcelFileDescriptor(unique_fd(dup(i->get()))));
336     }
337     return Status::ok();
338   }
339 
ThrowServiceException(int code)340   Status ThrowServiceException(int code) override {
341     return Status::fromServiceSpecificError(code);
342   }
343 
RepeatNullableIntArray(const unique_ptr<vector<int32_t>> & input,unique_ptr<vector<int32_t>> * _aidl_return)344   Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input,
345                                 unique_ptr<vector<int32_t>>* _aidl_return) {
346     return RepeatNullable(input, _aidl_return);
347   }
348 
RepeatNullableStringList(const unique_ptr<vector<unique_ptr<String16>>> & input,unique_ptr<vector<unique_ptr<String16>>> * _aidl_return)349   Status RepeatNullableStringList(
350              const unique_ptr<vector<unique_ptr<String16>>>& input,
351              unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) {
352     ALOGI("Repeating nullable string list");
353     if (!input) {
354       _aidl_return->reset();
355       return Status::ok();
356     }
357 
358     _aidl_return->reset(new vector<unique_ptr<String16>>);
359 
360     for (const auto& item : *input) {
361       if (!item) {
362         (*_aidl_return)->emplace_back(nullptr);
363       } else {
364         (*_aidl_return)->emplace_back(new String16(*item));
365       }
366     }
367 
368     return Status::ok();
369   }
370 
RepeatNullableString(const unique_ptr<String16> & input,unique_ptr<String16> * _aidl_return)371   Status RepeatNullableString(const unique_ptr<String16>& input,
372                               unique_ptr<String16>* _aidl_return) {
373     return RepeatNullable(input, _aidl_return);
374   }
375 
RepeatNullableParcelable(const unique_ptr<SimpleParcelable> & input,unique_ptr<SimpleParcelable> * _aidl_return)376   Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input,
377                               unique_ptr<SimpleParcelable>* _aidl_return) {
378     return RepeatNullable(input, _aidl_return);
379   }
380 
TakesAnIBinder(const sp<IBinder> & input)381   Status TakesAnIBinder(const sp<IBinder>& input) override {
382     (void)input;
383     return Status::ok();
384   }
TakesAnIBinderList(const vector<sp<IBinder>> & input)385   Status TakesAnIBinderList(const vector<sp<IBinder>>& input) override {
386     (void)input;
387     return Status::ok();
388   }
TakesANullableIBinder(const sp<IBinder> & input)389   Status TakesANullableIBinder(const sp<IBinder>& input) {
390     (void)input;
391     return Status::ok();
392   }
TakesANullableIBinderList(const unique_ptr<vector<sp<IBinder>>> & input)393   Status TakesANullableIBinderList(const unique_ptr<vector<sp<IBinder>>>& input) {
394     (void)input;
395     return Status::ok();
396   }
397 
RepeatUtf8CppString(const string & token,string * _aidl_return)398   Status RepeatUtf8CppString(const string& token,
399                              string* _aidl_return) override {
400     ALOGI("Repeating utf8 string '%s' of length=%zu", token.c_str(), token.size());
401     *_aidl_return = token;
402     return Status::ok();
403   }
404 
RepeatNullableUtf8CppString(const unique_ptr<string> & token,unique_ptr<string> * _aidl_return)405   Status RepeatNullableUtf8CppString(
406       const unique_ptr<string>& token,
407       unique_ptr<string>* _aidl_return) override {
408     if (!token) {
409       ALOGI("Received null @utf8InCpp string");
410       return Status::ok();
411     }
412     ALOGI("Repeating utf8 string '%s' of length=%zu",
413           token->c_str(), token->size());
414     _aidl_return->reset(new string(*token));
415     return Status::ok();
416   }
417 
ReverseUtf8CppString(const vector<string> & input,vector<string> * repeated,vector<string> * _aidl_return)418   Status ReverseUtf8CppString(const vector<string>& input,
419                               vector<string>* repeated,
420                               vector<string>* _aidl_return) {
421     return ReverseArray(input, repeated, _aidl_return);
422   }
423 
ReverseNullableUtf8CppString(const unique_ptr<vector<unique_ptr<string>>> & input,unique_ptr<vector<unique_ptr<string>>> * repeated,unique_ptr<vector<unique_ptr<string>>> * _aidl_return)424   Status ReverseNullableUtf8CppString(
425       const unique_ptr<vector<unique_ptr<string>>>& input,
426       unique_ptr<vector<unique_ptr<string>>>* repeated,
427       unique_ptr<vector<unique_ptr<string>>>* _aidl_return) {
428 
429     return ReverseUtf8CppStringList(input, repeated, _aidl_return);
430   }
431 
ReverseUtf8CppStringList(const unique_ptr<vector<unique_ptr<::string>>> & input,unique_ptr<vector<unique_ptr<string>>> * repeated,unique_ptr<vector<unique_ptr<string>>> * _aidl_return)432   Status ReverseUtf8CppStringList(
433       const unique_ptr<vector<unique_ptr<::string>>>& input,
434       unique_ptr<vector<unique_ptr<string>>>* repeated,
435       unique_ptr<vector<unique_ptr<string>>>* _aidl_return) {
436     if (!input) {
437       ALOGI("Received null list of utf8 strings");
438       return Status::ok();
439     }
440     _aidl_return->reset(new vector<unique_ptr<string>>);
441     repeated->reset(new vector<unique_ptr<string>>);
442 
443     for (const auto& item : *input) {
444       (*repeated)->emplace_back(nullptr);
445       (*_aidl_return)->emplace_back(nullptr);
446       if (item) {
447         (*repeated)->back().reset(new string(*item));
448         (*_aidl_return)->back().reset(new string(*item));
449       }
450     }
451     std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end());
452 
453     return Status::ok();
454   }
455 
GetCallback(bool return_null,sp<INamedCallback> * ret)456   Status GetCallback(bool return_null, sp<INamedCallback>* ret) {
457     if (!return_null) {
458       return GetOtherTestService(String16("ABT: always be testing"), ret);
459     }
460     return Status::ok();
461   }
462 
FillOutStructuredParcelable(::android::aidl::tests::StructuredParcelable * parcelable)463   virtual ::android::binder::Status FillOutStructuredParcelable(
464       ::android::aidl::tests::StructuredParcelable* parcelable) {
465     parcelable->shouldBeJerry = "Jerry";
466     parcelable->shouldContainThreeFs = {parcelable->f, parcelable->f, parcelable->f};
467     return Status::ok();
468   }
469 
UnimplementedMethod(int32_t,int32_t *)470   Status UnimplementedMethod(int32_t /* arg */, int32_t* /* _aidl_return */) override {
471     LOG_ALWAYS_FATAL("UnimplementedMethod shouldn't be called");
472   }
473 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)474   android::status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
475                                uint32_t flags) override {
476     if (code == ::android::IBinder::FIRST_CALL_TRANSACTION + 45 /* UnimplementedMethod */) {
477       // pretend that UnimplementedMethod isn't implemented by this service.
478       return android::UNKNOWN_TRANSACTION;
479     } else {
480       return BnTestService::onTransact(code, data, reply, flags);
481     }
482   }
483 
484  private:
485   map<String16, sp<INamedCallback>> service_map_;
486 };
487 
Run()488 int Run() {
489   android::sp<NativeService> service = new NativeService;
490   sp<Looper> looper(Looper::prepare(0 /* opts */));
491 
492   int binder_fd = -1;
493   ProcessState::self()->setThreadPoolMaxThreadCount(0);
494   IPCThreadState::self()->disableBackgroundScheduling(true);
495   IPCThreadState::self()->setupPolling(&binder_fd);
496   ALOGI("Got binder FD %d", binder_fd);
497   if (binder_fd < 0) return -1;
498 
499   sp<BinderCallback> cb(new BinderCallback);
500   if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
501                     nullptr) != 1) {
502     ALOGE("Failed to add binder FD to Looper");
503     return -1;
504   }
505 
506   defaultServiceManager()->addService(service->getInterfaceDescriptor(),
507                                       service);
508 
509   ALOGI("Entering loop");
510   while (true) {
511     const int result = looper->pollAll(-1 /* timeoutMillis */);
512     ALOGI("Looper returned %d", result);
513   }
514   return 0;
515 }
516 
517 }  // namespace
518 
main(int,char * [])519 int main(int /* argc */, char* /* argv */ []) {
520   return Run();
521 }
522