@@ -3,6 +3,8 @@ use core::sync::atomic::{AtomicBool, Ordering};
33
44use embassy_sync:: blocking_mutex:: Mutex ;
55use rmk_types:: ble:: BleState ;
6+ #[ cfg( feature = "_ble" ) ]
7+ use rmk_types:: ble:: BleStatus ;
68use rmk_types:: connection:: { ConnectionStatus , ConnectionType , UsbState } ;
79
810use crate :: RawMutex ;
@@ -17,6 +19,12 @@ use crate::event::{ConnectionChangeEvent, publish_event};
1719/// is re-established
1820static MATRIX_SCAN_OVERRIDE : AtomicBool = AtomicBool :: new ( false ) ;
1921
22+ /// Single source of truth for transport state and routing. All writes go
23+ /// through the mutator helpers below so the active-output cascade runs and
24+ /// change events fire on every transition.
25+ pub ( crate ) static CONNECTION_STATUS : Mutex < RawMutex , Cell < ConnectionStatus > > =
26+ Mutex :: new ( Cell :: new ( ConnectionStatus :: new ( ) ) ) ;
27+
2028#[ cfg( feature = "_ble" ) ]
2129pub ( crate ) fn enable_matrix_scan_override ( ) {
2230 MATRIX_SCAN_OVERRIDE . store ( true , Ordering :: Release ) ;
@@ -32,18 +40,17 @@ pub(crate) fn input_processing_ready() -> bool {
3240 active_transport ( ) . is_some ( ) || MATRIX_SCAN_OVERRIDE . load ( Ordering :: Acquire ) || cfg ! ( not( feature = "_no_usb" ) )
3341}
3442
35- /// Single source of truth for transport state and routing. All writes go
36- /// through the mutator helpers below so the active-output cascade runs and
37- /// change events fire on every transition.
38- pub ( crate ) static CONNECTION_STATUS : Mutex < RawMutex , Cell < ConnectionStatus > > =
39- Mutex :: new ( Cell :: new ( ConnectionStatus :: new ( ) ) ) ;
40-
4143pub ( crate ) fn active_transport ( ) -> Option < ConnectionType > {
42- connection_status ( ) . decide_active ( )
44+ CONNECTION_STATUS . lock ( |c| c . get ( ) . decide_active ( ) )
4345}
4446
45- pub ( crate ) fn connection_status ( ) -> ConnectionStatus {
46- CONNECTION_STATUS . lock ( |c| c. get ( ) )
47+ pub ( crate ) fn current_usb_state ( ) -> UsbState {
48+ CONNECTION_STATUS . lock ( |c| c. get ( ) . usb )
49+ }
50+
51+ #[ cfg( feature = "_ble" ) ]
52+ pub ( crate ) fn current_ble_status ( ) -> BleStatus {
53+ CONNECTION_STATUS . lock ( |c| c. get ( ) . ble )
4754}
4855
4956/// Read-modify-write the connection status atomically.
@@ -63,7 +70,7 @@ fn update_status(f: impl FnOnce(&mut ConnectionStatus)) {
6370
6471 let prev_active = prev. decide_active ( ) ;
6572 let new_active = new. decide_active ( ) ;
66- // TODO: Is it really needed?
73+
6774 if prev_active != new_active
6875 && let Some ( prev_active) = prev_active
6976 {
@@ -82,6 +89,7 @@ fn update_status(f: impl FnOnce(&mut ConnectionStatus)) {
8289 }
8390}
8491
92+ #[ cfg( not( feature = "std" ) ) ]
8593pub fn set_usb_state ( s : UsbState ) {
8694 update_status ( |c| c. usb = s) ;
8795}
@@ -129,7 +137,8 @@ pub(crate) async fn load_preferred_connection() -> ConnectionType {
129137 }
130138}
131139
132- pub ( crate ) fn toggle_preferred ( ) -> ConnectionType {
140+ #[ cfg( all( feature = "_ble" , not( feature = "_no_usb" ) ) ) ]
141+ pub ( crate ) async fn toggle_preferred ( ) {
133142 let mut new = ConnectionType :: Usb ;
134143 update_status ( |c| {
135144 c. preferred = match c. preferred {
@@ -138,12 +147,16 @@ pub(crate) fn toggle_preferred() -> ConnectionType {
138147 } ;
139148 new = c. preferred ;
140149 } ) ;
141- new
150+ info ! ( "Switching preferred transport to: {:?}" , new) ;
151+ #[ cfg( feature = "storage" ) ]
152+ crate :: channel:: FLASH_CHANNEL
153+ . send ( crate :: storage:: FlashOperationMessage :: ConnectionType ( new) )
154+ . await ;
142155}
143156
144157#[ cfg( feature = "_ble" ) ]
145158pub ( crate ) fn current_profile ( ) -> u8 {
146- connection_status ( ) . ble . profile
159+ CONNECTION_STATUS . lock ( |c| c . get ( ) . ble . profile )
147160}
148161
149162#[ cfg( test) ]
0 commit comments