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

Commit ac301f54 authored by Katherine Lai's avatar Katherine Lai
Browse files

Refactor sysprops to use gd/os/system_properties

Have one method to access system properties regardless of OS so
we can remove OS dependencies in code where sysprops are
currently being used. system/gd/os/system_properties was created
with that in mind, so utilize the gd abstraction in
osi_property_get/set and refactor sysprops to use osi_property.

Bug: 239956470
Tag: #refactor
Test: m
Change-Id: I639960af94965a7f151b2c5ec6583f419b59a8a8
parent 95f68955
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -36,7 +36,10 @@ cc_library {
        "liblog",
        "libcutils",
    ],
    static_libs: ["libosi"],
    static_libs: [
        "libbluetooth_gd",
        "libosi",
    ],
}

cc_library_static {
+3 −53
Original line number Diff line number Diff line
@@ -29,10 +29,6 @@

#include "btif_dm.h"

#ifdef OS_ANDROID
#include <android/sysprop/BluetoothProperties.sysprop.h>
#endif

#include <base/bind.h>
#include <base/logging.h>
#include <bluetooth/uuid.h>
@@ -51,10 +47,6 @@

#include <mutex>

#ifdef OS_ANDROID
#include <android/sysprop/BluetoothProperties.sysprop.h>
#endif

#include "advertise_data_parser.h"
#include "bta_csis_api.h"
#include "bta_dm_int.h"
@@ -1571,19 +1563,9 @@ void BTIF_dm_enable() {
  }

  /* Enable or disable local privacy */
  bool ble_privacy_enabled = true;
#ifdef OS_ANDROID
  ble_privacy_enabled =
      android::sysprop::BluetoothProperties::isGapLePrivacyEnabled().value_or(
          true);
#else
  char ble_privacy_text[PROPERTY_VALUE_MAX] = "true";  // default is enabled
  if (osi_property_get(PROPERTY_BLE_PRIVACY_ENABLED, ble_privacy_text,
                       "true") &&
      !strcmp(ble_privacy_text, "false")) {
    ble_privacy_enabled = false;
  }
#endif
  bool ble_privacy_enabled =
      osi_property_get_bool(PROPERTY_BLE_PRIVACY_ENABLED, /*default=*/true);

  LOG_INFO("%s BLE Privacy: %d", __func__, ble_privacy_enabled);
  BTA_DmBleConfigLocalPrivacy(ble_privacy_enabled);

@@ -2245,27 +2227,6 @@ void btif_dm_get_local_class_of_device(DEV_CLASS device_class) {
  device_class[1] = BTM_COD_MAJOR_UNCLASSIFIED;
  device_class[2] = BTM_COD_MINOR_UNCLASSIFIED;

#ifdef OS_ANDROID
  std::vector<std::optional<std::uint32_t>> local_device_class =
          android::sysprop::BluetoothProperties::getClassOfDevice();
  // Error check the inputs. Use the default if problems are found
  if (local_device_class.size() != 3) {
    LOG_ERROR("COD malformed, must have unsigned 3 integers.");
  } else if (!local_device_class[0].has_value()
      || local_device_class[0].value() > 0xFF) {
    LOG_ERROR("COD malformed, first value is missing or too large.");
  } else if (!local_device_class[1].has_value()
      || local_device_class[1].value() > 0xFF) {
    LOG_ERROR("COD malformed, second value is missing or too large.");
  } else if (!local_device_class[2].has_value()
      || local_device_class[2].value() > 0xFF) {
    LOG_ERROR("COD malformed, third value is missing or too large.");
  } else {
    device_class[0] = (uint8_t) local_device_class[0].value();
    device_class[1] = (uint8_t) local_device_class[1].value();
    device_class[2] = (uint8_t) local_device_class[2].value();
  }
#else
  char prop_cod[PROPERTY_VALUE_MAX];
  osi_property_get(PROPERTY_CLASS_OF_DEVICE, prop_cod, "");

@@ -2343,7 +2304,6 @@ void btif_dm_get_local_class_of_device(DEV_CLASS device_class) {
  } else {
    LOG_ERROR("%s: COD malformed, fewer than three numbers", __func__);
  }
#endif

  LOG_DEBUG("%s: Using class of device '0x%x, 0x%x, 0x%x'", __func__,
      device_class[0], device_class[1], device_class[2]);
@@ -3275,19 +3235,9 @@ static const char* btif_get_default_local_name() {
  if (btif_default_local_name[0] == '\0') {
    int max_len = sizeof(btif_default_local_name) - 1;

    // Use the stable sysprop on Android devices, otherwise use osi_property_get
#ifdef OS_ANDROID
    std::optional<std::string> default_local_name =
        android::sysprop::BluetoothProperties::getDefaultDeviceName();
    if (default_local_name.has_value() && default_local_name.value() != "") {
      strncpy(btif_default_local_name, default_local_name.value().c_str(),
              max_len);
    }
#else
    char prop_name[PROPERTY_VALUE_MAX];
    osi_property_get(PROPERTY_DEFAULT_DEVICE_NAME, prop_name, "");
    strncpy(btif_default_local_name, prop_name, max_len);
#endif

    // If no value was placed in the btif_default_local_name then use model name
    if (btif_default_local_name[0] == '\0') {
+2 −37
Original line number Diff line number Diff line
@@ -16,17 +16,15 @@
 *
 ******************************************************************************/

#include <string.h>

#include "osi/include/properties.h"

#ifdef TARGET_FLOSS
#include <string.h>

#include <algorithm>
#include <optional>
#include <string>

#include "gd/os/system_properties.h"
#endif

#if !defined(OS_GENERIC)
#undef PROPERTY_VALUE_MAX
@@ -37,7 +35,6 @@
#endif  // !defined(OS_GENERIC)

int osi_property_get(const char* key, char* value, const char* default_value) {
#if defined(TARGET_FLOSS)
  std::optional<std::string> result = bluetooth::os::GetSystemProperty(key);
  if (result) {
    memcpy(value, result->data(), result->size());
@@ -51,59 +48,27 @@ int osi_property_get(const char* key, char* value, const char* default_value) {
  } else {
    return 0;
  }
#elif defined(OS_GENERIC)
  /* For linux right now just return default value, if present */
  int len = 0;
  if (!default_value) return len;

  len = strlen(default_value);
  if (len >= PROPERTY_VALUE_MAX) len = PROPERTY_VALUE_MAX - 1;

  memcpy(value, default_value, len);
  value[len] = '\0';
  return len;
#else
  return property_get(key, value, default_value);
#endif  // defined(OS_GENERIC)
}

int osi_property_set(const char* key, const char* value) {
#if defined(TARGET_FLOSS)
  bool success = bluetooth::os::SetSystemProperty(key, value);
  return success ? 0 : -1;
#elif defined(OS_GENERIC)
  return -1;
#else
  return property_set(key, value);
#endif  // defined(OS_GENERIC)
}

int32_t osi_property_get_int32(const char* key, int32_t default_value) {
#if defined(TARGET_FLOSS)
  std::optional<std::string> result = bluetooth::os::GetSystemProperty(key);
  if (result) {
    return stoi(*result, nullptr);
  } else {
    return default_value;
  }
#elif defined(OS_GENERIC)
  return default_value;
#else
  return property_get_int32(key, default_value);
#endif  // defined(OS_GENERIC)
}

bool osi_property_get_bool(const char* key, bool default_value) {
#if defined(TARGET_FLOSS)
  std::optional<std::string> result = bluetooth::os::GetSystemProperty(key);
  if (result) {
    return *result == std::string("true");
  } else {
    return default_value;
  }
#elif defined(OS_GENERIC)
  return default_value;
#else
  return property_get_bool(key, default_value);
#endif  // defined(OS_GENERIC)
}