Sprawdź, czy katalog VSIDL zawiera niezbędne pliki kompilacji, protobuf i VSIDL. Aby zacząć od istniejącego przykładu, możesz znaleźć przykład prawidłowego folderu katalogu w
/system/software_defined_vehicle/samples/vsidl/stable/catalog.Przykładowa struktura katalogu:
my_catalog/ ├── Android.bp # Defines rust_protobuf modules for .proto files ├── types.proto # Protobuf message / RPC service definitions └── architecture.vsidl # VSIDL service bundle definitionsNapisz niestandardowy kod, aby wdrożyć logikę biznesową.
vsidlcgeneruje powiązania oprogramowania pośredniczącego i szkielet pakietu usług (lib.rs,service_bundle.rsimain.rs) dla języka Rust. Jeśli piszesz pakiet usług w C++, zaimplementuj interfejsy cyklu życia (libsdv_lifecycle_client_cpp) i komunikacji (libsdv_comms_cpp) bezpośrednio:- Serwery RPC (Rust): zaimplementuj wygenerowane cechy z
lib.rs. - Klienty publikowania, subskrypcji i RPC (Rust): wywołuj wygenerowane funkcje w
service_bundle.rs, aby wchodzić w interakcje z innymi usługami. Cykl życia i komunikacja (C++): utwórz podklasę
ServiceBundlei zainicjuj wydawców, subskrybentów lub punkty końcowe RPC wonStart.
Rdza
Wygeneruj implementację szkieletu, uruchamiając:
vsidlc -c /path/to/catalog -o /path/to/output --servicesUruchomienie polecenia
vsidlcz flagą--servicesgeneruje standardową implementację Rust dla każdego pakietu usług. Zawiera on kod źródłowy (main.rs) i plik konfiguracji kompilacji (Android.bp) ze wszystkimi zależnościami, dzięki czemu pakiet może od razu wymieniać domyślne wiadomości, podczas gdy Ty skupiasz się na logice biznesowej.W przypadku każdego pakietu usług implementację w języku Rust znajdziesz w:
/path/to/output/services/ServiceBundleName/src/main.rs.Domyślnie wygenerowana implementacja tworzy wiadomości z wartościami domyślnymi i wysyła je między aplikacjami publikującymi i subskrybującymi lub klientami i serwerami RPC. Aby zmienić to zachowanie, znajdź komentarze
TODOw plikumain.rsi dostosuj je do swoich preferencji. Przykład: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(); } }Aby mieć pewność, że zmiany wprowadzone w wygenerowanych plikach nie zostaną zastąpione przy następnym uruchomieniu
vsidlc, przenieś folderservicesz folderu/path/to/output.
C++
Utwórz nowy plik nagłówka
src/lib.hppz klasą pakietu usług:#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_bundleUtwórz nowy plik źródłowy
src/lib.cppz implementacją klasy:#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_bundleUtwórz cel Soong biblioteki pakietu usług w nowym lub istniejącym pliku
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, }
- Serwery RPC (Rust): zaimplementuj wygenerowane cechy z
Co dalej?
Aby wdrożyć pakiety usług, zapoznaj się z artykułem Tworzenie i wdrażanie pakietów usług.