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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/net/mqttprotocols.h"
#include "gate/libraries.h"
#include "gate/streams.h"
#include "gate/results.h"
#include "gate/serializers.h"
#include "gate/debugging.h"
/* #include <mosquitto.h> */
gate_result_t gate_mqtt_write_byte(gate_stream_t* stream, gate_uint8_t value)
{
return gate_stream_write(stream, (char const*)&value, 1, NULL);
}
gate_result_t gate_mqtt_write_word(gate_stream_t* stream, gate_uint16_t value)
{
char buff[4];
gate_size_t len = gate_serialize_uint16_b(buff, sizeof(buff), &value);
return gate_stream_write_block(stream, buff, len, NULL);
}
gate_result_t gate_mqtt_write_length(gate_stream_t* stream, gate_size_t value)
{
gate_result_t ret = GATE_RESULT_OK;
gate_size_t bufferused = 0;
unsigned char encoded_char;
while (GATE_SUCCEEDED(ret))
{
encoded_char = value % 128;
value /= 128;
if (value > 0)
{
encoded_char |= 128;
}
ret = gate_stream_write(stream, (char const*)&encoded_char, 1, NULL);
if (value == 0)
{
/* all bits are encoded, we are done */
break;
}
}
return ret;
}
gate_result_t gate_mqtt_write_string(gate_stream_t* stream, char const* str, gate_size_t length)
{
gate_uint16_t const len = (gate_uint16_t)length;
gate_result_t ret = gate_mqtt_write_word(stream, len);
if (GATE_SUCCEEDED(ret))
{
ret = gate_stream_write(stream, str, length, NULL);
}
return ret;
}
gate_result_t gate_mqtt_read_byte(gate_stream_t* stream, gate_uint8_t* value)
{
char chr = 0;
gate_size_t received = 0;
gate_result_t ret = gate_stream_read(stream, &chr, 1, &received);
if (GATE_SUCCEEDED(ret))
{
if (received != 1)
{
ret = GATE_RESULT_ENDOFSTREAM;
}
else
{
*value = (gate_uint8_t)chr;
}
}
return ret;
}
gate_result_t gate_mqtt_read_word(gate_stream_t* stream, gate_uint16_t* value)
{
char buff[2];
gate_size_t received = 0;
gate_result_t ret = gate_stream_read_block(stream, &buff[0], 2, &received);
if (GATE_SUCCEEDED(ret))
{
if (received != 2)
{
ret = GATE_RESULT_ENDOFSTREAM;
}
else
{
gate_deserialize_uint16_b(buff, 2, value);
}
}
return ret;
}
gate_result_t gate_mqtt_read_length(gate_stream_t* stream, gate_size_t* ptr_value)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_size_t value = 0;
unsigned char b;
gate_size_t len_received = 0;
gate_size_t multiplier = 1;
do
{
ret = gate_stream_read(stream, (char*)&b, 1, &len_received);
GATE_BREAK_IF_FAILED(ret);
if (len_received == 0)
{
ret = GATE_RESULT_ENDOFSTREAM;
break;
}
value += (b & 127) * multiplier;
multiplier *= 128;
if (multiplier > 128 * 128 * 128)
{
ret = GATE_RESULT_INVALIDCONTENT;
break;
}
} while ((b & 128) != 0);
if (ptr_value)
{
*ptr_value = value;
}
return ret;
}
gate_result_t gate_mqtt_read_string(gate_stream_t* stream, gate_strbuilder_t* builder)
{
char buffer[4096];<--- The scope of the variable 'buffer' can be reduced. [+]The scope of the variable 'buffer' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t transferred = 0;
gate_size_t received = 0;
gate_uint16_t str_length = 0;
gate_result_t ret = gate_mqtt_read_word(stream, &str_length);
if (GATE_SUCCEEDED(ret))
{
while (transferred < str_length)
{
gate_size_t required = str_length - transferred;
if (required > sizeof(buffer))
{
required = sizeof(buffer);
}
ret = gate_stream_read_block(stream, buffer, required, &received);
GATE_BREAK_IF_FAILED(ret);
gate_strbuilder_append_text(builder, buffer, received);
if (received < required)
{
ret = GATE_RESULT_INVALIDCONTENT;
break;
}
transferred += received;
}
}
return ret;
}
gate_result_t gate_mqtt_read_string_length_skip_data(gate_stream_t* stream, gate_uint16_t* ptr_length)
{
char buffer[4096];<--- The scope of the variable 'buffer' can be reduced. [+]The scope of the variable 'buffer' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_size_t transferred = 0;
gate_size_t received = 0;
gate_uint16_t str_length = 0;
gate_result_t ret = gate_mqtt_read_word(stream, &str_length);
if (GATE_SUCCEEDED(ret))
{
if (ptr_length)
{
*ptr_length = str_length;
}
while (transferred < str_length)
{
gate_size_t required = str_length - transferred;
if (required > sizeof(buffer))
{
required = sizeof(buffer);
}
ret = gate_stream_read_block(stream, buffer, required, &received);
GATE_BREAK_IF_FAILED(ret);
if (received < required)
{
ret = GATE_RESULT_INVALIDCONTENT;
break;
}
transferred += received;
}
}
return ret;
}
static gate_result_t serialize_memstream_packet(
gate_stream_t* output_stream,
gate_mqtt_packet_type_t packet_type,
unsigned char packet_flags,
gate_memstream_t* memstream)
{
gate_uint8_t const fixed_header_byte = (((gate_uint8_t)packet_type & 0x0f) << 4) | (packet_flags & 0x0f);
char const* const ptr_content = gate_memstream_get_data(memstream);
gate_size_t const content_size = gate_memstream_size(memstream);
gate_result_t ret;
do
{
ret = gate_mqtt_write_byte(output_stream, fixed_header_byte);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_length(output_stream, content_size);
GATE_BREAK_IF_FAILED(ret);
ret = gate_stream_write_block(output_stream, ptr_content, content_size, NULL);
GATE_BREAK_IF_FAILED(ret);
} while (0);
return ret;
}
static gate_result_t serialize_connect_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_result_t ret = GATE_RESULT_OK;
gate_memstream_impl_t data_memstream_impl;
char data_memstream_buffer[4096];
gate_memstream_t* const data_memstream = gate_memstream_create_static_unmanaged(&data_memstream_impl, data_memstream_buffer, sizeof(data_memstream_buffer), 0);
gate_stream_t* const data_stream = (gate_stream_t*)data_memstream;
do
{
/* var header */
ret = gate_mqtt_write_string(data_stream, "MQTT", 4);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_byte(data_stream, msg->content.connect.level);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_byte(data_stream, msg->content.connect.flags);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_word(data_stream, msg->content.connect.keep_alive);
GATE_BREAK_IF_FAILED(ret);
/* payload */
ret = gate_mqtt_write_string(data_stream, msg->content.connect.client_id, msg->content.connect.client_id_len);
GATE_BREAK_IF_FAILED(ret);
if (GATE_FLAG_ENABLED(msg->content.connect.flags, GATE_MQTT_CONNECT_FLAG_WILLFLAG))
{
ret = gate_mqtt_write_string(data_stream, msg->content.connect.will_topic, msg->content.connect.will_topic_len);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_string(data_stream, msg->content.connect.will_message, msg->content.connect.will_message_len);
GATE_BREAK_IF_FAILED(ret);
}
if (GATE_FLAG_ENABLED(msg->content.connect.flags, GATE_MQTT_CONNECT_FLAG_USER))
{
ret = gate_mqtt_write_string(data_stream, msg->content.connect.user, msg->content.connect.user_len);
GATE_BREAK_IF_FAILED(ret);
}
if (GATE_FLAG_ENABLED(msg->content.connect.flags, GATE_MQTT_CONNECT_FLAG_PASSWORD))
{
ret = gate_mqtt_write_string(data_stream, msg->content.connect.password, msg->content.connect.password_len);
GATE_BREAK_IF_FAILED(ret);
}
/* build full packet */
ret = serialize_memstream_packet(output_stream, gate_mqtt_packet_connect, msg->packet_flags, data_memstream);
} while (0);
return ret;
}
static gate_result_t serialize_connect_ack_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_result_t ret = GATE_RESULT_OK;
gate_memstream_impl_t data_memstream_impl;
char data_memstream_buffer[64];
gate_memstream_t* const data_memstream = gate_memstream_create_static_unmanaged(&data_memstream_impl, data_memstream_buffer, sizeof(data_memstream_buffer), 0);
gate_stream_t* const data_stream = (gate_stream_t*)data_memstream;
do
{
/* var header */
ret = gate_mqtt_write_byte(data_stream, msg->content.connect_ack.flags);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_byte(data_stream, msg->content.connect_ack.return_code);
GATE_BREAK_IF_FAILED(ret);
/* build full packet */
ret = serialize_memstream_packet(output_stream, gate_mqtt_packet_connect_ack, msg->packet_flags, data_memstream);
GATE_BREAK_IF_FAILED(ret);
/* succeeded */
} while (0);
return ret;
}
static gate_result_t serialize_pubish_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_result_t ret;
gate_size_t written = 0;
gate_memstream_t* data_memstream = NULL;
gate_stream_t* data_stream;<--- The scope of the variable 'data_stream' can be reduced. [+]The scope of the variable 'data_stream' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
do
{
const gate_size_t content_len = msg->content.publish.data_len + msg->content.publish.topic_len + 5;
data_memstream = gate_memstream_create(content_len);
if (NULL == data_memstream)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
data_stream = (gate_stream_t*)data_memstream;
/* var header */
ret = gate_mqtt_write_string(data_stream, msg->content.publish.topic, msg->content.publish.topic_len);
GATE_BREAK_IF_FAILED(ret);
if (GATE_FLAG_ENABLED(msg->packet_flags, GATE_MQTT_FLAG_QOS1) || GATE_FLAG_ENABLED(msg->packet_flags, GATE_MQTT_FLAG_QOS2))
{
ret = gate_mqtt_write_word(data_stream, msg->content.publish.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
}
/* payload */
ret = gate_stream_write_block(data_stream, msg->content.publish.data, msg->content.publish.data_len, &written);
GATE_BREAK_IF_FAILED(ret);
/* build full packet */
ret = serialize_memstream_packet(output_stream, gate_mqtt_packet_publish, msg->packet_flags, data_memstream);
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
if (data_memstream != NULL)
{
gate_object_release(data_memstream);
}
return ret;
}
static gate_result_t serialize_nocontent_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_memstream_impl_t data_memstream_impl;
gate_memstream_t* data_memstream = gate_memstream_create_static_unmanaged(&data_memstream_impl, NULL, 0, 0);
return serialize_memstream_packet(output_stream, msg->packet_type, msg->packet_flags, data_memstream);
}
static gate_result_t serialize_packet_identifier_only_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg, gate_uint16_t id)
{
char buffer[16];
gate_memstream_impl_t data_memstream_impl;
gate_memstream_t* data_memstream = gate_memstream_create_static_unmanaged(&data_memstream_impl, buffer, sizeof(buffer), 0);
gate_result_t result = gate_mqtt_write_word((gate_stream_t*)data_memstream, id);
if (GATE_SUCCEEDED(result))
{
result = serialize_memstream_packet(output_stream, msg->packet_type, msg->packet_flags, data_memstream);
}
return result;
}
static gate_result_t serialize_subscribe_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_result_t ret;
gate_size_t written = 0;
gate_memstream_t* data_memstream = NULL;
gate_stream_t* data_stream;<--- The scope of the variable 'data_stream' can be reduced. [+]The scope of the variable 'data_stream' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_mqtt_subscription_t const* subscr;<--- The scope of the variable 'subscr' can be reduced. [+]The scope of the variable 'subscr' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_uint16_t subscr_count;
do
{
data_memstream = gate_memstream_create(128);
if (NULL == data_memstream)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
data_stream = (gate_stream_t*)data_memstream;
/* var header */
ret = gate_mqtt_write_word(data_stream, msg->content.subscribe.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
/* payload */
subscr_count = msg->content.subscribe.topics_count;
subscr = msg->content.subscribe.topics;
for (; subscr_count > 0; --subscr_count, ++subscr)
{
ret = gate_mqtt_write_string(data_stream, subscr->topic, subscr->topic_len);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_write_byte(data_stream, subscr->qos);
GATE_BREAK_IF_FAILED(ret);
}
GATE_BREAK_IF_FAILED(ret);
/* build full packet */
ret = serialize_memstream_packet(output_stream, gate_mqtt_packet_subscribe, 0x02, data_memstream);
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
if (data_memstream != NULL)
{
gate_object_release(data_memstream);
}
return ret;
}
static gate_result_t serialize_subscribe_ack_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_result_t ret;
gate_size_t written = 0;
gate_memstream_t* data_memstream = NULL;
gate_stream_t* data_stream;<--- The scope of the variable 'data_stream' can be reduced. [+]The scope of the variable 'data_stream' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_uint8_t const* ret_codes;<--- The scope of the variable 'ret_codes' can be reduced. [+]The scope of the variable 'ret_codes' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_uint16_t ret_codes_count;
do
{
data_memstream = gate_memstream_create(128);
if (NULL == data_memstream)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
data_stream = (gate_stream_t*)data_memstream;
/* var header */
ret = gate_mqtt_write_word(data_stream, msg->content.subscribe_ack.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
/* payload */
ret_codes_count = msg->content.subscribe_ack.ret_codes_count;
ret_codes = msg->content.subscribe_ack.ret_codes;
for (; ret_codes_count > 0; --ret_codes_count, ++ret_codes)
{
ret = gate_mqtt_write_byte(data_stream, *ret_codes);
GATE_BREAK_IF_FAILED(ret);
}
GATE_BREAK_IF_FAILED(ret);
/* build full packet */
ret = serialize_memstream_packet(output_stream, gate_mqtt_packet_subscribe_ack, msg->packet_flags, data_memstream);
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
if (data_memstream != NULL)
{
gate_object_release(data_memstream);
}
return ret;
}
static gate_result_t serialize_unsubscribe_message(gate_stream_t* output_stream, gate_mqtt_message_t const* msg)
{
gate_result_t ret;
gate_size_t written = 0;
gate_memstream_t* data_memstream = NULL;
gate_stream_t* data_stream;<--- The scope of the variable 'data_stream' can be reduced. [+]The scope of the variable 'data_stream' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_mqtt_subscription_t const* subscr;<--- The scope of the variable 'subscr' can be reduced. [+]The scope of the variable 'subscr' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_uint16_t subscr_count;
do
{
data_memstream = gate_memstream_create(128);
if (NULL == data_memstream)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
data_stream = (gate_stream_t*)data_memstream;
/* var header */
ret = gate_mqtt_write_word(data_stream, msg->content.subscribe.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
/* payload */
subscr_count = msg->content.unsubscribe.topics_count;
subscr = msg->content.unsubscribe.topics;
for (; subscr_count > 0; --subscr_count, ++subscr)
{
ret = gate_mqtt_write_string(data_stream, subscr->topic, subscr->topic_len);
GATE_BREAK_IF_FAILED(ret);
}
GATE_BREAK_IF_FAILED(ret);
/* build full packet */
ret = serialize_memstream_packet(output_stream, gate_mqtt_packet_subscribe, msg->packet_flags, data_memstream);
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
if (data_memstream != NULL)
{
gate_object_release(data_memstream);
}
return ret;
}
gate_result_t gate_mqtt_send_message(gate_stream_t* mqtt_stream, gate_mqtt_message_t const* msg)
{
if (!mqtt_stream || !msg)
{
return GATE_RESULT_INVALIDARG;
}
switch (msg->packet_type)
{
case gate_mqtt_packet_connect: return serialize_connect_message(mqtt_stream, msg);
case gate_mqtt_packet_connect_ack: return serialize_connect_ack_message(mqtt_stream, msg);
case gate_mqtt_packet_publish: return serialize_pubish_message(mqtt_stream, msg);
case gate_mqtt_packet_publish_ack: return serialize_packet_identifier_only_message(mqtt_stream, msg, msg->content.publish_ack.packet_identifier);
case gate_mqtt_packet_publish_receive: return serialize_packet_identifier_only_message(mqtt_stream, msg, msg->content.publish_received.packet_identifier);
case gate_mqtt_packet_publish_release: return serialize_packet_identifier_only_message(mqtt_stream, msg, msg->content.publish_release.packet_identifier);
case gate_mqtt_packet_publish_complete: return serialize_packet_identifier_only_message(mqtt_stream, msg, msg->content.publish_complete.packet_identifier);
case gate_mqtt_packet_subscribe: return serialize_subscribe_message(mqtt_stream, msg);
case gate_mqtt_packet_subscribe_ack: return serialize_subscribe_ack_message(mqtt_stream, msg);
case gate_mqtt_packet_unsubscribe: return serialize_unsubscribe_message(mqtt_stream, msg);
case gate_mqtt_packet_unsubscribe_ack: return serialize_packet_identifier_only_message(mqtt_stream, msg, msg->content.unsubscribe_ack.packet_identifier);
case gate_mqtt_packet_ping_request: return serialize_nocontent_message(mqtt_stream, msg);
case gate_mqtt_packet_ping_response: return serialize_nocontent_message(mqtt_stream, msg);
case gate_mqtt_packet_disconnect: return serialize_nocontent_message(mqtt_stream, msg);
default: return GATE_RESULT_NOTSUPPORTED;
}
}
static gate_result_t deserialize_connect(gate_memoryblock_t* block, gate_mqtt_message_t* msg)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_size_t offset;
gate_strbuilder_t builder = GATE_INIT_EMPTY;
char buffer[4096];
gate_memstream_impl_t block_stream;
char const* const ptr_block_content = (char const*)gate_memoryblock_get_content(block);
gate_size_t const block_content_len = gate_memoryblock_get_size(block);
gate_stream_t* const strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&block_stream, ptr_block_content, block_content_len, block_content_len);
if (strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
gate_strbuilder_create_static(&builder, buffer, sizeof(buffer), 0);
ret = gate_mqtt_read_string(strm, &builder);
GATE_BREAK_IF_FAILED(ret);
if (gate_str_compare(gate_strbuilder_ptr(&builder, 0), gate_strbuilder_length(&builder), "MQTT", 4) != 0)
{
/* Not a valid MQTT connect message */
ret = GATE_RESULT_INVALIDHEADER;
break;
}
ret = gate_mqtt_read_byte(strm, &msg->content.connect.level);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_read_byte(strm, &msg->content.connect.flags);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_read_word(strm, &msg->content.connect.keep_alive);
GATE_BREAK_IF_FAILED(ret);
offset = 10; /* payload starts at block offset 10: */
/* read client id */
gate_mqtt_read_string_length_skip_data(strm, &msg->content.connect.client_id_len);
offset += 2;
msg->content.connect.client_id = &ptr_block_content[offset];
offset += msg->content.connect.client_id_len;
if (GATE_FLAG_ENABLED(msg->content.connect.flags, GATE_MQTT_CONNECT_FLAG_WILLFLAG))
{
/* read optional will topic+content */
gate_mqtt_read_string_length_skip_data(strm, &msg->content.connect.will_topic_len);
offset += 2;
msg->content.connect.will_topic = &ptr_block_content[offset];
offset += msg->content.connect.will_topic_len;
gate_mqtt_read_string_length_skip_data(strm, &msg->content.connect.will_message_len);
offset += 2;
msg->content.connect.will_message = &ptr_block_content[offset];
offset += msg->content.connect.will_message_len;
}
if (GATE_FLAG_ENABLED(msg->content.connect.flags, GATE_MQTT_CONNECT_FLAG_USER))
{
/* read optional user string */
gate_mqtt_read_string_length_skip_data(strm, &msg->content.connect.user_len);
offset += 2;
msg->content.connect.user = &ptr_block_content[offset];
offset += msg->content.connect.user_len;
}
if (GATE_FLAG_ENABLED(msg->content.connect.flags, GATE_MQTT_CONNECT_FLAG_PASSWORD))
{
/* read optional will topic+content */
gate_mqtt_read_string_length_skip_data(strm, &msg->content.connect.password_len);
offset += 2;
msg->content.connect.password = &ptr_block_content[offset];
offset += msg->content.connect.password_len;
}
} while (0);
return ret;
}
static gate_result_t deserialize_connect_ack(gate_memoryblock_t* block, gate_mqtt_message_t* msg)
{
gate_result_t ret = GATE_RESULT_FAILED;
gate_uint8_t ack_flags = 0;
gate_uint8_t ret_code = 0;
do
{
gate_memstream_impl_t block_stream;
char const* const ptr_block_content = (char const*)gate_memoryblock_get_content(block);
gate_size_t const block_content_len = gate_memoryblock_get_size(block);
gate_stream_t* const strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&block_stream, ptr_block_content, block_content_len, block_content_len);
if (strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
if (block_content_len != 2)
{
ret = GATE_RESULT_INVALIDHEADER;
break;
}
ret = gate_mqtt_read_byte(strm, &ack_flags);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_read_byte(strm, &ret_code);
GATE_BREAK_IF_FAILED(ret);
msg->content.connect_ack.flags = ack_flags;
msg->content.connect_ack.return_code = ret_code;
} while (0);
return ret;
}
static gate_result_t deserialize_package_identifier_only_message(gate_memoryblock_t* block, gate_uint16_t* ptr_packet_id)
{
gate_result_t ret;
void const* const ptr_block_data = gate_memoryblock_get_content(block);
gate_size_t const block_len = gate_memoryblock_get_size(block);
do
{
gate_memstream_impl_t data_strm;
gate_stream_t* input_strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&data_strm, ptr_block_data, block_len, block_len);
if (input_strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_mqtt_read_word(input_strm, ptr_packet_id);
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
return ret;
}
static gate_result_t deserialize_publish(gate_memoryblock_t* block, gate_mqtt_message_t* msg)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_size_t offset;
gate_memstream_impl_t block_stream;
char const* const ptr_block_content = (char const*)gate_memoryblock_get_content(block);
gate_size_t const block_content_len = gate_memoryblock_get_size(block);
gate_stream_t* const strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&block_stream, ptr_block_content, block_content_len, block_content_len);
if (strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_mqtt_read_string_length_skip_data(strm, &msg->content.publish.topic_len);
GATE_BREAK_IF_FAILED(ret);
offset = 2;
msg->content.publish.topic = &ptr_block_content[offset];
offset += msg->content.publish.topic_len;
if (GATE_FLAG_ENABLED(msg->packet_flags, GATE_MQTT_FLAG_QOS1) || GATE_FLAG_ENABLED(msg->packet_flags, GATE_MQTT_FLAG_QOS2))
{
ret = gate_mqtt_read_word(strm, &msg->content.publish.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
offset += 2;
}
else
{
msg->content.publish.packet_identifier = 0;
}
msg->content.publish.data_len = (gate_uint32_t)(block_content_len - offset);
msg->content.publish.data = &ptr_block_content[offset];
/* success case reached */
} while (0);
return ret;
}
static gate_result_t deserialize_subscribe(gate_memoryblock_t* block, gate_mqtt_message_t* msg)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_size_t offset;
gate_memstream_impl_t block_stream;
char const* const ptr_block_content = (char const*)gate_memoryblock_get_content(block);
gate_size_t const block_content_len = gate_memoryblock_get_size(block);
gate_stream_t* const strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&block_stream, ptr_block_content, block_content_len, block_content_len);
if (strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_mqtt_read_word(strm, &msg->content.subscribe.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
offset = 2;
msg->content.subscribe.topics_count = 0;
while ((gate_memstream_size(&block_stream) >= 4) && (msg->content.subscribe.topics_count < GATE_MQTT_ARRAY_MAX))
{
gate_mqtt_subscription_t* ptr_sub = &msg->content.subscribe.topics[msg->content.subscribe.topics_count];
ret = gate_mqtt_read_string_length_skip_data(strm, &ptr_sub->topic_len);
GATE_BREAK_IF_FAILED(ret);
offset += 2;
ptr_sub->topic = &ptr_block_content[offset];
offset += ptr_sub->topic_len;
ret = gate_mqtt_read_byte(strm, &ptr_sub->qos);
GATE_BREAK_IF_FAILED(ret);
offset += 1;
++msg->content.subscribe.topics_count;
}
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
return ret;
}
static gate_result_t deserialize_subscribe_ack(gate_memoryblock_t* block, gate_mqtt_message_t* msg)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_size_t offset;
gate_memstream_impl_t block_stream;
char const* const ptr_block_content = (char const*)gate_memoryblock_get_content(block);
gate_size_t const block_content_len = gate_memoryblock_get_size(block);
gate_stream_t* const strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&block_stream, ptr_block_content, block_content_len, block_content_len);
if (strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_mqtt_read_word(strm, &msg->content.subscribe_ack.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
offset = 2;
msg->content.subscribe_ack.ret_codes_count = 0;
while ((gate_memstream_size(&block_stream) > 0) && (msg->content.subscribe_ack.ret_codes_count < GATE_MQTT_ARRAY_MAX))
{
ret = gate_mqtt_read_byte(strm, &msg->content.subscribe_ack.ret_codes[msg->content.subscribe_ack.ret_codes_count]);
GATE_BREAK_IF_FAILED(ret);
++msg->content.subscribe_ack.ret_codes_count;
}
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
return ret;
}
static gate_result_t deserialize_unsubscribe(gate_memoryblock_t* block, gate_mqtt_message_t* msg)
{
gate_result_t ret = GATE_RESULT_FAILED;
do
{
gate_size_t offset;
gate_memstream_impl_t block_stream;
char const* const ptr_block_content = (char const*)gate_memoryblock_get_content(block);
gate_size_t const block_content_len = gate_memoryblock_get_size(block);
gate_stream_t* const strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(
&block_stream, ptr_block_content, block_content_len, block_content_len);
if (strm == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_mqtt_read_word(strm, &msg->content.unsubscribe.packet_identifier);
GATE_BREAK_IF_FAILED(ret);
offset = 2;
msg->content.unsubscribe.topics_count = 0;
while ((gate_memstream_size(&block_stream) >= 3) && (msg->content.unsubscribe.topics_count < GATE_MQTT_ARRAY_MAX))
{
gate_mqtt_subscription_t* ptr_sub = &msg->content.unsubscribe.topics[msg->content.unsubscribe.topics_count];
ret = gate_mqtt_read_string_length_skip_data(strm, &ptr_sub->topic_len);
GATE_BREAK_IF_FAILED(ret);
offset += 2;
ptr_sub->topic = &ptr_block_content[offset];
offset += ptr_sub->topic_len;
ptr_sub->qos = 0;
++msg->content.unsubscribe.topics_count;
}
GATE_BREAK_IF_FAILED(ret);
/* success case reached */
} while (0);
return ret;
}
gate_result_t gate_mqtt_has_queued_message(gate_stream_t* mqtt_stream)
{
gate_result_t ret = GATE_RESULT_FAILED;
char buffer[1024];
char* ptr_buffer = &buffer[0];
gate_size_t buffer_capacity = sizeof(buffer);
gate_size_t returned;
gate_memstream_impl_t memstrm;
gate_stream_t* strm = NULL;
do
{
gate_uint8_t fixed_header_byte = 0;
gate_size_t fixed_header_contentlen = 0;
gate_size_t remaining_bytes;
gate_size_t fixed_header_length;
gate_size_t total_packet_length;
ret = gate_stream_peek(mqtt_stream, ptr_buffer, buffer_capacity, &returned);
GATE_BREAK_IF_FAILED(ret);
if (returned < 2)
{
ret = GATE_RESULT_ENDOFSTREAM;
break;
}
strm = (gate_stream_t*)gate_memstream_create_static_unmanaged_readonly(&memstrm, ptr_buffer, returned, returned);
if (!strm)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_mqtt_read_byte(strm, &fixed_header_byte);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_read_length(strm, &fixed_header_contentlen);
GATE_BREAK_IF_FAILED(ret);
remaining_bytes = gate_memstream_size(&memstrm);
GATE_DEBUG_ASSERT(returned >= remaining_bytes);
fixed_header_length = returned - remaining_bytes;
total_packet_length = fixed_header_length + fixed_header_contentlen;
if (total_packet_length > buffer_capacity)
{
ptr_buffer = (char*)gate_mem_alloc(total_packet_length);
if (ptr_buffer == NULL)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
}
ret = gate_stream_peek(mqtt_stream, ptr_buffer, total_packet_length, &returned);
GATE_BREAK_IF_FAILED(ret);
if (returned < total_packet_length)
{
/* not enough bytes in stream to decode a full package */
ret = GATE_RESULT_ENDOFSTREAM;
break;
}
/* success case, we have a full packet in the stream */
ret = GATE_RESULT_OK;
} while (0);
if ((ptr_buffer != NULL) && (ptr_buffer != &buffer[0]))
{
gate_mem_dealloc(ptr_buffer);
}
return ret;
}
gate_result_t gate_mqtt_receive_message(gate_stream_t* mqtt_stream, gate_mqtt_message_t* msg, gate_memoryblock_t** ptr_new_memblock)
{
gate_result_t ret;
gate_memoryblock_t* mem_block = NULL;
do
{
gate_uint8_t fixed_header_byte = 0;
gate_size_t fixed_header_len = 0;
gate_uint64_t content_transferred = 0;
if (!mqtt_stream || !msg || !ptr_new_memblock)
{
ret = GATE_RESULT_INVALIDARG;
break;
}
ret = gate_mqtt_read_byte(mqtt_stream, &fixed_header_byte);
GATE_BREAK_IF_FAILED(ret);
ret = gate_mqtt_read_length(mqtt_stream, &fixed_header_len);
GATE_BREAK_IF_FAILED(ret);
msg->packet_type = (gate_mqtt_packet_type_t)((fixed_header_byte & 0xf0) >> 4);
msg->packet_flags = fixed_header_byte & 0x0f;
if (fixed_header_len > 0)
{
gate_memstream_impl_t memstream;
mem_block = gate_memoryblock_create(fixed_header_len);
if (NULL == mem_block)
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
if (NULL == gate_memstream_create_static_unmanaged(&memstream, gate_memoryblock_get_content(mem_block), gate_memoryblock_get_size(mem_block), 0))
{
ret = GATE_RESULT_OUTOFMEMORY;
break;
}
ret = gate_stream_transfer_limit(mqtt_stream, fixed_header_len, (gate_stream_t*)&memstream, &content_transferred);
GATE_BREAK_IF_FAILED(ret);
if (content_transferred < fixed_header_len)
{
ret = GATE_RESULT_ENDOFSTREAM;
break;
}
}
switch (msg->packet_type)
{
case gate_mqtt_packet_connect:
ret = deserialize_connect(mem_block, msg);
break;
case gate_mqtt_packet_connect_ack:
ret = deserialize_connect_ack(mem_block, msg);
break;
case gate_mqtt_packet_publish:
ret = deserialize_publish(mem_block, msg);
break;
case gate_mqtt_packet_publish_ack:
ret = deserialize_package_identifier_only_message(mem_block, &msg->content.publish_ack.packet_identifier);
break;
case gate_mqtt_packet_publish_receive:
ret = deserialize_package_identifier_only_message(mem_block, &msg->content.publish_received.packet_identifier);
break;
case gate_mqtt_packet_publish_release:
ret = deserialize_package_identifier_only_message(mem_block, &msg->content.publish_release.packet_identifier);
break;
case gate_mqtt_packet_publish_complete:
ret = deserialize_package_identifier_only_message(mem_block, &msg->content.publish_complete.packet_identifier);
break;
case gate_mqtt_packet_subscribe:
ret = deserialize_subscribe(mem_block, msg);
break;
case gate_mqtt_packet_subscribe_ack:
ret = deserialize_subscribe_ack(mem_block, msg);
break;
case gate_mqtt_packet_unsubscribe:
ret = deserialize_unsubscribe(mem_block, msg);
break;
case gate_mqtt_packet_unsubscribe_ack:
ret = deserialize_package_identifier_only_message(mem_block, &msg->content.unsubscribe_ack.packet_identifier);
break;
case gate_mqtt_packet_ping_request:
case gate_mqtt_packet_ping_response:
case gate_mqtt_packet_disconnect:
/* no content to deserialize */
break;
default:
ret = GATE_RESULT_NOTSUPPORTED;
break;
}
GATE_BREAK_IF_FAILED(ret);
if (ptr_new_memblock != NULL)
{
*ptr_new_memblock = mem_block;
mem_block = NULL;
}
} while (0);
if (mem_block)
{
gate_object_release(mem_block);
mem_block = NULL;
}
return ret;
}
|