Skip to content

Commit dfe50d0

Browse files
committed
Fix a few bugs
1 parent da48ab0 commit dfe50d0

File tree

7 files changed

+33
-51
lines changed

7 files changed

+33
-51
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,15 +804,25 @@ void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self) {
804804
self->now_advertising = false;
805805
self->extended_advertising = false;
806806
self->circuitpython_advertising = false;
807+
807808
int result = hci_le_set_advertising_enable(BT_HCI_LE_ADV_DISABLE);
808-
// OK if we're already stopped.
809-
if (result != BT_HCI_ERR_CMD_DISALLOWED) {
809+
// OK if we're already stopped. There seems to be an ESP32 HCI bug:
810+
// If advertising is already off, then LE_SET_ADV_ENABLE does not return a response.
811+
if (result != HCI_RESPONSE_TIMEOUT) {
810812
check_hci_error(result);
811813
}
812814

813815
//TODO startup CircuitPython advertising again.
814816
}
815817

818+
// Note that something stopped advertising, such as a connection happening.
819+
//Don't ask the adapter to stop.
820+
void bleio_adapter_advertising_was_stopped(bleio_adapter_obj_t *self) {
821+
self->now_advertising = false;
822+
self->extended_advertising = false;
823+
self->circuitpython_advertising = false;
824+
}
825+
816826
bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self) {
817827
check_enabled(self);
818828

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ typedef struct _bleio_adapter_obj_t {
8888
} bleio_adapter_obj_t;
8989

9090
uint16_t bleio_adapter_add_attribute(bleio_adapter_obj_t *adapter, mp_obj_t *attribute);
91+
void bleio_adapter_advertising_was_stopped(bleio_adapter_obj_t *self);
9192
mp_obj_t* bleio_adapter_get_attribute(bleio_adapter_obj_t *adapter, uint16_t handle);
9293
uint16_t bleio_adapter_max_attribute_handle(bleio_adapter_obj_t *adapter);
9394
void bleio_adapter_background(bleio_adapter_obj_t* adapter);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ void check_hci_error(hci_result_t result) {
4848
case HCI_OK:
4949
return;
5050

51-
case HCI_NO_RESPONSE:
52-
mp_raise_bleio_BluetoothError(translate("No HCI command response received"));
53-
return;
54-
55-
case HCI_READ_TIMEOUT:
51+
case HCI_RESPONSE_TIMEOUT:
5652
mp_raise_bleio_BluetoothError(translate("Timeout waiting for HCI response"));
5753
return;
5854

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ bool att_disconnect(uint16_t conn_handle) {
224224
}
225225

226226
hci_disconnect(conn_handle);
227-
hci_poll_for_incoming_pkt_timeout(timeout);
228227

229228
// Confirm we're now disconnected.
230229
return !att_handle_is_connected(conn_handle);
@@ -508,13 +507,13 @@ void att_add_connection(uint16_t handle, uint8_t role, bt_addr_le_t *peer_addr,
508507
}
509508

510509

511-
void att_remove_connection(uint16_t handle, uint8_t reason) {
510+
void att_remove_connection(uint16_t conn_handle, uint8_t reason) {
512511
(void) reason;
513512
int peer_index = -1;
514513
int peer_count = 0;
515514

516515
for (size_t i = 0; i < BLEIO_TOTAL_CONNECTION_COUNT; i++) {
517-
if (bleio_connections[i].conn_handle == handle) {
516+
if (bleio_connections[i].conn_handle == conn_handle) {
518517
peer_index = i;
519518
}
520519

@@ -532,7 +531,7 @@ void att_remove_connection(uint16_t handle, uint8_t reason) {
532531

533532
// Clear CCCD values on disconnect.
534533
size_t max_attribute_handle = bleio_adapter_max_attribute_handle(&common_hal_bleio_adapter_obj);
535-
for (size_t i = 1; handle <= max_attribute_handle; i++) {
534+
for (size_t handle = 1; handle <= max_attribute_handle; handle++) {
536535
mp_obj_t attribute_obj = bleio_adapter_get_attribute(&common_hal_bleio_adapter_obj, handle);
537536

538537
uint16_t zero = 0;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ uint16_t att_conn_handle(bt_addr_le_t *addr);
4949
uint16_t att_mtu(uint16_t handle);
5050
void att_add_connection(uint16_t handle, uint8_t role, bt_addr_le_t *peer_addr, uint16_t interval, uint16_t latency, uint16_t supervision_timeout, uint8_t master_clock_accuracy);
5151
void att_process_data(uint16_t conn_handle, uint8_t dlen, uint8_t data[]);
52-
void att_remove_connection(uint16_t handle, uint8_t reason);
52+
void att_remove_connection(uint16_t conn_handle, uint8_t reason);
5353
void att_set_max_mtu(uint16_t max_mtu);
5454
void att_set_timeout(unsigned long timeout);
5555
void att_write_cmd(uint16_t conn_handle, uint16_t handle, const uint8_t* data, uint8_t data_len);

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

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ STATIC void process_evt_pkt(size_t pkt_len, uint8_t pkt_data[])
175175

176176
att_remove_connection(disconn_complete->handle, disconn_complete->reason);
177177
//FIX L2CAPSignaling.removeConnection(disconn_complete->handle, disconn_complete->reason);
178-
hci_le_set_advertising_enable(0x01);
179178
break;
180179
}
181180

@@ -233,6 +232,12 @@ STATIC void process_evt_pkt(size_t pkt_len, uint8_t pkt_data[])
233232
uint8_t *le_evt = pkt->params + sizeof (struct bt_hci_evt_le_meta_event);
234233

235234
if (meta_evt->subevent == BT_HCI_EVT_LE_CONN_COMPLETE) {
235+
// Advertising stops when connection occurs.
236+
// We don't tell the adapter to stop, because stopping advertising
237+
// when it's already stopped seems to exercise a bug in the ESP32 HCI code:
238+
// It doesn't return a response.
239+
bleio_adapter_advertising_was_stopped(&common_hal_bleio_adapter_obj);
240+
236241
struct bt_hci_evt_le_conn_complete *le_conn_complete =
237242
(struct bt_hci_evt_le_conn_complete *) le_evt;
238243

@@ -281,29 +286,11 @@ void bleio_hci_reset(void) {
281286
bleio_att_reset();
282287
}
283288

284-
hci_result_t hci_poll_for_incoming_pkt_timeout(uint32_t timeout_msecs) {
285-
uint64_t start = supervisor_ticks_ms64();
286-
287-
hci_result_t result = HCI_OK;
288-
289-
while (supervisor_ticks_ms64() - start < timeout_msecs) {
290-
result = hci_poll_for_incoming_pkt();
291-
RUN_BACKGROUND_TASKS;
292-
}
293-
294-
return result;
295-
}
296-
297-
298289
hci_result_t hci_poll_for_incoming_pkt(void) {
299290
if (hci_poll_in_progress) {
300291
return HCI_OK;
301292
}
302-
common_hal_mcu_disable_interrupts();
303-
if (!hci_poll_in_progress) {
304-
hci_poll_in_progress = true;
305-
}
306-
common_hal_mcu_enable_interrupts();
293+
hci_poll_in_progress = true;
307294

308295
// Assert RTS low to say we're ready to read data.
309296
common_hal_digitalio_digitalinout_set_value(common_hal_bleio_adapter_obj.rts_digitalinout, false);
@@ -431,7 +418,7 @@ STATIC hci_result_t send_command(uint16_t opcode, uint8_t params_len, void* para
431418
// Wait for a response. Note that other packets may be received that are not
432419
// command responses.
433420
uint64_t start = supervisor_ticks_ms64();
434-
while (1) {
421+
while (supervisor_ticks_ms64() - start < RESPONSE_TIMEOUT_MSECS) {
435422
result = hci_poll_for_incoming_pkt();
436423
if (result != HCI_OK) {
437424
// I/O error.
@@ -442,18 +429,14 @@ STATIC hci_result_t send_command(uint16_t opcode, uint8_t params_len, void* para
442429
// If this is definitely a response to the command that was sent,
443430
// return the status value, which will will be
444431
// BT_HCI_ERR_SUCCESS (0x00) if the command succeeded,
445-
// or a BT_HCI_ERR_x value (> 0x00) if there ws a problem.
432+
// or a BT_HCI_ERR_x value (> 0x00) if there was a problem.
446433
return cmd_response_status;
447434
}
448-
449-
if (supervisor_ticks_ms64() - start > RESPONSE_TIMEOUT_MSECS) {
450-
return HCI_READ_TIMEOUT;
451-
}
452435
RUN_BACKGROUND_TASKS;
453436
}
454437

455438
// No I/O error, but no response sent back in time.
456-
return HCI_NO_RESPONSE;
439+
return HCI_RESPONSE_TIMEOUT;
457440
}
458441

459442
hci_result_t hci_send_acl_pkt(uint16_t handle, uint8_t cid, uint8_t data_len, uint8_t *data) {
@@ -528,11 +511,6 @@ hci_result_t hci_read_rssi(uint16_t handle, int *rssi) {
528511
int result = send_command(BT_HCI_OP_READ_RSSI, sizeof(handle), &handle);
529512
if (result == HCI_OK) {
530513
struct bt_hci_rp_read_rssi *response = (struct bt_hci_rp_read_rssi *) cmd_response_data;
531-
if (response->handle != handle) {
532-
// Handle doesn't match.
533-
return HCI_NO_RESPONSE;
534-
}
535-
536514
*rssi = response->rssi;
537515
}
538516

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ typedef struct _bleio_adapter_obj_t bleio_adapter_obj_t;
3232
// or is it > 0 and is an HCI command status value (see hci_include/hci_err.h)
3333
typedef int hci_result_t;
3434
#define HCI_OK (0)
35-
#define HCI_NO_RESPONSE (-1)
36-
#define HCI_READ_TIMEOUT (-2)
37-
#define HCI_WRITE_TIMEOUT (-3)
38-
#define HCI_READ_ERROR (-4)
39-
#define HCI_WRITE_ERROR (-5)
40-
#define HCI_ATT_ERROR (-6)
35+
#define HCI_RESPONSE_TIMEOUT (-1)
36+
#define HCI_WRITE_TIMEOUT (-2)
37+
#define HCI_READ_ERROR (-3)
38+
#define HCI_WRITE_ERROR (-4)
39+
#define HCI_ATT_ERROR (-5)
4140

4241
void bleio_hci_reset(void);
4342

@@ -65,7 +64,6 @@ hci_result_t hci_le_set_scan_parameters(uint8_t scan_type, uint16_t interval, ui
6564
hci_result_t hci_le_set_scan_response_data(uint8_t length, uint8_t data[]);
6665

6766
hci_result_t hci_poll_for_incoming_pkt(void);
68-
hci_result_t hci_poll_for_incoming_pkt_timeout(uint32_t timeout_msecs);
6967

7068
hci_result_t hci_read_bd_addr(bt_addr_t *addr);
7169
hci_result_t hci_read_buffer_size(uint16_t *acl_max_len, uint8_t *sco_max_len, uint16_t *acl_max_num, uint16_t *sco_max_num);

0 commit comments

Comments
 (0)