레이블이 Binder인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Binder인 게시물을 표시합니다. 모든 게시물 표시

2014년 6월 28일 토요일

[Binder] Native Service Example 2

I'll try to finish off Client part



class BpLcdService : public BpInterface
{
public:
 BpLcdService(const sp& impl): BpInterface(impl) {}

 virtual void LcdOn()
 {
  Parcel data, reply;
  remote()->transact(LCD_ON, data, &reply);
 }

 virtual void LcdOff()
 {
  Parcel data, reply;
  remote()->transact(LCD_OFF, data, &reply);
 }
};
IMPLEMENT_META_INTERFACE(LcdService, "android.app.lcdcontrol")

sp<ILcdService> LcdService()
{
sp<IBinder> binder = defaultServiceManager()->getService(String16("LcdService"));
return interface_cast<ILcdService>(binder);
}


This client does not have main() as I intended to make this as shared library file (.so) so I can call LcdOn/Off methods from Java application with Java Native Interface.

[Binder] Native Service Example 1

Here I'm going to make a native service for turning on/off LCD screen, and a client that make a request to the native service. At the end I will try to install a client application to my Android phone and see whether it actually work as intended.

Basically the flows looks like this:



Android uses some prefixes like Bp, Bn,
Bp stands for Binder Proxy (client side) whereas Bn stands for Binder Native, this one is for service server side. That's why we have BpLcdService, BnLcdService.

ok .. firstly in order to communicate between client and server, there should be some sort of protocol
so we define ILcdService that contains only two virtual methods. Whoever that use this protocol must provide LcdOn/Off methods.

class ILcdService : public IInterface
{
public:
       DECLARE_META_INTERFACE(LcdService)
       enum { LCD_ON = 1, LCD_OFF };

       virtual void LcdOn() = 0;
       virtual void LcdOff() = 0;
};

IInterface (in Android F/W) provides asBinder() method to convert the type of ILcdServce to IBinder. Why we need this conversion is that during IPC , IBinder type object will be stored in RPC data and sent to Binder driver. In other words say when registering LcdService to the System, Service Manager needs to send RPC data with service object (BBinder type) to Context Manager. In this process service will be converted to IBinder type and sent to Binder Driver.




2014년 6월 27일 금요일

What's Binder?

Everyone including app developers in Android world  may have heard about Binder, but I was wondering what exactly binder is and how that works. the following is what I've found so far.

Brief History of Binder?
Binder was originally made by George Hoffman as OpenBinder project for BeOS (I do not know why that lived for short time, will appreciate if anyone go find out and share some ideas/opinions)
Anyway OpenBinder project was carried out by Dinnie Hackborn after Be Inc. was acquired by Palm... and Dinnie moved to Google >>>> developed Binder in Android what we're using now.

Why use Binder? We can still use Inter Process Communication like sockets and pipes. Answer for this question would be performance. In Android all system functions are provided as server processes that means an optimized and fast communication method is required. Binder use a kernel memory, that's shared between all processes to minimize memory copy overhead, and provides Remote Procedure Call as well.

Benefits of adopting Binder mechanism seems very similar to those of Microkernel OS.
- It's easy to add a new service or remove an existing function
- It's not necessary to test the entire services, only limited components => easy to test
- Communication between processes is handled through Binder, so it guarantees transparency between components.


The following diagram depicts how Binder works in Android system, as previously mentioned two processes communicates through Binder