目的
在平台移植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,主要介紹下幾個指令。
hciconfig
檢視下幫助資訊
[[email protected] /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主裝置是掃描不到的
hcitool
檢視下幫助資訊
[[email protected] /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檔案,交叉編譯一下即可。具體指令如下:
–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
檢視下藍牙節點
[[email protected] /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服務
有了藍牙節點後,接下來就可以建立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調試
建立了服務,可以使用ble藍牙調試工具去掃描服務和進行簡單的通訊檢驗,我使用了IOS的nRF Connect工具

ble初始化腳本
為了友善初始化藍牙,寫了個啟動腳本blue_open.sh
[[email protected] /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