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

Commit cc2a54cf authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Add btif_config_(get/set)_uint64

Bug: 69623109
Test: added ConfigTest.config_get_uint64
Change-Id: Id2285a6f961f72d28912defad66663ddd05fb741
parent c12f1dc6
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -35,6 +35,10 @@ bool btif_config_get_int(const std::string& section, const std::string& key,
                         int* value);
bool btif_config_set_int(const std::string& section, const std::string& key,
                         int value);
bool btif_config_get_uint64(const std::string& section, const std::string& key,
                            uint64_t* value);
bool btif_config_set_uint64(const std::string& section, const std::string& key,
                            uint64_t value);
bool btif_config_get_str(const std::string& section, const std::string& key,
                         char* value, int* size_bytes);
bool btif_config_set_str(const std::string& section, const std::string& key,
+22 −0
Original line number Diff line number Diff line
@@ -267,6 +267,28 @@ bool btif_config_set_int(const std::string& section, const std::string& key,
  return true;
}

bool btif_config_get_uint64(const std::string& section, const std::string& key,
                            uint64_t* value) {
  CHECK(config != NULL);
  CHECK(value != NULL);

  std::unique_lock<std::mutex> lock(config_lock);
  bool ret = config_has_key(*config, section, key);
  if (ret) *value = config_get_uint64(*config, section, key, *value);

  return ret;
}

bool btif_config_set_uint64(const std::string& section, const std::string& key,
                            uint64_t value) {
  CHECK(config != NULL);

  std::unique_lock<std::mutex> lock(config_lock);
  config_set_uint64(config.get(), section, key, value);

  return true;
}

bool btif_config_get_str(const std::string& section, const std::string& key,
                         char* value, int* size_bytes) {
  CHECK(config != NULL);
+11 −0
Original line number Diff line number Diff line
@@ -71,6 +71,12 @@ bool config_has_key(const config_t& config, const std::string& section,
int config_get_int(const config_t& config, const std::string& section,
                   const std::string& key, int def_value);

// 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);

// 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
// function returns |def_value|.
@@ -91,6 +97,11 @@ const std::string* config_get_string(const config_t& config,
void config_set_int(config_t* config, const std::string& section,
                    const std::string& key, int value);

// Sets a uint64_t value for the |key| in |section|. If |key| or |section| do
// not already exist, this function creates them. |config| must not be NULL.
void config_set_uint64(config_t* config, const std::string& section,
                       const std::string& key, uint64_t value);

// Sets a boolean value for the |key| in |section|. If |key| or |section| do
// not already exist, this function creates them. |config| must not be NULL.
void config_set_bool(config_t* config, const std::string& section,
+15 −0
Original line number Diff line number Diff line
@@ -114,6 +114,16 @@ int config_get_int(const config_t& config, const std::string& section,
  return (*endptr == '\0') ? ret : def_value;
}

uint64_t 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;

  char* endptr;
  uint64_t ret = strtoull(entry->value.c_str(), &endptr, 0);
  return (*endptr == '\0') ? ret : def_value;
}

bool 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);
@@ -140,6 +150,11 @@ void config_set_int(config_t* config, const std::string& section,
  config_set_string(config, section, key, std::to_string(value));
}

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

void config_set_bool(config_t* config, const std::string& section,
                     const std::string& key, bool value) {
  config_set_string(config, section, key, value ? "true" : "false");
+17 −0
Original line number Diff line number Diff line
@@ -44,6 +44,9 @@ version = 0x1111
[DID]                                                                                \n\
[DID]                                                                                \n\
version = 0x1436                                                                     \n\
                                                                                     \n\
HiSyncId = 18446744073709551615                                                      \n\
HiSyncId2 = 15001900                                                                 \n\
";

class ConfigTest : public AllocationTestHarness {
@@ -126,6 +129,20 @@ TEST_F(ConfigTest, config_get_int_default) {
  EXPECT_EQ(config_get_int(*config, "DID", "primaryRecord", 123), 123);
}

TEST_F(ConfigTest, config_get_uint64) {
  std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
  EXPECT_EQ(config_get_uint64(*config, "DID", "HiSyncId", 0),
            0xFFFFFFFFFFFFFFFF);
  EXPECT_EQ(config_get_uint64(*config, "DID", "HiSyncId2", 0),
            uint64_t(15001900));
}

TEST_F(ConfigTest, config_get_uint64_default) {
  std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
  EXPECT_EQ(config_get_uint64(*config, "DID", "primaryRecord", 123),
            uint64_t(123));
}

TEST_F(ConfigTest, config_remove_section) {
  std::unique_ptr<config_t> config = config_new(CONFIG_FILE);
  EXPECT_TRUE(config_remove_section(config.get(), "DID"));