blob: 17c47eefe7642fe0c09594c94f3a894805b2719e [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>
19
Christopher Wileyb5e698c2015-10-17 09:32:22 -070020#include <binder/IInterface.h>
21#include <binder/IPCThreadState.h>
22#include <binder/IServiceManager.h>
23#include <binder/ProcessState.h>
24#include <utils/Errors.h>
25#include <utils/Log.h>
26#include <utils/Looper.h>
27#include <utils/StrongPointer.h>
28
Christopher Wiley521bb612015-10-22 11:40:30 -070029#include "android/aidl/tests/BnTestService.h"
30#include "android/aidl/tests/ITestService.h"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070031
32// Used implicitly.
33#undef LOG_TAG
Christopher Wiley33ad81e2015-10-21 14:41:11 -070034#define LOG_TAG "aidl_native_service"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070035
36// libutils:
37using android::Looper;
38using android::LooperCallback;
39using android::OK;
40using android::sp;
41using android::status_t;
42using android::String16;
43
44// libbinder:
45using android::BnInterface;
46using android::defaultServiceManager;
47using android::IInterface;
48using android::IPCThreadState;
49using android::Parcel;
50using android::ProcessState;
51
52// Generated code:
Christopher Wiley521bb612015-10-22 11:40:30 -070053using android::aidl::tests::BnTestService;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070054
55namespace android {
56namespace generated {
57namespace {
58
59class BinderCallback : public LooperCallback {
60 public:
61 BinderCallback() {}
62 ~BinderCallback() override {}
63
Brian Carlstromad3b8062015-10-21 08:54:48 -070064 int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070065 IPCThreadState::self()->handlePolledCommands();
66 return 1; // Continue receiving callbacks.
67 }
68};
69
Christopher Wiley521bb612015-10-22 11:40:30 -070070class NativeService : public BnTestService {
Christopher Wileyb5e698c2015-10-17 09:32:22 -070071 public:
Christopher Wiley33ad81e2015-10-21 14:41:11 -070072 NativeService() {}
73 ~NativeService() override {}
Christopher Wileyb5e698c2015-10-17 09:32:22 -070074
75 int Run() {
76 sp<Looper> looper(Looper::prepare(0 /* opts */));
77
78 int binder_fd = -1;
79 ProcessState::self()->setThreadPoolMaxThreadCount(0);
80 IPCThreadState::self()->disableBackgroundScheduling(true);
81 IPCThreadState::self()->setupPolling(&binder_fd);
82 ALOGI("Got binder FD %d", binder_fd);
83 if (binder_fd < 0) return -1;
84
85 sp<BinderCallback> cb(new BinderCallback);
86 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
87 nullptr) != 1) {
88 ALOGE("Failed to add binder FD to Looper");
89 return -1;
90 }
91
92 defaultServiceManager()->addService(getInterfaceDescriptor(), this);
93
94 ALOGI("Entering loop");
95 while (true) {
96 const int result = looper->pollAll(-1 /* timeoutMillis */);
97 ALOGI("Looper returned %d", result);
98 }
99 return 0;
100 }
101
Christopher Wileyd6130f22015-10-26 10:24:35 -0700102 void LogRepeatedStringToken(const String16& token) {
103 ALOGI("Repeating '%s' of length=%u", android::String8(token).string(),
104 token.size());
105 }
106
107 template<typename T>
108 void LogRepeatedToken(const T& token) {
109 std::ostringstream token_str;
110 token_str << token;
111 ALOGI("Repeating token %s", token_str.str().c_str());
112 }
113
114 status_t RepeatBoolean(bool token, bool* _aidl_return) override {
115 LogRepeatedToken(token ? 1 : 0);
116 *_aidl_return = token;
117 return OK;
118 }
119 status_t RepeatByte(int8_t token, int8_t* _aidl_return) override {
120 LogRepeatedToken(token);
121 *_aidl_return = token;
122 return OK;
123 }
124 status_t RepeatChar(char16_t token, char16_t* _aidl_return) override {
125 LogRepeatedStringToken(String16(&token, 1));
126 *_aidl_return = token;
127 return OK;
128 }
129 status_t RepeatInt(int32_t token, int32_t* _aidl_return) override {
130 LogRepeatedToken(token);
131 *_aidl_return = token;
132 return OK;
133 }
134 status_t RepeatLong(int64_t token, int64_t* _aidl_return) override {
135 LogRepeatedToken(token);
136 *_aidl_return = token;
137 return OK;
138 }
139 status_t RepeatFloat(float token, float* _aidl_return) override {
140 LogRepeatedToken(token);
141 *_aidl_return = token;
142 return OK;
143 }
144 status_t RepeatDouble(double token, double* _aidl_return) override {
145 LogRepeatedToken(token);
146 *_aidl_return = token;
147 return OK;
148 }
149 status_t RepeatString(String16 token, String16* _aidl_return) override {
150 LogRepeatedStringToken(token);
151 *_aidl_return = token;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700152 return OK;
153 }
154};
155
156} // namespace
157} // namespace generated
158} // namespace android
159
Brian Carlstromad3b8062015-10-21 08:54:48 -0700160int main(int /* argc */, char* /* argv */ []) {
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700161 android::generated::NativeService service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700162 return service.Run();
163}