ใช้ตรรกะทางธุรกิจ

  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 สร้างการเชื่อมโยงมิดเดิลแวร์และโครงสร้างแพ็กเกจบริการ (lib.rs, service_bundle.rs และ main.rs) สำหรับ Rust หากคุณเขียนแพ็กเกจบริการใน C++ ให้ใช้อินเทอร์เฟซวงจร (libsdv_lifecycle_client_cpp) และการสื่อสาร (libsdv_comms_cpp) โดยตรง ดังนี้

    • เซิร์ฟเวอร์ RPC (Rust): ใช้ลักษณะที่สร้างขึ้นจาก lib.rs
    • ไคลเอ็นต์การเผยแพร่ การสมัครใช้บริการ และ RPC (Rust): เรียกใช้ฟังก์ชันที่สร้างขึ้นภายใน service_bundle.rs เพื่อโต้ตอบกับบริการอื่นๆ
    • วงจรและการสื่อสาร (C++): คลาสย่อย ServiceBundle และเริ่มต้นผู้เผยแพร่ ผู้ติดตาม หรือปลายทาง RPC ใน onStart

    สนิม

    1. สร้างการติดตั้งใช้งานโครงร่างโดยเรียกใช้คำสั่งต่อไปนี้

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

      การเรียกใช้ vsidlc ด้วยแฟล็ก --services จะสร้างการใช้งาน Rust ต้นแบบสำหรับแพ็กเกจบริการแต่ละรายการ ซึ่งจะให้ซอร์สโค้ด (main.rs) และไฟล์การกำหนดค่าบิลด์ (Android.bp) พร้อมทรัพยากร Dependency ทั้งหมด เพื่อให้แพ็กเกจแลกเปลี่ยนข้อความเริ่มต้นได้ทันทีในขณะที่คุณมุ่งเน้นที่ตรรกะทางธุรกิจ

    2. สำหรับแพ็กเกจบริการแต่ละรายการ ให้ค้นหาการติดตั้งใช้งาน Rust ในตำแหน่งต่อไปนี้

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

    3. โดยค่าเริ่มต้น การติดตั้งใช้งานที่สร้างขึ้นจะสร้างข้อความที่มีค่าเริ่มต้นและส่งข้อความระหว่างผู้เผยแพร่และผู้สมัครใช้บริการ หรือไคลเอ็นต์และเซิร์ฟเวอร์ RPC หากต้องการแก้ไขลักษณะการทำงานนี้ ให้ค้นหาความคิดเห็น TODO ใน main.rs และปรับเปลี่ยนตามความต้องการของคุณ เช่น

      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. สร้างเป้าหมาย Soong ของไลบรารีแพ็กเกจบริการในไฟล์ Android.bp ใหม่หรือที่มีอยู่ ดังนี้

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

ขั้นตอนถัดไป

หากต้องการติดตั้งใช้งานแพ็กเกจบริการ โปรดดูหัวข้อสร้างและติดตั้งใช้งานแพ็กเกจบริการ