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

Commit cbbaf1d7 authored by Myles Watson's avatar Myles Watson
Browse files

hci: Timestamp HCI commands for timeouts

Test: Start Bluetooth with COMMAND_PENDING_TIMOUT_MS=2
Change-Id: I393592ede7b9b0aab9c13f6c79756c24edd3d930
parent 56546a4c
Loading
Loading
Loading
Loading
+11 −5
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include <chrono>
#include <mutex>

#include "btcore/include/module.h"
@@ -54,13 +56,14 @@ typedef struct {
  command_status_cb status_callback;
  void* context;
  BT_HDR* command;
  std::chrono::time_point<std::chrono::steady_clock> timestamp;
} waiting_command_t;

// Using a define here, because it can be stringified for the property lookup
#define DEFAULT_STARTUP_TIMEOUT_MS 8000
#define STRING_VALUE_OF(x) #x

// Abort if there is no response to an HCI command within two seconds.
// Abort if there is no response to an HCI command.
static const uint32_t COMMAND_PENDING_TIMEOUT_MS = 2000;

// Our interface
@@ -346,6 +349,7 @@ static void event_command_ready(fixed_queue_t* queue,
    {
      std::lock_guard<std::recursive_mutex> lock(
          commands_pending_response_mutex);
      wait_entry->timestamp = std::chrono::steady_clock::now();
      list_append(commands_pending_response, wait_entry);

      // Send it off
@@ -397,12 +401,14 @@ static void command_timed_out(UNUSED_ATTR void* context) {
    waiting_command_t* wait_entry = reinterpret_cast<waiting_command_t*>(
        list_front(commands_pending_response));

    int wait_time_ms =
        std::chrono::duration_cast<std::chrono::milliseconds>(
            std::chrono::steady_clock::now() - wait_entry->timestamp)
            .count();
    // We shouldn't try to recover the stack from this command timeout.
    // If it's caused by a software bug, fix it. If it's a hardware bug, fix it.
    LOG_ERROR(
        LOG_TAG,
        "%s hci layer timeout waiting for response to a command. opcode: 0x%x",
        __func__, wait_entry->opcode);
    LOG_ERROR(LOG_TAG, "%s: Waited %d ms for a response to opcode: 0x%x",
              __func__, wait_time_ms, wait_entry->opcode);
    LOG_EVENT_INT(BT_HCI_TIMEOUT_TAG_NUM, wait_entry->opcode);
    lock.unlock();
  }