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

Commit a01c6738 authored by Rahul Arya's avatar Rahul Arya
Browse files

[Private GATT] Make ATT permissions a bitmask

Simplifies code and makes it easier to extend in the future.

Bug: 255880936
Test: unit

Change-Id: Ibfba803117f6eb9dc1c6cada5a72bae2f384360b
parent 51caea58
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ rust_defaults {
        "libanyhow",
        "libcxx",
        "libbt_common",
        "libbitflags",

        // needed to work around duplicate symbols
        // caused by bug in Soong
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ bt_common = { path = "../gd/rust/common", default-features = false }
# External dependencies
# Note: source-of-truth is Android.bp, these are mirrored solely for IDE convenience
anyhow = "1.0"
bitflags = "1.3.2"
log = "*"
cxx = "*"
android_logger = "*"
+4 −13
Original line number Diff line number Diff line
@@ -250,10 +250,7 @@ fn records_to_service(service_records: &[GattRecord]) -> Result<GattServiceWithH
            GattRecordType::Characteristic => characteristics.push(GattCharacteristicWithHandle {
                handle: AttHandle(record.attribute_handle),
                type_: record.uuid,
                permissions: AttPermissions {
                    readable: record.properties & 0x02 != 0,
                    writable: record.properties & 0x08 != 0,
                },
                permissions: AttPermissions::from_bits_truncate(record.properties),
            }),
            _ => {
                warn!("ignoring unsupported database entry of type {:?}", record.record_type)
@@ -472,10 +469,7 @@ mod test {
        ])
        .unwrap();

        assert_eq!(
            service.characteristics[0].permissions,
            AttPermissions { readable: true, writable: false }
        );
        assert_eq!(service.characteristics[0].permissions, AttPermissions::READABLE);
    }

    #[test]
@@ -486,10 +480,7 @@ mod test {
        ])
        .unwrap();

        assert_eq!(
            service.characteristics[0].permissions,
            AttPermissions { readable: false, writable: true }
        );
        assert_eq!(service.characteristics[0].permissions, AttPermissions::WRITABLE);
    }

    #[test]
@@ -502,7 +493,7 @@ mod test {

        assert_eq!(
            service.characteristics[0].permissions,
            AttPermissions { readable: true, writable: true }
            AttPermissions::READABLE | AttPermissions::WRITABLE
        );
    }
}
+21 −11
Original line number Diff line number Diff line
use async_trait::async_trait;
use bitflags::bitflags;

use crate::{
    core::uuid::Uuid,
@@ -32,20 +33,29 @@ pub struct AttAttribute {
    pub permissions: AttPermissions,
}

bitflags! {
    /// The attribute properties supported by the current GATT server implementation
    /// Unimplemented properties will default to false.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AttPermissions {
    /// Whether an attribute is readable
    pub readable: bool,
    /// Whether an attribute is writable
    /// (using ATT_WRITE_REQ, so a response is expected)
    pub writable: bool,
    ///
    /// These values are from Core Spec 5.3 Vol 3G 3.3.1.1 Characteristic Properties,
    /// and also match what Android uses in JNI.
    pub struct AttPermissions : u8 {
        /// Attribute can be read using READ_REQ
        const READABLE = 0x02;
        /// Attribute can be written to using WRITE_REQ
        const WRITABLE = 0x08;
    }
}

impl AttPermissions {
    /// An attribute that is readable, but not writable
    pub const READONLY: Self = Self { readable: true, writable: false };
    /// Attribute can be read using READ_REQ
    pub fn readable(&self) -> bool {
        self.contains(AttPermissions::READABLE)
    }
    /// Attribute can be written to using WRITE_REQ
    pub fn writable(&self) -> bool {
        self.contains(AttPermissions::WRITABLE)
    }
}

#[async_trait(?Send)]
+3 −3
Original line number Diff line number Diff line
@@ -173,7 +173,7 @@ mod test {
            AttAttribute {
                handle: VALID_HANDLE,
                type_: Uuid::new(0x1234),
                permissions: AttPermissions { readable: true, writable: false },
                permissions: AttPermissions::READABLE,
            },
            vec![5, 6],
        )]);
@@ -241,12 +241,12 @@ mod test {
                GattCharacteristicWithHandle {
                    handle: VALID_HANDLE,
                    type_: Uuid::new(2),
                    permissions: AttPermissions::READONLY,
                    permissions: AttPermissions::READABLE,
                },
                GattCharacteristicWithHandle {
                    handle: ANOTHER_VALID_HANDLE,
                    type_: Uuid::new(2),
                    permissions: AttPermissions::READONLY,
                    permissions: AttPermissions::READABLE,
                },
            ],
        })
Loading