Stack Function - module_init

1. module_init

1.1 get_module

system/bt/btcore/src/module.cc

const module_t* get_module(const char* name) {
  module_t* module = (module_t*)dlsym(RTLD_DEFAULT, name);
  CHECK(module);
  return module;
}

1.1.1 MODULE 类型的定义 - module_t

system/bt/btcore/include/module.h

typedef struct {
  const char* name;
  module_lifecycle_fn init;
  module_lifecycle_fn start_up;
  module_lifecycle_fn shut_down;
  module_lifecycle_fn clean_up;
  const char* dependencies[BTCORE_MAX_MODULE_DEPENDENCIES];
} module_t;
typedef future_t* (*module_lifecycle_fn)(void);

system/bt/osi/src/future.cc

struct future_t {
  bool ready_can_be_called;
  semaphore_t* semaphore;  // NULL semaphore means immediate future
  void* result;
};

1.1.2 dlsym()

根据 动态链接库 操作句柄(handle)与符号(symbol),返回符号对应的地址。
使用这个函数不但可以获取函数地址,也可以获取变量地址。

1.2 module_init

system/bt/btcore/src/module.cc

bool module_init(const module_t* module) {
......
  if (!call_lifecycle_function(module->init)) {
    return false;
  }
  set_module_state(module, MODULE_STATE_INITIALIZED);
  return true;
}

module_init 中执行了 module 的 init 方法

static bool call_lifecycle_function(module_lifecycle_fn function) {
  // A NULL lifecycle function means it isn't needed, so assume success
  if (!function) return true;

  future_t* future = function();

  // A NULL future means synchronous success
  if (!future) return true;

  // Otherwise fall back to the future
  return future_await(future);
}

2. module_init(get_module(OSI_MODULE));

2.1 OSI_MODULE 的定义

system/bt/btcore/src/osi_module.cc

EXPORT_SYMBOL extern const module_t osi_module = {.name = OSI_MODULE,
                                                  .init = osi_init,
                                                  .start_up = NULL,
                                                  .shut_down = NULL,
                                                  .clean_up = osi_clean_up,
                                                  .dependencies = {NULL}};


future_t* osi_init(void) {
  return future_new_immediate(FUTURE_SUCCESS);
}

system/bt/osi/src/future.cc

future_t* future_new_immediate(void* value) {
  future_t* ret = static_cast<future_t*>(osi_calloc(sizeof(future_t)));

  ret->result = value;
  ret->ready_can_be_called = false;
  return ret;
}

3. module_init(get_module(BT_UTILS_MODULE));

system/bt/utils/src/bt_utils.cc

EXPORT_SYMBOL extern const module_t bt_utils_module = {.name = BT_UTILS_MODULE,
                                                       .init = init,
                                                       .start_up = NULL,
                                                       .shut_down = NULL,
                                                       .clean_up = clean_up,
                                                       .dependencies = {NULL}};



static pthread_once_t g_DoSchedulingGroupOnce[TASK_HIGH_MAX];
static bool g_DoSchedulingGroup[TASK_HIGH_MAX];
static int g_TaskIDs[TASK_HIGH_MAX];



static future_t* init(void) {
  int i;

  for (i = 0; i < TASK_HIGH_MAX; i++) {
    g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
    g_DoSchedulingGroup[i] = true;
    g_TaskIDs[i] = INVALID_TASK_ID;
  }

  return NULL;
}

4. module_init(get_module(BTIF_CONFIG_MODULE));

system/bt/btif/src/btif_config.cc

EXPORT_SYMBOL module_t btif_config_module = {.name = BTIF_CONFIG_MODULE,
                                             .init = init,
                                             .start_up = NULL,
                                             .shut_down = shut_down,
                                             .clean_up = clean_up};



