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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
use super::{
PacketId,
field::{self, Field, FieldBuffer},
protocol,
qos::Qos,
};
pub struct Connect<'a> {
pub client_id: &'a str,
pub clean_session: bool,
pub username: Option<&'a str>,
pub password: Option<&'a [u8]>,
pub will_topic: Option<&'a str>,
pub will_payload: Option<&'a [u8]>,
pub will_retain: bool,
pub keepalive: Option<u16>,
}
pub fn connect<'a>(buffer: &mut FieldBuffer<'a>, connect: Connect<'a>) {
let mut flags = 0;
if connect.clean_session {
flags |= protocol::CONNECT_FLAG_CLEAN_SESSION;
}
if connect.username.is_some() {
flags |= protocol::CONNECT_FLAG_USERNAME;
}
if connect.password.is_some() {
flags |= protocol::CONNECT_FLAG_PASSWORD;
}
if connect.will_topic.is_some() {
flags |= protocol::CONNECT_FLAG_WILL_FLAG;
}
if connect.will_retain {
flags |= protocol::CONNECT_FLAG_WILL_RETAIN;
}
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_CONNECT,
0,
)));
buffer.push(Field::VarInt(0));
buffer.push(Field::LenPrefixedString(protocol::PROTOCOL_NAME));
buffer.push(Field::U8(protocol::PROTOCOL_LEVEL_3_1_1));
buffer.push(Field::U8(flags));
buffer.push(Field::U16(connect.keepalive.unwrap_or(0)));
buffer.push(Field::LenPrefixedString(connect.client_id));
if let Some(will_topic) = connect.will_topic {
buffer.push(Field::LenPrefixedString(will_topic));
buffer.push(Field::LenPrefixedBuffer(
connect.will_payload.unwrap_or(&[]),
));
}
if let Some(username) = connect.username {
buffer.push(Field::LenPrefixedString(username));
}
if let Some(password) = connect.password {
buffer.push(Field::LenPrefixedBuffer(password));
}
let message_size = field::fields_size(&buffer.as_slice()[2..]);
buffer.set(1, Field::VarInt(u32::try_from(message_size).unwrap()));
}
pub struct Publish<'a> {
pub topic: &'a str,
pub payload: &'a [u8],
pub qos: Qos,
pub retain: bool,
pub dup: bool,
pub packet_id: Option<PacketId>,
}
pub fn publish<'a>(buffer: &mut FieldBuffer<'a>, publish: Publish<'a>) {
let mut flags = 0u8;
// Set QoS bits (bits 1-2)
flags |= (publish.qos.to_u8() & 0x03) << 1;
// Set RETAIN flag (bit 0)
if publish.retain {
flags |= 0x01;
}
// Set DUP flag (bit 3)
if publish.dup {
flags |= 0x08;
}
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_PUBLISH,
flags,
)));
buffer.push(Field::VarInt(0));
buffer.push(Field::LenPrefixedString(publish.topic));
// Packet ID is only present for QoS 1 and 2
if publish.qos.to_u8() > 0 {
// TODO: turn this into a warning
let packet_id = publish.packet_id.expect("packet_id required for QoS > 0");
buffer.push(Field::U16(packet_id.into()));
}
buffer.push(Field::Buffer(publish.payload));
let message_size = field::fields_size(&buffer.as_slice()[2..]);
buffer.set(1, Field::VarInt(u32::try_from(message_size).unwrap()));
}
pub struct Subscribe<'a> {
pub topic: &'a str,
pub qos: Qos,
pub packet_id: PacketId,
}
pub fn subscribe<'a>(buffer: &mut FieldBuffer<'a>, subscribe: Subscribe<'a>) {
// SUBSCRIBE packets have fixed header flags (reserved bits)
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_SUBSCRIBE,
protocol::SUBSCRIBE_HEADER_FLAGS,
)));
buffer.push(Field::VarInt(0));
// Variable header: packet identifier
buffer.push(Field::U16(subscribe.packet_id.into()));
// Payload: topic filter + QoS
buffer.push(Field::LenPrefixedString(subscribe.topic));
buffer.push(Field::U8(subscribe.qos.to_u8()));
let message_size = field::fields_size(&buffer.as_slice()[2..]);
buffer.set(1, Field::VarInt(u32::try_from(message_size).unwrap()));
}
pub struct Unsubscribe<'a> {
pub topic: &'a str,
pub packet_id: PacketId,
}
pub fn unsubscribe<'a>(buffer: &mut FieldBuffer<'a>, unsubscribe: Unsubscribe<'a>) {
// UNSUBSCRIBE packets have fixed header flags (reserved bits)
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_UNSUBSCRIBE,
protocol::UNSUBSCRIBE_HEADER_FLAGS,
)));
buffer.push(Field::VarInt(0));
// Variable header: packet identifier
buffer.push(Field::U16(unsubscribe.packet_id.into()));
// Payload: topic filter (no QoS)
buffer.push(Field::LenPrefixedString(unsubscribe.topic));
let message_size = field::fields_size(&buffer.as_slice()[2..]);
buffer.set(1, Field::VarInt(u32::try_from(message_size).unwrap()));
}
pub fn disconnect(buffer: &mut FieldBuffer) {
// DISCONNECT has no variable header or payload
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_DISCONNECT,
0,
)));
buffer.push(Field::VarInt(0));
}
pub fn puback(buffer: &mut FieldBuffer, packet_id: PacketId) {
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_PUBACK,
0,
)));
buffer.push(Field::VarInt(2)); // Remaining length is always 2 (packet ID)
buffer.push(Field::U16(packet_id.into()));
}
pub fn pubrec(buffer: &mut FieldBuffer, packet_id: PacketId) {
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_PUBREC,
0,
)));
buffer.push(Field::VarInt(2)); // Remaining length is always 2 (packet ID)
buffer.push(Field::U16(packet_id.into()));
}
pub fn pubrel(buffer: &mut FieldBuffer, packet_id: PacketId) {
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_PUBREL,
protocol::PUBREL_HEADER_FLAGS,
)));
buffer.push(Field::VarInt(2)); // Remaining length is always 2 (packet ID)
buffer.push(Field::U16(packet_id.into()));
}
pub fn pubcomp(buffer: &mut FieldBuffer, packet_id: PacketId) {
buffer.push(Field::U8(protocol::create_header_control(
protocol::PACKET_TYPE_PUBCOMP,
0,
)));
buffer.push(Field::VarInt(2)); // Remaining length is always 2 (packet ID)
buffer.push(Field::U16(packet_id.into()));
}
|