Port Bluetooth bluez

Source: Internet
Author: User

All rights reserved by bluez --- Dennis, High Performance Computing Center, Tongji University

Thank you for sending me an email to discuss dennis.he.2005@gmail.com with me.
First, I will introduce the hardware. I used a USB Bluetooth adapter that I bought at random in a computer store. The price is about 80 yuan. It should be made in Taiwan and there is no brand, like a smaller USB flash drive. Before buying the phone, ask the seller to test it in windows. Bring a mobile phone with Bluetooth and try again.
There are several Bluetooth protocol stacks on Linux, and the Official Bluetooth protocol stack is bluez. All the instructions and source code can be found at www.bluez.org. I first installed bluez's protocol stack on my PC, and then transplanted it to huaheng HHARM2410-R3.

(1) install bluez on Red Hat 9
For PC-based installation, I have referred to O 'Reilly's Linux unwired book. Chapter 7 describes the basic knowledge, installation, configuration, and usage of Bluetooth in Linux. Before installation, you must understand the basic structure of bluez. The Protocol directly above the transmit protocol is HCI (host controlinterface ), the high-level protocols used by my mobile phone and Bluetooth GPS are the Service Discovery Protocol SDP and the Protocol RFCOMM for simulating serial ports. The middle layer of the Protocol is L2CAP. Should it be for processing the Logical Link, I am not very clear. Please refer to the official bluez documentation.
The kernel version used on the HHARM2410-R3 is 2.4.18, so I also used the 2.4.18 kernel when installing bluez on the PC. If you use bluez.pdf to patchthe inner, download linux-2.4.18.tar.bz2 to the kernel on www.kernel.org. So decompress the package and patch it:
# Tar xjvf linux-2.4.18.tar.bz2
# Cd Linux/
# Gzip-DC ../patch-2.4.18-mh15.gz | patch-p1
Note the patch output at this time. In terms of source code, the patch modifies the code in the driver/, include/, and net/directories and does not modify the content in the kernel, there should be no changes to the core of Linux, and there should be no major problems when porting to S3C2410.
The following describes how to compile the kernel. During the config process, my configuration for bluez is as follows:
#
# Bluetooth support
#

Config_bluez = m
Config_bluez_l2cap = m
Config_bluez_sco = m
Config_bluez_rfcomm = m
Config_bluez_rfcomm_tty = y
Config_bluez_bnep = m
Config_bluez_bnep_mc_filter = y
Config_bluez_bnep_proto_filter = y

