Skip to content

Commit 2bd9ea5

Browse files
committed
Improve packet dumps (esp-rs#165)
* packet-dump dumps the raw packets * Update README, serialport config in wifishark
1 parent 5402d70 commit 2bd9ea5

File tree

3 files changed

+49
-63
lines changed

3 files changed

+49
-63
lines changed

esp-wifi/src/ble/btdm.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ extern "C" fn notify_host_recv(data: *mut u8, len: u16) -> i32 {
9393
let mut queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
9494
queue.enqueue(packet).unwrap();
9595
});
96-
96+
97+
dump_packet_info(&core::slice::from_raw_parts(
98+
data as *const u8,
99+
len as usize,
100+
));
101+
97102
#[cfg(feature = "async")]
98103
crate::ble::controller::asynch::hci_read_data_available();
99104
}
@@ -529,7 +534,10 @@ static mut BLE_HCI_READ_DATA_LEN: usize = 0;
529534
pub fn have_hci_read_data() -> bool {
530535
critical_section::with(|cs| {
531536
let queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
532-
!queue.is_empty() || unsafe { BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX) }
537+
!queue.is_empty()
538+
|| unsafe {
539+
BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX)
540+
}
533541
})
534542
}
535543

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

593+
dump_packet_info(packet);
594+
585595
break;
586596
}
587597
}
588598

589599
hci_out.reset();
590600
}
591601
}
602+
603+
#[allow(unreachable_code, unused_variables)]
604+
fn dump_packet_info(buffer: &[u8]) {
605+
#[cfg(not(feature = "dump-packets"))]
606+
return;
607+
608+
critical_section::with(|cs| {
609+
log::info!("@HCIFRAME {:02x?}", buffer);
610+
});
611+
}

esp-wifi/src/ble/npl.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,8 +1231,10 @@ unsafe extern "C" fn ble_hs_hci_rx_evt(cmd: *const u8, arg: *const c_void) {
12311231
data,
12321232
})
12331233
.unwrap();
1234+
1235+
dump_packet_info(&data[..(len + 3) as usize]);
12341236
});
1235-
1237+
12361238
#[cfg(feature = "async")]
12371239
crate::ble::controller::asynch::hci_read_data_available();
12381240
}
@@ -1257,8 +1259,10 @@ unsafe extern "C" fn ble_hs_rx_data(om: *const OsMbuf, arg: *const c_void) {
12571259
data,
12581260
})
12591261
.unwrap();
1262+
1263+
dump_packet_info(&data[..(len + 1) as usize]);
12601264
});
1261-
1265+
12621266
#[cfg(feature = "async")]
12631267
crate::ble::controller::asynch::hci_read_data_available();
12641268
}
@@ -1271,7 +1275,10 @@ static mut BLE_HCI_READ_DATA_LEN: usize = 0;
12711275
pub fn have_hci_read_data() -> bool {
12721276
critical_section::with(|cs| {
12731277
let queue = BT_RECEIVE_QUEUE.borrow_ref_mut(cs);
1274-
!queue.is_empty() || unsafe { BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX) }
1278+
!queue.is_empty()
1279+
|| unsafe {
1280+
BLE_HCI_READ_DATA_LEN > 0 && (BLE_HCI_READ_DATA_LEN >= BLE_HCI_READ_DATA_INDEX)
1281+
}
12751282
})
12761283
}
12771284

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

1327+
dump_packet_info(&packet);
1328+
13201329
if packet[0] == DATA_TYPE_COMMAND {
13211330
let res = (ble_hci_trans_funcs_ptr.ble_hci_trans_hs_cmd_tx.unwrap())(
13221331
&packet[1] as *const _ as *mut u8, // don't send the TYPE
@@ -1400,3 +1409,13 @@ fn is_free(mempool: &OsMempool, mbuf: *const OsMbuf) -> bool {
14001409
next = unsafe { (*next).next };
14011410
}
14021411
}
1412+
1413+
#[allow(unreachable_code, unused_variables)]
1414+
fn dump_packet_info(buffer: &[u8]) {
1415+
#[cfg(not(feature = "dump-packets"))]
1416+
return;
1417+
1418+
critical_section::with(|cs| {
1419+
log::info!("@HCIFRAME {:02x?}", buffer);
1420+
});
1421+
}

esp-wifi/src/wifi/mod.rs

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ use crate::{
8484
},
8585
compat::queue::SimpleQueue,
8686
};
87-
use log::{debug, info};
87+
use log::debug;
8888

