bluetoothd运行时(main函数启动时),加载plugin(调用plugin_init函数):
gboolean plugin_init(GKeyFile *config)
{
GSList *list;
GDir *dir;
const gchar *file;
gchar **disabled;
unsigned int i;
/* Make a call to BtIO API so its symbols got resolved before the
* plugins are loaded. */
bt_io_error_quark();
if (config)
disabled = g_key_file_get_string_list(config, "General",
"DisablePlugins",
NULL, NULL);
else
disabled = NULL;
info("Loading builtin plugins");
//add default plugins, those plugins always need for bluetoothd runing
//those plugins will add to the global link named plugins
for (i = 0; __bluetooth_builtin[i]; i++) {
if (is_disabled(__bluetooth_builtin[i]->name, disabled))
continue;
add_plugin(NULL, __bluetooth_builtin[i]);
}
if (strlen(PLUGINDIR) == 0) {
g_strfreev(disabled);
goto start;
}
info("Loading plugins %s\n", PLUGINDIR);
dir = g_dir_open(PLUGINDIR, 0, NULL);
if (!dir) {
g_strfreev(disabled);
goto start;
}
//add user plugins, those plugins stored in PLUGINDIR path, and the
//PLUGINDIR = /usr/local/lib/bluetooth/plugins. The bluetoothd will
//find all those plugins which name *.so, and open them, get the method
//named bluetooth_plugin_desc, it will also add those plugins to the
//plugins links.
while ((file = g_dir_read_name(dir)) != NULL) {
struct bluetooth_plugin_desc *desc;
void *handle;
gchar *filename;
if (g_str_has_prefix(file, "lib") == TRUE ||
g_str_has_suffix(file, ".so") == FALSE)
continue;
if (is_disabled(file, disabled))
continue;
filename = g_build_filename(PLUGINDIR, file, NULL);
handle = dlopen(filename, RTLD_NOW);
if (handle == NULL) {
error("Can't load plugin %s: %s", filename,
dlerror());
g_free(filename);
continue;
}
g_free(filename);
desc = dlsym(handle, "bluetooth_plugin_desc");
if (desc == NULL) {
error("Can't load plugin description: %s", dlerror());
dlclose(handle);
continue;
}
if (add_plugin(handle, desc) == FALSE)
dlclose(handle);
}
g_dir_close(dir);
g_strfreev(disabled);
start:
//init all of the plugins by calling the plugins init function
for (list = plugins; list; list = list->next) {
struct bluetooth_plugin *plugin = list->data;
if (plugin->desc->init() < 0) {
error("Failed to init %s plugin", plugin->desc->name);
continue;
}
info("plugins active\n");
plugin->active = TRUE;
}
return TRUE;
}
函数中__bluetooth_builtin结构体为存储加载的plugin的入口地址,这些地址是通过连接宏##连接的,其中包含hciops模块的加载。此函数将结构体中的地址加载成plugins链表,然后循环调用每个模块的初始化init函数。对应于hciops模块,调用初始化函数为hciops_init。__bluetooth_builtin结构体和连接宏定义如下:
static struct bluetooth_plugin_desc *__bluetooth_builtin[] = {
&__bluetooth_builtin_audio,
&__bluetooth_builtin_input,
&__bluetooth_builtin_serial,
&__bluetooth_builtin_network,
&__bluetooth_builtin_service,
&__bluetooth_builtin_hciops,
&__bluetooth_builtin_hal,
&__bluetooth_builtin_storage,
NULL
};
#define BLUETOOTH_PLUGIN_DEFINE(name, version, priority, init, exit) \
struct bluetooth_plugin_desc __bluetooth_builtin_ ## name = { \
#n