サービスの実装

これで AIDL サービスを実装できます。

birthday_service/src/lib.rs:

// Copyright 2024 Google LLC
// SPDX-License-Identifier: Apache-2.0

//! `IBirthdayService` AIDL インターフェースの実装。
use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService;
use com_example_birthdayservice::binder;

/// The `IBirthdayService` implementation.
pub struct BirthdayService;

impl binder::Interface for BirthdayService {}

impl IBirthdayService for BirthdayService {
    fn wishHappyBirthday(&self, name: &str, years: i32) -> binder::Result<String> {
        Ok(format!("Happy Birthday {name}, congratulations with the {years} years!"))
    }
}

birthday_service/Android.bp:

rust_library {
    name: "libbirthdayservice",
    crate_name: "birthdayservice",
    srcs: ["src/lib.rs"],
    rustlibs: [
        "com.example.birthdayservice-rust",
    ],
}
  • 生成された IBirthdayService トレイトへのパスを示し、各セグメントがそれぞれ必要である理由を説明してください。
  • wishHappyBirthday とその他の AIDL IPC メソッドは、&mut self ではなく &self を受け取る点に注意してください。
    • これは、Binder が受信したリクエストにスレッドプール上で応答し、複数のリクエストを並列に処理できるようにするために必要です。これにより、サービスメソッドは self への共有参照しか受け取れません。
    • サービスによって変更する必要がある状態は、安全に変更できるように Mutex のようなものに入れる必要があります。
    • サービス状態を管理する適切な方法は、サービスの詳細に大きく依存します。
  • TODO: binder::Interface トレイトは何をするのでしょうか。オーバーライドするメソッドはありますか。ソースはどこにありますか。