//! K2121–K2125 power authority. Only the current holder of the power grant //! (PID1) can obtain a one-shot ticket for an operation the grant allows. //! Any other caller is denied and nothing changes except the visible denial //! count: no process is stopped, no pending ticket is created. A ticket must //! be committed (handed to the platform executor) or cancelled before another //! is issued; a transfer or revocation makes earlier tickets stale. #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error { Configuration=1, Denied, Unsupported, Pending, Stale, Revoked } #[repr(u64)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Op { Off=1, Reset=2 } pub const ALL:u64=0b11; #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Identity { pub pid:u64, pub generation:u64 } /// One-shot approval. Not Clone: it is consumed by commit or cancel. #[repr(C)] #[derive(Debug,PartialEq,Eq)] pub struct Ticket { op:Op, holder:Identity, grant:u64, sequence:u64 } impl Ticket { pub fn op(&self)->Op{self.op} pub fn sequence(&self)->u64{self.sequence} } #[repr(C)] #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Status { pub holder:Identity, pub grant:u64, pub ops:u64, pub pending:u64, pub issued:u64, pub committed:u64, pub cancelled:u64, pub denied:u64, pub revoked:bool } #[repr(C)] #[derive(Debug)] pub struct Authority { holder:Identity, grant:u64, ops:u64, pending:u64, issued:u64, committed:u64, cancelled:u64, denied:u64, revoked:bool } impl Authority { pub fn new(holder:Identity,ops:u64)->Result{ if holder.pid==0||holder.generation==0||ops==0||ops&!ALL!=0{return Err(Error::Configuration)} Ok(Self{holder,grant:1,ops,pending:0,issued:0,committed:0,cancelled:0,denied:0,revoked:false}) } pub fn status(&self)->Status { Status{holder:self.holder,grant:self.grant,ops:self.ops,pending:self.pending,issued:self.issued,committed:self.committed,cancelled:self.cancelled,denied:self.denied,revoked:self.revoked} } /// `caller` is the trusted identity of the requesting process. pub fn request(&mut self,caller:Identity,op:Op)->Result{ if caller!=self.holder{self.denied+=1;return Err(Error::Denied)} if self.revoked{return Err(Error::Revoked)} if self.ops&(op as u64)==0{return Err(Error::Unsupported)} if self.pending!=0{return Err(Error::Pending)} self.issued+=1;self.pending=self.issued; Ok(Ticket{op,holder:self.holder,grant:self.grant,sequence:self.issued}) } // Sequences start at 1, so a cleared pending slot (0) never matches; transfer // and revocation clear it as well. fn matches(&self,t:&Ticket)->bool{t.holder==self.holder&&t.grant==self.grant&&t.sequence==self.pending} /// Hands a valid ticket to the platform executor exactly once. pub fn commit(&mut self,ticket:Ticket)->Result{ if !self.matches(&ticket){return Err((Error::Stale,ticket))} self.pending=0;self.committed+=1;Ok(ticket.op) } pub fn cancel(&mut self,ticket:Ticket)->Result<(),(Error,Ticket)>{ if !self.matches(&ticket){return Err((Error::Stale,ticket))} self.pending=0;self.cancelled+=1;Ok(()) } /// Only the holder can move the grant; outstanding tickets become stale. pub fn transfer(&mut self,caller:Identity,to:Identity)->Result{ if caller!=self.holder{self.denied+=1;return Err(Error::Denied)} if self.revoked{return Err(Error::Revoked)} if to.pid==0||to.generation==0{return Err(Error::Configuration)} self.holder=to;self.grant+=1;self.pending=0;Ok(self.status()) } pub fn revoke(&mut self,caller:Identity)->Result{ if caller!=self.holder{self.denied+=1;return Err(Error::Denied)} self.revoked=true;self.pending=0;Ok(self.status()) } }