Skip to content

Commit

Permalink
Merge pull request #144 from taks/fix-multiple-drop
Browse files Browse the repository at this point in the history
Fixed BLEClient dropping multiple times
  • Loading branch information
taks authored Sep 28, 2024
2 parents f54a982 + f3e6d96 commit d65147f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
8 changes: 5 additions & 3 deletions src/client/ble_attribute.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::BLEClient;
use crate::utilities::ArcUnsafeCell;
use esp_idf_svc::sys as esp_idf_sys;

use super::ble_client::BLEClientState;

pub(crate) trait BLEAttribute {
fn get_client(&self) -> Option<BLEClient>;
fn get_client(&self) -> Option<ArcUnsafeCell<BLEClientState>>;

fn conn_handle(&self) -> u16 {
match self.get_client() {
Some(x) => x.conn_handle(),
Some(x) => x.conn_handle,
None => esp_idf_sys::BLE_HS_CONN_HANDLE_NONE as _,
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/client/ble_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use esp_idf_sys::*;
#[allow(clippy::type_complexity)]
pub(crate) struct BLEClientState {
address: Option<BLEAddress>,
conn_handle: u16,
pub(crate) conn_handle: u16,
services: Option<Vec<BLERemoteService>>,
signal: Signal<u32>,
connect_timeout_ms: u32,
Expand Down Expand Up @@ -54,10 +54,6 @@ impl BLEClient {
}
}

pub(crate) fn from_state(state: ArcUnsafeCell<BLEClientState>) -> Self {
Self { state }
}

pub(crate) fn conn_handle(&self) -> u16 {
self.state.conn_handle
}
Expand Down
5 changes: 3 additions & 2 deletions src/client/ble_remote_characteristic.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use core::borrow::Borrow;

use super::ble_client::BLEClientState;
use super::ble_remote_service::BLERemoteServiceState;
use super::{BLEReader, BLEWriter};
use crate::BLEAttribute;
use crate::{
ble,
utilities::{as_void_ptr, voidp_to_ref, ArcUnsafeCell, BleUuid, WeakUnsafeCell},
BLEError, BLERemoteDescriptor, Signal,
};
use crate::{BLEAttribute, BLEClient};
use alloc::{boxed::Box, vec::Vec};
use bitflags::bitflags;
use core::ffi::c_void;
Expand Down Expand Up @@ -41,7 +42,7 @@ pub struct BLERemoteCharacteristicState {
}

impl BLEAttribute for BLERemoteCharacteristicState {
fn get_client(&self) -> Option<BLEClient> {
fn get_client(&self) -> Option<ArcUnsafeCell<BLEClientState>> {
match self.service.upgrade() {
Some(x) => x.get_client(),
None => None,
Expand Down
6 changes: 3 additions & 3 deletions src/client/ble_remote_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::ble_client::BLEClientState;
use crate::{
ble,
utilities::{as_void_ptr, voidp_to_ref, ArcUnsafeCell, BleUuid, WeakUnsafeCell},
BLEAttribute, BLEClient, BLEError, BLERemoteCharacteristic, Signal,
BLEAttribute, BLEError, BLERemoteCharacteristic, Signal,
};
use alloc::vec::Vec;
use core::ffi::c_void;
Expand All @@ -18,8 +18,8 @@ pub struct BLERemoteServiceState {
}

impl BLEAttribute for BLERemoteServiceState {
fn get_client(&self) -> Option<BLEClient> {
self.client.upgrade().map(BLEClient::from_state)
fn get_client(&self) -> Option<ArcUnsafeCell<BLEClientState>> {
self.client.upgrade()
}
}

Expand Down

0 comments on commit d65147f

Please sign in to comment.