blob: 66ab2a64d48a3ee3b0e269f0859805dd47ee8601 [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:
Christopher Wiley521bb612015-10-22 11:40:30 -070062using android::aidl::tests::BnTestService;
Casey Dahlin389781f2015-10-22 13:13:21 -070063using android::aidl::tests::BnNamedCallback;
64using android::aidl::tests::INamedCallback;
Christopher Wiley95d44b02015-11-19 07:11:30 -080065using android::aidl::tests::SimpleParcelable;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070066
Casey Dahlin389781f2015-10-22 13:13:21 -070067// Standard library
68using std::map;
Casey Dahlin57dbe242015-12-04 11:44:02 -080069using std::unique_ptr;
Christopher Wiley8949f832015-10-27 15:32:03 -070070using std::vector;
71
Christopher Wileyb5e698c2015-10-17 09:32:22 -070072namespace android {
73namespace generated {
74namespace {
75
76class BinderCallback : public LooperCallback {
77 public:
78 BinderCallback() {}
79 ~BinderCallback() override {}
80
Brian Carlstromad3b8062015-10-21 08:54:48 -070081 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
105 int Run() {
106 sp<Looper> looper(Looper::prepare(0 /* opts */));
107
108 int binder_fd = -1;
109 ProcessState::self()->setThreadPoolMaxThreadCount(0);
110 IPCThreadState::self()->disableBackgroundScheduling(true);
111 IPCThreadState::self()->setupPolling(&binder_fd);
112 ALOGI("Got binder FD %d", binder_fd);
113 if (binder_fd < 0) return -1;
114
115 sp<BinderCallback> cb(new BinderCallback);
116 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
117 nullptr) != 1) {
118 ALOGE("Failed to add binder FD to Looper");
119 return -1;
120 }
121
122 defaultServiceManager()->addService(getInterfaceDescriptor(), this);
123
124 ALOGI("Entering loop");
125 while (true) {
126 const int result = looper->pollAll(-1 /* timeoutMillis */);
127 ALOGI("Looper returned %d", result);
128 }
129 return 0;
130 }
131
Christopher Wileyd6130f22015-10-26 10:24:35 -0700132 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700133 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700134 token.size());
135 }
136
137 template<typename T>
138 void LogRepeatedToken(const T& token) {
139 std::ostringstream token_str;
140 token_str << token;
141 ALOGI("Repeating token %s", token_str.str().c_str());
142 }
143
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800144 Status RepeatBoolean(bool token, bool* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700145 LogRepeatedToken(token ? 1 : 0);
146 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800147 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700148 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800149 Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700150 LogRepeatedToken(token);
151 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800152 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700153 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800154 Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700155 LogRepeatedStringToken(String16(&token, 1));
156 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800157 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700158 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800159 Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700160 LogRepeatedToken(token);
161 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800162 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700163 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800164 Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700165 LogRepeatedToken(token);
166 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800167 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700168 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800169 Status RepeatFloat(float token, float* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700170 LogRepeatedToken(token);
171 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800172 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700173 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800174 Status RepeatDouble(double token, double* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700175 LogRepeatedToken(token);
176 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800177 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700178 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800179 Status RepeatString(
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700180 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
Christopher Wiley95d44b02015-11-19 07:11:30 -0800186 Status RepeatParcelable(const SimpleParcelable& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800187 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
Christopher Wiley8949f832015-10-27 15:32:03 -0700195 template<typename T>
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800196 Status ReverseArray(const vector<T>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800197 vector<T>* repeated,
198 vector<T>* _aidl_return) {
Christopher Wiley8949f832015-10-27 15:32:03 -0700199 ALOGI("Reversing array of length %zu", input.size());
200 *repeated = input;
201 *_aidl_return = input;
202 std::reverse(_aidl_return->begin(), _aidl_return->end());
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800203 return Status::ok();
Christopher Wiley8949f832015-10-27 15:32:03 -0700204 }
205
Casey Dahlin57dbe242015-12-04 11:44:02 -0800206 template<typename T>
207 Status RepeatNullable(const unique_ptr<T>& input,
208 unique_ptr<T>* _aidl_return) {
209 ALOGI("Repeating nullable value");
210
211 _aidl_return->reset();
212 if (input) {
213 _aidl_return->reset(new T(*input));
214 }
215
216 return Status::ok();
217 }
218
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800219 Status ReverseBoolean(const vector<bool>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800220 vector<bool>* repeated,
221 vector<bool>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700222 return ReverseArray(input, repeated, _aidl_return);
223 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800224 Status ReverseByte(const vector<int8_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800225 vector<int8_t>* repeated,
226 vector<int8_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700227 return ReverseArray(input, repeated, _aidl_return);
228 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800229 Status ReverseChar(const vector<char16_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800230 vector<char16_t>* repeated,
231 vector<char16_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700232 return ReverseArray(input, repeated, _aidl_return);
233 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800234 Status ReverseInt(const vector<int32_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800235 vector<int32_t>* repeated,
236 vector<int32_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700237 return ReverseArray(input, repeated, _aidl_return);
238 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800239 Status ReverseLong(const vector<int64_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800240 vector<int64_t>* repeated,
241 vector<int64_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700242 return ReverseArray(input, repeated, _aidl_return);
243 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800244 Status ReverseFloat(const vector<float>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800245 vector<float>* repeated,
246 vector<float>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700247 return ReverseArray(input, repeated, _aidl_return);
248 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800249 Status ReverseDouble(const vector<double>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800250 vector<double>* repeated,
251 vector<double>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700252 return ReverseArray(input, repeated, _aidl_return);
253 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800254 Status ReverseString(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800255 vector<String16>* repeated,
256 vector<String16>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700257 return ReverseArray(input, repeated, _aidl_return);
258 }
Christopher Wiley95d44b02015-11-19 07:11:30 -0800259 Status ReverseParcelables(const vector<SimpleParcelable>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800260 vector<SimpleParcelable>* repeated,
261 vector<SimpleParcelable>* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800262 return ReverseArray(input, repeated, _aidl_return);
263 }
Casey Dahlin389781f2015-10-22 13:13:21 -0700264
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800265 Status GetOtherTestService(const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800266 sp<INamedCallback>* returned_service) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700267 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];
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800273 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -0700274 }
275
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800276 Status VerifyName(const sp<INamedCallback>& service, const String16& name,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800277 bool* returned_value) override {
Casey Dahlin389781f2015-10-22 13:13:21 -0700278 String16 foundName;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800279 Status status = service->GetName(&foundName);
Casey Dahlin389781f2015-10-22 13:13:21 -0700280
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800281 if (status.isOk()) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700282 *returned_value = foundName == name;
283 }
284
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800285 return status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700286 }
287
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800288 Status ReverseStringList(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800289 vector<String16>* repeated,
290 vector<String16>* _aidl_return) override {
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700291 return ReverseArray(input, repeated, _aidl_return);
292 }
293
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800294 Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800295 vector<sp<IBinder>>* repeated,
296 vector<sp<IBinder>>* _aidl_return) override {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800297 return ReverseArray(input, repeated, _aidl_return);
298 }
299
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800300 Status RepeatFileDescriptor(const ScopedFd& read,
301 ScopedFd* _aidl_return) override {
302 ALOGE("Repeating file descriptor");
303 *_aidl_return = ScopedFd(dup(read.get()));
304 return Status::ok();
305 }
306
307 Status ReverseFileDescriptorArray(const vector<ScopedFd>& input,
308 vector<ScopedFd>* repeated,
309 vector<ScopedFd>* _aidl_return) override {
310 ALOGI("Reversing descriptor array of length %zu", input.size());
311 for (const auto& item : input) {
312 repeated->push_back(ScopedFd(dup(item.get())));
313 _aidl_return->push_back(ScopedFd(dup(item.get())));
314 }
315 std::reverse(_aidl_return->begin(), _aidl_return->end());
316 return Status::ok();
317 }
318
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800319 Status ThrowServiceException(int code) override {
320 return Status::fromServiceSpecificError(code);
321 }
322
Casey Dahlin57dbe242015-12-04 11:44:02 -0800323 Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input,
324 unique_ptr<vector<int32_t>>* _aidl_return) {
325 return RepeatNullable(input, _aidl_return);
326 }
327
328 Status RepeatNullableStringList(
329 const unique_ptr<vector<unique_ptr<String16>>>& input,
330 unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) {
331 ALOGI("Repeating nullable string list");
332 if (!input) {
333 _aidl_return->reset();
334 return Status::ok();
335 }
336
337 _aidl_return->reset(new vector<unique_ptr<String16>>);
338
339 for (const auto& item : *input) {
340 if (!item) {
341 (*_aidl_return)->emplace_back(nullptr);
342 } else {
343 (*_aidl_return)->emplace_back(new String16(*item));
344 }
345 }
346
347 return Status::ok();
348 }
349
350 Status RepeatNullableString(const unique_ptr<String16>& input,
351 unique_ptr<String16>* _aidl_return) {
352 return RepeatNullable(input, _aidl_return);
353 }
354
355 Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input,
356 unique_ptr<SimpleParcelable>* _aidl_return) {
357 return RepeatNullable(input, _aidl_return);
358 }
359
Casey Dahlin389781f2015-10-22 13:13:21 -0700360 private:
361 map<String16, sp<INamedCallback>> service_map_;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700362};
363
364} // namespace
365} // namespace generated
366} // namespace android
367
Brian Carlstromad3b8062015-10-21 08:54:48 -0700368int main(int /* argc */, char* /* argv */ []) {
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700369 android::generated::NativeService service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700370 return service.Run();
371}