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
use heapless::Vec;
use rand_core::RngCore;
use crate::packet::v5::property::Property;
use crate::packet::v5::publish_packet::QualityOfService;
use crate::utils::types::{BinaryData, EncodedString};
#[derive(Clone, PartialEq)]
pub enum MqttVersion {
MQTTv3,
MQTTv5,
}
#[derive(Clone)]
pub struct ClientConfig<'a, const MAX_PROPERTIES: usize, T: RngCore> {
pub max_subscribe_qos: QualityOfService,
pub keep_alive: u16,
pub username_flag: bool,
pub username: EncodedString<'a>,
pub password_flag: bool,
pub password: BinaryData<'a>,
pub properties: Vec<Property<'a>, MAX_PROPERTIES>,
pub max_packet_size: u32,
pub mqtt_version: MqttVersion,
pub rng: T,
pub will_flag: bool,
pub will_topic: EncodedString<'a>,
pub will_payload: BinaryData<'a>,
pub will_retain: bool,
pub client_id: EncodedString<'a>,
}
impl<'a, const MAX_PROPERTIES: usize, T: RngCore> ClientConfig<'a, MAX_PROPERTIES, T> {
pub fn new(version: MqttVersion, rng: T) -> Self {
Self {
max_subscribe_qos: QualityOfService::QoS0,
keep_alive: 60,
username_flag: false,
username: EncodedString::new(),
password_flag: false,
password: BinaryData::new(),
properties: Vec::<Property<'a>, MAX_PROPERTIES>::new(),
max_packet_size: 265_000,
mqtt_version: version,
rng,
will_flag: false,
will_topic: EncodedString::new(),
will_payload: BinaryData::new(),
will_retain: false,
client_id: EncodedString::new(),
}
}
pub fn add_max_subscribe_qos(&mut self, qos: QualityOfService) {
self.max_subscribe_qos = qos;
}
pub fn add_will(&mut self, topic: &'a str, payload: &'a [u8], retain: bool) {
let mut topic_s = EncodedString::new();
topic_s.string = topic;
topic_s.len = topic.len() as u16;
let mut payload_d = BinaryData::new();
payload_d.bin = payload;
payload_d.len = payload.len() as u16;
self.will_flag = true;
self.will_retain = retain;
self.will_topic = topic_s;
self.will_payload = payload_d;
}
pub fn add_username(&mut self, username: &'a str) {
let mut username_s: EncodedString = EncodedString::new();
username_s.string = username;
username_s.len = username.len() as u16;
self.username_flag = true;
self.username = username_s;
}
pub fn add_password(&mut self, password: &'a str) {
let mut password_s: BinaryData = BinaryData::new();
password_s.bin = password.as_bytes();
password_s.len = password_s.bin.len() as u16;
self.password = password_s;
self.password_flag = true;
}
pub fn add_property(&mut self, prop: Property<'a>) {
if self.properties.len() < MAX_PROPERTIES {
self.properties.push(prop);
}
}
pub fn add_max_packet_size_as_prop(&mut self) -> u32 {
if self.properties.len() < MAX_PROPERTIES {
let prop = Property::MaximumPacketSize(self.max_packet_size);
self.properties.push(prop);
return 5;
}
0
}
pub fn add_client_id(&mut self, client_id: &'a str) {
let mut client_id_s = EncodedString::new();
client_id_s.string = client_id;
client_id_s.len = client_id.len() as u16;
self.client_id = client_id_s
}
}