-
Notifications
You must be signed in to change notification settings - Fork 939
/
Copy pathmqtt_connect.cpp
407 lines (338 loc) · 8.28 KB
/
mqtt_connect.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/mqtt/mqtt_connect.hpp"
#endif
namespace acl {
enum {
MQTT_STAT_HDR_VAR,
MQTT_STAT_CID_LEN,
MQTT_STAT_CID_VAL,
MQTT_STAT_USERNAME_LEN,
MQTT_STAT_USERNAME_VAL,
MQTT_STAT_PASSWD_LEN,
MQTT_STAT_PASSWD_VAL,
MQTT_STAT_WILL_TOPIC_LEN,
MQTT_STAT_WILL_TOPIC_VAL,
MQTT_STAT_WILL_MSG_LEN,
MQTT_STAT_WILL_MSG_VAL,
};
mqtt_connect::mqtt_connect()
: mqtt_message(MQTT_CONNECT)
, finished_(false)
, dlen_(0)
, will_qos_(MQTT_QOS0)
, conn_flags_(0)
, keep_alive_(300)
{
status_ = MQTT_STAT_HDR_VAR; // just for update()
}
mqtt_connect::mqtt_connect(const mqtt_header& header)
: mqtt_message(header)
, finished_(false)
, dlen_(0)
, will_qos_(MQTT_QOS0)
, conn_flags_(0)
, keep_alive_(300)
{
status_ = MQTT_STAT_HDR_VAR; // just for update()
}
mqtt_connect::~mqtt_connect() {}
void mqtt_connect::set_keep_alive(unsigned short keep_alive) {
keep_alive_ = keep_alive;
}
void mqtt_connect::set_cid(const char* cid) {
if (cid && *cid) {
cid_ = cid;
}
}
void mqtt_connect::set_username(const char* name) {
if (name && *name) {
username_ = name;
}
}
void mqtt_connect::set_passwd(const char* passwd) {
if (passwd && *passwd) {
passwd_ = passwd;
}
}
void mqtt_connect::set_will_qos(mqtt_qos_t qos) {
will_qos_ = qos;
switch (qos) {
case MQTT_QOS0:
conn_flags_ &= ~(1 << 4 | 1 << 3);
break;
case MQTT_QOS1:
conn_flags_ &= ~(1 << 4);
conn_flags_ |= 1 << 3;
break;
case MQTT_QOS2:
conn_flags_ &= ~(1 << 3);
conn_flags_ |= 1 << 4;
break;
default:
logger_error("invalid qos: %d", (int) qos);
}
}
void mqtt_connect::set_will_topic(const char* topic) {
if (topic && *topic) {
will_topic_ = topic;
}
}
void mqtt_connect::set_will_msg(const char* msg) {
if (msg && *msg) {
will_msg_ = msg;
}
}
void mqtt_connect::clean_session() {
conn_flags_ |= 0x02;
}
bool mqtt_connect::has_session() const {
return conn_flags_ & 0x02 ? true : false;
}
bool mqtt_connect::to_string(string& out) {
unsigned len = 10; // length of the body's header
len += 2;
len += (unsigned) cid_.size();
if (!username_.empty()) {
conn_flags_ |= 1 << 7;
len += 2;
len += (unsigned) username_.size();
}
if (!passwd_.empty()) {
conn_flags_ |= 1 << 6;
len += 2;
len += (unsigned) passwd_.size();
}
if (!will_topic_.empty() && !will_msg_.empty()) {
conn_flags_ |= 1 << 2;
len += 2;
len += (unsigned) will_topic_.size();
len += 2;
len += (unsigned) will_msg_.size();
} else {
will_topic_.clear();
will_msg_.clear();
}
mqtt_header& header = this->get_header();
header.set_remaing_length(len);
if (!header.build_header(out)) {
return false;
}
this->pack_add((unsigned char) 0, out);
this->pack_add((unsigned char) 0x04, out);
this->pack_add((unsigned char) 'M', out);
this->pack_add((unsigned char) 'Q', out);
this->pack_add((unsigned char) 'T', out);
this->pack_add((unsigned char) 'T', out);
this->pack_add((unsigned char) 0x04, out);
this->pack_add((unsigned char) conn_flags_, out);
this->pack_add((unsigned short) keep_alive_, out);
this->pack_add(cid_, out);
if (!will_topic_.empty() && !will_msg_.empty()) {
this->pack_add(will_topic_, out);
this->pack_add(will_msg_, out);
}
if (!username_.empty()) {
this->pack_add(username_, out);
}
if (!passwd_.empty()) {
this->pack_add(passwd_, out);
}
return true;
}
static struct {
int status;
int (mqtt_connect::*handler)(const char*, int);
} handlers[] = {
{ MQTT_STAT_HDR_VAR, &mqtt_connect::update_header_var },
{ MQTT_STAT_CID_LEN, &mqtt_connect::update_cid_len },
{ MQTT_STAT_CID_VAL, &mqtt_connect::update_cid_val },
{ MQTT_STAT_USERNAME_LEN, &mqtt_connect::update_username_len },
{ MQTT_STAT_USERNAME_VAL, &mqtt_connect::update_username_val },
{ MQTT_STAT_PASSWD_LEN, &mqtt_connect::update_passwd_len },
{ MQTT_STAT_PASSWD_VAL, &mqtt_connect::update_passwd_val },
{ MQTT_STAT_WILL_TOPIC_LEN, &mqtt_connect::update_will_topic_len},
{ MQTT_STAT_WILL_TOPIC_VAL, &mqtt_connect::update_will_topic_val},
{ MQTT_STAT_WILL_MSG_LEN, &mqtt_connect::update_will_msg_len },
{ MQTT_STAT_WILL_MSG_VAL, &mqtt_connect::update_will_msg_val },
};
int mqtt_connect::update(const char* data, int dlen) {
if (data == NULL || dlen <= 0) {
logger_error("invalid input");
return -1;
}
while (dlen > 0 && !finished_) {
int ret = (this->*handlers[status_].handler)(data, dlen);
if (ret < 0) {
return -1;
}
data += dlen - ret;
dlen = ret;
}
return dlen;
}
#define HDR_VAR_LEN 10
int mqtt_connect::update_header_var(const char* data, int dlen) {
assert(data && dlen > 0);
assert(sizeof(buff_) >= HDR_VAR_LEN);
if (dlen_ >= HDR_VAR_LEN) {
logger_error("invalid header var");
return -1;
}
for (; dlen_ < HDR_VAR_LEN && dlen > 0;) {
buff_[dlen_++] = *data++;
dlen--;
}
if (dlen_ < HDR_VAR_LEN) {
assert(dlen == 0);
return dlen;
}
char label[5];
label[0] = buff_[2];
label[1] = buff_[3];
label[2] = buff_[4];
label[3] = buff_[5];
label[4] = 0;
if (strcasecmp(label, "MQTT") != 0) {
logger_warn("invalid label: %s", label);
}
conn_flags_ = buff_[7];
if (!this->unpack_short(&buff_[9], 2, keep_alive_)) {
logger_error("unpack keep_alive error");
return -1;
}
dlen_ = 0;
status_ = MQTT_STAT_CID_LEN;
return dlen;
}
#define HDR_LEN_LEN 2
#define SAVE_LENGTH(x) do { \
assert(data && dlen > 0); \
unsigned short n; \
for (; x < HDR_LEN_LEN && dlen > 0;) { \
buff_[x++] = *data++; \
dlen--; \
} \
if (x < HDR_LEN_LEN) { \
assert(dlen == 0); \
return dlen; \
} \
if (!this->unpack_short(&buff_[0], 2, n)) { \
logger_error("unpack cid len error"); \
return -1; \
} \
x = n; \
} while (0)
int mqtt_connect::update_cid_len(const char* data, int dlen) {
SAVE_LENGTH(dlen_);
if (dlen_ > 0) {
status_ = MQTT_STAT_CID_VAL;
} else if ((conn_flags_ & 0x04)) {
status_ = MQTT_STAT_WILL_TOPIC_LEN;
} else if ((conn_flags_ & 0x80)) {
status_ = MQTT_STAT_USERNAME_LEN;
} else if ((conn_flags_ & 0x40)) {
status_ = MQTT_STAT_PASSWD_LEN;
} else {
finished_ = true;
}
return dlen;
}
#define SAVE_DATA(buff) do { \
if (dlen_ <= 0) { \
logger_error("invalid dlen_=%d", dlen_); \
return -1; \
} \
assert(data && dlen > 0); \
for (; dlen_ > 0 && dlen > 0;) { \
buff += *data++; \
--dlen_; \
--dlen; \
} \
if (dlen_ > 0) { \
assert(dlen == 0); \
return dlen; \
} \
} while (0)
int mqtt_connect::update_cid_val(const char* data, int dlen) {
SAVE_DATA(cid_);
if ((conn_flags_ & 0x04)) {
status_ = MQTT_STAT_WILL_TOPIC_LEN;
} else if ((conn_flags_ & 0x80)) {
status_ = MQTT_STAT_USERNAME_LEN;
} else if ((conn_flags_ & 0x40)) {
status_ = MQTT_STAT_PASSWD_LEN;
} else {
finished_ = true;
}
return dlen;
}
int mqtt_connect::update_will_topic_len(const char* data, int dlen) {
SAVE_LENGTH(dlen_);
status_ = MQTT_STAT_WILL_TOPIC_VAL;
return dlen;
}
int mqtt_connect::update_will_topic_val(const char* data, int dlen) {
SAVE_DATA(will_topic_);
status_ = MQTT_STAT_WILL_MSG_LEN;
return dlen;
}
int mqtt_connect::update_will_msg_len(const char* data, int dlen) {
SAVE_LENGTH(dlen_);
if (dlen_ > 0) {
status_ = MQTT_STAT_WILL_MSG_VAL;
} else if ((conn_flags_ & 0x80)) {
status_ = MQTT_STAT_USERNAME_LEN;
} else if ((conn_flags_ & 0x40)) {
status_ = MQTT_STAT_PASSWD_LEN;
} else {
finished_ = true;
}
return dlen;
}
int mqtt_connect::update_will_msg_val(const char* data, int dlen) {
SAVE_DATA(will_msg_);
if ((conn_flags_ & 0x80)) {
status_ = MQTT_STAT_USERNAME_LEN;
} else if ((conn_flags_ & 0x40)) {
status_ = MQTT_STAT_PASSWD_LEN;
} else {
finished_ = true;
}
return dlen;
}
int mqtt_connect::update_username_len(const char* data, int dlen) {
SAVE_LENGTH(dlen_);
if (dlen_ > 0) {
status_ = MQTT_STAT_USERNAME_VAL;
} else if ((conn_flags_ & 0x40)) {
status_ = MQTT_STAT_PASSWD_LEN;
} else {
finished_ = true;
}
return dlen;
}
int mqtt_connect::update_username_val(const char* data, int dlen) {
SAVE_DATA(username_);
if ((conn_flags_ & 0x40)) {
status_ = MQTT_STAT_PASSWD_LEN;
} else {
finished_ = true;
}
return dlen;
}
int mqtt_connect::update_passwd_len(const char* data, int dlen) {
SAVE_LENGTH(dlen_);
if (dlen_ > 0) {
status_ = MQTT_STAT_PASSWD_VAL;
} else {
finished_ = true;
}
return dlen;
}
int mqtt_connect::update_passwd_val(const char* data, int dlen) {
SAVE_DATA(passwd_);
finished_ = true;
return dlen;
}
} // namespace acl