Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(macos): reading descriptor value #391

Merged
merged 3 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions src/corebluetooth/central_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ declare_class!(
}
}

#[method(peripheral:didUpdateNotificationStateForCharacteristic:error:)]
#[method(peripheral:didWriteValueForCharacteristic:error:)]
fn delegate_peripheral_didwritevalueforcharacteristic_error(
&self,
peripheral: &CBPeripheral,
Expand All @@ -622,7 +622,7 @@ declare_class!(
}
}

#[method(peripheral:didWriteValueForCharacteristic:error:)]
#[method(peripheral:didUpdateNotificationStateForCharacteristic:error:)]
fn delegate_peripheral_didupdatenotificationstateforcharacteristic_error(
&self,
peripheral: &CBPeripheral,
Expand Down Expand Up @@ -685,7 +685,7 @@ declare_class!(
service_uuid: cbuuid_to_uuid(unsafe { &service.UUID() }),
characteristic_uuid: cbuuid_to_uuid(unsafe { &characteristic.UUID() }),
descriptor_uuid: cbuuid_to_uuid(unsafe { &descriptor.UUID() }),
data: get_characteristic_value(&characteristic),
data: get_descriptor_value(&descriptor),
});
// Notify BluetoothGATTCharacteristic::read_value that read was successful.
}
Expand Down Expand Up @@ -749,6 +749,41 @@ fn get_characteristic_value(characteristic: &CBCharacteristic) -> Vec<u8> {
v.unwrap_or_default()
}

fn get_descriptor_value(descriptor: &CBDescriptor) -> Vec<u8> {
trace!("Getting data!");
let v = unsafe { descriptor.value() }.map(|value| unsafe {
let mut clazz = value.class();
// Find the root class until we reach NSObject
while let Some(superclass) = clazz.superclass() {
if superclass == NSObject::class() {
break;
}
clazz = superclass;
}

match clazz.name() {
"NSString" => {
let d: Retained<NSString> = Retained::cast(value);
d.to_string().into_bytes()
}
"NSData" => {
let d: Retained<NSData> = Retained::cast(value);
d.bytes().into()
}
"NSNumber" => {
let d: Retained<NSNumber> = Retained::cast(value);
d.stringValue().to_string().into_bytes()
}
_ => {
error!("Unknown descriptor value class: {:?}", clazz);
Vec::new()
}
}
});
trace!("BluetoothGATTDescriptor::get_value -> {:?}", v);
v.unwrap_or_default()
}

fn peripheral_debug(peripheral: &CBPeripheral) -> String {
let uuid = unsafe { peripheral.identifier() }.UUIDString();
if let Some(name) = unsafe { peripheral.name() } {
Expand Down
5 changes: 2 additions & 3 deletions src/corebluetooth/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,15 +1009,14 @@ impl CoreBluetoothInternal {
if let Some(service) = peripheral.services.get_mut(&service_uuid) {
if let Some(characteristic) = service.characteristics.get_mut(&characteristic_uuid)
{
if let Some(_descriptor) = characteristic.descriptors.get_mut(&descriptor_uuid)
{
if let Some(descriptor) = characteristic.descriptors.get_mut(&descriptor_uuid) {
trace!("Got read event!");

let mut data_clone = Vec::new();
for byte in data.iter() {
data_clone.push(*byte);
}
let state = characteristic.read_future_state.pop_back().unwrap();
let state = descriptor.read_future_state.pop_back().unwrap();
state
.lock()
.unwrap()
Expand Down
Loading