-
Notifications
You must be signed in to change notification settings - Fork 158
/
mqttclient.c
739 lines (644 loc) · 23.4 KB
/
mqttclient.c
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/* mqttclient.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfMQTT.
*
* wolfMQTT is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfMQTT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Include the autoconf generated config.h */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "mqttclient.h"
#include "examples/mqttnet.h"
/* Locals */
static int mStopRead = 0;
/* Configuration */
/* Maximum size for network read/write callbacks. There is also a v5 define that
describes the max MQTT control packet size, DEFAULT_MAX_PKT_SZ. */
#ifndef MAX_BUFFER_SIZE
#define MAX_BUFFER_SIZE 1024
#endif
#ifdef WOLFMQTT_PROPERTY_CB
#define MAX_CLIENT_ID_LEN 64
char gClientId[MAX_CLIENT_ID_LEN] = {0};
#endif
#ifdef WOLFMQTT_DISCONNECT_CB
/* callback indicates a network error occurred */
static int mqtt_disconnect_cb(MqttClient* client, int error_code, void* ctx)
{
(void)client;
(void)ctx;
PRINTF("Network Error Callback: %s (error %d)",
MqttClient_ReturnCodeToString(error_code), error_code);
return 0;
}
#endif
static int mqtt_message_cb(MqttClient *client, MqttMessage *msg,
byte msg_new, byte msg_done)
{
byte buf[PRINT_BUFFER_SIZE+1];
word32 len;
MQTTCtx* mqttCtx = (MQTTCtx*)client->ctx;
(void)mqttCtx;
if (msg_new) {
/* Determine min size to dump */
len = msg->topic_name_len;
if (len > PRINT_BUFFER_SIZE) {
len = PRINT_BUFFER_SIZE;
}
XMEMCPY(buf, msg->topic_name, len);
buf[len] = '\0'; /* Make sure its null terminated */
/* Print incoming message */
PRINTF("MQTT Message: Topic %s, Qos %d, Len %u",
buf, msg->qos, msg->total_len);
/* for test mode: check if DEFAULT_MESSAGE was received */
if (mqttCtx->test_mode) {
if (XSTRLEN(DEFAULT_MESSAGE) == msg->buffer_len &&
XSTRNCMP(DEFAULT_MESSAGE, (char*)msg->buffer,
msg->buffer_len) == 0)
{
mStopRead = 1;
}
}
}
/* Print message payload */
len = msg->buffer_len;
if (len > PRINT_BUFFER_SIZE) {
len = PRINT_BUFFER_SIZE;
}
XMEMCPY(buf, msg->buffer, len);
buf[len] = '\0'; /* Make sure its null terminated */
PRINTF("Payload (%d - %d) printing %d bytes:" LINE_END "%s",
msg->buffer_pos, msg->buffer_pos + msg->buffer_len, len, buf);
#ifdef WOLFMQTT_V5
{
/* Properties can be checked in the message callback */
MqttProp *prop = msg->props;
while (prop != NULL)
{
if (prop->type == MQTT_PROP_CONTENT_TYPE) {
PRINTF("Content type: %.*s", prop->data_str.len,
prop->data_str.str);
}
prop = prop->next;
}
}
#endif
if (msg_done) {
PRINTF("MQTT Message: Done");
}
/* Return negative to terminate publish processing */
return MQTT_CODE_SUCCESS;
}
#ifdef WOLFMQTT_PROPERTY_CB
/* The property callback is called after decoding a packet that contains at
least one property. The property list is deallocated after returning from
the callback. */
static int mqtt_property_cb(MqttClient *client, MqttProp *head, void *ctx)
{
MqttProp *prop = head;
int rc = 0;
MQTTCtx* mqttCtx;
if ((client == NULL) || (client->ctx == NULL)) {
return MQTT_CODE_ERROR_BAD_ARG;
}
mqttCtx = (MQTTCtx*)client->ctx;
while (prop != NULL) {
PRINTF("Property CB: Type %d", prop->type);
switch (prop->type) {
case MQTT_PROP_ASSIGNED_CLIENT_ID:
/* Store client ID in global */
mqttCtx->client_id = &gClientId[0];
/* Store assigned client ID from CONNACK*/
XSTRNCPY((char*)mqttCtx->client_id, prop->data_str.str,
MAX_CLIENT_ID_LEN - 1);
/* should use strlcpy() semantics, but non-portable */
((char*)mqttCtx->client_id)[MAX_CLIENT_ID_LEN - 1] = '\0';
break;
case MQTT_PROP_SUBSCRIPTION_ID_AVAIL:
mqttCtx->subId_not_avail =
prop->data_byte == 0;
break;
case MQTT_PROP_TOPIC_ALIAS_MAX:
mqttCtx->topic_alias_max =
(mqttCtx->topic_alias_max < prop->data_short) ?
mqttCtx->topic_alias_max : prop->data_short;
break;
case MQTT_PROP_MAX_PACKET_SZ:
if ((prop->data_int > 0) &&
(prop->data_int <= MQTT_PACKET_SZ_MAX))
{
client->packet_sz_max =
(client->packet_sz_max < prop->data_int) ?
client->packet_sz_max : prop->data_int;
}
else {
/* Protocol error */
rc = MQTT_CODE_ERROR_PROPERTY;
}
break;
case MQTT_PROP_SERVER_KEEP_ALIVE:
mqttCtx->keep_alive_sec = prop->data_short;
break;
case MQTT_PROP_MAX_QOS:
client->max_qos = prop->data_byte;
break;
case MQTT_PROP_RETAIN_AVAIL:
client->retain_avail = prop->data_byte;
break;
case MQTT_PROP_REASON_STR:
PRINTF("Reason String: %.*s",
prop->data_str.len, prop->data_str.str);
break;
case MQTT_PROP_USER_PROP:
PRINTF("User property: key=\"%.*s\", value=\"%.*s\"",
prop->data_str.len, prop->data_str.str,
prop->data_str2.len, prop->data_str2.str);
break;
case MQTT_PROP_PAYLOAD_FORMAT_IND:
case MQTT_PROP_MSG_EXPIRY_INTERVAL:
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESP_TOPIC:
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_SUBSCRIPTION_ID:
case MQTT_PROP_SESSION_EXPIRY_INTERVAL:
case MQTT_PROP_TOPIC_ALIAS:
case MQTT_PROP_TYPE_MAX:
case MQTT_PROP_RECEIVE_MAX:
case MQTT_PROP_WILDCARD_SUB_AVAIL:
case MQTT_PROP_SHARED_SUBSCRIPTION_AVAIL:
case MQTT_PROP_RESP_INFO:
case MQTT_PROP_SERVER_REF:
case MQTT_PROP_AUTH_METHOD:
case MQTT_PROP_AUTH_DATA:
case MQTT_PROP_NONE:
break;
case MQTT_PROP_REQ_PROB_INFO:
case MQTT_PROP_WILL_DELAY_INTERVAL:
case MQTT_PROP_REQ_RESP_INFO:
default:
/* Invalid */
rc = MQTT_CODE_ERROR_PROPERTY;
break;
}
prop = prop->next;
}
(void)ctx;
return rc;
}
#endif /* WOLFMQTT_PROPERTY_CB */
int mqttclient_test(MQTTCtx *mqttCtx)
{
int rc = MQTT_CODE_SUCCESS, i;
PRINTF("MQTT Client: QoS %d, Use TLS %d", mqttCtx->qos,
mqttCtx->use_tls);
/* Initialize Network */
rc = MqttClientNet_Init(&mqttCtx->net, mqttCtx);
PRINTF("MQTT Net Init: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
/* setup tx/rx buffers */
mqttCtx->tx_buf = (byte*)WOLFMQTT_MALLOC(MAX_BUFFER_SIZE);
mqttCtx->rx_buf = (byte*)WOLFMQTT_MALLOC(MAX_BUFFER_SIZE);
/* Initialize MqttClient structure */
rc = MqttClient_Init(&mqttCtx->client, &mqttCtx->net,
mqtt_message_cb,
mqttCtx->tx_buf, MAX_BUFFER_SIZE,
mqttCtx->rx_buf, MAX_BUFFER_SIZE,
mqttCtx->cmd_timeout_ms);
PRINTF("MQTT Init: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
/* The client.ctx will be stored in the cert callback ctx during
MqttSocket_Connect for use by mqtt_tls_verify_cb */
mqttCtx->client.ctx = mqttCtx;
#ifdef WOLFMQTT_DISCONNECT_CB
/* setup disconnect callback */
rc = MqttClient_SetDisconnectCallback(&mqttCtx->client,
mqtt_disconnect_cb, NULL);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
#endif
#ifdef WOLFMQTT_PROPERTY_CB
rc = MqttClient_SetPropertyCallback(&mqttCtx->client,
mqtt_property_cb, NULL);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
#endif
/* Connect to broker */
rc = MqttClient_NetConnect(&mqttCtx->client, mqttCtx->host,
mqttCtx->port,
DEFAULT_CON_TIMEOUT_MS, mqttCtx->use_tls, mqtt_tls_cb);
PRINTF("MQTT Socket Connect: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
/* Build connect packet */
XMEMSET(&mqttCtx->connect, 0, sizeof(MqttConnect));
mqttCtx->connect.keep_alive_sec = mqttCtx->keep_alive_sec;
mqttCtx->connect.clean_session = mqttCtx->clean_session;
mqttCtx->connect.client_id = mqttCtx->client_id;
/* Last will and testament sent by broker to subscribers
of topic when broker connection is lost */
XMEMSET(&mqttCtx->lwt_msg, 0, sizeof(mqttCtx->lwt_msg));
mqttCtx->connect.lwt_msg = &mqttCtx->lwt_msg;
mqttCtx->connect.enable_lwt = mqttCtx->enable_lwt;
if (mqttCtx->enable_lwt) {
/* Send client id in LWT payload */
mqttCtx->lwt_msg.qos = mqttCtx->qos;
mqttCtx->lwt_msg.retain = 0;
mqttCtx->lwt_msg.topic_name = WOLFMQTT_TOPIC_NAME"lwttopic";
mqttCtx->lwt_msg.buffer = (byte*)mqttCtx->client_id;
mqttCtx->lwt_msg.total_len = (word16)XSTRLEN(mqttCtx->client_id);
#ifdef WOLFMQTT_V5
{
/* Add a 5 second delay to sending the LWT */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->lwt_msg.props);
prop->type = MQTT_PROP_WILL_DELAY_INTERVAL;
prop->data_int = 5;
}
#endif
}
/* Optional authentication */
mqttCtx->connect.username = mqttCtx->username;
mqttCtx->connect.password = mqttCtx->password;
#ifdef WOLFMQTT_V5
mqttCtx->client.packet_sz_max = mqttCtx->max_packet_size;
mqttCtx->client.enable_eauth = mqttCtx->enable_eauth;
if (mqttCtx->client.enable_eauth == 1) {
/* Enhanced authentication */
/* Add property: Authentication Method */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props);
prop->type = MQTT_PROP_AUTH_METHOD;
prop->data_str.str = (char*)DEFAULT_AUTH_METHOD;
prop->data_str.len = (word16)XSTRLEN(prop->data_str.str);
}
{
/* Request Response Information */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props);
prop->type = MQTT_PROP_REQ_RESP_INFO;
prop->data_byte = 1;
}
{
/* Request Problem Information */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props);
prop->type = MQTT_PROP_REQ_PROB_INFO;
prop->data_byte = 1;
}
{
/* Maximum Packet Size */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props);
prop->type = MQTT_PROP_MAX_PACKET_SZ;
prop->data_int = (word32)mqttCtx->max_packet_size;
}
{
/* Topic Alias Maximum */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props);
prop->type = MQTT_PROP_TOPIC_ALIAS_MAX;
prop->data_short = mqttCtx->topic_alias_max;
}
if (mqttCtx->clean_session == 0) {
/* Session expiry interval */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->connect.props);
prop->type = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
prop->data_int = DEFAULT_SESS_EXP_INT; /* Session does not expire */
}
#endif
/* Send Connect and wait for Connect Ack */
rc = MqttClient_Connect(&mqttCtx->client, &mqttCtx->connect);
PRINTF("MQTT Connect: Proto (%s), %s (%d)",
MqttClient_GetProtocolVersionString(&mqttCtx->client),
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto disconn;
}
#ifdef WOLFMQTT_V5
if (mqttCtx->connect.props != NULL) {
/* Release the allocated properties */
MqttClient_PropsFree(mqttCtx->connect.props);
}
if (mqttCtx->lwt_msg.props != NULL) {
/* Release the allocated properties */
MqttClient_PropsFree(mqttCtx->lwt_msg.props);
}
#endif
/* Validate Connect Ack info */
PRINTF("MQTT Connect Ack: Return Code %u, Session Present %d",
mqttCtx->connect.ack.return_code,
(mqttCtx->connect.ack.flags &
MQTT_CONNECT_ACK_FLAG_SESSION_PRESENT) ?
1 : 0
);
#ifdef WOLFMQTT_PROPERTY_CB
/* Print the acquired client ID */
PRINTF("MQTT Connect Ack: Assigned Client ID: %s",
mqttCtx->client_id);
#endif
/* Build list of topics */
XMEMSET(&mqttCtx->subscribe, 0, sizeof(MqttSubscribe));
i = 0;
mqttCtx->topics[i].topic_filter = mqttCtx->topic_name;
mqttCtx->topics[i].qos = mqttCtx->qos;
#ifdef WOLFMQTT_V5
if (mqttCtx->subId_not_avail != 1) {
/* Subscription Identifier */
MqttProp* prop;
prop = MqttClient_PropsAdd(&mqttCtx->subscribe.props);
prop->type = MQTT_PROP_SUBSCRIPTION_ID;
prop->data_int = DEFAULT_SUB_ID;
}
#endif
/* Subscribe Topic */
mqttCtx->subscribe.packet_id = mqtt_get_packetid();
mqttCtx->subscribe.topic_count =
sizeof(mqttCtx->topics) / sizeof(MqttTopic);
mqttCtx->subscribe.topics = mqttCtx->topics;
rc = MqttClient_Subscribe(&mqttCtx->client, &mqttCtx->subscribe);
#ifdef WOLFMQTT_V5
if (mqttCtx->subscribe.props != NULL) {
/* Release the allocated properties */
MqttClient_PropsFree(mqttCtx->subscribe.props);
}
#endif
PRINTF("MQTT Subscribe: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto disconn;
}
/* show subscribe results */
for (i = 0; i < mqttCtx->subscribe.topic_count; i++) {
MqttTopic *topic = &mqttCtx->subscribe.topics[i];
PRINTF(" Topic %s, Qos %u, Return Code %u",
topic->topic_filter,
topic->qos, topic->return_code);
}
/* Publish Topic */
XMEMSET(&mqttCtx->publish, 0, sizeof(MqttPublish));
mqttCtx->publish.retain = 0;
mqttCtx->publish.qos = mqttCtx->qos;
mqttCtx->publish.duplicate = 0;
mqttCtx->publish.topic_name = mqttCtx->topic_name;
mqttCtx->publish.packet_id = mqtt_get_packetid();
if (mqttCtx->pub_file) {
/* If a file is specified, then read into the allocated buffer */
rc = mqtt_file_load(mqttCtx->pub_file, &mqttCtx->publish.buffer,
(int*)&mqttCtx->publish.total_len);
if (rc != MQTT_CODE_SUCCESS) {
/* There was an error loading the file */
PRINTF("MQTT Publish file error: %d", rc);
}
}
else {
mqttCtx->publish.buffer = (byte*)mqttCtx->message;
mqttCtx->publish.total_len = (word16)XSTRLEN(mqttCtx->message);
}
if (rc == MQTT_CODE_SUCCESS) {
#ifdef WOLFMQTT_V5
{
/* Payload Format Indicator */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->publish.props);
prop->type = MQTT_PROP_PAYLOAD_FORMAT_IND;
prop->data_byte = 1;
}
{
/* Content Type */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->publish.props);
prop->type = MQTT_PROP_CONTENT_TYPE;
prop->data_str.str = (char*)"wolf_type";
prop->data_str.len = (word16)XSTRLEN(prop->data_str.str);
}
if ((mqttCtx->topic_alias_max > 0) &&
(mqttCtx->topic_alias > 0) &&
(mqttCtx->topic_alias < mqttCtx->topic_alias_max)) {
/* Topic Alias */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->publish.props);
prop->type = MQTT_PROP_TOPIC_ALIAS;
prop->data_short = mqttCtx->topic_alias;
}
#endif
/* This loop allows payloads larger than the buffer to be sent by
* repeatedly calling publish. */
do {
rc = MqttClient_Publish(&mqttCtx->client, &mqttCtx->publish);
} while (rc == MQTT_CODE_PUB_CONTINUE || rc == MQTT_CODE_CONTINUE);
if ((mqttCtx->pub_file) && (mqttCtx->publish.buffer)) {
WOLFMQTT_FREE(mqttCtx->publish.buffer);
mqttCtx->publish.buffer = NULL;
mqttCtx->pub_file = NULL; /* don't try and send file again */
}
PRINTF("MQTT Publish: Topic %s, ID %d, %s (%d)",
mqttCtx->publish.topic_name, mqttCtx->publish.packet_id,
MqttClient_ReturnCodeToString(rc), rc);
#ifdef WOLFMQTT_V5
if (mqttCtx->qos > 0) {
PRINTF("\tResponse Reason Code %d", mqttCtx->publish.resp.reason_code);
}
#endif
if (rc != MQTT_CODE_SUCCESS) {
goto disconn;
}
#ifdef WOLFMQTT_V5
if (mqttCtx->publish.props != NULL) {
/* Release the allocated properties */
MqttClient_PropsFree(mqttCtx->publish.props);
}
#endif
}
/* Read Loop */
PRINTF("MQTT Waiting for message...");
do {
/* check for test mode */
if (mqttCtx->test_mode) {
PRINTF("MQTT Test mode, exit now");
break;
}
/* Try and read packet */
rc = MqttClient_WaitMessage(&mqttCtx->client,
mqttCtx->cmd_timeout_ms);
#ifdef WOLFMQTT_NONBLOCK
/* Track elapsed time with no activity and trigger timeout */
rc = mqtt_check_timeout(rc, &mqttCtx->start_sec,
mqttCtx->cmd_timeout_ms/1000);
#endif
if (mStopRead) {
rc = MQTT_CODE_SUCCESS;
PRINTF("MQTT Exiting...");
break;
}
/* check return code */
#ifdef WOLFMQTT_ENABLE_STDIN_CAP
else if (rc == MQTT_CODE_STDIN_WAKE) {
XMEMSET(mqttCtx->rx_buf, 0, MAX_BUFFER_SIZE);
if (XFGETS((char*)mqttCtx->rx_buf, MAX_BUFFER_SIZE - 1,
stdin) != NULL)
{
rc = (int)XSTRLEN((char*)mqttCtx->rx_buf);
/* Publish Topic */
mqttCtx->stat = WMQ_PUB;
XMEMSET(&mqttCtx->publish, 0, sizeof(MqttPublish));
mqttCtx->publish.retain = 0;
mqttCtx->publish.qos = mqttCtx->qos;
mqttCtx->publish.duplicate = 0;
mqttCtx->publish.topic_name = mqttCtx->topic_name;
mqttCtx->publish.packet_id = mqtt_get_packetid();
mqttCtx->publish.buffer = mqttCtx->rx_buf;
mqttCtx->publish.total_len = (word16)rc;
rc = MqttClient_Publish(&mqttCtx->client,
&mqttCtx->publish);
PRINTF("MQTT Publish: Topic %s, ID %d, %s (%d)",
mqttCtx->publish.topic_name, mqttCtx->publish.packet_id,
MqttClient_ReturnCodeToString(rc), rc);
}
}
#endif
else if (rc == MQTT_CODE_ERROR_TIMEOUT) {
/* Keep Alive */
PRINTF("Keep-alive timeout, sending ping");
rc = MqttClient_Ping_ex(&mqttCtx->client, &mqttCtx->ping);
if (rc != MQTT_CODE_SUCCESS) {
PRINTF("MQTT Ping Keep Alive Error: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
break;
}
}
else if (rc != MQTT_CODE_SUCCESS) {
/* There was an error */
PRINTF("MQTT Message Wait: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
break;
}
} while (!mStopRead);
/* Check for error */
if (rc != MQTT_CODE_SUCCESS) {
goto disconn;
}
/* Unsubscribe Topics */
XMEMSET(&mqttCtx->unsubscribe, 0, sizeof(MqttUnsubscribe));
mqttCtx->unsubscribe.packet_id = mqtt_get_packetid();
mqttCtx->unsubscribe.topic_count =
sizeof(mqttCtx->topics) / sizeof(MqttTopic);
mqttCtx->unsubscribe.topics = mqttCtx->topics;
/* Unsubscribe Topics */
rc = MqttClient_Unsubscribe(&mqttCtx->client,
&mqttCtx->unsubscribe);
PRINTF("MQTT Unsubscribe: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
if (rc != MQTT_CODE_SUCCESS) {
goto disconn;
}
mqttCtx->return_code = rc;
disconn:
/* Disconnect */
XMEMSET(&mqttCtx->disconnect, 0, sizeof(mqttCtx->disconnect));
#ifdef WOLFMQTT_V5
{
/* Session expiry interval */
MqttProp* prop = MqttClient_PropsAdd(&mqttCtx->disconnect.props);
prop->type = MQTT_PROP_SESSION_EXPIRY_INTERVAL;
prop->data_int = 0;
}
#if 0 /* enable to test sending a disconnect reason code */
if (mqttCtx->enable_lwt) {
/* Disconnect with Will Message */
mqttCtx->disconnect.reason_code = MQTT_REASON_DISCONNECT_W_WILL_MSG;
}
#endif
#endif
rc = MqttClient_Disconnect_ex(&mqttCtx->client, &mqttCtx->disconnect);
#ifdef WOLFMQTT_V5
if (mqttCtx->disconnect.props != NULL) {
/* Release the allocated properties */
MqttClient_PropsFree(mqttCtx->disconnect.props);
}
#endif
PRINTF("MQTT Disconnect: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
rc = MqttClient_NetDisconnect(&mqttCtx->client);
PRINTF("MQTT Socket Disconnect: %s (%d)",
MqttClient_ReturnCodeToString(rc), rc);
exit:
/* Free resources */
if (mqttCtx->tx_buf) WOLFMQTT_FREE(mqttCtx->tx_buf);
if (mqttCtx->rx_buf) WOLFMQTT_FREE(mqttCtx->rx_buf);
/* Cleanup network */
MqttClientNet_DeInit(&mqttCtx->net);
MqttClient_DeInit(&mqttCtx->client);
return rc;
}
/* so overall tests can pull in test function */
#ifdef USE_WINDOWS_API
#include <windows.h> /* for ctrl handler */
static BOOL CtrlHandler(DWORD fdwCtrlType)
{
if (fdwCtrlType == CTRL_C_EVENT) {
mStopRead = 1;
PRINTF("Received Ctrl+c");
return TRUE;
}
return FALSE;
}
#elif HAVE_SIGNAL
#include <signal.h>
static void sig_handler(int signo)
{
if (signo == SIGINT) {
mStopRead = 1;
PRINTF("Received SIGINT");
}
}
#endif
#if defined(NO_MAIN_DRIVER)
int mqttclient_main(int argc, char** argv)
#else
int main(int argc, char** argv)
#endif
{
int rc;
MQTTCtx mqttCtx;
/* init defaults */
mqtt_init_ctx(&mqttCtx);
/* parse arguments */
rc = mqtt_parse_args(&mqttCtx, argc, argv);
if (rc != 0) {
if (rc == MY_EX_USAGE) {
/* return success, so make check passes with TLS disabled */
return 0;
}
return rc;
}
#ifdef USE_WINDOWS_API
if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler,
TRUE) == FALSE)
{
PRINTF("Error setting Ctrl Handler! Error %d", (int)GetLastError());
}
#elif HAVE_SIGNAL
if (signal(SIGINT, sig_handler) == SIG_ERR) {
PRINTF("Can't catch SIGINT");
}
#endif
rc = mqttclient_test(&mqttCtx);
mqtt_free_ctx(&mqttCtx);
return (rc == 0) ? 0 : EXIT_FAILURE;
}