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

Commit 99431617 authored by Pavlin Radoslavov's avatar Pavlin Radoslavov Committed by Andre Eisenbach
Browse files

Add a mutex to guard all btsnoop operations

After switching to HIDL, capture() can be called by multiple threads.
Capturing a packet and writing to the btsnoop/btsnooz logs
require multiple operations, hence we need a mutex to make those
operations atomic.
Previously, there were occasional btsnoop_hci.log files
with corrupted data as reported by Wireshark.

Test: Manual - captured multiple btsnoops_hci.log files.
Change-Id: Ib89d9156ad7e27a7dee652f71a0cf38c85d4447c
parent ac6177ae
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@

#define LOG_TAG "bt_snoop"

#include <mutex>

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
@@ -56,6 +58,7 @@ static int logfile_fd = INVALID_FD;
static bool module_started;
static bool is_logging;
static bool logging_enabled_via_api;
static std::mutex btsnoop_mutex;

// TODO(zachoverflow): merge btsnoop and btsnoop_net together
void btsnoop_net_open();
@@ -100,6 +103,7 @@ static void set_api_wants_to_log(bool value) {
static void capture(const BT_HDR* buffer, bool is_received) {
  uint8_t* p = const_cast<uint8_t*>(buffer->data + buffer->offset);

  std::lock_guard<std::mutex> lock(btsnoop_mutex);
  uint64_t timestamp_us = time_gettimeofday_us();
  btsnoop_mem_capture(buffer, timestamp_us);

@@ -133,6 +137,8 @@ const btsnoop_t* btsnoop_get_interface() {
// Internal functions

static void update_logging() {
  std::lock_guard<std::mutex> lock(btsnoop_mutex);

  bool should_log = module_started && (logging_enabled_via_api ||
                                       stack_config->get_btsnoop_turned_on());