@@ -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-
298289hci_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
459442hci_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
0 commit comments