پیاده‌سازی منطق کسب‌وکار

برای پیاده‌سازی منطق کسب‌وکارتان، این مراحل را دنبال کنید:

  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
    
    برای جلوگیری از ابهام، تأیید کنید که هر فایل .proto فقط به یک هدف کتابخانه در کاتالوگ تعلق دارد.
  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) را با همه وابستگی‌ها ارائه می‌دهد تا دسته شما بتواند فوراً پیام‌های پیش‌فرض را مبادله کند و شما بتوانید روی منطق کسب‌وکارتان تمرکز کنید.

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

قدم بعدی چیست

برای استقرار دسته‌های خدمات، به ساختن و استقرار دسته‌های خدمات مراجعه کنید.