你好,游客 登录 注册 搜索
背景:
阅读新闻

Ubuntu Bluetooth 调试

[日期:2012-06-07] 来源:Linux社区  作者:jmhgg88 [字体:大 中 小]
可以看到,这个函数执行的最终结果会生成plugins全局链表。从函数中可以看到,它是通过add_plugin()函数将__bluetooth_builtin[]数组中的成员添加到plugins全局变量中的。__bluetooth_builtin具体是什么东西呢?看它的定义,这个数组定义在builtin.h文件中,如下:
  1. extern struct bluetooth_plugin_desc __bluetooth_builtin_audio;  
  2. extern struct bluetooth_plugin_desc __bluetooth_builtin_input;  
  3. extern struct bluetooth_plugin_desc __bluetooth_builtin_serial;  
  4. extern struct bluetooth_plugin_desc __bluetooth_builtin_network;  
  5. extern struct bluetooth_plugin_desc __bluetooth_builtin_service;  
  6. extern struct bluetooth_plugin_desc __bluetooth_builtin_hciops;  
  7. extern struct bluetooth_plugin_desc __bluetooth_builtin_hal;  
  8. extern struct bluetooth_plugin_desc __bluetooth_builtin_storage;  
  9.   
  10. static struct bluetooth_plugin_desc *__bluetooth_builtin[] = {  
  11.   &__bluetooth_builtin_audio,  
  12.   &__bluetooth_builtin_input,  
  13.   &__bluetooth_builtin_serial,  
  14.   &__bluetooth_builtin_network,  
  15.   &__bluetooth_builtin_service,  
  16.   &__bluetooth_builtin_hciops,  
  17.   &__bluetooth_builtin_hal,  
  18.   &__bluetooth_builtin_storage,  
  19.   NULL  
  20. };  
搜索整个代码工程,都无法找到__bluetooth_builtin_audio等等变量的定义。看这些成员都是extern应用,定义肯定在外部。要看看它们具体是怎么定义的,首先要知道如下#define定义:#define中的##使用。在了解里这个定义后,看下其中一个(其他类似)如__bluetooth_builtin_audio,来看一下以下这个文件:audio/main.c。这个文件中最下面有个宏定义:
  1. BLUETOOTH_PLUGIN_DEFINE(audio, VERSION,  
  2.             BLUETOOTH_PLUGIN_PRIORITY_DEFAULT, audio_init, audio_exit)  
这个宏定义在src/plugin.h文件中,如下:
  1. #ifdef BLUETOOTH_PLUGIN_BUILTIN   
  2. #define BLUETOOTH_PLUGIN_DEFINE(name, version, priority, init, exit) \   
  3.         struct bluetooth_plugin_desc __bluetooth_builtin_ ## name = { \  
  4.             #name, version, priority, init, exit \   
  5.         };  
  6. #else   
  7. #define BLUETOOTH_PLUGIN_DEFINE(name, version, priority, init, exit) \   
  8.         extern struct bluetooth_plugin_desc bluetooth_plugin_desc \  
  9.                 __attribute__ ((visibility("default"))); \  
  10.         struct bluetooth_plugin_desc bluetooth_plugin_desc = { \  
  11.             #name, version, priority, init, exit \   
  12.         };  
  13. #endif  
根据#define中的##使用这个语法,可以知道__bluetooth_builtin_audio结构体变量定义就是audio/main.c中这个宏,并且已经为这个结构体变量附好了初值(见audio/main.c宏定义中对应的参数值)。

代码解析之:adapter_ops_setup()

首先看函数定义:

  1. int adapter_ops_setup(void)  
  2. {  
  3.     if (!adapter_ops)  
  4.         return -EINVAL;  
  5.   
  6.     return adapter_ops->setup();  
  7. }  
函数很简单,执行了setup函数指针。这里主要看adapter_ops这个全局变量何时被赋值。从上一个函数plugin_init介绍,提到在将全局__bluetooth_builtin[]数组加载到plugins后,最后会依次执行各个plugins的init函数。看下加载__bluetooth_builtin_hciops时,调用的init函数,这个函数定义在plugins/hciops.c中,即hciops_init函数。这个函数将传入的静态全局变量hci_ops赋值给了adapter_ops,所以变量adapter_ops即变量hci_ops。所以此处调用adapter_ops->setup(),即调用hciops.c中的hciops_setup函数。看hciops_setup函数定义如下:
  1. static int hciops_setup(void)  
  2. {  
  3.     struct sockaddr_hci addr;  
  4.     struct hci_filter flt;  
  5.     GIOChannel *ctl_io, *child_io;  
  6.     int sock, err;  
  7.   
  8. info("hciops_setup\n");  
  9.   
  10.     if (child_pipe[0] != -1)  
  11.         return -EALREADY;  
  12.   
  13.     if (pipe(child_pipe) < 0) {  
  14.         err = errno;  
  15.         error("pipe(): %s (%d)", strerror(err), err);  
  16.         return -err;  
  17.     }  
  18.   
  19.     child_io = g_io_channel_unix_new(child_pipe[0]);  
  20.     g_io_channel_set_close_on_unref(child_io, TRUE);  
  21.     child_io_id = g_io_add_watch(child_io,  
  22.                 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,  
  23.                 child_exit, NULL);  
  24.     g_io_channel_unref(child_io);  
  25.   
  26.     /* Create and bind HCI socket */  
  27.     sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);  
  28.     if (sock < 0) {  
  29.         err = errno;  
  30.         error("Can't open HCI socket: %s (%d)", strerror(err),  
  31.                                 err);  
  32.         return -err;  
  33.     }  
  34.   
  35.     /* Set filter */  
  36.     hci_filter_clear(&flt);  
  37.     hci_filter_set_ptype(HCI_EVENT_PKT, &flt);  
  38.     hci_filter_set_event(EVT_STACK_INTERNAL, &flt);  
  39.     if (setsockopt(sock, SOL_HCI, HCI_FILTER, &flt,  
  40.                             sizeof(flt)) < 0) {  
  41.         err = errno;  
  42.         error("Can't set filter: %s (%d)", strerror(err), err);  
  43.         return -err;  
  44.     }  
  45.   
  46.     memset(&addr, 0, sizeof(addr));  
  47.     addr.hci_family = AF_BLUETOOTH;  
  48.     addr.hci_dev = HCI_DEV_NONE;  
  49.     if (bind(sock, (struct sockaddr *) &addr,  
  50.                             sizeof(addr)) < 0) {  
  51.         err = errno;  
  52.         error("Can't bind HCI socket: %s (%d)",  
  53.                             strerror(err), err);  
  54.         return -err;  
  55.     }  
  56.   
  57.     ctl_io = g_io_channel_unix_new(sock);  
  58.     g_io_channel_set_close_on_unref(ctl_io, TRUE);  
  59.   
  60.     ctl_io_id = g_io_add_watch(ctl_io, G_IO_IN, io_stack_event, NULL);  
  61.   
  62.     g_io_channel_unref(ctl_io);  
  63.   
  64.     /* Initialize already connected devices */  
  65.     return init_known_adapters(sock);  
  66. }  
linux
相关资讯       bluetoothd 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款