This commit is contained in:
ioacademy-jikim
2018-06-15 10:55:16 +09:00
parent f3d74671fa
commit 7a0a2db279
53 changed files with 3849 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
cc_binary {
name: "my_server_cpp_4",
shared_libs: ["liblog", "libutils", "libbinder"],
srcs: [
"my_server.cpp",
"ILedService.cpp",
"LedService.cpp",
"ILedClient.cpp",
],
}
cc_binary {
name: "my_client_cpp_4",
shared_libs: ["liblog", "libutils", "libbinder"],
srcs: [
"my_client.cpp",
"ILedService.cpp",
"ILedClient.cpp",
"Led.cpp",
],
}
+49
View File
@@ -0,0 +1,49 @@
#define LOG_TAG "LedClient"
#include "ILedClient.h"
#include <binder/IPCThreadState.h>
#include <binder/Parcel.h>
#include <unistd.h>
namespace android {
// ----------------------------------------------------------------------
class BpLedClient : public BpInterface<ILedClient>
{
public:
explicit BpLedClient(const sp<IBinder>& impl)
: BpInterface<ILedClient>(impl)
{
}
virtual void dataCallback(int ratio)
{
printf("BpLedClient::dataCallback()\n");
Parcel data, reply;
data.writeInt32(ratio);
remote()->transact(DATA_CALLBACK, data, &reply);
}
};
IMPLEMENT_META_INTERFACE(LedClient, "android.os.ILedClient");
// ----------------------------------------------------------------------
status_t BnLedClient::onTransact(
uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
{
switch (code) {
case DATA_CALLBACK: {
int temp;
temp = data.readInt32();
dataCallback(temp);
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
// ----------------------------------------------------------------------
}; // namespace android
+41
View File
@@ -0,0 +1,41 @@
#ifndef ANDROID_ILED_CLIENT_H
#define ANDROID_ILED_CLIENT_H
#include <binder/IInterface.h>
#include <utils/String16.h>
namespace android {
// ----------------------------------------------------------------------
class ILedClient : public IInterface
{
public:
DECLARE_META_INTERFACE(LedClient)
virtual void dataCallback (int ratio) = 0;
enum {
DATA_CALLBACK = IBinder::FIRST_CALL_TRANSACTION
};
};
// ----------------------------------------------------------------------
class BnLedClient : public BnInterface<ILedClient>
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_ISERVICE_MANAGER_H
+49
View File
@@ -0,0 +1,49 @@
#define LOG_TAG "LedService"
#include "ILedService.h"
#include <binder/IPCThreadState.h>
#include <binder/Parcel.h>
#include <unistd.h>
namespace android {
// ----------------------------------------------------------------------
class BpLedService : public BpInterface<ILedService>
{
public:
explicit BpLedService(const sp<IBinder>& impl)
: BpInterface<ILedService>(impl)
{
}
virtual void connect(sp<ILedClient>& 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<ILedClient> client;
data.readStrongBinder(&client);
connect(client);
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
// ----------------------------------------------------------------------
}; // namespace android
+42
View File
@@ -0,0 +1,42 @@
#ifndef ANDROID_ILED_SERVICE_H
#define ANDROID_ILED_SERVICE_H
#include <binder/IInterface.h>
#include <utils/String16.h>
#include "ILedClient.h"
namespace android {
// ----------------------------------------------------------------------
class ILedService : public IInterface
{
public:
DECLARE_META_INTERFACE(LedService)
virtual void connect(sp<ILedClient>& client) = 0;
enum {
CONNECT = IBinder::FIRST_CALL_TRANSACTION
};
};
// ----------------------------------------------------------------------
class BnLedService : public BnInterface<ILedService>
{
public:
virtual status_t onTransact( uint32_t code,
const Parcel& data,
Parcel* reply,
uint32_t flags = 0);
};
// ----------------------------------------------------------------------------
}; // namespace android
#endif // ANDROID_ISERVICE_MANAGER_H
+10
View File
@@ -0,0 +1,10 @@
#include "Led.h"
#include <stdio.h>
namespace android
{
void Led::dataCallback(int ratio)
{
printf("Led::dataCallback(%d)\n", ratio);
}
};
+13
View File
@@ -0,0 +1,13 @@
#ifndef ANDROID_LED_H
#define ANDROID_LED_H
#include "ILedClient.h"
namespace android {
class Led : public BnLedClient
{
public :
void dataCallback(int ratio);
};
};
#endif
+11
View File
@@ -0,0 +1,11 @@
#include "LedService.h"
#include <stdio.h>
namespace android
{
void LedService::connect(sp<ILedClient>& client)
{
printf("LedService::ledOn()\n");
client->dataCallback(222);
}
};
+13
View File
@@ -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<ILedClient>& client);
};
};
#endif
+54
View File
@@ -0,0 +1,54 @@
#if 1
#include <stdio.h>
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 <stdio.h>
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
Binary file not shown.
+27
View File
@@ -0,0 +1,27 @@
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/Binder.h>
#include <binder/IPCThreadState.h>
#include <utils/String16.h>
#include "ILedService.h"
#include "Led.h"
using namespace android;
int main()
{
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm(defaultServiceManager());
sp<IBinder> binder = sm->getService( String16("led.service") );
sp<ILedService> led = interface_cast<ILedService>( binder );
sp<ILedClient> client = new Led;
led->connect( client );
IPCThreadState::self()->joinThreadPool();
return 0;
}
+22
View File
@@ -0,0 +1,22 @@
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/Binder.h>
#include <binder/IPCThreadState.h>
#include <utils/String16.h>
#include "LedService.h"
using namespace android;
int main()
{
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm(defaultServiceManager());
sm->addService( String16("led.service"), new LedService );
IPCThreadState::self()->joinThreadPool();
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
#if 1
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int global=6;
void *foo(void *data)
{
printf("child , global=%d, local=%d\n", ++global, ++*(int*)data);
return 0;
}
int main()
{
int local=10;
pthread_t thread;
pthread_create(&thread, 0, foo, &local );
//foo(&local);
pthread_join( thread, 0 );
printf("parent , global=%d, local=%d\n", ++global, local);
return 0;
}
#endif
#if 0
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
int global=6;
int main()
{
pid_t pid;
pid = fork();
if( pid == 0 )
{
printf("child, global=%d\n", ++global);
exit(0);
}
wait(0);
printf("parent, global=%d\n", global);
return 0;
}
#endif