Skip to content

Commit d5b5263

Browse files
committed
Power policy refactor
1 parent 829f5da commit d5b5263

29 files changed

Lines changed: 630 additions & 377 deletions

File tree

embedded-service/src/power/policy/action/device.rs

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ use crate::power::policy::{ConsumerPowerCapability, Error, ProviderPowerCapabili
44
use crate::{info, trace};
55

66
/// Device state machine control
7-
pub struct Device<'a, S: Kind> {
8-
device: &'a device::Device,
7+
pub struct Device<'a, S: Kind, const N: usize> {
8+
device: &'a device::Device<N>,
99
_state: core::marker::PhantomData<S>,
1010
}
1111

1212
/// Enum to contain any state
13-
pub enum AnyState<'a> {
13+
pub enum AnyState<'a, const N: usize> {
1414
/// Detached
15-
Detached(Device<'a, Detached>),
15+
Detached(Device<'a, Detached, N>),
1616
/// Idle
17-
Idle(Device<'a, Idle>),
17+
Idle(Device<'a, Idle, N>),
1818
/// Connected Consumer
19-
ConnectedConsumer(Device<'a, ConnectedConsumer>),
19+
ConnectedConsumer(Device<'a, ConnectedConsumer, N>),
2020
/// Connected Provider
21-
ConnectedProvider(Device<'a, ConnectedProvider>),
21+
ConnectedProvider(Device<'a, ConnectedProvider, N>),
2222
}
2323

24-
impl AnyState<'_> {
24+
impl<const N: usize> AnyState<'_, N> {
2525
/// Return the kind of the contained state
2626
pub fn kind(&self) -> StateKind {
2727
match self {
@@ -33,22 +33,24 @@ impl AnyState<'_> {
3333
}
3434
}
3535

36-
impl<'a, S: Kind> Device<'a, S> {
36+
impl<'a, S: Kind, const N: usize> Device<'a, S, N> {
3737
/// Create a new state machine
38-
pub(crate) fn new(device: &'a device::Device) -> Self {
38+
pub(crate) fn new(device: &'a device::Device<N>) -> Self {
3939
Self {
4040
device,
4141
_state: core::marker::PhantomData,
4242
}
4343
}
4444

4545
/// Detach the device
46-
pub async fn detach(self) -> Result<Device<'a, Detached>, Error> {
46+
pub async fn detach(self) -> Result<Device<'a, Detached, N>, Error> {
4747
info!("Received detach from device {}", self.device.id().0);
4848
self.device.set_state(device::State::Detached).await;
4949
self.device.update_consumer_capability(None).await;
5050
self.device.update_requested_provider_capability(None).await;
51-
policy::send_request(self.device.id(), policy::RequestData::NotifyDetached)
51+
self.device
52+
.context_ref
53+
.send_request(self.device.id(), policy::RequestData::NotifyDetached)
5254
.await?
5355
.complete_or_err()?;
5456
Ok(Device::new(self.device))
@@ -60,7 +62,9 @@ impl<'a, S: Kind> Device<'a, S> {
6062
self.device.update_consumer_capability(None).await;
6163
self.device.update_requested_provider_capability(None).await;
6264
self.device.set_state(device::State::Idle).await;
63-
policy::send_request(self.device.id(), policy::RequestData::NotifyDisconnect)
65+
self.device
66+
.context_ref
67+
.send_request(self.device.id(), policy::RequestData::NotifyDisconnect)
6468
.await?
6569
.complete_or_err()
6670
}
@@ -76,12 +80,14 @@ impl<'a, S: Kind> Device<'a, S> {
7680
capability
7781
);
7882
self.device.update_consumer_capability(capability).await;
79-
policy::send_request(
80-
self.device.id(),
81-
policy::RequestData::NotifyConsumerCapability(capability),
82-
)
83-
.await?
84-
.complete_or_err()
83+
self.device
84+
.context_ref
85+
.send_request(
86+
self.device.id(),
87+
policy::RequestData::NotifyConsumerCapability(capability),
88+
)
89+
.await?
90+
.complete_or_err()
8591
}
8692

8793
/// Request the given power from the power policy service
@@ -97,29 +103,33 @@ impl<'a, S: Kind> Device<'a, S> {
97103

98104
info!("Request provide from device {}, {:#?}", self.device.id().0, capability);
99105
self.device.update_requested_provider_capability(Some(capability)).await;
100-
policy::send_request(
101-
self.device.id(),
102-
policy::RequestData::RequestProviderCapability(capability),
103-
)
104-
.await?
105-
.complete_or_err()?;
106+
self.device
107+
.context_ref
108+
.send_request(
109+
self.device.id(),
110+
policy::RequestData::RequestProviderCapability(capability),
111+
)
112+
.await?
113+
.complete_or_err()?;
106114
Ok(())
107115
}
108116
}
109117

110-
impl<'a> Device<'a, Detached> {
118+
impl<'a, const N: usize> Device<'a, Detached, N> {
111119
/// Attach the device
112-
pub async fn attach(self) -> Result<Device<'a, Idle>, Error> {
120+
pub async fn attach(self) -> Result<Device<'a, Idle, N>, Error> {
113121
info!("Received attach from device {}", self.device.id().0);
114122
self.device.set_state(device::State::Idle).await;
115-
policy::send_request(self.device.id(), policy::RequestData::NotifyAttached)
123+
self.device
124+
.context_ref
125+
.send_request(self.device.id(), policy::RequestData::NotifyAttached)
116126
.await?
117127
.complete_or_err()?;
118128
Ok(Device::new(self.device))
119129
}
120130
}
121131

122-
impl Device<'_, Idle> {
132+
impl<const N: usize> Device<'_, Idle, N> {
123133
/// Notify the power policy service of an updated consumer power capability
124134
pub async fn notify_consumer_power_capability(
125135
&self,
@@ -134,25 +144,26 @@ impl Device<'_, Idle> {
134144
}
135145
}
136146

137-
impl<'a> Device<'a, ConnectedConsumer> {
147+
impl<'a, const N: usize> Device<'a, ConnectedConsumer, N> {
138148
/// Disconnect this device
139-
pub async fn disconnect(self) -> Result<Device<'a, Idle>, Error> {
149+
pub async fn disconnect(self) -> Result<Device<'a, Idle, N>, Error> {
140150
self.disconnect_internal().await?;
141151
Ok(Device::new(self.device))
142152
}
143153

144154
/// Notify the power policy service of an updated consumer power capability
145155
pub async fn notify_consumer_power_capability(
146156
&self,
157+
147158
capability: Option<ConsumerPowerCapability>,
148159
) -> Result<(), Error> {
149160
self.notify_consumer_power_capability_internal(capability).await
150161
}
151162
}
152163

153-
impl<'a> Device<'a, ConnectedProvider> {
164+
impl<'a, const N: usize> Device<'a, ConnectedProvider, N> {
154165
/// Disconnect this device
155-
pub async fn disconnect(self) -> Result<Device<'a, Idle>, Error> {
166+
pub async fn disconnect(self) -> Result<Device<'a, Idle, N>, Error> {
156167
self.disconnect_internal().await?;
157168
Ok(Device::new(self.device))
158169
}
@@ -165,6 +176,7 @@ impl<'a> Device<'a, ConnectedProvider> {
165176
/// Notify the power policy service of an updated consumer power capability
166177
pub async fn notify_consumer_power_capability(
167178
&self,
179+
168180
capability: Option<ConsumerPowerCapability>,
169181
) -> Result<(), Error> {
170182
self.notify_consumer_power_capability_internal(capability).await

embedded-service/src/power/policy/action/policy.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ use crate::{error, info};
99
const DEFAULT_TIMEOUT: Duration = Duration::from_millis(5000);
1010

1111
/// Policy state machine control
12-
pub struct Policy<'a, S: Kind> {
13-
device: &'a device::Device,
12+
pub struct Policy<'a, S: Kind, const N: usize> {
13+
device: &'a device::Device<N>,
1414
_state: core::marker::PhantomData<S>,
1515
}
1616

1717
/// Enum to contain any state
18-
pub enum AnyState<'a> {
18+
pub enum AnyState<'a, const N: usize> {
1919
/// Detached
20-
Detached(Policy<'a, Detached>),
20+
Detached(Policy<'a, Detached, N>),
2121
/// Idle
22-
Idle(Policy<'a, Idle>),
22+
Idle(Policy<'a, Idle, N>),
2323
/// Connected Consumer
24-
ConnectedConsumer(Policy<'a, ConnectedConsumer>),
24+
ConnectedConsumer(Policy<'a, ConnectedConsumer, N>),
2525
/// Connected Provider
26-
ConnectedProvider(Policy<'a, ConnectedProvider>),
26+
ConnectedProvider(Policy<'a, ConnectedProvider, N>),
2727
}
2828

29-
impl AnyState<'_> {
29+
impl<const N: usize> AnyState<'_, N> {
3030
/// Return the kind of the contained state
3131
pub fn kind(&self) -> StateKind {
3232
match self {
@@ -38,9 +38,9 @@ impl AnyState<'_> {
3838
}
3939
}
4040

41-
impl<'a, S: Kind> Policy<'a, S> {
41+
impl<'a, S: Kind, const N: usize> Policy<'a, S, N> {
4242
/// Create a new state machine
43-
pub(crate) fn new(device: &'a device::Device) -> Self {
43+
pub(crate) fn new(device: &'a device::Device<N>) -> Self {
4444
Self {
4545
device,
4646
_state: core::marker::PhantomData,
@@ -97,14 +97,14 @@ impl<'a, S: Kind> Policy<'a, S> {
9797
}
9898

9999
// The policy can do nothing when no device is attached
100-
impl Policy<'_, Detached> {}
100+
impl<const N: usize> Policy<'_, Detached, N> {}
101101

102-
impl<'a> Policy<'a, Idle> {
102+
impl<'a, const N: usize> Policy<'a, Idle, N> {
103103
/// Connect this device as a consumer
104104
pub async fn connect_as_consumer_no_timeout(
105105
self,
106106
capability: ConsumerPowerCapability,
107-
) -> Result<Policy<'a, ConnectedConsumer>, Error> {
107+
) -> Result<Policy<'a, ConnectedConsumer, N>, Error> {
108108
info!("Device {} connecting as consumer", self.device.id().0);
109109

110110
self.device
@@ -122,7 +122,7 @@ impl<'a> Policy<'a, Idle> {
122122
pub async fn connect_consumer(
123123
self,
124124
capability: ConsumerPowerCapability,
125-
) -> Result<Policy<'a, ConnectedConsumer>, Error> {
125+
) -> Result<Policy<'a, ConnectedConsumer, N>, Error> {
126126
match with_timeout(DEFAULT_TIMEOUT, self.connect_as_consumer_no_timeout(capability)).await {
127127
Ok(r) => r,
128128
Err(TimeoutError) => Err(Error::Timeout),
@@ -133,7 +133,7 @@ impl<'a> Policy<'a, Idle> {
133133
pub async fn connect_provider_no_timeout(
134134
self,
135135
capability: ProviderPowerCapability,
136-
) -> Result<Policy<'a, ConnectedProvider>, Error> {
136+
) -> Result<Policy<'a, ConnectedProvider, N>, Error> {
137137
self.connect_as_provider_internal_no_timeout(capability)
138138
.await
139139
.map(|_| Policy::new(self.device))
@@ -143,30 +143,30 @@ impl<'a> Policy<'a, Idle> {
143143
pub async fn connect_provider(
144144
self,
145145
capability: ProviderPowerCapability,
146-
) -> Result<Policy<'a, ConnectedProvider>, Error> {
146+
) -> Result<Policy<'a, ConnectedProvider, N>, Error> {
147147
self.connect_provider_internal(capability)
148148
.await
149149
.map(|_| Policy::new(self.device))
150150
}
151151
}
152152

153-
impl<'a> Policy<'a, ConnectedConsumer> {
153+
impl<'a, const N: usize> Policy<'a, ConnectedConsumer, N> {
154154
/// Disconnect this device
155-
pub async fn disconnect_no_timeout(self) -> Result<Policy<'a, Idle>, Error> {
155+
pub async fn disconnect_no_timeout(self) -> Result<Policy<'a, Idle, N>, Error> {
156156
self.disconnect_internal_no_timeout()
157157
.await
158158
.map(|_| Policy::new(self.device))
159159
}
160160

161161
/// Disconnect this device
162-
pub async fn disconnect(self) -> Result<Policy<'a, Idle>, Error> {
162+
pub async fn disconnect(self) -> Result<Policy<'a, Idle, N>, Error> {
163163
self.disconnect_internal().await.map(|_| Policy::new(self.device))
164164
}
165165
}
166166

167-
impl<'a> Policy<'a, ConnectedProvider> {
167+
impl<'a, const N: usize> Policy<'a, ConnectedProvider, N> {
168168
/// Disconnect this device
169-
pub async fn disconnect_no_timeout(self) -> Result<Policy<'a, Idle>, Error> {
169+
pub async fn disconnect_no_timeout(self) -> Result<Policy<'a, Idle, N>, Error> {
170170
if let Err(e) = self.disconnect_internal_no_timeout().await {
171171
error!("Error disconnecting device {}: {:?}", self.device.id().0, e);
172172
return Err(e);
@@ -175,7 +175,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
175175
}
176176

177177
/// Disconnect this device
178-
pub async fn disconnect(self) -> Result<Policy<'a, Idle>, Error> {
178+
pub async fn disconnect(self) -> Result<Policy<'a, Idle, N>, Error> {
179179
match with_timeout(DEFAULT_TIMEOUT, self.disconnect_no_timeout()).await {
180180
Ok(r) => r,
181181
Err(TimeoutError) => Err(Error::Timeout),
@@ -186,7 +186,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
186186
pub async fn connect_as_consumer_no_timeout(
187187
self,
188188
capability: ConsumerPowerCapability,
189-
) -> Result<Policy<'a, ConnectedConsumer>, Error> {
189+
) -> Result<Policy<'a, ConnectedConsumer, N>, Error> {
190190
info!("Device {} connecting as consumer", self.device.id().0);
191191

192192
self.device
@@ -204,7 +204,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
204204
pub async fn connect_consumer(
205205
self,
206206
capability: ConsumerPowerCapability,
207-
) -> Result<Policy<'a, ConnectedConsumer>, Error> {
207+
) -> Result<Policy<'a, ConnectedConsumer, N>, Error> {
208208
match with_timeout(DEFAULT_TIMEOUT, self.connect_as_consumer_no_timeout(capability)).await {
209209
Ok(r) => r,
210210
Err(TimeoutError) => Err(Error::Timeout),
@@ -215,7 +215,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
215215
pub async fn connect_provider_no_timeout(
216216
&self,
217217
capability: ProviderPowerCapability,
218-
) -> Result<Policy<'a, ConnectedProvider>, Error> {
218+
) -> Result<Policy<'a, ConnectedProvider, N>, Error> {
219219
self.connect_as_provider_internal_no_timeout(capability)
220220
.await
221221
.map(|_| Policy::new(self.device))
@@ -225,7 +225,7 @@ impl<'a> Policy<'a, ConnectedProvider> {
225225
pub async fn connect_provider(
226226
&self,
227227
capability: ProviderPowerCapability,
228-
) -> Result<Policy<'a, ConnectedProvider>, Error> {
228+
) -> Result<Policy<'a, ConnectedProvider, N>, Error> {
229229
self.connect_provider_internal(capability)
230230
.await
231231
.map(|_| Policy::new(self.device))

0 commit comments

Comments
 (0)