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

Commit 3aab56f4 authored by Rahul Arya's avatar Rahul Arya
Browse files

[Private GATT] Add GATT server + module

Bug: 255880936
Test: unit
Change-Id: I94fafe8afc68dc31501f274099ef6f3a684009a1
parent fa7f1732
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ rust_defaults {
    ],
    rustlibs: [
        "liblog_rust",
        "libanyhow",
        "libcxx",
        "libtokio",
        "libbt_common",
+2 −1
Original line number Diff line number Diff line
@@ -22,7 +22,8 @@ edition = "2021"
bt_common = { path = "../gd/rust/common", default-features = false }

# External dependencies
# Note: source-of-truth is Android.bp, these are mirrored solely for IDE convenient
# Note: source-of-truth is Android.bp, these are mirrored solely for IDE convenience
anyhow = "1.0"
log = "*"
cxx = "*"
android_logger = "*"
+2 −0
Original line number Diff line number Diff line
//! This module is a simple GATT server that shares the ATT channel with the
//! existing C++ GATT client. See go/private-gatt-in-platform for the design.

pub mod channel;
pub mod ids;
pub mod mocks;
pub mod server;
+22 −0
Original line number Diff line number Diff line
//! This represents the TX end of an ATT Transport, to be either mocked (in
//! test) or linked to FFI (in production).

use crate::packets::{AttBuilder, SerializeError};

use super::ids::TransportIndex;

/// An instance of this trait will be provided to the GattModule on
/// initialization.
pub trait AttTransport {
    /// Serializes and sends a packet to the device associated with the
    /// specified transport. Note that the packet may be dropped if the link
    /// is disconnected, but the result will still be Ok(()).
    ///
    /// The tcb_idx is an identifier for this transport supplied from the
    /// native stack, and represents an underlying ACL-LE connection.
    fn send_packet(
        &self,
        tcb_idx: TransportIndex,
        packet: AttBuilder,
    ) -> Result<(), SerializeError>;
}
+39 −0
Original line number Diff line number Diff line
//! These are strongly-typed identifiers representing the various objects
//! interacted with, mostly over FFI

/// The ID of a connection at the GATT layer.
/// A ConnectionId is logically a (TransportIndex, ServerId) tuple,
/// where each contribute 8 bits to the 16-bit value.
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq, PartialOrd, Ord)]
pub struct ConnectionId(pub u16);

impl ConnectionId {
    /// Create a ConnectionId from a TransportIndex and ServerId
    pub const fn new(tcb_idx: TransportIndex, server_id: ServerId) -> ConnectionId {
        ConnectionId(((tcb_idx.0 as u16) << 8) + (server_id.0 as u16))
    }

    /// Extract the TransportIndex from a ConnectionId (upper 8 bits)
    pub fn get_tcb_idx(&self) -> TransportIndex {
        TransportIndex((self.0 >> 8) as u8)
    }

    /// Extract the ServerId from a ConnectionId (lower 8 bits)
    pub fn get_server_id(&self) -> ServerId {
        ServerId((self.0 & (u8::MAX as u16)) as u8)
    }
}

/// The server_if of a GATT server registered in legacy
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub struct ServerId(pub u8);

/// An arbitrary id representing a GATT transaction (request/response)
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub struct TransactionId(pub u32);

/// The TCB index in legacy GATT
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub struct TransportIndex(pub u8);

/// An advertising set ID (zero-based)
#[derive(Debug, Copy, Clone, PartialEq, Hash, Eq)]
pub struct AdvertiserId(pub u8);

/// The handle of a given ATT attribute
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct AttHandle(pub u16);
Loading