- extern struct bluetooth_plugin_desc __bluetooth_builtin_audio;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_input;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_serial;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_network;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_service;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_hciops;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_hal;
- extern struct bluetooth_plugin_desc __bluetooth_builtin_storage;
- 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
- };
- BLUETOOTH_PLUGIN_DEFINE(audio, VERSION,
- BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, audio_init, audio_exit)
- #ifdef BLUETOOTH_PLUGIN_BUILTIN
- #define BLUETOOTH_PLUGIN_DEFINE(name, version, priority, init, exit) \
- struct bluetooth_plugin_desc __bluetooth_builtin_ ## name = { \
- #name, version, priority, init, exit \
- };
- #else
- #define BLUETOOTH_PLUGIN_DEFINE(name, version, priority, init, exit) \
- extern struct bluetooth_plugin_desc bluetooth_plugin_desc \
- __attribute__ ((visibility("default"))); \
- struct bluetooth_plugin_desc bluetooth_plugin_desc = { \
- #name, version, priority, init, exit \
- };
- #endif
代码解析之:adapter_ops_setup()
首先看函数定义:
- int adapter_ops_setup(void)
- {
- if (!adapter_ops)
- return -EINVAL;
- return adapter_ops->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);
- }
