Struct rubble::link::data::Header[][src]

pub struct Header(u16);
Expand description

16-bit data channel header preceding the payload.

Layout (in Bluetooth 4.2):

LSB                                                                MSB
+----------+---------+---------+---------+------------+--------------+
|   LLID   |  NESN   |   SN    |   MD    |     -      |    Length    |
| (2 bits) | (1 bit) | (1 bit) | (1 bit) |  (3 bits)  |   (8 bits)   |
+----------+---------+---------+---------+------------+--------------+

Payload format depends on the value of the 2-bit LLID field:

  • 0b00: Reserved value.
  • 0b01: LL Data PDU Continuation fragment or empty PDU.
  • 0b10: LL Data PDU Start of L2CAP message (or complete message if no fragmentation necessary).
  • 0b11: LL Control PDU.

The NESN field specifies the Next Expected Sequence Number. The SN field specifies the Sequence Number of this PDU.

The MD field specifies that the device sending the packet has more data to send during this connection event. When both slave and master send a packet with the MD bit set to 0, the connection event ends.

The Length field specifies the length of payload and MIC. Prior to Bluetooth 4.2, this was a 5-bit field, resulting in payloads + MICs of up to 31 Bytes. With Bluetooth 4.2, devices can communicate their buffer sizes and optionally transmit larger packets.

Sequence Numbers

The NESN and SN fields are used for retransmission and acknowledgement. The link layer stores two 1-bit parameters for an established connection, called transmitSeqNum and nextExpectedSeqNum. When a connection is established, both start out as 0. Both parameters are repeatedly incremented by 1 when data is transmitted, using wrapping arithmetic.

When a data channel packet is sent for the first time (ie. not retransmitted), the SN field is set to transmitSeqNum. When the packet is resent, the SN field is not modified. In both cases, the NESN bit is set to nextExpectedSeqNum.

The NESN bit tells the receiver whether its last packet has arrived: When a packet is received with an NESN value equal to the receiver’s transmitSeqNum, the receiver has already sent a packet with the expected SN, but the other side hasn’t received it yet. The receiver must resend the last data channel PDU. No other data channel PDU must be sent by it.

When the received packet’s NESN bit is different from transmitSeqNum, the last PDU has been acknowledged and the receiver should increment transmitSeqNum by 1.

Similarly, the SN bit is used to distinguish retransmitted and new packets: When a packet is received with an SN value equal to the receiver’s nextExpectedSeqNum value, the packet is new (not a retransmission), and nextExpectedSeqNum should be incremented by 1. If the value is not equal to nextExpectedSeqNum, this packet is a retransmission, so nextExpectedSeqNum should not be changed.

Tuple Fields

0: u16

Implementations

Creates a header with the given LLID field and all other fields set to 0 (including the payload length).

Parses a header from raw bytes.

Panics when raw contains less than 2 Bytes.

Returns the raw representation of the header.

The returned u16 must be transmitted LSB and LSb first as the first 2 octets of the PDU.

Returns the length of the payload in octets as specified in the Length field.

Sets the payload length field to len.

Note that BLE <4.2 is restricted to 5-bit payload lengths.

Returns the LLID field (PDU type).

Returns the value of the NESN field (Next Expected Sequence Number).

Sets the value of the NESN field.

Returns the value of the SN field (Sequence Number).

Sets the value of the SN field.

Returns whether the MD field is set (More Data).

Sets the value of the MD field.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Decode a Self from a byte slice, advancing bytes to point past the data that was read. Read more

Converts self to bytes and writes them into writer, advancing writer to point past the encoded value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.