#
# Bluetooth device drivers
#
Config_bluez_hciusb = m
# Config_bluez_hciusb_sco is not set
Config_bluez_hciuart = m
Config_bluez_hciuart_h4 = m
Config_bluez_hciuart_bcsp = m
# Config_bluez_hciuart_bcsp_txcrc is not set
# Config_bluez_hcibfusb is not set
Config_bluez_hcidtl1 = m
Config_bluez_hcibt3c = m
Config_bluez_hcibluecard = m
Config_bluez_hcibtuart = m
Config_bluez_hcivhci = m
For your reference, the Linux unwired book has a detailed description. Note that if you use makemenuconfig, you must open the. config file to see if config_bluez_rfcomm_tty = Y, not N or M. Also note that config_usb_bluetooth cannot be selected (under USB support), otherwise it will affect the operation of bluez.
After the kernel is compiled, prepare the bluez library and tools. To begin. In PC, there is nothing to say about the installation process. Unzip,./configure, make, and makeinstall.
Then pay attention to the hcid. conf configuration file under the default configuration directory/etc/Bluetooth/, which is used to configure the background daemon process hcid. If you think of this PC machine + Bluetooth adapter as a bluetooth device, the process hcid manages the basic configuration information of the bluetooth device, such as name, class, address, and feature. In addition, when the device is paired with other Bluetooth devices Bonding/pairing, the program that processes pincode is also hcid, while in hcid. one of the most important items in the conf script is pin_helper. The default configuration is pin_helper =/usr/bin/bluepin. This is a program written in Python to read the pincode entered by the user, install the bluez-pin package in/usr/bin.
It is necessary to understand the pairing process. I feel that the two devices connect to each other by using their passwords. After reading the hcid code, I understand that, for example, two bluetooth devices a and B, a first sends a request requiring pairing to B, b. the user sets the password for pairing, that is, pincode, and then returns a message to a, saying that you want to pair. Do you know my password? Then a sends the pincode to the system. B will send a replyok message to a as it matches its own password, so that the two establish a trust relationship.
On the PC + Bluetooth adapter, the bluetooth device A, if the device sends a pairing request to another remote device B, when B sends a password request, the hcid background process running on a reads the PIN code through a pin_helper program and sends it back to B. If remote device B sends a pairing request to, the pincode used for verification on hcid is the content of/etc/Bluetooth/pin. If the read failure Program sets a password by default at startup, the code is in bluez-utils/hcid/security. in the init_security_data function in C, the default value is bluez. To match my mobile phone, I changed it to 123, and the mobile phone can only enter numbers.
For other hcid. conf settings, see man hcid. conf.
After setting the kernel, replace it with the new kernel and start Linux. Insert a USB Bluetooth adapter, # dmesg | tail will find a USB. c: usbdevice 2 (vend/prod 0x1131/0x1001) is not claimed by any activedriver. this problem, no matter it, as long as there is USB. c: registered new driver hci_usb message, then # lsmod will find that the hci_usb module is installed, and # modprobe RFCOMM, so basically all modules are installed.
# Hciconfig hci0 up
# Hcid-F/etc/Bluetooth/hcid. conf
Start the HCI device and hcid genie Process
# Hciconfig-
Hci0: Type: USB
BD address: 00: 11: 67: 0f: BB: A1 acl mtu: 678: 8 sco mtu: 48: 10
Up running pscan iscan
RX Bytes: 115 ACL: 0 SCO: 0 events: 15 errors: 0
TX Bytes: 313 ACL: 0 SCO: 0 commands: 15 errors: 0
Features: 0xff 0xff 0x8d 0x78 0x08 0x18 0x00 0x00
Packet type: DM1 dm3 dm5 dh1 DH3 DH5 hv1 HV2 hv3
Link policy: rswitch hold sniff Park
Link mode: slave accept
Name: 'bluez (0 )'
Class: 0x000100
Service Classes: Unspecified
Device class: Computer, uncategorized
HCI Ver: 1.2 (0x2) HCI Rev: 0x1ae LMP Ver: 1.2 (0x2) LMP subver: 0x1ae
Manufacturer: integrated system solution Corp. (57)
You can see the current configuration of the device. Some of them are read from hcid. conf, and some are read from the device.
Next, turn on the Bluetooth on your phone and start the Bluetooth GPS
# Hcitool Scan
Scanning...
00: 0e: 07: 58: 1A: 33 t628
00: 08: 1b: C0: D1: 3C holux GR-231
Now we can see two devices. I use Sony Ericsson's t628 and holux's bluetooth GPS. The first string should be the unique address of the bluetooth device.
# Sdptool browse can view the services provided by each device
Inquiring...
Browsing 00: 0e: 07: 58: 1A: 33...
Service name: Dial-Up Networking
Service rechandle: 0x10000
Service class ID list:
"Dialup networking" (0x1103)
"Generic networking" (0x1201)
Protocol descriptor list:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 1
Profile descriptor list:
"Dialup networking" (0x1103)
Version: 0x0100

.............

Service name: Serial Port 1
Service rechandle: 0x10003
Service class ID list:
"Serial Port" (0x1101)
Protocol descriptor list:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 4

............

Service name: obex object push
Service rechandle: 0x10005
Service class ID list:
"Obex object push" (0x1105)
Protocol descriptor list:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 10
"Obex" (0x0008)
Profile descriptor list:
"Obex object push" (0x1105)
Version: 0x0100

Browsing 00: 08: 1b: C0: D1: 3C...

