br-ble 0.2.0

This is an Bluetooth
Documentation
use crate::mac::platform::{NSData, ObjectPtr};
use super::*;

/// An object that provides further information about a remote peripheral’s characteristic.
///
/// Descriptors provide further information about a characteristic’s value. For example, they may
/// describe the value in human-readable form and describe how to format the value for presentation
/// purposes. characteristic descriptors also indicate whether a characteristic’s value indicates or
/// notifies a client (a central) when the value of the characteristic changes.
#[derive(Clone, Debug)]
pub struct Descriptor {
    id: Uuid,
    pub descriptor: StrongPtr<CBDescriptor>,
}

assert_impl_all!(Descriptor: Send, Sync);

impl Descriptor {
    /// # Safety
    /// 调用此函数时,调用者必须确保对象是有效的,且在整个调用过程中不会被释放或销毁。否则,可能会导致未定义行为。
    pub unsafe fn retain(o: impl ObjectPtr) -> Self {
        let descriptor = CBDescriptor::wrap(o).retain();
        Self {
            id: descriptor.id(),
            descriptor,
        }
    }
    pub fn get_id(&self) -> &Uuid {
        &self.id
    }
}

object_ptr_wrapper!(CBDescriptor);

impl CBDescriptor {
    pub fn id(&self) -> Uuid {
        unsafe {
            let r: *mut Object = msg_send![self.as_ptr(), UUID];
            CBUUID::wrap(r).to_uuid()
        }
    }

    pub fn value(&self) -> Option<Vec<u8>> {
        unsafe {
            let r: *mut Object = msg_send![self.as_ptr(), value];
            let r = NSData::wrap_nullable(r)?;
            Some(r.as_bytes().into())
        }
    }
}