linux设备上蓝牙协议栈(bluez)移植

本文章主要记录下linux上移植bluez的一些要点流程,以rv1109设备为例说明,蓝牙芯片为rtl8723ds。

  1. 根据硬件连接,确定蓝牙串口协议是H4 还是 H5,这两个主要差别在于H4是三线串口(rx,tx,gnd),H5是五线串口(三线基础上另加CTS、RTS),确定好串口传输协议,后面就按对应配置选择。
  2. 根据厂商提供的资料,配置kernel支持蓝牙,并编译蓝牙驱动ko,这部分问题不大。
  3. 厂商提供的资料中寻找rtk_hciattach工具源码,编译。
  4. 编译bluez以及相关依赖,rv1109的sdk使用buildroot构建,里面已经有bluez相关工具包了,我们只需要make menuconfig配置下打开,注意还有些相关依赖库,配置完毕编译出新的镜像。
  5. 含有bluez的镜像烧写运行后,就可以把前面编出来的蓝牙驱动ko放进来了,然后插入ko, insmod hci_uart.ko
  6. 使用rtk_hciattach初始化蓝牙(注意修改对应的串口节点)
    • 把前面编译好的rtk_hciattach工具拷贝到板端系统/bin下面,并修改其权限
    • 创建目录/lib/firmware/rtlbt,在供应商提供的资料中找到rtl8723d_config和rtl8723d_fw,拷贝到/lib/firmware/rtlbt,注意有无串口流控,对应的拷贝文件不一样,需要供应商提供
    • rtk_hciattach -n -s 115200 ttyS0 rtk_h5 & //初始化蓝牙
  7. 启动dbus
    • adduser messagebus
    • 把第4步中生成的dbus-daemon和dbus-uuidgen拷贝到/bin下面,并修改其权限
    • dbus-daemon --print-address --system 或者dbus-daemon --config-file=/usr/share/dbus-1/system.conf //运行dbus
    • 上面的命令运行后可能会报错,有可能是cnof文件或者相应的依赖库缺少,根据上面报错的提示,在第4步中编出来的文件找到system.conf和bluetooth.conf等放到提示的目录下,设备上没有的目录则创建。缺失的lib库也拷贝到/lib下面
      举例如下:
      /mnt/# dbus-daemon --print-address --system
      Failed to start message bus: Failed to open "/usr/local/etc/dbus-1/system.conf": No such file or directory(需要拷贝此文件)
      
      /mnt/# dbus-daemon --print-address --system
      Failed to start message bus: Failed to read directory "/usr/local/etc/dbus-1/system.d": No such file or directory(需要创建此目录/usr/local/etc/dbus-1/system.d/)
      
      /mnt/outdir# dbus-daemon --print-address --system
      Failed to start message bus: Failed to bind socket "/usr/local/var/run/dbus/system_bus_socket": No such file or directory(需要创建/usr/local/var/run/dbus/)
      
    dbus运行成功的日志如下:
    /mnt/# dbus-daemon --print-address --system
    Unknown group "lp" in message bus configuration file
    unix:path=/usr/local/var/run/dbus/system_bus_socket,guid=4c34eaaa4681006ae29c7791386d99d8
    或者
    ~ # dbus-daemon --config-file=/usr/local/etc/dbus-1/system.conf
    Unknown group "lp" in message bus configuration file
    
    上述过程中若提示
    /mnt/outdir # dbus-daemon --print-address --system
    Failed to start message bus: The pid file "/usr/local/var/run/dbus/pid" exists, if the message bus is not running, remove this file
    
    则删除/usr/local/var/run/dbus/pid,每次开机都需要删除这个
  8. 把第4步中生成的bluetoothd,bluetoothctl(可选hciconfig,hcitool,l2ping)等拷贝到/bin下面,修改其权限
  9. 运行bluetoothd
    • bluetoothd -d -n -E&
    • 根据提示在第4步的生成物中查找依赖库,并拷贝到/lib下面
      /mnt/outdir # bluetoothd -d -n -E&
      /mnt/outdir # bluetoothd[7028]: Bluetooth daemon 5.44
      D-Bus setup failed: Connection ":1.0" is not allowed to own the service "org.bluez" due to security policies in the configuration file
      bluetoothd[7028]: Unable to get on D-Bus
      
      则把第4步中生成的bluetooth.conf拷贝到/usr/local/etc/dbus-1/和/usr/local/etc/dbus-1/system.d/目录下
  10. 蓝牙测试,两种方法(最好配合手机上的蓝牙测试助手等)
    1. 使用bluetoothctl测试
      /mnt# bluetoothctl								//运行bluetoothctl
      [bluetooth]# show 						//查看控制器的 Power 是否为 yes,如果 Power 为 no,则运行 power on
      [bluetooth]# power on
      [bluetooth]# agent NoInputNoOutput 		//可以设置其他 IO caps, 如 KeyboardDisplay
      [bluetooth]# default-agent
      [bluetooth]# advertise on				//开启ble广播
      [bluetooth]# discoverable on			//允许被发现
      [bluetooth]# scan on 					//扫描到对应的设备后,使用 scan off 关闭 scan。
      [bluetooth]# pair 00:22:48:DC:89:0F 	//配对远端设备。
      [bluetooth]# connect 00:22:48:DC:89:0F 	//连接远端设备
      如果没有加入蓝牙音频相关协议,connect时可能会报错,使用手机上蓝牙测试助手进行无配对测试也可以
      
    2. 使用hciconfig以及hcitool测试
      hciconfig hci0 up
      hciconfig hci0 piscan
      hciconfig hci0 noencrypt
      hciconfig hci0 noauth
      hciconfig
      hcitool scan								//搜索
      l2ping F8:A2:D6:D4:6C:04					//简单ping测试
      l2test -O 675 -s A0:9F:10:7E:5C:E3
      l2test -I 675 -b 10000 -r
      
