作者:诺亚方舟菲芘袁子陽 | 来源:互联网 | 2017-10-13 23:48
bluetoothd运行时(main函数启动时),加载plugin(调用plugin_init函数):gbooleanplugin_init(GKeyFile*config){GSList*list;&nb
bluetoothd运行时(main函数启动时),加载plugin(调用plugin_init函数):
- gboolean plugin_init(GKeyFile *config)
- {
- GSList *list;
- GDir *dir;
- const gchar *file;
- gchar **disabled;
- unsigned int i;
-
-
-
- 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");
-
-
-
- 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;
- }
-
-
-
-
-
-
- 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:
-
- 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 = { \
- #name, version, priority, init, exit \
- };
hciops模块初始化:
- static int hciops_init(void)
- {
- info("hciops_init\n");
- return btd_register_adapter_ops(&hci_ops);
- }
- int btd_register_adapter_ops(struct btd_adapter_ops *btd_adapter_ops)
- {
-
- if (adapter_ops)
- return -EALREADY;
-
- if (btd_adapter_ops->setup == NULL)
- return -EINVAL;
-
- adapter_ops = btd_adapter_ops;
-
- return 0;
- }
这个初始化函数将静态hci_ops结构体变量赋值给了全局变量adapter_ops。hci_ops结构定义:
- static struct btd_adapter_ops hci_ops = {
- .setup = hciops_setup,
- .cleanup = hciops_cleanup,
- .start = hciops_start,
- .stop = hciops_stop,
- .set_powered = hciops_powered,
- .set_connectable = hciops_connectable,
- .set_discoverable = hciops_discoverable,
- .set_limited_discoverable = hciops_set_limited_discoverable,
- .start_discovery = hciops_start_discovery,
- .stop_discovery = hciops_stop_discovery,
- .resolve_name = hciops_resolve_name,
- .cancel_resolve_name = hciops_cancel_resolve_name,
- .set_name = hciops_set_name,
- .read_name = hciops_read_name,
- .set_class = hciops_set_class,
- };
在plugin_init加载完plugins后,调用了adapter_ops_setup函数来启动HCI适配层:
[cpp] view plaincopyprint?
- int adapter_ops_setup(void)
- {
- if (!adapter_ops)
- return -EINVAL;
-
- return adapter_ops->setup();
- }
这个函数即调用里 hci_ops静态全局变量的setup函数,看hci_ops全局变量的定义,.setup指向hciops_setup函数:
- static int hciops_setup(void)
- {
- struct sockaddr_hci addr;
- struct hci_filter flt;
- GIOChannel *ctl_io, *child_io;
- int sock, err;
-
- info("hciops_setup\n");
-
- if (child_pipe[0] != -1)
- return -EALREADY;
-
- if (pipe(child_pipe) < 0) {
- err = errno;
- error("pipe(): %s (%d)", strerror(err), err);
- return -err;
- }
-
- child_io = g_io_channel_unix_new(child_pipe[0]);
- g_io_channel_set_close_on_unref(child_io, TRUE);
- child_io_id = g_io_add_watch(child_io,
- G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
- child_exit, NULL);
- g_io_channel_unref(child_io);
-
-
- sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
- if (sock < 0) {
- err = errno;
- error("Can't open HCI socket: %s (%d)", strerror(err),
- err);
- return -err;
- }
-
-
- hci_filter_clear(&flt);
- hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
- hci_filter_set_event(EVT_STACK_INTERNAL, &flt);
- if (setsockopt(sock, SOL_HCI, HCI_FILTER, &flt,
- sizeof(flt)) < 0) {
- err = errno;
- error("Can't set filter: %s (%d)", strerror(err), err);
- return -err;
- }
-
- memset(&addr, 0, sizeof(addr));
- addr.hci_family = AF_BLUETOOTH;
- addr.hci_dev = HCI_DEV_NONE;
- if (bind(sock, (struct sockaddr *) &addr,
- sizeof(addr)) < 0) {
- err = errno;
- error("Can't bind HCI socket: %s (%d)",
- strerror(err), err);
- return -err;
- }
-
- ctl_io = g_io_channel_unix_new(sock);
- g_io_channel_set_close_on_unref(ctl_io, TRUE);
-
- ctl_io_id = g_io_add_watch(ctl_io, G_IO_IN, io_stack_event, NULL);
-
- g_io_channel_unref(ctl_io);
-
-
- return init_known_adapters(sock);
- }
在函数的最后,调用里init_known_adapters启动里已知的hci设备,初始化HCI适配器:
- static int init_known_adapters(int ctl)
- {
- struct hci_dev_list_req *dl;
- struct hci_dev_req *dr;
- int i, err;
-
- info("init_known_adapters\n");
-
- dl = g_try_malloc0(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
- if (!dl) {
- err = errno;
- error("Can't allocate devlist buffer: %s (%d)",
- strerror(err), err);
- return -err;
- }
-
- dl->dev_num = HCI_MAX_DEV;
- dr = dl->dev_req;
-
- if (ioctl(ctl, HCIGETDEVLIST, (void *) dl) < 0) {
- err = errno;
- error("Can't get device list: %s (%d)",
- strerror(err), err);
- g_free(dl);
- return -err;
- }
-
- for (i = 0; i < dl->dev_num; i++, dr++) {
- device_event(HCI_DEV_REG, dr->dev_id);
-
- if (hci_test_bit(HCI_UP, &dr->dev_opt)){
- info("here start the hci device\n");
- "color:#000000;">device_event(HCI_DEV_UP, dr->dev_id);
- }
- }
-
- g_free(dl);
- return 0;
- }