mirror of
https://github.com/ioacademy-jikim/android_framwork
synced 2025-06-07 16:06:29 +00:00
8
This commit is contained in:
parent
46d02d8dd3
commit
7c461cd04c
BIN
03_day.pptx
BIN
03_day.pptx
Binary file not shown.
18
03_day/binder_5/Android.bp
Normal file
18
03_day/binder_5/Android.bp
Normal file
@ -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",
|
||||||
|
],
|
||||||
|
}
|
46
03_day/binder_5/ILedService.cpp
Normal file
46
03_day/binder_5/ILedService.cpp
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#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 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
|
41
03_day/binder_5/ILedService.h
Normal file
41
03_day/binder_5/ILedService.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#ifndef ANDROID_ILED_SERVICE_H
|
||||||
|
#define ANDROID_ILED_SERVICE_H
|
||||||
|
|
||||||
|
#include <binder/IInterface.h>
|
||||||
|
#include <utils/String16.h>
|
||||||
|
|
||||||
|
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<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
03_day/binder_5/LedService.cpp
Normal file
10
03_day/binder_5/LedService.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "LedService.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
namespace android
|
||||||
|
{
|
||||||
|
void LedService::ledOn(void)
|
||||||
|
{
|
||||||
|
printf("LedService::ledOn()\n");
|
||||||
|
}
|
||||||
|
};
|
13
03_day/binder_5/LedService.h
Normal file
13
03_day/binder_5/LedService.h
Normal 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 ledOn(void);
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
54
03_day/binder_5/ProcessState.cpp
Normal file
54
03_day/binder_5/ProcessState.cpp
Normal 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
|
23
03_day/binder_5/my_client.cpp
Normal file
23
03_day/binder_5/my_client.cpp
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
#include <binder/ProcessState.h>
|
||||||
|
#include <binder/IServiceManager.h>
|
||||||
|
#include <binder/Binder.h>
|
||||||
|
#include <binder/IPCThreadState.h>
|
||||||
|
#include <utils/String16.h>
|
||||||
|
#include "ILedService.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 );
|
||||||
|
led->ledOn();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
22
03_day/binder_5/my_server.cpp
Normal file
22
03_day/binder_5/my_server.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
18
03_day/binder_6/Android.bp
Normal file
18
03_day/binder_6/Android.bp
Normal file
@ -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",
|
||||||
|
],
|
||||||
|
}
|
49
03_day/binder_6/ILedService.cpp
Normal file
49
03_day/binder_6/ILedService.cpp
Normal 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<ICameraClient> client;
|
||||||
|
data.readStrongBinder(&client);
|
||||||
|
connect(client);
|
||||||
|
return NO_ERROR;
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
return BBinder::onTransact(code, data, reply, flags);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----------------------------------------------------------------------
|
||||||
|
}; // namespace android
|
42
03_day/binder_6/ILedService.h
Normal file
42
03_day/binder_6/ILedService.h
Normal 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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
11
03_day/binder_6/LedService.cpp
Normal file
11
03_day/binder_6/LedService.cpp
Normal 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
03_day/binder_6/LedService.h
Normal file
13
03_day/binder_6/LedService.h
Normal 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
03_day/binder_6/ProcessState.cpp
Normal file
54
03_day/binder_6/ProcessState.cpp
Normal 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
|
25
03_day/binder_6/my_client.cpp
Normal file
25
03_day/binder_6/my_client.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
|
||||||
|
#include <binder/ProcessState.h>
|
||||||
|
#include <binder/IServiceManager.h>
|
||||||
|
#include <binder/Binder.h>
|
||||||
|
#include <binder/IPCThreadState.h>
|
||||||
|
#include <utils/String16.h>
|
||||||
|
#include "ILedService.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 );
|
||||||
|
led->connect( new Led );
|
||||||
|
IPCThreadState::self()->joinThreadPool();
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
22
03_day/binder_6/my_server.cpp
Normal file
22
03_day/binder_6/my_server.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user