8000 Merge pull request #9676 from dhalbert/bleio-char-empty-current-value · tannewt/circuitpython@29f64c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29f64c7

Browse files
authored
Merge pull request micropython#9676 from dhalbert/bleio-char-empty-current-value
espressif/common-hal/_bleio/Characteristic.c: handle empty initial value
2 parents 66e289d + f030c37 commit 29f64c7

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

ports/espressif/common-hal/_bleio/Characteristic.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

111114
bool 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

115118
void 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

131136
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,
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

Comments
 (0)
0