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

Commit 526b51e0 authored by William Escande's avatar William Escande Committed by Rahul Arya
Browse files

GD: use one source as init_flag

Add ffi for needed method and remove duplicate parsing in c++

Test: cargo test init_flags
Bug: 251328572
Tag: #refactor
Merged-In: Id5f60e7ac2834e71783787b4c2af70c9581f5d40
Change-Id: Id5f60e7ac2834e71783787b4c2af70c9581f5d40
(cherry picked from commit 66b626e1)
parent 400b0849
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -11,7 +11,6 @@ filegroup {
    name: "BluetoothCommonSources",
    srcs: [
        "audit_log.cc",
        "init_flags.cc",
        "metric_id_manager.cc",
        "strings.cc",
        "stop_watch.cc",
+0 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
source_set("BluetoothCommonSources") {
  sources = [
    "audit_log.cc",
    "init_flags.cc",
    "metric_id_manager.cc",
    "stop_watch.cc",
    "strings.cc",

system/gd/common/init_flags.cc

deleted100644 → 0
+0 −128
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2019 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 "init_flags.h"

#include <cstdlib>
#include <string>

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

namespace bluetooth {
namespace common {

bool InitFlags::logging_debug_enabled_for_all = false;
int InitFlags::hci_adapter = 0;
std::unordered_map<std::string, bool> InitFlags::logging_debug_explicit_tag_settings = {};
bool InitFlags::btm_dm_flush_discovery_queue_on_search_cancel = false;

bool ParseBoolFlag(const std::vector<std::string>& flag_pair, const std::string& flag, bool* variable) {
  if (flag != flag_pair[0]) {
    return false;
  }
  auto value = BoolFromString(flag_pair[1]);
  if (!value) {
    return false;
  }
  *variable = *value;
  return true;
}

bool ParseIntFlag(const std::vector<std::string>& flag_pair, const std::string& flag, int* variable) {
  if (flag != flag_pair[0]) {
    return false;
  }
  auto value = Int64FromString(flag_pair[1]);
  if (!value || *value > INT32_MAX) {
    return false;
  }

  *variable = *value;
  return true;
}

void InitFlags::Load(const char** flags) {
  const char** flags_copy = flags;
  SetAll(false);
  while (flags != nullptr && *flags != nullptr) {
    std::string flag_element = *flags;
    auto flag_pair = StringSplit(flag_element, "=", 2);
    if (flag_pair.size() != 2) {
      flags++;
      continue;
    }

    // Parse adapter index (defaults to 0)
    ParseIntFlag(flag_pair, "--hci", &hci_adapter);

    ParseBoolFlag(
        flag_pair,
        "INIT_btm_dm_flush_discovery_queue_on_search_cancel",
        &btm_dm_flush_discovery_queue_on_search_cancel);

    ParseBoolFlag(flag_pair, "INIT_logging_debug_enabled_for_all", &logging_debug_enabled_for_all);
    if ("INIT_logging_debug_enabled_for_tags" == flag_pair[0]) {
      auto tags = StringSplit(flag_pair[1], ",");
      for (const auto& tag : tags) {
        auto setting = logging_debug_explicit_tag_settings.find(tag);
        if (setting == logging_debug_explicit_tag_settings.end()) {
          logging_debug_explicit_tag_settings.insert_or_assign(tag, true);
        }
      }
    }
    if ("INIT_logging_debug_disabled_for_tags" == flag_pair[0]) {
      auto tags = StringSplit(flag_pair[1], ",");
      for (const auto& tag : tags) {
        logging_debug_explicit_tag_settings.insert_or_assign(tag, false);
      }
    }
    flags++;
  }

  std::vector<std::string> logging_debug_enabled_tags;
  std::vector<std::string> logging_debug_disabled_tags;
  for (const auto& tag_setting : logging_debug_explicit_tag_settings) {
    if (tag_setting.second) {
      logging_debug_enabled_tags.emplace_back(tag_setting.first);
    } else {
      logging_debug_disabled_tags.emplace_back(tag_setting.first);
    }
  }

  flags = flags_copy;
  rust::Vec<rust::String> rusted_flags = rust::Vec<rust::String>();
  while (flags != nullptr && *flags != nullptr) {
    rusted_flags.push_back(rust::String(*flags));
    flags++;
  }
  init_flags::load(std::move(rusted_flags));
}

void InitFlags::SetAll(bool value) {
  logging_debug_enabled_for_all = value;
  logging_debug_explicit_tag_settings.clear();
}

void InitFlags::SetAllForTesting() {
  init_flags::set_all_for_testing();
  SetAll(true);
}

}  // namespace common
}  // namespace bluetooth
+15 −18
Original line number Diff line number Diff line
@@ -27,37 +27,34 @@ namespace common {

class InitFlags final {
 public:
  static void Load(const char** flags);
  inline static void Load(const char** flags) {
    rust::Vec<rust::String> rusted_flags = rust::Vec<rust::String>();
    while (flags != nullptr && *flags != nullptr) {
      rusted_flags.push_back(rust::String(*flags));
      flags++;
    }
    init_flags::load(std::move(rusted_flags));
  }

  inline static bool IsDebugLoggingEnabledForTag(const std::string& tag) {
    auto tag_setting = logging_debug_explicit_tag_settings.find(tag);
    if (tag_setting != logging_debug_explicit_tag_settings.end()) {
      return tag_setting->second;
    }
    return logging_debug_enabled_for_all;
    return init_flags::is_debug_logging_enabled_for_tag(tag);
  }

  inline static bool IsDebugLoggingEnabledForAll() {
    return logging_debug_enabled_for_all;
    return init_flags::logging_debug_enabled_for_all_is_enabled();
  }

  inline static bool IsBtmDmFlushDiscoveryQueueOnSearchCancel() {
    return btm_dm_flush_discovery_queue_on_search_cancel;
    return init_flags::btm_dm_flush_discovery_queue_on_search_cancel_is_enabled();
  }

  inline static int GetAdapterIndex() {
    return hci_adapter;
    return init_flags::get_hci_adapter();
  }

  static void SetAllForTesting();

 private:
  static void SetAll(bool value);
  static bool logging_debug_enabled_for_all;
  static bool btm_dm_flush_discovery_queue_on_search_cancel;
  static int hci_adapter;
  // save both log allow list and block list in the map to save hashing time
  static std::unordered_map<std::string, bool> logging_debug_explicit_tag_settings;
  inline static void SetAllForTesting() {
    init_flags::set_all_for_testing();
  }
};

}  // namespace common
+10 −6
Original line number Diff line number Diff line
@@ -4,17 +4,21 @@ mod ffi {
        fn load(flags: Vec<String>);
        fn set_all_for_testing();

        fn sdp_serialization_is_enabled() -> bool;
        fn gd_core_is_enabled() -> bool;
        fn gd_security_is_enabled() -> bool;
        fn gd_l2cap_is_enabled() -> bool;
        fn btaa_hci_is_enabled() -> bool;
        fn btm_dm_flush_discovery_queue_on_search_cancel_is_enabled() -> bool;
        fn gatt_robust_caching_client_is_enabled() -> bool;
        fn gatt_robust_caching_server_is_enabled() -> bool;
        fn btaa_hci_is_enabled() -> bool;
        fn gd_rust_is_enabled() -> bool;
        fn gd_core_is_enabled() -> bool;
        fn gd_l2cap_is_enabled() -> bool;
        fn gd_link_policy_is_enabled() -> bool;
        fn gd_rust_is_enabled() -> bool;
        fn gd_security_is_enabled() -> bool;
        fn get_hci_adapter() -> i32;
        fn irk_rotation_is_enabled() -> bool;
        fn is_debug_logging_enabled_for_tag(tag: &str) -> bool;
        fn logging_debug_enabled_for_all_is_enabled() -> bool;
        fn pass_phy_update_callback_is_enabled() -> bool;
        fn sdp_serialization_is_enabled() -> bool;
        fn sdp_skip_rnr_if_known_is_enabled() -> bool;
    }
}