diff --git a/03_day.pptx b/03_day.pptx index 2def9ae..2ab9872 100644 Binary files a/03_day.pptx and b/03_day.pptx differ diff --git a/03_day/binder_3/Android.bp b/03_day/binder_3/Android.bp index a44bf50..f3030c0 100644 --- a/03_day/binder_3/Android.bp +++ b/03_day/binder_3/Android.bp @@ -5,3 +5,11 @@ cc_binary { "my_server.cpp", ], } + +cc_binary { + name: "my_client_cpp_1", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_client.cpp", + ], +} diff --git a/03_day/binder_3/my_client.cpp b/03_day/binder_3/my_client.cpp new file mode 100644 index 0000000..d948117 --- /dev/null +++ b/03_day/binder_3/my_client.cpp @@ -0,0 +1,22 @@ + +#include +#include +#include +#include +#include + +using namespace android; + +int main() +{ + sp proc(ProcessState::self()); + sp sm(defaultServiceManager()); + sp binder = sm->getService( String16("led.service") ); + Parcel data, reply; + binder->transact(1, data, &reply); + + return 0; +} + + + diff --git a/03_day/binder_3/my_server.cpp b/03_day/binder_3/my_server.cpp index 26c61af..d2481ff 100644 --- a/03_day/binder_3/my_server.cpp +++ b/03_day/binder_3/my_server.cpp @@ -7,11 +7,28 @@ using namespace android; +class LedService : public BBinder +{ + public: + status_t onTransact( + uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) + { + switch (code) { + case 1: + printf("LedService::LED_ON\n"); + return NO_ERROR; + + default: + return BBinder::onTransact(code, data, reply, flags); + } + } +}; + int main() { sp proc(ProcessState::self()); sp sm(defaultServiceManager()); - sm->addService( String16("led.service"), new BBinder ); + sm->addService( String16("led.service"), new LedService ); IPCThreadState::self()->joinThreadPool(); return 0; diff --git a/03_day/binder_4/.my_server.cpp.swp b/03_day/binder_4/.my_server.cpp.swp new file mode 100644 index 0000000..6fe7cf6 Binary files /dev/null and b/03_day/binder_4/.my_server.cpp.swp differ diff --git a/03_day/binder_4/Android.bp b/03_day/binder_4/Android.bp new file mode 100644 index 0000000..a2a327c --- /dev/null +++ b/03_day/binder_4/Android.bp @@ -0,0 +1,16 @@ +cc_binary { + name: "my_server_cpp_2", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_server.cpp", + ], +} + +cc_binary { + name: "my_client_cpp_2", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_client.cpp", + "ILedService.cpp", + ], +} diff --git a/03_day/binder_4/ILedService.cpp b/03_day/binder_4/ILedService.cpp new file mode 100644 index 0000000..6183bd5 --- /dev/null +++ b/03_day/binder_4/ILedService.cpp @@ -0,0 +1,31 @@ +#define LOG_TAG "LedService" + +#include "ILedService.h" + +#include +#include +#include + +namespace android { + +// ---------------------------------------------------------------------- + +class BpLedService : public BpInterface +{ +public: + explicit BpLedService(const sp& impl) + : BpInterface(impl) + { + } + + virtual void ledOn(void) + { + printf("BpLedService::ledOn()\n"); + Parcel data, reply; + remote()->transact(LED_ON, data, &reply); + } +}; + +IMPLEMENT_META_INTERFACE(LedService, "android.os.ILedService"); + +}; // namespace android diff --git a/03_day/binder_4/ILedService.h b/03_day/binder_4/ILedService.h new file mode 100644 index 0000000..fb6b1c1 --- /dev/null +++ b/03_day/binder_4/ILedService.h @@ -0,0 +1,26 @@ +#ifndef ANDROID_ILED_SERVICE_H +#define ANDROID_ILED_SERVICE_H + +#include +#include + +namespace android { + +// ---------------------------------------------------------------------- + +class ILedService : public IInterface +{ +public: + DECLARE_META_INTERFACE(LedService) + + virtual void ledOn() = 0; + + enum { + LED_ON = IBinder::FIRST_CALL_TRANSACTION + }; +}; + +}; // namespace android + +#endif // ANDROID_ISERVICE_MANAGER_H + diff --git a/03_day/binder_4/ProcessState.cpp b/03_day/binder_4/ProcessState.cpp new file mode 100644 index 0000000..07b6867 --- /dev/null +++ b/03_day/binder_4/ProcessState.cpp @@ -0,0 +1,54 @@ +#if 1 +#include +class ProcessState +{ + static ProcessState *gProcess; + ProcessState() + { + printf("fd=open(\"/dev/binder\", O_RDWR)\n"); + } + public: + static ProcessState *self() + { + if( gProcess == 0 ) + gProcess = new ProcessState; + return gProcess; + } + ~ProcessState() + { + printf("close(fd)\n"); + } +}; +ProcessState *ProcessState::gProcess = 0; + +int main() +{ + ProcessState *p1 = ProcessState::self(); + ProcessState *p2 = ProcessState::self(); + //ProcessState process; + return 0; +} +#endif + +#if 0 +#include +class ProcessState +{ + public: + ProcessState() + { + printf("fd=open(\"/dev/binder\", O_RDWR)\n"); + } + ~ProcessState() + { + printf("close(fd)\n"); + } +}; + +int main() +{ + ProcessState p1; + ProcessState p2; + return 0; +} +#endif diff --git a/03_day/binder_4/my_client.cpp b/03_day/binder_4/my_client.cpp new file mode 100644 index 0000000..c651d64 --- /dev/null +++ b/03_day/binder_4/my_client.cpp @@ -0,0 +1,23 @@ + +#include +#include +#include +#include +#include +#include "ILedService.h" + +using namespace android; + +int main() +{ + sp proc(ProcessState::self()); + sp sm(defaultServiceManager()); + sp binder = sm->getService( String16("led.service") ); + sp led = interface_cast( binder ); + led->ledOn(); + + return 0; +} + + + diff --git a/03_day/binder_4/my_server.cpp b/03_day/binder_4/my_server.cpp new file mode 100644 index 0000000..d2481ff --- /dev/null +++ b/03_day/binder_4/my_server.cpp @@ -0,0 +1,38 @@ + +#include +#include +#include +#include +#include + +using namespace android; + +class LedService : public BBinder +{ + public: + status_t onTransact( + uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) + { + switch (code) { + case 1: + printf("LedService::LED_ON\n"); + return NO_ERROR; + + default: + return BBinder::onTransact(code, data, reply, flags); + } + } +}; + +int main() +{ + sp proc(ProcessState::self()); + sp sm(defaultServiceManager()); + sm->addService( String16("led.service"), new LedService ); + IPCThreadState::self()->joinThreadPool(); + + return 0; +} + + +