-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUMQTT.Types.pas
73 lines (57 loc) · 1.74 KB
/
UMQTT.Types.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
unit UMQTT.Types;
interface
type
TControlPacket = (cpNone = 0,
cpConnect = 1,
cpConnack = 2,
cpPublish = 3,
cpPuback = 4,
cpPubrec = 5,
cpPubrel = 6,
cpPubcomp = 7,
cpSubscribe = 8,
cpSuback = 9,
cpUnsubscribe = 10,
cpUnsuback = 11,
cpPingreq = 12,
cpPingresp = 13,
cpDisconnect = 14,
cpAuth = 15);
TMQTTFixedHeader = packed record
Byte1: Byte; // 4bits control packet type | 4bits for flags
Byte2: Byte; // Remaining length
procedure ControlPacketType(const pValue: TControlPacket);
end;
implementation
uses
System.SysUtils;
{$REGION 'TMQTTFixedHeader'}
procedure TMQTTFixedHeader.ControlPacketType(const pValue: TControlPacket);
begin
Byte1 := Integer(pValue) shl 4;
// 2.1.3 Flags
case pValue of
cpPublish: ;
cpUnsubscribe,
cpSubscribe,
cpPubrel: Byte1 := Byte1 + (1 shl 1);
end;
end;
{$ENDREGION}
procedure TestFixedHeader;
var
vHeader: TMQTTFixedHeader;
begin
vHeader.ControlPacketType(cpConnect);
if not vHeader.Byte1 = 16 then
raise Exception.Create('Invalid byte 1 value');
vHeader.ControlPacketType(cpConnack);
if not vHeader.Byte1 = 32 then
raise Exception.Create('Invalid byte 1 value');
vHeader.ControlPacketType(cpSubscribe);
if not vHeader.Byte1 = 130 then
raise Exception.Create('Invalid byte 1 value');
end;
initialization
TestFixedHeader;
end.