برای پیادهسازی منطق کسبوکارتان، این مراحل را دنبال کنید:
بررسی کنید که فهرست راهنمای کاتالوگ 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فقط به یک هدف کتابخانه در کاتالوگ تعلق دارد.کد سفارشی بنویسید تا منطق کسبوکارتان را پیادهسازی کنید.
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مقداردهی اولیه کنید.
زنگار
با اجرای این فرمان، یک پیادهسازی اسکلتی ایجاد کنید:
vsidlc -c /path/to/catalog -o /path/to/output --servicesاجرای
vsidlcبا پرچم--servicesیک پیادهسازی Rust قالبی برای هر بسته سرویس تولید میکند. این کد منبع (main.rs) و فایل پیکربندی ساخت (Android.bp) را با همه وابستگیها ارائه میدهد تا دسته شما بتواند فوراً پیامهای پیشفرض را مبادله کند و شما بتوانید روی منطق کسبوکارتان تمرکز کنید.برای هر بسته سرویس، پیادهسازی Rust را در اینجا پیدا کنید:
/path/to/output/services/ServiceBundleName/src/main.rs.بهطور پیشفرض، پیادهسازی تولیدشده پیامهایی با مقادیر پیشفرض ایجاد میکند و آنها را بین ناشران و مشترکان یا کلاینتها و سرورهای 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(); } }برای اطمینان از اینکه تغییرات شما در فایلهای تولیدشده دفعه بعد که
vsidlcاجرا میشود رونویسی نمیشود، پوشهservicesرا از پوشه/path/to/outputخارج کنید.
C++
فایل سرایند جدیدی،
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فایل منبع جدیدی،
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یک هدف 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, }
- سرورهای RPC (زبان Rust): ویژگیهای تولیدشده از
قدم بعدی چیست
برای استقرار دستههای خدمات، به ساختن و استقرار دستههای خدمات مراجعه کنید.