static future_t* init(void) {
  std::unique_lock<std::recursive_mutex> lock(config_lock);
  std::unique_ptr<config_t> config;

  if (is_factory_reset()) delete_config_files();

  std::string file_source;

  if (config_checksum_pass(CONFIG_FILE_COMPARE_PASS)) {
    config = btif_config_open(CONFIG_FILE_PATH);
    btif_config_source = ORIGINAL;
  }
  if (!config) {
    if (config_checksum_pass(CONFIG_BACKUP_COMPARE_PASS)) {
      config = btif_config_open(CONFIG_BACKUP_PATH);
      btif_config_source = BACKUP;
      file_source = "Backup";
    }
  }
  if (!config) {
    config = btif_config_transcode(CONFIG_LEGACY_FILE_PATH);
    btif_config_source = LEGACY;
    file_source = "Legacy";
  }
  if (!config) {
    config = storage_config_get_interface()->config_new_empty();
    btif_config_source = NEW_FILE;
    file_source = "Empty";
  }

  // move persistent config data from btif_config file to btif config cache
  btif_config_cache.Init(std::move(config));

  if (!file_source.empty()) {
    btif_config_cache.SetString(INFO_SECTION, FILE_SOURCE, file_source);
  }

  // Cleanup temporary pairings if we have left guest mode
  if (!is_restricted_mode()) {
    btif_config_cache.RemovePersistentSectionsWithKey("Restricted");
  }

  // Read or set config file creation timestamp
  auto time_str = btif_config_cache.GetString(INFO_SECTION, FILE_TIMESTAMP);
  if (!time_str) {
    time_t current_time = time(NULL);
    struct tm* time_created = localtime(&current_time);
    strftime(btif_config_time_created, TIME_STRING_LENGTH, TIME_STRING_FORMAT,
             time_created);
    btif_config_cache.SetString(INFO_SECTION, FILE_TIMESTAMP,
                                btif_config_time_created);
  } else {
    strlcpy(btif_config_time_created, time_str->c_str(), TIME_STRING_LENGTH);
  }

  // Read or set metrics 256 bit hashing salt
  read_or_set_metrics_salt();

  // Initialize MetricIdAllocator
  init_metric_id_allocator();

  // TODO(sharvil): use a non-wake alarm for this once we have
  // API support for it. There's no need to wake the system to
  // write back to disk.
  config_timer = alarm_new("btif.config");
  if (!config_timer) {
    LOG_ERROR(LOG_TAG, "%s unable to create alarm.", __func__);
    goto error;
  }

  LOG_EVENT_INT(BT_CONFIG_SOURCE_TAG_NUM, btif_config_source);

  return future_new_immediate(FUTURE_SUCCESS);
......
}

5. module_init(get_module(INTEROP_MODULE));

system/bt/device/src/interop.cc

EXPORT_SYMBOL module_t interop_module = {
    .name = INTEROP_MODULE,
    .init = NULL,
    .start_up = NULL,
    .shut_down = NULL,
    .clean_up = interop_clean_up,
    .dependencies = {NULL},
};

6. module_init(get_module(STACK_CONFIG_MODULE));

读取配置文件 /etc/bluetooth/bt_stack.conf
system/bt/main/stack_config.cc

EXPORT_SYMBOL extern const module_t stack_config_module = {
    .name = STACK_CONFIG_MODULE,
    .init = init,
    .start_up = NULL,
    .shut_down = NULL,
    .clean_up = clean_up,
    .dependencies = {NULL}};



static future_t* init() {
#if defined(OS_GENERIC)
  const char* path = "bt_stack.conf";
#else  // !defined(OS_GENERIC)
  const char* path = "/etc/bluetooth/bt_stack.conf";
#endif  // defined(OS_GENERIC)

  config = config_new(path);
......

  return future_new_immediate(FUTURE_SUCCESS);
}



std::unique_ptr<config_t> config_new(const char* filename) {
  CHECK(filename != nullptr);

  std::unique_ptr<config_t> config = config_new_empty();

  FILE* fp = fopen(filename, "rt");
......
  if (!config_parse(fp, config.get())) {
    config.reset();
  }

  fclose(fp);
  return config;
}



static bool config_parse(FILE* fp, config_t* config) {
......

  int line_num = 0;
  char line[1024];
  char section[1024];
  strcpy(section, CONFIG_DEFAULT_SECTION);

  while (fgets(line, sizeof(line), fp)) {
    char* line_ptr = trim(line);
    ++line_num;

    // Skip blank and comment lines.
    if (*line_ptr == '\0' || *line_ptr == '#') continue;

    if (*line_ptr == '[') {
      size_t len = strlen(line_ptr);
      if (line_ptr[len - 1] != ']') {
        return false;
      }
      strncpy(section, line_ptr + 1, len - 2);  // NOLINT (len < 1024)
      section[len - 2] = '\0';
    } else {
      char* split = strchr(line_ptr, '=');
      if (!split) {
        return false;
      }

      *split = '\0';
      config_set_string(config, section, trim(line_ptr), trim(split + 1));
    }
  }
  return true;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容