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

Commit 8a8b663d authored by Katherine Lai's avatar Katherine Lai
Browse files

Floss: Implement Phy APIs for GATT Server

This implements IBluetoothGatt's ServerSetPreferredPhy and
ServerReadPhy as well as IBluetoothGattServerCallback's
OnPhyUpdate and OnPhyRead.

Bug: 193685149
Tag: #floss
Test: emerge-dedede floss and WIP btclient
Change-Id: I4b88b7cc6d09083e1c854e23016d5b3903302753
parent 093d5818
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -878,6 +878,26 @@ impl IBluetoothGattServerCallback for BtGattServerCallback {
            status
        );
    }

    fn on_phy_update(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus) {
        print_info!(
            "GATT server phy updated for addr = {}: tx_phy = {:?}, rx_phy = {:?}, status = {}",
            addr,
            tx_phy,
            rx_phy,
            status
        );
    }

    fn on_phy_read(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus) {
        print_info!(
            "GATT server phy read for addr = {}: tx_phy = {:?}, rx_phy = {:?}, status = {}",
            addr,
            tx_phy,
            rx_phy,
            status
        );
    }
}

impl RPCProxy for BtGattServerCallback {
+23 −0
Original line number Diff line number Diff line
@@ -1367,6 +1367,23 @@ impl IBluetoothGatt for BluetoothGattDBus {
    ) -> bool {
        dbus_generated!()
    }

    #[dbus_method("ServerSetPreferredPhy")]
    fn server_set_preferred_phy(
        &self,
        server_id: i32,
        addr: String,
        tx_phy: LePhy,
        rx_phy: LePhy,
        phy_options: i32,
    ) {
        dbus_generated!()
    }

    #[dbus_method("ServerReadPhy")]
    fn server_read_phy(&self, server_id: i32, addr: String) {
        dbus_generated!()
    }
}

struct IBluetoothGattCallbackDBus {}
@@ -1521,6 +1538,12 @@ impl IBluetoothGattServerCallback for IBluetoothGattCallbackDBus {

    #[dbus_method("OnNotificationSent")]
    fn on_notification_sent(&self, addr: String, status: GattStatus) {}

    #[dbus_method("OnPhyUpdate")]
    fn on_phy_update(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus) {}

    #[dbus_method("OnPhyRead")]
    fn on_phy_read(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus) {}
}

#[dbus_propmap(BluetoothServerSocket)]
+27 −0
Original line number Diff line number Diff line
@@ -219,6 +219,16 @@ impl IBluetoothGattServerCallback for BluetoothGattServerCallbackDBus {
    fn on_notification_sent(&self, addr: String, status: GattStatus) {
        dbus_generated!()
    }

    #[dbus_method("OnPhyUpdate")]
    fn on_phy_update(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus) {
        dbus_generated!()
    }

    #[dbus_method("OnPhyRead")]
    fn on_phy_read(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus) {
        dbus_generated!()
    }
}

// Represents Uuid128Bit as an array in D-Bus.
@@ -910,4 +920,21 @@ impl IBluetoothGatt for IBluetoothGattDBus {
    ) -> bool {
        dbus_generated!()
    }

    #[dbus_method("ServerSetPreferredPhy")]
    fn server_set_preferred_phy(
        &self,
        server_id: i32,
        addr: String,
        tx_phy: LePhy,
        rx_phy: LePhy,
        phy_options: i32,
    ) {
        dbus_generated!()
    }

    #[dbus_method("ServerReadPhy")]
    fn server_read_phy(&self, server_id: i32, addr: String) {
        dbus_generated!()
    }
}
+100 −0
Original line number Diff line number Diff line
@@ -658,6 +658,19 @@ pub trait IBluetoothGatt {
        confirm: bool,
        value: Vec<u8>,
    ) -> bool;

    /// Sets preferred PHY.
    fn server_set_preferred_phy(
        &self,
        server_id: i32,
        addr: String,
        tx_phy: LePhy,
        rx_phy: LePhy,
        phy_options: i32,
    );

    /// Reads the PHY used by a peer.
    fn server_read_phy(&self, server_id: i32, addr: String);
}

#[derive(Debug, Default, Clone)]
@@ -1007,6 +1020,12 @@ pub trait IBluetoothGattServerCallback: RPCProxy {

    /// When a notification or indication has been sent to a remote device.
    fn on_notification_sent(&self, _addr: String, _status: GattStatus);

    /// When there is a change of PHY.
    fn on_phy_update(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus);

    /// The completion of IBluetoothGatt::server_read_phy.
    fn on_phy_read(&self, addr: String, tx_phy: LePhy, rx_phy: LePhy, status: GattStatus);
}

