diff --git a/03_day.pptx b/03_day.pptx index 2ab9872..adc9d51 100644 Binary files a/03_day.pptx and b/03_day.pptx differ diff --git a/03_day/binder_5/Android.bp b/03_day/binder_5/Android.bp new file mode 100644 index 0000000..6ea477f --- /dev/null +++ b/03_day/binder_5/Android.bp @@ -0,0 +1,18 @@ +cc_binary { + name: "my_server_cpp_3", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_server.cpp", + "ILedService.cpp", + "LedService.cpp", + ], +} + +cc_binary { + name: "my_client_cpp_3", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_client.cpp", + "ILedService.cpp", + ], +} diff --git a/03_day/binder_5/ILedService.cpp b/03_day/binder_5/ILedService.cpp new file mode 100644 index 0000000..d8c6244 --- /dev/null +++ b/03_day/binder_5/ILedService.cpp @@ -0,0 +1,46 @@ +#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"); + +// ---------------------------------------------------------------------- + +status_t BnLedService::onTransact( + uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) +{ + switch (code) { + case LED_ON: { + ledOn(); + return NO_ERROR; + } break; + default: + return BBinder::onTransact(code, data, reply, flags); + } +} +// ---------------------------------------------------------------------- +}; // namespace android diff --git a/03_day/binder_5/ILedService.h b/03_day/binder_5/ILedService.h new file mode 100644 index 0000000..1def502 --- /dev/null +++ b/03_day/binder_5/ILedService.h @@ -0,0 +1,41 @@ +#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 + }; +}; + +// ---------------------------------------------------------------------- + +class BnLedService : public BnInterface +{ +public: + virtual status_t onTransact( uint32_t code, + const Parcel& data, + Parcel* reply, + uint32_t flags = 0); +}; + +// ---------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_ISERVICE_MANAGER_H + + + + diff --git a/03_day/binder_5/LedService.cpp b/03_day/binder_5/LedService.cpp new file mode 100644 index 0000000..a62c39f --- /dev/null +++ b/03_day/binder_5/LedService.cpp @@ -0,0 +1,10 @@ +#include "LedService.h" +#include + +namespace android +{ + void LedService::ledOn(void) + { + printf("LedService::ledOn()\n"); + } +}; diff --git a/03_day/binder_5/LedService.h b/03_day/binder_5/LedService.h new file mode 100644 index 0000000..27508ba --- /dev/null +++ b/03_day/binder_5/LedService.h @@ -0,0 +1,13 @@ +#ifndef ANDROID_LED_SERVICE_H +#define ANDROID_LED_SERVICE_H +#include "ILedService.h" + +namespace android { + class LedService : public BnLedService + { + public : + void ledOn(void); + }; +}; + +#endif diff --git a/03_day/binder_5/ProcessState.cpp b/03_day/binder_5/ProcessState.cpp new file mode 100644 index 0000000..07b6867 --- /dev/null +++ b/03_day/binder_5/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_5/my_client.cpp b/03_day/binder_5/my_client.cpp new file mode 100644 index 0000000..c651d64 --- /dev/null +++ b/03_day/binder_5/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_5/my_server.cpp b/03_day/binder_5/my_server.cpp new file mode 100644 index 0000000..18816ac --- /dev/null +++ b/03_day/binder_5/my_server.cpp @@ -0,0 +1,22 @@ + +#include +#include +#include +#include +#include +#include "LedService.h" + +using namespace android; + +int main() +{ + sp proc(ProcessState::self()); + sp sm(defaultServiceManager()); + sm->addService( String16("led.service"), new LedService ); + IPCThreadState::self()->joinThreadPool(); + + return 0; +} + + + diff --git a/03_day/binder_6/Android.bp b/03_day/binder_6/Android.bp new file mode 100644 index 0000000..6ea477f --- /dev/null +++ b/03_day/binder_6/Android.bp @@ -0,0 +1,18 @@ +cc_binary { + name: "my_server_cpp_3", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_server.cpp", + "ILedService.cpp", + "LedService.cpp", + ], +} + +cc_binary { + name: "my_client_cpp_3", + shared_libs: ["liblog", "libutils", "libbinder"], + srcs: [ + "my_client.cpp", + "ILedService.cpp", + ], +} diff --git a/03_day/binder_6/ILedService.cpp b/03_day/binder_6/ILedService.cpp new file mode 100644 index 0000000..a9529ad --- /dev/null +++ b/03_day/binder_6/ILedService.cpp @@ -0,0 +1,49 @@ +#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 connect(sp& client) + { + printf("BpLedService::ledOn()\n"); + Parcel data, reply; + data.writeStrongBinder(ILedClient::asBinder(client)); + remote()->transact(CONNECT, data, &reply); + } +}; + +IMPLEMENT_META_INTERFACE(LedService, "android.os.ILedService"); + +// ---------------------------------------------------------------------- + +status_t BnLedService::onTransact( + uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) +{ + switch (code) { + case CONNECT: { + sp client; + data.readStrongBinder(&client); + connect(client); + return NO_ERROR; + } break; + default: + return BBinder::onTransact(code, data, reply, flags); + } +} +// ---------------------------------------------------------------------- +}; // namespace android diff --git a/03_day/binder_6/ILedService.h b/03_day/binder_6/ILedService.h new file mode 100644 index 0000000..9a67220 --- /dev/null +++ b/03_day/binder_6/ILedService.h @@ -0,0 +1,42 @@ +#ifndef ANDROID_ILED_SERVICE_H +#define ANDROID_ILED_SERVICE_H + +#include +#include +#include "ILedClient.h" + +namespace android { + +// ---------------------------------------------------------------------- + +class ILedService : public IInterface +{ +public: + DECLARE_META_INTERFACE(LedService) + + virtual void connect(sp& client) = 0; + + enum { + CONNECT = IBinder::FIRST_CALL_TRANSACTION + }; +}; + +// ---------------------------------------------------------------------- + +class BnLedService : public BnInterface +{ +public: + virtual status_t onTransact( uint32_t code, + const Parcel& data, + Parcel* reply, + uint32_t flags = 0); +}; + +// ---------------------------------------------------------------------------- +}; // namespace android + +#endif // ANDROID_ISERVICE_MANAGER_H + + + + diff --git a/03_day/binder_6/LedService.cpp b/03_day/binder_6/LedService.cpp new file mode 100644 index 0000000..55b4ca0 --- /dev/null +++ b/03_day/binder_6/LedService.cpp @@ -0,0 +1,11 @@ +#include "LedService.h" +#include + +namespace android +{ + void LedService::connect(sp& client) + { + printf("LedService::ledOn()\n"); + client->dataCallback(222); + } +}; diff --git a/03_day/binder_6/LedService.h b/03_day/binder_6/LedService.h new file mode 100644 index 0000000..1bb5d4a --- /dev/null +++ b/03_day/binder_6/LedService.h @@ -0,0 +1,13 @@ +#ifndef ANDROID_LED_SERVICE_H +#define ANDROID_LED_SERVICE_H +#include "ILedService.h" + +namespace android { + class LedService : public BnLedService + { + public : + void connect(sp& client); + }; +}; + +#endif diff --git a/03_day/binder_6/ProcessState.cpp b/03_day/binder_6/ProcessState.cpp new file mode 100644 index 0000000..07b6867 --- /dev/null +++ b/03_day/binder_6/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_6/my_client.cpp b/03_day/binder_6/my_client.cpp new file mode 100644 index 0000000..e7bb9d1 --- /dev/null +++ b/03_day/binder_6/my_client.cpp @@ -0,0 +1,25 @@ + +#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->connect( new Led ); + IPCThreadState::self()->joinThreadPool(); + + + return 0; +} + + + diff --git a/03_day/binder_6/my_server.cpp b/03_day/binder_6/my_server.cpp new file mode 100644 index 0000000..18816ac --- /dev/null +++ b/03_day/binder_6/my_server.cpp @@ -0,0 +1,22 @@ + +#include +#include +#include +#include +#include +#include "LedService.h" + +using namespace android; + +int main() +{ + sp proc(ProcessState::self()); + sp sm(defaultServiceManager()); + sm->addService( String16("led.service"), new LedService ); + IPCThreadState::self()->joinThreadPool(); + + return 0; +} + + +