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 = { \
- #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)
- {
- /* Already registered */
- 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);
-
- /* Create and bind HCI socket */
- 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;
- }
-
- /* Set filter */
- 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);
-
- /* Initialize already connected devices */
- 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");
- <span style="color:#000000;">device_event(HCI_DEV_UP, dr->dev_id);</span>
- }
- }
-
- g_free(dl);
- return 0;
- }