Bắt đầu nhanh: Tạo và thực thi các gói dịch vụ SDV

Trang này trình bày cách tạo gói dịch vụ SDV, đóng gói gói dịch vụ đó vào định dạng tệp APEX và thực thi trên thiết bị Cuttlefish ảo. Các đơn vị chính để thực thi mã trên hệ thống SDV là các gói dịch vụ. Các gói dịch vụ cung cấp các phương thức vòng đời mà hệ thống thực thi khi trạng thái hệ thống thay đổi.

  1. Thiết lập môi trường tạo bản dựng

    Trong thư mục làm việc, hãy lấy nguồn tập lệnh envsetup.sh để thiết lập môi trường tạo bản dựng, đặt Cuttlefish làm mục tiêu tạo bản dựng và xây dựng công cụ tạo mã vsidlc:

    source build/envsetup.sh
    lunch sdv_core_cf-trunk_staging-userdebug
    m vsidlc
    
  2. Xác định kiến trúc bằng cách tạo một thư mục có tên my_catalog với các tệp sau:

    • tire.proto: Xác định cấu trúc dữ liệu mà bạn đang gửi:

      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: Xác định các gói dịch vụ và thông tin liên lạc của chúng:

      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: Xác định quy trình xây dựng:

      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. Tạo tệp cấu hình và tệp triển khai khung bằng cách chạy:

    vsidlc -c /path/to/catalog -o /path/to/output --services --apex
    
  4. Đối với mỗi gói dịch vụ, hãy tìm cách triển khai Rust trong /path/to/output/services/ServiceBundleName/src/main.rs.

  5. Theo mặc định, quá trình triển khai được tạo sẽ tạo các thông báo có giá trị mặc định và gửi các thông báo đó giữa nhà xuất bản và người đăng ký hoặc máy chủ và máy khách RPC. Để sửa đổi hành vi này, hãy tìm các nhận xét TODO trong main.rs và điều chỉnh chúng theo ý bạn. Ví dụ:

      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. Để đưa gói của bạn vào hình ảnh hệ thống, hãy thêm tên mô-đun APEX đã tạo vào tệp makefile sản phẩm.

    1. Mở tệp makefile sản phẩm (ví dụ: /device/google/sdv/sdv_core_base/sdv_samples_core_services.mk).

    2. Thêm mô-đun vào PRODUCT_PACKAGES bằng cách thêm:

      PRODUCT_PACKAGES += com.android.sdv.sample.quickstart
      PRODUCT_PACKAGES += com_android_sdv_sample_quickstart_orchestration_configurations
      
  7. Tạo hình ảnh:

    m
    
  8. Chạy và xác minh quá trình thực thi gói dịch vụ:

    1. Khởi động thiết bị Cuttlefish ảo:

      sdv-cf create --instance_name=instance1
      
    2. Chạy gói dịch vụ khi hệ thống khởi động:

      Thủ công

      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
      

      Người hòa âm

      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. Xác minh quá trình thực thi gói dịch vụ:

      adb logcat *:F com_android_sdv_sample_quickstart_Manager_instance:* com_android_sdv_sample_quickstart_Monitor_instance:*
      

      Lệnh này hiển thị nhật ký cho thấy các gói dịch vụ trao đổi thông báo:

      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 } } }
      

Các bước tiếp theo

Tìm hiểu thêm về Ngôn ngữ định nghĩa giao diện dịch vụ xe (VSIDL) và cách xác định các gói dịch vụ của riêng bạn từ đầu.