blob: 4cf41136c1eb518b2b83af759fc4e52e63886125 [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;
Casey Dahlin57dbe242015-12-04 11:44:02 -080070using std::unique_ptr;
Christopher Wiley8949f832015-10-27 15:32:03 -070071using std::vector;
72
Christopher Wileyb5e698c2015-10-17 09:32:22 -070073namespace android {
74namespace generated {
75namespace {
76
77class BinderCallback : public LooperCallback {
78 public:
79 BinderCallback() {}
80 ~BinderCallback() override {}
81
Samuel Tan88c32002015-12-03 16:30:43 -080082 int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070083 IPCThreadState::self()->handlePolledCommands();
84 return 1; // Continue receiving callbacks.
85 }
86};
87
Casey Dahlin389781f2015-10-22 13:13:21 -070088class NamedCallback : public BnNamedCallback {
89 public:
90 NamedCallback(String16 name) : name_(name) {}
91
Christopher Wiley433c8bb2015-11-12 14:20:46 -080092 Status GetName(String16* ret) {
Casey Dahlin389781f2015-10-22 13:13:21 -070093 *ret = name_;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080094 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -070095 }
96
97 private:
98 String16 name_;
99};
100
Christopher Wiley521bb612015-10-22 11:40:30 -0700101class NativeService : public BnTestService {
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700102 public:
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700103 NativeService() {}
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800104 virtual ~NativeService() = default;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700105
Christopher Wileyd6130f22015-10-26 10:24:35 -0700106 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700107 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700108 token.size());
109 }
110
Samuel Tan88c32002015-12-03 16:30:43 -0800111 template <typename T>
Christopher Wileyd6130f22015-10-26 10:24:35 -0700112 void LogRepeatedToken(const T& token) {
113 std::ostringstream token_str;
114 token_str << token;
115 ALOGI("Repeating token %s", token_str.str().c_str());
116 }
117
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800118 Status RepeatBoolean(bool token, bool* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700119 LogRepeatedToken(token ? 1 : 0);
120 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800121 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700122 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800123 Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700124 LogRepeatedToken(token);
125 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800126 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700127 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800128 Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700129 LogRepeatedStringToken(String16(&token, 1));
130 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800131 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700132 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800133 Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700134 LogRepeatedToken(token);
135 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800136 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700137 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800138 Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700139 LogRepeatedToken(token);
140 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800141 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700142 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800143 Status RepeatFloat(float token, float* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700144 LogRepeatedToken(token);
145 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800146 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700147 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800148 Status RepeatDouble(double token, double* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700149 LogRepeatedToken(token);
150 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800151 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700152 }
Samuel Tan88c32002015-12-03 16:30:43 -0800153 Status RepeatString(const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700154 LogRepeatedStringToken(token);
155 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800156 return Status::ok();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700157 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700158
Samuel Tan88c32002015-12-03 16:30:43 -0800159 Status RepeatSimpleParcelable(const SimpleParcelable& input,
160 SimpleParcelable* repeat,
161 SimpleParcelable* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800162 ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
163 *repeat = input;
164 *_aidl_return = input;
165 return Status::ok();
166 }
167
Samuel Tan88c32002015-12-03 16:30:43 -0800168 Status RepeatPersistableBundle(const PersistableBundle& input,
169 PersistableBundle* _aidl_return) override {
170 ALOGI("Repeated a PersistableBundle");
171 *_aidl_return = input;
172 return Status::ok();
173 }
174
175 template <typename T>
176 Status ReverseArray(const vector<T>& input, vector<T>* repeated,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800177 vector<T>* _aidl_return) {
Christopher Wiley8949f832015-10-27 15:32:03 -0700178 ALOGI("Reversing array of length %zu", input.size());
179 *repeated = input;
180 *_aidl_return = input;
181 std::reverse(_aidl_return->begin(), _aidl_return->end());
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800182 return Status::ok();
Christopher Wiley8949f832015-10-27 15:32:03 -0700183 }
184
Casey Dahlin57dbe242015-12-04 11:44:02 -0800185 template<typename T>
186 Status RepeatNullable(const unique_ptr<T>& input,
187 unique_ptr<T>* _aidl_return) {
188 ALOGI("Repeating nullable value");
189
190 _aidl_return->reset();
191 if (input) {
192 _aidl_return->reset(new T(*input));
193 }
194
195 return Status::ok();
196 }
197
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800198 Status ReverseBoolean(const vector<bool>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800199 vector<bool>* repeated,
200 vector<bool>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700201 return ReverseArray(input, repeated, _aidl_return);
202 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800203 Status ReverseByte(const vector<int8_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800204 vector<int8_t>* repeated,
205 vector<int8_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700206 return ReverseArray(input, repeated, _aidl_return);
207 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800208 Status ReverseChar(const vector<char16_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800209 vector<char16_t>* repeated,
210 vector<char16_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700211 return ReverseArray(input, repeated, _aidl_return);
212 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800213 Status ReverseInt(const vector<int32_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800214 vector<int32_t>* repeated,
215 vector<int32_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700216 return ReverseArray(input, repeated, _aidl_return);
217 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800218 Status ReverseLong(const vector<int64_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800219 vector<int64_t>* repeated,
220 vector<int64_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700221 return ReverseArray(input, repeated, _aidl_return);
222 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800223 Status ReverseFloat(const vector<float>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800224 vector<float>* repeated,
225 vector<float>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700226 return ReverseArray(input, repeated, _aidl_return);
227 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800228 Status ReverseDouble(const vector<double>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800229 vector<double>* repeated,
230 vector<double>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700231 return ReverseArray(input, repeated, _aidl_return);
232 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800233 Status ReverseString(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800234 vector<String16>* repeated,
235 vector<String16>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700236 return ReverseArray(input, repeated, _aidl_return);
237 }
Samuel Tan88c32002015-12-03 16:30:43 -0800238 Status ReverseSimpleParcelables(
239 const vector<SimpleParcelable>& input,
240 vector<SimpleParcelable>* repeated,
241 vector<SimpleParcelable>* _aidl_return) override {
242 return ReverseArray(input, repeated, _aidl_return);
243 }
244 Status ReversePersistableBundles(
245 const vector<PersistableBundle>& input,
246 vector<PersistableBundle>* repeated,
247 vector<PersistableBundle>* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800248 return ReverseArray(input, repeated, _aidl_return);
249 }
Casey Dahlin389781f2015-10-22 13:13:21 -0700250
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800251 Status GetOtherTestService(const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800252 sp<INamedCallback>* returned_service) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700253 if (service_map_.find(name) == service_map_.end()) {
254 sp<INamedCallback> new_item(new NamedCallback(name));
255 service_map_[name] = new_item;
256 }
257
258 *returned_service = service_map_[name];
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800259 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -0700260 }
261
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800262 Status VerifyName(const sp<INamedCallback>& service, const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800263 bool* returned_value) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700264 String16 foundName;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800265 Status status = service->GetName(&foundName);
Casey Dahlin389781f2015-10-22 13:13:21 -0700266
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800267 if (status.isOk()) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700268 *returned_value = foundName == name;
269 }
270
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800271 return status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700272 }
273
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800274 Status ReverseStringList(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800275 vector<String16>* repeated,
276 vector<String16>* _aidl_return) override {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700277 return ReverseArray(input, repeated, _aidl_return);
278 }
279
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800280 Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800281 vector<sp<IBinder>>* repeated,
282 vector<sp<IBinder>>* _aidl_return) override {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800283 return ReverseArray(input, repeated, _aidl_return);
284 }
285
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800286 Status RepeatFileDescriptor(const ScopedFd& read,
287 ScopedFd* _aidl_return) override {
288 ALOGE("Repeating file descriptor");
289 *_aidl_return = ScopedFd(dup(read.get()));
290 return Status::ok();
291 }
292
293 Status ReverseFileDescriptorArray(const vector<ScopedFd>& input,
294 vector<ScopedFd>* repeated,
295 vector<ScopedFd>* _aidl_return) override {
296 ALOGI("Reversing descriptor array of length %zu", input.size());
297 for (const auto& item : input) {
298 repeated->push_back(ScopedFd(dup(item.get())));
299 _aidl_return->push_back(ScopedFd(dup(item.get())));
300 }
301 std::reverse(_aidl_return->begin(), _aidl_return->end());
302 return Status::ok();
303 }
304
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800305 Status ThrowServiceException(int code) override {
306 return Status::fromServiceSpecificError(code);
307 }
308
Casey Dahlin57dbe242015-12-04 11:44:02 -0800309 Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input,
310 unique_ptr<vector<int32_t>>* _aidl_return) {
311 return RepeatNullable(input, _aidl_return);
312 }
313
314 Status RepeatNullableStringList(
315 const unique_ptr<vector<unique_ptr<String16>>>& input,
316 unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) {
317 ALOGI("Repeating nullable string list");
318 if (!input) {
319 _aidl_return->reset();
320 return Status::ok();
321 }
322
323 _aidl_return->reset(new vector<unique_ptr<String16>>);
324
325 for (const auto& item : *input) {
326 if (!item) {
327 (*_aidl_return)->emplace_back(nullptr);
328 } else {
329 (*_aidl_return)->emplace_back(new String16(*item));
330 }
331 }
332
333 return Status::ok();
334 }
335
336 Status RepeatNullableString(const unique_ptr<String16>& input,
337 unique_ptr<String16>* _aidl_return) {
338 return RepeatNullable(input, _aidl_return);
339 }
340
341 Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input,
342 unique_ptr<SimpleParcelable>* _aidl_return) {
343 return RepeatNullable(input, _aidl_return);
344 }
345
Casey Dahlin389781f2015-10-22 13:13:21 -0700346 private:
347 map<String16, sp<INamedCallback>> service_map_;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700348};
349
350} // namespace
Casey Dahlinfc465462016-01-07 16:13:57 -0800351
352int Run() {
353 android::sp<android::generated::NativeService> service =
354 new android::generated::NativeService;
355 sp<Looper> looper(Looper::prepare(0 /* opts */));
356
357 int binder_fd = -1;
358 ProcessState::self()->setThreadPoolMaxThreadCount(0);
359 IPCThreadState::self()->disableBackgroundScheduling(true);
360 IPCThreadState::self()->setupPolling(&binder_fd);
361 ALOGI("Got binder FD %d", binder_fd);
362 if (binder_fd < 0) return -1;
363
364 sp<BinderCallback> cb(new BinderCallback);
365 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
366 nullptr) != 1) {
367 ALOGE("Failed to add binder FD to Looper");
368 return -1;
369 }
370
371 defaultServiceManager()->addService(service->getInterfaceDescriptor(),
372 service);
373
374 ALOGI("Entering loop");
375 while (true) {
376 const int result = looper->pollAll(-1 /* timeoutMillis */);
377 ALOGI("Looper returned %d", result);
378 }
379 return 0;
380}
381
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700382} // namespace generated
383} // namespace android
384
Brian Carlstromad3b8062015-10-21 08:54:48 -0700385int main(int /* argc */, char* /* argv */ []) {
Casey Dahlinfc465462016-01-07 16:13:57 -0800386 return android::generated::Run();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700387}