aidl_test_service.cpp revision 7ecd69f2cc3c31cf569840e9c2d53ed352328d18
1/*
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
17#include <map>
18#include <sstream>
19#include <string>
20#include <vector>
21
22#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
31#include "android/aidl/tests/BnTestService.h"
32#include "android/aidl/tests/ITestService.h"
33
34#include "android/aidl/tests/BnNamedCallback.h"
35#include "android/aidl/tests/INamedCallback.h"
36
37// Used implicitly.
38#undef LOG_TAG
39#define LOG_TAG "aidl_native_service"
40
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:
58using android::aidl::tests::BnTestService;
59using android::aidl::tests::BnNamedCallback;
60using android::aidl::tests::INamedCallback;
61
62// Standard library
63using std::map;
64using std::vector;
65
66namespace android {
67namespace generated {
68namespace {
69
70class BinderCallback : public LooperCallback {
71 public:
72  BinderCallback() {}
73  ~BinderCallback() override {}
74
75  int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override {
76    IPCThreadState::self()->handlePolledCommands();
77    return 1;  // Continue receiving callbacks.
78  }
79};
80
81class 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
94class NativeService : public BnTestService {
95 public:
96  NativeService() {}
97  ~NativeService() override {}
98
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
126  void LogRepeatedStringToken(const String16& token) {
127    ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
128          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  }
173  status_t RepeatString(
174      const String16& token, String16* _aidl_return) override {
175    LogRepeatedStringToken(token);
176    *_aidl_return = token;
177    return OK;
178  }
179
180  template<typename T>
181  status_t ReverseArray(const vector<T>& input,
182                        vector<T>* repeated,
183                        vector<T>* _aidl_return) {
184    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  }
231
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
255  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
261  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
267 private:
268  map<String16, sp<INamedCallback>> service_map_;
269};
270
271}  // namespace
272}  // namespace generated
273}  // namespace android
274
275int main(int /* argc */, char* /* argv */ []) {
276  android::generated::NativeService service;
277  return service.Run();
278}
279