在平台移植WiFi6,用的模组是正基AP6275S模块,该模块是wifi+bluetooth二合一的,这篇简单写下自己的移植过程。
主要罗列下关于蓝牙的配置,具体如下:
CONFIG_BT=y
CONFIG_BT_BREDR=y
CONFIG_BT_HS=y
CONFIG_BT_LE=y
CONFIG_BT_DEBUGFS=y
CONFIG_BT_HCIUART=y
CONFIG_BT_HCIUART_H4=y
CONFIG_BT_RFCOMM=y
ONFIG_BT_RFCOMM_TTY=y
CONFIG_BT_BNEP=y
CONFIG_BT_BNEP_MC_FILTER=y
CONFIG_BT_BNEP_PROTO_FILTER=y
CONFIG_BT_HIDP=y
CONFIG_BT_L2CAP=y
CONFIG_BT_SCO=y
我用的开源的Bluez5,主要介绍下几个命令。
查看下帮助信息
[root@eufy /oem]$ hciconfig -h
hciconfig - HCI device configuration utility
Usage:
hciconfig
hciconfig [-a] hciX [command ...]
Commands:
up Open and initialize HCI device
down Close HCI device
reset Reset HCI device
rstat Reset statistic counters
auth Enable Authentication
noauth Disable Authentication
encrypt Enable Encryption
noencrypt Disable Encryption
piscan Enable Page and Inquiry scan
noscan Disable scan
iscan Enable Inquiry scan
pscan Enable Page scan
ptype [type] Get/Set default packet type
lm [mode] Get/Set default link mode
lp [policy] Get/Set default link policy
name [name] Get/Set local name
class [class] Get/Set class of device
voice [voice] Get/Set voice setting
iac [iac] Get/Set inquiry access code
inqtpl [level] Get/Set inquiry transmit power level
inqmode [mode] Get/Set inquiry mode
inqdata [data] Get/Set inquiry data
inqtype [type] Get/Set inquiry scan type
inqparms [win:int] Get/Set inquiry scan window and interval
pageparms [win:int] Get/Set page scan window and interval
pageto [to] Get/Set page timeout
afhmode [mode] Get/Set AFH mode
sspmode [mode] Get/Set Simple Pairing Mode
aclmtu <mtu:pkt> Set ACL MTU and number of packets
scomtu <mtu:pkt> Set SCO MTU and number of packets
delkey <bdaddr> Delete link key from the device
oobdata Get local OOB data
commands Display supported commands
features Display device features
version Display version information
revision Display revision information
block <bdaddr> Add a device to the blacklist
unblock <bdaddr> Remove a device from the blacklist
lerandaddr <bdaddr> Set LE Random Address
leadv [type] Enable LE advertising
0 - Connectable undirected advertising (default)
3 - Non connectable undirected advertising
noleadv Disable LE advertising
lestates Display the supported LE states
hciconfig 选项有很多,介绍下常用的命令,我感觉跟ifconfig命令类似。
#要带上具体的蓝牙节点,我这以hci0节点为例
hciconfig hci0 up #激活蓝牙
hciconfig hci0 down #关闭蓝牙
hciconfig hci0 name XXX #修改蓝牙名
hciconfig hci0 class 0xXXXXXX 修改蓝牙类型
hciconfig hci0 piscan #允许被其它蓝牙扫描到,是PSCAN ISCAN组合
hciconfig hci0 leadv #开启ble蓝牙广播
其中,一定要注意的点是,当创建了ble服务,一定要开始广播,否则其它ble主设备是扫描不到的
查看下帮助信息
[root@eufy /oem]$ hcitool -h
hcitool - HCI Tool ver 5.50
Usage:
hcitool [options] <command> [command parameters]
Options:
--help Display help
-i dev HCI device
Commands:
dev Display local devices
inq Inquire remote devices
scan Scan for remote devices
name Get name from remote device
info Get information from remote device
spinq Start periodic inquiry
epinq Exit periodic inquiry
cmd Submit arbitrary HCI commands
con Display active connections
cc Create connection to remote device
dc Disconnect from remote device
sr Switch master/slave role
cpt Change connection packet type
rssi Display connection RSSI
lq Display link quality
tpl Display transmit power level
afh Display AFH channel map
lp Set/display link policy settings
lst Set/display link supervision timeout
auth Request authentication
enc Set connection encryption
key Change connection link key
clkoff Read clock offset
clock Read local or remote clock
lescan Start LE scan
leinfo Get LE remote information
lewladd Add device to LE White List
lewlrm Remove device from LE White List
lewlsz Read size of LE White List
lewlclr Clear LE White List
lerladd Add device to LE Resolving List
lerlrm Remove device from LE Resolving List
lerlclr Clear LE Resolving List
lerlsz Read size of LE Resolving List
lerlon Enable LE Address Resolution
lerloff Disable LE Address Resolution
lecc Create a LE Connection
ledc Disconnect a LE Connection
lecup LE Connection Update
For more information on the usage of each command use:
hcitool <command> --help
因为我是将该设备作为从设备,所以能用到的也就是扫描命令
hcitool scan # 扫描经典蓝牙
hcitool lescan # 扫描ble设备
将模块厂家提供的固件下载到模块上,用的是brcm_patchram_plus应用程序,下载地址:brcm_patchram_plus,就一个c文件,交叉编译一下即可。具体命令如下:
brcm_patchram_plus -d --enable_hci --no2bytes --tosleep 200000 --baudrate 1500000 --patchram /system/etc/firmware/BCM4362A2.hcd /dev/ttyS4 &
–baudrate设置波特率
–patchram固件的绝对路径
/dev/ttyS4绑定的串口号
出现类似信息表示下载固件成功,会出现蓝牙节点。
received 7
04 0e 04 01 4c fc 00
writing
01 4c fc b2 8e 3e 16 00 00 e0 73 78 a3 74 20 46
16 f7 1f ff 28 b1 26 4b a2 7c 1b 78 9a 42 38 bf
a3 74 20 46 ff f7 e3 fe 20 46 be f6 6c f9 d0 b1
02 23 b5 f8 f2 20 94 f8 24 10 b5 f8 f0 00 db f6
04 f8 00 23 a5 f8 f0 00 84 f8 a0 30 b5 f8 f0 30
23 84 b5 f8 f2 30 63 84 02 23 84 f8 24 30 01 23
84 f8 25 30 06 e0 20 46 18 f7 09 fc 01 46 20 46
17 f7 22 fa 20 46 17 f7 0c fe 03 9a 3b 68 9a 42
01 d0 a5 f6 da fa 05 b0 bd e8 f0 8f 00 bf 90 08
20 00 50 30 20 00 78 6f 20 00 34 30 20 00 4a 30
20 00 a1 91 20 00 28 30 20 00 64 30 20 00 49 30
20 00 54 30 20 00
received 7
04 0e 04 01 4c fc 00
writing
01 4e fc 04 ff ff ff ff
received 7
04 0e 04 01 4e fc 00
writing
01 03 0c 00
writing
01 03 0c 00
received 7
04 0e 04 01 03 0c 00
writing
01 18 fc 06 00 00 60 e3 16 00
received 7
04 0e 04 01 18 fc 00
Done setting baudrate
Done setting line discpline
查看下蓝牙节点
[root@eufy /oem]$ hciconfig -a
hci0: Type: Primary Bus: UART
BD Address: 10:2C:6B:80:2D:17 ACL MTU: 1021:8 SCO MTU: 64:1
DOWN
RX bytes:701 acl:0 sco:0 events:37 errors:0
TX bytes:437 acl:0 sco:0 commands:37 errors:0
Features: 0xbf 0xfe 0xcf 0xfe 0xdb 0xff 0x7b 0x87
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH SNIFF
Link mode: SLAVE ACCEPT
蓝牙节点hci0已经出现
注意,如果下载固件失败,要检查下模块的cts和rts初始状态,cts要为高电平,rts为低电平
有了蓝牙节点后,接下来就可以创建ble服务了,可以阅读Bluez5的源码gatt-service.c帮助创建服务,我编译了一个示例应用程序my_app去创建服务,详细命令见最后。
gatt-service unique name: :1.1
Registered service: /service1
Get Primary: True
Get UUID: 00001802-0000-1000-8000-00805f9b34fb
Exist Includes: 00001802-0000-1000-8000-00805f9b34fb
Get Includes: 00001802-0000-1000-8000-00805f9b34fb
Includes path: /service3
Get Includes: /service2
Characteristic(00002a06-0000-1000-8000-00805f9b34fb): receive data:
Descriptor(8260c653-1a54-426b-9e36-e84c238bc669): Get("Value")
Get Primary: True
Get UUID: 00001802-0000-1000-8000-00805f9b34fb
Exist Includes: 00001802-0000-1000-8000-00805f9b34fb
Get Includes: 00001802-0000-1000-8000-00805f9b34fb
Includes path: /service3
Get Includes: /service2
Characteristic(00002a06-0000-1000-8000-00805f9b34fb): receive data:
Descriptor(8260c653-1a54-426b-9e36-e84c238bc669): Get("Value")
bluetoothd[1948]: include service not found
bluetoothd[1948]: include service not found
RegisterApplication: OK
创建了服务,可以使用ble蓝牙调试工具去扫描服务和进行简单的通讯检验,我使用了IOS的nRF Connect工具
为了方便初始化蓝牙,写了个启动脚本blue_open.sh
[root@eufy /oem]$ cat blue_open.sh
#!/bin/sh
mkdir -p /var/run/dbus/
dbus-daemon --system
echo 0 > /sys/class/rfkill/rfkill0/state
sleep 1
echo 1 > /sys/class/rfkill/rfkill0/state
brcm_patchram_plus -d --enable_hci --no2bytes --tosleep 200000 --baudrate 1500000 --patchram /system/etc/firmware/BCM4362A2.hcd /dev/ttyS4 &
sleep 30
/usr/libexec/bluetooth/bluetoothd --compat -n & #启动蓝牙协议
sleep 3
hciconfig hci0 up
hciconfig hci0 piscan
./my_app &
sleep 2
hciconfig hci0 leadv