कारोबारी नियम लागू करना

अपने कारोबारी नियम लागू करने के लिए, यह तरीका अपनाएं:

  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, Rust के लिए मिडलवेयर बाइंडिंग और सर्विस बंडल स्कैफ़ोल्डिंग (lib.rs, service_bundle.rs, और main.rs) जनरेट करता है. अगर C++ में सर्विस बंडल लिखा जाता है, तो लाइफ़साइकल (libsdv_lifecycle_client_cpp) और कम्यूनिकेशन (libsdv_comms_cpp) इंटरफ़ेस को सीधे तौर पर लागू करें:

    • आरपीसी सर्वर (Rust): lib.rs से जनरेट किए गए ट्रेट लागू करें.
    • पब्लिश, सब्सक्राइब, और आरपीसी क्लाइंट (Rust): अन्य सेवाओं के साथ इंटरैक्ट करने के लिए, service_bundle.rs में जनरेट किए गए फ़ंक्शन को कॉल करें.
    • लाइफ़साइकल और कम्यूनिकेशन (C++): ServiceBundle को सबक्लास करें और onStart में अपने पब्लिशर, सब्सक्राइबर या आरपीसी एंडपॉइंट को शुरू करें.

    ज़ंग

    1. स्केलेटन लागू करने के लिए, यह कोड चलाएं:

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

      --services फ़्लैग के साथ vsidlc को चलाने पर, हर सर्विस बंडल के लिए बॉयलरप्लेट रस्ट इंप्लिमेंटेशन जनरेट होता है. इससे सोर्स कोड (main.rs) और बिल्ड कॉन्फ़िगरेशन फ़ाइल (Android.bp) मिलती है. इसमें सभी डिपेंडेंसी शामिल होती हैं. इससे आपका बंडल तुरंत डिफ़ॉल्ट मैसेज भेज सकता है. इस दौरान, आपके पास अपने कारोबारी नियम पर फ़ोकस करने का विकल्प होता है.

    2. हर सर्विस बंडल के लिए, Rust इंप्लिमेंटेशन यहां ढूंढें:

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

    3. डिफ़ॉल्ट रूप से, जनरेट किया गया लागू करने का तरीका, डिफ़ॉल्ट वैल्यू वाले मैसेज बनाता है. साथ ही, उन्हें पब्लिशर और सदस्यों या आरपीसी क्लाइंट और सर्वर के बीच भेजता है. इस व्यवहार में बदलाव करने के लिए, main.rs में TODO टिप्पणियां ढूंढें और उन्हें अपनी पसंद के मुताबिक अडजस्ट करें. उदाहरण के लिए:

      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. किसी नई या मौजूदा Android.bp फ़ाइल में, सर्विस बंडल लाइब्रेरी का Soong टारगेट बनाएं:

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

अगला कदम क्या है

अपने सर्विस बंडल डिप्लॉय करने के लिए, सर्विस बंडल बनाना और डिप्लॉय करना लेख पढ़ें.