歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Ubuntu Bluetooth 調試

Ubuntu Bluetooth 調試

日期:2017/3/1 10:20:25   编辑:Linux編程

源碼: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. info("Starting SDP server");
  6. if (init_server(mtu, master, compat) < 0) {
  7. error("Server initialization failed");
  8. return -1;
  9. }
  10. if (did && strlen(did) > 0) {
  11. const char *ptr = did;
  12. uint16_t vid = 0x0000, pid = 0x0000, ver = 0x0000;
  13. vid = (uint16_t) strtol(ptr, NULL, 16);
  14. ptr = strchr(ptr, ':');
  15. if (ptr) {
  16. pid = (uint16_t) strtol(ptr + 1, NULL, 16);
  17. ptr = strchr(ptr + 1, ':');
  18. if (ptr)
  19. ver = (uint16_t) strtol(ptr + 1, NULL, 16);
  20. register_device_id(vid, pid, ver);
  21. }
  22. }
  23. //create a channel according to socket, just like create a port according to the socket
  24. //then add io_accept_event func listen to the channel if there are someone connect to
  25. //the channel, just like we create a port on linux, then we will listen to the port because
  26. //there maybe someone connect to the port, here we act as a server.
  27. l2cap_io = g_io_channel_unix_new(l2cap_sock);
  28. g_io_channel_set_close_on_unref(l2cap_io, TRUE);
  29. g_io_add_watch(l2cap_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
  30. io_accept_event, &l2cap_sock);
  31. if (compat && unix_sock > fileno(stderr)) {
  32. unix_io = g_io_channel_unix_new(unix_sock);
  33. g_io_channel_set_close_on_unref(unix_io, TRUE);
  34. g_io_add_watch(unix_io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
  35. io_accept_event, &unix_sock);
  36. }
  37. return 0;
  38. }
這個函數在最後創建裡l2cap_io,並用io_accept_event接口偵聽此channel。這裡www.linuxidc.com我的理解就類似於linux下我們創建端口port後,會用listen接口去偵聽創建的端口,這樣一旦有client連接上來,我們就可以用accept接口去接受連接。這裡我覺得應該原理相同,這裡無非是用glib庫實現而已。
Copyright © Linux教程網 All Rights Reserved