Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/std/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ embedded-sensors-hal-async = "0.3.0"
embedded-fans-async = "0.2.0"
thermal-service = { path = "../../thermal-service", features = ["log", "mock"] }
thermal-service-messages = { path = "../../thermal-service-messages" }
odp-service-common = { path = "../../odp-service-common" }

env_logger = "0.11.8"
log = "0.4.14"
Expand Down
33 changes: 19 additions & 14 deletions examples/std/src/bin/thermal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ async fn run(spawner: Spawner) {
let fans = FANS.init([fan.device()]);

static STORAGE: OnceLock<ts::Service<'static>> = OnceLock::new();
let service = ts::Service::init(&STORAGE, sensors, fans).await;
let thermal_service = ts::Service::init(&STORAGE, sensors, fans).await;

spawner.must_spawn(sensor_task(service, sensor));
spawner.must_spawn(fan_task(service, fan));
spawner.must_spawn(monitor(service));
let _fan_service = odp_service_common::spawn_service!(
spawner,
ts::fan::Service<'static, ts::mock::fan::MockFan, 16>,
ts::fan::InitParams { fan, thermal_service }
)
.expect("Failed to spawn fan service");

let _sensor_service = odp_service_common::spawn_service!(
spawner,
ts::sensor::Service<'static, ts::mock::sensor::MockSensor, 16>,
ts::sensor::InitParams {
sensor,
thermal_service
}
)
.expect("Failed to spawn sensor service");

spawner.must_spawn(monitor(thermal_service));
}

fn main() {
Expand All @@ -39,16 +54,6 @@ fn main() {
});
}

#[embassy_executor::task]
async fn sensor_task(service: &'static ts::Service<'static>, sensor: &'static ts::mock::TsMockSensor) {
ts::task::sensor_task(sensor, service).await
}

#[embassy_executor::task]
async fn fan_task(service: &'static ts::Service<'static>, fan: &'static ts::mock::TsMockFan) {
ts::task::fan_task(fan, service).await;
}

#[embassy_executor::task]
async fn monitor(service: &'static ts::Service<'static>) {
loop {
Expand Down
1 change: 1 addition & 0 deletions thermal-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ embedded-hal-async.workspace = true
embedded-hal.workspace = true
embedded-services.workspace = true
heapless.workspace = true
odp-service-common.workspace = true
thermal-service-messages.workspace = true
uuid.workspace = true
embedded-fans-async = "0.2.0"
Expand Down
88 changes: 88 additions & 0 deletions thermal-service/src/fan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,91 @@ impl<T: Controller, const SAMPLE_BUF_LEN: usize> Fan<T, SAMPLE_BUF_LEN> {
}
}
}

/// The memory resources required by the fan.
pub struct Resources<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
inner: Option<ServiceInner<'hw, T, SAMPLE_BUF_LEN>>,
}

// Note: We can't derive Default unless we trait bound T by Default,
// but we don't want that restriction since the default is just the None case
impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> Default for Resources<'hw, T, SAMPLE_BUF_LEN> {
fn default() -> Self {
Self { inner: None }
}
}

struct ServiceInner<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
fan: &'hw Fan<T, SAMPLE_BUF_LEN>,
thermal_service: &'hw crate::Service<'hw>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> ServiceInner<'hw, T, SAMPLE_BUF_LEN> {
fn new(init_params: InitParams<'hw, T, SAMPLE_BUF_LEN>) -> Self {
Self {
fan: init_params.fan,
thermal_service: init_params.thermal_service,
}
}

fn fan(&self) -> &Fan<T, SAMPLE_BUF_LEN> {
self.fan
}
}

/// A task runner for a fan. Users must run this in an embassy task or similar async execution context.
pub struct Runner<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
service: &'hw ServiceInner<'hw, T, SAMPLE_BUF_LEN>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> odp_service_common::runnable_service::ServiceRunner<'hw>
for Runner<'hw, T, SAMPLE_BUF_LEN>
{
async fn run(self) -> embedded_services::Never {
loop {
let _ = embassy_futures::join::join3(
self.service.fan.handle_rx(),
self.service.fan.handle_sampling(),
self.service.fan.handle_auto_control(self.service.thermal_service),
)
.await;
}
}
}

/// Fan service control handle.
pub struct Service<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
inner: &'hw ServiceInner<'hw, T, SAMPLE_BUF_LEN>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> Service<'hw, T, SAMPLE_BUF_LEN> {
/// Get a reference to the inner fan.
pub fn fan(&self) -> &Fan<T, SAMPLE_BUF_LEN> {
self.inner.fan()
}
}

