SlideShare a Scribd company logo
Android Binder IPC
Chethan Palakshamurthy
Outline
• What is Binder IPC?
• High level design
• Communication between participants
• Low level design
• Creation of proxy and native binders
Index
• What is Binder IPC?
Binder IPC
• The features of Binder are comparable to functionality
provided by any mature traditional client/server
architecture or IPC mechanisms.
– Symbian IPC, Linux D-Bus are couple of the examples.
• Binder takes a different approach with the constructs
used, to better support Android Interface Definition
Language (AIDL) and its implementation
• The main feature of Binder is that, instead of sharing
enumerated command/request ids, the client and server
sides share a common abstract service interface
– There exist two objects which implement the same interface.
(1) Local proxy – for use by application in the same process
and (2) Remote service object – which has the actual service
implementation, resides in service’s process
– Invoking an API on the local proxy object, translates to a call
on the remote object
Index
• What is Binder IPC?
• High level design
Binder IPC – High level design
Service
Manager
Service
Register/
Deregister by service
name
Application
Get Service0..* 1
1
0..*
0..*
0..*
Via
Kernel Driver
IServiceManager
IAbcService
Binder IPC – High level view
• Binder framework uses a kernel driver for IPC - /dev/binder
• Clients to the driver are
– App (Service user)
– Services
– Service Manager (Also a service – a special one)
• Driver assigns and maintains IDs or handles (and much more info)
of each.
• Service manager (Id = 0)
– Registers itself with binder driver, as ‘Manager’ on device startup
– Manages a list of services.
• Services
– Services register themselves with SVC manager on service startup
– Provide an abstract service interface
Binder IPC – Using a service
• First, application gets IServiceManager handle.
– Using the globally known identifier – 0.
– There are helper functions to get this object
• App invokes IServiceManager::GetService to get
a handle of IAbcService for a service “Abc”
– IServiceManager object is implemented by
framework and is part of binder library
• Invokes IAbcService::PerformSomething call
– The call gets translated to PerformSomething call on
the service object
– Service provider needs to implement the
IAbcService
Binder IPC – High level design
Service Object
Application
Local Proxy Object
ProxyAbcService
Remote Native Object
NativeAbcService
IAbcService::
PerformSomething()
Driver
IAbcService::
PerformSomething()
IAbcService
App process Service process
Index
• What is Binder IPC?
• High level design
• Communication
Binder IPC Design – Messaging
…
Service
Manager
Application Service 1
Service 2
Service N
Kernel
Driver
fd=/dev/binder
“Media.Player”
Id: 0
2.
ioctl(fd, params{fd,params{
cmd=ADDSERVICE,
service=“Media.Player”
target=0})
//on startup of process
4.
ioctl(fd, params
{cmd=CREATE,
target=1})
3.
ioctl(fd, params
{cmd=GET,
srv=“media.player”
target=0
outHandle=})
Binder IPC Design - Messaging
1. Service manager opens ‘/dev/binder’ and registers itself (handle
= 0) as manager using ioctl
2. Media Player Service, on process startup, creates an object
instance (MediaPlayerService) and registers it (instance as
handle, say 0x70FF) along with a name, with SVC Mgr.
– By calling ioctl with target handle = 0, in parameter
– Driver knows ‘0’. It directs it to SVC Mgr.
– Seeing ADD_SERVICE in param, SVC Mgr, registers the service along
with provided handle.
– Now, SVC manager knows “Media.Player”. Driver knows media
player service handle – 0x70FF.
3. Application asks SVC Mgr for “Media.Player” service
– By calling ioctl with target handle = 0, cmd=“GET_SERVICE”,
name=“Media.Player”
– SVC Mgr returns the handle associated with “Media.Player”, in ioctl
out params.
Binder IPC Design - Messaging
4.Application asks the service to create one
instance of media player. (Media Player Service supports
multiple player instances)
– By calling ioctl with target handle = ‘0x70FF’
(say)
– Media Player Service on seeing command
‘CREATE’ creates a player instance and embeds
the instance handle in the reply.
Binder IPC Design – Send/Receive
Impl.
• Each client of the driver has 1 or more threads.
• A thread on the server waits on a loop on an ioctl waiting for a
service request.
• The driver puts the thread to sleep using
wait_event_interruptible.
• When an app calls ioctl on its end targeting a service, the driver
wakes up a thread of that service
• ioctl on service end, comes out of the wait, services the request
• Now, if it’s a sync request, app makes another ioctl call waiting
for reply.
• The services sends a reply parcel back by calling ioctl, waking up
the app; and goes back to sleep with another ioctl call (typically
in a loop)
• If the request is Async, service calls ioctl sometime later. But this
time, one of the threads waiting with ioctl will pick it up
Binder IPC Design – Send/Receive -
Sync
App SVC1::Thread1
ioctl(fd,params)
ioctl blocks the thread
and puts into sleep
Kern Driver
ioctl(fd, params)
Thread is resumed,
as ioctl returns
Process(params)
ioctl(fd, ..)
//reply
calls ioctl again to go
back to waiting mode,
suspending the
thread
ioctl(fd,params)
Sending request
mesg
Waiting
for reply
Process
Reply data
Binder IPC Design – Async call from
Service
SVCApp::Thread1
ioctl(fd,params)
ioctl blocks the thread
and puts into sleep
Kern Driver
ioctl(fd, params)
Thread is resumed,
ioctl returns
Process(params)
ioctl(fd, params)
//reply
calls ioctl again to go
back to waiting mode,
suspending the
thread
Sending request
mesg
Waiting
for reply
Process
Reply data
ioctl(fd,params)
Index
• What is Binder IPC?
• High level design
• Communication
• Low level design
Binder IPC – LLD
• Application or service do not call ioctl directly.
• There are layers of objects before an application
intent gets translated to an ioctl. Some important
ones are -
1. Local proxy object  Implements a service specific
abstract interface
– E.g., BpMediaPlayerService (B=binder, p=proxy)
– Each API implementation creates `Parcels` that
encapsulate command/request ID etc.
– Forwards Parcel to proxy helper.
2. Proxy Helper 
– Flattens & converts the parcels into ioctl parameter
objects and makes the ioctl call.
– BpBinder, IPCThreadState
Binder IPC – LLD
3. Remote helper 
– Receives and unflattens the ioctl parameters
– Delegates parcel to remote native object.
– IPCThreadState, BBinder
4. Remote native object
– Does the exact opposite of local proxy object
– Receives the parcel and calls the appropriate service
object
– BnMediaPlayerService
5. Service Object
– Has the ‘real’ implementation of the service
– E.g., MediaPlayerService : BnMediaPlayerService
(B=binder, n=proxy)
– MediaPlayerService
Binder IPC – LLD
IABCInterfaceApplication
Local Proxy object
Remote Helper
Binder Driver
Proxy Helper
<<Extends>>
IPC Message
IPC Message
Remote Native object
Google
Service Implementer
Transact(Parcel*)
onTransact(Parcel*)
Service Object
Binder IPC – LLD
Application
Binder Driver
BpMediaPlayerService
setDataSource(…)
Transact(SET_DATA_SOURCE_URL, Parcel*)
BpBinder
MediaPlayerService::Client
BnMediaPlayerService
setDataSource(…)
OnTransact(SET_DATA_SOURCE_URL, Parcel*)
BBinder
ioctl
resume ioctl
Thread
transact()
IPCThreadState
transact()
IMediaPlayerService
Index
• What is Binder IPC?
• High level design
• Communication
• Low level design
• Creation of proxy and native binders
Binder IPC – Creation of proxy and
native binders
• Getting Service Manager object
– Use sp<IServiceManager> defaultServiceManager() to get handle.
– This function creates a BpBinder(0) and wraps it
with BpServiceManager
– BpBinder is the helper object which can send IPC to
the desired handle. In this case handle = 0.
– BpServiceManager translates manager calls to IPC
using BpBinder object
– That is, a service proxy object wraps a BpBinder
– Wrapping is done with interface_cast<>
Binder IPC – Creation of proxy and
native binders
• Getting service object
– App gets a desired service using sp<IBinder>
IServiceManager::GetService (“Media.Player”)
– When GetService calls ioctl, it gets a virtual
handle to MediaPlayerService.
– A BpBinder(handle) is created and wrapped
with BpMediaPlayerService
– Thus sp<BpMediaPlayerService> is obtained for
App’s use.
Binder IPC – Creation of proxy and
native binders
• Creating a media player instance
– sp<BpMediaPlayerService>.create(…)
– create() sends ioctl message to MediaPlayerService
instance on Media server process
– create API is invoked on MediaPlayerService instance.
– Based on parameters, the service creates a media player
instance - BnMediaPlayer.
– The instance handle is returned embedded in the ioctl
call as a ‘cookie’
– Driver notes the cookie (in binder node inside driver)
and in future transactions to Media Player, it sends the
cookie, along with any msg from Application.
– On app side, sp<BpMediaPlayerService>.create()
method again creates a BpBinder with that handle of
MediaPlayer
Binder IPC – Creation of proxy and
native binders
• Calling API on media player instance
– sp<BpMediaPlayer>.setDataSource(…)
– The implementation creates a Parcel and passes it
on to BpBinder
– The IPC message is delivered to the media server.
– The driver adds the ‘proxy’ pointer along with the
message
– The binder framework on the media server on
receiving the cookie, fetches the native service
instance and passes on the Parcel.
– The instance eventually calls setDataSource on
itself.

More Related Content

What's hot (20)

PDF
Low Level View of Android System Architecture
National Cheng Kung University
 
PPTX
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Bin Chen
 
PDF
Android Binder IPC for Linux
Yu-Hsin Hung
 
PDF
Introduction to Android Window System
National Cheng Kung University
 
PDF
Understanding binder in android
Haifeng Li
 
PDF
Embedded Android : System Development - Part II (HAL)
Emertxe Information Technologies Pvt Ltd
 
PDF
Android Internals
Opersys inc.
 
PDF
Android Internals
Opersys inc.
 
PDF
Embedded Android : System Development - Part III (Audio / Video HAL)
Emertxe Information Technologies Pvt Ltd
 
PDF
Android's HIDL: Treble in the HAL
Opersys inc.
 
PDF
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
PPT
Learning AOSP - Android Booting Process
Nanik Tolaram
 
PPTX
Android AIDL Concept
Charile Tsai
 
ODP
Android Camera Architecture
Picker Weng
 
PPT
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
Nanik Tolaram
 
ODP
Q4.11: Porting Android to new Platforms
Linaro
 
PDF
Android device driver structure introduction
William Liang
 
PDF
The Android graphics path, in depth
Chris Simmonds
 
PPT
Learning AOSP - Android Linux Device Driver
Nanik Tolaram
 
PPTX
Aidl service
Anjan Debnath
 
Low Level View of Android System Architecture
National Cheng Kung University
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Bin Chen
 
Android Binder IPC for Linux
Yu-Hsin Hung
 
Introduction to Android Window System
National Cheng Kung University
 
Understanding binder in android
Haifeng Li
 
Embedded Android : System Development - Part II (HAL)
Emertxe Information Technologies Pvt Ltd
 
Android Internals
Opersys inc.
 
Android Internals
Opersys inc.
 
Embedded Android : System Development - Part III (Audio / Video HAL)
Emertxe Information Technologies Pvt Ltd
 
Android's HIDL: Treble in the HAL
Opersys inc.
 
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
Learning AOSP - Android Booting Process
Nanik Tolaram
 
Android AIDL Concept
Charile Tsai
 
Android Camera Architecture
Picker Weng
 
"Learning AOSP" - Android Hardware Abstraction Layer (HAL)
Nanik Tolaram
 
Q4.11: Porting Android to new Platforms
Linaro
 
Android device driver structure introduction
William Liang
 
The Android graphics path, in depth
Chris Simmonds
 
Learning AOSP - Android Linux Device Driver
Nanik Tolaram
 
Aidl service
Anjan Debnath
 

Viewers also liked (20)

ODP
Inter-process communication of Android
Tetsuyuki Kobayashi
 
PDF
Android binder-ipc
magoroku Yamamoto
 
PDF
Android ipm 20110409
Tetsuyuki Kobayashi
 
PPTX
Understanding open max il
Chethan Pchethan
 
ODP
Intentの概要
l_b__
 
PDF
Androidのリカバリシステム (Androidのシステムアップデート)
l_b__
 
PDF
Binderのはじめの一歩とAndroid
l_b__
 
PDF
IOMX in Android
Raghavan Venkateswaran
 
ODP
Android crash debugging
Ashish Agrawal
 
PPTX
Dense Bituminous macadam
pradip dangar
 
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
PDF
Using the Presentation API and external screens on Android
Xavier Hallade
 
PPT
Bituminous pavement
Rron de Guzman
 
PPTX
Knee biomechanic
Ratan Khuman
 
PDF
Android IPC: Binder
Kan-Han (John) Lu
 
PDF
Flow of events during Media Player creation in Android
Somenath Mukhopadhyay
 
PDF
An Introduction to Android Internals
Anjana Somathilake
 
PDF
Beyond JVM - YOW! Brisbane 2013
Charles Nutter
 
PPT
Android Hacks - Hack57
Masanori Ohkawara
 
PPTX
Permission use analysis for vetting undesirable behavior in
chaitrabhat777
 
Inter-process communication of Android
Tetsuyuki Kobayashi
 
Android binder-ipc
magoroku Yamamoto
 
Android ipm 20110409
Tetsuyuki Kobayashi
 
Understanding open max il
Chethan Pchethan
 
Intentの概要
l_b__
 
Androidのリカバリシステム (Androidのシステムアップデート)
l_b__
 
Binderのはじめの一歩とAndroid
l_b__
 
IOMX in Android
Raghavan Venkateswaran
 
Android crash debugging
Ashish Agrawal
 
Dense Bituminous macadam
pradip dangar
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
Using the Presentation API and external screens on Android
Xavier Hallade
 
Bituminous pavement
Rron de Guzman
 
Knee biomechanic
Ratan Khuman
 
Android IPC: Binder
Kan-Han (John) Lu
 
Flow of events during Media Player creation in Android
Somenath Mukhopadhyay
 
An Introduction to Android Internals
Anjana Somathilake
 
Beyond JVM - YOW! Brisbane 2013
Charles Nutter
 
Android Hacks - Hack57
Masanori Ohkawara
 
Permission use analysis for vetting undesirable behavior in
chaitrabhat777
 
Ad

Similar to Overview of Android binder IPC implementation (20)

PDF
Embedded Android : System Development - Part IV (Android System Services)
Emertxe Information Technologies Pvt Ltd
 
PDF
June 2014 - IPC in android
BlrDroid
 
PDF
Binding android piece by piece
Bucharest Java User Group
 
PPTX
Inter Process Communication (IPC) in Android
Malwinder Singh
 
PPTX
IPC: AIDL is not a curse
Yonatan Levin
 
PDF
Android framework design and development
ramalinga prasad tadepalli
 
PPTX
Ipc: aidl sexy, not a curse
Yonatan Levin
 
PPTX
IPC: AIDL is sexy, not a curse
Yonatan Levin
 
PPTX
Android service, aidl - day 1
Utkarsh Mankad
 
PDF
Performance evaluation of Android IPC
承剛 謝
 
PDF
Android IPC Mechanism
Lihan Chen
 
PDF
IPC with Qt
Marius Bugge Monsen
 
PDF
AIDL - Android Interface Definition Language
Arvind Devaraj
 
PPT
Service oriented component model
ravindrareddy
 
PDF
Android service
Krazy Koder
 
ODP
Android App Development - 08 Services
Diego Grancini
 
PPTX
WCF Fundamentals
Safaa Farouk
 
PPTX
Android Service
Charile Tsai
 
PDF
UA Mobile 2012 (English)
dmalykhanov
 
PDF
GOTO Night with Todd Montgomery: Aeron: What, why and what next?
Alexandra Masterson
 
Embedded Android : System Development - Part IV (Android System Services)
Emertxe Information Technologies Pvt Ltd
 
June 2014 - IPC in android
BlrDroid
 
Binding android piece by piece
Bucharest Java User Group
 
Inter Process Communication (IPC) in Android
Malwinder Singh
 
IPC: AIDL is not a curse
Yonatan Levin
 
Android framework design and development
ramalinga prasad tadepalli
 
Ipc: aidl sexy, not a curse
Yonatan Levin
 
IPC: AIDL is sexy, not a curse
Yonatan Levin
 
Android service, aidl - day 1
Utkarsh Mankad
 
Performance evaluation of Android IPC
承剛 謝
 
Android IPC Mechanism
Lihan Chen
 
IPC with Qt
Marius Bugge Monsen
 
AIDL - Android Interface Definition Language
Arvind Devaraj
 
Service oriented component model
ravindrareddy
 
Android service
Krazy Koder
 
Android App Development - 08 Services
Diego Grancini
 
WCF Fundamentals
Safaa Farouk
 
Android Service
Charile Tsai
 
UA Mobile 2012 (English)
dmalykhanov
 
GOTO Night with Todd Montgomery: Aeron: What, why and what next?
Alexandra Masterson
 
Ad

Recently uploaded (20)

PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Introduction to Basic Renewable Energy.pptx
examcoordinatormesu
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPT
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Introduction to Basic Renewable Energy.pptx
examcoordinatormesu
 
Thermal runway and thermal stability.pptx
godow93766
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
MATLAB : Introduction , Features , Display Windows, Syntax, Operators, Graph...
Amity University, Patna
 
Design Thinking basics for Engineers.pdf
CMR University
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Water Industry Process Automation & Control Monthly July 2025
Water Industry Process Automation & Control
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 

Overview of Android binder IPC implementation

  • 1. Android Binder IPC Chethan Palakshamurthy
  • 2. Outline • What is Binder IPC? • High level design • Communication between participants • Low level design • Creation of proxy and native binders
  • 3. Index • What is Binder IPC?
  • 4. Binder IPC • The features of Binder are comparable to functionality provided by any mature traditional client/server architecture or IPC mechanisms. – Symbian IPC, Linux D-Bus are couple of the examples. • Binder takes a different approach with the constructs used, to better support Android Interface Definition Language (AIDL) and its implementation • The main feature of Binder is that, instead of sharing enumerated command/request ids, the client and server sides share a common abstract service interface – There exist two objects which implement the same interface. (1) Local proxy – for use by application in the same process and (2) Remote service object – which has the actual service implementation, resides in service’s process – Invoking an API on the local proxy object, translates to a call on the remote object
  • 5. Index • What is Binder IPC? • High level design
  • 6. Binder IPC – High level design Service Manager Service Register/ Deregister by service name Application Get Service0..* 1 1 0..* 0..* 0..* Via Kernel Driver IServiceManager IAbcService
  • 7. Binder IPC – High level view • Binder framework uses a kernel driver for IPC - /dev/binder • Clients to the driver are – App (Service user) – Services – Service Manager (Also a service – a special one) • Driver assigns and maintains IDs or handles (and much more info) of each. • Service manager (Id = 0) – Registers itself with binder driver, as ‘Manager’ on device startup – Manages a list of services. • Services – Services register themselves with SVC manager on service startup – Provide an abstract service interface
  • 8. Binder IPC – Using a service • First, application gets IServiceManager handle. – Using the globally known identifier – 0. – There are helper functions to get this object • App invokes IServiceManager::GetService to get a handle of IAbcService for a service “Abc” – IServiceManager object is implemented by framework and is part of binder library • Invokes IAbcService::PerformSomething call – The call gets translated to PerformSomething call on the service object – Service provider needs to implement the IAbcService
  • 9. Binder IPC – High level design Service Object Application Local Proxy Object ProxyAbcService Remote Native Object NativeAbcService IAbcService:: PerformSomething() Driver IAbcService:: PerformSomething() IAbcService App process Service process
  • 10. Index • What is Binder IPC? • High level design • Communication
  • 11. Binder IPC Design – Messaging … Service Manager Application Service 1 Service 2 Service N Kernel Driver fd=/dev/binder “Media.Player” Id: 0 2. ioctl(fd, params{fd,params{ cmd=ADDSERVICE, service=“Media.Player” target=0}) //on startup of process 4. ioctl(fd, params {cmd=CREATE, target=1}) 3. ioctl(fd, params {cmd=GET, srv=“media.player” target=0 outHandle=})
  • 12. Binder IPC Design - Messaging 1. Service manager opens ‘/dev/binder’ and registers itself (handle = 0) as manager using ioctl 2. Media Player Service, on process startup, creates an object instance (MediaPlayerService) and registers it (instance as handle, say 0x70FF) along with a name, with SVC Mgr. – By calling ioctl with target handle = 0, in parameter – Driver knows ‘0’. It directs it to SVC Mgr. – Seeing ADD_SERVICE in param, SVC Mgr, registers the service along with provided handle. – Now, SVC manager knows “Media.Player”. Driver knows media player service handle – 0x70FF. 3. Application asks SVC Mgr for “Media.Player” service – By calling ioctl with target handle = 0, cmd=“GET_SERVICE”, name=“Media.Player” – SVC Mgr returns the handle associated with “Media.Player”, in ioctl out params.
  • 13. Binder IPC Design - Messaging 4.Application asks the service to create one instance of media player. (Media Player Service supports multiple player instances) – By calling ioctl with target handle = ‘0x70FF’ (say) – Media Player Service on seeing command ‘CREATE’ creates a player instance and embeds the instance handle in the reply.
  • 14. Binder IPC Design – Send/Receive Impl. • Each client of the driver has 1 or more threads. • A thread on the server waits on a loop on an ioctl waiting for a service request. • The driver puts the thread to sleep using wait_event_interruptible. • When an app calls ioctl on its end targeting a service, the driver wakes up a thread of that service • ioctl on service end, comes out of the wait, services the request • Now, if it’s a sync request, app makes another ioctl call waiting for reply. • The services sends a reply parcel back by calling ioctl, waking up the app; and goes back to sleep with another ioctl call (typically in a loop) • If the request is Async, service calls ioctl sometime later. But this time, one of the threads waiting with ioctl will pick it up
  • 15. Binder IPC Design – Send/Receive - Sync App SVC1::Thread1 ioctl(fd,params) ioctl blocks the thread and puts into sleep Kern Driver ioctl(fd, params) Thread is resumed, as ioctl returns Process(params) ioctl(fd, ..) //reply calls ioctl again to go back to waiting mode, suspending the thread ioctl(fd,params) Sending request mesg Waiting for reply Process Reply data
  • 16. Binder IPC Design – Async call from Service SVCApp::Thread1 ioctl(fd,params) ioctl blocks the thread and puts into sleep Kern Driver ioctl(fd, params) Thread is resumed, ioctl returns Process(params) ioctl(fd, params) //reply calls ioctl again to go back to waiting mode, suspending the thread Sending request mesg Waiting for reply Process Reply data ioctl(fd,params)
  • 17. Index • What is Binder IPC? • High level design • Communication • Low level design
  • 18. Binder IPC – LLD • Application or service do not call ioctl directly. • There are layers of objects before an application intent gets translated to an ioctl. Some important ones are - 1. Local proxy object  Implements a service specific abstract interface – E.g., BpMediaPlayerService (B=binder, p=proxy) – Each API implementation creates `Parcels` that encapsulate command/request ID etc. – Forwards Parcel to proxy helper. 2. Proxy Helper  – Flattens & converts the parcels into ioctl parameter objects and makes the ioctl call. – BpBinder, IPCThreadState
  • 19. Binder IPC – LLD 3. Remote helper  – Receives and unflattens the ioctl parameters – Delegates parcel to remote native object. – IPCThreadState, BBinder 4. Remote native object – Does the exact opposite of local proxy object – Receives the parcel and calls the appropriate service object – BnMediaPlayerService 5. Service Object – Has the ‘real’ implementation of the service – E.g., MediaPlayerService : BnMediaPlayerService (B=binder, n=proxy) – MediaPlayerService
  • 20. Binder IPC – LLD IABCInterfaceApplication Local Proxy object Remote Helper Binder Driver Proxy Helper <<Extends>> IPC Message IPC Message Remote Native object Google Service Implementer Transact(Parcel*) onTransact(Parcel*) Service Object
  • 21. Binder IPC – LLD Application Binder Driver BpMediaPlayerService setDataSource(…) Transact(SET_DATA_SOURCE_URL, Parcel*) BpBinder MediaPlayerService::Client BnMediaPlayerService setDataSource(…) OnTransact(SET_DATA_SOURCE_URL, Parcel*) BBinder ioctl resume ioctl Thread transact() IPCThreadState transact() IMediaPlayerService
  • 22. Index • What is Binder IPC? • High level design • Communication • Low level design • Creation of proxy and native binders
  • 23. Binder IPC – Creation of proxy and native binders • Getting Service Manager object – Use sp<IServiceManager> defaultServiceManager() to get handle. – This function creates a BpBinder(0) and wraps it with BpServiceManager – BpBinder is the helper object which can send IPC to the desired handle. In this case handle = 0. – BpServiceManager translates manager calls to IPC using BpBinder object – That is, a service proxy object wraps a BpBinder – Wrapping is done with interface_cast<>
  • 24. Binder IPC – Creation of proxy and native binders • Getting service object – App gets a desired service using sp<IBinder> IServiceManager::GetService (“Media.Player”) – When GetService calls ioctl, it gets a virtual handle to MediaPlayerService. – A BpBinder(handle) is created and wrapped with BpMediaPlayerService – Thus sp<BpMediaPlayerService> is obtained for App’s use.
  • 25. Binder IPC – Creation of proxy and native binders • Creating a media player instance – sp<BpMediaPlayerService>.create(…) – create() sends ioctl message to MediaPlayerService instance on Media server process – create API is invoked on MediaPlayerService instance. – Based on parameters, the service creates a media player instance - BnMediaPlayer. – The instance handle is returned embedded in the ioctl call as a ‘cookie’ – Driver notes the cookie (in binder node inside driver) and in future transactions to Media Player, it sends the cookie, along with any msg from Application. – On app side, sp<BpMediaPlayerService>.create() method again creates a BpBinder with that handle of MediaPlayer
  • 26. Binder IPC – Creation of proxy and native binders • Calling API on media player instance – sp<BpMediaPlayer>.setDataSource(…) – The implementation creates a Parcel and passes it on to BpBinder – The IPC message is delivered to the media server. – The driver adds the ‘proxy’ pointer along with the message – The binder framework on the media server on receiving the cookie, fetches the native service instance and passes on the Parcel. – The instance eventually calls setDataSource on itself.