├── README.md ├── cmds ├── Android.mk └── main_client.cpp ├── include ├── HelloWorld.h ├── IHelloWorld.h ├── IHelloWorldClient.h └── IHelloWorldService.h ├── myserver ├── Android.mk └── main_myserver.cpp ├── services └── helloWorld │ ├── Android.mk │ ├── HelloWorldService.cpp │ └── HelloWorldService.h └── src ├── HelloWorld.cpp ├── IHelloWorld.cpp ├── IHelloWorldClient.cpp └── IHelloWorldService.cpp /README.md: -------------------------------------------------------------------------------- 1 | Android-native-service 2 | ====================== 3 | 4 | It is a native system server which is summed up by the media server and the camera service. Multi clients can asynchronously connect to a native service by Bp and Bn. It includes the client test code and server code. The source code also include binder linkToDeath and callback, which is a thoughtfult framework. 5 | 6 | All the people can directly use the native framework as a new server in android platform, and add other service to the server. 7 | 8 | please copy the project to the folder /frameworks in the android source code, and compile. client file [myclient] and server file [myserver] would be generated. 9 | 10 | Please copy myclient & my server to out\target\product\***\root\system\bin\ and test the server in terminal, run the myserver before the myclient. In release version, please add the code [service myserver /system/bin/myserver...] in init.{hardware}.rc. 11 | -------------------------------------------------------------------------------- /cmds/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_C_INCLUDES := \ 5 | $(LOCAL_PATH)/../include 6 | 7 | LOCAL_SRC_FILES:= \ 8 | main_client.cpp 9 | 10 | LOCAL_SHARED_LIBRARIES := \ 11 | libutils \ 12 | libcutils \ 13 | libbinder \ 14 | liblog \ 15 | libhelloworld 16 | 17 | LOCAL_MODULE_TAGS := optional 18 | 19 | LOCAL_MODULE:= myclient 20 | 21 | include $(BUILD_EXECUTABLE) 22 | -------------------------------------------------------------------------------- /cmds/main_client.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "HelloWorld.h" 4 | 5 | using namespace android; 6 | 7 | int main(int argc, char** argv) 8 | { 9 | sp c = HelloWorld::connect(); 10 | c->setHelloWorld(); 11 | 12 | ProcessState::self()->startThreadPool(); 13 | printf("hello world client is starting now.\n"); 14 | 15 | sleep(2); 16 | 17 | c->disconnect(); 18 | 19 | IPCThreadState::self()->joinThreadPool(); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /include/HelloWorld.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROID_HELLOWORLD_H 2 | #define ANDROID_HELLOWORLD_H 3 | 4 | #include "IHelloWorld.h" 5 | #include "IHelloWorldClient.h" 6 | #include "IHelloWorldService.h" 7 | 8 | namespace android { 9 | 10 | class HelloWorld : public BnHelloWorldClient, public IBinder::DeathRecipient 11 | { 12 | public: 13 | static sp connect(); 14 | 15 | status_t setHelloWorld(); 16 | void disconnect(); 17 | 18 | virtual ~HelloWorld(); 19 | 20 | virtual void notifyCallback(int32_t msgType, int32_t ext, int32_t ext2); 21 | 22 | virtual status_t onTransact( uint32_t code, 23 | const Parcel & data, 24 | Parcel * reply, 25 | uint32_t flags); 26 | 27 | private: 28 | HelloWorld(); 29 | static const sp& getHelloWorldService(); //get helloWorld service handler 30 | 31 | virtual void binderDied(const wp& who); 32 | 33 | class DeathNotifier: public IBinder::DeathRecipient 34 | { 35 | public: 36 | DeathNotifier() { 37 | } 38 | 39 | virtual void binderDied(const wp& who); 40 | }; 41 | 42 | static sp mDeathNotifier; 43 | 44 | sp mHelloWorld; 45 | static sp mHelloWorldService; 46 | static Mutex mLock; 47 | 48 | }; 49 | 50 | }; 51 | 52 | #endif 53 | 54 | -------------------------------------------------------------------------------- /include/IHelloWorld.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROID_IHELLOWORLD_H 2 | #define ANDROID_IHELLOWORLD_H 3 | 4 | #include 5 | 6 | namespace android { 7 | 8 | class IHelloWorld : public IInterface 9 | { 10 | public: 11 | DECLARE_META_INTERFACE(HelloWorld); 12 | 13 | virtual status_t helloWorld(const char *str) = 0; 14 | virtual void disconnect() = 0; 15 | }; 16 | 17 | class BnHelloWorld : public BnInterface 18 | { 19 | public: 20 | virtual status_t onTransact( uint32_t code, 21 | const Parcel & data, 22 | Parcel * reply, 23 | uint32_t flags); 24 | }; 25 | 26 | }; 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /include/IHelloWorldClient.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROID_IHELLOWORLDCLIENT_H 2 | #define ANDROID_IHELLOWORLDCLIENT_H 3 | 4 | #include 5 | 6 | namespace android { 7 | 8 | class IHelloWorldClient : public IInterface 9 | { 10 | public: 11 | DECLARE_META_INTERFACE(HelloWorldClient); 12 | 13 | virtual void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) = 0; 14 | }; 15 | 16 | class BnHelloWorldClient : public BnInterface 17 | { 18 | public: 19 | virtual status_t onTransact( uint32_t code, 20 | const Parcel & data, 21 | Parcel * reply, 22 | uint32_t flags); 23 | }; 24 | 25 | }; 26 | 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /include/IHelloWorldService.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROID_IHELLOWORLDSERVICE_H 2 | #define ANDROID_IHELLOWORLDSERVICE_H 3 | 4 | #include 5 | 6 | namespace android { 7 | 8 | class IHelloWorldService : public IInterface 9 | { 10 | public: 11 | DECLARE_META_INTERFACE(HelloWorldService); 12 | 13 | virtual sp connect(const sp& helloWorldClient) = 0; 14 | }; 15 | 16 | class BnHelloWorldService : public BnInterface 17 | { 18 | public: 19 | virtual status_t onTransact( uint32_t code, 20 | const Parcel & data, 21 | Parcel * reply, 22 | uint32_t flags); 23 | }; 24 | 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /myserver/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_C_INCLUDES := \ 5 | $(LOCAL_PATH)/../include \ 6 | $(LOCAL_PATH)/../services/helloWorld 7 | 8 | LOCAL_SRC_FILES:= \ 9 | main_myserver.cpp 10 | 11 | LOCAL_SHARED_LIBRARIES := \ 12 | libutils \ 13 | libcutils \ 14 | libbinder \ 15 | liblog \ 16 | libhelloworld 17 | 18 | LOCAL_MODULE_TAGS := optional 19 | 20 | LOCAL_MODULE:= myserver 21 | 22 | include $(BUILD_EXECUTABLE) 23 | -------------------------------------------------------------------------------- /myserver/main_myserver.cpp: -------------------------------------------------------------------------------- 1 | #define LOG_TAG "myserver" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "HelloWorldService.h" 9 | 10 | using namespace android; 11 | 12 | int main(int argc, char** argv) 13 | { 14 | HelloWorldService::instantiate(); 15 | ProcessState::self()->startThreadPool(); 16 | printf("my server is starting now.\n"); 17 | IPCThreadState::self()->joinThreadPool(); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /services/helloWorld/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | 3 | include $(CLEAR_VARS) 4 | 5 | LOCAL_C_INCLUDES := \ 6 | $(LOCAL_PATH)/../../include 7 | 8 | LOCAL_SRC_FILES:= \ 9 | ../../src/IHelloWorldService.cpp \ 10 | ../../src/IHelloWorldClient.cpp \ 11 | ../../src/IHelloWorld.cpp \ 12 | ../../src/HelloWorld.cpp \ 13 | HelloWorldService.cpp 14 | 15 | LOCAL_SHARED_LIBRARIES := \ 16 | libutils \ 17 | libcutils \ 18 | libbinder \ 19 | liblog 20 | 21 | LOCAL_MODULE_TAGS := optional 22 | 23 | LOCAL_MODULE:= libhelloworld 24 | 25 | include $(BUILD_SHARED_LIBRARY) 26 | -------------------------------------------------------------------------------- /services/helloWorld/HelloWorldService.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "HelloWorldService.h" 4 | #include "IHelloWorldClient.h" 5 | 6 | namespace android { 7 | 8 | enum { 9 | HW_CALLBACK_NOTICE = 0x1000, 10 | }; 11 | 12 | Mutex HelloWorldService::mServiceLock; 13 | int HelloWorldService::mClientId; 14 | 15 | sp HelloWorldService::connect(const sp& helloWorldClient) 16 | { 17 | sp client; 18 | 19 | Mutex::Autolock lock(mServiceLock); 20 | 21 | if (mClient[mClientId] != 0) 22 | { 23 | client = mClient[mClientId].promote(); 24 | if (client != 0) 25 | { 26 | if (helloWorldClient->asBinder() == client->getHelloWorldClient()->asBinder()) 27 | { 28 | printf("helloWorldClient is existed\n"); 29 | return client; 30 | } 31 | else 32 | { 33 | return NULL; 34 | } 35 | } 36 | mClient[mClientId].clear(); 37 | } 38 | 39 | client = new Client(this, helloWorldClient); 40 | 41 | helloWorldClient->asBinder()->linkToDeath(this); 42 | 43 | mClient[mClientId] = client; 44 | mClientId++; 45 | 46 | return client; 47 | } 48 | 49 | void HelloWorldService::removeClient(const sp& helloWorldClient) 50 | { 51 | Mutex::Autolock lock(mServiceLock); 52 | 53 | int outIndex = 0; 54 | sp client = findClientUnsafe(helloWorldClient->asBinder(), outIndex); 55 | 56 | if (client != 0) 57 | { 58 | // Found our helloWorld, clear and leave. 59 | mClient[outIndex].clear(); 60 | client->unlinkToDeath(this); 61 | } 62 | } 63 | 64 | sp HelloWorldService::findClientUnsafe(const wp& helloWorldClient, int& outIndex) 65 | { 66 | sp client; 67 | 68 | for (int i = 0; i <= mClientId; i++) 69 | { 70 | 71 | // This happens when we have already disconnected (or this is 72 | // just another unused camera). 73 | if (mClient[i] == 0) continue; 74 | 75 | // Promote mClient. It can fail if we are called from this path: 76 | // Client::~Client() -> disconnect() -> removeClient(). 77 | client = mClient[i].promote(); 78 | 79 | // Clean up stale client entry 80 | if (client == NULL) { 81 | mClient[i].clear(); 82 | continue; 83 | } 84 | 85 | if (helloWorldClient == client->getHelloWorldClient()->asBinder()) 86 | { 87 | // Found our helloWorld 88 | outIndex = i; 89 | return client; 90 | } 91 | } 92 | 93 | outIndex = -1; 94 | return NULL; 95 | } 96 | 97 | HelloWorldService::HelloWorldService() 98 | : BnHelloWorldService() 99 | { 100 | printf("HelloWorldService is created\n"); 101 | } 102 | 103 | HelloWorldService::~HelloWorldService() 104 | { 105 | printf("HelloWorldService is destroyed\n"); 106 | } 107 | 108 | status_t HelloWorldService::onTransact( uint32_t code, const Parcel & data, Parcel * reply, uint32_t flags) 109 | { 110 | printf("HelloWorldService onTransact\n"); 111 | 112 | return BnHelloWorldService::onTransact(code, data, reply, flags); 113 | } 114 | 115 | HelloWorldService::Client::Client(const sp& helloWorldService, const sp& helloWorldClient) 116 | { 117 | mHelloWorldService = helloWorldService; 118 | mHelloWorldClient = helloWorldClient; 119 | } 120 | 121 | HelloWorldService::Client::~Client() 122 | { 123 | // unconditionally disconnect. function is idempotent 124 | Client::disconnect(); 125 | } 126 | 127 | void HelloWorldService::Client::disconnect() 128 | { 129 | printf("service disconnect.\n"); 130 | 131 | mHelloWorldService->removeClient(mHelloWorldClient); 132 | } 133 | 134 | status_t HelloWorldService::Client::helloWorld(const char *str) 135 | { 136 | printf("service get:%s\n", str); 137 | 138 | Mutex::Autolock lock(mServiceLock); 139 | 140 | sp c= mHelloWorldClient; 141 | 142 | if(c != NULL) 143 | { 144 | c->notifyCallback(HW_CALLBACK_NOTICE, 0, 0); 145 | } 146 | 147 | return NO_ERROR; 148 | } 149 | 150 | sp HelloWorldService::getClientByRemote(const wp& helloworldClient) 151 | { 152 | sp client; 153 | 154 | Mutex::Autolock lock(mServiceLock); 155 | 156 | int outIndex; 157 | client = findClientUnsafe(helloworldClient, outIndex); 158 | 159 | return client; 160 | 161 | } 162 | 163 | void HelloWorldService::binderDied(const wp &who) 164 | { 165 | printf("java clients' binder died\n"); 166 | 167 | sp helloWorldClient = getClientByRemote(who); 168 | 169 | if (helloWorldClient == 0) 170 | { 171 | printf("java clients' binder death already cleaned up (normal case)\n"); 172 | return; 173 | } 174 | 175 | helloWorldClient->disconnect(); 176 | } 177 | 178 | }; 179 | -------------------------------------------------------------------------------- /services/helloWorld/HelloWorldService.h: -------------------------------------------------------------------------------- 1 | #ifndef ANDROID_HELLOWORLDSERVICE_H 2 | #define ANDROID_HELLOWORLDSERVICE_H 3 | 4 | #include 5 | #include 6 | #include "IHelloWorld.h" 7 | #include "IHelloWorldClient.h" 8 | #include "IHelloWorldService.h" 9 | 10 | #define MAX_CLIENTS 5 11 | 12 | namespace android { 13 | 14 | class HelloWorldService : 15 | public BinderService, 16 | public BnHelloWorldService, 17 | public IBinder::DeathRecipient 18 | { 19 | friend class BinderService; // for HelloWorldService() 20 | 21 | public: 22 | static const char* getServiceName() { return "myserver.hello_world_service"; } 23 | 24 | void removeClient(const sp& helloWorldClient); 25 | 26 | virtual sp connect(const sp& helloWorldClient); 27 | 28 | virtual status_t onTransact( uint32_t code, 29 | const Parcel & data, 30 | Parcel * reply, 31 | uint32_t flags); 32 | 33 | class Client : public BnHelloWorld 34 | { 35 | public: 36 | void disconnect(); 37 | status_t helloWorld(const char *str); 38 | 39 | Client(const sp& helloWorldService, const sp& helloWorldClient); 40 | ~Client(); 41 | 42 | const sp& getHelloWorldClient() 43 | { 44 | return mHelloWorldClient; 45 | } 46 | 47 | protected: 48 | // these are initialized in the constructor. 49 | sp mHelloWorldService; 50 | sp mHelloWorldClient; 51 | int mHelloWorldId; 52 | 53 | }; 54 | 55 | virtual sp getClientByRemote(const wp& helloworldClient); 56 | 57 | private: 58 | HelloWorldService(); 59 | virtual ~HelloWorldService(); 60 | sp findClientUnsafe(const wp& helloWorldClient, int& outIndex); 61 | 62 | static Mutex mServiceLock; 63 | static int mClientId; 64 | wp mClient[MAX_CLIENTS]; 65 | 66 | // IBinder::DeathRecipient implementation 67 | virtual void binderDied(const wp &who); 68 | }; 69 | 70 | }; 71 | #endif 72 | 73 | -------------------------------------------------------------------------------- /src/HelloWorld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "IHelloWorld.h" 5 | #include "IHelloWorldClient.h" 6 | #include "IHelloWorldService.h" 7 | #include "HelloWorld.h" 8 | 9 | namespace android { 10 | 11 | Mutex HelloWorld::mLock; 12 | sp HelloWorld::mHelloWorldService; 13 | sp HelloWorld::mDeathNotifier; 14 | 15 | const sp& HelloWorld::getHelloWorldService() 16 | { 17 | Mutex::Autolock lock(mLock); 18 | 19 | if(mHelloWorldService.get() == 0) 20 | { 21 | sp sm = defaultServiceManager(); 22 | sp binder; 23 | 24 | do { 25 | binder = sm->getService(String16("myserver.hello_world_service")); 26 | if(binder != 0) 27 | { 28 | break; 29 | } 30 | printf("HelloWorld Service is not working, waiting...\n"); 31 | usleep(500000); 32 | } while(true); 33 | 34 | if (mDeathNotifier == NULL) 35 | { 36 | mDeathNotifier = new DeathNotifier(); 37 | } 38 | binder->linkToDeath(mDeathNotifier); 39 | 40 | mHelloWorldService = interface_cast(binder); 41 | } 42 | 43 | return mHelloWorldService; 44 | } 45 | 46 | sp HelloWorld::connect() 47 | { 48 | sp c = new HelloWorld(); 49 | const sp& hw = getHelloWorldService(); 50 | 51 | if(hw != 0) 52 | { 53 | c->mHelloWorld = hw->connect(c); 54 | } 55 | 56 | if (c->mHelloWorld != 0) 57 | { 58 | c->mHelloWorld->asBinder()->linkToDeath(c); 59 | } else 60 | { 61 | c.clear(); 62 | } 63 | 64 | return c; 65 | } 66 | 67 | status_t HelloWorld::setHelloWorld() 68 | { 69 | sp c = mHelloWorld; 70 | 71 | return c->helloWorld("hi, renwei"); 72 | } 73 | 74 | void HelloWorld::disconnect() 75 | { 76 | sp c = mHelloWorld; 77 | 78 | if (c != 0) 79 | { 80 | c->disconnect(); 81 | c->asBinder()->unlinkToDeath(this); 82 | c = 0; 83 | } 84 | } 85 | 86 | void HelloWorld::notifyCallback(int32_t msgType, int32_t ext, int32_t ext2) 87 | { 88 | printf("client receive callback!\n"); 89 | } 90 | 91 | status_t HelloWorld::onTransact( uint32_t code, const Parcel & data, Parcel * reply, uint32_t flags) 92 | { 93 | printf("HelloWorld onTransact\n"); 94 | 95 | return BnHelloWorldClient::onTransact(code, data, reply, flags); 96 | } 97 | 98 | HelloWorld::HelloWorld() 99 | { 100 | 101 | } 102 | 103 | HelloWorld::~HelloWorld() 104 | { 105 | 106 | } 107 | 108 | void HelloWorld::binderDied(const wp& who) 109 | { 110 | printf("IHelloWorld died\n"); 111 | 112 | } 113 | 114 | void HelloWorld::DeathNotifier::binderDied(const wp& who) 115 | { 116 | Mutex::Autolock lock(mLock); 117 | HelloWorld::mHelloWorldService.clear(); 118 | printf("HelloWorld service died!\n"); 119 | } 120 | 121 | }; 122 | 123 | -------------------------------------------------------------------------------- /src/IHelloWorld.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IHelloWorld.h" 3 | 4 | namespace android { 5 | 6 | enum { 7 | DISCONNECT = IBinder::FIRST_CALL_TRANSACTION, 8 | HW_HELLOWORLD, 9 | }; 10 | 11 | class BpHelloWorld : public BpInterface 12 | { 13 | public: 14 | BpHelloWorld(const sp& impl) 15 | : BpInterface(impl) 16 | { 17 | } 18 | 19 | // disconnect from helloWorld service 20 | virtual void disconnect() 21 | { 22 | printf("bp send disconnect.\n"); 23 | Parcel data, reply; 24 | data.writeInterfaceToken(IHelloWorld::getInterfaceDescriptor()); 25 | remote()->transact(DISCONNECT, data, &reply); 26 | } 27 | 28 | virtual status_t helloWorld(const char *str) 29 | { 30 | printf("bp send helloWorld.\n"); 31 | Parcel data, reply; 32 | data.writeInterfaceToken(IHelloWorld::getInterfaceDescriptor()); 33 | data.writeCString(str); 34 | remote()->transact(HW_HELLOWORLD, data, &reply); 35 | return reply.readInt32(); 36 | } 37 | 38 | }; 39 | 40 | IMPLEMENT_META_INTERFACE(HelloWorld, "android.myserver.IHelloWorld"); 41 | 42 | status_t BnHelloWorld::onTransact( uint32_t code, 43 | const Parcel & data, 44 | Parcel * reply, 45 | uint32_t flags) 46 | { 47 | switch(code) 48 | { 49 | case DISCONNECT: { 50 | printf("bn disconnect.\n"); 51 | CHECK_INTERFACE(IHelloWorld,data,reply); 52 | disconnect(); 53 | return NO_ERROR; 54 | } break; 55 | case HW_HELLOWORLD: { 56 | printf("bn get helloWorld.\n"); 57 | CHECK_INTERFACE(IHelloWorld,data,reply); 58 | const char *str; 59 | str = data.readCString(); 60 | reply->writeInt32(helloWorld(str)); 61 | return NO_ERROR; 62 | } break; 63 | default: 64 | return BBinder::onTransact(code, data, reply, flags); 65 | } 66 | } 67 | 68 | } 69 | 70 | -------------------------------------------------------------------------------- /src/IHelloWorldClient.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IHelloWorldClient.h" 3 | 4 | namespace android { 5 | 6 | enum { 7 | NOTIFY_CALLBACK = IBinder::FIRST_CALL_TRANSACTION, 8 | }; 9 | 10 | class BpHelloWorldClient : public BpInterface 11 | { 12 | public: 13 | BpHelloWorldClient(const sp& impl) 14 | : BpInterface(impl) 15 | { 16 | } 17 | 18 | // generic callback from helloWorld service to app 19 | virtual void notifyCallback(int32_t msgType, int32_t ext1, int32_t ext2) 20 | { 21 | printf("bp notifyCallback.\n"); 22 | Parcel data, reply; 23 | data.writeInterfaceToken(IHelloWorldClient::getInterfaceDescriptor()); 24 | data.writeInt32(msgType); 25 | data.writeInt32(ext1); 26 | data.writeInt32(ext2); 27 | remote()->transact(NOTIFY_CALLBACK, data, &reply, IBinder::FLAG_ONEWAY); 28 | } 29 | }; 30 | 31 | IMPLEMENT_META_INTERFACE(HelloWorldClient, "android.myserver.IHelloWorldClient"); 32 | 33 | status_t BnHelloWorldClient::onTransact( uint32_t code, 34 | const Parcel & data, 35 | Parcel * reply, 36 | uint32_t flags) 37 | { 38 | switch(code) 39 | { 40 | case NOTIFY_CALLBACK: { 41 | printf("bn notifyCallback.\n"); 42 | CHECK_INTERFACE(IHelloWorldClient,data,reply); 43 | int32_t msgType = data.readInt32(); 44 | int32_t ext1 = data.readInt32(); 45 | int32_t ext2 = data.readInt32(); 46 | notifyCallback(msgType, ext1, ext2); 47 | return NO_ERROR; 48 | } break; 49 | default: 50 | return BBinder::onTransact(code, data, reply, flags); 51 | } 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /src/IHelloWorldService.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "IHelloWorld.h" 3 | #include "IHelloWorldClient.h" 4 | #include "IHelloWorldService.h" 5 | 6 | namespace android { 7 | 8 | enum { 9 | HW_CONNECT = IBinder::FIRST_CALL_TRANSACTION, 10 | }; 11 | 12 | class BpHelloWorldService : public BpInterface 13 | { 14 | public: 15 | BpHelloWorldService(const sp& impl) 16 | : BpInterface(impl) 17 | { 18 | } 19 | 20 | virtual sp connect(const sp& helloWorldClient) 21 | { 22 | printf("bp send connect.\n"); 23 | Parcel data, reply; 24 | data.writeInterfaceToken(IHelloWorldService::getInterfaceDescriptor()); 25 | data.writeStrongBinder(helloWorldClient->asBinder()); 26 | remote()->transact(HW_CONNECT, data, &reply); 27 | return interface_cast(reply.readStrongBinder()); 28 | } 29 | }; 30 | 31 | IMPLEMENT_META_INTERFACE(HelloWorldService, "android.myserver.IHelloWorldService"); 32 | 33 | status_t BnHelloWorldService::onTransact( uint32_t code, 34 | const Parcel & data, 35 | Parcel * reply, 36 | uint32_t flags) 37 | { 38 | switch(code) 39 | { 40 | case HW_CONNECT: { 41 | printf("bn get connect.\n"); 42 | CHECK_INTERFACE(IHelloWorldService,data,reply); 43 | sp helloWorldClient = interface_cast(data.readStrongBinder()); 44 | sp helloworld = connect(helloWorldClient); 45 | reply->writeStrongBinder(helloworld->asBinder()); 46 | return NO_ERROR; 47 | } break; 48 | default: 49 | return BBinder::onTransact(code, data, reply, flags); 50 | } 51 | } 52 | 53 | }; 54 | --------------------------------------------------------------------------------