Skip to content

Commit 567c273

Browse files
committed
ESP32-S3 BLE: set up Characteristic properly during discovery
1 parent af1b879 commit 567c273

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,21 @@ STATIC void _new_connection(uint16_t conn_handle) {
284284
esp_ble_tx_power_set(conn_handle, ESP_PWR_LVL_N0);
285285

286286

287-
// Find an empty connection. One must always be available because the SD has the same
287+
// Find an empty connection. One should always be available because the SD has the same
288288
// total connection limit.
289-
bleio_connection_internal_t *connection;
289+
bleio_connection_internal_t *connection = NULL;
290290
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
291291
connection = &bleio_connections[i];
292292
if (connection->conn_handle == BLEIO_HANDLE_INVALID) {
293293
break;
294294
}
295295
}
296+
297+
// Shouldn't happen, but just return if no connection available.
298+
if (!connection) {
299+
return;
300+
}
301+
296302
connection->conn_handle = conn_handle;
297303
connection->connection_obj = mp_const_none;
298304
connection->pair_status = PAIR_NOT_PAIRED;

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,25 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
5050
self->props = props;
5151
self->read_perm = read_perm;
5252
self->write_perm = write_perm;
53-
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
53+
54+
if (initial_value_bufinfo != NULL) {
55+
// Copy the initial value if it's on the heap. Otherwise it's internal and we may not be able
56+
// to allocate.
57+
self->current_value_len = initial_value_bufinfo->len;
58+
if (gc_alloc_possible()) {
59+
if (gc_nbytes(initial_value_bufinfo->buf) > 0) {
60+
uint8_t *initial_value = m_malloc(self->current_value_len);
61+
self->current_value_alloc = self->current_value_len;
62+
memcpy(initial_value, initial_value_bufinfo->buf, self->current_value_len);
63+
self->current_value = initial_value;
64+
} else {
65+
self->current_value_alloc = 0;
66+
self->current_value = initial_value_bufinfo->buf;
67+
}
68+
} else {
69+
self->current_value = initial_value_bufinfo->buf;
70+
}
71+
}
5472

5573
if (gc_alloc_possible()) {
5674
self->descriptor_list = mp_obj_new_list(0, NULL);

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int bleio_connection_event_cb(struct ble_gap_event *event, void *connection_in)
6363
connection->pair_status = PAIR_NOT_PAIRED;
6464

6565
#if CIRCUITPY_VERBOSE_BLE
66-
mp_printf(&mp_plat_print, "disconnected %02x\n", event->disconnect.reason);
66+
mp_printf(&mp_plat_print, "event->disconnect.reason: 0x%x\n", event->disconnect.reason);
6767
#endif
6868
if (connection->connection_obj != mp_const_none) {
6969
bleio_connection_obj_t *obj = connection->connection_obj;
@@ -209,6 +209,7 @@ STATIC int _discovered_characteristic_cb(uint16_t conn_handle,
209209
_last_discovery_status = error->status;
210210
xTaskNotifyGive(discovery_task);
211211
}
212+
return 0;
212213
}
213214
// If any of these memory allocations fail, we set _last_discovery_status
214215
// and let the process continue.
@@ -233,11 +234,14 @@ STATIC int _discovered_characteristic_cb(uint16_t conn_handle,
233234
((chr->properties & BLE_GATT_CHR_PROP_WRITE_NO_RSP) != 0 ? CHAR_PROP_WRITE_NO_RESPONSE : 0);
234235

235236
// Call common_hal_bleio_characteristic_construct() to initialize some fields and set up evt handler.
237+
mp_buffer_info_t mp_const_empty_bytes_bufinfo;
238+
mp_get_buffer_raise(mp_const_empty_bytes, &mp_const_empty_bytes_bufinfo, MP_BUFFER_READ);
239+
236240
common_hal_bleio_characteristic_construct(
237241
characteristic, service, chr->val_handle, uuid,
238242
props, SECURITY_MODE_OPEN, SECURITY_MODE_OPEN,
239243
0, false, // max_length, fixed_length: values don't matter for gattc
240-
mp_const_empty_bytes,
244+
&mp_const_empty_bytes_bufinfo,
241245
NULL);
242246
// Set def_handle directly since it is only used in discovery.
243247
characteristic->def_handle = chr->def_handle;
@@ -260,6 +264,7 @@ STATIC int _discovered_descriptor_cb(uint16_t conn_handle,
260264
_last_discovery_status = error->status;
261265
}
262266
xTaskNotifyGive(discovery_task);
267+
return 0;
263268
}
264269
// If any of these memory allocations fail, we set _last_discovery_status
265270
// and let the process continue.

ports/espressif/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#ifndef MICROPY_INCLUDED_ESPRESSIF_MPCONFIGPORT_H
2929
#define MICROPY_INCLUDED_ESPRESSIF_MPCONFIGPORT_H
3030

31+
// Enable for debugging.
32+
// #define CIRCUITPY_VERBOSE_BLE (1)
33+
3134
#define MICROPY_NLR_THUMB (0)
3235

3336
#define MICROPY_USE_INTERNAL_PRINTF (0)

py/circuitpy_mpconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ void background_callback_run_all(void);
470470
#error "boot counter requires CIRCUITPY_NVM enabled"
471471
#endif
472472

473+
#ifndef CIRCUITPY_VERBOSE_BLE
473474
#define CIRCUITPY_VERBOSE_BLE 0
475+
#endif
474476

475477
// Display the Blinka logo in the REPL on displayio displays.
476478
#ifndef CIRCUITPY_REPL_LOGO

0 commit comments

Comments
 (0)