摘 要:基于对Linux蓝牙协议栈BlueZ 源代码的分析,给出BlueZ的组织结构和特点。分析蓝牙USB 传输驱动机制和数据处理过程, 给出实现蓝牙设备驱动的重要数据结构和流程,并总结Linux 下开发蓝牙USB 设备驱动的一般方法和关键技术。 关键词:Linux 系统;蓝牙协议栈设备驱动 USB Device Driver for Linux Bluetooth Stack LIANG Jun-xue, YU Bin (Institute of Electronic Technology, PLA Information Engineering University, Zhengzhou 450004) 【Abstract】This paper depicts the structure and characteristics of BlueZ based on analyzing the source code of Linux bluetooth stack BlueZ. It analyzes the implementation of bluetooth USB transport driver scheme and data processing procedure in detail, and gives the key data structure and implementation of bluetooth device driver. It summarizes the approach of developing Linux bluetooth USB device driver and the key technology. 【Key wordsLinux system; bluetooth stack; device driver 计 算 机 工 程 Computer Engineering 第 34 卷 第 9 期 Vol.34 No.9 2008 年 5 月 May 2008 ·开发研究与设计技术· 文章编号:1000—3428(2008)09—0273—03 文献标识码:A 中图分类号:TP391 1 概述 蓝牙技术是开放式通信规范,而 Linux 是开放源码的操 作系统。廉价设备与免费软件的结合,促进了蓝牙技术和 Linux 的发展与融合。 Linux最早的蓝牙协议栈是由Axis Communication Inc在 1999 年发布的 OpenBT 协议栈。 随后, IBM 发布了 BlueDrekar 协议栈,但没有公开其源码。Qualcomm Incorporated 在 2001 年发布的 BlueZ 协议栈被接纳为 2.4.6 内核的一部分。此外, Rappore Technology 及 Nokia 的 Affix Bluetooth Stack 都是 Linux 系统下的蓝牙协议栈,应用在不同的设备和领域中。 BlueZLinux 的官方蓝牙协议栈,也是目前应用最广 泛的协议栈,几乎支持所有已通过认证的蓝牙设备。对于基 于主机的蓝牙应用,目前常见的硬件接口有 UART, USB 和 PC 卡等,USB 作为 PC 的标准外设接口,具有连接方便、兼 容性好和支持高速设备等特点,已广泛应用于蓝牙设备。 目前对 Linux 下 USB 设备驱动的研究已较为广泛而深 入[1-4] ,但对 Linux 下的蓝牙设备驱动还没有专门的研究。本 文在分析 USB 设备驱动和蓝牙协议栈的基础上,总结了 Linux 下开发蓝牙 USB 驱动程序的一般方法,并深入剖析了 其关键技术。 2 Linux 蓝牙协议栈 BlueZ 简介 BlueZ 目前已成为一个开放性的源码工程。它可以很好 地在 Linux 支持的各种体系的硬件平台下运行,包括各种单 处理器平台、多处理器平台及超线程系统。 BlueZ 由多个独立的模块组成,内核空间主要包括设备 驱动层、蓝牙核心及 HCI 层、L2CAP 与 SCO 音频层、 RFCOMM, BNEP, CMTP 与 HIDP 层、通用蓝牙 SDP 库和后 台服务及面向所有层的标准套接字接口;在用户空间提供了 蓝牙配置、测试及协议分析等工具。其组织结构如图 1 所示, BlueZ 没有实现专门的 SDP 层,而是将其实现为运行在后台 的蓝牙服务库例程(图 1 没有描述该后台服务)。 RFOMM 层支 持标准的套接口,并提供了串行仿真 TTY 接口,这使串行端 口应用程序和协议可以不加更改地运行在蓝牙设备上,例如 通过点对点协议 PPP 可实现基于 TCP/IP 协议簇的所有网络 应用。BNEP 层实现了蓝牙的以太网仿真,TCP/IP 可以直接 运行于其上。 USB设备驱动 (hci_usb.o) L2CAP层(l2cap.o) RFCOMM层 (rfcomm.o) BNEP层 (bnep.o) CMTP层 (cmtp.o) 串口设备驱动 (hci_uart.o) 虚拟串口设备驱动 (hci_vhci.o) 音频 socket RFCOMM socket BNEP socket CMTP socket L2CAP socket HCI socket 内核 空间 用户 空间 串口设备 CAPI设备 输入设备 网络设备 HDIP socket 音频设备 AF_BLUETOOTH socket 音频层(sco.o) PPP TCP/IP AF_INET socket BNEP层 (bnep.o) 其他设备驱动 (bluecard_cs.o等) BlueZ工具和实用程序 HDIP层 (hdip.o) BlueZ核心 及HCI层(bluez.o/bluetooth.o) 图 1 BlueZ 组织结构 3 蓝牙 USB 设备驱动 设备驱动程序在 Linux 内核中起着重要作用,它使某个 硬件能响应一个定义良好的内部编程接口。这些接口隐藏了 设备的工作细节,用户通过一组独立于特定驱动程序的标准 调用来操作设备。而将这些调用映射到作用于实际硬件设备 的特有操作上,则是驱动程序的任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值