Skip to content

Commit

Permalink
Improve packet dumps (esp-rs#165)
Browse files Browse the repository at this point in the history
* packet-dump dumps the raw packets

* Update README, serialport config in wifishark
  • Loading branch information
bjoernQ committed May 23, 2024
1 parent 5402d70 commit 2bd9ea5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 63 deletions.
24 changes: 22 additions & 2 deletions esp-wifi/src/ble/btdm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ extern "C" fn notify_host_recv(data: *mut u8, len: u16) -> i32 {
let mut queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
queue.enqueue(packet).unwrap();
});


dump_packet_info(&core::slice::from_raw_parts(
data as *const u8,
len as usize,
));

#[cfg(feature = "async")]
crate::ble::controller::asynch::hci_read_data_available();
}
Expand Down Expand Up @@ -529,7 +534,10 @@ static mut BLE_HCI_READ_DATA_LEN: usize = 0;
pub fn have_hci_read_data() -> bool {
critical_section::with(|cs| {
let queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
!queue.is_empty() || unsafe { BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX) }
!queue.is_empty()
|| unsafe {
BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX)
}
})
}

Expand Down Expand Up @@ -582,10 +590,22 @@ pub fn send_hci(data: &[u8]) {
API_vhci_host_send_packet(packet.as_ptr() as *const u8, packet.len() as u16);
log::trace!("sent vhci host packet");

dump_packet_info(packet);

break;
}
}

hci_out.reset();
}
}

#[allow(unreachable_code, unused_variables)]
fn dump_packet_info(buffer: &[u8]) {
#[cfg(not(feature = "dump-packets"))]
return;

critical_section::with(|cs| {
log::info!("@HCIFRAME {:02x?}", buffer);
});
}
25 changes: 22 additions & 3 deletions esp-wifi/src/ble/npl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,8 +1231,10 @@ unsafe extern "C" fn ble_hs_hci_rx_evt(cmd: *const u8, arg: *const c_void) {
data,
})
.unwrap();

dump_packet_info(&data[..(len + 3) as usize]);
});

#[cfg(feature = "async")]
crate::ble::controller::asynch::hci_read_data_available();
}
Expand All @@ -1257,8 +1259,10 @@ unsafe extern "C" fn ble_hs_rx_data(om: *const OsMbuf, arg: *const c_void) {
data,
})
.unwrap();

dump_packet_info(&data[..(len + 1) as usize]);
});

#[cfg(feature = "async")]
crate::ble::controller::asynch::hci_read_data_available();
}
Expand All @@ -1271,7 +1275,10 @@ static mut BLE_HCI_READ_DATA_LEN: usize = 0;
pub fn have_hci_read_data() -> bool {
critical_section::with(|cs| {
let queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
!queue.is_empty() || unsafe { BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX) }
!queue.is_empty()
|| unsafe {
BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX)
}
})
}

Expand Down Expand Up @@ -1317,6 +1324,8 @@ pub fn send_hci(data: &[u8]) {
const DATA_TYPE_COMMAND: u8 = 1;
const DATA_TYPE_ACL: u8 = 2;

dump_packet_info(&packet);

if packet[0] == DATA_TYPE_COMMAND {
let res = (ble_hci_trans_funcs_ptr.ble_hci_trans_hs_cmd_tx.unwrap())(
&packet[1] as *const _ as *mut u8, // don't send the TYPE
Expand Down Expand Up @@ -1400,3 +1409,13 @@ fn is_free(mempool: &OsMempool, mbuf: *const OsMbuf) -> bool {
next = unsafe { (*next).next };
}
}

#[allow(unreachable_code, unused_variables)]
fn dump_packet_info(buffer: &[u8]) {
#[cfg(not(feature = "dump-packets"))]
return;

critical_section::with(|cs| {
log::info!("@HCIFRAME {:02x?}", buffer);
});
}
63 changes: 5 additions & 58 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ use crate::{
},
compat::queue::SimpleQueue,
};
use log::{debug, info};
use log::debug;

#[derive(Debug, Clone, Copy)]
pub enum WifiMode {
Expand All @@ -101,11 +101,6 @@ impl WifiMode {
}
}

#[cfg(feature = "dump-packets")]
static DUMP_PACKETS: bool = true;
#[cfg(not(feature = "dump-packets"))]
static DUMP_PACKETS: bool = false;

#[derive(Debug, Clone, Copy)]
pub(crate) struct DataFrame<'a> {
len: usize,
Expand Down Expand Up @@ -1221,60 +1216,12 @@ impl embedded_svc::wifi::Wifi for WifiController<'_> {
}
}

#[allow(unreachable_code, unused_variables)]
fn dump_packet_info(buffer: &[u8]) {
if !DUMP_PACKETS {
return;
}
#[cfg(not(feature = "dump-packets"))]
return;

let ef = smoltcp::wire::EthernetFrame::new_unchecked(buffer);
info!(
"src={:x?} dst={:x?} type={:x?}",
ef.src_addr(),
ef.dst_addr(),
ef.ethertype()
);
match ef.ethertype() {
smoltcp::wire::EthernetProtocol::Ipv4 => {
let ip = smoltcp::wire::Ipv4Packet::new_unchecked(ef.payload());
info!(
"src={:?} dst={:?} proto={:x?}",
ip.src_addr(),
ip.dst_addr(),
ip.next_header()
);

match ip.next_header() {
smoltcp::wire::IpProtocol::HopByHop => {}
smoltcp::wire::IpProtocol::Icmp => {}
smoltcp::wire::IpProtocol::Igmp => {}
smoltcp::wire::IpProtocol::Tcp => {
let tp = smoltcp::wire::TcpPacket::new_unchecked(ip.payload());
info!("src={:?} dst={:?}", tp.src_port(), tp.dst_port());
}
smoltcp::wire::IpProtocol::Udp => {
let up = smoltcp::wire::UdpPacket::new_unchecked(ip.payload());
info!("src={:?} dst={:?}", up.src_port(), up.dst_port());
}
smoltcp::wire::IpProtocol::Ipv6Route => {}
smoltcp::wire::IpProtocol::Ipv6Frag => {}
smoltcp::wire::IpProtocol::Icmpv6 => {}
smoltcp::wire::IpProtocol::Ipv6NoNxt => {}
smoltcp::wire::IpProtocol::Ipv6Opts => {}
smoltcp::wire::IpProtocol::Unknown(_) => {}
}
}
smoltcp::wire::EthernetProtocol::Arp => {
let ap = smoltcp::wire::ArpPacket::new_unchecked(ef.payload());
info!(
"src={:x?} dst={:x?} src proto addr={:x?}",
ap.source_hardware_addr(),
ap.target_hardware_addr(),
ap.source_protocol_addr()
);
}
smoltcp::wire::EthernetProtocol::Ipv6 => {}
smoltcp::wire::EthernetProtocol::Unknown(_) => {}
}
log::info!("@WIFIFRAME {:02x?}", buffer);
}

#[macro_export]
Expand Down

0 comments on commit 2bd9ea5

Please sign in to comment.