Prosody data communications Encodings: HDLC data delivery

As described in Prosody data communications Encodings: HDLC, data is delivered in a sequence of 32-bit words, followed by a marker word which also provides a status word called S1. S1 contains any remaining data bits. The table below shows how the last bits of a frame are delivered. Bits shown as d are data bits from the frame and other bits are added by the receiver. If a frame has less than 26 data bits, no data words will have been delivered before the end-of-frame marker, so there will be no 'last data word', and all the data bits appear in S1. If you are only interested in frames which contain whole octets with no extra bits left over, you can check frames like this:

		// assume 'bp' points to space for next octet
	if (S1 != 0x3e) {	// one, two, or three octets to go
		*bp++ = S1 & 0xff;
		S1 >>= 8;
		if (S1 != 0x3e) {	// one or two octets to go
			*bp++ = S1 & 0xff;
			S1 >>= 8;
			if (S1 != 0x3e) {	// one octet to go
				*bp++ = S1 & 0xff;
				S1 >>= 8;
			}
		}
	}
	if (S1 != 0x3e) {	// shouldn't be any left now
		// error - not an integral number of octets
	} else {
		// handle frame
	}

Note that the CRC is delivered with the data as normal data bits, so you may need to trim it off before processing the received frame.

Frame length (bits) Frame length (octets) S1 last data word
32x + 26 - 00000000 00000000 00000000 00000000 111110dd dddddddd dddddddd dddddddd
32x + 27 - 00000000 00000000 00000000 00000001 11110ddd dddddddd dddddddd dddddddd
32x + 28 - 00000000 00000000 00000000 00000011 1110dddd dddddddd dddddddd dddddddd
32x + 29 - 00000000 00000000 00000000 00000111 110ddddd dddddddd dddddddd dddddddd
32x + 30 - 00000000 00000000 00000000 00001111 10dddddd dddddddd dddddddd dddddddd
32x + 31 - 00000000 00000000 00000000 00011111 0ddddddd dddddddd dddddddd dddddddd
32x + 0 4x+0 00000000 00000000 00000000 00111110 dddddddd dddddddd dddddddd dddddddd
32x + 1 - 00000000 00000000 00000000 0111110d dddddddd dddddddd dddddddd dddddddd
32x + 2 - 00000000 00000000 00000000 111110dd dddddddd dddddddd dddddddd dddddddd
32x + 3 - 00000000 00000000 00000001 11110ddd dddddddd dddddddd dddddddd dddddddd
32x + 4 - 00000000 00000000 00000011 1110dddd dddddddd dddddddd dddddddd dddddddd
32x + 5 - 00000000 00000000 00000111 110ddddd dddddddd dddddddd dddddddd dddddddd
32x + 6 - 00000000 00000000 00001111 10dddddd dddddddd dddddddd dddddddd dddddddd
32x + 7 - 00000000 00000000 00011111 0ddddddd dddddddd dddddddd dddddddd dddddddd
32x + 8 4x+1 00000000 00000000 00111110 dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 9 - 00000000 00000000 0111110d dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 10 - 00000000 00000000 111110dd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 11 - 00000000 00000001 11110ddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 12 - 00000000 00000011 1110dddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 13 - 00000000 00000111 110ddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 14 - 00000000 00001111 10dddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 15 - 00000000 00011111 0ddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 16 4x+2 00000000 00111110 dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 17 - 00000000 0111110d dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 18 - 00000000 111110dd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 19 - 00000001 11110ddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 20 - 00000011 1110dddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 21 - 00000111 110ddddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 22 - 00001111 10dddddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 23 - 00011111 0ddddddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 24 4x+3 00111110 dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd
32x + 25 - 0111110d dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd dddddddd