Skip to content

Commit 06f3b40

Browse files
committed
fix #3228 for nrf; still needs to be fixed for HCI; tested
1 parent 92cf560 commit 06f3b40

File tree

17 files changed

+85
-96
lines changed

17 files changed

+85
-96
lines changed

devices/ble_hci/common-hal/_bleio/Characteristic.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
4747
self->props = props;
4848
self->read_perm = read_perm;
4949
self->write_perm = write_perm;
50-
self->descriptor_list = NULL;
50+
self->descriptor_linked_list = mp_obj_new_list(0, NULL);
51+
self->watchers_list = mp_obj_new_list(0, NULL);
5152

5253
const mp_int_t max_length_max = 512;
5354
if (max_length < 0 || max_length > max_length_max) {
@@ -67,8 +68,8 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
6768
}
6869
}
6970

70-
bleio_descriptor_obj_t *common_hal_bleio_characteristic_get_descriptor_list(bleio_characteristic_obj_t *self) {
71-
return self->descriptor_list;
71+
bleio_descriptor_obj_t *common_hal_bleio_characteristic_get_descriptor_linked_list(bleio_characteristic_obj_t *self) {
72+
return self->descriptor_linked_list;
7273
}
7374

7475
bleio_service_obj_t *common_hal_bleio_characteristic_get_service(bleio_characteristic_obj_t *self) {
@@ -153,8 +154,8 @@ void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *
153154
self->service->end_handle = descriptor->handle;
154155

155156
// Link together all the descriptors for this characteristic.
156-
descriptor->next = self->descriptor_list;
157-
self->descriptor_list = descriptor;
157+
descriptor->next = self->descriptor_linked_list;
158+
self->descriptor_linked_list = descriptor;
158159
}
159160

160161
void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self, bool notify, bool indicate) {
@@ -201,3 +202,20 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
201202
// }
202203

203204
}
205+
206+
bool bleio_characteristic_set_local_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo) {
207+
if (self->fixed_length && bufinfo->len != self->max_length) {
208+
return false;
209+
}
210+
if (bufinfo->len > self->max_length) {
211+
bool
212+
}
213+
214+
mp_buffer_info_t char_bufinfo;
215+
if (!mp_get_buffer(characteristic->value, &bufinfo, MP_BUFFER_WRITE)) {
216+
return false;
217+
}
218+
memcpy(&char_bufinfo->buf, bufinfo->buf, bufinfo->len);
219+
220+
for (size_t i; i < characteristic->set_callbacks.
221+
}

devices/ble_hci/common-hal/_bleio/Characteristic.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ typedef struct _bleio_characteristic_obj {
4040
bleio_service_obj_t *service;
4141
bleio_uuid_obj_t *uuid;
4242
mp_obj_t value;
43+
mp_obj_list_t watcher_list;
44+
mp_obj_list_t descriptor_linked_list;
4345
uint16_t max_length;
4446
bool fixed_length;
4547
uint16_t decl_handle;
4648
uint16_t handle; // Should be decl_handle+1.
4749
bleio_characteristic_properties_t props;
4850
bleio_attribute_security_mode_t read_perm;
4951
bleio_attribute_security_mode_t write_perm;
50-
bleio_descriptor_obj_t *descriptor_list;
52+
bleio_descriptor_obj_t *descriptor_linked_list;
5153
uint16_t user_desc_handle;
5254
uint16_t cccd_handle;
5355
uint16_t sccd_handle;

devices/ble_hci/common-hal/_bleio/Connection.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ static volatile bool m_discovery_successful;
319319
// }
320320

321321
void bleio_connection_clear(bleio_connection_internal_t *self) {
322-
self->remote_service_list = NULL;
322+
self->remote_service_linked_list = NULL;
323323

324324
//FIX self->conn_handle = BLE_CONN_HANDLE_INVALID;
325325
self->pair_status = PAIR_NOT_PAIRED;
@@ -449,7 +449,7 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
449449
// }
450450

451451
// STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *response, bleio_connection_internal_t* connection) {
452-
// bleio_service_obj_t* tail = connection->remote_service_list;
452+
// bleio_service_obj_t* tail = connection->remote_service_linked_list;
453453

454454
// for (size_t i = 0; i < response->count; ++i) {
455455
// ble_gattc_service_t *gattc_service = &response->services[i];
@@ -482,7 +482,7 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
482482
// tail = service;
483483
// }
484484

485-
// connection->remote_service_list = tail;
485+
// connection->remote_service_linked_list = tail;
486486

487487
// if (response->count > 0) {
488488
// m_discovery_successful = true;
@@ -581,7 +581,7 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
581581
// GATT_MAX_DATA_LENGTH, false, mp_const_empty_bytes);
582582
// descriptor->handle = gattc_desc->handle;
583583

584-
// mp_obj_list_append(m_desc_discovery_characteristic->descriptor_list, MP_OBJ_FROM_PTR(descriptor));
584+
// mp_obj_list_append(m_desc_discovery_characteristic->descriptor_linked_list, MP_OBJ_FROM_PTR(descriptor));
585585
// }
586586

587587
// if (response->count > 0) {
@@ -622,7 +622,7 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
622622
// ble_drv_add_event_handler(discovery_on_ble_evt, self);
623623

624624
// // Start over with an empty list.
625-
// self->remote_service_list = NULL;
625+
// self->remote_service_linked_list = NULL;
626626

627627
// if (service_uuids_whitelist == mp_const_none) {
628628
// // List of service UUID's not given, so discover all available services.
@@ -634,7 +634,7 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
634634

635635
// // Get the most recently discovered service, and then ask for services
636636
// // whose handles start after the last attribute handle inside that service.
637-
// const bleio_service_obj_t *service = self->remote_service_list;
637+
// const bleio_service_obj_t *service = self->remote_service_linked_list;
638638
// next_service_start_handle = service->end_handle + 1;
639639
// }
640640
// } else {
@@ -658,7 +658,7 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
658658
// }
659659

660660

661-
// bleio_service_obj_t *service = self->remote_service_list;
661+
// bleio_service_obj_t *service = self->remote_service_linked_list;
662662
// while (service != NULL) {
663663
// // Skip the service if it had an unknown (unregistered) UUID.
664664
// if (service->uuid == NULL) {
@@ -707,14 +707,14 @@ void common_hal_bleio_connection_set_connection_interval(bleio_connection_intern
707707

708708
// // Stop when we go past the end of the range of handles for this service or
709709
// // discovery call returns nothing.
710-
// // discover_next_descriptors() appends to the descriptor_list.
710+
// // discover_next_descriptors() appends to the descriptor_linked_list.
711711
// while (next_desc_start_handle <= service->end_handle &&
712712
// next_desc_start_handle <= next_desc_end_handle &&
713713
// discover_next_descriptors(self, characteristic,
714714
// next_desc_start_handle, next_desc_end_handle)) {
715715
// // Get the most recently discovered descriptor, and then ask for descriptors
716716
// // whose handles start after that descriptor's handle.
717-
// const bleio_descriptor_obj_t *descriptor = characteristic->descriptor_list;
717+
// const bleio_descriptor_obj_t *descriptor = characteristic->descriptor_linked_list;
718718
// next_desc_start_handle = descriptor->handle + 1;
719719
// }
720720
// }
@@ -730,8 +730,8 @@ mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_conne
730730
//FIX discover_remote_services(self->connection, service_uuids_whitelist);
731731
bleio_connection_ensure_connected(self);
732732
// Convert to a tuple and then clear the list so the callee will take ownership.
733-
mp_obj_tuple_t *services_tuple = service_linked_list_to_tuple(self->connection->remote_service_list);
734-
self->connection->remote_service_list = NULL;
733+
mp_obj_tuple_t *services_tuple = service_linked_list_to_tuple(self->connection->remote_service_linked_list);
734+
self->connection->remote_service_linked_list = NULL;
735735

736736
return services_tuple;
737737
}

devices/ble_hci/common-hal/_bleio/Connection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef struct {
5050
uint16_t conn_handle;
5151
bool is_central;
5252
// Remote services discovered when this peripheral is acting as a client.
53-
bleio_service_obj_t *remote_service_list;
53+
bleio_service_obj_t *remote_service_linked_list;
5454
// The advertising data and scan response buffers are held by us, not by the SD, so we must
5555
// maintain them and not change it. If we need to change the contents during advertising,
5656
// there are tricks to get the SD to notice (see DevZone - TBS).

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enable
352352
if (enabled) {
353353
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
354354
bleio_connection_internal_t *connection = &bleio_connections[i];
355+
// Reset connection.
356+
bleio_connection_clear(connection);
355357
connection->conn_handle = BLE_CONN_HANDLE_INVALID;
356358
}
357359
bleio_adapter_reset_name(self);
@@ -576,6 +578,7 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre
576578
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
577579
bleio_connection_internal_t *connection = &bleio_connections[i];
578580
if (connection->conn_handle == conn_handle) {
581+
connection->is_central = true;
579582
return bleio_connection_new_from_internal(connection);
580583
}
581584
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
9090
self->props = props;
9191
self->read_perm = read_perm;
9292
self->write_perm = write_perm;
93-
self->descriptor_list = NULL;
93+
self->descriptor_list = mp_obj_new_list(0, NULL);
9494

9595
const mp_int_t max_length_max = fixed_length ? BLE_GATTS_FIX_ATTR_LEN_MAX : BLE_GATTS_VAR_ATTR_LEN_MAX;
9696
if (max_length < 0 || max_length > max_length_max) {
@@ -111,8 +111,8 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
111111
}
112112
}
113113

114-
bleio_descriptor_obj_t *common_hal_bleio_characteristic_get_descriptor_list(bleio_characteristic_obj_t *self) {
115-
return self->descriptor_list;
114+
mp_obj_tuple_t *common_hal_bleio_characteristic_get_descriptors(bleio_characteristic_obj_t *self) {
115+
return mp_obj_new_tuple(self->descriptor_list->len, self->descriptor_list->items);
116116
}
117117

118118
bleio_service_obj_t *common_hal_bleio_characteristic_get_service(bleio_characteristic_obj_t *self) {
@@ -218,8 +218,8 @@ void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *
218218

219219
check_nrf_error(sd_ble_gatts_descriptor_add(self->handle, &desc_attr, &descriptor->handle));
220220

221-
descriptor->next = self->descriptor_list;
222-
self->descriptor_list = descriptor;
221+
mp_obj_list_append(MP_OBJ_FROM_PTR(self->descriptor_list),
222+
MP_OBJ_FROM_PTR(descriptor));
223223
}
224224

225225
void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self, bool notify, bool indicate) {

ports/nrf/common-hal/_bleio/Characteristic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ typedef struct _bleio_characteristic_obj {
4646
bleio_characteristic_properties_t props;
4747
bleio_attribute_security_mode_t read_perm;
4848
bleio_attribute_security_mode_t write_perm;
49-
bleio_descriptor_obj_t *descriptor_list;
49+
mp_obj_list_t *descriptor_list;
5050
uint16_t user_desc_handle;
5151
uint16_t cccd_handle;
5252
uint16_t sccd_handle;

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,11 @@ bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
325325
}
326326

327327
void bleio_connection_clear(bleio_connection_internal_t *self) {
328-
self->remote_service_list = NULL;
328+
mp_obj_list_clear(MP_OBJ_FROM_PTR(self->remote_service_list));
329329

330330
self->conn_handle = BLE_CONN_HANDLE_INVALID;
331331
self->pair_status = PAIR_NOT_PAIRED;
332+
self->is_central = false;
332333
bonding_clear_keys(&self->bonding_keys);
333334
}
334335

@@ -452,8 +453,6 @@ STATIC bool discover_next_descriptors(bleio_connection_internal_t* connection, b
452453
}
453454

454455
STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *response, bleio_connection_internal_t* connection) {
455-
bleio_service_obj_t* tail = connection->remote_service_list;
456-
457456
for (size_t i = 0; i < response->count; ++i) {
458457
ble_gattc_service_t *gattc_service = &response->services[i];
459458

@@ -481,12 +480,10 @@ STATIC void on_primary_srv_discovery_rsp(ble_gattc_evt_prim_srvc_disc_rsp_t *res
481480
service->uuid = NULL;
482481
}
483482

484-
service->next = tail;
485-
tail = service;
483+
mp_obj_list_append(MP_OBJ_FROM_PTR(connection->remote_service_list),
484+
MP_OBJ_FROM_PTR(service));
486485
}
487486

488-
connection->remote_service_list = tail;
489-
490487
if (response->count > 0) {
491488
m_discovery_successful = true;
492489
}
@@ -528,7 +525,8 @@ STATIC void on_char_discovery_rsp(ble_gattc_evt_char_disc_rsp_t *response, bleio
528525
GATT_MAX_DATA_LENGTH, false, // max_length, fixed_length: values may not matter for gattc
529526
NULL);
530527

531-
mp_obj_list_append(m_char_discovery_service->characteristic_list, MP_OBJ_FROM_PTR(characteristic));
528+
mp_obj_list_append(MP_OBJ_FROM_PTR(m_char_discovery_service->characteristic_list),
529+
MP_OBJ_FROM_PTR(characteristic));
532530
}
533531

534532
if (response->count > 0) {
@@ -584,7 +582,8 @@ STATIC void on_desc_discovery_rsp(ble_gattc_evt_desc_disc_rsp_t *response, bleio
584582
GATT_MAX_DATA_LENGTH, false, mp_const_empty_bytes);
585583
descriptor->handle = gattc_desc->handle;
586584

587-
mp_obj_list_append(m_desc_discovery_characteristic->descriptor_list, MP_OBJ_FROM_PTR(descriptor));
585+
mp_obj_list_append(MP_OBJ_FROM_PTR(m_desc_discovery_characteristic->descriptor_list),
586+
MP_OBJ_FROM_PTR(descriptor));
588587
}
589588

590589
if (response->count > 0) {
@@ -625,7 +624,7 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
625624
ble_drv_add_event_handler(discovery_on_ble_evt, self);
626625

627626
// Start over with an empty list.
628-
self->remote_service_list = NULL;
627+
self->remote_service_list = mp_obj_new_list(0, NULL);
629628

630629
if (service_uuids_whitelist == mp_const_none) {
631630
// List of service UUID's not given, so discover all available services.
@@ -637,7 +636,9 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
637636

638637
// Get the most recently discovered service, and then ask for services
639638
// whose handles start after the last attribute handle inside that service.
640-
const bleio_service_obj_t *service = self->remote_service_list;
639+
// There must be at least one if discover_next_services() returned true.
640+
const bleio_service_obj_t *service =
641+
self->remote_service_list->items[self->remote_service_list->len - 1];
641642
next_service_start_handle = service->end_handle + 1;
642643
}
643644
} else {
@@ -661,11 +662,10 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
661662
}
662663

663664

664-
bleio_service_obj_t *service = self->remote_service_list;
665-
while (service != NULL) {
665+
for (size_t i = 0; i < self->remote_service_list->len; i++) {
666+
bleio_service_obj_t *service = MP_OBJ_TO_PTR(self->remote_service_list->items[i]);
666667
// Skip the service if it had an unknown (unregistered) UUID.
667668
if (service->uuid == NULL) {
668-
service = service->next;
669669
continue;
670670
}
671671

@@ -677,9 +677,9 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
677677
while (next_char_start_handle <= service->end_handle &&
678678
discover_next_characteristics(self, service, next_char_start_handle)) {
679679

680-
681680
// Get the most recently discovered characteristic, and then ask for characteristics
682681
// whose handles start after the last attribute handle inside that characteristic.
682+
// There must be at least one if discover_next_characteristics() returned true.
683683
const bleio_characteristic_obj_t *characteristic =
684684
MP_OBJ_TO_PTR(service->characteristic_list->items[service->characteristic_list->len - 1]);
685685

@@ -717,24 +717,26 @@ STATIC void discover_remote_services(bleio_connection_internal_t *self, mp_obj_t
717717
next_desc_start_handle, next_desc_end_handle)) {
718718
// Get the most recently discovered descriptor, and then ask for descriptors
719719
// whose handles start after that descriptor's handle.
720-
const bleio_descriptor_obj_t *descriptor = characteristic->descriptor_list;
720+
// There must be at least one if discover_next_descriptors() returned true.
721+
const bleio_descriptor_obj_t *descriptor =
722+
characteristic->descriptor_list->items[characteristic->descriptor_list->len - 1];
721723
next_desc_start_handle = descriptor->handle + 1;
722724
}
723725
}
724-
service = service->next;
725726
}
726727

727728
// This event handler is no longer needed.
728729
ble_drv_remove_event_handler(discovery_on_ble_evt, self);
729-
730730
}
731731

732732
mp_obj_tuple_t *common_hal_bleio_connection_discover_remote_services(bleio_connection_obj_t *self, mp_obj_t service_uuids_whitelist) {
733733
discover_remote_services(self->connection, service_uuids_whitelist);
734734
bleio_connection_ensure_connected(self);
735735
// Convert to a tuple and then clear the list so the callee will take ownership.
736-
mp_obj_tuple_t *services_tuple = service_linked_list_to_tuple(self->connection->remote_service_list);
737-
self->connection->remote_service_list = NULL;
736+
mp_obj_tuple_t *services_tuple =
737+
mp_obj_new_tuple(self->connection->remote_service_list->len,
738+
self->connection->remote_service_list->items);
739+
mp_obj_list_clear(MP_OBJ_FROM_PTR(self->connection->remote_service_list));
738740

739741
return services_tuple;
740742
}

ports/nrf/common-hal/_bleio/Connection.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef struct {
5353
uint16_t conn_handle;
5454
bool is_central;
5555
// Remote services discovered when this peripheral is acting as a client.
56-
bleio_service_obj_t *remote_service_list;
56+
mp_obj_list_t *remote_service_list;
5757
// The advertising data and scan response buffers are held by us, not by the SD, so we must
5858
// maintain them and not change it. If we need to change the contents during advertising,
5959
// there are tricks to get the SD to notice (see DevZone - TBS).
@@ -67,7 +67,7 @@ typedef struct {
6767
ble_gap_conn_params_t conn_params;
6868
volatile bool conn_params_updating;
6969
uint16_t mtu;
70-
// Request that CCCD values for this conenction be saved, using sys_attr values.
70+
// Request that CCCD values for this connection be saved, using sys_attr values.
7171
volatile bool do_bond_cccds;
7272
// Request that security key info for this connection be saved.
7373
volatile bool do_bond_keys;
@@ -83,6 +83,7 @@ typedef struct {
8383
uint8_t disconnect_reason;
8484
} bleio_connection_obj_t;
8585

86+
void bleio_connection_clear(bleio_connection_internal_t *self);
8687
bool connection_on_ble_evt(ble_evt_t *ble_evt, void *self_in);
8788

8889
uint16_t bleio_connection_get_conn_handle(bleio_connection_obj_t *self);

ports/nrf/common-hal/_bleio/Descriptor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ typedef struct _bleio_descriptor_obj {
4747
uint16_t handle;
4848
bleio_attribute_security_mode_t read_perm;
4949
bleio_attribute_security_mode_t write_perm;
50-
struct _bleio_descriptor_obj* next;
5150
} bleio_descriptor_obj_t;
5251

5352
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_DESCRIPTOR_H

0 commit comments

Comments
 (0)