Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
********************************************************************************/

use com_api::{
Builder, Error, FindServiceSpecifier, InstanceSpecifier, LolaRuntimeBuilderImpl,
Builder, Error, FindServiceSpecifier, InstanceSpecifier, Interface, LolaRuntimeBuilderImpl,
OfferedProducer, Producer, Publisher, Result, Runtime, RuntimeBuilder, SampleContainer,
SampleMaybeUninit, SampleMut, ServiceDiscovery, Subscriber, Subscription,
};

use com_api_gen::{Tire, VehicleConsumer, VehicleInterface, VehicleOfferedProducer};
use com_api_gen::{Tire, VehicleInterface };

// Type aliases for generated consumer and offered producer types for the Vehicle interface
// VehicleConsumer is the consumer type generated for the Vehicle interface, parameterized by the runtime R
type VehicleConsumer<R> = <VehicleInterface as Interface>::Consumer<R>;
// VehicleOfferedProducer is the offered producer type generated for the Vehicle interface, parameterized by the runtime R
type VehicleOfferedProducer<R> =
<<VehicleInterface as Interface>::Producer<R> as Producer<R>>::OfferedProducer;

// Example struct demonstrating composition with VehicleConsumer
pub struct VehicleMonitor<R: Runtime> {
Expand Down
103 changes: 21 additions & 82 deletions score/mw/com/example/com-api-example/com-api-gen/com_api_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

use com_api::{
CommData, Consumer, Interface, OfferedProducer, Producer, ProviderInfo, Publisher, Reloc,
Runtime, Subscriber,
};
use com_api::{interface, CommData, ProviderInfo, Publisher, Reloc, Subscriber};

#[derive(Debug, Reloc)]
#[repr(C)]
Expand All @@ -34,81 +31,23 @@ impl CommData for Exhaust {
const ID: &'static str = "Exhaust";
}

pub struct VehicleInterface {}

/// Generic
impl Interface for VehicleInterface {
const INTERFACE_ID: &'static str = "VehicleInterface";
type Consumer<R: Runtime + ?Sized> = VehicleConsumer<R>;
type Producer<R: Runtime + ?Sized> = VehicleProducer<R>;
}

pub struct VehicleConsumer<R: Runtime + ?Sized> {
pub left_tire: R::Subscriber<Tire>,
pub exhaust: R::Subscriber<Exhaust>,
}

impl<R: Runtime + ?Sized> Consumer<R> for VehicleConsumer<R> {
fn new(instance_info: R::ConsumerInfo) -> Self {
VehicleConsumer {
left_tire: R::Subscriber::new("left_tire", instance_info.clone())
.expect("Failed to create subscriber"),
exhaust: R::Subscriber::new("exhaust", instance_info.clone())
.expect("Failed to create subscriber"),
}
}
}

pub struct AnotherInterface {}

pub struct VehicleProducer<R: Runtime + ?Sized> {
_runtime: core::marker::PhantomData<R>,
instance_info: R::ProviderInfo,
}

impl<R: Runtime + ?Sized> Producer<R> for VehicleProducer<R> {
type Interface = VehicleInterface;
type OfferedProducer = VehicleOfferedProducer<R>;

fn offer(self) -> com_api::Result<Self::OfferedProducer> {
let vehicle_offered_producer = VehicleOfferedProducer {
left_tire: R::Publisher::new("left_tire", self.instance_info.clone())
.expect("Failed to create publisher"),
exhaust: R::Publisher::new("exhaust", self.instance_info.clone())
.expect("Failed to create publisher"),
instance_info: self.instance_info.clone(),
};
// Offer the service instance to make it discoverable
// this is called after skeleton created using producer_builder API
self.instance_info.offer_service()?;
Ok(vehicle_offered_producer)
}

fn new(instance_info: R::ProviderInfo) -> com_api::Result<Self> {
Ok(VehicleProducer {
_runtime: core::marker::PhantomData,
instance_info,
})
}
}

pub struct VehicleOfferedProducer<R: Runtime + ?Sized> {
pub left_tire: R::Publisher<Tire>,
pub exhaust: R::Publisher<Exhaust>,
instance_info: R::ProviderInfo,
}

impl<R: Runtime + ?Sized> OfferedProducer<R> for VehicleOfferedProducer<R> {
type Interface = VehicleInterface;
type Producer = VehicleProducer<R>;

fn unoffer(self) -> com_api::Result<Self::Producer> {
let vehicle_producer = VehicleProducer {
_runtime: std::marker::PhantomData,
instance_info: self.instance_info.clone(),
};
// Stop offering the service instance to withdraw it from system availability
self.instance_info.stop_offer_service()?;
Ok(vehicle_producer)
}
}
// Example interface definition using the interface macro with a custom UID for the interface.
// This will generate the following types and trait implementations:
// - VehicleInterface struct with INTERFACE_ID = "VehicleInterface"
// - VehicleConsumer<R>, VehicleProducer<R>, VehicleOfferedProducer<R> with appropriate trait
// implementations for the Vehicle interface.
// The macro invocation defines an interface named "Vehicle" with two events: "left_tire" and "exhaust".
// The generated code will include:
// - VehicleInterface struct with INTERFACE_ID = "VehicleInterface"
// - VehicleConsumer<R> struct that implements Consumer trait for subscribing to "left_tire"
// and "exhaust" events.
// - VehicleProducer<R> struct that implements Producer trait for producing "left_tire" and
// "exhaust" events.
// - VehicleOfferedProducer<R> struct that implements OfferedProducer trait for offering
// "left_tire" and "exhaust" events.
interface!(
interface Vehicle, "VehicleInterface", {
left_tire: Event<Tire>,
exhaust: Event<Exhaust>,
}
);
21 changes: 19 additions & 2 deletions score/mw/com/impl/rust/com-api/com-api-concept/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
#
# SPDX-License-Identifier: Apache-2.0
# *******************************************************************************
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test","rust_doc_test")

rust_library(
name = "com-api-concept",
srcs = [
"lib.rs",
"com_api_concept.rs",
"reloc.rs",
"interface_macros.rs",
],
crate_root = "com_api_concept.rs",
proc_macro_deps = [
"//score/mw/com/impl/rust/com-api/com-api-concept-macros",
"@crate_index//:paste",
],
visibility = [
"//visibility:public", # platform_only
Expand All @@ -36,3 +38,18 @@ rust_test(
tags = ["manual"],
deps = [":com-api-concept"],
)

rust_doc_test(
name = "com-api-concept-macros-tests",
crate = ":com-api-concept",
deps = ["//score/mw/com/impl/rust/com-api/com-api",],
)

rust_test(
name = "com-api-concept-macros-unit-tests",
srcs = ["interface_macros.rs"],
proc_macro_deps = [
"@crate_index//:paste",
],
deps = ["//score/mw/com/impl/rust/com-api/com-api",],
)
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@
use core::fmt::Debug;
use core::future::Future;
use core::ops::{Deref, DerefMut};
pub mod reloc;
use containers::fixed_capacity::FixedCapacityQueue;
pub use reloc::Reloc;
use std::path::Path;
use crate::Reloc;

/// Error enumeration for different failure cases in the Consumer/Producer/Runtime APIs.
#[derive(Debug)]
Expand Down
Loading