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

Commit 69ff5ec1 authored by Cheng Li's avatar Cheng Li Committed by Gerrit Code Review
Browse files

Merge "Add trace points for HCI packets" into main

parents e1af6bca 34dd83f4
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -24,7 +24,10 @@ source_set("BluetoothHalSources") {
  ]

  configs += [ "//bt/system/gd:gd_defaults" ]
  deps = [ "//bt/system/gd:gd_default_deps" ]
  deps = [
    "//bt/flags:bluetooth_flags_c_lib",
    "//bt/system/gd:gd_default_deps"
  ]
}

source_set("BluetoothHalSources_hci_host") {
+64 −0
Original line number Diff line number Diff line
@@ -14,10 +14,16 @@
 * limitations under the License.
 */

#define ATRACE_TAG ATRACE_TAG_APP

#include "hal/snoop_logger.h"

#include <arpa/inet.h>
#include <bluetooth/log.h>
#include <com_android_bluetooth_flags.h>
#ifdef __ANDROID__
#include <cutils/trace.h>
#endif // __ANDROID__
#include <sys/stat.h>

#include <algorithm>
@@ -28,6 +34,7 @@
#include "common/circular_buffer.h"
#include "common/strings.h"
#include "hal/snoop_logger_common.h"
#include "hci/hci_packets.h"
#include "module_dumper_flatbuffer.h"
#include "os/files.h"
#include "os/parameter_provider.h"
@@ -1123,6 +1130,13 @@ void SnoopLogger::Capture(const HciPacket& immutable_packet, Direction direction
  HciPacket mutable_packet(immutable_packet);
  HciPacket& packet = mutable_packet;
  //////////////////////////////////////////////////////////////////////////

  #ifdef __ANDROID__
  if (com::android::bluetooth::flags::snoop_logger_tracing()) {
    LogTracePoint(packet, direction, type);
  }
  #endif // __ANDROID__

  uint64_t timestamp_us = std::chrono::duration_cast<std::chrono::microseconds>(
                                  std::chrono::system_clock::now().time_since_epoch())
                                  .count();
@@ -1401,5 +1415,55 @@ const ModuleFactory SnoopLogger::Factory = ModuleFactory([]() {
                         IsBtSnoopLogPersisted());
});

#ifdef __ANDROID__
void SnoopLogger::LogTracePoint(const HciPacket& packet, Direction direction, PacketType type) {
  switch (type) {
    case PacketType::EVT: {
      uint8_t evt_code = packet[0];

      if (evt_code == static_cast<uint8_t>(hci::EventCode::LE_META_EVENT) ||
          evt_code == static_cast<uint8_t>(hci::EventCode::VENDOR_SPECIFIC)) {
        uint8_t subevt_code = packet[2];
        std::string message =
                fmt::format("BTSL:{}/{}/{}/{:02x}/{:02x}", static_cast<uint8_t>(type),
                            static_cast<uint8_t>(direction), packet.size(), evt_code, subevt_code);

        ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
      } else {
        std::string message = fmt::format("BTSL:{}/{}/{}/{:02x}", static_cast<uint8_t>(type),
                                          static_cast<uint8_t>(direction), packet.size(), evt_code);

        ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
      }
    } break;
    case PacketType::CMD: {
      uint16_t op_code = packet[0] | (packet[1] << 8);

      std::string message = fmt::format("BTSL:{}/{}/{}/{:04x}", static_cast<uint8_t>(type),
                                        static_cast<uint8_t>(direction), packet.size(), op_code);

      ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
    } break;
    case PacketType::ACL: {
      uint16_t handle = (packet[0] | (packet[1] << 8)) & 0x0fff;
      uint8_t pb_flag = (packet[1] & 0x30) >> 4;

      std::string message = fmt::format("BTSL:{}/{}/{}/{:03x}/{}", static_cast<uint8_t>(type),
                                        static_cast<uint8_t>(direction), packet.size(), handle,
                                        pb_flag);

      ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
    } break;
    case PacketType::ISO:
    case PacketType::SCO: {
      std::string message = fmt::format("BTSL:{}/{}/{}", static_cast<uint8_t>(type),
                                        static_cast<uint8_t>(direction), packet.size());

      ATRACE_INSTANT_FOR_TRACK(LOG_TAG, message.c_str());
    } break;
  }
}
#endif // __ANDROID__

}  // namespace hal
}  // namespace bluetooth
+4 −0
Original line number Diff line number Diff line
@@ -319,6 +319,10 @@ protected:

  std::unique_ptr<SnoopLoggerSocketThread> snoop_logger_socket_thread_;

  #ifdef __ANDROID__
  void LogTracePoint(const HciPacket& packet, Direction direction, PacketType type);
  #endif // __ANDROID__

private:
  static std::string btsnoop_mode_;
  std::string snoop_log_path_;
+2 −0
Original line number Diff line number Diff line
@@ -1741,6 +1741,7 @@ cc_test {
    shared_libs: [
        "libaconfig_storage_read_api_cc",
        "libcrypto",
        "libcutils",
        "server_configurable_flags",
    ],
    sanitize: {
@@ -2313,6 +2314,7 @@ cc_test {
    shared_libs: [
        "libaconfig_storage_read_api_cc",
        "libcrypto",
        "libcutils",
        "server_configurable_flags",
    ],
    sanitize: {