Skip to content

Commit

Permalink
Extract MTU workaround into function
Browse files Browse the repository at this point in the history
  • Loading branch information
surban committed Sep 13, 2023
1 parent bf4ed3b commit 2452e15
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
7 changes: 3 additions & 4 deletions bluer/src/gatt/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use tokio_stream::wrappers::ReceiverStream;
use uuid::Uuid;

use super::{
make_socket_pair, CharacteristicFlags, CharacteristicReader, CharacteristicWriter, DescriptorFlags, WriteOp,
CHARACTERISTIC_INTERFACE, DESCRIPTOR_INTERFACE, SERVICE_INTERFACE,
make_socket_pair, mtu_workaround, CharacteristicFlags, CharacteristicReader, CharacteristicWriter,
DescriptorFlags, WriteOp, CHARACTERISTIC_INTERFACE, DESCRIPTOR_INTERFACE, SERVICE_INTERFACE,
};
use crate::{
method_call, parent_path, Adapter, Address, DbusResult, Device, Error, ErrorKind, Result, SessionInner,
Expand Down Expand Up @@ -942,8 +942,7 @@ impl RegisteredCharacteristic {
// BlueZ has already confirmed the start of the notification session.
// So there is no point in making this fail-able by our users.
let (fd, stream) = make_socket_pair(true).map_err(|_| ReqError::Failed)?;
// WORKAROUND: BlueZ drops data at end of packet if full MTU is used.
let mtu = options.mtu.saturating_sub(5).into();
let mtu = mtu_workaround(options.mtu.into());
let writer = CharacteristicWriter {
adapter_name: options.adapter_name.clone(),
device_address: options.device_address,
Expand Down
8 changes: 8 additions & 0 deletions bluer/src/gatt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,11 @@ pub(crate) fn make_socket_pair(non_block: bool) -> std::io::Result<(OwnedFd, Uni

Ok((fd1, us))
}

/// Apply MTU workaround.
///
/// BlueZ drops data at end of packet if full reported MTU is used, thus we
/// substract 5 bytes to workaround this issue.
pub(crate) fn mtu_workaround(mtu: usize) -> usize {
mtu.saturating_sub(5)
}
9 changes: 4 additions & 5 deletions bluer/src/gatt/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use tokio::net::UnixStream;
use uuid::Uuid;

use super::{
CharacteristicFlags, CharacteristicReader, CharacteristicWriter, WriteOp, CHARACTERISTIC_INTERFACE,
DESCRIPTOR_INTERFACE, SERVICE_INTERFACE,
mtu_workaround, CharacteristicFlags, CharacteristicReader, CharacteristicWriter, WriteOp,
CHARACTERISTIC_INTERFACE, DESCRIPTOR_INTERFACE, SERVICE_INTERFACE,
};
use crate::{
all_dbus_objects, Address, Device, Error, ErrorKind, Event, InternalErrorKind, Result, SessionInner,
Expand Down Expand Up @@ -354,8 +354,7 @@ impl Characteristic {
let stream = unsafe { std::os::unix::net::UnixStream::from_raw_fd(fd.into_fd()) };
stream.set_nonblocking(true)?;
let stream = UnixStream::from_std(stream)?;
// WORKAROUND: BlueZ drops data at end of packet if full MTU is used.
let mtu = mtu.saturating_sub(5).into();
let mtu = mtu_workaround(mtu.into());
Ok(CharacteristicWriter {
adapter_name: self.adapter_name().to_string(),
device_address: self.device_address,
Expand Down Expand Up @@ -541,7 +540,7 @@ define_properties!(
property(
Mtu, usize,
dbus: (CHARACTERISTIC_INTERFACE, "MTU", u16, MANDATORY),
get: (mtu, v => { (*v).into() }),
get: (mtu, v => { mtu_workaround((*v).into()) }),
);
}
);
Expand Down

0 comments on commit 2452e15

Please sign in to comment.