| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 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 | |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 17 | #include <sstream> |
| 18 | #include <string> |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 19 | #include <vector> |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 20 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 21 | #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 Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 30 | #include "android/aidl/tests/BnTestService.h" |
| 31 | #include "android/aidl/tests/ITestService.h" |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 32 | |
| 33 | // Used implicitly. |
| 34 | #undef LOG_TAG |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 35 | #define LOG_TAG "aidl_native_service" |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 36 | |
| 37 | // libutils: |
| 38 | using android::Looper; |
| 39 | using android::LooperCallback; |
| 40 | using android::OK; |
| 41 | using android::sp; |
| 42 | using android::status_t; |
| 43 | using android::String16; |
| 44 | |
| 45 | // libbinder: |
| 46 | using android::BnInterface; |
| 47 | using android::defaultServiceManager; |
| 48 | using android::IInterface; |
| 49 | using android::IPCThreadState; |
| 50 | using android::Parcel; |
| 51 | using android::ProcessState; |
| 52 | |
| 53 | // Generated code: |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 54 | using android::aidl::tests::BnTestService; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 55 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 56 | using std::vector; |
| 57 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 58 | namespace android { |
| 59 | namespace generated { |
| 60 | namespace { |
| 61 | |
| 62 | class BinderCallback : public LooperCallback { |
| 63 | public: |
| 64 | BinderCallback() {} |
| 65 | ~BinderCallback() override {} |
| 66 | |
| Brian Carlstrom | ad3b806 | 2015-10-21 08:54:48 -0700 | [diff] [blame] | 67 | int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override { |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 68 | IPCThreadState::self()->handlePolledCommands(); |
| 69 | return 1; // Continue receiving callbacks. |
| 70 | } |
| 71 | }; |
| 72 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 73 | class NativeService : public BnTestService { |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 74 | public: |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 75 | NativeService() {} |
| 76 | ~NativeService() override {} |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 77 | |
| 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 Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 105 | void LogRepeatedStringToken(const String16& token) { |
| Chih-Hung Hsieh | 048b55e | 2015-10-27 15:07:50 -0700 | [diff] [blame] | 106 | ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(), |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 107 | 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 Wiley | b8e49a4 | 2015-10-27 12:55:18 -0700 | [diff] [blame] | 152 | status_t RepeatString( |
| 153 | const String16& token, String16* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 154 | LogRepeatedStringToken(token); |
| 155 | *_aidl_return = token; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 156 | return OK; |
| 157 | } |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 158 | |
| 159 | template<typename T> |
| 160 | status_t ReverseArray(const vector<T>& input, |
| 161 | vector<T>* repeated, |
| Christopher Wiley | 47964b0 | 2015-10-28 14:01:25 -0700 | [diff] [blame^] | 162 | vector<T>* _aidl_return) { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 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 Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | } // namespace |
| 213 | } // namespace generated |
| 214 | } // namespace android |
| 215 | |
| Brian Carlstrom | ad3b806 | 2015-10-21 08:54:48 -0700 | [diff] [blame] | 216 | int main(int /* argc */, char* /* argv */ []) { |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 217 | android::generated::NativeService service; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 218 | return service.Run(); |
| 219 | } |