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

Commit 008aec6a authored by Hui Peng's avatar Hui Peng Committed by Soma Jagannathan
Browse files

Clean up GetSystemProperty.* APIs

1. Factor out duplicated implementation of
   GetSystemPropertyUint32 on host/linux/android
2. Add GetSystemPropertyBool and factor out the code
   for retrieving bool properties in system/gd/hal/snoop_logger.cc

Bug: 263323082
Test: refactoring CL. Existing unit tests still pass.

Dirty-cherry because of missing GetSystemPropertyUint32

Merged-In: I5a24533f2ce6d146e528661e7cf6b86008537f3f
(cherry picked from commit c4d022fc)
Change-Id: I074e35aecefe3f1f8fc1e95d1fd0401f99d57f55
parent 785140c9
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -445,9 +445,8 @@ size_t SnoopLogger::GetMaxPacketsPerFile() {
size_t SnoopLogger::GetMaxPacketsPerBuffer() {
  // We want to use at most 256 KB memory for btsnooz log for release builds
  // and 512 KB memory for userdebug/eng builds
  auto is_debuggable = os::GetSystemProperty(kIsDebuggableProperty);
  size_t btsnooz_max_memory_usage_bytes =
      ((is_debuggable.has_value() && common::StringTrim(is_debuggable.value()) == "1") ? 1024 : 256) * 1024;
  auto is_debuggable = os::GetSystemPropertyBool(kIsDebuggableProperty, false);
  size_t btsnooz_max_memory_usage_bytes = (is_debuggable ? 1024 : 256) * 1024;
  // Calculate max number of packets based on max memory usage and max packet size
  return btsnooz_max_memory_usage_bytes / kDefaultBtSnoozMaxBytesPerPacket;
}
@@ -457,8 +456,8 @@ std::string SnoopLogger::GetBtSnoopMode() {
  // In userdebug/eng build, it can also be overwritten by modifying the global setting
  std::string default_mode = kBtSnoopLogModeDisabled;
  {
    auto is_debuggable = os::GetSystemProperty(kIsDebuggableProperty);
    if (is_debuggable.has_value() && common::StringTrim(is_debuggable.value()) == "1") {
    auto is_debuggable = os::GetSystemPropertyBool(kIsDebuggableProperty, false);
    if (is_debuggable) {
      auto default_mode_property = os::GetSystemProperty(kBtSnoopDefaultLogModeProperty);
      if (default_mode_property) {
        default_mode = std::move(default_mode_property.value());
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ filegroup {
filegroup {
    name: "BluetoothOsSources_android",
    srcs: [
        "system_properties_common.cc",
        "android/metrics.cc",
        "android/parameter_provider.cc",
        "android/system_properties.cc",
@@ -35,6 +36,7 @@ filegroup {
filegroup {
    name: "BluetoothOsSources_host",
    srcs: [
        "system_properties_common.cc",
        "host/metrics.cc",
        "host/parameter_provider.cc",
        "host/system_properties.cc",
+1 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ source_set("BluetoothOsSources_linux") {
    "linux/parameter_provider.cc",
    "linux/system_properties.cc",
    "linux/wakelock_native.cc",
    "system_properties_common.cc",
    "syslog.cc",
  ]

+9 −0
Original line number Diff line number Diff line
@@ -26,6 +26,15 @@ namespace os {
// or if the platform does not support system property
std::optional<std::string> GetSystemProperty(const std::string& property);

// Get |property| keyed system property as uint32_t from supported platform, return |default_value| if the property
// does not exist or if the platform does not support system property
uint32_t GetSystemPropertyUint32(const std::string& property, uint32_t default_value);

// Get |property| keyed property as bool from supported platform, return
// |default_value| if the property does not exist or if the platform
// does not support system property
bool GetSystemPropertyBool(const std::string& property, bool default_value);

// Set |property| keyed system property to |value|, return true if the set was successful and false if the set failed
// Replace existing value if property already exists
bool SetSystemProperty(const std::string& property, const std::string& value);
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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 <string>

#include "common/strings.h"
#include "os/system_properties.h"

namespace bluetooth {
namespace os {

uint32_t GetSystemPropertyUint32(const std::string& property, uint32_t default_value) {
  std::optional<std::string> result = GetSystemProperty(property);
  if (result.has_value()) {
    return static_cast<uint32_t>(std::stoul(*result));
  }
  return default_value;
}

bool GetSystemPropertyBool(const std::string& property, bool default_value) {
  std::optional<std::string> result = GetSystemProperty(property);
  if (result.has_value()) {
    std::string trimmed_val = common::StringTrim(result.value());
    if (trimmed_val == "true" || trimmed_val == "1") {
      return true;
    }
    if (trimmed_val == "false" || trimmed_val == "0") {
      return false;
    }
  }
  return default_value;
}

}  // namespace os
}  // namespace bluetooth