@@ -21,7 +21,7 @@ use embassy_sync::blocking_mutex::raw::NoopRawMutex;
2121use embassy_sync:: channel:: Channel ;
2222use embassy_sync:: mutex:: Mutex ;
2323
24- use demo_common:: { takepipe, DemoServer , SSHConfig , ServerApp } ;
24+ use demo_common:: { takepipe, DemoCommon , DemoServer , SSHConfig } ;
2525use sunset:: * ;
2626use sunset_demo_embassy_common as demo_common;
2727use sunset_embassy:: { ProgressHolder , SSHServer , SunsetMutex } ;
@@ -107,8 +107,8 @@ async fn main(spawner: Spawner) {
107107
108108 // state depends on the mac for cyw43 vs w5500
109109 let net_mac = SunsetMutex :: new ( [ 0 ; 6 ] ) ;
110- static STATE : StaticCell < GlobalState > = StaticCell :: new ( ) ;
111- let state = STATE . init_with ( || GlobalState {
110+ static STATE : StaticCell < PicoDemo > = StaticCell :: new ( ) ;
111+ let state = STATE . init_with ( || PicoDemo {
112112 usb_pipe,
113113 serial1_pipe,
114114 config,
@@ -158,12 +158,16 @@ async fn main(spawner: Spawner) {
158158async fn net_listener (
159159 stack : Stack < ' static > ,
160160 config : & ' static SunsetMutex < SSHConfig > ,
161- global : & ' static GlobalState ,
161+ demo : & ' static PicoDemo ,
162162) -> ! {
163- demo_common:: listener :: < PicoServer > ( stack, config, global ) . await
163+ demo_common:: listen ( stack, config, & demo ) . await
164164}
165165
166- pub ( crate ) struct GlobalState {
166+ /// Shared state for the device.
167+ ///
168+ /// This is shared between all connections.
169+ /// `PicoDemo` implements `DemoServer` to handle incoming connections.
170+ pub ( crate ) struct PicoDemo {
167171 // If locking multiple mutexes, always lock in the order below to avoid inversion.
168172 pub usb_pipe : & ' static TakePipe < ' static > ,
169173 pub serial1_pipe : & ' static TakePipe < ' static > ,
@@ -175,18 +179,14 @@ pub(crate) struct GlobalState {
175179 pub net_mac : SunsetMutex < [ u8 ; 6 ] > ,
176180}
177181
178- struct PicoServer {
179- global : & ' static GlobalState ,
180- }
181-
182182// Presents a menu, either on serial or incoming SSH
183183//
184184// `local` is set for usb serial menus which require different auth
185185async fn menu < R , W > (
186186 chanr : & mut R ,
187187 chanw : & mut W ,
188188 local : bool ,
189- state : & ' static GlobalState ,
189+ state : & ' static PicoDemo ,
190190) -> Result < ( ) >
191191where
192192 R : Read < Error = sunset:: Error > ,
@@ -288,14 +288,9 @@ where
288288 info ! ( "serial task completed" ) ;
289289 Ok ( ( ) )
290290}
291- impl DemoServer for PicoServer {
292- type Init = & ' static GlobalState ;
293-
294- fn new ( global : & Self :: Init ) -> Self {
295- Self { global }
296- }
297291
298- async fn run ( & self , serv : & SSHServer < ' _ > , mut common : ServerApp ) -> Result < ( ) > {
292+ impl DemoServer for & ' static PicoDemo {
293+ async fn run ( & self , serv : & SSHServer < ' _ > , mut common : DemoCommon ) -> Result < ( ) > {
299294 let username = Mutex :: < NoopRawMutex , _ > :: new ( String :: < 20 > :: new ( ) ) ;
300295 let chan_pipe = Channel :: < NoopRawMutex , ChanHandle , 1 > :: new ( ) ;
301296
@@ -337,14 +332,14 @@ impl DemoServer for PicoServer {
337332 #[ cfg( feature = "serial1" ) ]
338333 let default_pipe = self . global . serial1_pipe ;
339334 #[ cfg( not( feature = "serial1" ) ) ]
340- let default_pipe = self . global . usb_pipe ;
335+ let default_pipe = self . usb_pipe ;
341336
342337 let mut stdio2 = stdio. clone ( ) ;
343338 match username. lock ( ) . await . as_str ( ) {
344339 // Open the config menu
345- "config" => menu ( & mut stdio, & mut stdio2, false , self . global ) . await ,
340+ "config" => menu ( & mut stdio, & mut stdio2, false , self ) . await ,
346341 // Open the usb serial directly
347- "usb" => serial ( & mut stdio, & mut stdio2, self . global . usb_pipe ) . await ,
342+ "usb" => serial ( & mut stdio, & mut stdio2, self . usb_pipe ) . await ,
348343 // Open the normal (?) serial directly (either usb or uart)
349344 _ => serial ( & mut stdio, & mut stdio2, default_pipe) . await ,
350345 }
0 commit comments