Skip to content

Commit

Permalink
Avoid overwriting HCI buffer until sent (#398)
Browse files Browse the repository at this point in the history
* Avoid overwriting HCI buffer until sent

* Fix formatting
  • Loading branch information
bjoernQ authored Jan 2, 2024
1 parent a69545d commit 90589fd
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::{cell::RefCell, ptr::addr_of};

use critical_section::Mutex;
use portable_atomic::{AtomicBool, Ordering};

use crate::ble::btdm::ble_os_adapter_chip_specific::{osi_funcs_s, G_OSI_FUNCS};
use crate::ble::HciOutCollector;
Expand Down Expand Up @@ -31,6 +32,8 @@ pub struct ReceivedPacket {
static BT_INTERNAL_QUEUE: Mutex<RefCell<SimpleQueue<[u8; 8], 10>>> =
Mutex::new(RefCell::new(SimpleQueue::new()));

static PACKET_SENT: AtomicBool = AtomicBool::new(true);

#[repr(C)]
struct vhci_host_callback_s {
notify_host_send_available: extern "C" fn(), /* callback used to notify that the host can send packet to controller */
Expand Down Expand Up @@ -64,6 +67,8 @@ static VHCI_HOST_CALLBACK: vhci_host_callback_s = vhci_host_callback_s {

extern "C" fn notify_host_send_available() {
trace!("notify_host_send_available");

PACKET_SENT.store(true, Ordering::Relaxed);
}

extern "C" fn notify_host_recv(data: *mut u8, len: u16) -> i32 {
Expand Down Expand Up @@ -581,13 +586,17 @@ pub fn send_hci(data: &[u8]) {
continue;
}

PACKET_SENT.store(false, Ordering::Relaxed);
API_vhci_host_send_packet(packet.as_ptr() as *const u8, packet.len() as u16);
trace!("sent vhci host packet");

dump_packet_info(packet);

break;
}

// make sure the packet buffer doesn't get touched until sent
while !PACKET_SENT.load(Ordering::Relaxed) {}
}

hci_out.reset();
Expand Down

0 comments on commit 90589fd

Please sign in to comment.