Skip to content

Commit 62d9668

Browse files
ivmarkovCopilot
andcommitted
Apply code review feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c452d1a commit 62d9668

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

openthread/src/esp.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ impl<'a> EspRadio<'a> {
6363
pan_id: config.pan_id,
6464
short_addr: config.short_addr,
6565
ext_addr: config.ext_addr,
66+
// The default of 10 is too small for OpenThread,
67+
// which can have bursts of incoming frames, so we increase it to 50.
68+
// TODO: See if we can get by with a smaller number to save memory.
6669
rx_queue_size: 50,
6770
..Default::default()
6871
};
@@ -110,7 +113,7 @@ impl Radio for EspRadio<'_> {
110113
TX_SIGNAL.reset();
111114

112115
trace!(
113-
"802.15.4 TX: {} bytes ch{}",
116+
"802.15.4: About to TX {} bytes ch{}",
114117
psdu.len(),
115118
self.config.channel
116119
);
@@ -122,48 +125,54 @@ impl Radio for EspRadio<'_> {
122125
let success = TX_SIGNAL.wait().await;
123126

124127
if success {
125-
trace!("ESP Radio, transmission done");
128+
trace!("802.15.4: TX done");
126129

127130
if let Some(ack_psdu_buf) = ack_psdu_buf {
128131
// After tx_done signal received, get the ACK frame:
129132
if let Some(ack_frame) = self.driver.get_ack_frame() {
130-
let ack_psdu_len =
131-
(ack_frame.data.len() - 1).min((ack_frame.data[0] & 0x7f) as usize);
132-
ack_psdu_buf[..ack_psdu_len]
133-
.copy_from_slice(&ack_frame.data[1..][..ack_psdu_len]);
134-
135-
trace!(
136-
"ESP Radio, received ACK: {} on channel {}",
137-
Bytes(&ack_psdu_buf[..ack_psdu_len]),
138-
ack_frame.channel
139-
);
140-
141-
let rssi = ack_frame.data[1..][ack_psdu_len] as i8;
142-
143-
return Ok(Some(PsduMeta {
144-
len: ack_psdu_len,
145-
channel: ack_frame.channel,
146-
rssi: Some(rssi),
147-
}));
133+
if ack_frame.data.len() >= 1 {
134+
// Must have at least 1 byte for PSDU
135+
let ack_psdu_len =
136+
(ack_frame.data.len() - 1).min((ack_frame.data[0] & 0x7f) as usize);
137+
138+
ack_psdu_buf[..ack_psdu_len]
139+
.copy_from_slice(&ack_frame.data[1..][..ack_psdu_len]);
140+
141+
trace!(
142+
"802.15.4: ACK: {} on ch{}",
143+
Bytes(&ack_psdu_buf[..ack_psdu_len]),
144+
ack_frame.channel
145+
);
146+
147+
// Only read RSSI if there is at least one byte after the PSDU.
148+
let rssi = if ack_frame.data.len() > 1 + ack_psdu_len {
149+
Some(ack_frame.data[1..][ack_psdu_len] as i8)
150+
} else {
151+
None
152+
};
153+
154+
return Ok(Some(PsduMeta {
155+
len: ack_psdu_len,
156+
channel: ack_frame.channel,
157+
rssi,
158+
}));
159+
}
148160
}
149161
}
150162

151163
Ok(None)
152164
} else {
153-
trace!("ESP Radio, transmission failed");
165+
trace!("802.15.4: TX failed");
154166

155-
// Report as NoAck error so OpenThread SubMac retries
167+
// Report as a failure so OpenThread SubMac retries
156168
Err(RadioErrorKind::TxFailed)
157169
}
158170
}
159171

160172
async fn receive(&mut self, psdu_buf: &mut [u8]) -> Result<PsduMeta, Self::Error> {
161173
RX_SIGNAL.reset();
162174

163-
trace!(
164-
"ESP Radio, about to receive on channel {}",
165-
self.config.channel
166-
);
175+
trace!("802.15.4: About to RX on ch{}", self.config.channel);
167176

168177
self.driver.start_receive();
169178

@@ -175,13 +184,23 @@ impl Radio for EspRadio<'_> {
175184
RX_SIGNAL.wait().await;
176185
};
177186

187+
if raw.data.len() < 1 {
188+
// Must have at least 1 byte for PSDU
189+
return Err(RadioErrorKind::Other);
190+
}
191+
178192
let psdu_len = (raw.data.len() - 1).min((raw.data[0] & 0x7f) as usize);
179193
psdu_buf[..psdu_len].copy_from_slice(&raw.data[1..][..psdu_len]);
180194

181-
let rssi = raw.data[1..][psdu_len] as i8;
195+
// Only read RSSI if there is at least one byte after the PSDU.
196+
let rssi = if raw.data.len() > 1 + psdu_len {
197+
Some(raw.data[1..][psdu_len] as i8)
198+
} else {
199+
None
200+
};
182201

183202
trace!(
184-
"802.15.4 RX: {} bytes ch{} rssi={}",
203+
"802.15.4: RX {} bytes ch{} rssi={:?}",
185204
psdu_len,
186205
raw.channel,
187206
rssi
@@ -190,7 +209,7 @@ impl Radio for EspRadio<'_> {
190209
Ok(PsduMeta {
191210
len: psdu_len,
192211
channel: raw.channel,
193-
rssi: Some(rssi),
212+
rssi,
194213
})
195214
}
196215
}

openthread/src/radio.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,13 @@ impl Config {
168168
pub const fn new() -> Self {
169169
Self {
170170
channel: 11,
171+
// Run with max power by default
172+
// TODO: Figure out how to have this specified by the user
171173
power: 20,
172174
cca: Cca::Carrier,
173175
sfd: 0,
174176
promiscuous: false,
177+
// OpenThread can have bursts of incoming frames, so we need to receive during idle state to not miss them.
175178
rx_when_idle: true,
176179
pan_id: None,
177180
short_addr: None,

0 commit comments

Comments
 (0)