Triển khai logic kinh doanh

  1. Xác minh rằng thư mục danh mục VSIDL của bạn có các tệp cần thiết về bản dựng, protobuf và VSIDL. Để bắt đầu từ một mẫu hiện có, bạn có thể tìm thấy 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:

    • Máy chủ RPC: Triển khai các đặc điểm từ lib.rs.
    • Xuất bản và đăng ký cũng như máy khách RPC: Gọi các hàm đã tạo trong service_bundle.rs để tương tác với các dịch vụ khác.

    Rust

    1. Tạo quá trình 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 một cờ tạo quá trình triển khai Rust cho mỗi gói dịch vụ. Quá trình này cung cấp giàn giáo cần thiết (bao gồm cả mã nguồn (main.rs) và tệp cấu hình bản dựng (Android.bp) với tất cả các phần phụ thuộc), cho phép trao đổi ngay lập tức các thông báo mặc định trong khi bạn tập trung vào việc triển khai logic kinh doanh cốt lõi.

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

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

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

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

    C++

    1. Tạo tệp tiêu đề mới, src/lib.hpp, có 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 tệp nguồn mới, src/lib.cpp, có quá trình 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() {
          // 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
      
    3. Tạo mục tiêu Soong của thư viện gói dịch vụ trong tệp mới hoặc hiện có 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,
      }
      

Các bước tiếp theo

Để triển khai các gói dịch vụ, hãy xem bài viết Tạo và triển khai gói dịch vụ.