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

Commit 14c7f254 authored by Zach Johnson's avatar Zach Johnson
Browse files

Format gd/storage with new clang-format

find storage -regex '.*\.\(cc\|h\|tpp\)' -exec clang-format -style=file -i {} \;

Test: cert/run --host
Bug: 156858180
Tag: #gd-refactor
Change-Id: I2e971765d736479b21906727c6b763b5eeb390b8
parent 60335747
Loading
Loading
Loading
Loading
+37 −20
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#define LOG_TAG "bt_storage"

#include "storage/legacy.h"

#include "storage/legacy_osi_config.h"

namespace bluetooth {
@@ -34,8 +35,8 @@ struct LegacyModule::impl {
    handler->Post(common::BindOnce(std::move(callback), filename, std::move(config)));
  }

  void config_write(const std::string filename, const config_t config, LegacyWriteConfigCallback callback,
                    os::Handler* handler) {
  void config_write(
      const std::string filename, const config_t config, LegacyWriteConfigCallback callback, os::Handler* handler) {
    handler->Post(common::BindOnce(std::move(callback), filename, legacy::osi::config::config_save(config, filename)));
  }

@@ -44,7 +45,10 @@ struct LegacyModule::impl {
        common::BindOnce(std::move(callback), filename, legacy::osi::config::checksum_read(filename.c_str())));
  }

