快速入門:建立及執行 SDV 服務套裝組合

本頁面說明如何建立 SDV 服務套件、將其封裝為 APEX 檔案格式,並在虛擬 Cuttlefish 裝置上執行。SDV 系統上程式碼執行的主要單位是服務套件。服務套件提供生命週期方法,系統會在系統狀態變更時執行這些方法。

  1. 設定建構環境

    在工作目錄中,找出 envsetup.sh 指令碼,設定建構環境、將 Cuttlefish 設為建構目標,並建構 vsidlc 程式碼生成器:

    source build/envsetup.sh
    lunch sdv_core_cf-trunk_staging-userdebug
    m vsidlc
    
  2. 建立名為 my_catalog 的資料夾,並加入下列檔案,定義架構:

    • tire.proto:定義您傳送的資料結構:

      syntax = "proto3";
      
      package com.android.sdv.sample.quickstart;
      
      import "google/protobuf/empty.proto";
      import "sdv/vsidl/v1/annotations.proto";
      
      message TirePressure {
        uint32 pressure = 1;
      }
      
      message TireInfoResponse {
        string serial_number = 1;
        uint32 max_pressure = 2;
      }
      
      service TireService {
        rpc GetTireInfo(google.protobuf.Empty) returns (TireInfoResponse);
      }
      
    • architecture.vsidl:定義服務套件及其通訊方式:

      package: "com.android.sdv.sample.quickstart"
      
      service_bundle {
        name: "Manager"
      
        publisher {
          message: "TirePressure"
          topic: "pressure"
          capacity: 8
        }
      
        server {
          service: "TireService"
          channel: "tire-service"
        }
      }
      
      service_bundle {
        name: "Monitor"
      
        subscriber {
          message: "TirePressure"
          topic: "pressure"
        }
      
        client {
          service: "TireService"
          channel: "tire-service"
        }
      }
      
    • Android.bp:定義建構程序:

      rust_protobuf {
          name: "libquickstart_sample_tire_proto",
          crate_name: "quickstart_sample_tire_proto",
          protos: [
              "tire.proto",
          ],
          source_stem: "quickstart_sample_tire_proto_source",
          rustlibs: [
              "libvsidl_v1_stdlib_proto_rs",
          ],
          proto_flags: [
              "-I external/protobuf/src",
          ],
          vendor_available: true,
          product_available: true,
          apex_available: [
              "//apex_available:platform",
              "//apex_available:anyapex",
          ],
          min_sdk_version: "35",
      }
      
      filegroup {
          name: "vsidl_quickstart_catalog",
          srcs: ["**/*"],
      }
      
  3. 執行下列指令,產生骨架實作和設定檔:

    vsidlc -c /path/to/catalog -o /path/to/output --services --apex
    
  4. 針對每個服務套件,在 /path/to/output/services/ServiceBundleName/src/main.rs 中找出 Rust 實作項目。

  5. 根據預設,產生的實作會建立含有預設值的訊息,並在發布端和訂閱端或 RPC 用戶端和伺服器之間傳送。如要修改這項行為,請在 main.rs 中尋找 TODO 註解,並根據偏好調整。例如:

      async fn handle_tire_pressure_front_left_publisher(publisher: sdv::mw::Publisher<TirePressure>) {
          loop {
              // TODO: Modify the frequency of publishing messages here.
              sleep(Duration::from_secs(1)).await;
              // TODO: Modify the message content here.
              let message = TirePressure::default();
              info!("Publishing on TirePressure#FRONT_LEFT");
              publisher.publish(&message).unwrap();
          }
      }
    
  6. 如要在系統映像檔中加入套件組合,請將產生的 APEX 模組名稱新增至產品 Makefile。

    1. 開啟產品的 Makefile (例如 /device/google/sdv/sdv_core_base/sdv_samples_core_services.mk)。

    2. 將模組新增至 PRODUCT_PACKAGES,方法是加入:

      PRODUCT_PACKAGES += com.android.sdv.sample.quickstart
      PRODUCT_PACKAGES += com_android_sdv_sample_quickstart_orchestration_configurations
      
  7. 建構映像檔:

    m
    
  8. 執行並驗證服務套件執行作業:

    1. 啟動 Cuttlefish 虛擬裝置:

      sdv-cf create --instance_name=instance1
      
    2. 在系統啟動時執行服務套件:

      手動

      adb wait-for-device
      adb root
      adb shell sdv_service_bundle start local-vm:com.android.sdv.sample.quickstart.Manager/instance
      adb shell sdv_service_bundle start local-vm:com.android.sdv.sample.quickstart.Monitor/instance
      

      自動調度管理工具

      adb wait-for-device
      adb root
      # Apply global orchestration configuration by setting system property
      adb shell setprop persist.sdv.orchestrator_config_path "etc/orch/vm_quickstart_orch_config.textproto"
      adb reboot
      
    3. 確認服務套件的執行情況:

      adb logcat *:F com_android_sdv_sample_quickstart_Manager_instance:* com_android_sdv_sample_quickstart_Monitor_instance:*
      

      這項指令會顯示記錄,指出服務套件正在交換訊息:

      03-30 13:41:31.505   967   976 I com_android_sdv_sample_quickstart_Manager_instance: sdv_lm_manager: Publishing on TirePressure#PRESSURE
      03-30 13:41:31.505   983   991 I com_android_sdv_sample_quickstart_Monitor_instance: sdv_lm_monitor: Received message on TirePressure#PRESSURE: [TirePressure { pressure: 0, special_fields: SpecialFields { unknown_fields: UnknownFields { fields: None }, cached_size: CachedSize { size: 0 } } }]
      03-30 13:41:31.626   983   991 I com_android_sdv_sample_quickstart_Monitor_instance: sdv_lm_monitor: Sending request on Monitor/TireService
      03-30 13:41:31.627   967   976 I com_android_sdv_sample_quickstart_Manager_instance: sdv_lm_manager: Received request on Manager/TireService: Empty { special_fields: SpecialFields { unknown_fields: UnknownFields { fields: None }, cached_size: CachedSize { size: 0 } } }
      03-30 13:41:31.627   983   991 I com_android_sdv_sample_quickstart_Monitor_instance: sdv_lm_monitor: Received response on Monitor/TireService: TireInfoResponse { serial_number: "", max_pressure: 0, special_fields: SpecialFields { unknown_fields: UnknownFields { fields: None }, cached_size: CachedSize { size: 0 } } }
      

後續步驟

進一步瞭解車輛服務介面定義語言 (VSIDL),以及如何從頭定義自己的服務套裝組合。