#![allow( clippy::type_complexity, clippy::too_many_arguments, clippy::large_enum_variant, clippy::upper_case_acronyms )] use mpstthree::binary::struct_trait::session::Session; use mpstthree::binary::struct_trait::{end::End, recv::Recv}; use mpstthree::generate; use mpstthree::role::broadcast::RoleBroadcast; use mpstthree::role::end::RoleEnd; use std::error::Error; // Create the new MeshedChannels for three participants and the close and fork functions generate!("rec_and_cancel", MeshedChannels, Scl, Sda); // Labels // Bus free time between start and stop condition struct BUF {} // Hold time(repeated) START condition // After this period, the first clock pulse is generated struct HDSTA {} // Set-up time for a repeated START condition struct SUSTA {} // Set-up time for STOP condition struct SUSTO {} // Data hold time struct HDDAT {} // Data setup time struct SUDAT {} // Stop loop struct Stop {} // Types // Scl type Choice0FromSclToSda = Recv; type SdaToSclLoop = Recv>>>>>; type SdaToSclStop = Recv; enum Branching0FromSclToSda { Loop( MeshedChannels< SdaToSclLoop, RoleSda>>>>>>, NameScl, >, ), Stop(MeshedChannels, NameScl>), } // Sda type ChooseFromSclToSda = ::Dual; type SclToSdaLoop = ::Dual; type SclToSdaStop = ::Dual; // Endpoints // Scl type Endpoint0Car = MeshedChannels, NameScl>; // Sda type Endpoint0Key = MeshedChannels; type Endpoint1KeyLoop = MeshedChannels< SclToSdaLoop, RoleScl>>>>>, NameSda, >; type Endpoint1KeyStop = MeshedChannels, NameSda>; // Functions ///////////////////////// // Car fn endpoint_scl(s: Endpoint0Car) -> Result<(), Box> { recurs_scl(s) } fn recurs_scl(s: Endpoint0Car) -> Result<(), Box> { offer_mpst!(s, { Branching0FromSclToSda::Stop(s) => { let (_stop, s) = s.recv()?; s.close() }, Branching0FromSclToSda::Loop(s) => { let (_buf, s) = s.recv()?; let (_hdsta, s) = s.recv()?; let (_susta, s) = s.recv()?; let (_susto, s) = s.recv()?; let (_hddat, s) = s.recv()?; let (_sudat, s) = s.recv()?; recurs_scl(s) }, }) } ///////////////////////// // Key fn endpoint_sda(s: Endpoint0Key) -> Result<(), Box> { recurs_sda(s, LOOPS) } fn recurs_sda(s: Endpoint0Key, loops: i64) -> Result<(), Box> { match loops { 0 => { let s: Endpoint1KeyStop = choose_mpst_sda_to_all!(s, Branching0FromSclToSda::Stop); let s = s.send(Stop {})?; s.close() } i => { let s: Endpoint1KeyLoop = choose_mpst_sda_to_all!(s, Branching0FromSclToSda::Loop); let s = s.send(BUF {})?; let s = s.send(HDSTA {})?; let s = s.send(SUSTA {})?; let s = s.send(SUSTO {})?; let s = s.send(HDDAT {})?; let s = s.send(SUDAT {})?; recurs_sda(s, i - 1) } } } ///////////////////////// fn main() { let (thread_scl, thread_sda) = fork_mpst(endpoint_scl, endpoint_sda); thread_scl.join().unwrap(); thread_sda.join().unwrap(); println!("Done"); } static LOOPS: i64 = 100;