blob: 5f45ee874dc0e90ea6be9123fb7e5b5dd00a8ae2 [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
106 int Run() {
107 sp<Looper> looper(Looper::prepare(0 /* opts */));
108
109 int binder_fd = -1;
110 ProcessState::self()->setThreadPoolMaxThreadCount(0);
111 IPCThreadState::self()->disableBackgroundScheduling(true);
112 IPCThreadState::self()->setupPolling(&binder_fd);
113 ALOGI("Got binder FD %d", binder_fd);
114 if (binder_fd < 0) return -1;
115
116 sp<BinderCallback> cb(new BinderCallback);
117 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
118 nullptr) != 1) {
119 ALOGE("Failed to add binder FD to Looper");
120 return -1;
121 }
122
123 defaultServiceManager()->addService(getInterfaceDescriptor(), this);
124
125 ALOGI("Entering loop");
126 while (true) {
127 const int result = looper->pollAll(-1 /* timeoutMillis */);
128 ALOGI("Looper returned %d", result);
129 }
130 return 0;
131 }
132
Christopher Wileyd6130f22015-10-26 10:24:35 -0700133 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700134 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700135 token.size());
136 }
137
Samuel Tan88c32002015-12-03 16:30:43 -0800138 template <typename T>
Christopher Wileyd6130f22015-10-26 10:24:35 -0700139 void LogRepeatedToken(const T& token) {
140 std::ostringstream token_str;
141 token_str << token;
142 ALOGI("Repeating token %s", token_str.str().c_str());
143 }
144
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800145 Status RepeatBoolean(bool token, bool* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700146 LogRepeatedToken(token ? 1 : 0);
147 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800148 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700149 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800150 Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700151 LogRepeatedToken(token);
152 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800153 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700154 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800155 Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700156 LogRepeatedStringToken(String16(&token, 1));
157 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800158 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700159 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800160 Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700161 LogRepeatedToken(token);
162 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800163 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700164 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800165 Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700166 LogRepeatedToken(token);
167 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800168 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700169 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800170 Status RepeatFloat(float token, float* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700171 LogRepeatedToken(token);
172 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800173 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700174 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800175 Status RepeatDouble(double token, double* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700176 LogRepeatedToken(token);
177 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800178 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700179 }
Samuel Tan88c32002015-12-03 16:30:43 -0800180 Status RepeatString(const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700181 LogRepeatedStringToken(token);
182 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800183 return Status::ok();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700184 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700185
Samuel Tan88c32002015-12-03 16:30:43 -0800186 Status RepeatSimpleParcelable(const SimpleParcelable& input,
187 SimpleParcelable* repeat,
188 SimpleParcelable* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800189 ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
190 *repeat = input;
191 *_aidl_return = input;
192 return Status::ok();
193 }
194
Samuel Tan88c32002015-12-03 16:30:43 -0800195 Status RepeatPersistableBundle(const PersistableBundle& input,
196 PersistableBundle* _aidl_return) override {
197 ALOGI("Repeated a PersistableBundle");
198 *_aidl_return = input;
199 return Status::ok();
200 }
201
202 template <typename T>
203 Status ReverseArray(const vector<T>& input, vector<T>* repeated,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800204 vector<T>* _aidl_return) {
Christopher Wiley8949f832015-10-27 15:32:03 -0700205 ALOGI("Reversing array of length %zu", input.size());
206 *repeated = input;
207 *_aidl_return = input;
208 std::reverse(_aidl_return->begin(), _aidl_return->end());
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800209 return Status::ok();
Christopher Wiley8949f832015-10-27 15:32:03 -0700210 }
211
Casey Dahlin57dbe242015-12-04 11:44:02 -0800212 template<typename T>
213 Status RepeatNullable(const unique_ptr<T>& input,
214 unique_ptr<T>* _aidl_return) {
215 ALOGI("Repeating nullable value");
216
217 _aidl_return->reset();
218 if (input) {
219 _aidl_return->reset(new T(*input));
220 }
221
222 return Status::ok();
223 }
224
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800225 Status ReverseBoolean(const vector<bool>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800226 vector<bool>* repeated,
227 vector<bool>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700228 return ReverseArray(input, repeated, _aidl_return);
229 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800230 Status ReverseByte(const vector<int8_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800231 vector<int8_t>* repeated,
232 vector<int8_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700233 return ReverseArray(input, repeated, _aidl_return);
234 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800235 Status ReverseChar(const vector<char16_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800236 vector<char16_t>* repeated,
237 vector<char16_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700238 return ReverseArray(input, repeated, _aidl_return);
239 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800240 Status ReverseInt(const vector<int32_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800241 vector<int32_t>* repeated,
242 vector<int32_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700243 return ReverseArray(input, repeated, _aidl_return);
244 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800245 Status ReverseLong(const vector<int64_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800246 vector<int64_t>* repeated,
247 vector<int64_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700248 return ReverseArray(input, repeated, _aidl_return);
249 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800250 Status ReverseFloat(const vector<float>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800251 vector<float>* repeated,
252 vector<float>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700253 return ReverseArray(input, repeated, _aidl_return);
254 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800255 Status ReverseDouble(const vector<double>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800256 vector<double>* repeated,
257 vector<double>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700258 return ReverseArray(input, repeated, _aidl_return);
259 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800260 Status ReverseString(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800261 vector<String16>* repeated,
262 vector<String16>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700263 return ReverseArray(input, repeated, _aidl_return);
264 }
Samuel Tan88c32002015-12-03 16:30:43 -0800265 Status ReverseSimpleParcelables(
266 const vector<SimpleParcelable>& input,
267 vector<SimpleParcelable>* repeated,
268 vector<SimpleParcelable>* _aidl_return) override {
269 return ReverseArray(input, repeated, _aidl_return);
270 }
271 Status ReversePersistableBundles(
272 const vector<PersistableBundle>& input,
273 vector<PersistableBundle>* repeated,
274 vector<PersistableBundle>* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800275 return ReverseArray(input, repeated, _aidl_return);
276 }
Casey Dahlin389781f2015-10-22 13:13:21 -0700277
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800278 Status GetOtherTestService(const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800279 sp<INamedCallback>* returned_service) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700280 if (service_map_.find(name) == service_map_.end()) {
281 sp<INamedCallback> new_item(new NamedCallback(name));
282 service_map_[name] = new_item;
283 }
284
285 *returned_service = service_map_[name];
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800286 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -0700287 }
288
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800289 Status VerifyName(const sp<INamedCallback>& service, const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800290 bool* returned_value) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700291 String16 foundName;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800292 Status status = service->GetName(&foundName);
Casey Dahlin389781f2015-10-22 13:13:21 -0700293
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800294 if (status.isOk()) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700295 *returned_value = foundName == name;
296 }
297
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800298 return status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700299 }
300
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800301 Status ReverseStringList(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800302 vector<String16>* repeated,
303 vector<String16>* _aidl_return) override {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700304 return ReverseArray(input, repeated, _aidl_return);
305 }
306
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800307 Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800308 vector<sp<IBinder>>* repeated,
309 vector<sp<IBinder>>* _aidl_return) override {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800310 return ReverseArray(input, repeated, _aidl_return);
311 }
312
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800313 Status RepeatFileDescriptor(const ScopedFd& read,
314 ScopedFd* _aidl_return) override {
315 ALOGE("Repeating file descriptor");
316 *_aidl_return = ScopedFd(dup(read.get()));
317 return Status::ok();
318 }
319
320 Status ReverseFileDescriptorArray(const vector<ScopedFd>& input,
321 vector<ScopedFd>* repeated,
322 vector<ScopedFd>* _aidl_return) override {
323 ALOGI("Reversing descriptor array of length %zu", input.size());
324 for (const auto& item : input) {
325 repeated->push_back(ScopedFd(dup(item.get())));
326 _aidl_return->push_back(ScopedFd(dup(item.get())));
327 }
328 std::reverse(_aidl_return->begin(), _aidl_return->end());
329 return Status::ok();
330 }
331
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800332 Status ThrowServiceException(int code) override {
333 return Status::fromServiceSpecificError(code);
334 }
335
Casey Dahlin57dbe242015-12-04 11:44:02 -0800336 Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input,
337 unique_ptr<vector<int32_t>>* _aidl_return) {
338 return RepeatNullable(input, _aidl_return);
339 }
340
341 Status RepeatNullableStringList(
342 const unique_ptr<vector<unique_ptr<String16>>>& input,
343 unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) {
344 ALOGI("Repeating nullable string list");
345 if (!input) {
346 _aidl_return->reset();
347 return Status::ok();
348 }
349
350 _aidl_return->reset(new vector<unique_ptr<String16>>);
351
352 for (const auto& item : *input) {
353 if (!item) {
354 (*_aidl_return)->emplace_back(nullptr);
355 } else {
356 (*_aidl_return)->emplace_back(new String16(*item));
357 }
358 }
359
360 return Status::ok();
361 }
362
363 Status RepeatNullableString(const unique_ptr<String16>& input,
364 unique_ptr<String16>* _aidl_return) {
365 return RepeatNullable(input, _aidl_return);
366 }
367
368 Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input,
369 unique_ptr<SimpleParcelable>* _aidl_return) {
370 return RepeatNullable(input, _aidl_return);
371 }
372
Casey Dahlin389781f2015-10-22 13:13:21 -0700373 private:
374 map<String16, sp<INamedCallback>> service_map_;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700375};
376
377} // namespace
378} // namespace generated
379} // namespace android
380
Brian Carlstromad3b8062015-10-21 08:54:48 -0700381int main(int /* argc */, char* /* argv */ []) {
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700382 android::generated::NativeService service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700383 return service.Run();
384}