Triển khai logic kinh doanh

Hãy làm theo các bước sau để triển khai logic nghiệp vụ:

  1. Xác minh rằng thư mục danh mục VSIDL của bạn có các tệp bản dựng, protobuf và VSIDL cần thiết. Để bắt đầu từ một mẫu hiện có, bạn có thể tìm ví dụ về thư mục danh mục hợp lệ trong /system/software_defined_vehicle/samples/vsidl/stable/catalog.

    Cấu trúc danh mục mẫu:

    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. Viết mã tuỳ chỉnh để triển khai logic kinh doanh.

    vsidlc tạo các liên kết phần mềm trung gian và cấu trúc gói dịch vụ (lib.rs, service_bundle.rs và main.rs) cho Rust. Nếu bạn viết một gói dịch vụ bằng C++, hãy triển khai trực tiếp các giao diện vòng đời (libsdv_lifecycle_client_cpp) và giao tiếp (libsdv_comms_cpp):

    • Máy chủ RPC (Rust): Triển khai các trait được tạo từ lib.rs.
    • Phát hành, đăng ký và ứng dụng RPC (Rust): Gọi các hàm được tạo trong service_bundle.rs để tương tác với các dịch vụ khác.
    • Vòng đời và giao tiếp (C++): Tạo lớp con ServiceBundle và khởi chạy nhà xuất bản, người đăng ký hoặc điểm cuối RPC trong onStart.

    Rust

    1. Tạo một bản triển khai khung bằng cách chạy:

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

      Việc chạy vsidlc bằng cờ --services sẽ tạo ra một phương thức triển khai Rust nguyên mẫu cho mỗi gói dịch vụ. Thao tác này sẽ cung cấp mã nguồn (main.rs) và tệp cấu hình bản dựng (Android.bp) cùng với tất cả phần phụ thuộc, nhờ đó, gói của bạn có thể trao đổi ngay các thông báo mặc định trong khi bạn tập trung vào logic kinh doanh của mình.

    2. Đối với mỗi gói dịch vụ, hãy tìm cách triển khai Rust trong:

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

    3. Theo mặc định, phương thức triển khai được tạo sẽ tạo thông báo có giá trị mặc định và gửi thông báo giữa nhà xuất bản và người đăng ký hoặc ứng dụng RPC và máy chủ. Để sửa đổi hành vi này, hãy tìm các nhận xét TODO trong main.rs rồi điều chỉnh theo ý muốn. Ví dụ:

      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. Để đảm bảo rằng các nội dung sửa đổi của bạn đối với tệp đã tạo không bị ghi đè vào lần tiếp theo vsidlc chạy, hãy di chuyển thư mục services ra khỏi thư mục /path/to/output.

    C++

    1. Tạo một tệp tiêu đề mới, src/lib.hpp, có một lớp cho gói dịch vụ của bạn:

      #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. Tạo một tệp nguồn mới, src/lib.cpp, với cách triển khai lớp:

      #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. Tạo mục tiêu Soong của thư viện gói dịch vụ trong tệp Android.bp mới hoặc hiện có:

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

Các bước tiếp theo

Để triển khai gói dịch vụ, hãy xem phần Tạo và triển khai gói dịch vụ.