ビジネス ロジックの実装

  1. VSIDL カタログ ディレクトリに、必要なビルド、protobuf、VSIDL ファイルがあることを確認します。既存のサンプルから始めるには、/system/software_defined_vehicle/samples/vsidl/stable/catalog で有効なカタログ フォルダの例を確認してください。

    カタログ構造の例:

    my_catalog/
    ├── Android.bp          # Defines rust_protobuf modules for .proto files
    ├── types.proto         # Protobuf message / RPC service definitions
    └── architecture.vsidl  # VSIDL service bundle definitions
    
  2. カスタムコードを記述してビジネス ロジックを実装する。

    vsidlc は、Rust のミドルウェア バインディングとサービス バンドルのスキャフォールディング(lib.rs、service_bundle.rs、main.rs)を生成します。C++ でサービス バンドルを作成する場合は、ライフサイクル(libsdv_lifecycle_client_cpp)インターフェースと通信(libsdv_comms_cpp)インターフェースを直接実装します。

    • RPC サーバー(Rust): lib.rs から生成されたトレイトを実装します。
    • パブリッシュ、サブスクライブ、RPC クライアント(Rust): service_bundle.rs 内で生成された関数を呼び出して、他のサービスとやり取りします。
    • ライフサイクルと通信(C++): ServiceBundle をサブクラス化し、onStart でパブリッシャー、サブスクライバー、RPC エンドポイントを初期化します。

    Rust

    1. 次のコマンドを実行して、スケルトン実装を生成します。

      vsidlc -c /path/to/catalog -o /path/to/output --services
      

      --services フラグを指定して vsidlc を実行すると、サービス バンドルごとにボイラープレート Rust 実装が生成されます。これにより、ソースコード(main.rs)と、すべての依存関係を含むビルド構成ファイル(Android.bp)が提供されるため、ビジネス ロジックに集中しながら、バンドルでデフォルトのメッセージをすぐに交換できます。

    2. 各サービス バンドルの Rust 実装は、次の場所にあります。

      /path/to/output/services/ServiceBundleName/src/main.rs。

    3. デフォルトでは、生成された実装はデフォルト値でメッセージを作成し、パブリッシャーとサブスクライバー、または RPC クライアントとサーバー間で送信します。この動作を変更するには、main.rs で TODO コメントを見つけて、好みに合わせて調整します。次に例を示します。

      rust async fn handle_tire_pressure_range_unique_publisher( publisher: sdv::mw::Publisher<TirePressureRange>, ) { loop { // TODO: Modify the frequency of publishing messages here. sleep(Duration::from_secs(1)).await; // TODO: Modify the message content here. let message = TirePressureRange::default(); info!("Publishing on TirePressureRange#UNIQUE"); publisher.publish(&message).unwrap(); } }

    4. 生成されたファイルに加えた変更が、次回 vsidlc の実行時に上書きされないようにするには、services フォルダを /path/to/output フォルダの外に移動します。

    C++

    1. サービス バンドルのクラスを含む新しいヘッダー ファイル src/lib.hpp を作成します。

      #pragma once
      #include <sdv/service_bundle.h>
      #include <sdv/context.hpp>
      
      namespace com::sdv::oem::service_bundle {
      
      // Sample implementation of the service bundle interface.
      class ServiceBundleName : public android::sdv::service_bundle::ServiceBundle {
      public:
          ServiceBundleName(sdv_comms::ctx::Context context);
          ~ServiceBundleName();
          void onStart() override;
          void onStop() override;
      };
      }  // namespace com::sdv::oem::service_bundle
      
    2. クラスの実装を含む新しいソースファイル src/lib.cpp を作成します。

      #include "src/lib.hpp"
      #include <sdv/sb_macro.h>
      #include <iostream>
      using com::sdv::oem::service_bundle::ServiceBundleName;
      
      // Register the new service bundle.
      REGISTER_SERVICE_BUNDLE(ServiceBundleName);
      
      // Sample implementation of the service bundle interface.
      namespace com::sdv::oem::service_bundle {
      // Creates a new instance of the ServiceBundleName.
      // Called when service bundle is created by the system.
      // Context object is provided as a parameter that gives access to the
      // communication stack APIs.
      ServiceBundleName::ServiceBundleName([[maybe_unused]] sdv_comms::ctx::Context context) :
        ServiceBundle(context) {
          // Memory allocations and static data loading should be done as
          // part of this method.
          //
          // Loading of the dynamic resources (sockets, files, etc)
          // is strongly discouraged due to possible Suspend-to-RAM scenario.
          //
          // The dynamic data can be loaded in the onStart method.
      }
      
      // Called when the service bundle is started by the system.
      void ServiceBundleName::onStart() {
          // Allocate dynamic resources (sockets, files, publishers, and
          // subscribers such as TirePressureRange#UNIQUE) during this call.
      }
      
      // Called when the service bundle is stopped by the system in preparation
      // for shutdown or suspend to RAM/Disc.
      void ServiceBundleName::onStop() {
          // Stop active publishing loops and release the dynamic resources
          // that were allocated in the onStart method.
      }
      
      // Called when the service bundle is destroyed by the system.
      ServiceBundleName::~ServiceBundleName() {
          // Static resources deallocation needs to be implemented in the destructor.
      }
      }  // namespace com::sdv::oem::service_bundle
      
    3. 新規または既存の Android.bp ファイルに、サービス バンドル ライブラリの Soong ターゲットを作成します。

      // Service bundle library.
      cc_library_shared {
        name: "libservice_bundle",
        srcs: ["src/lib.cpp",],
        // Allows the library to be available inside APEX.
        apex_available: [
            "//apex_available:platform",
            "//apex_available:anyapex",
        ],
        shared_libs: [
          // Service Bundle lifecycle C++ API.
          "libsdv_lifecycle_client_cpp",
          // commstack library that provides context object reference
          "libsdv_comms_cpp",
        ],
      
        // Service bundle package is packed into /product apexes.
        product_specific: true,
      }
      

次のステップ

サービス バンドルをデプロイするには、サービス バンドルをビルドしてデプロイするをご覧ください。