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

Commit 60dc72c5 authored by Myles Watson's avatar Myles Watson
Browse files

Storage: Encapsulate ConfigCache

Bug: 260006855
Test: atest bluetooth_unit_test_gd
Change-Id: I25282fe9da887d642a19f2b0486dcb2578bdcbbd
parent b57e8c97
Loading
Loading
Loading
Loading
+120 −11
Original line number Diff line number Diff line
@@ -106,16 +106,6 @@ Mutation StorageModule::Modify() {
  return Mutation(&pimpl_->cache_, &pimpl_->memory_only_cache_);
}

ConfigCache* StorageModule::GetConfigCache() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return &pimpl_->cache_;
}

ConfigCache* StorageModule::GetMemoryOnlyConfigCache() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return &pimpl_->memory_only_cache_;
}

void StorageModule::SaveDelayed() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  if (pimpl_->has_pending_config_save_) {
@@ -148,6 +138,11 @@ void StorageModule::SaveImmediately() {
  }
}

void StorageModule::Clear() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  pimpl_->cache_.Clear();
}

void StorageModule::ListDependencies(ModuleList* list) const {
    list->add<metrics::CounterMetrics>();
}
@@ -260,7 +255,7 @@ AdapterConfig StorageModule::GetAdapterConfig() {

std::vector<Device> StorageModule::GetBondedDevices() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  auto persistent_sections = GetConfigCache()->GetPersistentSections();
  auto persistent_sections = pimpl_->cache_.GetPersistentSections();
  std::vector<Device> result;
  result.reserve(persistent_sections.size());
  for (const auto& section : persistent_sections) {
@@ -273,5 +268,119 @@ bool StorageModule::is_config_checksum_pass(int check_bit) {
  return ((os::ParameterProvider::GetCommonCriteriaConfigCompareResult() & check_bit) == check_bit);
}

bool StorageModule::HasSection(const std::string& section) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return pimpl_->cache_.HasSection(section);
}

bool StorageModule::HasProperty(const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return pimpl_->cache_.HasProperty(section, property);
}

std::optional<std::string> StorageModule::GetProperty(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return pimpl_->cache_.GetProperty(section, property);
}

void StorageModule::SetProperty(std::string section, std::string property, std::string value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  pimpl_->cache_.SetProperty(section, property, value);
}

std::vector<std::string> StorageModule::GetPersistentSections() const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return pimpl_->cache_.GetPersistentSections();
}

void StorageModule::RemoveSection(const std::string& section) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  pimpl_->cache_.RemoveSection(section);
}

bool StorageModule::RemoveProperty(const std::string& section, const std::string& property) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return pimpl_->cache_.RemoveProperty(section, property);
}

void StorageModule::ConvertEncryptOrDecryptKeyIfNeeded() {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  pimpl_->cache_.ConvertEncryptOrDecryptKeyIfNeeded();
}

void StorageModule::RemoveSectionWithProperty(const std::string& property) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return pimpl_->cache_.RemoveSectionWithProperty(property);
}

void StorageModule::SetBool(const std::string& section, const std::string& property, bool value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ConfigCacheHelper::FromConfigCache(pimpl_->cache_).SetBool(section, property, value);
}

std::optional<bool> StorageModule::GetBool(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return ConfigCacheHelper::FromConfigCache(pimpl_->cache_).GetBool(section, property);
}

void StorageModule::SetUint64(
    const std::string& section, const std::string& property, uint64_t value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ConfigCacheHelper::FromConfigCache(pimpl_->cache_).SetUint64(section, property, value);
}

std::optional<uint64_t> StorageModule::GetUint64(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return ConfigCacheHelper::FromConfigCache(pimpl_->cache_).GetUint64(section, property);
}

void StorageModule::SetUint32(
    const std::string& section, const std::string& property, uint32_t value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ConfigCacheHelper::FromConfigCache(pimpl_->cache_).SetUint32(section, property, value);
}

std::optional<uint32_t> StorageModule::GetUint32(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return ConfigCacheHelper::FromConfigCache(pimpl_->cache_).GetUint32(section, property);
}
void StorageModule::SetInt64(
    const std::string& section, const std::string& property, int64_t value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ConfigCacheHelper::FromConfigCache(pimpl_->cache_).SetInt64(section, property, value);
}
std::optional<int64_t> StorageModule::GetInt64(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return ConfigCacheHelper::FromConfigCache(pimpl_->cache_).GetInt64(section, property);
}

void StorageModule::SetInt(const std::string& section, const std::string& property, int value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ConfigCacheHelper::FromConfigCache(pimpl_->cache_).SetInt(section, property, value);
}

