Skip to content

Commit 7b1f5c0

Browse files
authored
Merge pull request #9014 from dhalbert/atomic-scanentry
shared-module/_bleio/ScanResults.c: make ScanEntry handling be atomic
2 parents a14094c + 9b6fd48 commit 7b1f5c0

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

shared-module/_bleio/ScanResults.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,26 @@ mp_obj_t common_hal_bleio_scanresults_next(bleio_scanresults_obj_t *self) {
5252
}
5353

5454
// Create a ScanEntry out of the data on the buffer.
55+
56+
// Remove data atomically.
57+
common_hal_mcu_disable_interrupts();
58+
5559
uint8_t type = ringbuf_get(&self->buf);
5660
bool connectable = (type & (1 << 0)) != 0;
5761
bool scan_response = (type & (1 << 1)) != 0;
5862
uint64_t ticks_ms;
5963
ringbuf_get_n(&self->buf, (uint8_t *)&ticks_ms, sizeof(ticks_ms));
60-
uint8_t rssi = ringbuf_get(&self->buf);
64+
int8_t rssi = ringbuf_get(&self->buf);
6165
uint8_t peer_addr[NUM_BLEIO_ADDRESS_BYTES];
6266
ringbuf_get_n(&self->buf, peer_addr, sizeof(peer_addr));
6367
uint8_t addr_type = ringbuf_get(&self->buf);
6468
uint16_t len;
6569
ringbuf_get_n(&self->buf, (uint8_t *)&len, sizeof(len));
66-
6770
mp_obj_str_t *o = MP_OBJ_TO_PTR(mp_obj_new_bytes_of_zeros(len));
6871
ringbuf_get_n(&self->buf, (uint8_t *)o->data, len);
6972

73+
common_hal_mcu_enable_interrupts();
74+
7075
bleio_scanentry_obj_t *entry = mp_obj_malloc(bleio_scanentry_obj_t, &bleio_scanentry_type);
7176
entry->rssi = rssi;
7277

@@ -92,13 +97,6 @@ void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t *self,
9297
uint8_t addr_type,
9398
const uint8_t *data,
9499
uint16_t len) {
95-
int32_t packet_size = sizeof(uint8_t) + sizeof(ticks_ms) + sizeof(rssi) + NUM_BLEIO_ADDRESS_BYTES +
96-
sizeof(addr_type) + sizeof(len) + len;
97-
int32_t empty_space = self->buf.size - ringbuf_num_filled(&self->buf);
98-
if (packet_size >= empty_space) {
99-
// We can't fit the packet so skip it.
100-
return;
101-
}
102100
// Filter the packet.
103101
if (rssi < self->minimum_rssi) {
104102
return;
@@ -116,14 +114,26 @@ void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t *self,
116114
type |= 1 << 1;
117115
}
118116

119-
// Add the packet to the buffer.
120-
ringbuf_put(&self->buf, type);
121-
ringbuf_put_n(&self->buf, (uint8_t *)&ticks_ms, sizeof(ticks_ms));
122-
ringbuf_put(&self->buf, rssi);
123-
ringbuf_put_n(&self->buf, peer_addr, NUM_BLEIO_ADDRESS_BYTES);
124-
ringbuf_put(&self->buf, addr_type);
125-
ringbuf_put_n(&self->buf, (uint8_t *)&len, sizeof(len));
126-
ringbuf_put_n(&self->buf, data, len);
117+
// Add the packet to the buffer, atomically.
118+
common_hal_mcu_disable_interrupts();
119+
120+
// Check whether will fit.
121+
int32_t packet_size = sizeof(uint8_t) + sizeof(ticks_ms) + sizeof(rssi) + NUM_BLEIO_ADDRESS_BYTES +
122+
sizeof(addr_type) + sizeof(len) + len;
123+
int32_t empty_space = self->buf.size - ringbuf_num_filled(&self->buf);
124+
125+
if (packet_size <= empty_space) {
126+
// Packet will fit.
127+
ringbuf_put(&self->buf, type);
128+
ringbuf_put_n(&self->buf, (uint8_t *)&ticks_ms, sizeof(ticks_ms));
129+
ringbuf_put(&self->buf, rssi);
130+
ringbuf_put_n(&self->buf, peer_addr, NUM_BLEIO_ADDRESS_BYTES);
131+
ringbuf_put(&self->buf, addr_type);
132+
ringbuf_put_n(&self->buf, (uint8_t *)&len, sizeof(len));
133+
ringbuf_put_n(&self->buf, data, len);
134+
}
135+
136+
common_hal_mcu_enable_interrupts();
127137
}
128138

129139
bool shared_module_bleio_scanresults_get_done(bleio_scanresults_obj_t *self) {

0 commit comments

Comments
 (0)