8989
#[derive(Debug, Clone, Copy)]
9090
pub enum WifiMode {
@@ -101,11 +101,6 @@ impl WifiMode {
101101
}
102102
}
103103

104-
#[cfg(feature = "dump-packets")]
105-
static DUMP_PACKETS: bool = true;
106-
#[cfg(not(feature = "dump-packets"))]
107-
static DUMP_PACKETS: bool = false;
108-
109104
#[derive(Debug, Clone, Copy)]
110105
pub(crate) struct DataFrame<'a> {
111106
len: usize,
@@ -1221,60 +1216,12 @@ impl embedded_svc::wifi::Wifi for WifiController<'_> {
12211216
}
12221217
}
12231218

1219+
#[allow(unreachable_code, unused_variables)]
12241220
fn dump_packet_info(buffer: &[u8]) {
1225-
if !DUMP_PACKETS {
1226-
return;
1227-
}
1221+
#[cfg(not(feature = "dump-packets"))]
1222+
return;
12281223

1229-
let ef = smoltcp::wire::EthernetFrame::new_unchecked(buffer);
1230-
info!(
1231-
"src={:x?} dst={:x?} type={:x?}",
1232-
ef.src_addr(),
1233-
ef.dst_addr(),
1234-
ef.ethertype()
1235-
);
1236-
match ef.ethertype() {
1237-
smoltcp::wire::EthernetProtocol::Ipv4 => {
1238-
let ip = smoltcp::wire::Ipv4Packet::new_unchecked(ef.payload());
1239-
info!(
1240-
"src={:?} dst={:?} proto={:x?}",
1241-
ip.src_addr(),
1242-
ip.dst_addr(),
1243-
ip.next_header()
1244-
);
1245-
1246-
match ip.next_header() {
1247-
smoltcp::wire::IpProtocol::HopByHop => {}
1248-
smoltcp::wire::IpProtocol::Icmp => {}
1249-
smoltcp::wire::IpProtocol::Igmp => {}
1250-
smoltcp::wire::IpProtocol::Tcp => {
1251-
let tp = smoltcp::wire::TcpPacket::new_unchecked(ip.payload());
1252-
info!("src={:?} dst={:?}", tp.src_port(), tp.dst_port());
1253-
}
1254-
smoltcp::wire::IpProtocol::Udp => {
1255-
let up = smoltcp::wire::UdpPacket::new_unchecked(ip.payload());
1256-
info!("src={:?} dst={:?}", up.src_port(), up.dst_port());
1257-
}
1258-
smoltcp::wire::IpProtocol::Ipv6Route => {}
1259-
smoltcp::wire::IpProtocol::Ipv6Frag => {}
1260-
smoltcp::wire::IpProtocol::Icmpv6 => {}
1261-
smoltcp::wire::IpProtocol::Ipv6NoNxt => {}
1262-
smoltcp::wire::IpProtocol::Ipv6Opts => {}
1263-
smoltcp::wire::IpProtocol::Unknown(_) => {}
1264-
}
1265-
}
1266-
smoltcp::wire::EthernetProtocol::Arp => {
1267-
let ap = smoltcp::wire::ArpPacket::new_unchecked(ef.payload());
1268-
info!(
1269-
"src={:x?} dst={:x?} src proto addr={:x?}",
1270-
ap.source_hardware_addr(),
1271-
ap.target_hardware_addr(),
1272-
ap.source_protocol_addr()
1273-
);
1274-
}
1275-
smoltcp::wire::EthernetProtocol::Ipv6 => {}
1276-
smoltcp::wire::EthernetProtocol::Unknown(_) => {}
1277-
}
1224+
log::info!("@WIFIFRAME {:02x?}", buffer);
12781225
}
12791226

12801227
#[macro_export]

0 commit comments

Comments
 (0)