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

Commit 80f8370e authored by Henri Chataing's avatar Henri Chataing
Browse files

system/gd/storage: Migrate to libbluetooth_log

Test: m com.android.btservices
Bug: 305066880
Flag: EXEMPT, mechanical refactor
Change-Id: Ibb49d336b72f5064cba9aba9855decbecbb41806
parent 0fdfae6a
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "storage/config_cache.h"

#include <bluetooth/log.h>

#include <ios>
#include <sstream>
#include <utility>
@@ -286,7 +288,7 @@ bool ConfigCache::RemoveProperty(const std::string& section, const std::string&

void ConfigCache::ConvertEncryptOrDecryptKeyIfNeeded() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  LOG_INFO("%s", __func__);
  log::info("");
  auto persistent_sections = GetPersistentSections();
  for (const auto& section : persistent_sections) {
    auto section_iter = persistent_devices_.find(section);
@@ -327,7 +329,7 @@ void ConfigCache::RemoveSectionWithProperty(const std::string& property) {
  for (auto* config_section : {&information_sections_, &persistent_devices_}) {
    for (auto it = config_section->begin(); it != config_section->end();) {
      if (it->second.contains(property)) {
        LOG_INFO("Removing persistent section %s with property %s", it->first.c_str(), property.c_str());
        log::info("Removing persistent section {} with property {}", it->first, property);
        it = config_section->erase(it);
        num_persistent_removed++;
        continue;
@@ -337,7 +339,7 @@ void ConfigCache::RemoveSectionWithProperty(const std::string& property) {
  }
  for (auto it = temporary_devices_.begin(); it != temporary_devices_.end();) {
    if (it->second.contains(property)) {
      LOG_INFO("Removing temporary section %s with property %s", it->first.c_str(), property.c_str());
      log::info("Removing temporary section {} with property {}", it->first, property);
      it = temporary_devices_.erase(it);
      continue;
    }
+4 −2
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "storage/config_cache_helper.h"

#include <bluetooth/log.h>

#include "common/numbers.h"
#include "common/strings.h"

@@ -115,7 +117,7 @@ std::optional<std::vector<uint8_t>> ConfigCacheHelper::GetBin(
  }
  auto value = common::FromHexString(*value_str);
  if (!value) {
    LOG_WARN("value_str cannot be parsed to std::vector<uint8_t>");
    log::warn("value_str cannot be parsed to std::vector<uint8_t>");
  }
  return value;
}
+3 −1
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "storage/device.h"

#include <bluetooth/log.h>

#include <algorithm>
#include <limits>

@@ -60,7 +62,7 @@ std::string GetConfigSection(
      // One cannot create a new device just using LE legacy pseudo address
      [[fallthrough]];
    default:
      LOG_ALWAYS_FATAL("Unknown key_address_type %d", static_cast<int>(key_address_type));
      log::fatal("Unknown key_address_type {}", static_cast<int>(key_address_type));
      return "";
  }
}
+6 −4
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "storage/legacy_config_file.h"

#include <bluetooth/log.h>

#include <cerrno>
#include <fstream>
#include <sstream>
@@ -36,7 +38,7 @@ std::optional<ConfigCache> LegacyConfigFile::Read(size_t temp_devices_capacity)
  ASSERT(!path_.empty());
  std::ifstream config_file(path_);
  if (!config_file || !config_file.is_open()) {
    LOG_ERROR("unable to open file '%s', error: %s", path_.c_str(), strerror(errno));
    log::error("unable to open file '{}', error: {}", path_, strerror(errno));
    return std::nullopt;
  }
  [[maybe_unused]] int line_num = 0;
@@ -55,7 +57,7 @@ std::optional<ConfigCache> LegacyConfigFile::Read(size_t temp_devices_capacity)
    }
    if (line.front() == '[') {
      if (line.back() != ']') {
        LOG_WARN("unterminated section name on line %d", line_num);
        log::warn("unterminated section name on line {}", line_num);
        return std::nullopt;
      }
      // Read 'test' from '[text]', hence -2
@@ -63,7 +65,7 @@ std::optional<ConfigCache> LegacyConfigFile::Read(size_t temp_devices_capacity)
    } else {
      auto tokens = common::StringSplit(line, "=", 2);
      if (tokens.size() != 2) {
        LOG_WARN("no key/value separator found on line %d", line_num);
        log::warn("no key/value separator found on line {}", line_num);
        return std::nullopt;
      }
      tokens[0] = common::StringTrim(std::move(tokens[0]));
@@ -80,7 +82,7 @@ bool LegacyConfigFile::Write(const ConfigCache& cache) {

bool LegacyConfigFile::Delete() {
  if (!os::FileExists(path_)) {
    LOG_WARN("Config file at \"%s\" does not exist", path_.c_str());
    log::warn("Config file at \"{}\" does not exist", path_);
    return false;
  }
  return os::RemoveFile(path_);
+9 −6
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#include "storage/storage_module.h"

#include <bluetooth/log.h>

#include <chrono>
#include <ctime>
#include <iomanip>
@@ -127,7 +129,7 @@ void StorageModule::SaveImmediately() {
    ASSERT(os::RenameFile(config_file_path_, config_backup_path_));
#else
    if (!os::RenameFile(config_file_path_, config_backup_path_)) {
      LOG_ERROR("Unable to rename old config to back up name");
      log::error("Unable to rename old config to back up name");
    }
#endif
  }
@@ -136,12 +138,12 @@ void StorageModule::SaveImmediately() {
  ASSERT(LegacyConfigFile::FromPath(config_file_path_).Write(pimpl_->cache_));
#else
  if (!LegacyConfigFile::FromPath(config_file_path_).Write(pimpl_->cache_)) {
    LOG_ERROR("Unable to write config file to disk");
    log::error("Unable to write config file to disk");
  }
#endif
  // 3. now write back up to disk as well
  if (!LegacyConfigFile::FromPath(config_backup_path_).Write(pimpl_->cache_)) {
    LOG_ERROR("Unable to write backup config file");
    log::error("Unable to write backup config file");
  }
  // 4. save checksum if it is running in common criteria mode
  if (bluetooth::os::ParameterProvider::GetBtKeystoreInterface() != nullptr &&
@@ -164,7 +166,7 @@ void StorageModule::Start() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  std::string file_source;
  if (os::GetSystemProperty(kFactoryResetProperty) == "true") {
    LOG_INFO("%s is true, delete config files", kFactoryResetProperty.c_str());
    log::info("{} is true, delete config files", kFactoryResetProperty);
    LegacyConfigFile::FromPath(config_file_path_).Delete();
    LegacyConfigFile::FromPath(config_backup_path_).Delete();
    os::SetSystemProperty(kFactoryResetProperty, "false");
@@ -178,14 +180,15 @@ void StorageModule::Start() {
  bool save_needed = false;
  auto config = LegacyConfigFile::FromPath(config_file_path_).Read(temp_devices_capacity_);
  if (!config || !config->HasSection(kAdapterSection)) {
    LOG_WARN("cannot load config at %s, using backup at %s.", config_file_path_.c_str(), config_backup_path_.c_str());
    log::warn(
        "cannot load config at {}, using backup at {}.", config_file_path_, config_backup_path_);
    config = LegacyConfigFile::FromPath(config_backup_path_).Read(temp_devices_capacity_);
    file_source = "Backup";
    // Make sure to update the file, since it wasn't read from the config_file_path_
    save_needed = true;
  }
  if (!config || !config->HasSection(kAdapterSection)) {
    LOG_WARN("cannot load backup config at %s; creating new empty ones", config_backup_path_.c_str());
    log::warn("cannot load backup config at {}; creating new empty ones", config_backup_path_);
    config.emplace(temp_devices_capacity_, Device::kLinkKeyProperties);
    file_source = "Empty";
  }
Loading