Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b66b4056 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi
Browse files

floss: Fix hal host startup race

When hal host starts, it calls 'Read Index List' which will return all
the available indices. Depending on timing, it's possible for a 'Index
Added' to be emitted before the 'Read Index List' command completes. In
this case, we should simply continue (and wait for 'Read Index List')
rather than exit with an error.

Bug: 271899772
Test: mma -j
Test: emerge-brya floss and start/stop stress test on Chromebook
Change-Id: Ice5d13c7b3e5f3a76e97144ae09e4463efec0f51
parent 1c07cf8c
Loading
Loading
Loading
Loading
+19 −11
Original line number Diff line number Diff line
@@ -60,7 +60,6 @@ constexpr uint16_t HCI_DEV_NONE = 0xffff;

/* reference from <kernel>/include/net/bluetooth/mgmt.h */
#define MGMT_OP_INDEX_LIST 0x0003
#define MGMT_EV_INDEX_ADDED 0x0004
#define MGMT_EV_COMMAND_COMP 0x0001
#define MGMT_EV_SIZE_MAX 1024
#define REPEAT_ON_INTR(fn) \
@@ -145,23 +144,32 @@ int waitHciDev(int hci_interface) {
        break;
      }

      if (ev.opcode == MGMT_EV_INDEX_ADDED && ev.index == hci_interface) {
        close(fd);
        return -1;
      } else if (ev.opcode == MGMT_EV_COMMAND_COMP) {
      if (ev.opcode == MGMT_EV_COMMAND_COMP) {
        struct mgmt_event_read_index* cc;
        int i;

        cc = (struct mgmt_event_read_index*)ev.data;

        if (cc->cc_opcode != MGMT_OP_INDEX_LIST || cc->status != 0) continue;
        if (cc->cc_opcode != MGMT_OP_INDEX_LIST) continue;

        // Find the interface in the list of available indices. If unavailable,
        // the result is -1.
        ret = -1;
        if (cc->status == 0) {
          for (i = 0; i < cc->num_intf; i++) {
            if (cc->index[i] == hci_interface) {
            close(fd);
            return 0;
              ret = 0;
              break;
            }
          }
        } else {
          // Unlikely event (probably developer error or driver shut down).
          LOG_ERROR("Failed to read index list: status(%d)", cc->status);
        }

        // Close and return result of Index List.
        close(fd);
        return ret;
      }
    }
  }