מוודאים שספריית הקטלוג VSIDL מכילה את קובצי ה-build, 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כתיבת קוד בהתאמה אישית להטמעה של הלוגיקה העסקית:
- שרתי RPC: מטמיעים את ה-traits מ-
lib.rs. לקוחות RPC של פרסום והרשמה: קוראים לפונקציות שנוצרו בתוך
service_bundle.rsכדי ליצור אינטראקציה עם שירותים אחרים.
חלודה
כדי ליצור הטמעה בסיסית, מריצים את הפקודה:
vsidlc -c /path/to/catalog -o /path/to/output --servicesהפעלת
vsidlcעם הדגל--servicesיוצרת הטמעה של Rust לכל חבילת שירות. התבנית מספקת את המסגרת הדרושה – כולל קוד המקור (main.rs) וקובץ הגדרות build (Android.bp) עם כל התלויות – ומאפשרת החלפה מיידית של הודעות ברירת המחדל בזמן שאתם מתמקדים בהטמעה של הלוגיקה העסקית העיקרית.לכל חבילת שירות, אפשר למצוא את ההטמעה של Rust במיקום הבא:
/path/to/output/services/ServiceBundleName/src/main.rs.כברירת מחדל, ההטמעה שנוצרת יוצרת הודעות עם ערכי ברירת מחדל ושולחת אותן בין בעלי אתרים למנויים או בין לקוחות ושרתים של RPC. כדי לשנות את ההתנהגות הזו, צריך למצוא את
TODOהתגובות ב-main.rsולשנות אותן לפי ההעדפות שלכם. לדוגמה: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() { // Dynamic resources(sockets, files, etc) should be allocated 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 phase requires the service bundle to delete the dynamic resources // (sockets, files, etc) that were previously 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: מטמיעים את ה-traits מ-
המאמרים הבאים
במאמר יצירה ופריסה של חבילות שירותים מוסבר איך פורסים חבילות שירותים.