On the Internet today, I saw a small example of using C ++ to implement the binder communication, but I didn't provide a complete demo. I made it and sorted it out. I can run it on a real machine. The basic architecture is the same, and the code will not be analyzed. To analyze the Binder Mechanism, you can use it as a template. You need to add or modify functions, that is to say, it is easy to add virtual functions in itest, which are implemented on the BP, bn, and class test terminals, and add case branches in ontransact. The Code is as follows:
Binder_test.h:
#ifndef __BINDER_TEST_H#define __BINDER_TEST_H#include <binder/Parcel.h>#include <binder/IPCThreadState.h>#include <binder/ProcessState.h>#include <binder/IServiceManager.h>#include <binder/IBinder.h>#include <binder/IInterface.h>#include <unistd.h>#include <stdio.h>#include <utils/misc.h>#include <utils/Atomic.h>#include <utils/Log.h>namespace android{ class ITest : public IInterface { public: DECLARE_META_INTERFACE(Test); virtual void getTest() = 0; }; class BnTest: public BnInterface<ITest> { public: virtual status_t onTransact( uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0); }; class Test : public BnTest { public: //Test(){ALOGE("got the service");}; //~Test(); virtual void getTest(){ALOGE("got the service");}; void print(){ALOGE("got the service");}; }; enum { PRINT = IBinder::FIRST_CALL_TRANSACTION, }; class BpTest : public BpInterface<ITest> { public: BpTest(const sp<IBinder>& impl ):BpInterface<ITest>(impl) { } virtual void getTest() { printf("in the get Test\n"); Parcel data, reply; data.writeInterfaceToken(ITest::getInterfaceDescriptor()); remote()->transact(PRINT, data, &reply); printf("send Print %d\n",reply.readInt32()); } }; IMPLEMENT_META_INTERFACE(Test,"android.TestServer.ITest"); status_t BnTest::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { switch(code) { case PRINT: { printf("got the client msg\n"); CHECK_INTERFACE(ITest, data, reply); getTest(); reply->writeInt32(100); return NO_ERROR; }break; default:break; } return NO_ERROR; }}// namespace android#endif
Binder_test_client.cpp:
#include "binder_test.h" using namespace android;int main(int argc, char *argv[]){ sp<IBinder> binder; sp<ProcessState> proc(ProcessState::self()); //get service manager sp<IServiceManager> sm = defaultServiceManager(); do{ binder = sm->getService(String16("service.testmanager")); if(binder != 0) break; sleep(1); }while(true); const sp<ITest>& bts = interface_cast<ITest>(binder); ALOGE("bindertest client is starting....."); bts->getTest(); return 0;}
Binder_test_service.cpp:
#include "binder_test.h"using namespace android;int main(int argc, char *argv[]){ sp<ProcessState> proc(ProcessState::self()); //get service manager sp<IServiceManager> sm = defaultServiceManager(); sm->addService(String16("service.testmanager"),new Test());ALOGE("bindertest service is starting....."); ProcessState::self()->startThreadPool(); IPCThreadState::self()->joinThreadPool(); return 0;}
Android. mk:
LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_SRC_FILES:= \binder_test_service.cpp LOCAL_SHARED_LIBRARIES := \libutils \libbinder# FIXME The duplicate audioflinger is temporaryLOCAL_C_INCLUDES := binder_test.hLOCAL_MODULE:= bindertestserviceinclude $(BUILD_EXECUTABLE)include $(CLEAR_VARS)LOCAL_SRC_FILES:= \binder_test_client.cpp LOCAL_SHARED_LIBRARIES := \libutils \libbinder# FIXME The duplicate audioflinger is temporaryLOCAL_C_INCLUDES := binder_test.hLOCAL_MODULE:= bindertestclientinclude $(BUILD_EXECUTABLE)