std::optional<int> StorageModule::GetInt(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return ConfigCacheHelper::FromConfigCache(pimpl_->cache_).GetInt(section, property);
}

void StorageModule::SetBin(
    const std::string& section, const std::string& property, const std::vector<uint8_t>& value) {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  ConfigCacheHelper::FromConfigCache(pimpl_->cache_).SetBin(section, property, value);
}

std::optional<std::vector<uint8_t>> StorageModule::GetBin(
    const std::string& section, const std::string& property) const {
  std::lock_guard<std::recursive_mutex> lock(mutex_);
  return ConfigCacheHelper::FromConfigCache(pimpl_->cache_).GetBin(section, property);
}

}  // namespace storage
}  // namespace bluetooth
+32 −2
Original line number Diff line number Diff line
@@ -115,8 +115,6 @@ class StorageModule : public bluetooth::Module {
  std::string ToString() const override;

  friend shim::BtifConfigInterface;
  // For shim layer only
  ConfigCache* GetConfigCache();
  // For unit test only
  ConfigCache* GetMemoryOnlyConfigCache();
  // Normally, underlying config will be saved at most 3 seconds after the first config change in a series of changes
@@ -125,6 +123,8 @@ class StorageModule : public bluetooth::Module {
  // In some cases, one may want to save the config immediately to disk. Call this method with caution as it runs
  // immediately on the calling thread
  void SaveImmediately();
  // remove all content in this config cache, restore it to the state after the explicit constructor
  void Clear();

  // Create the storage module where:
  // - config_file_path is the path to the config file on disk, a .bak file will be created with the original
@@ -138,6 +138,36 @@ class StorageModule : public bluetooth::Module {
      bool is_restricted_mode,
      bool is_single_user_mode);

  bool HasSection(const std::string& section) const;
  bool HasProperty(const std::string& section, const std::string& property) const;

  std::optional<std::string> GetProperty(
      const std::string& section, const std::string& property) const;
  void SetProperty(std::string section, std::string property, std::string value);

  std::vector<std::string> GetPersistentSections() const;

  void RemoveSection(const std::string& section);
  bool RemoveProperty(const std::string& section, const std::string& property);
  void ConvertEncryptOrDecryptKeyIfNeeded();
  // Remove sections with |property| set
  void RemoveSectionWithProperty(const std::string& property);

  void SetBool(const std::string& section, const std::string& property, bool value);
  std::optional<bool> GetBool(const std::string& section, const std::string& property) const;
  void SetUint64(const std::string& section, const std::string& property, uint64_t value);
  std::optional<uint64_t> GetUint64(const std::string& section, const std::string& property) const;
  void SetUint32(const std::string& section, const std::string& property, uint32_t value);
  std::optional<uint32_t> GetUint32(const std::string& section, const std::string& property) const;
  void SetInt64(const std::string& section, const std::string& property, int64_t value);
  std::optional<int64_t> GetInt64(const std::string& section, const std::string& property) const;
  void SetInt(const std::string& section, const std::string& property, int value);
  std::optional<int> GetInt(const std::string& section, const std::string& property) const;
  void SetBin(
      const std::string& section, const std::string& property, const std::vector<uint8_t>& value);
  std::optional<std::vector<uint8_t>> GetBin(
      const std::string& section, const std::string& property) const;

 private:
  struct impl;
  mutable std::recursive_mutex mutex_;
+21 −30
Original line number Diff line number Diff line
@@ -16,15 +16,15 @@

#define LOG_TAG "bt_shim_storage"

#include "main/shim/config.h"

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <memory>

#include "gd/os/log.h"
#include "gd/storage/config_cache_helper.h"
#include "gd/storage/storage_module.h"
#include "main/shim/config.h"
#include "main/shim/entry.h"

using ::bluetooth::shim::GetStorage;
@@ -34,19 +34,18 @@ namespace bluetooth {
namespace shim {

bool BtifConfigInterface::HasSection(const std::string& section) {
  return GetStorage()->GetConfigCache()->HasSection(section);
  return GetStorage()->HasSection(section);
}

bool BtifConfigInterface::HasProperty(const std::string& section,
                                      const std::string& property) {
  return GetStorage()->GetConfigCache()->HasProperty(section, property);
  return GetStorage()->HasProperty(section, property);
}

bool BtifConfigInterface::GetInt(const std::string& section,
                                 const std::string& property, int* value) {
  ASSERT(value != nullptr);
  auto ret = ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
                 .GetInt(section, property);
  auto ret = GetStorage()->GetInt(section, property);
  if (ret) {
    *value = *ret;
  }
@@ -55,8 +54,7 @@ bool BtifConfigInterface::GetInt(const std::string& section,

bool BtifConfigInterface::SetInt(const std::string& section,
                                 const std::string& property, int value) {
  ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
      .SetInt(section, property, value);
  GetStorage()->SetInt(section, property, value);
  return true;
}

@@ -64,8 +62,7 @@ bool BtifConfigInterface::GetUint64(const std::string& section,
                                    const std::string& property,
                                    uint64_t* value) {
  ASSERT(value != nullptr);
  auto ret = ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
                 .GetUint64(section, property);
  auto ret = GetStorage()->GetUint64(section, property);
  if (ret) {
    *value = *ret;
  }
@@ -75,8 +72,7 @@ bool BtifConfigInterface::GetUint64(const std::string& section,
bool BtifConfigInterface::SetUint64(const std::string& section,
                                    const std::string& property,
                                    uint64_t value) {
  ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
      .SetUint64(section, property, value);
  GetStorage()->SetUint64(section, property, value);
  return true;
}

@@ -85,13 +81,13 @@ bool BtifConfigInterface::GetStr(const std::string& section,
                                 int* size_bytes) {
  ASSERT(value != nullptr);
  ASSERT(size_bytes != nullptr);
  auto str = GetStorage()->GetConfigCache()->GetProperty(section, property);
  if (*size_bytes == 0) {
    return HasProperty(section, property);
  }
  auto str = GetStorage()->GetProperty(section, property);
  if (!str) {
    return false;
  }
  if (*size_bytes == 0) {
    return true;
  }
  // std::string::copy does not null-terminate resultant string by default
  // avoided using strlcpy to prevent extra dependency
  *size_bytes = str->copy(value, (*size_bytes - 1));
@@ -102,13 +98,13 @@ bool BtifConfigInterface::GetStr(const std::string& section,

std::optional<std::string> BtifConfigInterface::GetStr(
    const std::string& section, const std::string& property) {
  return GetStorage()->GetConfigCache()->GetProperty(section, property);
  return GetStorage()->GetProperty(section, property);
}

bool BtifConfigInterface::SetStr(const std::string& section,
                                 const std::string& property,
                                 const std::string& value) {
  GetStorage()->GetConfigCache()->SetProperty(section, property, value);
  GetStorage()->SetProperty(section, property, value);
  return true;
}

@@ -118,9 +114,7 @@ bool BtifConfigInterface::GetBin(const std::string& section,
                                 size_t* length) {
  ASSERT(value != nullptr);
  ASSERT(length != nullptr);
  auto value_vec =
      ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
          .GetBin(section, property);
  auto value_vec = GetStorage()->GetBin(section, property);
  if (!value_vec) {
    return false;
  }
@@ -130,9 +124,7 @@ bool BtifConfigInterface::GetBin(const std::string& section,
}
size_t BtifConfigInterface::GetBinLength(const std::string& section,
                                         const std::string& property) {
  auto value_vec =
      ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
          .GetBin(section, property);
  auto value_vec = GetStorage()->GetBin(section, property);
  if (!value_vec) {
    return 0;
  }
@@ -143,28 +135,27 @@ bool BtifConfigInterface::SetBin(const std::string& section,
                                 const uint8_t* value, size_t length) {
  ASSERT(value != nullptr);
  std::vector<uint8_t> value_vec(value, value + length);
  ConfigCacheHelper::FromConfigCache(*GetStorage()->GetConfigCache())
      .SetBin(section, property, value_vec);
  GetStorage()->SetBin(section, property, value_vec);
  return true;
}
bool BtifConfigInterface::RemoveProperty(const std::string& section,
                                         const std::string& property) {
  return GetStorage()->GetConfigCache()->RemoveProperty(section, property);
  return GetStorage()->RemoveProperty(section, property);
}

std::vector<std::string> BtifConfigInterface::GetPersistentDevices() {
  return GetStorage()->GetConfigCache()->GetPersistentSections();
  return GetStorage()->GetPersistentSections();
}

void BtifConfigInterface::ConvertEncryptOrDecryptKeyIfNeeded() {
  GetStorage()->GetConfigCache()->ConvertEncryptOrDecryptKeyIfNeeded();
  GetStorage()->ConvertEncryptOrDecryptKeyIfNeeded();
}

void BtifConfigInterface::Save() { GetStorage()->SaveDelayed(); }

void BtifConfigInterface::Flush() { GetStorage()->SaveImmediately(); }

void BtifConfigInterface::Clear() { GetStorage()->GetConfigCache()->Clear(); }
void BtifConfigInterface::Clear() { GetStorage()->Clear(); }

}  // namespace shim
}  // namespace bluetooth