Implementar a lógica de negócios

  1. Verifique se o diretório do catálogo VSIDL tem os arquivos de build, protobuf e VSIDL necessários. Para começar com um exemplo atual, encontre um exemplo de pasta de catálogo válida em /system/software_defined_vehicle/samples/vsidl/stable/catalog.

    Exemplo de estrutura de catálogo:

    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. Escreva um código personalizado para implementar sua lógica de negócios:

    • Servidores RPC: implemente os traços de lib.rs.
    • Clientes de publicação e assinatura e RPC: chame as funções geradas em service_bundle.rs para interagir com outros serviços.

    Rust

    1. Gere uma implementação de esqueleto executando:

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

      A execução de vsidlc com a flag --services gera uma implementação Rust padrão para cada pacote de serviços. Isso fornece a estrutura necessária, incluindo o código-fonte (main.rs) e um arquivo de configuração do build (Android.bp) com todas as dependências, permitindo a troca imediata de mensagens padrão enquanto você se concentra na implementação da lógica de negócios principal.

    2. Para cada pacote de serviços, encontre a implementação Rust em:

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

    3. Por padrão, a implementação gerada cria mensagens com valores padrão e as envia entre editores e assinantes ou clientes e servidores RPC. Para modificar esse comportamento, encontre os comentários TODO em main.rs e ajuste-os às suas preferências. Exemplo:

      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. Para garantir que suas modificações nos arquivos gerados não sejam substituídas na próxima vez que vsidlc for executado, mova a pasta services para fora da pasta /path/to/output.

    C++

    1. Crie um novo arquivo principal, src/lib.hpp, com uma classe para seu pacote de serviços:

      #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. Crie um novo arquivo de origem, src/lib.cpp, com a implementação da classe:

      #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. Crie um destino Soong de biblioteca de pacote de serviços em um arquivo novo ou existente 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,
      }
      

A seguir

Para implantar seus pacotes de serviços, consulte Criar e implantar pacotes de serviços.