blob: b41cac705c092e3c100ba1d205ff737392d18bf3 [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;
46using android::status_t;
47using android::String16;
48
49// libbinder:
50using android::BnInterface;
51using android::defaultServiceManager;
52using android::IInterface;
53using android::IPCThreadState;
54using android::Parcel;
55using android::ProcessState;
56
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
85 status_t GetName(String16* ret) {
86 *ret = name_;
87 return OK;
88 }
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
138 status_t RepeatBoolean(bool token, bool* _aidl_return) override {
139 LogRepeatedToken(token ? 1 : 0);
140 *_aidl_return = token;
141 return OK;
142 }
143 status_t RepeatByte(int8_t token, int8_t* _aidl_return) override {
144 LogRepeatedToken(token);
145 *_aidl_return = token;
146 return OK;
147 }
148 status_t RepeatChar(char16_t token, char16_t* _aidl_return) override {
149 LogRepeatedStringToken(String16(&token, 1));
150 *_aidl_return = token;
151 return OK;
152 }
153 status_t RepeatInt(int32_t token, int32_t* _aidl_return) override {
154 LogRepeatedToken(token);
155 *_aidl_return = token;
156 return OK;
157 }
158 status_t RepeatLong(int64_t token, int64_t* _aidl_return) override {
159 LogRepeatedToken(token);
160 *_aidl_return = token;
161 return OK;
162 }
163 status_t RepeatFloat(float token, float* _aidl_return) override {
164 LogRepeatedToken(token);
165 *_aidl_return = token;
166 return OK;
167 }
168 status_t RepeatDouble(double token, double* _aidl_return) override {
169 LogRepeatedToken(token);
170 *_aidl_return = token;
171 return OK;
172 }
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700173 status_t RepeatString(
174 const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700175 LogRepeatedStringToken(token);
176 *_aidl_return = token;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700177 return OK;
178 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700179
180 template<typename T>
181 status_t ReverseArray(const vector<T>& input,
182 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());
188 return OK;
189 }
190
191 status_t ReverseBoolean(const vector<bool>& input,
192 vector<bool>* repeated,
193 vector<bool>* _aidl_return) override {
194 return ReverseArray(input, repeated, _aidl_return);
195 }
196 status_t ReverseByte(const vector<int8_t>& input,
197 vector<int8_t>* repeated,
198 vector<int8_t>* _aidl_return) override {
199 return ReverseArray(input, repeated, _aidl_return);
200 }
201 status_t ReverseChar(const vector<char16_t>& input,
202 vector<char16_t>* repeated,
203 vector<char16_t>* _aidl_return) override {
204 return ReverseArray(input, repeated, _aidl_return);
205 }
206 status_t ReverseInt(const vector<int32_t>& input,
207 vector<int32_t>* repeated,
208 vector<int32_t>* _aidl_return) override {
209 return ReverseArray(input, repeated, _aidl_return);
210 }
211 status_t ReverseLong(const vector<int64_t>& input,
212 vector<int64_t>* repeated,
213 vector<int64_t>* _aidl_return) override {
214 return ReverseArray(input, repeated, _aidl_return);
215 }
216 status_t ReverseFloat(const vector<float>& input,
217 vector<float>* repeated,
218 vector<float>* _aidl_return) override {
219 return ReverseArray(input, repeated, _aidl_return);
220 }
221 status_t ReverseDouble(const vector<double>& input,
222 vector<double>* repeated,
223 vector<double>* _aidl_return) override {
224 return ReverseArray(input, repeated, _aidl_return);
225 }
226 status_t ReverseString(const vector<String16>& input,
227 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
232 status_t GetOtherTestService(const String16& name,
233 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];
240 return OK;
241 }
242
243 status_t VerifyName(const sp<INamedCallback>& service, const String16& name,
244 bool* returned_value) override {
245 String16 foundName;
246 status_t err = service->GetName(&foundName);
247
248 if (err == OK) {
249 *returned_value = foundName == name;
250 }
251
252 return err;
253 }
254
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700255 status_t ReverseStringList(const vector<String16>& input,
256 vector<String16>* repeated,
257 vector<String16>* _aidl_return) override {
258 return ReverseArray(input, repeated, _aidl_return);
259 }
260
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800261 status_t ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
262 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}