Loading system/gd/rust/linux/mgmt/src/state_machine.rs +25 −18 Original line number Diff line number Diff line Loading @@ -621,9 +621,9 @@ pub async fn mainloop( AdapterStateActions::StartBluetooth(i) => { hci = *i; prev_state = context.state_machine.get_process_state(hci); next_state = ProcessState::TurningOn; let action = context.state_machine.action_start_bluetooth(hci); let action; (next_state, action) = context.state_machine.action_start_bluetooth(hci); cmd_timeout.lock().unwrap().handle_timeout_action(hci, action); } AdapterStateActions::StopBluetooth(i) => { Loading Loading @@ -693,9 +693,12 @@ pub async fn mainloop( prev_state = context.state_machine.get_process_state(hci); let adapter_change_action; (next_state, adapter_change_action) = let timeout_action; (next_state, adapter_change_action, timeout_action) = context.state_machine.action_on_hci_presence_changed(hci, *present); cmd_timeout.lock().unwrap().handle_timeout_action(hci, timeout_action); match adapter_change_action { AdapterChangeAction::NewDefaultAdapter(new_hci) => { context Loading Loading @@ -1247,7 +1250,10 @@ impl StateMachineInternal { } /// Returns an action to reset timer if we are starting bluetooth process. pub fn action_start_bluetooth(&mut self, hci: VirtualHciIndex) -> CommandTimeoutAction { pub fn action_start_bluetooth( &mut self, hci: VirtualHciIndex, ) -> (ProcessState, CommandTimeoutAction) { let state = self.get_process_state(hci); let present = self.get_state(hci, move |a: &AdapterState| Some(a.present)).unwrap_or(false); let floss_enabled = self.get_floss_enabled(); Loading @@ -1262,10 +1268,10 @@ impl StateMachineInternal { }); self.process_manager .start(hci.to_string(), self.get_real_hci_by_virtual_id(hci).to_string()); CommandTimeoutAction::ResetTimer (ProcessState::TurningOn, CommandTimeoutAction::ResetTimer) } // Otherwise no op _ => CommandTimeoutAction::DoNothing, _ => (state, CommandTimeoutAction::DoNothing), } } Loading Loading @@ -1499,19 +1505,19 @@ impl StateMachineInternal { &mut self, hci: VirtualHciIndex, present: bool, ) -> (ProcessState, AdapterChangeAction) { ) -> (ProcessState, AdapterChangeAction, CommandTimeoutAction) { let prev_present = self.get_state(hci, |a: &AdapterState| Some(a.present)).unwrap_or(false); let prev_state = self.get_process_state(hci); // No-op if same as previous present. if prev_present == present { return (prev_state, AdapterChangeAction::DoNothing); return (prev_state, AdapterChangeAction::DoNothing, CommandTimeoutAction::DoNothing); } self.modify_state(hci, |a: &mut AdapterState| a.present = present); let floss_enabled = self.get_floss_enabled(); let next_state = let (next_state, timeout_action) = match self.get_state(hci, |a: &AdapterState| Some((a.state, a.config_enabled))) { // Start the adapter if present, config is enabled and floss is enabled. Some((ProcessState::Off, true)) if floss_enabled && present => { Loading @@ -1522,10 +1528,9 @@ impl StateMachineInternal { // Both should reset the restart count. self.modify_state(hci, |a: &mut AdapterState| a.restart_count = 0); self.action_start_bluetooth(hci); ProcessState::TurningOn self.action_start_bluetooth(hci) } _ => prev_state, _ => (prev_state, CommandTimeoutAction::DoNothing), }; let default_adapter = VirtualHciIndex(self.default_adapter.load(Ordering::Relaxed)); Loading @@ -1537,16 +1542,18 @@ impl StateMachineInternal { // 2) The current default adapter is no longer present or enabled. // * Switch to the lowest numbered adapter present or do nothing. // return if present && hci == desired_adapter && hci != default_adapter { (next_state, AdapterChangeAction::NewDefaultAdapter(desired_adapter)) let adapter_change_action = if present && hci == desired_adapter && hci != default_adapter { AdapterChangeAction::NewDefaultAdapter(desired_adapter) } else if !present && hci == default_adapter { match self.get_lowest_available_adapter() { Some(v) => (next_state, AdapterChangeAction::NewDefaultAdapter(v)), None => (next_state, AdapterChangeAction::DoNothing), Some(v) => AdapterChangeAction::NewDefaultAdapter(v), None => AdapterChangeAction::DoNothing, } } else { (next_state, AdapterChangeAction::DoNothing) AdapterChangeAction::DoNothing }; (next_state, adapter_change_action, timeout_action) } } Loading Loading @@ -1681,7 +1688,7 @@ mod tests { state_machine.action_start_bluetooth(DEFAULT_ADAPTER); assert_eq!( state_machine.action_start_bluetooth(DEFAULT_ADAPTER), CommandTimeoutAction::ResetTimer (ProcessState::TurningOn, CommandTimeoutAction::ResetTimer) ); }) } Loading Loading
system/gd/rust/linux/mgmt/src/state_machine.rs +25 −18 Original line number Diff line number Diff line Loading @@ -621,9 +621,9 @@ pub async fn mainloop( AdapterStateActions::StartBluetooth(i) => { hci = *i; prev_state = context.state_machine.get_process_state(hci); next_state = ProcessState::TurningOn; let action = context.state_machine.action_start_bluetooth(hci); let action; (next_state, action) = context.state_machine.action_start_bluetooth(hci); cmd_timeout.lock().unwrap().handle_timeout_action(hci, action); } AdapterStateActions::StopBluetooth(i) => { Loading Loading @@ -693,9 +693,12 @@ pub async fn mainloop( prev_state = context.state_machine.get_process_state(hci); let adapter_change_action; (next_state, adapter_change_action) = let timeout_action; (next_state, adapter_change_action, timeout_action) = context.state_machine.action_on_hci_presence_changed(hci, *present); cmd_timeout.lock().unwrap().handle_timeout_action(hci, timeout_action); match adapter_change_action { AdapterChangeAction::NewDefaultAdapter(new_hci) => { context Loading Loading @@ -1247,7 +1250,10 @@ impl StateMachineInternal { } /// Returns an action to reset timer if we are starting bluetooth process. pub fn action_start_bluetooth(&mut self, hci: VirtualHciIndex) -> CommandTimeoutAction { pub fn action_start_bluetooth( &mut self, hci: VirtualHciIndex, ) -> (ProcessState, CommandTimeoutAction) { let state = self.get_process_state(hci); let present = self.get_state(hci, move |a: &AdapterState| Some(a.present)).unwrap_or(false); let floss_enabled = self.get_floss_enabled(); Loading @@ -1262,10 +1268,10 @@ impl StateMachineInternal { }); self.process_manager .start(hci.to_string(), self.get_real_hci_by_virtual_id(hci).to_string()); CommandTimeoutAction::ResetTimer (ProcessState::TurningOn, CommandTimeoutAction::ResetTimer) } // Otherwise no op _ => CommandTimeoutAction::DoNothing, _ => (state, CommandTimeoutAction::DoNothing), } } Loading Loading @@ -1499,19 +1505,19 @@ impl StateMachineInternal { &mut self, hci: VirtualHciIndex, present: bool, ) -> (ProcessState, AdapterChangeAction) { ) -> (ProcessState, AdapterChangeAction, CommandTimeoutAction) { let prev_present = self.get_state(hci, |a: &AdapterState| Some(a.present)).unwrap_or(false); let prev_state = self.get_process_state(hci); // No-op if same as previous present. if prev_present == present { return (prev_state, AdapterChangeAction::DoNothing); return (prev_state, AdapterChangeAction::DoNothing, CommandTimeoutAction::DoNothing); } self.modify_state(hci, |a: &mut AdapterState| a.present = present); let floss_enabled = self.get_floss_enabled(); let next_state = let (next_state, timeout_action) = match self.get_state(hci, |a: &AdapterState| Some((a.state, a.config_enabled))) { // Start the adapter if present, config is enabled and floss is enabled. Some((ProcessState::Off, true)) if floss_enabled && present => { Loading @@ -1522,10 +1528,9 @@ impl StateMachineInternal { // Both should reset the restart count. self.modify_state(hci, |a: &mut AdapterState| a.restart_count = 0); self.action_start_bluetooth(hci); ProcessState::TurningOn self.action_start_bluetooth(hci) } _ => prev_state, _ => (prev_state, CommandTimeoutAction::DoNothing), }; let default_adapter = VirtualHciIndex(self.default_adapter.load(Ordering::Relaxed)); Loading @@ -1537,16 +1542,18 @@ impl StateMachineInternal { // 2) The current default adapter is no longer present or enabled. // * Switch to the lowest numbered adapter present or do nothing. // return if present && hci == desired_adapter && hci != default_adapter { (next_state, AdapterChangeAction::NewDefaultAdapter(desired_adapter)) let adapter_change_action = if present && hci == desired_adapter && hci != default_adapter { AdapterChangeAction::NewDefaultAdapter(desired_adapter) } else if !present && hci == default_adapter { match self.get_lowest_available_adapter() { Some(v) => (next_state, AdapterChangeAction::NewDefaultAdapter(v)), None => (next_state, AdapterChangeAction::DoNothing), Some(v) => AdapterChangeAction::NewDefaultAdapter(v), None => AdapterChangeAction::DoNothing, } } else { (next_state, AdapterChangeAction::DoNothing) AdapterChangeAction::DoNothing }; (next_state, adapter_change_action, timeout_action) } } Loading Loading @@ -1681,7 +1688,7 @@ mod tests { state_machine.action_start_bluetooth(DEFAULT_ADAPTER); assert_eq!( state_machine.action_start_bluetooth(DEFAULT_ADAPTER), CommandTimeoutAction::ResetTimer (ProcessState::TurningOn, CommandTimeoutAction::ResetTimer) ); }) } Loading