/// Interface for scanner callbacks to clients, passed to
@@ -2454,6 +2473,34 @@ impl IBluetoothGatt for BluetoothGatt {

        true
    }

    fn server_set_preferred_phy(
        &self,
        server_id: i32,
        addr: String,
        tx_phy: LePhy,
        rx_phy: LePhy,
        phy_options: i32,
    ) {
        (|| {
            let address = RawAddress::from_string(addr)?;

            self.gatt.as_ref().unwrap().lock().unwrap().server.set_preferred_phy(
                &address,
                tx_phy.to_u8().unwrap_or_default(),
                rx_phy.to_u8().unwrap_or_default(),
                phy_options as u16,
            );

            Some(())
        })();
    }

    fn server_read_phy(&self, server_id: i32, addr: String) {
        if let Some(address) = RawAddress::from_string(addr.clone()) {
            self.gatt.as_ref().unwrap().lock().unwrap().server.read_phy(server_id, &address);
        }
    }
}

#[btif_callbacks_dispatcher(dispatch_gatt_client_callbacks, GattClientCallbacks)]
@@ -3079,6 +3126,19 @@ pub(crate) trait BtifGattServerCallbacks {

    #[btif_callback(Congestion)]
    fn congestion_cb(&mut self, conn_id: i32, congested: bool);

    #[btif_callback(PhyUpdated)]
    fn phy_updated_cb(&mut self, conn_id: i32, tx_phy: u8, rx_phy: u8, status: GattStatus);

    #[btif_callback(ReadPhy)]
    fn read_phy_cb(
        &mut self,
        server_id: i32,
        addr: RawAddress,
        tx_phy: u8,
        rx_phy: u8,
        status: GattStatus,
    );
}

impl BtifGattServerCallbacks for BluetoothGatt {
@@ -3316,6 +3376,46 @@ impl BtifGattServerCallbacks for BluetoothGatt {
            }
        }
    }

    fn phy_updated_cb(&mut self, conn_id: i32, tx_phy: u8, rx_phy: u8, status: GattStatus) {
        (|| {
            let address = self.server_context_map.get_address_from_conn_id(conn_id)?;
            let server_cbid = self.server_context_map.get_by_conn_id(conn_id)?.cbid;

            if let Some(cb) = self.server_context_map.get_callback_from_callback_id(server_cbid) {
                cb.on_phy_update(
                    address,
                    LePhy::from_u8(tx_phy).unwrap_or_default(),
                    LePhy::from_u8(rx_phy).unwrap_or_default(),
                    status,
                );
            }

            Some(())
        })();
    }

    fn read_phy_cb(
        &mut self,
        server_id: i32,
        addr: RawAddress,
        tx_phy: u8,
        rx_phy: u8,
        status: GattStatus,
    ) {
        if let Some(cbid) =
            self.server_context_map.get_by_server_id(server_id).map(|server| server.cbid)
        {
            if let Some(cb) = self.server_context_map.get_callback_from_callback_id(cbid) {
                cb.on_phy_read(
                    addr.to_string(),
                    LePhy::from_u8(tx_phy).unwrap_or_default(),
                    LePhy::from_u8(rx_phy).unwrap_or_default(),
                    status,
                );
            }
        }
    }
}

#[btif_callbacks_dispatcher(dispatch_le_scanner_callbacks, GattScannerCallbacks)]
+2 −2
Original line number Diff line number Diff line
@@ -615,7 +615,7 @@ pub enum GattServerCallbacks {
    IndicationSent(i32, GattStatus),
    Congestion(i32, bool),
    MtuChanged(i32, i32),
    PhyUpdated(i32, u8, u8, u8),
    PhyUpdated(i32, u8, u8, GattStatus),
    ConnUpdated(i32, u16, u16, u16, u8),
    ReadPhy(i32, RawAddress, u8, u8, GattStatus),
}
@@ -865,7 +865,7 @@ cb_variant!(
cb_variant!(
    GattServerCb,
    gs_phy_updated_cb -> GattServerCallbacks::PhyUpdated,
    i32, u8, u8, u8, {}
    i32, u8, u8, u8 -> GattStatus, {}
);

cb_variant!(