【BlueZ5】 板载驱动移植全过程,实现Ble Slave(Dbus版本)

一,编译依赖库与必要应用

1,准备编译工作之前,需要了解什么是buildroot,跨平台的库与文件都通过buildroot来实现。

About Buildroot:Buildroot is a tool that simplifies and automates the process of building a complete Linux system for an embedded system, using cross-compilation.
In order to achieve this, Buildroot is able to generate a cross-compilation toolchain, a root filesystem, a Linux kernel image and a bootloader for your target. Buildroot can be used for any combination of these options, independently (you can for example use an existing cross-compilation toolchain, and build only your root filesystem with Buildroot).
Buildroot is useful mainly for people working with embedded systems. Embedded systems often use processors that are not the regular x86 processors everyone is used to having in his PC. They can be PowerPC processors, MIPS processors, ARM processors, etc.
Buildroot supports numerous processors and their variants; it also comes with default configurations for several boards available off-the-shelf. Besides this, a number of third-party projects are based on, or develop their BSP 1 or SDK 2 on top of Buildroot.

Buildroot信息:https://buildroot.org/
Linux下获取链接:

git clone https://git.buildroot.net/buildroot   
git clone git://git.buildroot.net/buildroot

2,设定对应交叉工具链的Buildroot配置信息(如下以Mstar平台为例)

2.1 配置Target Options

| |                                    Target Architecture (ARM (little endian))  --->                                                   | |  
| |                                    Target Binary Format (ELF)  --->                                                                  | |  
| |                                    Target Architecture Variant (cortex-A7)  --->                                                     | |  
| |                                    Target ABI (EABIhf)  --->                                                                         | |  
| |                                    Floating point strategy (VFPv2)  --->                                                             | |  
| |                                    ARM instruction set (ARM)  --->

注意选择对应平台的信息!!!

2.2 配置Toolchain(这一步很关键,如果错了会直接影响编译)

+--------------------------------------------------------------- Toolchain ----------------------------------------------------------------+
  |  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters are hotkeys.  Pressing <Y>  |  
  |  selects a feature, while <N> excludes a feature.  Press <Esc><Esc> to exit, <?> for Help, </> for Search.  Legend: [*] feature is       |  
  |  selected  [ ] feature is excluded                                                                                                       |  
  |                                                                                                                                          |  
  | +--------------------------------------------------------------------------------------------------------------------------------------+ |  
  | |                                    Toolchain type (External toolchain)  --->                                                         | |  
  | |                                    *** Toolchain External Options ***                                                                | |  
  | |                                    Toolchain (Custom toolchain)  --->                                                                | |  
  | |                                    Toolchain origin (Pre-installed toolchain)  --->                                                  | |  
  | |                                (/opt/arm-buildroot-linux-uclibcgnueabihf-4.9.4/usr) Toolchain path                                   | |  
  | |                                (arm-buildroot-linux-uclibcgnueabihf) Toolchain prefix                                                | |  
  | |                                    External toolchain gcc version (4.9.x)  --->                                                      | |  
  | |                                    External toolchain kernel headers series (3.18.x)  --->                                           | |  
  | |                                    External toolchain C library (uClibc/uClibc-ng)  --->                                             | |  
  | |                                [*] Toolchain has WCHAR support?                                                                      | |  
  | |                                [ ] Toolchain has locale support?                                                                     | |  
  | |                                [*] Toolchain has threads support?                                                                    | |  
  | |                                [*]   Toolchain has threads debugging support?                                                        | |  
  | |                                [*]   Toolchain has NPTL threads support?                                                             | |  
  | |                                [*] Toolchain has SSP support?                                                                        | |  
  | |                                [*]   Toolchain has SSP strong support?                                                               | |  
  | |                                [ ] Toolchain has RPC support?                                                                        | |  
  | |                                [*] Toolchain has C++ support?                                                                        | |  
  | |                                [ ] Toolchain has D support?                                                                          | |  
  | |                                [ ] Toolchain has Fortran support?                                                                    | |  
  | |                                [ ] Toolchain has OpenMP support?                                                                     | |  
  | |                                [ ] Copy gdb server to the Target                                                                     | |  
  | |                                    *** Host GDB Options ***                                                                          | |  
  | |                                [*] Build cross gdb for the host                                                                      | |  
  | |                                [ ]   TUI support                                                                                     | |  
  | |                                [ ]   Python support                                                                                  | |  
  | |                                [ ]   Simulator support                                                 
  | |                                      GDB debugger Version (gdb 8.2.x)  --->                                                          | |  
  | |                                    *** Toolchain Generic Options ***                                                                 | |  
  | |                                ()  Extra toolchain libraries to be copied to target                                                  | |  
  | |                                [*] Enable MMU support                                                                                | |  
  | |                                ()  Target Optimizations                                                                              | |  
  | |                                ()  Target linker options                                                                             | |  
  | |                                [ ] Register toolchain within Eclipse Buildroot plug-in                                               | |                                
  | +--------------------------------↓(+)--------------------------------------------------------------------------------------------------+ |  
  +------------------------------------------------------------------------------------------------------------------------------------------+  
  |                                         <Select>    < Exit >    < Help >    < Save >    < Load >                                         |  
  +------------------------------------------------------------------------------------------------------------------------------------------+

