androidandroid-serviceandroid-sourceaidlandroid-service-binding

Connect service with app via AIDL in AOSP


I'm trying to connect HAL service to the user app via AIDL. I've tried connecting app to service without luck. How to properly connect user app to HAL C++ service via AIDL? What am I doing wrong?

For simplicity, SELinux is set to permissive.

Interface

The AIDL interface is very basic

package org.mwe.service;

interface IDummy {
    String simple();

    String complex(in byte[] from, out byte[] to, in String original, in int numeric);
}

Service

I've added (for the most part by copying the Vibrator AIDL) interface and the default implementation. The full code of the service is available here. The service rc is

service mwe_dummy.service /vendor/bin/org.mwe.service.dummy-default myprefix
    class hal
    user root
    group root
    interface aidl org.mwe.service.IDummy/default

I can validate that the service is running by executing command

> adb shell service check org.mwe.service.IDummy/default
Service org.mwe.service.IDummy/default: found

I cannot execute methods directly, though

> adb shell service call org.mwe.service.IDummy/default 1
Result: Parcel(NULL)

App

I've found two AIDL connection approaches and implemented both:

  1. Via intent
  2. Direct connection

The full code of the kotlin application is available here.

Connection via intent

Somewhat simplified code of the connection via intent is as follows (almost exact copy of the example)

class MainActivity : AppCompatActivity() {
    private var service: IDummy? = null
    private var isServiceBound = false
    private val connection =
            object : ServiceConnection {
                override fun onServiceConnected(className: ComponentName, service: IBinder) {
                    service = IDummy.Stub.asInterface(service)
                    isServiceBound = true
                }
                override fun onServiceDisconnected(className: ComponentName) {
                    service = null
                    isServiceBound = false
                }
            }
    private fun initConnectionIntent() {
        if (isServiceBound) return
        Intent(IDummy::class.java.name).apply { setPackage(getPackageName()) }.let {
            bindService(it, connection, Context.BIND_AUTO_CREATE)
            Log.d(TAG, "binding")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initConnectionIntent()
    }
}

With Intent constructed in various ways

Intent(IDummy::class.java.name).apply { setPackage(getPackageName()) }
// or
Intent(IDummy::class.java.name).apply {
    setPackage(getPackageName())
    setAction("org.mwe.service")
// or
Intent().apply {
    component = ComponentName("org.mwe.service.IDummy", "default")
}

the log shows the message

ActivityManager: Unable to start service Intent { ... } U=0: not found
Direct connection

Somewhat simplified code of the direct connection is as follows

class MainActivity : AppCompatActivity() {
    private var service: IDummy? = null
    private fun initConnectionDirect() {
        val checkService =
                Class.forName("android.os.ServiceManager")
                        .getMethod("checkService", String::class.java)
        val bdr = checkService.invoke(null, "org.mwe.service.IDummy/default")!! as IBinder
        service = IDummy.Stub.asInterface(bdr)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initConnectionDirect()
    }
}

The example code uses ServiceManager, which is marked hidden in android, therefore, this trick is used to access it.

This code is able to find my service but it throws the following error when trying to call a method

I org.mwe.app: type=1400 audit(0.0:579): avc: denied { call } for scontext=u:r:untrusted_app:s0:c117,c256,c512,c768 tcontext=u:r:mwe_dummy_default:s0 tclass=binder permissive=1 app=org.mwe.app
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: org.mwe.app, PID: 3505
E AndroidRuntime: java.lang.IllegalArgumentException
E AndroidRuntime:   at android.os.BinderProxy.transactNative(Native Method)
E AndroidRuntime:   at android.os.BinderProxy.transact(BinderProxy.java:573)
E AndroidRuntime:   at org.mwe.service.IDummy$Stub$Proxy.simple(IDummy.java:123)
E AndroidRuntime:   at org.mwe.app.MainKt$AppHost$1$1$1.invoke(Main.kt:53)
E AndroidRuntime:   at org.mwe.app.MainKt$AppHost$1$1$1.invoke(Main.kt:50)
E AndroidRuntime:   at androidx.compose.foundation.ClickablePointerInputNode$pointerInput$3.invoke-k-4lQ0M(Clickable.kt:987)
E AndroidRuntime:   at androidx.compose.foundation.ClickablePointerInputNode$pointerInput$3.invoke(Clickable.kt:981)
E AndroidRuntime:   at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1.invokeSuspend(TapGestureDetector.kt:255)
E AndroidRuntime:   at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
E AndroidRuntime:   at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:179)
E AndroidRuntime:   at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168)
E AndroidRuntime:   at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474)
E AndroidRuntime:   at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508)
E AndroidRuntime:   at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497)
E AndroidRuntime:   at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:368)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNodeImpl$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:665)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNodeImpl.dispatchPointerEvent(SuspendingPointerInputFilter.kt:544)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.SuspendingPointerInputModifierNodeImpl.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:566)
E AndroidRuntime:   at androidx.compose.foundation.AbstractClickablePointerInputNode.onPointerEvent-H0pRuoY(Clickable.kt:947)
E AndroidRuntime:   at androidx.compose.foundation.AbstractClickableNode.onPointerEvent-H0pRuoY(Clickable.kt:795)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:317)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:303)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:303)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:185)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:104)
E AndroidRuntime:   at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:113)
E AndroidRuntime:   at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1577)
E AndroidRuntime:   at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1528)
E AndroidRuntime:   at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1467)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3175)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2827)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3175)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2827)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3175)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2827)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3175)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2827)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3175)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2827)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3175)
E AndroidRuntime:   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2827)
E AndroidRuntime:   at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:502)
E AndroidRuntime:   at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1890)
E AndroidRuntime:   at android.app.Activity.dispatchTouchEvent(Activity.java:4199)
E AndroidRuntime:   at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:70)
E AndroidRuntime:   at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:460)
E AndroidRuntime:   at android.view.View.dispatchPointerEvent(View.java:14838)
E AndroidRuntime:   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6430)
E AndroidRuntime:   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6231)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5704)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5762)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5728)
E AndroidRuntime:   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5893)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5736)
E AndroidRuntime:   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5950)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5709)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5762)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5728)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5736)
E AndroidRuntime:   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5709)
E AndroidRuntime:   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:8661)
E AndroidRuntime:   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:8612)
E AndroidRuntime:   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:8578)
E AndroidRuntime:   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:8792)
E AndroidRuntime:   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:259)
E AndroidRuntime:   at android.os.MessageQueue.nativePollOnce(Native Method)
E AndroidRuntime:   at android.os.MessageQueue.next(MessageQueue.java:335)
E AndroidRuntime:   at android.os.Looper.loopOnce(Looper.java:161)
E AndroidRuntime:   at android.os.Looper.loop(Looper.java:288)
E AndroidRuntime:   at android.app.ActivityThread.main(ActivityThread.java:7941)
E AndroidRuntime:   at java.lang.reflect.Method.invoke(Native Method)
E AndroidRuntime:   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:553)
E AndroidRuntime:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
E AndroidRuntime:   Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@fb28f23, androidx.compose.runtime.BroadcastFrameClock@eb5cc20, StandaloneCoroutine{Cancelling}@859d9d9, AndroidUiDispatcher@5780f9e]

Solution

  • The unintuitive solution to the problem is to mark AIDL interface with @VintfStability tag in service (and .bp file) and not to do this in application. The gitlab project is updated to the working state and may be used as a reference.