aidl_test_service.cpp revision 33ad81e306649cb769e02d10ccf77520b68c7742
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 <binder/IInterface.h> 18#include <binder/IPCThreadState.h> 19#include <binder/IServiceManager.h> 20#include <binder/ProcessState.h> 21#include <utils/Errors.h> 22#include <utils/Log.h> 23#include <utils/Looper.h> 24#include <utils/StrongPointer.h> 25 26#include "android/os/BnPingResponder.h" 27#include "android/os/IPingResponder.h" 28 29// Used implicitly. 30#undef LOG_TAG 31#define LOG_TAG "aidl_native_service" 32 33// libutils: 34using android::Looper; 35using android::LooperCallback; 36using android::OK; 37using android::sp; 38using android::status_t; 39using android::String16; 40 41// libbinder: 42using android::BnInterface; 43using android::defaultServiceManager; 44using android::IInterface; 45using android::IPCThreadState; 46using android::Parcel; 47using android::ProcessState; 48 49// Generated code: 50using android::os::BnPingResponder; 51 52namespace android { 53namespace generated { 54namespace { 55 56class BinderCallback : public LooperCallback { 57 public: 58 BinderCallback() {} 59 ~BinderCallback() override {} 60 61 int handleEvent(int /* fd */, int /* events */, void* /* data */ ) override { 62 IPCThreadState::self()->handlePolledCommands(); 63 return 1; // Continue receiving callbacks. 64 } 65}; 66 67class NativeService : public BnPingResponder { 68 public: 69 NativeService() {} 70 ~NativeService() override {} 71 72 int Run() { 73 sp<Looper> looper(Looper::prepare(0 /* opts */)); 74 75 int binder_fd = -1; 76 ProcessState::self()->setThreadPoolMaxThreadCount(0); 77 IPCThreadState::self()->disableBackgroundScheduling(true); 78 IPCThreadState::self()->setupPolling(&binder_fd); 79 ALOGI("Got binder FD %d", binder_fd); 80 if (binder_fd < 0) return -1; 81 82 sp<BinderCallback> cb(new BinderCallback); 83 if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb, 84 nullptr) != 1) { 85 ALOGE("Failed to add binder FD to Looper"); 86 return -1; 87 } 88 89 defaultServiceManager()->addService(getInterfaceDescriptor(), this); 90 91 ALOGI("Entering loop"); 92 while (true) { 93 const int result = looper->pollAll(-1 /* timeoutMillis */); 94 ALOGI("Looper returned %d", result); 95 } 96 return 0; 97 } 98 99 // BnPingResponder: 100 status_t Ping(int32_t token, int32_t* returned_token) override { 101 ALOGI("Got ping with token %d", token); 102 *returned_token = token; 103 return OK; 104 } 105}; 106 107} // namespace 108} // namespace generated 109} // namespace android 110 111int main(int /* argc */, char* /* argv */ []) { 112 android::generated::NativeService service; 113 return service.Run(); 114} 115