注意:
a, 其中Tool Path和Tool Prefix填自己的信息,之后会让你重新配置gcc version和kernel version,依葫芦画瓢就可以了。
b,如出现Cannot execute cross-compile,请核对再三Target Option信息。(笔者亲身经历:前期无法执行交叉工具链的,配置信息有误)

2.3 配置BlueZ5,并执行编译。

clone之后,在buildroot目录下,再次执行

make menuconfig

//  查找bluez5在哪?
 Symbol: BR2_PACKAGE_BLUEZ5_UTILS [=y]                                                                                                    |  
  | Type  : bool                                                                                                                             |  
  | Prompt: bluez-utils 5.x                                                                                                                  |  
  |   Location:                                                                                                                              |  
  |     -> Target packages                                                                                                                   |  
  | (6)   -> Networking applications                                                                                                         |  
  |   Defined at package/bluez5_utils/Config.in:1                                                                                            |  
  |   Depends on: BR2_USE_WCHAR [=y] && BR2_TOOLCHAIN_HAS_THREADS [=y] && BR2_USE_MMU [=y] && !BR2_STATIC_LIBS [=n] && BR2_TOOLCHAIN_HEADERS |  
  |   Selects: BR2_PACKAGE_DBUS [=y] && BR2_PACKAGE_LIBGLIB2 [=y]                                                                            |  
  |   Selected by [n]:                                                                                                                       |  
  |   - BR2_PACKAGE_BLUEZ_ALSA [=n] && !BR2_STATIC_LIBS [=n] && BR2_TOOLCHAIN_HAS_THREADS_NPTL [=y] && BR2_TOOLCHAIN_HEADERS_AT_LEAST_3_4 [= |  
  |   - BR2_PACKAGE_GST1_PLUGINS_BAD_PLUGIN_BLUEZ [=n] && BR2_PACKAGE_GSTREAMER1 [=n] && BR2_PACKAGE_GST1_PLUGINS_BAD [=n] && BR2_USE_WCHAR  |  
  |   - BR2_PACKAGE_KODI_BLUEZ [=n] && BR2_PACKAGE_KODI [=n] && BR2_TOOLCHAIN_HEADERS_AT_LEAST_3_4 [=y] && BR2_TOOLCHAIN_HAS_SYNC_4 [=y]     |  
  |   - BR2_PACKAGE_CWIID [=n] && !BR2_STATIC_LIBS [=n] && BR2_USE_WCHAR [=y] && BR2_TOOLCHAIN_HAS_THREADS [=y] && BR2_USE_MMU [=y] && BR2_T |  
  |   - BR2_PACKAGE_USSP_PUSH [=n] && !BR2_STATIC_LIBS [=n] && BR2_USE_WCHAR [=y] && BR2_TOOLCHAIN_HAS_THREADS [=y] && BR2_USE_MMU [=y] && B |  
  |   - BR2_PACKAGE_SCONESERVER_BLUETOOTH [=n] && BR2_PACKAGE_SCONESERVER [=n] && BR2_USE_WCHAR [=y] && BR2_USE_MMU [=y] && BR2_TOOLCHAIN_HA |  
  |   - BR2_PACKAGE_BLUEZ_UTILS [=n] && !BR2_SKIP_LEGACY [=n] && BR2_TOOLCHAIN_HEADERS_AT_LEAST_3_4 [=y] && BR2_TOOLCHAIN_HAS_SYNC_4 [=y]    |  
  |                                                                                                                                          |  

选择所需配置:

//  当你勾选了bluez-utils5之后,所有的依赖库都会被捆绑在一起。
//  这里我只用bluez5实现ble slave,故没有选取过多设置,可以按需选取

  | |                                [*] bluez-utils 5.x                                                                                   | |  
  | |                                [ ]   build OBEX support                                                                              | |  
  | |                                [*]   build CLI client                                                                                | |  
  | |                                [*]     install deprecated tool                                                                       | |  
  | |                                [ ]   build experimental obexd plugin                                                                 | |  
  | |                                [ ]   build health plugin                                                                             | |  
  | |                                [ ]   build midi profile                                                                              | |  
  | |                                [ ]   build nfc plugin                                                                                | |  
  | |                                [ ]   build sap plugin                                                                                | |  

需要说明的是,buildroot这点做的真的挺傻瓜式操作,一键式捆绑动作。免去了所有麻烦!!!

由于我勾选了rootfs,所以make之后回生成rootfs

>>>   Generating filesystem image rootfs.tar

检查一下Output是否有输出bluez的一系列安装包。

root@ubuntu:/home/cheng/buildroot/buildroot# ls output/build/
alsa-lib-1.2.1.2   host-attr-2.4.48                                            host-libtool-2.4.6   host-pkgconf-1.6.1              libffi-3.3                      skeleton
alsa-utils-1.2.1   host-autoconf-2.69                                          host-libzlib-1.2.11  host-python3-3.8.2              libglib2-2.62.4                 skeleton-init-common
**bluez5_utils-5.52**  host-automake-1.15.1                                        host-lz4-1.9.2       host-python3-setuptools-41.4.0  libiconv-1.15                   skeleton-init-sysv
bluez-alsa-2.1.0   host-dbus-1.12.16                                           host-lzo-2.10        host-skeleton                   libsndfile-1.0.28               toolchain
buildroot-config   host-dbus-glib-0.110                                        host-m4-1.4.18       host-squashfs-4.4               libzlib-1.2.11                  toolchain-external
buildroot-fs       host-expat-2.2.9                                            host-makedevs        host-util-linux-2.35.1          ncurses-6.1                     toolchain-external-custom
build-time.log     host-fakeroot-1.20.2                                        host-meson-0.53.1    host-xz-5.2.4                   packages-file-list-host.txt     zlib
busybox-1.31.1     host-gdb-8.2.1                                              host-mkpasswd        host-zlib                       packages-file-list-staging.txt
dbus-1.12.16       host-gettext                                                host-ncurses-6.1     host-zstd-1.4.3                 packages-file-list.txt
dbus-glib-0.110    host-gettext-tiny-adaa9c64921e80f2b8dd3610ffb508618b9204f3  host-ninja-1.10.0    initscripts                     pcre-8.43
expat-2.2.9        host-libffi-3.3                                             host-patchelf-0.9    libbsd-0.10.0                   readline-8.0
host-acl-2.2.53    host-libglib2-2.62.4                                        host-pcre-8.43       libedit-20190324-3.1            sbc-1.4

确认OK后,就完成了编译工作!

  • 第六点会给出详细地如何替换自己想要的bluez版本的方式!
  • 如需要strip,可以在build option下勾选上,以达到节省空间的要求。

二,移植对应所需配置文件以及依赖库

1,确定板载所需要的配置文件以及库

// 对应bin文件:
bluetoothd  dbus-daemon  hciconfig
// 其中bluetoothd  dbus-daemon 为bluetooth进程和dbus守护进程,这两部分可以在bluez5_utils-5.52和dbus-1.12.16里面找到。

// 对应lib:
libdbus-1.so.3  libexpat.so.1  libglib-2.0.so.0  libiconv.so.2  libintl.so  libncursesw.so.6  libpcre.so.1  libreadline.so.8
// 这部分已经做了相应的精简,具体到每一个文件都不可以落下

// 对应dbus配置文件:
etc/: bluetooth.conf  session.conf system.conf
usr/share: dbus-1  glib-2.0

这里是渔!!!
root@ubuntu:/home/cheng/buildroot/buildroot#  find . -name xxxxx*

// 所有文件与库也可以在生成的rootfs.tar中找到

注意这些库移植从虚拟机到windows、或者到mnt/share时,需要压缩解压缩,否则会出现符号链接异常等问题。

2,确定编译所需要的依赖库

参照上一点中lib,可对应该文件类的所有库都拷贝到对应工具链。

三,准备蓝牙驱动固件以及KO文件

前述:本次使用的是瑞昱Rtl8723du wifi+ble双模芯片,蓝牙类型:USB

1,固件及配置文件:

rtl8723du_config  rtl8723du_fw

2,KO文件:

Makefile  rtk_bt.c  rtk_bt.h  rtk_bt.o.ur-safe  rtk_coex.c  rtk_coex.h  rtk_coex.o.ur-safe  rtk_misc.c  rtk_misc.h

// 直接make即可

Makefile       Module.symvers  rtk_bt.h  rtk_bt.o.ur-safe  rtk_btusb.mod.c  rtk_btusb.o  rtk_coex.h  rtk_coex.o.ur-safe  rtk_misc.h
modules.order  rtk_bt.c        rtk_bt.o  rtk_btusb.ko      rtk_btusb.mod.o  rtk_coex.c   rtk_coex.o  rtk_misc.c          rtk_misc.o

对应固件放入lib/firmware 即可。

四,准备脚本文件以及编译根文件系统

1,准备启动脚本文件

#!/bin/sh

## debug in sd card

insmod /mnt/sdcard/bluez_bin/rtk_btusb.ko   // 加载驱动,自载入firmware到rtl8723du中

#export LD_LIBRARY_PATH=/mnt/sdcard/bluez_lib

cp -rf /mnt/sdcard/bluez_lib /var/lib/     // 由于链接了动态库到/var/lib,所以把库都copy到该位置,编译rootfs可把所有都cp -rf bluez_lib/* /lib/

mkdir /var/run/dbus       // dbus-daemon 启动时会动态生成socket file,所以需要在该目录下创建dbus文件夹

/mnt/sdcard/bluez_bin/dbus-daemon --system &  // 启动dbus-daemon

sleep 1
/mnt/sdcard/bluez_bin/hciconfig hci0 up      // 启动蓝牙
sleep 1
/mnt/sdcard/bluez_bin/bluetoothd --compat -n  &  // 启动bluetoothd
sleep 1

/mnt/sdcard/bluez_bin/hciconfig hci0 noscan   // 双模芯片,主动关闭piscan
sleep 1

/mnt/sdcard/bluez_bin/hciconfig hci0 name 'TY'  // 设置名字
sleep 1

/mnt/sdcard/bluez_bin/hciconfig hci0 down    
sleep 1

/mnt/sdcard/bluez_bin/hciconfig hci0 up    // 重启蓝牙
sleep 2

2,准备rootfs,并烧录Mstar平台

a:将第二点中对应可执行文件拷贝到bin中

b:将第二点中对应lib拷贝到/lib/中

c:将usr和etc文件拷贝到对应位置。

d:烧录rootfs到对应区域。

// 确认分区
/mnt/mmcblk0p1 # cat /proc/mtd 
dev:    size   erasesize  name
mtd0: 00050000 00010000 "boot"
mtd1: 00220000 00010000 "rootfs"
mtd2: 00200000 00010000 "kernel"
mtd3: 001e0000 00010000 "nvrservice"
mtd4: 004a0000 00010000 "system"
mtd5: 004a0000 00010000 "backup"
mtd6: 00050000 00010000 "mtd"
mtd7: 00020000 00010000 "factory"

// 烧录rootfs
/mnt/mmcblk0p1 # flashcp rootfs.squashfs.img /dev/mtd1 -v

五,实现跨平台Demo,验证与测试

1, 确认设备是否可正常运行

// 查看设备加载是否成功
/mnt/mmcblk0p1 # lsmod
rtk_btusb 39450 0 - Live 0xbf920000 (O)

// 查看进程是否运行正常
mnt/mmcblk0p1 # ps
 2847 dbus      0:01 /mnt/sdcard/bluez_bin/dbus-daemon --system
 2924 root      0:00 ./bluez_bin/bluetoothd --compat -n

2,验证性测试

【暂时先屏蔽该细节】

3,输出接口库验证

【暂时先屏蔽该细节】

六,问题总结与梳理

1, 如何更换BlueZ5版本。

root@ubuntu:/home/cheng/buildroot/buildroot# vi package/bluez5_utils/bluez5_utils.mk

// 更换版本信息
################################################################################
#
# bluez5_utils
#
################################################################################

# Keep the version and patches in sync with bluez5_utils-headers
BLUEZ5_UTILS_VERSION = 5.52   // 更改此处版本信息
BLUEZ5_UTILS_SOURCE = bluez-$(BLUEZ5_UTILS_VERSION).tar.xz
BLUEZ5_UTILS_SITE = $(BR2_KERNEL_MIRROR)/linux/bluetooth
BLUEZ5_UTILS_INSTALL_STAGING = YES
BLUEZ5_UTILS_LICENSE = GPL-2.0+, LGPL-2.1+
BLUEZ5_UTILS_LICENSE_FILES = COPYING COPYING.LIB

// 更新Hash值,该网站下有所有Bluez的版本,切换版本会校验Hash值
# From https://www.kernel.org/pub/linux/bluetooth/sha256sums.asc:
sha256  f7144ce2039202cfac18ccb52426efea11c98e4f6e1bb8041bcb994b8378560a  bluez-5.52.tar.xz

2,问题梳理与说明

【Issue】:is not allowed to own the service "org.bluez" due to security policies in the configuration file
【Issue】:Failed to start message bus: Could not get UID and GID for username "dbus"

该问题主要是dbus权限问题,以及bluetooth.conf配置信息未加载

权限可添加至/etc/passwd

root@ubuntu:/home/cheng/buildroot/buildroot# cat output/target/etc/passwd
root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/false
dbus:x:1000:1000:DBus messagebus user:/var/run/dbus:/bin/false

以及,bluetooth.conf

root@ubuntu:/home/cheng/buildroot/buildroot# find . -name bluetooth.conf
./output/host/arm-buildroot-linux-uclibcgnueabihf/sysroot/etc/dbus-1/system.d/bluetooth.conf
./output/build/bluez5_utils-5.52/src/bluetooth.conf
./output/target/etc/dbus-1/system.d/bluetooth.conf

./output/target/etc/dbus-1/system.d/bluetooth.conf
// 拷贝对那个/etc下dbus-1到对应rootfs目录下

【Issue】:Failed to connect to socket /var/run/dbus/system_bus_socket: No such file or directory

该问题是因为执行dbus-daemon时,无法在/var/run下创建dbus/system_bus_socket,故运行Bluetoothd的时候无法连接到该socket file。

解决方式:

可提前在/var/run下mkdir dbus,重新运行dbus-daemon的时候会自动生成该文件,继而可以运行bluetoothd。

【说明】:关于bluetoothd的说明

NAME
bluetoothd - Bluetooth daemon

SYNOPSIS
bluetoothd [--version] | [--help]
bluetoothd [--nodetach] [--compat] [--experimental] [--debug=<files>] [--plugin=<plugins>] [--noplugin=<plugins>]


DESCRIPTION
This manual page documents briefly the bluetoothd daemon, which manages all the Bluetooth devices. bluetoothd can also provide a number of services via the D-Bus message bus system.

OPTIONS
-v, --version
Print bluetoothd version and exit.
-h, --help
Print bluetoothd options and exit.
-n, --nodetach
Enable logging in foreground. Directs log output to the controlling terminal in addition to syslog.
-f, --configfile
Specifies an explicit config file path instead of relying on the default path (/etc/bluetooth/main.conf) for the config file.
-d, --debug=<file1>:<file2>:...
Sets how much information bluetoothd sends to the log destination (usually syslog's "daemon" facility). If the file options are omitted, then debugging information from all the source files are printed. If file options are present, then only debug prints from that source file are printed. The option can be a pattern containing "*" and "?" characters.
Example: --debug=src/adapter.c:src/agent.c

-p, --plugin=<plugin1>,<plugin2>,..
Load these plugins only. The option can be a pattern containing "*" and "?" characters.
-P, --noplugin=<plugin1>,<plugin2>,..
Never load these plugins. The option can be a pattern containing "*" and "?" characters.
-C, --compat
Provide deprecated command line interfaces.
-E, --experimental
Enable experimental interfaces. Those interfaces are not guaranteed to be compatible or present in future releases.
FILES
/etc/bluetooth/main.conf
Location of the global configuration file.
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tim-Cheng

你的鼓励是我最大的动力,奥利给

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值