blob: 856a67b05de262b06321c19687297e6fe5540cdf [file] [log] [blame]
Christopher Wileyb5e698c2015-10-17 09:32:22 -07001/*
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
Casey Dahlin389781f2015-10-22 13:13:21 -070017#include <map>
Christopher Wileyd6130f22015-10-26 10:24:35 -070018#include <sstream>
19#include <string>
Christopher Wiley8949f832015-10-27 15:32:03 -070020#include <vector>
Christopher Wileyd6130f22015-10-26 10:24:35 -070021
Casey Dahlina4ba4b62015-11-02 15:43:30 -080022#include <unistd.h>
23
Christopher Wileyb5e698c2015-10-17 09:32:22 -070024#include <binder/IInterface.h>
25#include <binder/IPCThreadState.h>
26#include <binder/IServiceManager.h>
27#include <binder/ProcessState.h>
Christopher Wiley7621d4d2015-11-28 16:58:06 -080028#include <binder/Status.h>
Casey Dahlina4ba4b62015-11-02 15:43:30 -080029#include <nativehelper/ScopedFd.h>
Christopher Wileyb5e698c2015-10-17 09:32:22 -070030#include <utils/Errors.h>
31#include <utils/Log.h>
32#include <utils/Looper.h>
33#include <utils/StrongPointer.h>
34
Christopher Wiley521bb612015-10-22 11:40:30 -070035#include "android/aidl/tests/BnTestService.h"
36#include "android/aidl/tests/ITestService.h"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070037
Casey Dahlin389781f2015-10-22 13:13:21 -070038#include "android/aidl/tests/BnNamedCallback.h"
39#include "android/aidl/tests/INamedCallback.h"
40
Christopher Wileyb5e698c2015-10-17 09:32:22 -070041// Used implicitly.
42#undef LOG_TAG
Christopher Wiley33ad81e2015-10-21 14:41:11 -070043#define LOG_TAG "aidl_native_service"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070044
45// libutils:
46using android::Looper;
47using android::LooperCallback;
48using android::OK;
49using android::sp;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070050using android::String16;
51
52// libbinder:
53using android::BnInterface;
54using android::defaultServiceManager;
55using android::IInterface;
56using android::IPCThreadState;
57using android::Parcel;
58using android::ProcessState;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080059using android::binder::Status;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070060
61// Generated code:
Casey Dahlin389781f2015-10-22 13:13:21 -070062using android::aidl::tests::BnNamedCallback;
Samuel Tan88c32002015-12-03 16:30:43 -080063using android::aidl::tests::BnTestService;
Casey Dahlin389781f2015-10-22 13:13:21 -070064using android::aidl::tests::INamedCallback;
Christopher Wiley95d44b02015-11-19 07:11:30 -080065using android::aidl::tests::SimpleParcelable;
Samuel Tan88c32002015-12-03 16:30:43 -080066using android::os::PersistableBundle;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070067
Casey Dahlin389781f2015-10-22 13:13:21 -070068// Standard library
69using std::map;
Christopher Wileydca2ecf2016-01-29 10:55:43 -080070using std::string;
Casey Dahlin57dbe242015-12-04 11:44:02 -080071using std::unique_ptr;
Christopher Wiley8949f832015-10-27 15:32:03 -070072using std::vector;
73
Christopher Wileyb5e698c2015-10-17 09:32:22 -070074namespace {
75
76class BinderCallback : public LooperCallback {
77 public:
78 BinderCallback() {}
79 ~BinderCallback() override {}
80
Samuel Tan88c32002015-12-03 16:30:43 -080081 int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070082 IPCThreadState::self()->handlePolledCommands();
83 return 1; // Continue receiving callbacks.
84 }
85};
86
Casey Dahlin389781f2015-10-22 13:13:21 -070087class NamedCallback : public BnNamedCallback {
88 public:
89 NamedCallback(String16 name) : name_(name) {}
90
Christopher Wiley433c8bb2015-11-12 14:20:46 -080091 Status GetName(String16* ret) {
Casey Dahlin389781f2015-10-22 13:13:21 -070092 *ret = name_;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080093 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -070094 }
95
96 private:
97 String16 name_;
98};
99
Christopher Wiley521bb612015-10-22 11:40:30 -0700100class NativeService : public BnTestService {
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700101 public:
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700102 NativeService() {}
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800103 virtual ~NativeService() = default;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700104
Christopher Wileyd6130f22015-10-26 10:24:35 -0700105 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700106 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700107 token.size());
108 }
109
Samuel Tan88c32002015-12-03 16:30:43 -0800110 template <typename T>
Christopher Wileyd6130f22015-10-26 10:24:35 -0700111 void LogRepeatedToken(const T& token) {
112 std::ostringstream token_str;
113 token_str << token;
114 ALOGI("Repeating token %s", token_str.str().c_str());
115 }
116
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800117 Status RepeatBoolean(bool token, bool* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700118 LogRepeatedToken(token ? 1 : 0);
119 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800120 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700121 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800122 Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700123 LogRepeatedToken(token);
124 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800125 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700126 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800127 Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700128 LogRepeatedStringToken(String16(&token, 1));
129 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800130 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700131 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800132 Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700133 LogRepeatedToken(token);
134 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800135 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700136 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800137 Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700138 LogRepeatedToken(token);
139 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800140 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700141 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800142 Status RepeatFloat(float token, float* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700143 LogRepeatedToken(token);
144 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800145 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700146 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800147 Status RepeatDouble(double token, double* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700148 LogRepeatedToken(token);
149 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800150 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700151 }
Samuel Tan88c32002015-12-03 16:30:43 -0800152 Status RepeatString(const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700153 LogRepeatedStringToken(token);
154 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800155 return Status::ok();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700156 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700157
Samuel Tan88c32002015-12-03 16:30:43 -0800158 Status RepeatSimpleParcelable(const SimpleParcelable& input,
159 SimpleParcelable* repeat,
160 SimpleParcelable* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800161 ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
162 *repeat = input;
163 *_aidl_return = input;
164 return Status::ok();
165 }
166
Samuel Tan88c32002015-12-03 16:30:43 -0800167 Status RepeatPersistableBundle(const PersistableBundle& input,
168 PersistableBundle* _aidl_return) override {
169 ALOGI("Repeated a PersistableBundle");
170 *_aidl_return = input;
171 return Status::ok();
172 }
173
174 template <typename T>
175 Status ReverseArray(const vector<T>& input, vector<T>* repeated,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800176 vector<T>* _aidl_return) {
Christopher Wiley8949f832015-10-27 15:32:03 -0700177 ALOGI("Reversing array of length %zu", input.size());
178 *repeated = input;
179 *_aidl_return = input;
180 std::reverse(_aidl_return->begin(), _aidl_return->end());
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800181 return Status::ok();
Christopher Wiley8949f832015-10-27 15:32:03 -0700182 }
183
Casey Dahlin57dbe242015-12-04 11:44:02 -0800184 template<typename T>
185 Status RepeatNullable(const unique_ptr<T>& input,
186 unique_ptr<T>* _aidl_return) {
187 ALOGI("Repeating nullable value");
188
189 _aidl_return->reset();
190 if (input) {
191 _aidl_return->reset(new T(*input));
192 }
193
194 return Status::ok();
195 }
196
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800197 Status ReverseBoolean(const vector<bool>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800198 vector<bool>* repeated,
199 vector<bool>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700200 return ReverseArray(input, repeated, _aidl_return);
201 }
Casey Dahline9e73f12016-02-09 11:10:17 -0800202 Status ReverseByte(const vector<uint8_t>& input,
203 vector<uint8_t>* repeated,
204 vector<uint8_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700205 return ReverseArray(input, repeated, _aidl_return);
206 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800207 Status ReverseChar(const vector<char16_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800208 vector<char16_t>* repeated,
209 vector<char16_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700210 return ReverseArray(input, repeated, _aidl_return);
211 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800212 Status ReverseInt(const vector<int32_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800213 vector<int32_t>* repeated,
214 vector<int32_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700215 return ReverseArray(input, repeated, _aidl_return);
216 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800217 Status ReverseLong(const vector<int64_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800218 vector<int64_t>* repeated,
219 vector<int64_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700220 return ReverseArray(input, repeated, _aidl_return);
221 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800222 Status ReverseFloat(const vector<float>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800223 vector<float>* repeated,
224 vector<float>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700225 return ReverseArray(input, repeated, _aidl_return);
226 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800227 Status ReverseDouble(const vector<double>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800228 vector<double>* repeated,
229 vector<double>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700230 return ReverseArray(input, repeated, _aidl_return);
231 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800232 Status ReverseString(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800233 vector<String16>* repeated,
234 vector<String16>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700235 return ReverseArray(input, repeated, _aidl_return);
236 }
Samuel Tan88c32002015-12-03 16:30:43 -0800237 Status ReverseSimpleParcelables(
238 const vector<SimpleParcelable>& input,
239 vector<SimpleParcelable>* repeated,
240 vector<SimpleParcelable>* _aidl_return) override {
241 return ReverseArray(input, repeated, _aidl_return);
242 }
243 Status ReversePersistableBundles(
244 const vector<PersistableBundle>& input,
245 vector<PersistableBundle>* repeated,
246 vector<PersistableBundle>* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800247 return ReverseArray(input, repeated, _aidl_return);
248 }
Casey Dahlin389781f2015-10-22 13:13:21 -0700249
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800250 Status GetOtherTestService(const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800251 sp<INamedCallback>* returned_service) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700252 if (service_map_.find(name) == service_map_.end()) {
253 sp<INamedCallback> new_item(new NamedCallback(name));
254 service_map_[name] = new_item;
255 }
256
257 *returned_service = service_map_[name];
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800258 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -0700259 }
260
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800261 Status VerifyName(const sp<INamedCallback>& service, const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800262 bool* returned_value) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700263 String16 foundName;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800264 Status status = service->GetName(&foundName);
Casey Dahlin389781f2015-10-22 13:13:21 -0700265
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800266 if (status.isOk()) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700267 *returned_value = foundName == name;
268 }
269
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800270 return status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700271 }
272
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800273 Status ReverseStringList(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800274 vector<String16>* repeated,
275 vector<String16>* _aidl_return) override {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700276 return ReverseArray(input, repeated, _aidl_return);
277 }
278
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800279 Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800280 vector<sp<IBinder>>* repeated,
281 vector<sp<IBinder>>* _aidl_return) override {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800282 return ReverseArray(input, repeated, _aidl_return);
283 }
284
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800285 Status RepeatFileDescriptor(const ScopedFd& read,
286 ScopedFd* _aidl_return) override {
287 ALOGE("Repeating file descriptor");
288 *_aidl_return = ScopedFd(dup(read.get()));
289 return Status::ok();
290 }
291
292 Status ReverseFileDescriptorArray(const vector<ScopedFd>& input,
293 vector<ScopedFd>* repeated,
294 vector<ScopedFd>* _aidl_return) override {
295 ALOGI("Reversing descriptor array of length %zu", input.size());
296 for (const auto& item : input) {
297 repeated->push_back(ScopedFd(dup(item.get())));
298 _aidl_return->push_back(ScopedFd(dup(item.get())));
299 }
300 std::reverse(_aidl_return->begin(), _aidl_return->end());
301 return Status::ok();
302 }
303
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800304 Status ThrowServiceException(int code) override {
305 return Status::fromServiceSpecificError(code);
306 }
307
Casey Dahlin57dbe242015-12-04 11:44:02 -0800308 Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input,
309 unique_ptr<vector<int32_t>>* _aidl_return) {
310 return RepeatNullable(input, _aidl_return);
311 }
312
313 Status RepeatNullableStringList(
314 const unique_ptr<vector<unique_ptr<String16>>>& input,
315 unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) {
316 ALOGI("Repeating nullable string list");
317 if (!input) {
318 _aidl_return->reset();
319 return Status::ok();
320 }
321
322 _aidl_return->reset(new vector<unique_ptr<String16>>);
323
324 for (const auto& item : *input) {
325 if (!item) {
326 (*_aidl_return)->emplace_back(nullptr);
327 } else {
328 (*_aidl_return)->emplace_back(new String16(*item));
329 }
330 }
331
332 return Status::ok();
333 }
334
335 Status RepeatNullableString(const unique_ptr<String16>& input,
336 unique_ptr<String16>* _aidl_return) {
337 return RepeatNullable(input, _aidl_return);
338 }
339
340 Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input,
341 unique_ptr<SimpleParcelable>* _aidl_return) {
342 return RepeatNullable(input, _aidl_return);
343 }
344
Christopher Wileydca2ecf2016-01-29 10:55:43 -0800345 Status RepeatUtf8CppString(const string& token,
346 string* _aidl_return) override {
347 ALOGI("Repeating utf8 string '%s' of length=%zu", token.c_str(), token.size());
348 *_aidl_return = token;
349 return Status::ok();
350 }
351
352 Status RepeatNullableUtf8CppString(
353 const unique_ptr<string>& token,
354 unique_ptr<string>* _aidl_return) override {
355 if (!token) {
356 ALOGI("Received null @utf8InCpp string");
357 return Status::ok();
358 }
359 ALOGI("Repeating utf8 string '%s' of length=%zu",
360 token->c_str(), token->size());
361 _aidl_return->reset(new string(*token));
362 return Status::ok();
363 }
364
365 Status ReverseUtf8CppString(const vector<string>& input,
366 vector<string>* repeated,
367 vector<string>* _aidl_return) {
368 return ReverseArray(input, repeated, _aidl_return);
369 }
370
Christopher Wiley99d699b2016-02-01 13:19:43 -0800371 Status ReverseUtf8CppStringList(
372 const unique_ptr<vector<unique_ptr<::string>>>& input,
373 unique_ptr<vector<unique_ptr<string>>>* repeated,
374 unique_ptr<vector<unique_ptr<string>>>* _aidl_return) {
375 if (!input) {
376 ALOGI("Received null list of utf8 strings");
377 return Status::ok();
378 }
379 _aidl_return->reset(new vector<unique_ptr<string>>);
380 repeated->reset(new vector<unique_ptr<string>>);
381
382 for (const auto& item : *input) {
383 (*repeated)->emplace_back(nullptr);
384 (*_aidl_return)->emplace_back(nullptr);
385 if (item) {
386 (*repeated)->back().reset(new string(*item));
387 (*_aidl_return)->back().reset(new string(*item));
388 }
389 }
390 std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end());
391
392 return Status::ok();
393 }
394
Casey Dahlin389781f2015-10-22 13:13:21 -0700395 private:
396 map<String16, sp<INamedCallback>> service_map_;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700397};
398
Casey Dahlinfc465462016-01-07 16:13:57 -0800399int Run() {
Christopher Wileydca2ecf2016-01-29 10:55:43 -0800400 android::sp<NativeService> service = new NativeService;
Casey Dahlinfc465462016-01-07 16:13:57 -0800401 sp<Looper> looper(Looper::prepare(0 /* opts */));
402
403 int binder_fd = -1;
404 ProcessState::self()->setThreadPoolMaxThreadCount(0);
405 IPCThreadState::self()->disableBackgroundScheduling(true);
406 IPCThreadState::self()->setupPolling(&binder_fd);
407 ALOGI("Got binder FD %d", binder_fd);
408 if (binder_fd < 0) return -1;
409
410 sp<BinderCallback> cb(new BinderCallback);
411 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
412 nullptr) != 1) {
413 ALOGE("Failed to add binder FD to Looper");
414 return -1;
415 }
416
417 defaultServiceManager()->addService(service->getInterfaceDescriptor(),
418 service);
419
420 ALOGI("Entering loop");
421 while (true) {
422 const int result = looper->pollAll(-1 /* timeoutMillis */);
423 ALOGI("Looper returned %d", result);
424 }
425 return 0;
426}
427
Christopher Wileydca2ecf2016-01-29 10:55:43 -0800428} // namespace
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700429
Brian Carlstromad3b8062015-10-21 08:54:48 -0700430int main(int /* argc */, char* /* argv */ []) {
Christopher Wileydca2ecf2016-01-29 10:55:43 -0800431 return Run();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700432}