blob: 11222fc42be6efbde53fafb3adcfb3ef40c8c906 [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
Christopher Wileyd6130f22015-10-26 10:24:35 -070017#include <sstream>
18#include <string>
Christopher Wiley8949f832015-10-27 15:32:03 -070019#include <vector>
Christopher Wileyd6130f22015-10-26 10:24:35 -070020
Christopher Wileyb5e698c2015-10-17 09:32:22 -070021#include <binder/IInterface.h>
22#include <binder/IPCThreadState.h>
23#include <binder/IServiceManager.h>
24#include <binder/ProcessState.h>
25#include <utils/Errors.h>
26#include <utils/Log.h>
27#include <utils/Looper.h>
28#include <utils/StrongPointer.h>
29
Christopher Wiley521bb612015-10-22 11:40:30 -070030#include "android/aidl/tests/BnTestService.h"
31#include "android/aidl/tests/ITestService.h"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070032
33// Used implicitly.
34#undef LOG_TAG
Christopher Wiley33ad81e2015-10-21 14:41:11 -070035#define LOG_TAG "aidl_native_service"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070036
37// libutils:
38using android::Looper;
39using android::LooperCallback;
40using android::OK;
41using android::sp;
42using android::status_t;
43using android::String16;
44
45// libbinder:
46using android::BnInterface;
47using android::defaultServiceManager;
48using android::IInterface;
49using android::IPCThreadState;
50using android::Parcel;
51using android::ProcessState;
52
53// Generated code:
Christopher Wiley521bb612015-10-22 11:40:30 -070054using android::aidl::tests::BnTestService;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070055
Christopher Wiley8949f832015-10-27 15:32:03 -070056using std::vector;
57
Christopher Wileyb5e698c2015-10-17 09:32:22 -070058namespace android {
59namespace generated {
60namespace {
61
62class BinderCallback : public LooperCallback {
63 public:
64 BinderCallback() {}
65 ~BinderCallback() override {}
66
Brian Carlstromad3b8062015-10-21 08:54:48 -070067 int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070068 IPCThreadState::self()->handlePolledCommands();
69 return 1; // Continue receiving callbacks.
70 }
71};
72
Christopher Wiley521bb612015-10-22 11:40:30 -070073class NativeService : public BnTestService {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070074 public:
Christopher Wiley33ad81e2015-10-21 14:41:11 -070075 NativeService() {}
76 ~NativeService() override {}
Christopher Wileyb5e698c2015-10-17 09:32:22 -070077
78 int Run() {
79 sp<Looper> looper(Looper::prepare(0 /* opts */));
80
81 int binder_fd = -1;
82 ProcessState::self()->setThreadPoolMaxThreadCount(0);
83 IPCThreadState::self()->disableBackgroundScheduling(true);
84 IPCThreadState::self()->setupPolling(&binder_fd);
85 ALOGI("Got binder FD %d", binder_fd);
86 if (binder_fd < 0) return -1;
87
88 sp<BinderCallback> cb(new BinderCallback);
89 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
90 nullptr) != 1) {
91 ALOGE("Failed to add binder FD to Looper");
92 return -1;
93 }
94
95 defaultServiceManager()->addService(getInterfaceDescriptor(), this);
96
97 ALOGI("Entering loop");
98 while (true) {
99 const int result = looper->pollAll(-1 /* timeoutMillis */);
100 ALOGI("Looper returned %d", result);
101 }
102 return 0;
103 }
104
Christopher Wileyd6130f22015-10-26 10:24:35 -0700105 void LogRepeatedStringToken(const String16& token) {
Chih-Hung Hsieh048b55e2015-10-27 15:07:50 -0700106 ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
Christopher Wileyd6130f22015-10-26 10:24:35 -0700107 token.size());
108 }
109
110 template<typename T>
111 void LogRepeatedToken(const T& token) {
112 std::ostringstream token_str;
113 token_str << token;
114 ALOGI("Repeating token %s", token_str.str().c_str());
115 }
116
117 status_t RepeatBoolean(bool token, bool* _aidl_return) override {
118 LogRepeatedToken(token ? 1 : 0);
119 *_aidl_return = token;
120 return OK;
121 }
122 status_t RepeatByte(int8_t token, int8_t* _aidl_return) override {
123 LogRepeatedToken(token);
124 *_aidl_return = token;
125 return OK;
126 }
127 status_t RepeatChar(char16_t token, char16_t* _aidl_return) override {
128 LogRepeatedStringToken(String16(&token, 1));
129 *_aidl_return = token;
130 return OK;
131 }
132 status_t RepeatInt(int32_t token, int32_t* _aidl_return) override {
133 LogRepeatedToken(token);
134 *_aidl_return = token;
135 return OK;
136 }
137 status_t RepeatLong(int64_t token, int64_t* _aidl_return) override {
138 LogRepeatedToken(token);
139 *_aidl_return = token;
140 return OK;
141 }
142 status_t RepeatFloat(float token, float* _aidl_return) override {
143 LogRepeatedToken(token);
144 *_aidl_return = token;
145 return OK;
146 }
147 status_t RepeatDouble(double token, double* _aidl_return) override {
148 LogRepeatedToken(token);
149 *_aidl_return = token;
150 return OK;
151 }
Christopher Wileyb8e49a42015-10-27 12:55:18 -0700152 status_t RepeatString(
153 const String16& token, String16* _aidl_return) override {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700154 LogRepeatedStringToken(token);
155 *_aidl_return = token;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700156 return OK;
157 }
Christopher Wiley8949f832015-10-27 15:32:03 -0700158
159 template<typename T>
160 status_t ReverseArray(const vector<T>& input,
161 vector<T>* repeated,
162 vector<T>* _aidl_return) override {
163 ALOGI("Reversing array of length %zu", input.size());
164 *repeated = input;
165 *_aidl_return = input;
166 std::reverse(_aidl_return->begin(), _aidl_return->end());
167 return OK;
168 }
169
170 status_t ReverseBoolean(const vector<bool>& input,
171 vector<bool>* repeated,
172 vector<bool>* _aidl_return) override {
173 return ReverseArray(input, repeated, _aidl_return);
174 }
175 status_t ReverseByte(const vector<int8_t>& input,
176 vector<int8_t>* repeated,
177 vector<int8_t>* _aidl_return) override {
178 return ReverseArray(input, repeated, _aidl_return);
179 }
180 status_t ReverseChar(const vector<char16_t>& input,
181 vector<char16_t>* repeated,
182 vector<char16_t>* _aidl_return) override {
183 return ReverseArray(input, repeated, _aidl_return);
184 }
185 status_t ReverseInt(const vector<int32_t>& input,
186 vector<int32_t>* repeated,
187 vector<int32_t>* _aidl_return) override {
188 return ReverseArray(input, repeated, _aidl_return);
189 }
190 status_t ReverseLong(const vector<int64_t>& input,
191 vector<int64_t>* repeated,
192 vector<int64_t>* _aidl_return) override {
193 return ReverseArray(input, repeated, _aidl_return);
194 }
195 status_t ReverseFloat(const vector<float>& input,
196 vector<float>* repeated,
197 vector<float>* _aidl_return) override {
198 return ReverseArray(input, repeated, _aidl_return);
199 }
200 status_t ReverseDouble(const vector<double>& input,
201 vector<double>* repeated,
202 vector<double>* _aidl_return) override {
203 return ReverseArray(input, repeated, _aidl_return);
204 }
205 status_t ReverseString(const vector<String16>& input,
206 vector<String16>* repeated,
207 vector<String16>* _aidl_return) override {
208 return ReverseArray(input, repeated, _aidl_return);
209 }
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700210};
211
212} // namespace
213} // namespace generated
214} // namespace android
215
Brian Carlstromad3b8062015-10-21 08:54:48 -0700216int main(int /* argc */, char* /* argv */ []) {
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700217 android::generated::NativeService service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700218 return service.Run();
219}