Skip to content
New issue

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

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

Already on GitHub? # to your account

Fixed BLEClient dropping multiple times #144

Merged
merged 1 commit into from
Sep 28, 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
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