Skip to content

Commit e84f968

Browse files
committed
Power policy refactor
1 parent 104853b commit e84f968

File tree

3 files changed

+31
-39
lines changed

3 files changed

+31
-39
lines changed

power-policy-service/src/consumer.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ fn cmp_consumer_capability(
3535
))
3636
}
3737

38-
impl PowerPolicy {
38+
impl<D: Lockable + 'static, R: EventReceiver + 'static> PowerPolicy<D, R>
39+
where
40+
D::Inner: DeviceTrait,
41+
{
3942
/// Iterate over all devices to determine what is best power port provides the highest power
4043
async fn find_best_consumer(&self, state: &InternalState) -> Result<Option<AvailableConsumer>, Error> {
4144
let mut best_consumer = None;
4245
let current_consumer_id = state.current_consumer_state.map(|f| f.device_id);
4346

4447
for node in self.context.devices().await {
45-
let device = node.data::<Device>().ok_or(Error::InvalidDevice)?;
48+
let device = node.data::<Device<D, R>>().ok_or(Error::InvalidDevice)?;
4649

4750
// Update the best available consumer
4851
best_consumer = match (best_consumer, device.consumer_capability().await) {
@@ -83,7 +86,7 @@ impl PowerPolicy {
8386
// Count how many available unconstrained devices we have
8487
let mut unconstrained_new = UnconstrainedState::default();
8588
for node in self.context.devices().await {
86-
let device = node.data::<Device>().ok_or(Error::InvalidDevice)?;
89+
let device = node.data::<Device<D, R>>().ok_or(Error::InvalidDevice)?;
8790
if let Some(capability) = device.consumer_capability().await {
8891
// The device is considered unconstrained if it meets the auto unconstrained power threshold
8992
let auto_unconstrained = self

power-policy-service/src/lib.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#![no_std]
22
use core::ops::DerefMut;
33
use embassy_sync::mutex::Mutex;
4-
use embassy_sync::once_lock::OnceLock;
54
use embedded_services::GlobalRawMutex;
6-
use embedded_services::power::policy::device::Device;
5+
use embedded_services::power::policy::device::{Device, DeviceTrait};
6+
use embedded_services::power::policy::policy::EventReceiver;
77
use embedded_services::power::policy::{action, policy, *};
8+
use embedded_services::sync::Lockable;
89
use embedded_services::{comms, error, info};
910

1011
pub mod config;
@@ -25,9 +26,12 @@ struct InternalState {
2526
}
2627

2728
/// Power policy state
28-
pub struct PowerPolicy {
29+
pub struct PowerPolicy<D: Lockable, R: EventReceiver>
30+
where
31+
D::Inner: DeviceTrait,
32+
{
2933
/// Power policy context
30-
context: policy::ContextToken,
34+
context: policy::ContextToken<D, R>,
3135
/// State
3236
state: Mutex<GlobalRawMutex, InternalState>,
3337
/// Comms endpoint
@@ -36,7 +40,10 @@ pub struct PowerPolicy {
3640
config: config::Config,
3741
}
3842

39-
impl PowerPolicy {
43+
impl<D: Lockable + 'static, R: EventReceiver + 'static> PowerPolicy<D, R>
44+
where
45+
D::Inner: DeviceTrait,
46+
{
4047
/// Create a new power policy
4148
pub fn create(config: config::Config) -> Option<Self> {
4249
Some(Self {
@@ -48,30 +55,25 @@ impl PowerPolicy {
4855
}
4956

5057
async fn process_notify_attach(&self) -> Result<(), Error> {
51-
self.context.send_response(Ok(policy::ResponseData::Complete)).await;
5258
Ok(())
5359
}
5460

5561
async fn process_notify_detach(&self) -> Result<(), Error> {
56-
self.context.send_response(Ok(policy::ResponseData::Complete)).await;
5762
self.update_current_consumer().await?;
5863
Ok(())
5964
}
6065

6166
async fn process_notify_consumer_power_capability(&self) -> Result<(), Error> {
62-
self.context.send_response(Ok(policy::ResponseData::Complete)).await;
6367
self.update_current_consumer().await?;
6468
Ok(())
6569
}
6670

6771
async fn process_request_provider_power_capabilities(&self, device: DeviceId) -> Result<(), Error> {
68-
self.context.send_response(Ok(policy::ResponseData::Complete)).await;
6972
self.connect_provider(device).await;
7073
Ok(())
7174
}
7275

7376
async fn process_notify_disconnect(&self) -> Result<(), Error> {
74-
self.context.send_response(Ok(policy::ResponseData::Complete)).await;
7577
if let Some(consumer) = self.state.lock().await.current_consumer_state.take() {
7678
info!("Device{}: Connected consumer disconnected", consumer.device_id.0);
7779
self.disconnect_chargers().await?;
@@ -103,31 +105,31 @@ impl PowerPolicy {
103105
let device = self.context.get_device(request.id).await?;
104106

105107
match request.data {
106-
policy::RequestData::NotifyAttached => {
108+
policy::RequestData::Attached => {
107109
info!("Received notify attached from device {}", device.id().0);
108110
self.process_notify_attach().await
109111
}
110-
policy::RequestData::NotifyDetached => {
112+
policy::RequestData::Detached => {
111113
info!("Received notify detached from device {}", device.id().0);
112114
self.process_notify_detach().await
113115
}
114-
policy::RequestData::NotifyConsumerCapability(capability) => {
116+
policy::RequestData::UpdatedConsumerCapability(capability) => {
115117
info!(
116118
"Device{}: Received notify consumer capability: {:#?}",
117119
device.id().0,
118120
capability,
119121
);
120122
self.process_notify_consumer_power_capability().await
121123
}
122-
policy::RequestData::RequestProviderCapability(capability) => {
124+
policy::RequestData::RequestedProviderCapability(capability) => {
123125
info!(
124126
"Device{}: Received request provider capability: {:#?}",
125127
device.id().0,
126128
capability,
127129
);
128130
self.process_request_provider_power_capabilities(device.id()).await
129131
}
130-
policy::RequestData::NotifyDisconnect => {
132+
policy::RequestData::Disconnected => {
131133
info!("Received notify disconnect from device {}", device.id().0);
132134
self.process_notify_disconnect().await
133135
}
@@ -141,23 +143,7 @@ impl PowerPolicy {
141143
}
142144
}
143145

144-
impl comms::MailboxDelegate for PowerPolicy {}
145-
146-
#[embassy_executor::task]
147-
pub async fn task(config: config::Config) {
148-
info!("Starting power policy task");
149-
static POLICY: OnceLock<PowerPolicy> = OnceLock::new();
150-
let policy =
151-
POLICY.get_or_init(|| PowerPolicy::create(config).expect("Power policy singleton already initialized"));
152-
153-
if comms::register_endpoint(policy, &policy.tp).await.is_err() {
154-
error!("Failed to register power policy endpoint");
155-
return;
156-
}
157-
158-
loop {
159-
if let Err(e) = policy.process().await {
160-
error!("Error processing request: {:?}", e);
161-
}
162-
}
146+
impl<D: Lockable + 'static, R: EventReceiver + 'static> comms::MailboxDelegate for PowerPolicy<D, R> where
147+
D::Inner: DeviceTrait
148+
{
163149
}

power-policy-service/src/provider.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ pub(super) struct State {
2525
state: PowerState,
2626
}
2727

28-
impl PowerPolicy {
28+
impl<D: Lockable + 'static, R: EventReceiver + 'static> PowerPolicy<D, R>
29+
where
30+
D::Inner: DeviceTrait,
31+
{
2932
/// Attempt to connect the requester as a provider
3033
pub(super) async fn connect_provider(&self, requester_id: DeviceId) {
3134
trace!("Device{}: Attempting to connect as provider", requester_id.0);
@@ -48,7 +51,7 @@ impl PowerPolicy {
4851
let mut total_power_mw = 0;
4952

5053
// Determine total requested power draw
51-
for device in self.context.devices().await.iter_only::<device::Device>() {
54+
for device in self.context.devices().await.iter_only::<device::Device<D, R>>() {
5255
let target_provider_cap = if device.id() == requester_id {
5356
// Use the requester's requested power capability
5457
// this handles both new connections and upgrade requests

0 commit comments

Comments
 (0)