8000 extmod/modbluetooth: Persist reference to NimBLE service instances. · micropython/micropython@f34e16d · GitHub
[go: up one dir, main page]

Skip to content

Commit f34e16d

Browse files
committed
extmod/modbluetooth: Persist reference to NimBLE service instances.
NimBLE doesn't actually copy this data, it requires it to stay live. Only dereference when we register a new set of services. Fixes #5226 This will allow incrementally adding services in the future, so rename `reset` to `append` to make it clearer.
1 parent c7ae8c5 commit f34e16d

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

extmod/modbluetooth.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,9 @@ STATIC mp_obj_t bluetooth_ble_gatts_register_services(mp_obj_t self_in, mp_obj_t
421421
uint16_t **handles = m_new0(uint16_t*, len);
422422
size_t *num_handles = m_new0(size_t, len);
423423

8000 424-
// We always reset the service list, as Nimble has no other option.
425-
// TODO: Add a `reset` or `clear` kwarg (defaulting to True) to make this behavior optional.
426-
int err = mp_bluetooth_gatts_register_service_begin(true);
424+
// TODO: Add a `append` kwarg (defaulting to False) to make this behavior optional.
425+
bool append = false;
426+
int err = mp_bluetooth_gatts_register_service_begin(append);
427427
if (err != 0) {
428428
return bluetooth_handle_errno(err);
429429
}

extmod/modbluetooth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int mp_bluetooth_gap_advertise_start(bool connectable, int32_t interval_us, cons
165165
void mp_bluetooth_gap_advertise_stop(void);
166166

167167
// Start adding services. Must be called before mp_bluetooth_register_service.
168-
int mp_bluetooth_gatts_register_service_begin(bool reset);
168+
int mp_bluetooth_gatts_register_service_begin(bool append);
169169
// // Add a service with the given list of characteristics to the queue to be registered.
170170
// The value_handles won't be valid until after mp_bluetooth_register_service_end is called.
171171
int mp_bluetooth_gatts_register_service(mp_obj_bluetooth_uuid_t *service_uuid, mp_obj_bluetooth_uuid_t **characteristic_uuids, uint8_t *characteristic_flags, mp_obj_bluetooth_uuid_t **descriptor_uuids, uint8_t *descriptor_flags, uint8_t *num_descriptors, uint16_t *handles, size_t num_characteristics);

extmod/modbluetooth_nimble.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static int characteristic_access_cb(uint16_t conn_handle, uint16_t value_handle,
440440
return BLE_ATT_ERR_UNLIKELY;
441441
}
442442

443-
int mp_bluetooth_gatts_register_service_begin(bool reset) {
443+
int mp_bluetooth_gatts_register_service_begin(bool append) {
444444
int ret = ble_gatts_reset();
445445
if (ret != 0) {
446446
return ble_hs_err_to_errno(ret);
@@ -452,7 +452,13 @@ int mp_bluetooth_gatts_register_service_begin(bool reset) {
452452
// By default, just register the default gap service.
453453
ble_svc_gap_init();
454454

455-
MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services = 0;
455+
if (!append) {
456+
// Unref any previous service definitions.
457+
for A398 (int i = 0; i < MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services; ++i) {
458+
MP_STATE_PORT(bluetooth_nimble_root_pointers)->services[i] = NULL;
459+
}
460+
MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services = 0;
461+
}
456462

457463
return 0;
458464
}
@@ -463,11 +469,6 @@ int mp_bluetooth_gatts_register_service_end() {
463469
return ble_hs_err_to_errno(ret);
464470
}
465471

466-
for (int i = 0; i < MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services; ++i) {
467-
MP_STATE_PORT(bluetooth_nimble_root_pointers)->services[i] = NULL;
468-
}
469-
MP_STATE_PORT(bluetooth_nimble_root_pointers)->n_services = 0;
470-
471472
return 0;
472473
}
473474

0 commit comments

Comments
 (0)
0