2008年4月3日 星期四

如何移植Bluetooth到ARM Embedded Linux

Bluetooth in ARM Linux
- 使用BlueZ架構下,編譯移植 Linux Kernel, bluez-libs, bluez-utils, dbus, libxml2, openobex, libiconv, sobexsrv的>過程

內容如下
1. Bluetooth簡介
2. 硬體方面簡介
2. 軟體架構簡介
3. 移植過程

1. Bluetooth簡介 - 摘錄Linux unwired - Chapter 7[2]
wireless技術.
使用2.4GHz頻率.
最快速度(單向)720 Kbps.
雖然跟WiFi 802.11同是使用頻率2.4GHz, 比較上802.11傳輸速度快但硬體設備相對耗電, 兩者應用範圍不太一致, bluetooth在於
讓兩個設備有連結能力的一個低成本低耗電的方法.
Linux kernel支援Bluetooth已經成熟.
Bluetooth由Special Interest Group(SIG)提出,最早的版本是在1999年. 1.1版在2001年公佈. 1.2版在2003年公佈

Some layers of the BLuetooth specification

---------- ------- -------
| RFCOMM | | SDP | | |
---------- ------- | SCO |
------------------ | |
| L2CAP | | |
------------------ -------
--------------------------
| HCI |
--------------------------
--------------------------
| Radio transport |
--------------------------
RFCOMM: Radio Frequency Communication. Serial emulation
SDP: Service Discovery Protocol
L2CAP: Logical Linck Control and Adaptation Protocol
SCO: for audio
HCI: Host controller interface

Bluetooth可以用來做什麼?
Serial port, Object exchange(OBEX), Synchronization, Networking, Input devices, Audio

Bluetooth Conceputs-

Bluetooth Address:
簡稱 BDADDR, 類似Ethernet上的MAC address, 每個Bluetooth設備都有獨一無二的號碼

Bluetooth Protocols:
規範了許多protocol但並不是每個device都要支援所有的protocol. 列舉一些Protocols.
Link Manager Protocol(LMP), Logical Link Control and Adaptation Protocol(L2CAP), Radio Frequency Communication(RFCOMM), Object Exchange(OBEX), Bluetooth Network Encapsulation Protocol(BNEP), Service Discovery Protocol(SDP), Telephony Control Protocol Specification(TCS)

Bluetooth Profiles:
Profile的意思就是 - 實作一個或一個以上protocols來達到特定應用服務
常見的有
Service Discovery Access Profile(SDAP), Serial Port Profile(SPP), Hardcopy Cable Replacement(HCRP), Dial-up Networking Profile(DUN), LAN Access Profile(LAP), Headset Profile(HS), Generic Object Exchange Profile(GOEP), File Transfer Profile(FTP), Synchronization Profile(SP), Human Interface Device Profile(HID), Personal Area Networking(PAN), Basic Printing Profile(BPP)

Bonding:
Bonding又叫pairing.一個建立兩個Bluetooth device的過程.使用者需要輸入Personal Identification numbers(PINs)來驗證. >通常PIN都是4個數字, 只能提供很低的安全性. 但當兩者設備Bonding完畢. 兩者會產生一組Link key來保障傳遞時的安全性

Discoverability:
當其他Bluetooth設備可以探訪到這台Bluetooth設備,就是代表有discovery的能力

Device classes:
因為Bluetooth device有很多方面的功能, 快速的指出這個device主要的功能就是靠class
class code是24-bit number組成

bit 12-8
00000 Miscellaneous
00001 Computer(from desktop to PDA)
00010 Telephone(cell phone, payphone, cordless phone)
00011 Network access point
00100 Audio/Video device(headset, speakers)
00101 Peripheral(Keyboard, mouse, joystick)
00110 Imaging(printer, camera, scanner)
11111 Uncategorized


