blob: 5c3d67211fcdf662076079346a4656dfb43aec37 [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
Christopher Wileyb5e698c2015-10-17 09:32:22 -070022#include <binder/IInterface.h>
23#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
25#include <binder/ProcessState.h>
26#include <utils/Errors.h>
27#include <utils/Log.h>
28#include <utils/Looper.h>
29#include <utils/StrongPointer.h>
30
Christopher Wiley521bb612015-10-22 11:40:30 -070031#include "android/aidl/tests/BnTestService.h"
32#include "android/aidl/tests/ITestService.h"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070033
Casey Dahlin389781f2015-10-22 13:13:21 -070034#include "android/aidl/tests/BnNamedCallback.h"
35#include "android/aidl/tests/INamedCallback.h"
36
Christopher Wileyb5e698c2015-10-17 09:32:22 -070037// Used implicitly.
38#undef LOG_TAG
Christopher Wiley33ad81e2015-10-21 14:41:11 -070039#define LOG_TAG "aidl_native_service"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070040
41// libutils:
42using android::Looper;
43using android::LooperCallback;
44using android::OK;
45using android::sp;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070046using android::String16;
47
48// libbinder:
49using android::BnInterface;
50using android::defaultServiceManager;
51using android::IInterface;
52using android::IPCThreadState;
53using android::Parcel;
54using android::ProcessState;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080055using android::binder::Status;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070056
57// Generated code:
Christopher Wiley521bb612015-10-22 11:40:30 -070058using android::aidl::tests::BnTestService;
Casey Dahlin389781f2015-10-22 13:13:21 -070059using android::aidl::tests::BnNamedCallback;
60using android::aidl::tests::INamedCallback;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070061
Casey Dahlin389781f2015-10-22 13:13:21 -070062// Standard library
63using std::map;
Christopher Wiley8949f832015-10-27 15:32:03 -070064using std::vector;
65
Christopher Wileyb5e698c2015-10-17 09:32:22 -070066namespace android {
67namespace generated {
68namespace {
69
70class BinderCallback : public LooperCallback {
71 public:
72 BinderCallback() {}
73 ~BinderCallback() override {}
74
Brian Carlstromad3b8062015-10-21 08:54:48 -070075 int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070076 IPCThreadState::self()->handlePolledCommands();
77 return 1; // Continue receiving callbacks.
78 }
79};
80
Casey Dahlin389781f2015-10-22 13:13:21 -070081class NamedCallback : public BnNamedCallback {
82 public:
83 NamedCallback(String16 name) : name_(name) {}
84
Christopher Wiley433c8bb2015-11-12 14:20:46 -080085 Status GetName(String16* ret) {
Casey Dahlin389781f2015-10-22 13:13:21 -070086 *ret = name_;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080087 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -070088 }
89
90 private:
91 String16 name_;
92};
93
Christopher Wiley521bb612015-10-22 11:40:30 -070094class NativeService : public BnTestService {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070095 public:
Christopher Wiley33ad81e2015-10-21 14:41:11 -070096 NativeService() {}
97 ~NativeService() override {}
Christopher Wileyb5e698c2015-10-17 09:32:22 -070098
99 int Run() {
100 sp<Looper> looper(Looper::prepare(0 /* opts */));
101
102 int binder_fd = -1;
103 ProcessState::self()->setThreadPoolMaxThreadCount(0);
104 IPCThreadState::self()->disableBackgroundScheduling(true);
105 IPCThreadState::self()->setupPolling(&binder_fd);
106 ALOGI("Got binder FD %d", binder_fd);
107 if (binder_fd < 0) return -1;
108
109 sp<BinderCallback> cb(new BinderCallback);
110 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
111 nullptr) != 1) {
112 ALOGE("Failed to add binder FD to Looper");
113 return -1;
114 }
115
116 defaultServiceManager()->addService(getInterfaceDescriptor(), this);
117
118 ALOGI("Entering loop");
119 while (true) {
120 const int result = looper->pollAll(-1 /* timeoutMillis */);
121 ALOGI("Looper returned %d", result);
122 }
123 return 0;
124 }
125
Christopher Wileyd6130f22015-10-26 10:24:35 -0700126 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700127 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700128 token.size());
129 }
130
131 template<typename T>
132 void LogRepeatedToken(const T& token) {
133 std::ostringstream token_str;
134 token_str << token;
135 ALOGI("Repeating token %s", token_str.str().c_str());
136 }
137
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800138 Status RepeatBoolean(bool token, bool* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700139 LogRepeatedToken(token ? 1 : 0);
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 RepeatByte(int8_t token, int8_t* _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 RepeatChar(char16_t token, char16_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700149 LogRepeatedStringToken(String16(&token, 1));
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 RepeatInt(int32_t token, int32_t* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700154 LogRepeatedToken(token);
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 RepeatLong(int64_t token, int64_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 RepeatFloat(float token, float* _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 RepeatDouble(double token, double* _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 RepeatString(
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700174 const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700175 LogRepeatedStringToken(token);
176 *_aidl_return = token;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800177 return Status::ok();
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700178 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700179
180 template<typename T>
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800181 Status ReverseArray(const vector<T>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700182 vector<T>* repeated,
Christopher Wiley47964b02015-10-28 14:01:25 -0700183 vector<T>* _aidl_return) {
Christopher Wiley8949f832015-10-27 15:32:03 -0700184 ALOGI("Reversing array of length %zu", input.size());
185 *repeated = input;
186 *_aidl_return = input;
187 std::reverse(_aidl_return->begin(), _aidl_return->end());
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800188 return Status::ok();
Christopher Wiley8949f832015-10-27 15:32:03 -0700189 }
190
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800191 Status ReverseBoolean(const vector<bool>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700192 vector<bool>* repeated,
193 vector<bool>* _aidl_return) override {
194 return ReverseArray(input, repeated, _aidl_return);
195 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800196 Status ReverseByte(const vector<int8_t>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700197 vector<int8_t>* repeated,
198 vector<int8_t>* _aidl_return) override {
199 return ReverseArray(input, repeated, _aidl_return);
200 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800201 Status ReverseChar(const vector<char16_t>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700202 vector<char16_t>* repeated,
203 vector<char16_t>* _aidl_return) override {
204 return ReverseArray(input, repeated, _aidl_return);
205 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800206 Status ReverseInt(const vector<int32_t>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700207 vector<int32_t>* repeated,
208 vector<int32_t>* _aidl_return) override {
209 return ReverseArray(input, repeated, _aidl_return);
210 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800211 Status ReverseLong(const vector<int64_t>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700212 vector<int64_t>* repeated,
213 vector<int64_t>* _aidl_return) override {
214 return ReverseArray(input, repeated, _aidl_return);
215 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800216 Status ReverseFloat(const vector<float>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700217 vector<float>* repeated,
218 vector<float>* _aidl_return) override {
219 return ReverseArray(input, repeated, _aidl_return);
220 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800221 Status ReverseDouble(const vector<double>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700222 vector<double>* repeated,
223 vector<double>* _aidl_return) override {
224 return ReverseArray(input, repeated, _aidl_return);
225 }
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800226 Status ReverseString(const vector<String16>& input,
Christopher Wiley8949f832015-10-27 15:32:03 -0700227 vector<String16>* repeated,
228 vector<String16>* _aidl_return) override {
229 return ReverseArray(input, repeated, _aidl_return);
230 }
Casey Dahlin389781f2015-10-22 13:13:21 -0700231
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800232 Status GetOtherTestService(const String16& name,
Casey Dahlin389781f2015-10-22 13:13:21 -0700233 sp<INamedCallback>* returned_service) override {
234 if (service_map_.find(name) == service_map_.end()) {
235 sp<INamedCallback> new_item(new NamedCallback(name));
236 service_map_[name] = new_item;
237 }
238
239 *returned_service = service_map_[name];
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800240 return Status::ok();
Casey Dahlin389781f2015-10-22 13:13:21 -0700241 }
242
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800243 Status VerifyName(const sp<INamedCallback>& service, const String16& name,
Casey Dahlin389781f2015-10-22 13:13:21 -0700244 bool* returned_value) override {
245 String16 foundName;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800246 Status status = service->GetName(&foundName);
Casey Dahlin389781f2015-10-22 13:13:21 -0700247
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800248 if (status.isOk()) {
Casey Dahlin389781f2015-10-22 13:13:21 -0700249 *returned_value = foundName == name;
250 }
251
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800252 return status;
Casey Dahlin389781f2015-10-22 13:13:21 -0700253 }
254
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800255 Status ReverseStringList(const vector<String16>& input,
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700256 vector<String16>* repeated,
257 vector<String16>* _aidl_return) override {
258 return ReverseArray(input, repeated, _aidl_return);
259 }
260
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800261 Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800262 vector<sp<IBinder>>* repeated,
263 vector<sp<IBinder>>* _aidl_return) override {
264 return ReverseArray(input, repeated, _aidl_return);
265 }
266
Casey Dahlin389781f2015-10-22 13:13:21 -0700267 private:
268 map<String16, sp<INamedCallback>> service_map_;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700269};
270
271} // namespace
272} // namespace generated
273} // namespace android
274
Brian Carlstromad3b8062015-10-21 08:54:48 -0700275int main(int /* argc */, char* /* argv */ []) {
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700276 android::generated::NativeService service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700277 return service.Run();
278}