Skip to content

Commit af1b879

Browse files
committed
fix heap warning in espressif Adapter.c;fix error codes
1 parent f3920c4 commit af1b879

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ bool common_hal_bleio_connection_get_connected(bleio_connection_obj_t *self) {
128128
}
129129

130130
void common_hal_bleio_connection_disconnect(bleio_connection_internal_t *self) {
131+
// Second argument is an HCI reason, not an HS error code.
131132
ble_gap_terminate(self->conn_handle, BLE_ERR_REM_USER_CONN_TERM);
132133
}
133134

@@ -162,9 +163,9 @@ STATIC int _discovered_service_cb(uint16_t conn_handle,
162163
void *arg) {
163164
bleio_connection_internal_t *self = (bleio_connection_internal_t *)arg;
164165

165-
if (error->status != BLE_ERR_SUCCESS) {
166+
if (error->status != 0) {
166167
// Keep the first error in case it's due to memory.
167-
if (_last_discovery_status == BLE_ERR_SUCCESS) {
168+
if (_last_discovery_status == 0) {
168169
_last_discovery_status = error->status;
169170
xTaskNotifyGive(discovery_task);
170171
}
@@ -173,7 +174,7 @@ STATIC int _discovered_service_cb(uint16_t conn_handle,
173174

174175
// If any of these memory allocations fail, we set _last_discovery_status
175176
// and let the process continue.
176-
if (_last_discovery_status != BLE_ERR_SUCCESS) {
177+
if (_last_discovery_status != 0) {
177178
return 0;
178179
}
179180
bleio_service_obj_t *service = mp_obj_malloc(bleio_service_obj_t, &bleio_service_type);
@@ -202,16 +203,16 @@ STATIC int _discovered_characteristic_cb(uint16_t conn_handle,
202203
void *arg) {
203204
bleio_service_obj_t *service = (bleio_service_obj_t *)arg;
204205

205-
if (error->status != BLE_ERR_SUCCESS) {
206+
if (error->status != 0) {
206207
// Keep the first error in case it's due to memory.
207-
if (_last_discovery_status == BLE_ERR_SUCCESS) {
208+
if (_last_discovery_status == 0) {
208209
_last_discovery_status = error->status;
209210
xTaskNotifyGive(discovery_task);
210211
}
211212
}
212213
// If any of these memory allocations fail, we set _last_discovery_status
213214
// and let the process continue.
214-
if (_last_discovery_status != BLE_ERR_SUCCESS) {
215+
if (_last_discovery_status != 0) {
215216
return 0;
216217
}
217218

@@ -253,16 +254,16 @@ STATIC int _discovered_descriptor_cb(uint16_t conn_handle,
253254
void *arg) {
254255
bleio_characteristic_obj_t *characteristic = (bleio_characteristic_obj_t *)arg;
255256

256-
if (error->status != BLE_ERR_SUCCESS) {
257+
if (error->status != 0) {
257258
// Keep the first error in case it's due to memory.
258-
if (_last_discovery_status == BLE_ERR_SUCCESS) {
259+
if (_last_discovery_status == 0) {
259260
_last_discovery_status = error->status;
260261
}
261262
xTaskNotifyGive(discovery_task);
262263
}
263264
// If any of these memory allocations fail, we set _last_discovery_status
264265
// and let the process continue.
265-
if (_last_discovery_status != BLE_ERR_SUCCESS) {
266+
if (_last_discovery_status != 0) {
266267
return 0;
267268
}
268269

@@ -306,7 +307,7 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
306307

307308
discovery_task = xTaskGetCurrentTaskHandle();
308309
if (service_uuids_whitelist == mp_const_none) {
309-
_last_discovery_status = BLE_ERR_SUCCESS;
310+
_last_discovery_status = 0;
310311
CHECK_NIMBLE_ERROR(ble_gattc_disc_all_svcs(self->conn_handle, _discovered_service_cb, self));
311312

312313
// Wait for sync.
@@ -324,7 +325,7 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
324325
}
325326
bleio_uuid_obj_t *uuid = MP_OBJ_TO_PTR(uuid_obj);
326327

327-
_last_discovery_status = BLE_ERR_SUCCESS;
328+
_last_discovery_status = 0;
328329
// Make sure we start with a clean notification state
329330
ulTaskNotifyValueClear(discovery_task, 0xffffffff);
330331
CHECK_NIMBLE_ERROR(ble_gattc_disc_svc_by_uuid(self->conn_handle, &uuid->nimble_ble_uuid.u,
@@ -340,7 +341,7 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
340341
for (size_t i = 0; i < self->remote_service_list->len; i++) {
341342
bleio_service_obj_t *service = MP_OBJ_TO_PTR(self->remote_service_list->items[i]);
342343

343-
_last_discovery_status = BLE_ERR_SUCCESS;
344+
_last_discovery_status = 0;
344345
CHECK_NIMBLE_ERROR(ble_gattc_disc_all_chrs(self->conn_handle,
345346
service->start_handle,
346347
service->end_handle,
@@ -375,7 +376,7 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
375376
continue;
376377
}
377378

378-
_last_discovery_status = BLE_ERR_SUCCESS;
379+
_last_discovery_status = 0;
379380
CHECK_NIMBLE_ERROR(ble_gattc_disc_all_dscs(self->conn_handle, characteristic->handle,
380381
end_handle,
381382
_discovered_descriptor_cb, characteristic));

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ void check_nimble_error(int rc, const char *file, size_t line) {
112112
}
113113

114114
void check_ble_error(int error_code, const char *file, size_t line) {
115-
if (error_code == BLE_ERR_SUCCESS) {
115+
// 0 means success. For BLE_HS_* codes, there is no defined "SUCCESS" value.
116+
if (error_code == 0) {
116117
return;
117118
}
118119
switch (error_code) {

shared-bindings/_bleio/Adapter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ STATIC mp_obj_t bleio_adapter_start_scan(size_t n_args, const mp_obj_t *pos_args
353353
prefix_bufinfo.len = 0;
354354
if (args[ARG_prefixes].u_obj != MP_OBJ_NULL) {
355355
mp_get_buffer_raise(args[ARG_prefixes].u_obj, &prefix_bufinfo, MP_BUFFER_READ);
356-
if (gc_nbytes(prefix_bufinfo.buf) == 0) {
356+
// An empty buffer may not be on the heap, but that doesn't matter.
357+
if (prefix_bufinfo.len > 0 && gc_nbytes(prefix_bufinfo.buf) == 0) {
357358
mp_raise_ValueError(MP_ERROR_TEXT("Prefix buffer must be on the heap"));
358359
}
359360
}

0 commit comments

Comments
 (0)