@@ -52,21 +52,26 @@ mp_obj_t common_hal_bleio_scanresults_next(bleio_scanresults_obj_t *self) {
52
52
}
53
53
54
54
// Create a ScanEntry out of the data on the buffer.
55
+
56
+ // Remove data atomically.
57
+ common_hal_mcu_disable_interrupts ();
58
+
55
59
uint8_t type = ringbuf_get (& self -> buf );
56
60
bool connectable = (type & (1 << 0 )) != 0 ;
57
61
bool scan_response = (type & (1 << 1 )) != 0 ;
58
62
uint64_t ticks_ms ;
59
63
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 );
61
65
uint8_t peer_addr [NUM_BLEIO_ADDRESS_BYTES ];
62
66
ringbuf_get_n (& self -> buf , peer_addr , sizeof (peer_addr ));
63
67
uint8_t addr_type = ringbuf_get (& self -> buf );
64
68
uint16_t len ;
65
69
ringbuf_get_n (& self -> buf , (uint8_t * )& len , sizeof (len ));
66
-
67
70
mp_obj_str_t * o = MP_OBJ_TO_PTR (mp_obj_new_bytes_of_zeros (len ));
68
71
ringbuf_get_n (& self -> buf , (uint8_t * )o -> data , len );
69
72
73
+ common_hal_mcu_enable_interrupts ();
74
+
70
75
bleio_scanentry_obj_t * entry = mp_obj_malloc (bleio_scanentry_obj_t , & bleio_scanentry_type );
71
76
entry -> rssi = rssi ;
72
77
@@ -92,13 +97,6 @@ void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t *self,
92
97
uint8_t addr_type ,
93
98
const uint8_t * data ,
94
99
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
- }
102
100
// Filter the packet.
103
101
if (rssi < self -> minimum_rssi ) {
104
102
return ;
@@ -116,14 +114,26 @@ void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t *self,
116
114
type |= 1 << 1 ;
117
115
}
118
116
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 ();
127
137
}
128
138
129
139
bool shared_module_bleio_scanresults_get_done (bleio_scanresults_obj_t * self ) {
0 commit comments