/// Parameters required to initialize a fan service.
pub struct InitParams<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
/// The underlying `Fan` wrapper this service will control.
pub fan: &'hw Fan<T, SAMPLE_BUF_LEN>,
/// The thermal service handle for this fan to communicate with a sensor.
pub thermal_service: &'hw crate::Service<'hw>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> odp_service_common::runnable_service::Service<'hw>
for Service<'hw, T, SAMPLE_BUF_LEN>
{
type Runner = Runner<'hw, T, SAMPLE_BUF_LEN>;
type Resources = Resources<'hw, T, SAMPLE_BUF_LEN>;
type ErrorType = Error;
type InitParams = InitParams<'hw, T, SAMPLE_BUF_LEN>;

async fn new(
service_storage: &'hw mut Self::Resources,
init_params: Self::InitParams,
) -> Result<(Self, Self::Runner), Self::ErrorType> {
let service = service_storage.inner.insert(ServiceInner::new(init_params));
Ok((Self { inner: service }, Runner { service }))
}
}
1 change: 0 additions & 1 deletion thermal-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub mod fan;
pub mod mock;
pub mod mptf;
pub mod sensor;
pub mod task;
pub mod utils;

/// Thermal error
Expand Down
87 changes: 87 additions & 0 deletions thermal-service/src/sensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,90 @@ impl<T: Controller, const SAMPLE_BUF_LEN: usize> Sensor<T, SAMPLE_BUF_LEN> {
}
}
}

/// The memory resources required by the sensor.
pub struct Resources<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
inner: Option<ServiceInner<'hw, T, SAMPLE_BUF_LEN>>,
}

// Note: We can't derive Default unless we trait bound T by Default,
// but we don't want that restriction since the default is just the None case
impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> Default for Resources<'hw, T, SAMPLE_BUF_LEN> {
fn default() -> Self {
Self { inner: None }
}
}

struct ServiceInner<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
sensor: &'hw Sensor<T, SAMPLE_BUF_LEN>,
thermal_service: &'hw crate::Service<'hw>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> ServiceInner<'hw, T, SAMPLE_BUF_LEN> {
fn new(init_params: InitParams<'hw, T, SAMPLE_BUF_LEN>) -> Self {
Self {
sensor: init_params.sensor,
thermal_service: init_params.thermal_service,
}
}

fn sensor(&self) -> &Sensor<T, SAMPLE_BUF_LEN> {
self.sensor
}
}

/// A task runner for a sensor. Users must run this in an embassy task or similar async execution context.
pub struct Runner<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
service: &'hw ServiceInner<'hw, T, SAMPLE_BUF_LEN>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> odp_service_common::runnable_service::ServiceRunner<'hw>
for Runner<'hw, T, SAMPLE_BUF_LEN>
{
async fn run(self) -> embedded_services::Never {
loop {
let _ = embassy_futures::join::join(
self.service.sensor.handle_rx(),
self.service.sensor.handle_sampling(self.service.thermal_service),
)
.await;
}
}
}

/// Sensor service control handle.
pub struct Service<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
inner: &'hw ServiceInner<'hw, T, SAMPLE_BUF_LEN>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> Service<'hw, T, SAMPLE_BUF_LEN> {
/// Get a reference to the inner sensor.
pub fn sensor(&self) -> &Sensor<T, SAMPLE_BUF_LEN> {
self.inner.sensor()
}
}

/// Parameters required to initialize a sensor service.
pub struct InitParams<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> {
/// The underlying `Sensor` wrapper this service will control.
pub sensor: &'hw Sensor<T, SAMPLE_BUF_LEN>,
/// The thermal service handle for this sensor to communicate events to.
pub thermal_service: &'hw crate::Service<'hw>,
}

impl<'hw, T: Controller, const SAMPLE_BUF_LEN: usize> odp_service_common::runnable_service::Service<'hw>
for Service<'hw, T, SAMPLE_BUF_LEN>
{
type Runner = Runner<'hw, T, SAMPLE_BUF_LEN>;
type Resources = Resources<'hw, T, SAMPLE_BUF_LEN>;
type ErrorType = Error;
type InitParams = InitParams<'hw, T, SAMPLE_BUF_LEN>;

async fn new(
service_storage: &'hw mut Self::Resources,
init_params: Self::InitParams,
) -> Result<(Self, Self::Runner), Self::ErrorType> {
let service = service_storage.inner.insert(ServiceInner::new(init_params));
Ok((Self { inner: service }, Runner { service }))
}
}
18 changes: 0 additions & 18 deletions thermal-service/src/task.rs

This file was deleted.

Loading