blob: d68842b9a24235a4976500f3a797e9ea27f21898 [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;
Christopher Wiley8949f832015-10-27 15:32:03 -070069using std::vector;
70
Christopher Wileyb5e698c2015-10-17 09:32:22 -070071namespace android {
72namespace generated {
73namespace {
74
75class BinderCallback : public LooperCallback {
76 public:
77 BinderCallback() {}
78 ~BinderCallback() override {}
79
Brian Carlstromad3b8062015-10-21 08:54:48 -070080 int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070081 IPCThreadState::self()->handlePolledCommands();
82 return 1; // Continue receiving callbacks.
83 }
84};
85
Casey Dahlin389781f2015-10-22 13:13:21 -070086class NamedCallback : public BnNamedCallback {
87 public:
88 NamedCallback(String16 name) : name_(name) {}
89
Christopher Wiley433c8bb2015-11-12 14:20:46 -080090 Status GetName(String16* ret) {
Casey Dahlin389781f2015-10-22 13:13:21 -070091 *ret = name_;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080092 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -070093 }
94
95 private:
96 String16 name_;
97};
98
Christopher Wiley521bb612015-10-22 11:40:30 -070099class NativeService : public BnTestService {
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700100 public:
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700101 NativeService() {}
Christopher Wiley7621d4d2015-11-28 16:58:06 -0800102 virtual ~NativeService() = default;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700103
104 int Run() {
105 sp<Looper> looper(Looper::prepare(0 /* opts */));
106
107 int binder_fd = -1;
108 ProcessState::self()->setThreadPoolMaxThreadCount(0);
109 IPCThreadState::self()->disableBackgroundScheduling(true);
110 IPCThreadState::self()->setupPolling(&binder_fd);
111 ALOGI("Got binder FD %d", binder_fd);
112 if (binder_fd < 0) return -1;
113
114 sp<BinderCallback> cb(new BinderCallback);
115 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
116 nullptr) != 1) {
117 ALOGE("Failed to add binder FD to Looper");
118 return -1;
119 }
120
121 defaultServiceManager()->addService(getInterfaceDescriptor(), this);
122
123 ALOGI("Entering loop");
124 while (true) {
125 const int result = looper->pollAll(-1 /* timeoutMillis */);
126 ALOGI("Looper returned %d", result);
127 }
128 return 0;
129 }
130
Christopher Wileyd6130f22015-10-26 10:24:35 -0700131 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700132 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700133 token.size());
134 }
135
136 template<typename T>
137 void LogRepeatedToken(const T& token) {
138 std::ostringstream token_str;
139 token_str << token;
140 ALOGI("Repeating token %s", token_str.str().c_str());
141 }
142
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800143 Status RepeatBoolean(bool token, bool* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700144 LogRepeatedToken(token ? 1 : 0);
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 RepeatByte(int8_t token, int8_t* _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 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800153 Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700154 LogRepeatedStringToken(String16(&token, 1));
155 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800156 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700157 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800158 Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700159 LogRepeatedToken(token);
160 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800161 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700162 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800163 Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700164 LogRepeatedToken(token);
165 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800166 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700167 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800168 Status RepeatFloat(float token, float* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700169 LogRepeatedToken(token);
170 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800171 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700172 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800173 Status RepeatDouble(double token, double* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700174 LogRepeatedToken(token);
175 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800176 return Status::ok();
Christopher Wileyd6130f22015-10-26 10:24:35 -0700177 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800178 Status RepeatString(
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700179 const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700180 LogRepeatedStringToken(token);
181 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800182 return Status::ok();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700183 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700184
Christopher Wiley95d44b02015-11-19 07:11:30 -0800185 Status RepeatParcelable(const SimpleParcelable& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800186 SimpleParcelable* repeat,
187 SimpleParcelable* _aidl_return) override {
Christopher Wiley95d44b02015-11-19 07:11:30 -0800188 ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
189 *repeat = input;
190 *_aidl_return = input;
191 return Status::ok();
192 }
193
Christopher Wiley8949f832015-10-27 15:32:03 -0700194 template<typename T>
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800195 Status ReverseArray(const vector<T>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800196 vector<T>* repeated,
197 vector<T>* _aidl_return) {
Christopher Wiley8949f832015-10-27 15:32:03 -0700198 ALOGI("Reversing array of length %zu", input.size());
199 *repeated = input;
200 *_aidl_return = input;
201 std::reverse(_aidl_return->begin(), _aidl_return->end());
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800202 return Status::ok();
Christopher Wiley8949f832015-10-27 15:32:03 -0700203 }
204
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800205 Status ReverseBoolean(const vector<bool>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800206 vector<bool>* repeated,
207 vector<bool>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700208 return ReverseArray(input, repeated, _aidl_return);
209 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800210 Status ReverseByte(const vector<int8_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800211 vector<int8_t>* repeated,
212 vector<int8_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700213 return ReverseArray(input, repeated, _aidl_return);
214 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800215 Status ReverseChar(const vector<char16_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800216 vector<char16_t>* repeated,
217 vector<char16_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700218 return ReverseArray(input, repeated, _aidl_return);
219 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800220 Status ReverseInt(const vector<int32_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800221 vector<int32_t>* repeated,
222 vector<int32_t>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700223 return ReverseArray(input, repeated, _aidl_return);
224 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800225 Status ReverseLong(const vector<int64_t>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800226 vector<int64_t>* repeated,
227 vector<int64_t>* _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 ReverseFloat(const vector<float>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800231 vector<float>* repeated,
232 vector<float>* _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 ReverseDouble(const vector<double>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800236 vector<double>* repeated,
237 vector<double>* _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 ReverseString(const vector<String16>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800241 vector<String16>* repeated,
242 vector<String16>* _aidl_return) override {
Christopher Wiley8949f832015-10-27 15:32:03 -0700243 return ReverseArray(input, repeated, _aidl_return);
244 }
Christopher Wiley95d44b02015-11-19 07:11:30 -0800245 Status ReverseParcelables(const vector<SimpleParcelable>& input,
Casey Dahlina4ba4b62015-11-02 15:43:30 -0800246 vector<SimpleParcelable>* repeated,
247 vector<SimpleParcelable>* _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 Dahlin389781f2015-10-22 13:13:21 -0700309 private:
310 map<String16, sp<INamedCallback>> service_map_;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700311};
312
313} // namespace
314} // namespace generated
315} // namespace android
316
Brian Carlstromad3b8062015-10-21 08:54:48 -0700317int main(int /* argc */, char* /* argv */ []) {
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700318 android::generated::NativeService service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700319 return service.Run();
320}