@@ -75,17 +75,23 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
75
75
self -> flags |= BLE_GATT_CHR_F_WRITE_AUTHEN ;
76
76
}
77
77
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
+ }
84
87
}
85
88
}
86
89
self -> current_value_alloc = max_length ;
87
90
self -> current_value_len = 0 ;
88
91
92
+ self -> max_length = max_length ;
93
+ self -> fixed_length = fixed_length ;
94
+
89
95
if (initial_value_bufinfo != NULL ) {
90
96
common_hal_bleio_characteristic_set_value (self , initial_value_bufinfo );
91
97
}
@@ -96,9 +102,6 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
96
102
self -> descriptor_list = NULL ;
97
103
}
98
104
99
- self -> max_length = max_length ;
100
- self -> fixed_length = fixed_length ;
101
-
102
105
if (service -> is_remote ) {
103
106
// If the service is remote, we're buffering incoming notifications and indications.
104
107
self -> handle = handle ;
@@ -109,23 +112,25 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
109
112
}
110
113
111
114
bool common_hal_bleio_characteristic_deinited (bleio_characteristic_obj_t * self ) {
112
- return self -> current_value == NULL ;
115
+ return self -> handle == BLEIO_HANDLE_INVALID ;
113
116
}
114
117
115
118
void common_hal_bleio_characteristic_deinit (bleio_characteristic_obj_t * self ) {
116
119
if (common_hal_bleio_characteristic_deinited (self )) {
117
120
return ;
118
121
}
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
+ }
122
128
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 ;
127
130
}
128
- self -> current_value = NULL ;
131
+
132
+ // Used to indicate deinit.
133
+ self -> handle = BLEIO_HANDLE_INVALID ;
129
134
}
130
135
131
136
mp_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,
172
177
}
173
178
} else {
174
179
// Validate data length for local characteristics only.
175
- // TODO: Test this once we can get servers going.
176
180
if (self -> fixed_length && bufinfo -> len != self -> max_length ) {
177
181
mp_raise_ValueError (MP_ERROR_TEXT ("Value length != required fixed length" ));
178
182
}
0 commit comments