Implementa la lógica empresarial

  1. Verifica que el directorio del catálogo de VSIDL tenga los archivos de compilación, protobuf y VSIDL necesarios. Para comenzar con una muestra existente, puedes encontrar un ejemplo de una carpeta de catálogo válida en /system/software_defined_vehicle/samples/vsidl/stable/catalog.

    Estructura de catálogo de muestra:

    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. Escribe código personalizado para implementar tu lógica empresarial:

    • Servidores de RPC: Implementa los rasgos de lib.rs.
    • Clientes de publicación y suscripción y de RPC: Llama a las funciones generadas dentro de service_bundle.rs para interactuar con otros servicios.

    Rust

    1. Para generar un esqueleto de implementación, ejecuta el siguiente comando:

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

      Ejecutar vsidlc con la marca --services genera una implementación de Rust estándar para cada paquete de servicios. Esto proporciona la estructura necesaria, incluido el código fuente (main.rs) y un archivo de configuración de compilación (Android.bp) con todas las dependencias, lo que permite el intercambio inmediato de mensajes predeterminados mientras te enfocas en implementar la lógica comercial principal.

    2. Para cada paquete de servicios, busca la implementación de Rust en la siguiente ubicación:

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

    3. De forma predeterminada, la implementación generada crea mensajes con valores predeterminados y los envía entre publicadores y suscriptores, o entre clientes y servidores de RPC. Para modificar este comportamiento, busca los comentarios de TODO en main.rs y ajústalos según tus preferencias. Por ejemplo:

      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 asegurarte de que tus modificaciones en los archivos generados no se sobrescriban la próxima vez que se ejecute vsidlc, mueve la carpeta services fuera de la carpeta /path/to/output.

    C++

    1. Crea un nuevo archivo de encabezado, src/lib.hpp, con una clase para tu paquete de servicios:

      #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. Crea un archivo fuente nuevo, src/lib.cpp, con la implementación de la clase:

      #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. Crea un destino de Soong de biblioteca de paquetes de servicios en un archivo Android.bp nuevo o existente:

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

¿Qué sigue?

Para implementar tus paquetes de servicios, consulta Compila e implementa paquetes de servicios.