@@ -75,17 +75,23 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
7575 self -> flags |= BLE_GATT_CHR_F_WRITE_AUTHEN ;
7676 }
7777
78- if (gc_alloc_possible ()) {
79- self -> current_value = m_malloc (max_length );
80- } else {
81- self -> current_value = port_malloc (max_length , false);
82- if (self -> current_value == NULL ) {
83- reset_into_safe_mode (SAFE_MODE_NO_HEAP );
78+ // If max_length is 0, then no storage is allocated.
79+ if (max_length > 0 ) {
80+ if (gc_alloc_possible ()) {
81+ self -> current_value = m_malloc (max_length );
82+ } else {
83+ self -> current_value = port_malloc (max_length , false);
84+ if (self -> current_value == NULL ) {
85+ reset_into_safe_mode (SAFE_MODE_NO_HEAP );
86+ }
8487 }
8588 }
8689 self -> current_value_alloc = max_length ;
8790 self -> current_value_len = 0 ;
8891
92+ self -> max_length = max_length ;
93+ self -> fixed_length = fixed_length ;
94+
8995 if (initial_value_bufinfo != NULL ) {
9096 common_hal_bleio_characteristic_set_value (self , initial_value_bufinfo );
9197 }
@@ -96,9 +102,6 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
96102 self -> descriptor_list = NULL ;
97103 }
98104
99- self -> max_length = max_length ;
100- self -> fixed_length = fixed_length ;
101-
102105 if (service -> is_remote ) {
103106 // If the service is remote, we're buffering incoming notifications and indications.
104107 self -> handle = handle ;
@@ -109,23 +112,25 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
109112}
110113
111114bool common_hal_bleio_characteristic_deinited (bleio_characteristic_obj_t * self ) {
112- return self -> current_value == NULL ;
115+ return self -> handle == BLEIO_HANDLE_INVALID ;
113116}
114117
115118void common_hal_bleio_characteristic_deinit (bleio_characteristic_obj_t * self ) {
116119 if (common_hal_bleio_characteristic_deinited (self )) {
117120 return ;
118121 }
119- if (self -> current_value == NULL ) {
120- return ;
121- }
122+ if (self -> current_value != NULL ) {
123+ if (gc_nbytes (self -> current_value ) > 0 ) {
124+ m_free (self -> current_value );
125+ } else {
126+ port_free (self -> current_value );
127+ }
122128
123- if (gc_nbytes (self -> current_value ) > 0 ) {
124- m_free (self -> current_value );
125- } else {
126- port_free (self -> current_value );
129+ self -> current_value = NULL ;
127130 }
128- self -> current_value = NULL ;
131+
132+ // Used to indicate deinit.
133+ self -> handle = BLEIO_HANDLE_INVALID ;
129134}
130135
131136mp_obj_tuple_t * common_hal_bleio_characteristic_get_descriptors (bleio_characteristic_obj_t * self ) {
@@ -172,7 +177,6 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
172177 }
173178 } else {
174179 // Validate data length for local characteristics only.
175- // TODO: Test this once we can get servers going.
176180 if (self -> fixed_length && bufinfo -> len != self -> max_length ) {
177181 mp_raise_ValueError (MP_ERROR_TEXT ("Value length != required fixed length" ));
178182 }
0 commit comments