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

Commit 6769e23f authored by Ying Hsu's avatar Ying Hsu Committed by Gerrit Code Review
Browse files

Merge "floss: Refactor lock usage for advertising in btclient"

parents 008f2788 8ac01a90
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -436,15 +436,17 @@ impl IAdvertisingSetCallback for AdvertisingSetCallback {
            tx_power,
            status
        );

        let mut context = self.context.lock().unwrap();
        if status != GattStatus::Success {
            print_error!(
                "on_advertising_set_started: remove advertising set registered ({})",
                reg_id
            );
            self.context.lock().unwrap().adv_sets.remove(&reg_id);
            context.adv_sets.remove(&reg_id);
            return;
        }
        if let Some(s) = self.context.lock().unwrap().adv_sets.get_mut(&reg_id) {
        if let Some(s) = context.adv_sets.get_mut(&reg_id) {
            s.adv_id = Some(advertiser_id);
        } else {
            print_error!("on_advertising_set_started: invalid callback for reg_id={}", reg_id);
+33 −53
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ use crate::{console_red, console_yellow, print_error, print_info};
use bt_topshim::btif::{BtConnectionState, BtStatus, BtTransport};
use bt_topshim::profiles::gatt::LePhy;
use btstack::bluetooth::{BluetoothDevice, IBluetooth, IBluetoothQA};
use btstack::bluetooth_adv::{AdvertiserId, RegId};
use btstack::bluetooth_gatt::{
    IBluetoothGatt, ScanFilter, ScanFilterCondition, ScanSettings, ScanType,
};
@@ -942,14 +941,15 @@ impl CommandHandler {

        enforce_arg_len(args, 1, "advertise <on|off|set-interval>", || match &args[0][0..] {
            "on" => {
                if self.context.lock().unwrap().adv_sets.keys().len() > 0 {
                let mut context = self.context.lock().unwrap();

                if context.adv_sets.keys().len() > 0 {
                    print_error!("Already started advertising");
                    return;
                }

                let s = AdvSet::new();
                let reg_id =
                    self.context.lock().unwrap().gatt_dbus.as_mut().unwrap().start_advertising_set(
                let reg_id = context.gatt_dbus.as_mut().unwrap().start_advertising_set(
                    s.params.clone(),
                    s.data.clone(),
                    None,
@@ -960,28 +960,18 @@ impl CommandHandler {
                    callback_id,
                );
                print_info!("Starting advertising set for reg_id = {}", reg_id);
                self.context.lock().unwrap().adv_sets.insert(reg_id, s);
                context.adv_sets.insert(reg_id, s);
            }
            "off" => {
                let adv_ids: Vec<AdvertiserId> = self
                    .context
                    .lock()
                    .unwrap()
                    .adv_sets
                    .iter()
                    .filter_map(|(_, s)| s.adv_id)
                    .collect();
                let mut context = self.context.lock().unwrap();

                let adv_ids: Vec<_> =
                    context.adv_sets.iter().filter_map(|(_, s)| s.adv_id).collect();
                for adv_id in adv_ids {
                    print_info!("Stopping advertising set {}", adv_id);
                    self.context
                        .lock()
                        .unwrap()
                        .gatt_dbus
                        .as_mut()
                        .unwrap()
                        .stop_advertising_set(adv_id);
                    context.gatt_dbus.as_mut().unwrap().stop_advertising_set(adv_id);
                }
                self.context.lock().unwrap().adv_sets.clear();
                context.adv_sets.clear();
            }
            "set-interval" => {
                if args.len() < 2 {
@@ -995,30 +985,20 @@ impl CommandHandler {
                }
                let interval = ms.unwrap() * 8 / 5; // in 0.625 ms.

                let reg_ids: Vec<RegId> =
                    self.context.lock().unwrap().adv_sets.keys().copied().collect();
                for reg_id in reg_ids {
                    self.context
                        .lock()
                        .unwrap()
                        .adv_sets
                        .get_mut(&reg_id)
                        .unwrap()
                        .params
                        .interval = interval;
                let mut context = self.context.lock().unwrap();
                context.adv_sets.iter_mut().for_each(|(_, s)| s.params.interval = interval);

                    let adv_id = self.context.lock().unwrap().adv_sets[&reg_id].adv_id.clone();
                    if let Some(adv_id) = adv_id {
                        let params = self.context.lock().unwrap().adv_sets[&reg_id].params.clone();
                // To avoid borrowing context as mutable from an immutable borrow.
                // Required information is collected in advance and then passed
                // to the D-Bus call which requires a mutable borrow.
                let advs: Vec<(_, _)> = context
                    .adv_sets
                    .iter()
                    .filter_map(|(_, s)| s.adv_id.map(|adv_id| (adv_id.clone(), s.params.clone())))
                    .collect();
                for (adv_id, params) in advs {
                    print_info!("Setting advertising parameters for {}", adv_id);
                        self.context
                            .lock()
                            .unwrap()
                            .gatt_dbus
                            .as_mut()
                            .unwrap()
                            .set_advertising_parameters(adv_id, params);
                    }
                    context.gatt_dbus.as_mut().unwrap().set_advertising_parameters(adv_id, params);
                }
            }
            _ => {