16
16
#include "shared-bindings/_bleio/PacketBuffer.h"
17
17
#include "shared-bindings/_bleio/Service.h"
18
18
#include "shared-bindings/time/__init__.h"
19
+ #include "supervisor/shared/safe_mode.h"
19
20
20
21
#include "common-hal/_bleio/Adapter.h"
21
22
#include "common-hal/_bleio/Service.h"
@@ -74,27 +75,20 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
74
75
self -> flags |= BLE_GATT_CHR_F_WRITE_AUTHEN ;
75
76
}
76
77
77
- if (initial_value_bufinfo != NULL ) {
78
- // Copy the initial value if it's on the heap. Otherwise it's internal and we may not be able
79
- // to allocate.
80
- self -> current_value_len = initial_value_bufinfo -> len ;
81
- if (gc_alloc_possible ()) {
82
- self -> current_value = m_malloc (max_length );
83
- self -> current_value_alloc = max_length ;
84
- if (gc_nbytes (initial_value_bufinfo -> buf ) > 0 ) {
85
- memcpy (self -> current_value , initial_value_bufinfo -> buf , self -> current_value_len );
86
- }
87
- } else {
88
- self -> current_value = initial_value_bufinfo -> buf ;
89
- assert (self -> current_value_len == max_length );
90
- }
78
+ if (gc_alloc_possible ()) {
79
+ self -> current_value = m_malloc (max_length );
91
80
} else {
92
81
self -> current_value = port_malloc (max_length , false);
93
- if (self -> current_value != NULL ) {
94
- self -> current_value_alloc = max_length ;
95
- self -> current_value_len = 0 ;
82
+ if (self -> current_value == NULL ) {
83
+ reset_into_safe_mode (SAFE_MODE_NO_HEAP );
96
84
}
97
85
}
86
+ self -> current_value_alloc = max_length ;
87
+ self -> current_value_len = 0 ;
88
+
89
+ if (initial_value_bufinfo != NULL ) {
90
+ common_hal_bleio_characteristic_set_value (self , initial_value_bufinfo );
91
+ }
98
92
99
93
if (gc_alloc_possible ()) {
100
94
self -> descriptor_list = mp_obj_new_list (0 , NULL );
@@ -114,6 +108,26 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
114
108
}
115
109
}
116
110
111
+ bool common_hal_bleio_characteristic_deinited (bleio_characteristic_obj_t * self ) {
112
+ return self -> current_value == NULL ;
113
+ }
114
+
115
+ void common_hal_bleio_characteristic_deinit (bleio_characteristic_obj_t * self ) {
116
+ if (common_hal_bleio_characteristic_deinited (self )) {
117
+ return ;
118
+ }
119
+ if (self -> current_value == NULL ) {
120
+ return ;
121
+ }
122
+
123
+ if (gc_nbytes (self -> current_value ) > 0 ) {
124
+ m_free (self -> current_value );
125
+ } else {
126
+ port_free (self -> current_value );
127
+ }
128
+ self -> current_value = NULL ;
129
+ }
130
+
117
131
mp_obj_tuple_t * common_hal_bleio_characteristic_get_descriptors (bleio_characteristic_obj_t * self ) {
118
132
if (self -> descriptor_list == NULL ) {
119
133
return mp_const_empty_tuple ;
@@ -173,21 +187,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
173
187
}
174
188
175
189
self -> current_value_len = bufinfo -> len ;
176
- // If we've already allocated an internal buffer or the provided buffer
177
- // is on the heap, then copy into the internal buffer.
178
- if (self -> current_value_alloc > 0 || gc_nbytes (bufinfo -> buf ) > 0 ) {
179
- if (self -> current_value_alloc < bufinfo -> len ) {
180
- self -> current_value = m_realloc (self -> current_value , bufinfo -> len );
181
- // Get the number of bytes from the heap because it may be more
182
- // than the len due to gc block size.
183
- self -> current_value_alloc = gc_nbytes (self -> current_value );
184
- }
185
- memcpy (self -> current_value , bufinfo -> buf , bufinfo -> len );
186
- } else {
187
- // Otherwise, use the provided buffer to delay any heap allocation.
188
- self -> current_value = bufinfo -> buf ;
189
- self -> current_value_alloc = 0 ;
190
- }
190
+ memcpy (self -> current_value , bufinfo -> buf , self -> current_value_len );
191
191
192
192
ble_gatts_chr_updated (self -> handle );
193
193
}
0 commit comments