Mengimplementasikan logika bisnis

Ikuti langkah-langkah berikut untuk menerapkan logika bisnis Anda:

  1. Pastikan direktori katalog VSIDL Anda memiliki file build, protobuf, dan VSIDL yang diperlukan. Untuk memulai dari contoh yang ada, Anda dapat menemukan contoh folder katalog yang valid di /system/software_defined_vehicle/samples/vsidl/stable/catalog.

    Contoh struktur katalog:

    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. Tulis kode kustom untuk menerapkan logika bisnis Anda.

    vsidlc menghasilkan binding middleware dan scaffolding paket layanan (lib.rs, service_bundle.rs, dan main.rs) untuk Rust. Jika Anda menulis paket layanan di C++, implementasikan antarmuka siklus proses (libsdv_lifecycle_client_cpp) dan komunikasi (libsdv_comms_cpp) secara langsung:

    • Server RPC (Rust): Mengimplementasikan trait yang dihasilkan dari lib.rs.
    • Klien publikasi, langganan, dan RPC (Rust): Panggil fungsi yang dihasilkan dalam service_bundle.rs untuk berinteraksi dengan layanan lain.
    • Siklus proses dan komunikasi (C++): Buat subclass ServiceBundle dan inisialisasi penayang, pelanggan, atau endpoint RPC Anda di onStart.

    Karat

    1. Buat implementasi skeleton dengan menjalankan:

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

      Menjalankan vsidlc dengan flag --services akan menghasilkan implementasi Rust boilerplate untuk setiap paket layanan. Tindakan ini akan menyediakan kode sumber (main.rs) dan file konfigurasi build (Android.bp) dengan semua dependensi, sehingga paket Anda dapat langsung bertukar pesan default sementara Anda berfokus pada logika bisnis.

    2. Untuk setiap paket layanan, temukan implementasi Rust di:

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

    3. Secara default, implementasi yang dihasilkan membuat pesan dengan nilai default dan mengirimkannya antara penayang dan pelanggan atau klien dan server RPC. Untuk mengubah perilaku ini, temukan komentar TODO di main.rs dan sesuaikan dengan preferensi Anda. Contoh:

      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. Untuk membantu memastikan bahwa perubahan Anda pada file yang dihasilkan tidak ditimpa saat vsidlc dijalankan berikutnya, pindahkan folder services keluar dari folder /path/to/output.

    C++

    1. Buat file header baru, src/lib.hpp, dengan class untuk paket layanan Anda:

      #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. Buat file sumber baru, src/lib.cpp, dengan implementasi class:

      #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. Buat target Soong library paket layanan dalam file Android.bp baru atau yang sudah ada:

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

Langkah berikutnya

Untuk men-deploy paket layanan, lihat Membangun dan men-deploy paket layanan.