Skip to content

Commit c0ed46a

Browse files
committed
Avoid overwriting HCI buffer until sent (esp-rs#398)
* Avoid overwriting HCI buffer until sent * Fix formatting
1 parent 1d6f791 commit c0ed46a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

esp-wifi/src/ble/btdm.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::{cell::RefCell, ptr::addr_of};
22

33
use critical_section::Mutex;
4+
use portable_atomic::{AtomicBool, Ordering};
45

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

35+
static PACKET_SENT: AtomicBool = AtomicBool::new(true);
36+
3437
#[repr(C)]
3538
struct vhci_host_callback_s {
3639
notify_host_send_available: extern "C" fn(), /* callback used to notify that the host can send packet to controller */
@@ -64,6 +67,8 @@ static VHCI_HOST_CALLBACK: vhci_host_callback_s = vhci_host_callback_s {
6467

6568
extern "C" fn notify_host_send_available() {
6669
trace!("notify_host_send_available");
70+
71+
PACKET_SENT.store(true, Ordering::Relaxed);
6772
}
6873

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

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

587593
dump_packet_info(packet);
588594

589595
break;
590596
}
597+
598+
// make sure the packet buffer doesn't get touched until sent
599+
while !PACKET_SENT.load(Ordering::Relaxed) {}
591600
}
592601

593602
hci_out.reset();

0 commit comments

Comments
 (0)