ysk-san KT

技術系の情報をKTするために、まずは勉強

AIDLとHIDLってなに??/What are AIDL and HIDL?

(English below)

今日のモバイルアプリ開発において、プログラマーは、様々なデバイスからの入力や出力を処理し、それらをアプリケーションと統合する必要があります。これには、Android開発においては、AIDLとHIDLのようなRPC(Remote Procedure Call)フレームワークが利用されます。この記事では、AIDLとHIDLの両方について解説し、それらの違いについても詳しく説明します。

AIDLとは

AIDL(Android Interface Definition Language)は、Android開発におけるRPCの実装の1つです。AIDLは、クライアントとサーバーの間でオブジェクトを受け渡すための簡単な方法を提供します。AIDLは、主にAndroidのサービスとして使用され、バインドされたサービスとしてアプリケーションと通信するために使用されます。

AIDLを使用するためには、まずインターフェース定義ファイルを作成する必要があります。インターフェース定義ファイルは、サーバー側で実装されるメソッドやクライアント側で使用されるメソッドなどを含むインターフェースを定義するために使用されます。この定義ファイルを使用することで、サーバーとクライアントは同じメソッド名、引数、戻り値を使用して通信することができます。

以下は、サービスのインターフェース定義ファイルの例です。

interface IRemoteService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

 

AIDLは、サービスの実装、サービスのバインド、クライアント側での呼び出しに使用されます。以下は、サービスの実装の例です。

public class RemoteService extends Service {
    // Use a layout file as the view for the activity
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // AIDL implementation
    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                double aDouble, String aString) {
            // Implementation code here
        }
    };
}

 

これにより、アプリケーションは、サービスのバインドを要求し、AIDLで定義されたメソッドをサービスに送信し、戻りり値を受け取ることができます。

 

public class MainActivity extends AppCompatActivity {
    // AIDL interface
    private IRemoteService mService;

    // Connection to the service
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // Bind to the service's interface
            mService = IRemoteService.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };

    // Bind to the service
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    // Call the AIDL method
    private void callServiceMethod() {
        try {
            mService.basicTypes(1, 2L, true, 3.0f, 4.0, "hello");
        } catch (RemoteException e) {
            // Exception handling
        }
    }

    // Unbind from the service
    @Override
    protected void onStop() {
        super.onStop();
        unbindService(mConnection);
    }
}

この例では、MainActivityがAIDLを使用してRemoteServiceに接続し、basicTypesメソッドを呼び出しています。この方法で、アプリケーションはバックグラウンドで実行されるサービスとやりとりし、必要なデータを受け取ることができます。

 

HIDLとは

HIDL(HAL Interface Definition Language)は、AndroidのHAL(Hardware Abstraction Layer)に使用されるRPCフレームワークです。HIDLは、AndroidのHALのような低レベルのシステムコンポーネントに対して設計されており、AIDLとは異なり、プラットフォーム上で低レベルのデバイスのアクセスを可能にします。

HIDLは、AIDLに似ていますが、より高度な型のサポートや、非同期呼び出し、スレッドプール、エラー処理などの追加機能を提供します。HIDLは、特定のデバイスタイプに対するインターフェースを定義するために使用され、HALとアプリケーション間の通信に使用されます。

以下は、HALインターフェースの定義ファイルの例です。

 
interface ICamera {
    bool init();
    void setResolution(in int width, in int height);
    void capture(inout CameraData data);
}

この例では、ICameraインターフェースが定義されており、init、setResolution、captureの3つのメソッドが定義されています。これらのメソッドは、カメラデバイスにアクセスするために使用されます。HIDLを使用することで、HALとアプリケーションの間で高速かつ安全な通信を確立することができます

 

AIDLとHIDLの違い

AIDLとHIDLは、両方ともAndroidでRPC通信を行うためのフレームワークですが、いくつかの違いがあります。

まず、AIDLは主にAndroidのアプリケーション間通信(IPC)に使用されます。一方、HIDLはHALとアプリケーション間通信に使用されます。つまり、AIDLはアプリケーションレベルのコンポーネント間の通信に使用され、HIDLはシステムレベルのコンポーネントとの通信に使用されます。

次に、AIDLはJavaベースであり、Javaインターフェースを定義するために使用されます。一方、HIDLはC++ベースであり、HALインターフェースを定義するために使用されます。つまり、AIDLは主にJavaアプリケーションで使用され、HIDLは主にC++ HALで使用されます。

さらに、HIDLはより高度な型のサポートや非同期呼び出し、スレッドプール、エラー処理など、AIDLにはない機能を提供します。これらの機能により、HIDLはより高速で効率的な通信を実現し、HALとアプリケーションの間での通信をより簡単にします。

最後に、AIDLは主に単方向呼び出しをサポートしており、同期的に通信を行います。一方、HIDLは双方向呼び出しをサポートしており、非同期的に通信を行うことができます。つまり、AIDLは一方向にデータを送信するために使用され、HIDLは双方向通信を行うために使用されます。

 

