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

Commit 2bdbb4a8 authored by Łukasz Rymanowski's avatar Łukasz Rymanowski
Browse files

leaudio: Add implementation of LeAudioHistory

This patch adds separate loging journal for le audio activities.
It will be visiable in dumpsys

adb shell dumpsys bluetooth_manager  | grep -a :le_audio -A 120

Bug: 276693370
Test: manual
Tag: #feature

Change-Id: Ie8bdc6b399b3356f594aaf342271a8438c02e493
parent 0a706152
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ cc_library_static {
        "le_audio/client_parser.cc",
        "le_audio/audio_hal_client/audio_sink_hal_client.cc",
        "le_audio/audio_hal_client/audio_source_hal_client.cc",
        "le_audio/le_audio_log_history.cc",
        "le_audio/le_audio_utils.cc",
        "le_audio/le_audio_set_configuration_provider.cc",
        "le_audio/le_audio_set_configuration_provider_json.cc",
+147 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "le_audio_log_history.h"

#include <base/logging.h>

#include <cstdint>
#include <memory>
#include <string>

#include "gd/common/circular_buffer.h"
#include "gd/common/strings.h"
#include "main/shim/dumpsys.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#include "osi/include/properties.h"

constexpr size_t kMaxLogSize = 255;
constexpr size_t kLeAudioLogHistoryBufferSize = 200;

class TimestampedStringCircularBuffer
    : public bluetooth::common::TimestampedCircularBuffer<std::string> {
 public:
  explicit TimestampedStringCircularBuffer(size_t size)
      : bluetooth::common::TimestampedCircularBuffer<std::string>(size) {}

  void Push(const std::string& s) {
    bluetooth::common::TimestampedCircularBuffer<std::string>::Push(
        s.substr(0, kMaxLogSize));
  }

  template <typename... Args>
  void Push(Args... args) {
    char buf[kMaxLogSize];
    std::snprintf(buf, sizeof(buf), args...);
    bluetooth::common::TimestampedCircularBuffer<std::string>::Push(
        std::string(buf));
  }
};

class LeAudioLogHistoryImpl;
LeAudioLogHistoryImpl* instance;

constexpr size_t kMaxLogHistoryTagLength = 14;
constexpr size_t kMaxLogHistoryMsgLength = 44;
const std::string kTimeFormat("%Y-%m-%d %H:%M:%S");

using Record = bluetooth::common::TimestampedEntry<std::string>;

class LeAudioLogHistoryImpl : public LeAudioLogHistory {
 public:
  ~LeAudioLogHistoryImpl(void) { history_.reset(); }

  LeAudioLogHistoryImpl(void) {
    history_ = std::make_shared<TimestampedStringCircularBuffer>(
        kLeAudioLogHistoryBufferSize);
    CHECK(history_ != nullptr);
    history_->Push(std::string("Initialized le_audio history"));
  }

  void Dump(int fd) {
#define DUMPSYS_TAG "::le_audio"

    LOG_DUMPSYS_TITLE(fd, DUMPSYS_TAG);
    if (history_ == nullptr) {
      return;
    }
    std::vector<Record> history = history_->Pull();
    for (auto& record : history) {
      time_t then = record.timestamp / 1000;
      struct tm tm;
      localtime_r(&then, &tm);
      auto s2 = bluetooth::common::StringFormatTime(kTimeFormat, tm);
      LOG_DUMPSYS(fd, " %s.%03u %s", s2.c_str(),
                  static_cast<unsigned int>(record.timestamp % 1000),
                  record.entry.c_str());
    }
#undef DUMPSYS_TAG
  }

  void AddLogHistory(const std::string& tag, int group_id,
                     const RawAddress& addr, const std::string& msg,
                     const std::string& extra) {
    add_logs_history_common(tag, group_id, addr, msg, extra);
  }

  void AddLogHistory(const std::string& tag, int group_id,
                     const RawAddress& addr, const std::string& msg) {
    AddLogHistory(tag, group_id, addr, msg, std::string());
  }

 private:
  void add_logs_history_common(const std::string& tag, int group_id,
                               const RawAddress& addr, const std::string& msg,
                               const std::string& extra) {
    if (history_ == nullptr) {
      LOG_ERROR(
          "LeAudioLogHistory has not been constructed or already destroyed !");
      return;
    }

    history_->Push("%-*s GID %-3d  %-*s: %-22s %s", kMaxLogHistoryTagLength,
                   tag.substr(0, kMaxLogHistoryTagLength).c_str(), group_id,
                   kMaxLogHistoryMsgLength,
                   msg.substr(0, kMaxLogHistoryMsgLength).c_str(),
                   ADDRESS_TO_LOGGABLE_CSTR(addr), extra.c_str());
  }

  std::shared_ptr<TimestampedStringCircularBuffer> history_{nullptr};
};

LeAudioLogHistory* LeAudioLogHistory::Get(void) {
  if (instance) {
    return instance;
  }
  instance = new LeAudioLogHistoryImpl();
  return instance;
}

void LeAudioLogHistory::DebugDump(int fd) {
  if (instance) {
    instance->Dump(fd);
  }
}

void LeAudioLogHistory::Cleanup(void) {
  if (!instance) {
    return;
  }
  auto ptr = instance;
  instance = nullptr;
  delete ptr;
}
 No newline at end of file
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <string>

#include "types/raw_address.h"

/* Some predefined tags */
static std::string kLogConnectionTag("CONN_STATE");
static std::string kLogStateMachineTag("SM");
static std::string kLogControlPointCmd("ASCS_CP_CMD");
static std::string kLogControlPointNotif("ASCS_CP_NOTIF");
static std::string kLogAseStateNotif("ASE_NOTIF");
static std::string kLogHciEvent("HCI_EVENT");

/* Operations on SM and ASEs */
static std::string kLogStateChangedOp("STATE CHANGED");
static std::string kLogTargetStateChangedOp("TARGET STATE CHANGED");
static std::string kLogAseConfigOp("CODEC_CONFIG: ");
static std::string kLogAseQoSConfigOp("QOS_CONFIG: ");
static std::string kLogAseEnableOp("ENABLE: ");
static std::string kLogAseDisableOp("DISABLE: ");
static std::string kLogAseReleaseOp("RELEASE: ");
static std::string kLogAseSuspendOp("SUSPEND: ");
static std::string kLogAseUpdateMetadataOp("METADATA_UPDATE: ");
static std::string kLogAseStartReadyOp("RCV_START_READY: ");
static std::string kLogAseStopReadyOp("RCV_STOP_READY: ");

/* Operations on CISes */
static std::string kLogCigCreateOp("CIG_CREATE:");
static std::string kLogCigRemoveOp("CIG_REMOVE:");
static std::string kLogCisCreateOp("CIS_CREATE: ");
static std::string kLogCisEstablishedOp("CIS_ESTABLISED: ");
static std::string kLogCisDisconnectOp("CIS_DISCONNECT: ");
static std::string kLogCisDisconnectedOp("CIS_DISCONNECTED: ");
static std::string kLogSetDataPathOp("SET_DATA_PATH: ");
static std::string kLogRemoveDataPathOp("REMOVE_DATA_PATH: ");
static std::string kLogDataPathCompleteOp("DATA_PATH_COMPLETE: ");

class LeAudioLogHistory {
 public:
  virtual ~LeAudioLogHistory(void) = default;
  static LeAudioLogHistory* Get(void);
  static void Cleanup(void);
  static void DebugDump(int fd);

  virtual void AddLogHistory(const std::string& tag, int group_id,
                             const RawAddress& addr,
                             const std::string& msg) = 0;
  virtual void AddLogHistory(const std::string& tag, int group_id,
                             const RawAddress& addr, const std::string& msg,
                             const std::string& extra) = 0;
};
 No newline at end of file