Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4d33d48f authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Emit callbacks when enabled/disabled in manager"

parents 55af75a9 f77e3d09
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -257,4 +257,7 @@ impl manager_service::RPCProxy for IBluetoothManagerCallbackDBus {
impl IBluetoothManagerCallback for IBluetoothManagerCallbackDBus {
    #[dbus_method("OnHciDeviceChanged")]
    fn on_hci_device_changed(&self, hci_interface: i32, present: bool) {}

    #[dbus_method("OnHciEnabledChanged")]
    fn on_hci_enabled_changed(&self, hci_interface: i32, enabled: bool) {}
}
+4 −0
Original line number Diff line number Diff line
@@ -30,6 +30,10 @@ impl IBluetoothManagerCallback for BtManagerCallback {
    fn on_hci_device_changed(&self, hci_interface: i32, present: bool) {
        print_info!("hci{} present = {}", hci_interface, present);
    }

    fn on_hci_enabled_changed(&self, hci_interface: i32, enabled: bool) {
        print_info!("hci{} enabled = {}", hci_interface, enabled);
    }
}

impl manager_service::RPCProxy for BtManagerCallback {
+6 −0
Original line number Diff line number Diff line
@@ -25,6 +25,12 @@ impl BluetoothManager {
            callback.on_hci_device_changed(hci_device, present);
        }
    }

    pub(crate) fn callback_hci_enabled_change(&self, hci_device: i32, enabled: bool) {
        for callback in &self.callbacks {
            callback.on_hci_enabled_changed(hci_device, enabled);
        }
    }
}

impl IBluetoothManager for BluetoothManager {
+3 −0
Original line number Diff line number Diff line
@@ -50,4 +50,7 @@ struct BluetoothManagerCallbackDBus {}
impl IBluetoothManagerCallback for BluetoothManagerCallbackDBus {
    #[dbus_method("OnHciDeviceChanged")]
    fn on_hci_device_changed(&self, hci_interface: i32, present: bool) {}

    #[dbus_method("OnHciEnabledChanged")]
    fn on_hci_enabled_changed(&self, hci_interface: i32, enabled: bool) {}
}
+33 −6
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ pub enum StateMachineActions {
    StartBluetooth(i32),
    StopBluetooth(i32),
    BluetoothStarted(i32, i32), // PID and HCI
    BluetoothStopped(),
    BluetoothStopped(i32),
}

pub struct StateMachineContext<PM> {
@@ -136,8 +136,19 @@ pub async fn mainloop<PM>(
    loop {
        tokio::select! {
            Some(action) = context.rx.recv() => {
              // Grab previous state from lock and release
              let mut next_state;
              let mut prev_state;
              {
                  prev_state = *context.state_machine.state.lock().await;
              }
              let mut hci = 0;

              match action {
                StateMachineActions::StartBluetooth(i) => {
                    next_state = State::TurningOn;
                    hci = i;

                    match context.state_machine.action_start_bluetooth(i).await {
                        true => {
                            command_timeout.reset(COMMAND_TIMEOUT_DURATION);
@@ -146,6 +157,9 @@ pub async fn mainloop<PM>(
                    }
                },
                StateMachineActions::StopBluetooth(i) => {
                    next_state = State::TurningOff;
                    hci = i;

                    match context.state_machine.action_stop_bluetooth(i).await {
                      true => {
                          command_timeout.reset(COMMAND_TIMEOUT_DURATION);
@@ -153,7 +167,10 @@ pub async fn mainloop<PM>(
                      false => command_timeout.cancel(),
                  }
                },
                StateMachineActions::BluetoothStarted(pid, hci) => {
                StateMachineActions::BluetoothStarted(pid, i) => {
                  next_state = State::On;
                  hci = i;

                  match context.state_machine.action_on_bluetooth_started(pid, hci).await {
                      true => {
                          command_timeout.cancel();
@@ -161,7 +178,10 @@ pub async fn mainloop<PM>(
                      false => error!("unexpected BluetoothStarted pid{} hci{}", pid, hci),
                  }
                },
                StateMachineActions::BluetoothStopped() => {
                StateMachineActions::BluetoothStopped(i) => {
                  next_state = State::Off;
                  hci = i;

                  match context.state_machine.action_on_bluetooth_stopped().await {
                      true => {
                          command_timeout.cancel();
@@ -171,7 +191,14 @@ pub async fn mainloop<PM>(
                      }
                  }
                },
              };

              // Only emit enabled event for certain transitions
              if next_state != prev_state &&
                 (next_state == State::On || prev_state == State::On) {
                  bluetooth_manager.lock().unwrap().callback_hci_enabled_change(hci, next_state == State::On);
              }

            },
            _expired = command_timeout.expired() => {
                info!("expired {:?}", *context.state_machine.state.lock().await);
@@ -202,8 +229,8 @@ pub async fn mainloop<PM>(
                                },
                                (inotify::EventMask::DELETE, Some(oss)) => {
                                    let file_name = oss.to_str().unwrap_or("invalid file");
                                    if let Some(_hci) = get_hci_interface_from_pid_file_name(file_name) {
                                        context.tx.send_timeout(StateMachineActions::BluetoothStopped(), TX_SEND_TIMEOUT_DURATION).await.unwrap();
                                    if let Some(hci) = get_hci_interface_from_pid_file_name(file_name) {
                                        context.tx.send_timeout(StateMachineActions::BluetoothStopped(hci), TX_SEND_TIMEOUT_DURATION).await.unwrap();
                                    }
                                },
                                _ => debug!("Ignored event {:?}", event.mask)
Loading