Bluetooth L2CAP 学习

信道的概念

CID (Channel ID) 在L2CAP层的很重要。

其中有一些固定的CID,比如在ACL_U逻辑链路上,

  • 0x0001 代表Signaling channel
  • 0x0002 代表无连接的信道
  • 0x0040~0xFFFF 用于动态分配(主要用于connection-orientation 面向连接)

Bluetooth L2CAP 学习_第1张图片
Bluetooth L2CAP 学习_第2张图片

基本的信道类型

如下图所示,基本的信道主要是三种:

  • Connection-oriented
  • Connectionless Data
  • L2CAP Signaling
    其中面向连接的Connection-oriented是动态分配的CID,其他的两种面向连接和Signaling都有固定的信道。 其中LE_U逻辑链路的上的Signaling CID 是0x0005
    Bluetooth L2CAP 学习_第3张图片

信道的传输模式

  • Basic L2CAP Mode
  • Flow Control Mode
  • Retransmission Mode
  • Enhanced Retransmission Mode
  • Streaming Mode
  • LE Credit Based Flow Control Mode
  • Enhanced Credit Based Flow Control Mode

待补充

数据包格式(Package Format)

Connection-orientation (面向连接)

Basic Frame

Bluetooth L2CAP 学习_第4张图片
Length: 2 bytes
Channel ID : 2 bytes
Information payload: 65535 bytes (由长度的决定)

Connectionless (无连接)

Basic Frame

Bluetooth L2CAP 学习_第5张图片
Length: 2 bytes
CID: 2 bytes,固定是0x0002
PSM(Protocol/Service Multiplexer) : >= 2bytes 最小是两个字节 eg:SDP, Refcommon
Information payload: 65535 bytes

Retransmission / Flow Control / Sreaming modeBluetooth L2CAP 学习_第6张图片
Control (2~3 bytes)

The Control Field identifies whether the frame is an S-frame or I-frame and contains various information about the frame.

  • 1 Standard Control Field
    The Standard Control Field shall be used for Retransmission mode and Flow Control mode
  • 2 Enhanced Control Field
  • 3 Extended Control Field
    The Enhanced and Extended Control Fields shall be used for Enhanced Retransmission mode and Streaming mode.
S-Frame

用来控制数据帧是否重传。
S-frames are used to acknowledge I-frames and request retransmission of Iframes.

Each S-frame has an ReqSeq sequence number which may acknowledge additional I-frames received by the data Link Layer entity.

Each S-frame with a Standard Control Field has a retransmission bit (R bit) that affects whether I-frames are retransmitted.

Each S-frame with an Enhanced Control field or an Extended Control Field has a Poll bit (P-bit) and a Final bit (F-bit) and does not have an R-bit.

Defined types of S-frames are RR (Receiver Ready), REJ (Reject), RNR(Receiver Not Ready) and SREJ (Selective Reject).

Bluetooth L2CAP 学习_第7张图片

I-Frame

主要用来传输L2CAP的数据。
The I-frames are used to transfer information between L2CAP entities.

Each I-frame has a TxSeq(Send sequence number), ReqSeq(Receive sequence number) which can acknowledge additional I-frames received by the dataLink Layer entity.

Each I-frame with a Standard Control field has a retransmission bit (R bit) that affects whether I-frames are retransmitted.

Each I-frame with an Enhanced Control Field or an Extended Control Field has an F-bit that is used in Poll/Final bit functions.

//(SAR segmentation and reassembly)
The SAR field in the I-frame is used for segmentation and reassembly control. The L2CAP SDU Length field specifies the length of an SDU, including the aggregate length across all segments if segmented.

Bluetooth L2CAP 学习_第8张图片

是S-Frame还是I-Frame 由 Control 字段中最低位决定的。
Information and Supervisory frames types are distinguished by the least significant bit in the Control Field.

  • Type
    The type bit shall be 0 for an I-frame and 1 for an S-frame.

  • Send Sequence Number - TxSeq
    The send sequence number is used to number each I-frame, to enable sequencing and retransmission.

  • Receive Sequence Number - ReqSeq
    The receive sequence number is used by the receiver side to acknowledge I-frames, and in the REJ and SREJ frames to request the retransmission of an I-frame with a specific send sequence number.

  • Retransmission Disable Bit - R
    R=0: Normal operation. Sender uses the RetransmissionTimer to control retransmission of I-frames. Sender does not use the MonitorTimer.
    R=1: Receiver side requests sender to postpone retransmission of I-frames. Sender monitors signaling with the MonitorTimer. Sender does not use the RetransmissionTimer.

  • Segmentation and Reassembly - SAR
    The SAR bits define whether an L2CAP SDU is segmented。
    Bluetooth L2CAP 学习_第9张图片

  • Supervisory function - S
    The S-bits mark the type of S-frame
    Bluetooth L2CAP 学习_第10张图片

  • Poll - P
    The P-bit is set to 1 to solicit a response from the receiver. The receiver shall respond immediately with a frame with the F-bit set to 1.

  • Final - F
    The F-bit is set to 1 in response to an S-frame with the P bit set to 1.

Signaling 数据包 (C-Frame)

0x0001 information payload 可以包含多条指令。 0x0005只能包含一条指令
Multiple commands may be sent in a single C-frame over fixed channel CID 0x0001 while only one command per C-frame shall be sent over fixed channel CID 0x0005.

C-Frame不能超过MTU中定义的长度。如果超过,接受端会立即发L2CAP_COMMAND_REJECT_RSP包含支持的最大的长度。同时C-Frame 最小长度如下表。
Bluetooth L2CAP 学习_第11张图片
Note: The name of a signalling packet has a suffix indicating its type: _REQ for requests, _RSP for responses, and _IND for indications.

Bluetooth L2CAP 学习_第12张图片

Bluetooth L2CAP 学习_第13张图片

  • Code (1 byte)
    如果接受端收到未知或者不允许的Code,会立即发送L2CAP_COMMAND_-REJECT_RSP,其中包含拒绝码。参见下表:
    Bluetooth L2CAP 学习_第14张图片
  • Identifier (1 byte)
    确认码,在Request和Response之间保持统一, Response 会使用Request的identifier进行回复。
    The requesting device sets this field and the responding device uses the same value in its response.

连续的命令,需要使用不同的Identifier。
Within each signaling channel a different Identifier shall be used for each successive command.

同时0x00不可以使用,会被当做无效signaling抛弃。
Signaling identifier 0x00 is an illegal identifier and shall never be used in any command.

你可能感兴趣的:(Bluetooth,stack)