Mobile phones are bound to many services, including dial-up services, audio services, serial port services, and file exchange services (obex)... under the SDP protocol, each service is bound to a channel. No service is bound to GPS, which is used only as a virtual serial port device.
The RFCOMM utility can be used to bind RFCOMM-compliant services to a similar serial device. Before binding, you must ensure that the/dev device has RFCOMM *. If not, create the device:
# Mknod-M 666/dev/rfcommx C 216 x (x is a number)
In this way, you can bind it. For example, if you need to use the dial-up function of the mobile phone to access the Internet, you can see that channel 1 is the dialupnetworking function of RFCOMM. Then:
# RFCOMM bind 0 00: 0e: 07: 58: 1A: 33 1
Bind the channel1 service of the 00: 0e: 07: 58: 1A: 33 device to/dev/rfcomm0, it is equivalent to establishing a virtual serial connection to a dial-up wireless modem. Open minicom, set the serial port to/dev/rfcomm0, the baud rate is 19200, 8n1, type atz, and Return OK. You can use PPP to dial up the Internet. When you connect to the internet for the first time, the phone will pop up an input box asking for pincode, which will be no longer needed.
The GPS device is only used as a serial port. It is also very simple:
# RFCOMM bind 1 00: 08: 1b: C0: D1: 3C
Enable minicom and set the baud rate to 4800 to read GPS data!
In addition, the sdpd program is a service provided by the device to external users. You can use sdptool add -- channel = xsp to add external services. I have not studied it. In addition, you still need to learn more about file transfer obex and personal LAN bneq.

2. Transplantation on HHARM2410-R3
After completing the installation on Red Hat, I am familiar with some basic things of the Bluetooth protocol stack. The following task is to move the protocol stack to the Development Board.
The first step is of course the kernel. Since hua Heng's kernel code has been changed, I don't know whether the patch downloaded from bluez can be used. I just tried it.
# Cd/hharm2410/kernel
# Gzip-DC ~ /Patch-2.4.18-mh15.gz | patch-p1
# Find./-name '* rej'
./Fs/cramfs/inode. C. rej
No, there are files that cannot be patched. Check whether the patch is for the cramfs file system. I don't know why the bluez patch will be changed to cramfs, but I think the problem should not be big. The kernel source code of the Bluetooth part of Hua Heng should have not been changed, so the main part of the patch should be no problem. If a problem occurs after the kernel is compiled, I think we can only look at the kernel code.
First, try it. It is similar to that on a common PC, except that some of m are all y now. Part of my configuration about bluez is set:
#
# Bluetooth support
#
Config_bluez = y
Config_bluez_l2cap = y
# Config_bluez_sco is not set
Config_bluez_rfcomm = y
Config_bluez_rfcomm_tty = y
Config_bluez_bnep = y
Config_bluez_bnep_mc_filter = y
Config_bluez_bnep_proto_filter = y
# Config_bluez_hidp is not set
#
# Bluetooth device drivers
#
Config_bluez_hciusb = y
# Config_bluez_hciusb_sco is not set
# Config_bluez_hciuart is not set
# Config_bluez_hcibfusb is not set
# Config_bluez_hcidtl1 is not set
# Config_bluez_hcibt3c is not set
# Config_bluez_hcibluecard is not set
# Config_bluez_hcibtuart is not set
# Config_bluez_hcivhci is not set

The device part is configured with a USB, And the other parts are not configured.
Next we will continue the general process of make Dep; Make zimage. OK, there is no compilation problem, so we can be a little more comfortable.

The following is the basic library of bluez to be transplanted and executed under RedHat 9.
# LDD/usr/lib/libbluetooth. so.1.0.17
Libc. so.6 =>/lib/libc. so.6 (0x4002c000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x80000000)
If you find that there is no dependency on any inexplicable library, you should change the compiler and configure it. My configuration command is simple:
#. /Configure -- Host = armv4l -- prefix =/hharm2410/applications/bluezport/libscc =/opt/host/armv4l/bin/armv4l-unknown-linux-gccCPP =/opt/host/armv4l/bin/armv4l-unknown-linux-cppAR = /opt/host/armv4l/bin/armv4l-unknown-linux-arSTRIP =/opt/host/armv4l/bin/armv4l-unknown-linux-stripRANLIB =/opt/host/armv4l/bin/armv4l-unknown-linux-ranlib
LD =/opt/host/armv4l/bin/armv4l-unknown-linux-ld
# Make
# Make install
Okay. Now let's take a look at/hharm2410/applications/bluezport/libs. I don't know why. A and. La static libraries exist, and there is no dynamic library. Had to do it yourself, to the bluz-libs-2.17/src/, their own dynamic library Compilation:
#/Opt/host/armv4l/bin/armv4l-unknown-linux-gcc-shared-olibbluetooth. so.1.0.17 Bluetooth. o hci. o sdp. o
Connect two symbols
# Ln-s libbluetooth. so.1.0.17 libbluetooth. So
# Ln-s libbluetooth. so.1.0.17 libbluetooth. so.1
All right, after the library is cross-compiled, the MV can be saved to the/hharm2410/application/bluezport/libs/lib folder.