まとめ

AIDLとHIDLは、AndroidでRPC通信を行うためのフレームワークです。AIDLは主にAndroidのアプリケーション間通信(IPC)に使用され、一方、HIDLはHALとアプリケーション間通信に使用されます。AIDLはJavaベースであり、Javaインターフェースを定義するために使用され、一方、HIDLはC++ベースであり、HALインターフェースを定義するために使用されます。さらに、HIDLはより高度な型のサポートや非同期呼び出し、スレッドプール、エラー処理など、AIDLにはない機能を提供します。

またGoogleは近年HIDLよりもAIDLの利用を推奨しています。AIDL、HIDLどちらでも目的を達成できる場合はAIDLを選んだ方がベターでしょう。

 

■English translation

 

In today's mobile app development, programmers need to process inputs and outputs from various devices and integrate them with the application. For this, RPC (Remote Procedure Call) frameworks such as AIDL and HIDL are used in Android development. This article describes both AIDL and HIDL and details the differences between them.

What is AIDL?

AIDL (Android Interface Definition Language) is one of the RPC implementations in Android development. service and is used to communicate with applications as a bound service.

To use AIDL, an interface definition file must first be created. The interface definition file is used to define the interface, including methods to be implemented on the server side and methods to be used on the client side. This definition file allows the server and client to communicate using the same method names, arguments, and return values.

The following is an example of an interface definition file for a service

interface IRemoteService {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

 

AIDL is used to implement services, bind services, and invoke them on the client side. The following is an example of a service implementation

public class RemoteService extends Service {
    // Use a layout file as the view for the activity
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // AIDL implementation
    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                double aDouble, String aString) {
            // Implementation code here
        }
    };
}

 

This allows the application to request a service bind, send the method defined in the AIDL to the service, and receive the return value.

 

public class MainActivity extends AppCompatActivity {
    // AIDL interface
    private IRemoteService mService;

    // Connection to the service
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            // Bind to the service's interface
            mService = IRemoteService.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };

    // Bind to the service
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, RemoteService.class);
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    // Call the AIDL method
    private void callServiceMethod() {
        try {
            mService.basicTypes(1, 2L, true, 3.0f, 4.0, "hello");
        } catch (RemoteException e) {
            // Exception handling
        }
    }

    // Unbind from the service
    @Override
    protected void onStop() {
        super.onStop();
        unbindService(mConnection);
    }
}

In this example, MainActivity connects to RemoteService using AIDL and calls the basicTypes method. In this way, the application can interact with the service running in the background and receive the necessary data.

 

What is HIDL?

HIDL (HAL Interface Definition Language) is an RPC framework used for Android's Hardware Abstraction Layer (HAL). HIDL is designed for low-level system components such as Android's HAL and, unlike AIDL, enables low-level device access on the platform.

HIDL is similar to AIDL, but provides additional features such as more advanced type support, asynchronous calls, thread pooling, error handling, etc. HIDL is used to define interfaces to specific device types and is used for communication between the HAL and the application HIDL is used to define the interface to a specific device type and is used for communication between HAL and the application.

Below is an example of a HAL interface definition file.

 
interface ICamera {
    bool init();
    void setResolution(in int width, in int height);
    void capture(inout CameraData data);
}

In this example, the ICamera interface is defined and three methods are defined: init, setResolution, and capture. These methods are used to access the camera device; by using HIDL, a fast and secure communication can be established between the HAL and the application

Differences between AIDL and HIDL

AIDL and HIDL are both frameworks for RPC communication in Android, but there are several differences.

First, AIDL is primarily used for application-to-application communication (IPC) in Android. HIDL, on the other hand, is used for HAL and application-to-application communication. In other words, AIDL is used for communication between application-level components, while HIDL is used for communication with system-level components.

Second, AIDL is Java-based and is used to define Java interfaces. HIDL, on the other hand, is C++-based and is used to define HAL interfaces. In other words, AIDL is primarily used in Java applications, while HIDL is primarily used in C++ HAL.

In addition, HIDL offers features not found in AIDL, such as more advanced type support, asynchronous calls, thread pooling, and error handling. These features make HIDL faster and more efficient and make communication between the HAL and the application easier.

Finally, AIDL primarily supports unidirectional calls and communicates synchronously. HIDL, on the other hand, supports bidirectional calls and can communicate asynchronously. In other words, AIDL is used to send data in one direction, while HIDL is used for bidirectional communication.

 

Summary.

AIDL and HIDL are frameworks for RPC communication in Android. AIDL is Java-based and is used to define the Java interface, while HIDL is C++-based and is used to define the HAL interface. In addition, HIDL offers features not found in AIDL, such as more advanced type support, asynchronous calls, thread pooling, and error handling.

Google has also recommended the use of AIDL over HIDL in recent years, and if either AIDL or HIDL can achieve your goals, you are better off choosing AIDL.