{"id":303,"date":"2011-01-03T10:02:42","date_gmt":"2011-01-03T15:02:42","guid":{"rendered":"http:\/\/ebixio.com\/blog\/?p=303"},"modified":"2019-01-21T11:31:38","modified_gmt":"2019-01-21T16:31:38","slug":"the-android-ipc-system","status":"publish","type":"post","link":"http:\/\/ebixio.com\/blog\/2011\/01\/03\/the-android-ipc-system\/","title":{"rendered":"The Android IPC system"},"content":{"rendered":"<p>The information below comes from a number of sources, including my own experiments with the Android IPC and some disparate internet sources.<\/p>\n<p>The overall architecture of the Android IPC system is shown in the diagram below. It consists of four major blocks; one in kernel space, and the other three in user space. The dashed lines represent the logical RPC calls. The solid lines represent the actual data flow.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/ebixio.com\/blog\/wp-content\/uploads\/2010\/12\/AndroidIPC.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-307\" title=\"AndroidIPC\" src=\"http:\/\/ebixio.com\/blog\/wp-content\/uploads\/2010\/12\/AndroidIPC.png\" alt=\"\" width=\"416\" height=\"213\" srcset=\"http:\/\/ebixio.com\/blog\/wp-content\/uploads\/2010\/12\/AndroidIPC.png 594w, http:\/\/ebixio.com\/blog\/wp-content\/uploads\/2010\/12\/AndroidIPC-300x153.png 300w\" sizes=\"auto, (max-width: 416px) 100vw, 416px\" \/><\/a><\/p>\n<ul>\n<li><code>BinderDriver<\/code>: This is the core of IPC system. It passes data between a <code>ServiceProvider<\/code>(s) and a <code>ServiceUser<\/code>(s). This kernel component is provided by Android.<\/li>\n<li><code>ServiceProvider<\/code>: Provides some kind of service. It parses the received RPC data from the <code>BinderDriver<\/code> and does the real work. Application developers will either make use of existing service providers (such as the <code>Camera<\/code> or <code>AudioFlinger<\/code>), or in some cases will write their own.<\/li>\n<li><code>ServiceManager<\/code>: This is a special singleton ServiceProvider that provides service manager services for other service providers. This component is provided by Android.<\/li>\n<li><code>ServiceUser<\/code>: This is the client. It remote calls the <code>ServiceProvider<\/code> by generating an RPC and sending it to the <code>BinderDriver<\/code>. Application developers typically write their own <code>ServiceUser<\/code> as part of their application.<\/li>\n<\/ul>\n<p>Here is a typical flow of events for a fictitious <code>MultServiceProvider<\/code> (a service provider that multiplies two numbers for a client) and a <code>MultServiceUser<\/code> client which doesn&#8217;t know how to do multiplication (maybe because the numbers are quaternions) and needs to use the <code>MultServiceProvider<\/code>:<\/p>\n<ol>\n<li><code>ServiceManager<\/code> runs first (at power-up) and registers a special node (node O) with the <code>BinderDriver<\/code>.<\/li>\n<li>The <code>MultServiceProvider<\/code> gets an <code>IServiceManager<\/code> proxy object for the special node O by calling the global <code>defaultServiceManager()<\/code> function.<\/li>\n<li>The <code>MultServiceProvider<\/code> then calls <code>defaultServiceManager()-&gt;addService(\"Multiplier\", new MultServiceProvider())<\/code> to add itself as a service provider and then waits in an infinite loop for someone to request its services. The <code>addService<\/code> RPC call is routed to the <code>ServiceManager<\/code> through the <code>BinderDriver<\/code>.<\/li>\n<li>The <code>BinderDriver<\/code> notices that the RPC is for the <code>ServiceManager<\/code> to add a new service, so besides routing the RPC to the <code>ServiceManager<\/code> it generates another node (let&#8217;s call it node M), for the new <code>MultServiceProvider<\/code>.<\/li>\n<li>The <code>ServiceManager<\/code> reads the data from the <code>BinderDriver<\/code> and processes the <code>IServiceManager::addService<\/code> RPC call.<\/li>\n<li>The <code>MultServiceUser<\/code> client process gets an <code>IServiceManager<\/code> proxy object for the special node O (again by using <code>defaultServiceManager()<\/code>).<\/li>\n<li>The client does an <code>IServiceManager::getService(\"Multiplier\")<\/code> RPC call to get the <code>MultServiceProvider<\/code>. This call is routed to the <code>ServiceManager<\/code> through the <code>BinderDriver<\/code>.<\/li>\n<li>The <code>ServiceManager<\/code> reads the RPC data from the <code>BinderDriver<\/code>, processes the <code>IServiceManager::getService<\/code> request and returns back the node representing the <code>MultServiceProvider<\/code>.<\/li>\n<li><code>MultServiceUser<\/code> calls <code>MultServiceProvider::multiply(a, b)<\/code>. This call is routed to\u00a0 the <code>MultServiceProvider<\/code> by the <code>BinderDriver<\/code>.<\/li>\n<li>The <code>MultServiceProvider<\/code> handles the <code>MultServiceProvider::multiply<\/code> RPC call and sends the product of the 2 numbers in a reply to the <code>BinderDriver<\/code>.<\/li>\n<li>The <code>BinderDriver<\/code> routes the reply back to the client.<\/li>\n<li>The client reads the data from the <code>BinderDriver<\/code> which contains the result of &#8220;a * b&#8221;.<\/li>\n<\/ol>\n<p>In a future post I hope to discuss the whole architecture in more detail, with concrete code examples for how to use <code>IBinder<\/code>, <code>IInterface<\/code>, <code>BBinder<\/code>, <code>BpInterface<\/code>, <code>BnInterface<\/code>, etc&#8230; to create a <code>ServiceProvider<\/code> and a <code>ServiceUser<\/code> all in native C++ code on Android.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The information below comes from a number of sources, including my own experiments with the Android IPC and some disparate internet sources. The overall architecture of the Android IPC system is shown in the diagram below. It consists of four major blocks; one in kernel space, and the other three in user space. The dashed [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[33],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-android"],"_links":{"self":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":10,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":651,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts\/303\/revisions\/651"}],"wp:attachment":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}