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

Trang này hướng dẫn bạn cách tạo một gói dịch vụ SDV, đóng gói gói đó vào định dạng tệp APEX và thực thi gói đó trên một thiết bị Cuttlefish ảo. Đơn vị thực thi mã chính trên hệ thống SDV là các gói dịch vụ. 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 đang hoạt động, hãy lấy 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 bản dựng và tạo trình tạo mã vsidlc:

    source build/envsetup.sh
    lunch sdv_core_cf-aosp_current-userdebug
    m vsidlc
    
  2. Xác định cấu trúc của bạn 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 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à hoạt động giao tiếp của các gói đó (để biết thông tin về tính năng làm nổi bật cú pháp, xác thực và tự động hoàn thành trong Android Studio cho Nền tảng, hãy xem phần Chỉnh sửa tệp 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: 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à 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, phương thức triển khai được tạo sẽ tạo thông báo có giá trị mặc định và gửi thông báo giữa nhà xuất bản và người đăng ký hoặc ứng dụng và máy 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 theo ý muốn của bạn. Ví dụ:

    rust async fn handle_tire_pressure_pressure_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#PRESSURE"); 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 của sản phẩm.

    1. Mở tệp makefile của 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
    

    Sau khi hình ảnh hệ thống bao gồm APEX (hoặc khoá dùng để ký), bạn cũng có thể tạo và triển khai các thay đổi lặp lại ngay từ biểu tượng lề apex/Android.bp trong Android Studio cho Nền tảng. Để biết thêm thông tin, hãy xem bài viết Triển khai gói dịch vụ SDV.

  8. Chạy và xác minh việc 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
        
      • Sử dụng trình điều phối:

        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 việc 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 gói dịch vụ của riêng bạn từ đầu.