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

Commit d1fdfb01 authored by Michał Narajowski's avatar Michał Narajowski
Browse files

device_iot_config: add init flag

An init flag can be toggled at any time so module methods
are not using it. The module is initialized regardless
if the flag is set. Only the public API is protected
and if the flag is not set then the calls to the API will return
immediately.

Bug: 261319328
Tag: #feature
Test: atest bluetooth_test_gd
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Change-Id: I83541dccf7c24678a37ff4afc92a23fcfd14c203
parent 02b292f0
Loading
Loading
Loading
Loading
+20 −18
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include "btcore/include/module.h"
#include "btif/include/btif_api.h"
#include "btif/include/btif_util.h"
#include "common/init_flags.h"
#include "device/include/device_iot_config.h"
#include "device_iot_config_int.h"
#include "osi/include/alarm.h"
@@ -51,10 +52,11 @@ char device_iot_config_time_created[TIME_STRING_LENGTH];
std::mutex config_lock;  // protects operations on |config|.
std::unique_ptr<config_t> config;
alarm_t* config_timer;
bool iot_logging_enabled = false;

using bluetooth::common::InitFlags;

bool device_iot_config_has_section(const std::string& section) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -64,7 +66,7 @@ bool device_iot_config_has_section(const std::string& section) {

bool device_iot_config_exist(const std::string& section,
                             const std::string& key) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -74,7 +76,7 @@ bool device_iot_config_exist(const std::string& section,

bool device_iot_config_get_int(const std::string& section,
                               const std::string& key, int& value) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -87,7 +89,7 @@ bool device_iot_config_get_int(const std::string& section,

bool device_iot_config_set_int(const std::string& section,
                               const std::string& key, int value) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -104,7 +106,7 @@ bool device_iot_config_set_int(const std::string& section,

bool device_iot_config_int_add_one(const std::string& section,
                                   const std::string& key) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -124,7 +126,7 @@ bool device_iot_config_int_add_one(const std::string& section,

bool device_iot_config_get_hex(const std::string& section,
                               const std::string& key, int& value) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -147,7 +149,7 @@ bool device_iot_config_get_hex(const std::string& section,
bool device_iot_config_set_hex(const std::string& section,
                               const std::string& key, int value,
                               int byte_num) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -173,7 +175,7 @@ bool device_iot_config_set_hex(const std::string& section,
bool device_iot_config_set_hex_if_greater(const std::string& section,
                                          const std::string& key, int value,
                                          int byte_num) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  int stored_value = 0;
  bool ret = device_iot_config_get_hex(section, key, stored_value);
@@ -185,7 +187,7 @@ bool device_iot_config_set_hex_if_greater(const std::string& section,
bool device_iot_config_get_str(const std::string& section,
                               const std::string& key, char* value,
                               int* size_bytes) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);
  CHECK(value != NULL);
@@ -206,7 +208,7 @@ bool device_iot_config_get_str(const std::string& section,
bool device_iot_config_set_str(const std::string& section,
                               const std::string& key,
                               const std::string& value) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -222,7 +224,7 @@ bool device_iot_config_set_str(const std::string& section,
bool device_iot_config_get_bin(const std::string& section,
                               const std::string& key, uint8_t* value,
                               size_t* length) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);
  CHECK(value != NULL);
@@ -256,7 +258,7 @@ bool device_iot_config_get_bin(const std::string& section,

size_t device_iot_config_get_bin_length(const std::string& section,
                                        const std::string& key) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return 0;

  CHECK(config != NULL);

@@ -272,7 +274,7 @@ size_t device_iot_config_get_bin_length(const std::string& section,
bool device_iot_config_set_bin(const std::string& section,
                               const std::string& key, const uint8_t* value,
                               size_t length) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  const char* lookup = "0123456789abcdef";

@@ -307,7 +309,7 @@ bool device_iot_config_set_bin(const std::string& section,

bool device_iot_config_remove(const std::string& section,
                              const std::string& key) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return false;

  CHECK(config != NULL);

@@ -316,7 +318,7 @@ bool device_iot_config_remove(const std::string& section,
}

void device_iot_config_flush(void) {
  CHECK_LOGGING_ENABLED((void)0);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return;

  CHECK(config != NULL);
  CHECK(config_timer != NULL);
@@ -329,7 +331,7 @@ void device_iot_config_flush(void) {
}

bool device_iot_config_clear(void) {
  CHECK_LOGGING_ENABLED(false);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return true;

  CHECK(config != NULL);
  CHECK(config_timer != NULL);
@@ -351,7 +353,7 @@ bool device_iot_config_clear(void) {
}

void device_debug_iot_config_dump(int fd) {
  CHECK_LOGGING_ENABLED((void)0);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return;

  dprintf(fd, "\nBluetooth Iot Config:\n");

+6 −8
Original line number Diff line number Diff line
@@ -52,7 +52,8 @@ extern char device_iot_config_time_created[TIME_STRING_LENGTH];
extern std::mutex config_lock;  // protects operations on |config|.
extern std::unique_ptr<config_t> config;
extern alarm_t* config_timer;
extern bool iot_logging_enabled;

using bluetooth::common::InitFlags;

static void cleanup() {
  alarm_free(config_timer);
@@ -67,11 +68,6 @@ future_t* device_iot_config_module_init(void) {
  LOG_INFO("");

  std::unique_lock<std::mutex> lock(config_lock);
  iot_logging_enabled = osi_property_get_bool(PROPERTY_ENABLE_LOGGING, false);
  if (!iot_logging_enabled) {
    device_iot_config_delete_files();
    return future_new_immediate(FUTURE_SUCCESS);
  }

  if (device_iot_config_is_factory_reset()) {
    device_iot_config_delete_files();
@@ -198,7 +194,7 @@ EXPORT_SYMBOL module_t device_iot_config_module = {
    .clean_up = device_iot_config_module_clean_up};

void device_iot_config_write(uint16_t event, UNUSED_ATTR char* p_param) {
  CHECK_LOGGING_ENABLED((void)0);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return;

  CHECK(config != NULL);
  CHECK(config_timer != NULL);
@@ -237,6 +233,8 @@ bool device_iot_config_has_key_value(const std::string& section,
}

void device_iot_config_save_async(void) {
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return;

  CHECK(config != NULL);
  CHECK(config_timer != NULL);

@@ -246,7 +244,7 @@ void device_iot_config_save_async(void) {
}

int device_iot_config_get_device_num(const config_t& conf) {
  CHECK_LOGGING_ENABLED(0);
  if (!InitFlags::IsDeviceIotConfigLoggingEnabled()) return 0;

  int devices = 0;

+4 −3
Original line number Diff line number Diff line
@@ -66,7 +66,8 @@ enum ConfigSource { NOT_LOADED, ORIGINAL, BACKUP, NEW_FILE, RESET };

#define CHECK_LOGGING_ENABLED(return_value)                               \
  do {                                                                    \
    if (!iot_logging_enabled) return (return_value); \
    if (!bluetooth::common::InitFlags::IsDeviceIotConfigLoggingEnabled()) \
      return (return_value);                                              \
  } while (0)

struct config_t;
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ table InitFlagsData {
    asynchronously_start_l2cap_coc_is_enabled:bool (privacy:"Any");
    btaa_hci_is_enabled:bool (privacy:"Any");
    btm_dm_flush_discovery_queue_on_search_cancel_is_enabled:bool (privacy:"Any");
    device_iot_config_logging_is_enabled:bool (privacy:"Any");
    finite_att_timeout_is_enabled:bool (privacy:"Any");
    gatt_robust_caching_client_is_enabled:bool (privacy:"Any");
    gatt_robust_caching_server_is_enabled:bool (privacy:"Any");
+4 −0
Original line number Diff line number Diff line
@@ -44,6 +44,10 @@ class InitFlags final {
    return init_flags::logging_debug_enabled_for_all_is_enabled();
  }

  inline static bool IsDeviceIotConfigLoggingEnabled() {
    return init_flags::device_iot_config_logging_is_enabled();
  }

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