Then transplant the bluez toolset. Similar to the library, let's take a look at the libraries required by each program, for example:
# LDD/usr/sbin/hcid
Libbluetooth. so.1 =>/usr/lib/libbluetooth. so.1 (0x40035000)
Libc. so.6 =>/lib/libc. so.6 (0x40042000)
/Lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000)
Try the required tools one by one and find that the required libraries Hua Heng and bluez are provided. You should change the compiler to compile the tool. Before compilation, I modified the bluez-utils-2.17/hcid/security to the convenience of connecting to my own phone on the board. in the c file, set the default password in the init_security_data function, which is very simple to change to two lines:
Strcpy (hcid. pin_code, "123 ");
Hcid. pin_len = 3;
Then I want to configure Configure. My configure is set:
#. /Configure -- prefix =/HHARM2410-R3/applications/bluezport/utils -- includedir =/HHARM2410-R3/applications/bluezport/libs/include -- libdir =/HHARM2410-R3/applications/bluezport/libs/lib -- with-bluez =/HHARM2410-R3/applications/bluezport/Libs -- disable-test -- disable-cups
-- Disable-pcmia -- Host = armv4svc =/opt/host/armv4l/bin/armv4l-unknown-linux-gccCPP =/opt/host/armv4l/bin/armv4l-unknown-linux-cppAR =/opt/host/armv4l/bin/ armv4l-unknown-linux-arSTRIP =/opt/host/armv4l/bin/armv4l-unknown-linux-stripRANLIB =/opt/host/armv4l/bin/armv4l-unknown-linux-ranlibLD =/opt/host/armv4l/bin/armv4l-unknown-linux-ld
# Make
In this step of make, I encountered a small error. It seems that path_max is not defined. This is the maximum length of the buffer for storing path names set in Linux. It should be caused by a small header file. I did not elaborate on it, so I added three lines to config. h In the current file:
# Ifndef path_max
# Define path_max 4095
# Endif
Use a simple and rude way to add a definition. Make again, pass.
# Make install
All right, the toolkit is under/HHARM2410-R3/applications/bluezport/util
Finally, because the default pin_helper/usr/bin/bluepin in PC Linux is written in Python, it cannot be used on MySQL 2410, therefore, you need to write a pin_helper program. Code of/usr/bin/bluepin and hcid/security. C code. In fact, the hcid process sets up a pipe pointing to the standard output of the pin_helper process during pincode verification. The pin_helper process asks the user for pincode. If the user enters the password XXXX, the "pin: the format of XXXX is written to the standard output. If the user reject is used, write "Err" back.
I implement the pin_helper program in the simplest way, that is, write a code, such as 123, at the cost of security.
Script implementation is:
#! /Bin/sh

Echo "pin: 123"
One implementation in C is:
# Include <stdio. h>

Int main (INT argc, char * argv [])
{
Printf ("pin: 123 ");
}
Compile it into my_pin_helper and put it under/usr/bin. Then you can change hcid. conf of the board:
Pin_helper/usr/pin/my_pin_helper
Later, I wrote a pin_helper on the board using MiniGUI, which is quite simple.

Okay, everything is ready. Do ramdisk well. Remember to put the mknod RFCOMM in/Dev and put it on the board to run it!
Plug in the USB adapter and do not use probe for Modules
# Hciconfig hci0 up
# Hcid-F/etc/Bluetooth/hcid. conf
# Hciconfig-
The device is displayed, and other operations are OK. In combination with PPP, you can use a mobile phone to access the Internet ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.