1616#include "shared-bindings/_bleio/PacketBuffer.h"
1717#include "shared-bindings/_bleio/Service.h"
1818#include "shared-bindings/time/__init__.h"
19+ #include "supervisor/shared/safe_mode.h"
1920
2021#include "common-hal/_bleio/Adapter.h"
2122#include "common-hal/_bleio/Service.h"
@@ -74,27 +75,20 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
7475 self -> flags |= BLE_GATT_CHR_F_WRITE_AUTHEN ;
7576 }
7677
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 );
9180 } else {
9281 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 );
9684 }
9785 }
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+ }
9892
9993 if (gc_alloc_possible ()) {
10094 self -> descriptor_list = mp_obj_new_list (0 , NULL );
@@ -114,6 +108,26 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
114108 }
115109}
116110
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+
117131mp_obj_tuple_t * common_hal_bleio_characteristic_get_descriptors (bleio_characteristic_obj_t * self ) {
118132 if (self -> descriptor_list == NULL ) {
119133 return mp_const_empty_tuple ;
@@ -173,21 +187,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
173187 }
174188
175189 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 );
191191
192192 ble_gatts_chr_updated (self -> handle );
193193 }
0 commit comments