-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathumqtt.c
More file actions
2004 lines (1817 loc) · 65.9 KB
/
umqtt.c
File metadata and controls
2004 lines (1817 loc) · 65.9 KB
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-05-08 springcity the first version
*/
#include <string.h>
#include <rtthread.h>
#include <rtdef.h>
#include <sys/errno.h>
#include "umqtt_cfg.h"
#include "umqtt_internal.h"
#include "umqtt.h"
#define DBG_TAG "umqtt"
#ifdef PKG_UMQTT_USING_DEBUG
#define DBG_LVL DBG_LOG
#else
#define DBG_LVL DBG_INFO
#endif /* MQTT_DEBUG */
#include <rtdbg.h>
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
#define UMQTT_CLIENT_LOCK(CLIENT) rt_mutex_take(CLIENT->lock_client, RT_WAITING_FOREVER)
#define UMQTT_CLIENT_UNLOCK(CLIENT) rt_mutex_release(CLIENT->lock_client)
#define UMQTT_SET_CONNECT_FLAGS(user_name_flag, password_flag, will_retain, will_qos, will_flag, clean_session, reserved) \
(((user_name_flag & 0x01) << 7) | \
((password_flag & 0x01) << 6) | \
((will_retain & 0x01) << 5) | \
((will_qos & 0x01) << 3) | \
((will_flag & 0x01) << 2) | \
((clean_session & 0x01) << 1) | \
(reserved & 0x01))
#define UMQTT_DEF_CONNECT_FLAGS (UMQTT_SET_CONNECT_FLAGS(0,0,0,0,0,1,0))
struct umqtt_msg_ack
{
rt_uint16_t packet_id;
rt_uint8_t msg_type;
};
struct umqtt_qos2_msg
{
rt_uint16_t topic_name_len;
char *topic_name;
rt_uint16_t packet_id;
char *payload;
rt_uint32_t payload_len;
rt_list_t next_list;
};
struct umqtt_pubrec_msg
{
int cnt;
int packet_id;
int next_tick; /* next tick*/
};
struct umqtt_client
{
int sock; /* socket sock */
enum umqtt_client_state connect_state; /* mqtt client status */
struct umqtt_info mqtt_info; /* user mqtt config information */
rt_uint8_t reconnect_count; /* mqtt client reconnect count */
rt_uint8_t keepalive_count; /* mqtt keepalive count */
rt_uint32_t pingreq_last_tick; /* mqtt ping request message */
rt_uint32_t uplink_next_tick; /* uplink (include: publish/subscribe/unsub/connect/ping/... client->broker) next tick(ping) */
rt_uint32_t uplink_last_tick; /* uplink (include: publish/subscribe/unsub/connect/ping/... client->broker) next tick(ping) */
rt_uint32_t reconnect_next_tick; /* client unlink, reconnect next tick */
rt_uint32_t reconnect_last_tick; /* client unlink, reconnect last tick */
rt_uint8_t *send_buf, *recv_buf; /* send data buffer, receive data buffer */
rt_size_t send_len, recv_len; /* send datas length, receive datas length */
rt_uint16_t packet_id; /* mqtt packages id */
rt_mutex_t lock_client; /* mqtt client lock */
rt_mq_t msg_queue; /* fro receive thread with other thread to communicate message */
rt_timer_t uplink_timer; /* client send message to broker manager timer */
int sub_recv_list_len; /* subscribe topic, receive topicname to deal datas */
rt_list_t sub_recv_list; /* subscribe information list header */
rt_list_t qos2_msg_list; /* qos2 message list */
struct umqtt_pubrec_msg pubrec_msg[PKG_UMQTT_QOS2_QUE_MAX]; /* pubrec message array */
umqtt_user_callback user_handler; /* user handler */
void *user_data; /* user data */
rt_thread_t task_handle; /* task thread */
rt_list_t list; /* list header */
};
enum tick_item
{
UPLINK_LAST_TICK = 0,
UPLINK_NEXT_TICK = 1,
RECON_LAST_TICK = 2,
RECON_NEXT_TICK = 3,
};
static int umqtt_handle_readpacket(struct umqtt_client *client);
static void umqtt_deliver_message(struct umqtt_client *client,
const char *topic_name, int len,
struct umqtt_pkgs_publish *msg);
static int get_next_packetID(struct umqtt_client *client)
{
return client->packet_id = (client->packet_id == UMQTT_MAX_PACKET_ID) ? 1 : (client->packet_id + 1);
}
static int add_one_qos2_msg(struct umqtt_client *client, struct umqtt_pkgs_publish *pdata)
{
int _ret = UMQTT_OK;
struct umqtt_qos2_msg *msg = RT_NULL;
if ((pdata) && (client))
{
if (rt_list_len(&client->qos2_msg_list) >= PKG_UMQTT_QOS2_QUE_MAX)
{
LOG_W(" qos2 message list is over !");
}
else
{
msg = (struct umqtt_qos2_msg *)rt_calloc(1, sizeof(struct umqtt_qos2_msg));
if (msg)
{
msg->topic_name_len = pdata->topic_name_len;
msg->packet_id = pdata->packet_id;
msg->payload_len = pdata->payload_len;
if ((pdata->topic_name != RT_NULL) && (pdata->topic_name_len != 0))
{
msg->topic_name = (char *)rt_calloc(1, sizeof(char) * pdata->topic_name_len);
if (msg->topic_name)
{
rt_strncpy(msg->topic_name, pdata->topic_name, msg->topic_name_len);
}
else
{
_ret = UMQTT_MEM_FULL;
LOG_E(" calloc umqtt qos2 message topic name failed! memory full! ");
}
}
if ((pdata->payload != RT_NULL) && (pdata->payload_len != 0))
{
msg->payload = (char *)rt_calloc(1, sizeof(char) * pdata->payload_len);
if (msg->payload)
{
rt_strncpy(msg->payload, pdata->payload, msg->payload_len);
}
else
{
_ret = UMQTT_MEM_FULL;
LOG_E(" calloc umqtt qos2 message payload failed! memory full! ");
}
}
}
else
{
_ret = UMQTT_MEM_FULL;
LOG_E(" calloc umqtt qos2 message failed! ");
}
}
}
else
{
_ret = UMQTT_INPARAMS_NULL;
LOG_E(" add qos2 message failed! input params is valid! ");
}
if (_ret == UMQTT_OK)
{
rt_list_insert_after(&client->qos2_msg_list, &msg->next_list);
}
//_exit:
if (_ret == UMQTT_MEM_FULL)
{
if (msg)
{
if (msg->topic_name) { rt_free(msg->topic_name); msg->topic_name = RT_NULL; }
if (msg->payload) { rt_free(msg->payload); msg->payload = RT_NULL; }
rt_free(msg); msg = RT_NULL;
}
}
return _ret;
}
static int qos2_publish_delete(struct umqtt_client *client, int packet_id)
{
int _ret = UMQTT_OK, tmp_ret = 0;
struct umqtt_qos2_msg *p_msg = RT_NULL;
rt_list_t *node = RT_NULL;
struct umqtt_pkgs_publish publish_msg = { 0 };
/* call publish, delete packet id delete */
if ((tmp_ret == rt_list_isempty(&client->qos2_msg_list)) == 0)
{
rt_list_for_each(node, &client->qos2_msg_list)
{
p_msg = rt_list_entry(node, struct umqtt_qos2_msg, next_list);
if (p_msg->packet_id == packet_id)
{
LOG_D(" qos2, deliver message! topic nme: %s ", p_msg->topic_name);
publish_msg.topic_name_len = p_msg->topic_name_len;
publish_msg.payload_len = p_msg->payload_len;
publish_msg.packet_id = p_msg->packet_id;
publish_msg.topic_name = p_msg->topic_name;
publish_msg.payload = p_msg->payload;
umqtt_deliver_message(client, p_msg->topic_name, p_msg->topic_name_len, &publish_msg);
if (p_msg->topic_name) { rt_free(p_msg->topic_name); p_msg->topic_name = RT_NULL; }
if (p_msg->payload) { rt_free(p_msg->payload); p_msg->payload = RT_NULL; }
rt_list_remove(&(p_msg->next_list));
rt_free(p_msg); p_msg = RT_NULL;
goto _exit;
}
}
}
_exit:
return _ret;
}
static int add_one_pubrec_msg(struct umqtt_client *client, int packet_id)
{
int _ret = UMQTT_OK, _cnt = 0;
for (_cnt = 0; _cnt < PKG_UMQTT_QOS2_QUE_MAX; _cnt++)
{
if (client->pubrec_msg[_cnt].cnt == -1)
{
UMQTT_CLIENT_LOCK(client);
client->pubrec_msg[_cnt].cnt = PKG_UMQTT_PUBLISH_RECON_MAX;
client->pubrec_msg[_cnt].packet_id = packet_id;
client->pubrec_msg[_cnt].next_tick = rt_tick_get() + PKG_UMQTT_RECPUBREC_INTERVAL_TIME;
UMQTT_CLIENT_UNLOCK(client);
break;
}
}
if (_cnt >= PKG_UMQTT_QOS2_QUE_MAX)
{
LOG_W(" add one pubrec message is full! ");
_ret = UMQTT_MEM_FULL;
}
return _ret;
}
static int clear_one_pubrec_msg(struct umqtt_client *client, int packet_id)
{
int _ret = UMQTT_OK, _cnt = 0;
for (_cnt = 0; _cnt < PKG_UMQTT_QOS2_QUE_MAX; _cnt++)
{
if (client->pubrec_msg[_cnt].packet_id == packet_id)
{
UMQTT_CLIENT_LOCK(client);
client->pubrec_msg[_cnt].cnt = -1;
client->pubrec_msg[_cnt].packet_id = -1;
client->pubrec_msg[_cnt].next_tick = -1;
UMQTT_CLIENT_UNLOCK(client);
break;
}
}
return _ret;
}
static int pubrec_cycle_callback(struct umqtt_client *client)
{
int _ret = UMQTT_OK, _cnt = 0;
struct umqtt_msg encode_msg = { 0 };
/* search pubrec packet id, encode, transport, change next tick time */
for (_cnt = 0; _cnt < PKG_UMQTT_QOS2_QUE_MAX; _cnt++)
{
if ((client->pubrec_msg[_cnt].cnt != -1)
&& (client->pubrec_msg[_cnt].packet_id != -1)
&& (client->pubrec_msg[_cnt].next_tick != -1))
{
if (client->pubrec_msg[_cnt].next_tick <= rt_tick_get())
{
UMQTT_CLIENT_LOCK(client);
client->pubrec_msg[_cnt].cnt--;
client->pubrec_msg[_cnt].next_tick = rt_tick_get() + PKG_UMQTT_RECPUBREC_INTERVAL_TIME;
UMQTT_CLIENT_UNLOCK(client);
if (client->pubrec_msg[_cnt].cnt < 0)
{
qos2_publish_delete(client, client->pubrec_msg[_cnt].packet_id);
LOG_W(" pubrec failed!");
UMQTT_CLIENT_LOCK(client);
client->pubrec_msg[_cnt].cnt = -1;
client->pubrec_msg[_cnt].packet_id = -1;
client->pubrec_msg[_cnt].next_tick = -1;
UMQTT_CLIENT_UNLOCK(client);
}
else
{
rt_memset(&encode_msg, 0, sizeof(struct umqtt_msg));
encode_msg.header.bits.qos = UMQTT_QOS2;
encode_msg.header.bits.dup = 0;
encode_msg.header.bits.type = UMQTT_TYPE_PUBREC;
encode_msg.msg.pubrec.packet_id = client->pubrec_msg[_cnt].packet_id;
_ret = umqtt_encode(encode_msg.header.bits.type, client->send_buf, client->mqtt_info.send_size,
&encode_msg);
if (_ret < 0)
{
_ret = UMQTT_ENCODE_ERROR;
LOG_E(" pubrec failed!");
goto _exit;
}
client->send_len = _ret;
_ret = umqtt_trans_send(client->sock, client->send_buf, client->send_len,
client->mqtt_info.send_timeout);
if (_ret < 0)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" trans send failed!");
goto _exit;
}
}
}
}
}
_exit:
return _ret;
}
static void set_connect_status(struct umqtt_client *client, enum umqtt_client_state status)
{
UMQTT_CLIENT_LOCK(client);
client->connect_state = status;
UMQTT_CLIENT_UNLOCK(client);
}
static void set_uplink_recon_tick(struct umqtt_client *client, enum tick_item item)
{
RT_ASSERT(client);
switch (item)
{
case UPLINK_LAST_TICK:
UMQTT_CLIENT_LOCK(client);
client->uplink_last_tick = rt_tick_get();
UMQTT_CLIENT_UNLOCK(client);
break;
case UPLINK_NEXT_TICK:
UMQTT_CLIENT_LOCK(client);
client->uplink_next_tick = client->mqtt_info.keepalive_interval * 1000 + rt_tick_get();
UMQTT_CLIENT_UNLOCK(client);
break;
case RECON_LAST_TICK:
UMQTT_CLIENT_LOCK(client);
client->reconnect_last_tick = rt_tick_get();
UMQTT_CLIENT_UNLOCK(client);
break;
case RECON_NEXT_TICK:
UMQTT_CLIENT_LOCK(client);
client->reconnect_next_tick = client->mqtt_info.reconnect_interval * 1000 + rt_tick_get();
UMQTT_CLIENT_UNLOCK(client);
break;
default:
LOG_W(" set tick item outof set! value: %d", item);
break;
}
};
static int umqtt_connect(struct umqtt_client *client, int block)
{
int _ret = 0, _length = 0, _cnt = 0;
struct umqtt_msg encode_msg = { 0 };
//struct umqtt_msg_ack msg_ack = { 0 };
RT_ASSERT(client);
_reconnect:
client->reconnect_count++;
if (client->reconnect_count > client->mqtt_info.reconnect_max_num)
{
_ret = UMQTT_RECONNECT_FAILED;
client->reconnect_count = 0;
LOG_E(" reconnect failed!");
goto exit;
}
_ret = umqtt_trans_connect(client->mqtt_info.uri, &(client->sock));
if (_ret < 0)
{
_ret = UMQTT_SOCK_CONNECT_FAILED;
LOG_E(" umqtt connect, transport connect failed!");
goto disconnect;
}
encode_msg.msg.connect.protocol_name_len = PKG_UMQTT_PROTOCOL_NAME_LEN;
encode_msg.msg.connect.protocol_name = PKG_UMQTT_PROTOCOL_NAME;
encode_msg.msg.connect.protocol_level = PKG_UMQTT_PROTOCOL_LEVEL;
encode_msg.msg.connect.connect_flags.connect_sign = UMQTT_DEF_CONNECT_FLAGS;
#ifdef PKG_UMQTT_TEST_SHORT_KEEPALIVE_TIME
encode_msg.msg.connect.keepalive_interval_sec = ((client->mqtt_info.connect_keepalive_sec == 0) ? PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME : client->mqtt_info.connect_keepalive_sec);
#else
encode_msg.msg.connect.keepalive_interval_sec = PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME;
#endif
encode_msg.msg.connect.client_id = client->mqtt_info.client_id;
encode_msg.msg.connect.will_topic = client->mqtt_info.lwt_topic;
encode_msg.msg.connect.will_message = client->mqtt_info.lwt_message;
encode_msg.msg.connect.user_name = client->mqtt_info.user_name;
if (client->mqtt_info.user_name)
{
encode_msg.msg.connect.connect_flags.bits.username_flag = 1;
}
encode_msg.msg.connect.password = client->mqtt_info.password;
if (client->mqtt_info.password) {
encode_msg.msg.connect.connect_flags.bits.password_flag = 1;
encode_msg.msg.connect.password_len = rt_strlen(client->mqtt_info.password);
}
_length = umqtt_encode(UMQTT_TYPE_CONNECT, client->send_buf, client->mqtt_info.send_size, &encode_msg);
if (_length <= 0)
{
_ret = UMQTT_ENCODE_ERROR;
LOG_E(" connect encode failed!");
goto exit;
}
client->send_len = _length;
_ret = umqtt_trans_send(client->sock, client->send_buf, client->send_len, client->mqtt_info.send_timeout);
if (_ret < 0)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" connect trans send failed! errno:0x%04x", errno);
goto exit;
}
set_uplink_recon_tick(client, UPLINK_LAST_TICK);
if (block != 0)
{
_ret = umqtt_handle_readpacket(client);
if (_ret == UMQTT_FIN_ACK)
{
LOG_D(" server fin ack, connect failed!");
goto disconnect;
}
else if (_ret < 0)
{
_ret = UMQTT_READ_ERROR;
LOG_E(" connect trans recv failed!");
goto exit;
}
while (1)
{
if (_cnt++ >= (client->mqtt_info.connect_time << 1))
{
_ret = UMQTT_TIMEOUT;
LOG_E(" connect recv message timeout!");
goto exit;
}
else
{
if (client->connect_state == UMQTT_CS_LINKED)
{
_cnt = 0;
_ret = UMQTT_OK;
LOG_I(" connect success!");
goto exit;
}
}
rt_thread_mdelay(500);
}
}
_ret = UMQTT_OK;
LOG_D(" connect sucess!");
disconnect:
if ((_ret == UMQTT_FIN_ACK) || (_ret == UMQTT_SOCK_CONNECT_FAILED))
{
_ret = UMQTT_FIN_ACK;
umqtt_trans_disconnect(client->sock);
client->sock = -1;
rt_thread_delay(RT_TICK_PER_SECOND * 5);
LOG_E(" server send fin ack, need to reconnect!");
goto _reconnect;
}
exit:
if (_ret < 0)
{
set_connect_status(client, UMQTT_CS_DISCONNECT); /* reconnect failed */
LOG_W(" set client status disconnect! ");
}
return _ret;
}
static int umqtt_disconnect(struct umqtt_client *client)
{
int _ret = 0, _length = 0;
RT_ASSERT(client);
_length = umqtt_encode(UMQTT_TYPE_DISCONNECT, client->send_buf, client->mqtt_info.send_size, RT_NULL);
if (_length < 0)
{
_ret = UMQTT_ENCODE_ERROR;
LOG_E(" disconnect encode failed!");
goto exit;
}
client->send_len = _length;
_ret = umqtt_trans_send(client->sock, client->send_buf, client->send_len,
client->mqtt_info.send_timeout);
if (_ret < 0)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" disconnect trans send failed!");
goto exit;
}
_ret = UMQTT_OK;
exit:
return _ret;
}
static rt_uint8_t topicname_is_matched(char *topic_filter, char *topic_name, int len)
{
RT_ASSERT(topic_filter);
RT_ASSERT(topic_name);
char *cur_f = topic_filter;
char *cur_n = topic_name;
char *cur_n_end = cur_n + len;
while (*cur_f && (cur_n < cur_n_end))
{
if ((*cur_n == '/') && (*cur_f != '/'))
break;
if ((*cur_f != '+') && (*cur_f != '#') && (*cur_f != *cur_n))
break;
if (*cur_f == '+')
{
char *nextpos = cur_n + 1;
while (nextpos < cur_n_end && *nextpos != '/')
nextpos = ++cur_n + 1;
}
else if (*cur_f == '#')
{
cur_n = cur_n_end - 1;
}
cur_f++;
cur_n++;
};
return ((cur_n == cur_n_end) && (*cur_f == '\0'));
}
static void umqtt_deliver_message(struct umqtt_client *client,
const char *topic_name, int len,
struct umqtt_pkgs_publish *msg)
{
RT_ASSERT(client);
RT_ASSERT(topic_name);
RT_ASSERT(msg);
struct subtop_recv_handler *p_subtop = RT_NULL;
rt_list_t *node = RT_NULL;
rt_list_for_each(node, &client->sub_recv_list)
{
p_subtop = rt_list_entry(node, struct subtop_recv_handler, next_list);
if ((p_subtop->topicfilter)
&& (topicname_is_matched(p_subtop->topicfilter, (char *)topic_name, len)))
{
if (p_subtop->callback != RT_NULL)
{
p_subtop->callback(client, msg);
}
}
}
}
static int umqtt_readpacket(struct umqtt_client *client, unsigned char *buf, int len, int timeout)
{
int bytes = 0, _ret = 0;
rt_tick_t timeout_tick = (rt_tick_from_millisecond(timeout) & (RT_TICK_MAX >> 1));
rt_tick_t end_tick = rt_tick_get() + timeout_tick;
RT_ASSERT(client);
RT_ASSERT(buf);
if (len == 0)
{
_ret = UMQTT_OK;
return _ret;
}
while (bytes < len)
{
_ret = umqtt_trans_recv(client->sock, &buf[bytes], (size_t)(len - bytes));
if (_ret == -1)
{
if (!(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN))
{
_ret = UMQTT_READ_FAILED;
LOG_E(" readpacket error! errno(%d)", errno);
goto exit;
}
}
else if (_ret == 0)
{
_ret = UMQTT_FIN_ACK;
LOG_W(" readpacket return 0!");
goto exit;
}
else
{
bytes += _ret;
}
if ((rt_tick_get() - end_tick) < (RT_TICK_MAX >> 1))
{
_ret = UMQTT_READ_TIMEOUT;
LOG_W(" readpacket timeout! now_tick(%ld), endtick(%ld), timeout(%ld) ",
rt_tick_get(), end_tick, timeout_tick);
goto exit;
}
}
_ret = UMQTT_OK;
exit:
return _ret;
}
static int umqtt_handle_readpacket(struct umqtt_client *client)
{
int _ret = 0, _onedata = 0, _cnt = 0, _loop_cnt = 0;// _remain_len = 0;
int _temp_ret = 0;
int _pkt_len = 0;
int _multiplier = 1;
int _pkt_type = 0;
struct umqtt_msg decode_msg = { 0 };
struct umqtt_msg_ack msg_ack = { 0 };
struct umqtt_msg encode_msg = { 0 };
RT_ASSERT(client);
/* 1. read the heade type */
_temp_ret = umqtt_trans_recv(client->sock, client->recv_buf, 1);
if (_temp_ret <= 0)
{
_ret = UMQTT_FIN_ACK;
LOG_W(" server fin ack! connect failed! need to reconnect!");
goto exit;
}
/* 2. read length */
do {
if (++_cnt > MAX_NO_OF_REMAINING_LENGTH_BYTES)
{
_ret = UMQTT_FAILED;
LOG_E(" umqtt packet length error!");
goto exit;
}
_ret = umqtt_readpacket(client, (unsigned char *)&_onedata, 1, client->mqtt_info.recv_time_ms);
if (_ret == UMQTT_FIN_ACK)
{
LOG_W(" server fin ack! connect failed! need to reconnect!");
goto exit;
}
else if (_ret != UMQTT_OK)
{
_ret = UMQTT_READ_FAILED;
goto exit;
}
*(client->recv_buf + _cnt) = _onedata;
_pkt_len += (_onedata & 0x7F) * _multiplier;
_multiplier *= 0x80;
} while ((_onedata & 0x80) != 0);
/* read and delete if the data length is greater than the cache buff */
if ((_pkt_len + 1 + _cnt) > client->mqtt_info.recv_size)
{
LOG_W(" socket read buffer too short! will read and delete socket buff! ");
_loop_cnt = _pkt_len / client->mqtt_info.recv_size;
do
{
if (_loop_cnt == 0)
{
umqtt_readpacket(client, client->recv_buf, _pkt_len, client->mqtt_info.recv_time_ms);
_ret = UMQTT_BUFFER_TOO_SHORT;
LOG_W(" finish read and delete socket buff!");
goto exit;
}
else
{
_loop_cnt--;
umqtt_readpacket(client, client->recv_buf, client->mqtt_info.recv_size, client->mqtt_info.recv_time_ms);
_pkt_len -= client->mqtt_info.recv_size;
}
}while(1);
}
/* 3. read the remain datas */
_ret = umqtt_readpacket(client, client->recv_buf + _cnt + 1, _pkt_len, client->mqtt_info.recv_time_ms);
if (_ret == UMQTT_FIN_ACK)
{
LOG_W(" server fin ack! connect failed! need to reconnect!");
goto exit;
}
else if (_ret != UMQTT_OK)
{
_ret = UMQTT_READ_FAILED;
LOG_E(" read remain datas error!");
goto exit;
}
/* 4. encode packet datas */
rt_memset(&decode_msg, 0, sizeof(decode_msg));
_ret = umqtt_decode(client->recv_buf, _pkt_len + _cnt + 1, &decode_msg);
if (_ret < 0)
{
_ret = UMQTT_DECODE_ERROR;
LOG_E(" decode error!");
goto exit;
}
_pkt_type = decode_msg.header.bits.type;
switch (_pkt_type)
{
case UMQTT_TYPE_CONNACK:
{
LOG_D(" read connack cmd information!");
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
set_connect_status(client, UMQTT_CS_LINKED);
}
break;
case UMQTT_TYPE_PUBLISH:
{
LOG_D(" read publish cmd information!");
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
if (decode_msg.header.bits.qos != UMQTT_QOS2)
{
LOG_D(" qos: %d, deliver message! topic nme: %s ", decode_msg.header.bits.qos, decode_msg.msg.publish.topic_name);
umqtt_deliver_message(client, decode_msg.msg.publish.topic_name, decode_msg.msg.publish.topic_name_len,
&(decode_msg.msg.publish));
}
if (decode_msg.header.bits.qos != UMQTT_QOS0)
{
rt_memset(&encode_msg, 0, sizeof(encode_msg));
encode_msg.header.bits.qos = decode_msg.header.bits.qos;
encode_msg.header.bits.dup = decode_msg.header.bits.dup;
if (decode_msg.header.bits.qos == UMQTT_QOS1)
{
encode_msg.header.bits.type = UMQTT_TYPE_PUBACK;
encode_msg.msg.puback.packet_id = decode_msg.msg.publish.packet_id;
}
else if (decode_msg.header.bits.qos == UMQTT_QOS2)
{
encode_msg.header.bits.type = UMQTT_TYPE_PUBREC;
add_one_qos2_msg(client, &(decode_msg.msg.publish));
encode_msg.msg.pubrel.packet_id = decode_msg.msg.publish.packet_id;
add_one_pubrec_msg(client, encode_msg.msg.pubrel.packet_id); /* add pubrec message */
}
_ret = umqtt_encode(encode_msg.header.bits.type, client->send_buf, client->mqtt_info.send_size,
&encode_msg);
if (_ret < 0)
{
_ret = UMQTT_ENCODE_ERROR;
LOG_E(" puback / pubrec failed!");
goto exit;
}
client->send_len = _ret;
_ret = umqtt_trans_send(client->sock, client->send_buf, client->send_len,
client->mqtt_info.send_timeout);
if (_ret < 0)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" trans send failed!");
goto exit;
}
}
}
break;
case UMQTT_TYPE_PUBACK:
{
LOG_D(" read puback cmd information!");
rt_memset(&msg_ack, 0, sizeof(msg_ack));
msg_ack.msg_type = UMQTT_TYPE_PUBACK;
msg_ack.packet_id = decode_msg.msg.puback.packet_id;
_ret = rt_mq_send(client->msg_queue, &msg_ack, sizeof(struct umqtt_msg_ack));
if (_ret != RT_EOK)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" mq send failed!");
goto exit;
}
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
}
break;
case UMQTT_TYPE_PUBREC:
{
LOG_D(" read pubrec cmd information!");
rt_memset(&msg_ack, 0, sizeof(msg_ack));
msg_ack.msg_type = UMQTT_TYPE_PUBREC;
msg_ack.packet_id = decode_msg.msg.puback.packet_id;
_ret = rt_mq_send(client->msg_queue, &msg_ack, sizeof(struct umqtt_msg_ack));
if (_ret != RT_EOK)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" mq send failed!");
goto exit;
}
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
}
break;
case UMQTT_TYPE_PUBREL:
{
LOG_D(" read pubrel cmd information!");
rt_memset(&encode_msg, 0, sizeof(encode_msg));
encode_msg.header.bits.type = UMQTT_TYPE_PUBCOMP;
encode_msg.header.bits.qos = decode_msg.header.bits.qos;
encode_msg.header.bits.dup = decode_msg.header.bits.dup;
encode_msg.msg.pubrel.packet_id = decode_msg.msg.pubrec.packet_id;
/* publish callback, and delete callback */
qos2_publish_delete(client, encode_msg.msg.pubrel.packet_id);
/* delete array numbers! */
clear_one_pubrec_msg(client, encode_msg.msg.pubrel.packet_id);
_ret = umqtt_encode(UMQTT_TYPE_PUBCOMP, client->send_buf, client->mqtt_info.send_size, &encode_msg);
if (_ret < 0)
{
_ret = UMQTT_ENCODE_ERROR;
LOG_E(" pubcomp failed!");
goto exit;
}
client->send_len = _ret;
_ret = umqtt_trans_send(client->sock, client->send_buf, client->send_len,
client->mqtt_info.send_timeout);
if (_ret < 0)
{
_ret = UMQTT_SEND_FAILED;
LOG_E(" trans send failed!");
goto exit;
}
}
break;
case UMQTT_TYPE_PUBCOMP:
{
LOG_D(" read pubcomp cmd information!");
rt_memset(&msg_ack, 0, sizeof(msg_ack));
msg_ack.msg_type = UMQTT_TYPE_PUBCOMP;
msg_ack.packet_id = decode_msg.msg.pubcomp.packet_id;
_ret = rt_mq_send(client->msg_queue, &msg_ack, sizeof(struct umqtt_msg_ack));
if (_ret != RT_EOK)
{
_ret = UMQTT_SEND_FAILED;
goto exit;
}
}
break;
case UMQTT_TYPE_SUBACK:
{
LOG_D(" read suback cmd information!");
rt_memset(&msg_ack, 0, sizeof(msg_ack));
msg_ack.msg_type = UMQTT_TYPE_SUBACK;
msg_ack.packet_id = decode_msg.msg.suback.packet_id;
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
_ret = rt_mq_send(client->msg_queue, &msg_ack, sizeof(struct umqtt_msg_ack));
if (_ret != RT_EOK)
{
_ret = UMQTT_SEND_FAILED;
goto exit;
}
}
break;
case UMQTT_TYPE_UNSUBACK:
{
LOG_D(" read unsuback cmd information!");
rt_memset(&msg_ack, 0, sizeof(msg_ack));
msg_ack.msg_type = UMQTT_TYPE_UNSUBACK;
msg_ack.packet_id = decode_msg.msg.unsuback.packet_id;
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
_ret = rt_mq_send(client->msg_queue, &msg_ack, sizeof(struct umqtt_msg_ack));
if (_ret != RT_EOK)
{
_ret = UMQTT_SEND_FAILED;
goto exit;
}
}
break;
case UMQTT_TYPE_PINGRESP:
{
LOG_I(" ping resp! broker -> client! now tick: %d ", rt_tick_get());
set_uplink_recon_tick(client, UPLINK_NEXT_TICK);
}
break;
default:
{
LOG_W(" not right type(0x%02x)!", _pkt_type);
}
break;
}
exit:
return _ret;
}
static void umqtt_thread(void *params)
{
int _ret = 0;
struct umqtt_client *client = (struct umqtt_client *)params;
RT_ASSERT(client);
while (1) {
_ret = umqtt_handle_readpacket(client);
if (_ret == UMQTT_FIN_ACK)
{
LOG_W("reconnect start! stop timer -> trans disconnect delay 5000ms -> umqtt connect! ");
rt_timer_stop(client->uplink_timer);
umqtt_trans_disconnect(client->sock);
client->sock = -1;
rt_thread_mdelay(5000);
_ret = umqtt_connect(client, 1);
if (_ret == UMQTT_RECONNECT_FAILED)
{
LOG_E(" umqtt reconnect failed!");
goto _exit;
}
}
}
_exit:
LOG_I(" umqtt thread is quit! ");
client->task_handle = RT_NULL;
return;
}
static void umqtt_check_def_info(struct umqtt_info *info)
{
if (info)
{
if (info->send_size == 0) { info->send_size = PKG_UMQTT_INFO_DEF_SENDSIZE; }
if (info->recv_size == 0) { info->recv_size = PKG_UMQTT_INFO_DEF_RECVSIZE; }
if (info->reconnect_max_num == 0) { info->reconnect_max_num = PKG_UMQTT_INFO_DEF_RECONNECT_MAX_NUM; }
if (info->reconnect_interval == 0) { info->reconnect_interval = PKG_UMQTT_INFO_DEF_RECONNECT_INTERVAL; }
if (info->keepalive_max_num == 0) { info->keepalive_max_num = PKG_UMQTT_INFO_DEF_KEEPALIVE_MAX_NUM; }
if (info->keepalive_interval == 0) { info->keepalive_interval = PKG_UMQTT_INFO_DEF_HEARTBEAT_INTERVAL; }
if (info->connect_time == 0) { info->connect_time = PKG_UMQTT_INFO_DEF_CONNECT_TIMEOUT; }
if (info->recv_time_ms == 0) { info->recv_time_ms = PKG_UMQTT_INFO_DEF_RECV_TIMEOUT_MS; }
if (info->send_timeout == 0) { info->send_timeout = PKG_UMQTT_INFO_DEF_SEND_TIMEOUT; }
if (info->thread_stack_size == 0) { info->thread_stack_size = PKG_UMQTT_INFO_DEF_THREAD_STACK_SIZE; }
if (info->thread_priority == 0) { info->thread_priority = PKG_UMQTT_INFO_DEF_THREAD_PRIORITY; }
}
}
static int umqtt_keepalive_callback(struct umqtt_client *client)
{
int _ret = 0, _length = 0;
rt_uint32_t _connect_kp_time = 0;
RT_ASSERT(client);
#ifdef PKG_UMQTT_TEST_SHORT_KEEPALIVE_TIME
_connect_kp_time = (client->mqtt_info.connect_keepalive_sec == 0) ? (PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME >> 1) : (client->mqtt_info.connect_keepalive_sec >> 1);
#else
_connect_kp_time = PKG_UMQTT_CONNECT_KEEPALIVE_DEF_TIME >> 1;
#endif
if (client->connect_state == UMQTT_CS_LINKED)
{
if (((client->uplink_next_tick <= rt_tick_get())
&& (client->uplink_next_tick > client->uplink_last_tick))
|| ((client->pingreq_last_tick + _connect_kp_time) < rt_tick_get()))
{