Bit 16 Position(location information, e.g. GPS_
Bit 17 Networking
Bit 18 Rendering(printer, speakers)
Bit 19 Capturing(scanner, microphone)
Bit 20 Object transfer
Bit 21 Audio(speaker, microphone, headset)
Bit 22 Telephone(modem, cordless telephone, headset)
Bit 23 Information(web server)

ex: 如果class = 0x3e0100
代表的就是 0011 1110 0000 0001 0000 0000
- Audio(speaker, microphone, headset)
- Object transfer
- Capturing(scanner, microphone)
- Rendering(printer, speakers)
- Networking
- Computer(from desktop to PDA)

2. 硬體
CSR的USB Bluetooth dongle,售價大概台幣NT150左右
拿來插入工作的主機USB port後,先用一些hci相關指令查看一些詳細的資訊

代碼:
$ hciconfig -a


代碼:
...
hci1: Type: USB
BD Address: 00:02:5B:01:CD:FF ACL MTU: 384:8 SCO MTU: 64:8
UP RUNNING PSCAN ISCAN
RX bytes:1339 acl:0 sco:0 events:41 errors:0
TX bytes:393 acl:0 sco:0 commands:30 errors:0
Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80
Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
Link policy: RSWITCH HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Name: 'clc-HP-1'
Class: 0x08010c
Service Classes: Capturing
Device Class: Computer, Laptop
HCI Ver: 1.2 (0x2) HCI Rev: 0x7a6 LMP Ver: 1.2 (0x2) LMP Subver: 0x7a6
Manufacturer: Cambridge Silicon Radio (10)



在根據BD Address
代碼:
$ sudo hcitool info 00:02:5B:01:CD:FF


代碼:
Requesting information ...
BD Address: 00:02:5B:01:CD:FF
Device Name: clc-HP-1
LMP Version: 2.0 (0x3) LMP Subversion: 0x7a6
Manufacturer: Cambridge Silicon Radio (10)
Features: 0xff 0xff 0x8f 0xfe 0x9b 0xf9 0x00 0x80
<3-slot> <5-slot>







<3-slot> <5-slot>

<3-slot>


3. 移植過程
# bluez-utils : bluez-libs, dbus,
# dbus : libxml2
# sobexsrv : openobex, libiconv

==============================================================================================================
#bluez-libs-3.24
代碼:
tar zxvf bluez-libs-3.24.tar.gz
cd bluez-libs-3.24
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC=arm-linux-gcc --build=i686 --host=arm-linux


==============================================================================================================
#libxml2-2.6.31
代碼:
tar zxvf libxml2-2.6.31.tar.gz
cd libxml2-2.6.31
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC=arm-linux-gcc --build=i686 --host=arm-linux


==============================================================================================================
#dbus-1.0.2
代碼:
tar zxvf dbus-1.0.2.tar.gz
cd dbus-1.0.2
export ac_cv_have_abstract_sockets=yes
export PKG_CONFIG_PATH=/work/msq/test/blue_dir/lib/pkgconfig
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC=arm-linux-gcc --build=i686 --host=arm-linux --with-x=no


==============================================================================================================
#bluez-utils-3.24
代碼:
tar zxvf bluez-utils-3.24.tar.gz
cd bluez-utils-3.24
export PKG_CONFIG_PATH=/work/msq/test/blue_dir/lib/pkgconfig
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC="arm-linux-gcc -I/work/msq/test/blue_dir/include -L/work/msq/test/blue_dir/lib" --build=i686 --host=arm-linux


有一些問題
1.
代碼:
arm-linux-gcc -I/work/msq/test/blue_dir/include -I/work/msq/test/blue_dir/include -I/work/msq/test/blue_dir/include -I/usr/local/include -I/work/msq/test/blue_dir/include/dbus-1.0 -I/work/msq/test/blue_dir/lib/dbus-1.0/include -I../eglib -Wall -O2 -D_FORTIFY_SOURCE=2 -o bluetoothd-service-transfer main.o server.o session.o process.o -L/work/msq/test/blue_dir/lib ../common/libhelper.a ../eglib/.libs/libeglib.a /work/msq/test/blue_dir/lib/libdbus-1.so -L/usr/local/lib /usr/local/lib/libopenobex.so /work/msq/test/blue_dir/lib/libbluetooth.so -Wl,--rpath -Wl,/work/msq/test/blue_dir/lib -Wl,--rpath -Wl,/work/msq/test/blue_dir/lib
/usr/local/lib/libopenobex.so: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make[2]: *** [bluetoothd-service-transfer] Error 1
make[2]: Leaving directory `/work/msq/test/bluez/bluez-utils-3.24/transfer'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/work/msq/test/bluez/bluez-utils-3.24'
make: *** [all] Error 2


#ANS
cd transfer
#去掉 /usr/local/lib/libopenobex.so
arm-linux-gcc -I/work/msq/test/blue_dir/include -I/work/msq/test/blue_dir/include -I/work/msq/test/blue_dir/include -I/usr/local/include -I/work/msq/test/blue_dir/include/dbus-1.0 -I/work/msq/test/blue_dir/lib/dbus-1.0/include -I../eglib -Wall -O2 -D_FORTIFY_SOURCE=2 -o bluetoothd-service-transfer main.o server.o session.o process.o -L/work/msq/test/blue_dir/lib ../common/libhelper.a ../eglib/.libs/libeglib.a /work/msq/test/blue_dir/lib/libdbus-1.so -L/usr/local/lib /work/msq/test/blue_dir/lib/libbluetooth.so -Wl,--rpath -Wl,/work/msq/test/blue_dir/lib -Wl,--rpath -Wl,/work/msq/test/blue_dir/lib


2.
代碼:
arm-linux-gcc -I/work/msq/test/blue_dir/include -L/work/msq/test/blue_dir/lib -DHAVE_CONFIG_H -I. -I.. -I../common -I/work/msq/test/blue_dir/include -Wall -O2 -D_FORTIFY_SOURCE=2 -MT hciattach.o -MD -MP -MF .deps/hciattach.Tpo -c -o hciattach.o hciattach.c
hciattach.c: In function 'uart_speed':
hciattach.c:108: error: 'B230400' undeclared (first use in this function)
hciattach.c:108: error: (Each undeclared identifier is reported only once
hciattach.c:108: error: for each function it appears in.)
hciattach.c:110: error: 'B460800' undeclared (first use in this function)
hciattach.c:112: error: 'B500000' undeclared (first use in this function)
hciattach.c:114: error: 'B576000' undeclared (first use in this function)
hciattach.c:116: error: 'B921600' undeclared (first use in this function)
hciattach.c:118: error: 'B1000000' undeclared (first use in this function)
hciattach.c:120: error: 'B1152000' undeclared (first use in this function)
hciattach.c:122: error: 'B1500000' undeclared (first use in this function)
hciattach.c:124: error: 'B2000000' undeclared (first use in this function)
hciattach.c:126: error: 'B2500000' undeclared (first use in this function)
hciattach.c:128: error: 'B3000000' undeclared (first use in this function)
hciattach.c:130: error: 'B3500000' undeclared (first use in this function)
hciattach.c:132: error: 'B4000000' undeclared (first use in this function)
make[2]: *** [hciattach.o] Error 1
make[2]: Leaving directory `/work/msq/test/bluez/bluez-utils-3.24/tools'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/work/msq/test/bluez/bluez-utils-3.24'
make: *** [all] Error 2


#ANS
cd tools
vi hciattach.c
註解108-132行

==============================================================================================================
#openobex-1.3
代碼:
tar zxvf openobex-1.3.tar.gz
cd openobex-1.3
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC="arm-linux-gcc -I/work/msq/test/blue_dir/include -L/work/msq/test/blue_dir/lib" --build=i686 --host=arm-linux


==============================================================================================================
#libiconv-1.12
代碼:
tar zxvf libiconv-1.12.tar.gz
cd libiconv-1.12
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC=arm-linux-gcc --build=i686 --host=arm-linux


==============================================================================================================
#sobexsrv-1.0.1
代碼:
tar zxvf sobexsrv-1.0.1.tar.gz
cd sobexsrv-1.0.1
make CC="arm-linux-gcc -I/work/msq/test/blue_dir/include -L/work/msq/test/blue_dir/lib -liconv" DESTDIR=/work/msq/test/blue_dir install


==============================================================================================================
#bluez-hcidump-1.41
代碼:
tar zxvf bluez-hcidump-1.41.tar.gz
cd bluez-hcidump-1.41
./configure --prefix=/work/msq/test/blue_dir --target=arm-linux CC="arm-linux-gcc -I/work/msq/test/blue_dir/include -L/work/msq/test/blue_dir/lib" --build=i686 --host=arm-linux


======in target board========
代碼:
dbus-daemon --system

dbus-daemon: can't load library 'libxml2.so.2'

代碼:
cd /lib
cp /work/msq/test/blue_dir/lib/libxml2.so.2.6.31 .
ln -s libxml2.so.2.6.31 libxml2.so.2
ln -s libxml2.so.2.6.31 libxml2.so

cp /work/msq/test/blue_dir/lib/libbluetooth.so.2.9.4 /lib
ln -s libbluetooth.so.2.9.4 libbluetooth.so.2
ln -s libbluetooth.s
o.2.9.4 libbluetooth.so


參考:
1. bluez官網; http://www.bluez.org/; 找參考資料,下載原始碼一定都會來的地方
2. Linux Unwired - Chapter 7. Bluetooth, oreilly; By Roger Weeks, Edd Dumbill, Brian Jepson; 簡介Bluetooth觀念, 介
紹BlueZ這個Linux上最流行的Bluetooth protocol stack, 以及一些如何在Linux上安裝BlueZ的步驟
3. bluetooth study; http://tw.myblog.yahoo.com/lecheel/article?mid=136; By Lecheel; 移植Bluez到ARM Linux板子上的筆

1 則留言:

Moonist 提到...

您好,我希望将bluez的协议移植到s3c2440的板子上面,但是编译bluez-utils的时候发现
“/opt/crosstool/gcc-4.0.2-glibc-2.3.6/arm-linux/lib/gcc/arm-linux/4.0.2/../../../../arm-linux/lib/libc_nonshared.a(elf-init.oST): In function `__libc_csu_init':
elf-init.c:(.text+0x50): undefined reference to `__init_array_start'
elf-init.c:(.text+0x54): undefined reference to `__init_array_end' ”之类的错误,根据网上的一些说经,将GCC的CFLAG中添加-shared选项,编译通过了,但是在执行的时候出现segmentation fault 错误,请问您是否遇到过这样的问题?另外请教一下您使用的GCC的版本。
谢谢