Practical Linux Bluetooth programming (4) L2CAP layer Programming

Source: Internet
Author: User

The author's blog address: http://blog.sina.com.cn/samzhen1977

By Sam (zhufeng) sam_code@hotmail.com

(L2CAP protocol introduction, L2CAP implementation in bluez and L2CAP Programming Interface)

 

I. Introduction to The L2CAP protocol:

Logical Link Control and adaptation
Protocol (L2CAP)

 

Logical Connection Control and Adaptation Protocol
(L2CAP)

Provides connection-oriented and connectionless data services for upper-layer protocols, and provides multi-protocol functions and segmentation and reorganization operations.
L2CAP
 
Maximum transfer and receipt length for upper-layer protocols and applications is
64 K

Of
L2CAP

Data packets.

  
L2CAP

Based on
Channel
(Channel)

.
Channel
(Channel)

Is located in the baseband
(Baseband)

The logical connection above the connection. Each channel is bound to a single protocol in multiple-to-one mode.
(Single
Protocol)

. Multiple channels can be bound to the same protocol, but one channel cannot be bound to multiple protocols.
Each
L2CAP

Data packets are transmitted to the corresponding upper-layer protocol.
Multiple channels can share the same baseband connection.

 

The position of L2CAP In the Bluetooth protocol stack is as follows:


That is to say, all L2CAP data is transmitted to the remote device through HCI. Most of the data in the upper-layer protocol is also transmitted through L2CAP.

 

 

L2CAP can send command. Such as connection and disconnection.


 

The following is an example of command: Connection Request:

 

In this case, we need to note that The L2CAP uses The L2CAP Connection Request (Connection Request
) Command. L2CAP can reuse connection requests sent to upper-layer protocols. These upper-layer protocols include the Service Discovery Protocol (SDP ).
0x0001), RFCOMM (SMS = 0x0003), and telephone control (SMS = 0x0005.

 

 

Protocol PSM Reference
SDP Zero X 0001
SeeBluetooth
Service Discovery Protocol (SDP), Bluetooth
Sig.
RFCOMM Zero X 0003
See RFCOMM with TS 07.10, Bluetooth Sig.
TCS-BIN Zero X 0005
SeeBluetooth
Telephony control specification/TCS
Binary, Bluetooth Sig.
TCS-BIN-CORDLESS Zero X 0007
SeeBluetooth
Telephony control specification/TCS
Binary, Bluetooth Sig.
Bnep 0x000f
SeeBluetooth
Network encapsulation protocal, Bluetooth
Sig.
Hid_control Zero X 0011
See Human Interface Device, Bluetooth Sig.
Hid_interrupt Zero X 0013
See Human Interface Device, Bluetooth Sig.
UPnP Zero X 0015
See [ESDP], Bluetooth Sig.
Avctp Zero X 0017
See audio/video control transport protocol, Bluetooth
Sig.
Avdtp Zero X 0019
See audio/video distribution transport protocol, Bluetooth
Sig.
Avctp_browsing 0x001b
See audio/video remote control profile, Bluetooth SIG
UDI_C-Plane 0x001d
See the unrestricted digital information profile [Udi], Bluetooth
SIG

 

 

 

 

Ii. L2CAP programming method:

 

L2CAP programming is very important. It and HCI are basically Linux
The basics of Bluetooth programming. Almost all protocols are connected, disconnected, and read/write using L2CAP connections.

 

1. Create L2CAP socket:

Socket (pf_bluetooth, sock_raw, btproto_l2cap );

Domain = pf_bluetooth, type can be of multiple types. Protocol = btproto_l2cap.

 

2. BIND:

// Bind to local address
Memset (& ADDR, 0,
Sizeof (ADDR ));
ADDR. l2_family = af_bluetooth;
Bacpy (& ADDR. l2_bdaddr,
& Bdaddr); // The bdaddr is a local dongle bdaddr.

If
(BIND (SK, (struct sockaddr *)
& ADDR, sizeof (ADDR) <0 ){
Perror ("can't bind
Socket ");
Goto error;
}

 

3. Connection

Memset (& ADDR, 0, sizeof (ADDR ));
ADDR. l2_family = af_bluetooth;
Bacpy (ADDR. l2_bdaddr, Src );

ADDR. l2_psm = xxx;

If (connect (SK, (struct sockaddr *)
& ADDR, sizeof (ADDR) <0 ){
Perror ("can't connect ");
Goto error;
}

 

Note:

Struct sockaddr_l2 {
Sa_family_t l2_family;
// The value must be af_bluetooth.
Unsigned
Short l2_psm;
// Corresponds to the above-mentioned MnS, which is very important.
Bdaddr_t l2_bdaddr; // remote
Device bdaddr
Unsigned
Short l2_cid;
};

 

4. send data to the remote device:

Both send () and write () can be used.

 

5. receive data:

Revc () or read ()

 

The following are examples:

Note: In Bluetooth, one end of the active connection serves as the host end. Passively wait for others to connect as the client.

 

 

 

 

Background 1: the status of a bluetooth device

In HCI programming, IOCTL (hcigetdevinfo) is used to obtain a device.
Info (hci_dev_info). Among them, flags explained very easily at the time. In fact, it stores a bluetooth device (for example, USB
Bluetooth dongle) Current status:

The "up" and "down" statuses indicate whether the device is started. You can use IOCTL (hcidevup) to modify these statuses.

In addition:Inquiry scan, page
Scan

These statuses:

When Sam started his own L2CAP layer connection, he used another Linux machine to plug in USB Bluetooth dongle for remote
Device. You cannot use inquiry to scan remote devices, connect to remote devices, or even use l2ping.
Ping the remote device. It was strange that the remote device status settings were not correct. Pscan and iscan are not set.

Inquiry scan status indicates that the device can be inquiry. Page
Scan status indicates that the device can be connected.

# Hciconfig hci0 iscan

# Hciconfig hci0 pscan

Or: # hciconfig hci0 piscan

You can set the status to pscan or iscan.

For programming, you can use IOCTL (hcisetscan). dev_opt =
Scan_inquiry; Dr. dev_opt = scan_page; Dr. dev_opt = scan_page |
Scan_inquiry;

Then it can be inquiry or connect.

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.