| 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 | |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 17 | #include <map> |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 18 | #include <sstream> |
| 19 | #include <string> |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 20 | #include <vector> |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 21 | |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 24 | #include <binder/IInterface.h> |
| 25 | #include <binder/IPCThreadState.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/ProcessState.h> |
| Christopher Wiley | 7621d4d | 2015-11-28 16:58:06 -0800 | [diff] [blame] | 28 | #include <binder/Status.h> |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 29 | #include <nativehelper/ScopedFd.h> |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 30 | #include <utils/Errors.h> |
| 31 | #include <utils/Log.h> |
| 32 | #include <utils/Looper.h> |
| 33 | #include <utils/StrongPointer.h> |
| 34 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 35 | #include "android/aidl/tests/BnTestService.h" |
| 36 | #include "android/aidl/tests/ITestService.h" |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 37 | |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 38 | #include "android/aidl/tests/BnNamedCallback.h" |
| 39 | #include "android/aidl/tests/INamedCallback.h" |
| 40 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 41 | // Used implicitly. |
| 42 | #undef LOG_TAG |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 43 | #define LOG_TAG "aidl_native_service" |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 44 | |
| 45 | // libutils: |
| 46 | using android::Looper; |
| 47 | using android::LooperCallback; |
| 48 | using android::OK; |
| 49 | using android::sp; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 50 | using android::String16; |
| 51 | |
| 52 | // libbinder: |
| 53 | using android::BnInterface; |
| 54 | using android::defaultServiceManager; |
| 55 | using android::IInterface; |
| 56 | using android::IPCThreadState; |
| 57 | using android::Parcel; |
| 58 | using android::ProcessState; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 59 | using android::binder::Status; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 60 | |
| 61 | // Generated code: |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 62 | using android::aidl::tests::BnNamedCallback; |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 63 | using android::aidl::tests::BnTestService; |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 64 | using android::aidl::tests::INamedCallback; |
| Christopher Wiley | 95d44b0 | 2015-11-19 07:11:30 -0800 | [diff] [blame] | 65 | using android::aidl::tests::SimpleParcelable; |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 66 | using android::os::PersistableBundle; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 67 | |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 68 | // Standard library |
| 69 | using std::map; |
| Christopher Wiley | dca2ecf | 2016-01-29 10:55:43 -0800 | [diff] [blame] | 70 | using std::string; |
| Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 71 | using std::unique_ptr; |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 72 | using std::vector; |
| 73 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 74 | namespace { |
| 75 | |
| 76 | class BinderCallback : public LooperCallback { |
| 77 | public: |
| 78 | BinderCallback() {} |
| 79 | ~BinderCallback() override {} |
| 80 | |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 81 | int handleEvent(int /* fd */, int /* events */, void* /* data */) override { |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 82 | IPCThreadState::self()->handlePolledCommands(); |
| 83 | return 1; // Continue receiving callbacks. |
| 84 | } |
| 85 | }; |
| 86 | |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 87 | class NamedCallback : public BnNamedCallback { |
| 88 | public: |
| 89 | NamedCallback(String16 name) : name_(name) {} |
| 90 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 91 | Status GetName(String16* ret) { |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 92 | *ret = name_; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 93 | return Status::ok(); |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private: |
| 97 | String16 name_; |
| 98 | }; |
| 99 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 100 | class NativeService : public BnTestService { |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 101 | public: |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 102 | NativeService() {} |
| Christopher Wiley | 7621d4d | 2015-11-28 16:58:06 -0800 | [diff] [blame] | 103 | virtual ~NativeService() = default; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 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 | |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 110 | template <typename T> |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 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 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 117 | Status RepeatBoolean(bool token, bool* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 118 | LogRepeatedToken(token ? 1 : 0); |
| 119 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 120 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 121 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 122 | Status RepeatByte(int8_t token, int8_t* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 123 | LogRepeatedToken(token); |
| 124 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 125 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 126 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 127 | Status RepeatChar(char16_t token, char16_t* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 128 | LogRepeatedStringToken(String16(&token, 1)); |
| 129 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 130 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 131 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 132 | Status RepeatInt(int32_t token, int32_t* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 133 | LogRepeatedToken(token); |
| 134 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 135 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 136 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 137 | Status RepeatLong(int64_t token, int64_t* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 138 | LogRepeatedToken(token); |
| 139 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 140 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 141 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 142 | Status RepeatFloat(float token, float* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 143 | LogRepeatedToken(token); |
| 144 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 145 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 146 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 147 | Status RepeatDouble(double token, double* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 148 | LogRepeatedToken(token); |
| 149 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 150 | return Status::ok(); |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 151 | } |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 152 | Status RepeatString(const String16& token, String16* _aidl_return) override { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 153 | LogRepeatedStringToken(token); |
| 154 | *_aidl_return = token; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 155 | return Status::ok(); |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 156 | } |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 157 | |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 158 | Status RepeatSimpleParcelable(const SimpleParcelable& input, |
| 159 | SimpleParcelable* repeat, |
| 160 | SimpleParcelable* _aidl_return) override { |
| Christopher Wiley | 95d44b0 | 2015-11-19 07:11:30 -0800 | [diff] [blame] | 161 | ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str()); |
| 162 | *repeat = input; |
| 163 | *_aidl_return = input; |
| 164 | return Status::ok(); |
| 165 | } |
| 166 | |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 167 | Status RepeatPersistableBundle(const PersistableBundle& input, |
| 168 | PersistableBundle* _aidl_return) override { |
| 169 | ALOGI("Repeated a PersistableBundle"); |
| 170 | *_aidl_return = input; |
| 171 | return Status::ok(); |
| 172 | } |
| 173 | |
| 174 | template <typename T> |
| 175 | Status ReverseArray(const vector<T>& input, vector<T>* repeated, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 176 | vector<T>* _aidl_return) { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 177 | ALOGI("Reversing array of length %zu", input.size()); |
| 178 | *repeated = input; |
| 179 | *_aidl_return = input; |
| 180 | std::reverse(_aidl_return->begin(), _aidl_return->end()); |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 181 | return Status::ok(); |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 184 | template<typename T> |
| 185 | Status RepeatNullable(const unique_ptr<T>& input, |
| 186 | unique_ptr<T>* _aidl_return) { |
| 187 | ALOGI("Repeating nullable value"); |
| 188 | |
| 189 | _aidl_return->reset(); |
| 190 | if (input) { |
| 191 | _aidl_return->reset(new T(*input)); |
| 192 | } |
| 193 | |
| 194 | return Status::ok(); |
| 195 | } |
| 196 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 197 | Status ReverseBoolean(const vector<bool>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 198 | vector<bool>* repeated, |
| 199 | vector<bool>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 200 | return ReverseArray(input, repeated, _aidl_return); |
| 201 | } |
| Casey Dahlin | e9e73f1 | 2016-02-09 11:10:17 -0800 | [diff] [blame] | 202 | Status ReverseByte(const vector<uint8_t>& input, |
| 203 | vector<uint8_t>* repeated, |
| 204 | vector<uint8_t>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 205 | return ReverseArray(input, repeated, _aidl_return); |
| 206 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 207 | Status ReverseChar(const vector<char16_t>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 208 | vector<char16_t>* repeated, |
| 209 | vector<char16_t>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 210 | return ReverseArray(input, repeated, _aidl_return); |
| 211 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 212 | Status ReverseInt(const vector<int32_t>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 213 | vector<int32_t>* repeated, |
| 214 | vector<int32_t>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 215 | return ReverseArray(input, repeated, _aidl_return); |
| 216 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 217 | Status ReverseLong(const vector<int64_t>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 218 | vector<int64_t>* repeated, |
| 219 | vector<int64_t>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 220 | return ReverseArray(input, repeated, _aidl_return); |
| 221 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 222 | Status ReverseFloat(const vector<float>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 223 | vector<float>* repeated, |
| 224 | vector<float>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 225 | return ReverseArray(input, repeated, _aidl_return); |
| 226 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 227 | Status ReverseDouble(const vector<double>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 228 | vector<double>* repeated, |
| 229 | vector<double>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 230 | return ReverseArray(input, repeated, _aidl_return); |
| 231 | } |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 232 | Status ReverseString(const vector<String16>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 233 | vector<String16>* repeated, |
| 234 | vector<String16>* _aidl_return) override { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 235 | return ReverseArray(input, repeated, _aidl_return); |
| 236 | } |
| Samuel Tan | 88c3200 | 2015-12-03 16:30:43 -0800 | [diff] [blame] | 237 | Status ReverseSimpleParcelables( |
| 238 | const vector<SimpleParcelable>& input, |
| 239 | vector<SimpleParcelable>* repeated, |
| 240 | vector<SimpleParcelable>* _aidl_return) override { |
| 241 | return ReverseArray(input, repeated, _aidl_return); |
| 242 | } |
| 243 | Status ReversePersistableBundles( |
| 244 | const vector<PersistableBundle>& input, |
| 245 | vector<PersistableBundle>* repeated, |
| 246 | vector<PersistableBundle>* _aidl_return) override { |
| Christopher Wiley | 95d44b0 | 2015-11-19 07:11:30 -0800 | [diff] [blame] | 247 | return ReverseArray(input, repeated, _aidl_return); |
| 248 | } |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 249 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 250 | Status GetOtherTestService(const String16& name, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 251 | sp<INamedCallback>* returned_service) override { |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 252 | if (service_map_.find(name) == service_map_.end()) { |
| 253 | sp<INamedCallback> new_item(new NamedCallback(name)); |
| 254 | service_map_[name] = new_item; |
| 255 | } |
| 256 | |
| 257 | *returned_service = service_map_[name]; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 258 | return Status::ok(); |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 261 | Status VerifyName(const sp<INamedCallback>& service, const String16& name, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 262 | bool* returned_value) override { |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 263 | String16 foundName; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 264 | Status status = service->GetName(&foundName); |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 265 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 266 | if (status.isOk()) { |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 267 | *returned_value = foundName == name; |
| 268 | } |
| 269 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 270 | return status; |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 273 | Status ReverseStringList(const vector<String16>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 274 | vector<String16>* repeated, |
| 275 | vector<String16>* _aidl_return) override { |
| Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 276 | return ReverseArray(input, repeated, _aidl_return); |
| 277 | } |
| 278 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 279 | Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input, |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 280 | vector<sp<IBinder>>* repeated, |
| 281 | vector<sp<IBinder>>* _aidl_return) override { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 282 | return ReverseArray(input, repeated, _aidl_return); |
| 283 | } |
| 284 | |
| Casey Dahlin | a4ba4b6 | 2015-11-02 15:43:30 -0800 | [diff] [blame] | 285 | Status RepeatFileDescriptor(const ScopedFd& read, |
| 286 | ScopedFd* _aidl_return) override { |
| 287 | ALOGE("Repeating file descriptor"); |
| 288 | *_aidl_return = ScopedFd(dup(read.get())); |
| 289 | return Status::ok(); |
| 290 | } |
| 291 | |
| 292 | Status ReverseFileDescriptorArray(const vector<ScopedFd>& input, |
| 293 | vector<ScopedFd>* repeated, |
| 294 | vector<ScopedFd>* _aidl_return) override { |
| 295 | ALOGI("Reversing descriptor array of length %zu", input.size()); |
| 296 | for (const auto& item : input) { |
| 297 | repeated->push_back(ScopedFd(dup(item.get()))); |
| 298 | _aidl_return->push_back(ScopedFd(dup(item.get()))); |
| 299 | } |
| 300 | std::reverse(_aidl_return->begin(), _aidl_return->end()); |
| 301 | return Status::ok(); |
| 302 | } |
| 303 | |
| Christopher Wiley | 7621d4d | 2015-11-28 16:58:06 -0800 | [diff] [blame] | 304 | Status ThrowServiceException(int code) override { |
| 305 | return Status::fromServiceSpecificError(code); |
| 306 | } |
| 307 | |
| Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 308 | Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input, |
| 309 | unique_ptr<vector<int32_t>>* _aidl_return) { |
| 310 | return RepeatNullable(input, _aidl_return); |
| 311 | } |
| 312 | |
| 313 | Status RepeatNullableStringList( |
| 314 | const unique_ptr<vector<unique_ptr<String16>>>& input, |
| 315 | unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) { |
| 316 | ALOGI("Repeating nullable string list"); |
| 317 | if (!input) { |
| 318 | _aidl_return->reset(); |
| 319 | return Status::ok(); |
| 320 | } |
| 321 | |
| 322 | _aidl_return->reset(new vector<unique_ptr<String16>>); |
| 323 | |
| 324 | for (const auto& item : *input) { |
| 325 | if (!item) { |
| 326 | (*_aidl_return)->emplace_back(nullptr); |
| 327 | } else { |
| 328 | (*_aidl_return)->emplace_back(new String16(*item)); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return Status::ok(); |
| 333 | } |
| 334 | |
| 335 | Status RepeatNullableString(const unique_ptr<String16>& input, |
| 336 | unique_ptr<String16>* _aidl_return) { |
| 337 | return RepeatNullable(input, _aidl_return); |
| 338 | } |
| 339 | |
| 340 | Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input, |
| 341 | unique_ptr<SimpleParcelable>* _aidl_return) { |
| 342 | return RepeatNullable(input, _aidl_return); |
| 343 | } |
| 344 | |
| Christopher Wiley | dca2ecf | 2016-01-29 10:55:43 -0800 | [diff] [blame] | 345 | Status RepeatUtf8CppString(const string& token, |
| 346 | string* _aidl_return) override { |
| 347 | ALOGI("Repeating utf8 string '%s' of length=%zu", token.c_str(), token.size()); |
| 348 | *_aidl_return = token; |
| 349 | return Status::ok(); |
| 350 | } |
| 351 | |
| 352 | Status RepeatNullableUtf8CppString( |
| 353 | const unique_ptr<string>& token, |
| 354 | unique_ptr<string>* _aidl_return) override { |
| 355 | if (!token) { |
| 356 | ALOGI("Received null @utf8InCpp string"); |
| 357 | return Status::ok(); |
| 358 | } |
| 359 | ALOGI("Repeating utf8 string '%s' of length=%zu", |
| 360 | token->c_str(), token->size()); |
| 361 | _aidl_return->reset(new string(*token)); |
| 362 | return Status::ok(); |
| 363 | } |
| 364 | |
| 365 | Status ReverseUtf8CppString(const vector<string>& input, |
| 366 | vector<string>* repeated, |
| 367 | vector<string>* _aidl_return) { |
| 368 | return ReverseArray(input, repeated, _aidl_return); |
| 369 | } |
| 370 | |
| Christopher Wiley | 99d699b | 2016-02-01 13:19:43 -0800 | [diff] [blame] | 371 | Status ReverseUtf8CppStringList( |
| 372 | const unique_ptr<vector<unique_ptr<::string>>>& input, |
| 373 | unique_ptr<vector<unique_ptr<string>>>* repeated, |
| 374 | unique_ptr<vector<unique_ptr<string>>>* _aidl_return) { |
| 375 | if (!input) { |
| 376 | ALOGI("Received null list of utf8 strings"); |
| 377 | return Status::ok(); |
| 378 | } |
| 379 | _aidl_return->reset(new vector<unique_ptr<string>>); |
| 380 | repeated->reset(new vector<unique_ptr<string>>); |
| 381 | |
| 382 | for (const auto& item : *input) { |
| 383 | (*repeated)->emplace_back(nullptr); |
| 384 | (*_aidl_return)->emplace_back(nullptr); |
| 385 | if (item) { |
| 386 | (*repeated)->back().reset(new string(*item)); |
| 387 | (*_aidl_return)->back().reset(new string(*item)); |
| 388 | } |
| 389 | } |
| 390 | std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end()); |
| 391 | |
| 392 | return Status::ok(); |
| 393 | } |
| 394 | |
| Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 395 | private: |
| 396 | map<String16, sp<INamedCallback>> service_map_; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 397 | }; |
| 398 | |
| Casey Dahlin | fc46546 | 2016-01-07 16:13:57 -0800 | [diff] [blame] | 399 | int Run() { |
| Christopher Wiley | dca2ecf | 2016-01-29 10:55:43 -0800 | [diff] [blame] | 400 | android::sp<NativeService> service = new NativeService; |
| Casey Dahlin | fc46546 | 2016-01-07 16:13:57 -0800 | [diff] [blame] | 401 | sp<Looper> looper(Looper::prepare(0 /* opts */)); |
| 402 | |
| 403 | int binder_fd = -1; |
| 404 | ProcessState::self()->setThreadPoolMaxThreadCount(0); |
| 405 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 406 | IPCThreadState::self()->setupPolling(&binder_fd); |
| 407 | ALOGI("Got binder FD %d", binder_fd); |
| 408 | if (binder_fd < 0) return -1; |
| 409 | |
| 410 | sp<BinderCallback> cb(new BinderCallback); |
| 411 | if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb, |
| 412 | nullptr) != 1) { |
| 413 | ALOGE("Failed to add binder FD to Looper"); |
| 414 | return -1; |
| 415 | } |
| 416 | |
| 417 | defaultServiceManager()->addService(service->getInterfaceDescriptor(), |
| 418 | service); |
| 419 | |
| 420 | ALOGI("Entering loop"); |
| 421 | while (true) { |
| 422 | const int result = looper->pollAll(-1 /* timeoutMillis */); |
| 423 | ALOGI("Looper returned %d", result); |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | |
| Christopher Wiley | dca2ecf | 2016-01-29 10:55:43 -0800 | [diff] [blame] | 428 | } // namespace |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 429 | |
| Brian Carlstrom | ad3b806 | 2015-10-21 08:54:48 -0700 | [diff] [blame] | 430 | int main(int /* argc */, char* /* argv */ []) { |
| Christopher Wiley | dca2ecf | 2016-01-29 10:55:43 -0800 | [diff] [blame] | 431 | return Run(); |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 432 | } |