summaryrefslogtreecommitdiff
path: root/system/rust/src/gatt/ids.rs
blob: 297bb009f04cd5f45971a4851177aa45bb3f6088 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! 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);

impl AttHandle {
    /// The (only) reserved AttHandle
    pub const RESERVED: Self = AttHandle(0);
    /// The smallest valid AttHandle
    pub const MIN: Self = AttHandle(1);
    /// The largest valid AttHandle
    pub const MAX: Self = AttHandle(0xFFFF);
}