  void checksum_write(const std::string filename, const std::string checksum, LegacyWriteChecksumCallback callback,
  void checksum_write(
      const std::string filename,
      const std::string checksum,
      LegacyWriteChecksumCallback callback,
      os::Handler* handler) {
    handler->Post(
        common::BindOnce(std::move(callback), filename, legacy::osi::config::checksum_save(checksum, filename)));
@@ -70,28 +74,41 @@ void storage::LegacyModule::impl::Start() {

void storage::LegacyModule::impl::Stop() {}

void storage::LegacyModule::ConfigRead(const std::string filename, LegacyReadConfigCallback callback,
                                       os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(&LegacyModule::impl::config_read, common::Unretained(pimpl_.get()), filename,
                                      std::move(callback), handler));
void storage::LegacyModule::ConfigRead(
    const std::string filename, LegacyReadConfigCallback callback, os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(
      &LegacyModule::impl::config_read, common::Unretained(pimpl_.get()), filename, std::move(callback), handler));
}

void storage::LegacyModule::ConfigWrite(const std::string filename, const config_t& config,
                                        LegacyWriteConfigCallback callback, os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(&LegacyModule::impl::config_write, common::Unretained(pimpl_.get()), filename,
                                      config, std::move(callback), handler));
void storage::LegacyModule::ConfigWrite(
    const std::string filename, const config_t& config, LegacyWriteConfigCallback callback, os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(
      &LegacyModule::impl::config_write,
      common::Unretained(pimpl_.get()),
      filename,
      config,
      std::move(callback),
      handler));
}

void storage::LegacyModule::ChecksumRead(const std::string filename, LegacyReadChecksumCallback callback,
                                         os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(&LegacyModule::impl::checksum_read, common::Unretained(pimpl_.get()), filename,
                                      std::move(callback), handler));
void storage::LegacyModule::ChecksumRead(
    const std::string filename, LegacyReadChecksumCallback callback, os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(
      &LegacyModule::impl::checksum_read, common::Unretained(pimpl_.get()), filename, std::move(callback), handler));
}

void storage::LegacyModule::ChecksumWrite(const std::string filename, const std::string checksum,
                                          LegacyWriteChecksumCallback callback, os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(&LegacyModule::impl::checksum_write, common::Unretained(pimpl_.get()), filename,
                                      checksum, std::move(callback), handler));
void storage::LegacyModule::ChecksumWrite(
    const std::string filename,
    const std::string checksum,
    LegacyWriteChecksumCallback callback,
    os::Handler* handler) {
  GetHandler()->Post(common::BindOnce(
      &LegacyModule::impl::checksum_write,
      common::Unretained(pimpl_.get()),
      filename,
      checksum,
      std::move(callback),
      handler));
}

/**
+8 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#pragma once

#include <stdbool.h>

#include <array>
#include <cstdint>
#include <list>
@@ -39,10 +40,13 @@ using LegacyWriteChecksumCallback = common::OnceCallback<void(const std::string,
class LegacyModule : public bluetooth::Module {
 public:
  void ConfigRead(const std::string filename, LegacyReadConfigCallback callback, os::Handler* handler);
  void ConfigWrite(const std::string filename, const config_t& config, LegacyWriteConfigCallback callback,
                   os::Handler* handler);
  void ConfigWrite(
      const std::string filename, const config_t& config, LegacyWriteConfigCallback callback, os::Handler* handler);
  void ChecksumRead(const std::string filename, LegacyReadChecksumCallback callback, os::Handler* handler);
  void ChecksumWrite(const std::string filename, const std::string checksum, LegacyWriteChecksumCallback callback,
  void ChecksumWrite(
      const std::string filename,
      const std::string checksum,
      LegacyWriteChecksumCallback callback,
      os::Handler* handler);

  static const ModuleFactory Factory;
+26 −25
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@

#include <base/files/file_util.h>
#include <fcntl.h>

#include <string>

#include "os/log.h"
@@ -26,11 +27,12 @@ namespace {

bool config_parse(FILE* fp, config_t* config);

template <typename T,
template <
    typename T,
    class = typename std::enable_if<std::is_same<config_t, typename std::remove_const<T>::type>::value>>
auto section_find(T& config, const std::string& section) {
  return std::find_if(config.sections.begin(), config.sections.end(),
                      [&section](const section_t& sec) { return sec.name == section; });
  return std::find_if(
      config.sections.begin(), config.sections.end(), [&section](const section_t& sec) { return sec.name == section; });
}

const entry_t* entry_find(const config_t& config, const std::string& section, const std::string& key) {
@@ -148,13 +150,13 @@ bool bluetooth::legacy::osi::config::config_has_section(const config_t& config,
  return (section_find(config, section) != config.sections.end());
}

bool bluetooth::legacy::osi::config::config_has_key(const config_t& config, const std::string& section,
                                                    const std::string& key) {
bool bluetooth::legacy::osi::config::config_has_key(
    const config_t& config, const std::string& section, const std::string& key) {
  return (entry_find(config, section, key) != nullptr);
}

int bluetooth::legacy::osi::config::config_get_int(const config_t& config, const std::string& section,
                                                   const std::string& key, int def_value) {
int bluetooth::legacy::osi::config::config_get_int(
    const config_t& config, const std::string& section, const std::string& key, int def_value) {
  const entry_t* entry = entry_find(config, section, key);
  if (!entry) return def_value;

@@ -163,8 +165,8 @@ int bluetooth::legacy::osi::config::config_get_int(const config_t& config, const
  return (*endptr == '\0') ? ret : def_value;
}

uint64_t bluetooth::legacy::osi::config::config_get_uint64(const config_t& config, const std::string& section,
                                                           const std::string& key, uint64_t def_value) {
uint64_t bluetooth::legacy::osi::config::config_get_uint64(
    const config_t& config, const std::string& section, const std::string& key, uint64_t def_value) {
  const entry_t* entry = entry_find(config, section, key);
  if (!entry) return def_value;

@@ -173,8 +175,8 @@ uint64_t bluetooth::legacy::osi::config::config_get_uint64(const config_t& confi
  return (*endptr == '\0') ? ret : def_value;
}

bool bluetooth::legacy::osi::config::config_get_bool(const config_t& config, const std::string& section,
                                                     const std::string& key, bool def_value) {
bool bluetooth::legacy::osi::config::config_get_bool(
    const config_t& config, const std::string& section, const std::string& key, bool def_value) {
  const entry_t* entry = entry_find(config, section, key);
  if (!entry) return def_value;

@@ -184,32 +186,31 @@ bool bluetooth::legacy::osi::config::config_get_bool(const config_t& config, con
  return def_value;
}

const std::string* bluetooth::legacy::osi::config::config_get_string(const config_t& config, const std::string& section,
                                                                     const std::string& key,
                                                                     const std::string* def_value) {
const std::string* bluetooth::legacy::osi::config::config_get_string(
    const config_t& config, const std::string& section, const std::string& key, const std::string* def_value) {
  const entry_t* entry = entry_find(config, section, key);
  if (!entry) return def_value;

  return &entry->value;
}

void bluetooth::legacy::osi::config::config_set_int(config_t* config, const std::string& section,
                                                    const std::string& key, int value) {
void bluetooth::legacy::osi::config::config_set_int(
    config_t* config, const std::string& section, const std::string& key, int value) {
  legacy::osi::config::config_set_string(config, section, key, std::to_string(value));
}

void bluetooth::legacy::osi::config::config_set_uint64(config_t* config, const std::string& section,
                                                       const std::string& key, uint64_t value) {
void bluetooth::legacy::osi::config::config_set_uint64(
    config_t* config, const std::string& section, const std::string& key, uint64_t value) {
  legacy::osi::config::config_set_string(config, section, key, std::to_string(value));
}

void bluetooth::legacy::osi::config::config_set_bool(config_t* config, const std::string& section,
                                                     const std::string& key, bool value) {
void bluetooth::legacy::osi::config::config_set_bool(
    config_t* config, const std::string& section, const std::string& key, bool value) {
  legacy::osi::config::config_set_string(config, section, key, value ? "true" : "false");
}

void bluetooth::legacy::osi::config::config_set_string(config_t* config, const std::string& section,
                                                       const std::string& key, const std::string& value) {
void bluetooth::legacy::osi::config::config_set_string(
    config_t* config, const std::string& section, const std::string& key, const std::string& value) {
  CHECK(config);

  auto sec = section_find(*config, section);
@@ -247,8 +248,8 @@ bool bluetooth::legacy::osi::config::config_remove_section(config_t* config, con
  return true;
}

bool bluetooth::legacy::osi::config::config_remove_key(config_t* config, const std::string& section,
                                                       const std::string& key) {
bool bluetooth::legacy::osi::config::config_remove_key(
    config_t* config, const std::string& section, const std::string& key) {
  CHECK(config);
  auto sec = section_find(*config, section);
  if (sec == config->sections.end()) return false;
+5 −4
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
// This code wraps osi/include/config.h

#include <stdbool.h>

#include <list>
#include <memory>
#include <string>
@@ -84,8 +85,8 @@ int config_get_int(const config_t& config, const std::string& section, const std
// Returns the uint64_t value for a given |key| in |section|. If |section|
// or |key| do not exist, or the value cannot be fully converted to an integer,
// this function returns |def_value|.
uint64_t config_get_uint64(const config_t& config, const std::string& section, const std::string& key,
                           uint64_t def_value);
uint64_t config_get_uint64(
    const config_t& config, const std::string& section, const std::string& key, uint64_t def_value);

// Returns the boolean value for a given |key| in |section|. If |section|
// or |key| do not exist, or the value cannot be converted to a boolean, this
@@ -96,8 +97,8 @@ bool config_get_bool(const config_t& config, const std::string& section, const s
// |key| do not exist, this function returns |def_value|. The returned string
// is owned by the config module and must not be freed or modified. |def_value|
// may be NULL.
const std::string* config_get_string(const config_t& config, const std::string& section, const std::string& key,
                                     const std::string* def_value);
const std::string* config_get_string(
    const config_t& config, const std::string& section, const std::string& key, const std::string* def_value);

// Sets an integral value for the |key| in |section|. If |key| or |section| do
// not already exist, this function creates them. |config| must not be NULL.
+7 −8
Original line number Diff line number Diff line
@@ -16,13 +16,12 @@

#include "storage/legacy.h"

#include <base/files/file_util.h>
#include <gtest/gtest.h>

#include "os/handler.h"
#include "os/thread.h"

#include <base/files/file_util.h>

#ifdef OS_ANDROID
constexpr char CONFIG_FILE[] = "/data/local/tmp/config_test.conf";
#else
@@ -124,8 +123,8 @@ TEST_F(LegacyStorageTest, Module) {}
TEST_F(LegacyStorageTest, ConfigRead) {
  std::string filename(CONFIG_FILE);
  auto future = promise.get_future();
  legacy_module_->ConfigRead(filename, common::BindOnce(&LegacyStorageTest::OnConfigRead, common::Unretained(this)),
                             handler_);
  legacy_module_->ConfigRead(
      filename, common::BindOnce(&LegacyStorageTest::OnConfigRead, common::Unretained(this)), handler_);
  future.wait();
}

@@ -133,16 +132,16 @@ TEST_F(LegacyStorageTest, ConfigWrite) {
  std::string filename(CONFIG_FILE);
  config_t config;
  auto future = promise.get_future();
  legacy_module_->ConfigWrite(filename, config,
                              common::BindOnce(&LegacyStorageTest::OnConfigWrite, common::Unretained(this)), handler_);
  legacy_module_->ConfigWrite(
      filename, config, common::BindOnce(&LegacyStorageTest::OnConfigWrite, common::Unretained(this)), handler_);
  future.wait();
}

TEST_F(LegacyStorageTest, ChecksumRead) {
  std::string filename(CONFIG_FILE);
  auto future = promise.get_future();
  legacy_module_->ChecksumRead(filename, common::BindOnce(&LegacyStorageTest::OnChecksumRead, common::Unretained(this)),
                               handler_);
  legacy_module_->ChecksumRead(
      filename, common::BindOnce(&LegacyStorageTest::OnChecksumRead, common::Unretained(this)), handler_);
  future.wait();
}