確認 VSIDL 目錄含有必要的建構、protobuf 和 VSIDL 檔案。如要從現有範例開始,您可以在
/system/software_defined_vehicle/samples/vsidl/stable/catalog下找到有效目錄資料夾的範例。目錄結構範例:
my_catalog/ ├── Android.bp # Defines rust_protobuf modules for .proto files ├── types.proto # Protobuf message / RPC service definitions └── architecture.vsidl # VSIDL service bundle definitions編寫自訂程式碼,導入商業邏輯:
- RPC 伺服器:實作
lib.rs中的特徵。 發布、訂閱和 RPC 用戶端:呼叫
service_bundle.rs內產生的函式,與其他服務互動。
Rust
執行下列指令,產生骨架實作:
vsidlc -c /path/to/catalog -o /path/to/output --services使用
--services標記執行vsidlc,即可為每個服務套件產生樣板 Rust 實作項目。這會提供必要的架構,包括原始碼 (main.rs) 和建構設定檔 (Android.bp) (含所有依附元件),讓您專注於實作核心業務邏輯時,能立即交換預設訊息。針對每個服務套件,在下列位置找出 Rust 實作項目:
/path/to/output/services/ServiceBundleName/src/main.rs。根據預設,產生的實作會建立含有預設值的訊息,並在發布端和訂閱端或 RPC 用戶端和伺服器之間傳送。如要修改這項行為,請在
main.rs中找出TODO註解,然後根據偏好調整。例如: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(); } }為確保下次執行
vsidlc時,系統不會覆寫您對產生檔案所做的修改,請將services資料夾移出/path/to/output資料夾。
C++
建立新的標頭檔案
src/lib.hpp,其中包含服務套件的類別:#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建立新的來源檔案
src/lib.cpp,並實作類別:#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在新的或現有的
Android.bp檔案中,建立服務套件程式庫 Soong 目標:// 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, }
- RPC 伺服器:實作
後續步驟
如要部署服務套裝組合,請參閱「建構及部署服務套裝組合」。