Ubuntu Bluetooth 调试


源码:bluez_4.66.orig.tar.gz

编译

编译bluez-4.66时,在configure时,遇到如下dbus错误:

configure: error: D-Bus library is required

解决方法:

sudo apt-get install libdbus-1-dev libdbus-glib-1-dev

make -j4

最终产生bluetoothd,在src/.libs/目录下。

运行

在Ubuntu下,系统启动时默认已经启动里了bluetoothd,只是这个bluetoothd位于/usr/sbin/下。我们可以用killall -9 bluetoothd将默认启动的bluetoothd干掉,然后手动启动我们自己编译的bluetoothd,经检验,自己编译的bluetoothd也是可以配合运行的,基本配对传文件功能也正常。

我们用./bluetoothd -h查看bluetoothd运行命令,结果如下:

Usage:
  bluetoothd [OPTION...]

Help Options:
  -h, --help            Show help options

Application Options:
  -n, --nodaemon        Don't run as daemon in background
  -d, --debug=DEBUG     Enable debug information output
  -u, --udev            Run from udev mode of operation

最简单的情况下,我们用sudo ./bluetoothd去启动bluetoothd程序。用sudo的原因就不需要讲了,因为程序本身用到里很多root权限。我们用这个命令启动后,发现程序马上

就进入后台运行了。在看上面help出来的结果,在后面加上-n参数,这时候发现程序在控制台运行,并且可以用ctrl+c终止程序。这里我主要是为了调试蓝牙模块,所以用

控制台跑程序,以便打印一些我要的信息。其他两个参数后面在研究。

代码解析

代码解析之:start_sdp_server(mtu, main_opts.deviceid, SDP_SERVER_COMPAT);

此部分代码在sdpd-server.c文件中,函数如下:

  1. int start_sdp_server(uint16_t mtu, const char *did, uint32_t flags)  
  2. {  
  3.     int compat = flags & SDP_SERVER_COMPAT;  
  4.     int master = flags & SDP_SERVER_MASTER;  
  5.   
  6.     info("Starting SDP server");  
  7.   
  8.     if (init_server(mtu, master, compat) < 0) {  
  9.         error("Server initialization failed");  
  10.         return -1;  
  11.     }  
  12.   
  13.     if (did && strlen(did) > 0) {  
  14.         const char *ptr = did;  
  15.         uint16_t vid = 0x0000, pid = 0x0000, ver = 0x0000;  
  16.   
  17.         vid = (uint16_t) strtol(ptr, NULL, 16);  
  18.         ptr = strchr(ptr, ':');  
  19.         if (ptr) {  
  20.             pid = (uint16_t) strtol(ptr + 1, NULL, 16);  
  21.             ptr = strchr(ptr + 1, ':');  
  22.             if (ptr)  
  23.                 ver = (uint16_t) strtol(ptr + 1, NULL, 16);  
  24.             register_device_id(vid, pid, ver);  
  25.         }  
  26.     }  
  27.   
  28.     //create a channel according to socket, just like create a port according to the socket   
  29.     //then add io_accept_event func listen to the channel if there are someone connect to   
  30.     //the channel, just like we create a port on linux, then we will listen to the port because   
  31.     //there maybe someone connect to the port, here we act as a server.   
  32.     l2cap_io = g_io_channel_unix_new(l2cap_sock);  
  33.     g_io_channel_set_close_on_unref(l2cap_io, TRUE);  
  34.   
  35.     g_io_add_watch(l2cap_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,  
  36.                     io_accept_event, &l2cap_sock);  
  37.   
  38.     if (compat && unix_sock > fileno(stderr)) {  
  39.         unix_io = g_io_channel_unix_new(unix_sock);  
  40.         g_io_channel_set_close_on_unref(unix_io, TRUE);  
  41.   
  42.         g_io_add_watch(unix_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,  
  43.                     io_accept_event, &unix_sock);  
  44.     }  
  45.   
  46.     return 0;  
  47. }  
这个函数在最后创建里l2cap_io,并用io_accept_event接口侦听此channel。这里www.bkjia.com我的理解就类似于linux下我们创建端口port后,会用listen接口去侦听创建的端口,这样一旦有client连接上来,我们就可以用accept接口去接受连接。这里我觉得应该原理相同,这里无非是用glib库实现而已。
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 下一页

相关内容

    暂无相关文章