bluetoothd 在 main 函式啟動時,第一件事就是載入 plugin(plugin_init)。搞懂這條路徑,後續配對流程為何卡住才有跡可循。
gboolean plugin_init(GKeyFile *config)
{
GSList *list;
GDir *dir;
const gchar *file;
gchar **disabled;
unsigned int i;
/* 先呼叫 BtIO API,確保 plugin 載入前符號已解析 */
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");
// 內建 plugin 是 bluetoothd 運行的必要條件,會掛到 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;
}
// 使用者 plugin 放在 PLUGINDIR(/usr/local/lib/bluetooth/plugins),
// 凡是 *.so 都會被 dlopen,並取出 bluetooth_plugin_desc 一併掛入 plugins
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:
// 依序呼叫各 plugin 的 init
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。
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 \
};
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;
}
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,
};
也就是把靜態的 hci_ops 指標交給全域變數 adapter_ops。plugin_init 收尾時再呼叫 adapter_ops_setup 真正啟動 HCI 層:
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);
/* 建立並綁定 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;
}
/* 設定 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);
/* 初始化已連線的裝置 */
return init_known_adapters(sock);
}
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");
device_event(HCI_DEV_UP, dr->dev_id);
}
}
g_free(dl);
return 0;
}
整條鏈是:plugin_init 載入外掛 → hciops_init 註冊操作集 → adapter_ops_setup 呼叫 setup → hciops_setup 開 HCI socket 並綁事件 → init_known_adapters 逐一觸發 HCI_DEV_REG 與 HCI_DEV_UP。配對之所以能進行,就是因為這條路徑全部走通了。