11#![ no_std]
22use core:: ops:: DerefMut ;
33use embassy_sync:: mutex:: Mutex ;
4- use embassy_sync:: once_lock:: OnceLock ;
54use 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 ;
77use embedded_services:: power:: policy:: { action, policy, * } ;
8+ use embedded_services:: sync:: Lockable ;
89use embedded_services:: { comms, error, info} ;
910
1011pub 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}
0 commit comments