-
Notifications
You must be signed in to change notification settings - Fork 3k
/
sn_coap_protocol.c
2378 lines (2006 loc) · 117 KB
/
sn_coap_protocol.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
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) 2011-2015 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file sn_coap_protocol.c
*
* \brief CoAP Protocol implementation
*
* Functionality: CoAP Protocol
*
*/
/* * * * * * * * * * * * * * */
/* * * * INCLUDE FILES * * * */
/* * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h> /* For libary malloc() */
#include <string.h> /* For memset() and memcpy() */
#if defined __linux__ || defined TARGET_LIKE_MBED
#include <time.h>
#endif
#include "ns_types.h"
#include "mbed-coap/sn_coap_protocol.h"
#include "sn_coap_header_internal.h"
#include "sn_coap_protocol_internal.h"
#include "randLIB.h"
#include "mbed-trace/mbed_trace.h"
#define TRACE_GROUP "coap"
/* * * * * * * * * * * * * * * * * * * * */
/* * * * LOCAL FUNCTION PROTOTYPES * * * */
/* * * * * * * * * * * * * * * * * * * * */
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT/* If Message duplication detection is not used at all, this part of code will not be compiled */
static void sn_coap_protocol_linked_list_duplication_info_store(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id, void *param);
static coap_duplication_info_s *sn_coap_protocol_linked_list_duplication_info_search(struct coap_s *handle, sn_nsdl_addr_s *scr_addr_ptr, uint16_t msg_id);
static void sn_coap_protocol_linked_list_duplication_info_remove(struct coap_s *handle, uint8_t *scr_addr_ptr, uint16_t port, uint16_t msg_id);
static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(struct coap_s *handle);
#endif
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
static void sn_coap_protocol_linked_list_blockwise_msg_remove(struct coap_s *handle, coap_blockwise_msg_s *removed_msg_ptr);
static void sn_coap_protocol_linked_list_blockwise_payload_store(struct coap_s *handle, sn_nsdl_addr_s *addr_ptr, uint16_t stored_payload_len, uint8_t *stored_payload_ptr, uint32_t block_number);
static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length);
static bool sn_coap_protocol_linked_list_blockwise_payload_compare_block_number(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint32_t block_number);
static void sn_coap_protocol_linked_list_blockwise_payload_remove(struct coap_s *handle, coap_blockwise_payload_s *removed_payload_ptr);
static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest(struct coap_s *handle);
static uint32_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr);
static void sn_coap_protocol_handle_blockwise_timout(struct coap_s *handle);
static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr, void *param);
static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr);
#endif
#if ENABLE_RESENDINGS
static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len, uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param);
static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
static coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len);
static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handle, coap_send_msg_s *freed_send_msg_ptr);
static uint16_t sn_coap_count_linked_list_size(const coap_send_msg_list_t *linked_list_ptr);
static uint32_t sn_coap_calculate_new_resend_time(const uint32_t current_time, const uint8_t interval, const uint8_t counter);
#endif
/* * * * * * * * * * * * * * * * * */
/* * * * GLOBAL DECLARATIONS * * * */
/* * * * * * * * * * * * * * * * * */
static uint16_t message_id;
int8_t sn_coap_protocol_destroy(struct coap_s *handle)
{
if (handle == NULL) {
return -1;
}
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
sn_coap_protocol_clear_retransmission_buffer(handle);
#endif
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
ns_list_foreach_safe(coap_duplication_info_s, tmp, &handle->linked_list_duplication_msgs) {
if (tmp->coap == handle) {
if (tmp->address) {
if (tmp->address->addr_ptr) {
handle->sn_coap_protocol_free(tmp->address->addr_ptr);
tmp->address->addr_ptr = 0;
}
handle->sn_coap_protocol_free(tmp->address);
tmp->address = 0;
}
if (tmp->packet_ptr) {
handle->sn_coap_protocol_free(tmp->packet_ptr);
tmp->packet_ptr = 0;
}
ns_list_remove(&handle->linked_list_duplication_msgs, tmp);
handle->count_duplication_msgs--;
handle->sn_coap_protocol_free(tmp);
tmp = 0;
}
}
#endif
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwise is not used at all, this part of code will not be compiled */
ns_list_foreach_safe(coap_blockwise_msg_s, tmp, &handle->linked_list_blockwise_sent_msgs) {
if (tmp->coap == handle) {
if (tmp->coap_msg_ptr) {
if (tmp->coap_msg_ptr->payload_ptr) {
handle->sn_coap_protocol_free(tmp->coap_msg_ptr->payload_ptr);
tmp->coap_msg_ptr->payload_ptr = 0;
}
sn_coap_parser_release_allocated_coap_msg_mem(tmp->coap, tmp->coap_msg_ptr);
}
ns_list_remove(&handle->linked_list_blockwise_sent_msgs, tmp);
handle->sn_coap_protocol_free(tmp);
tmp = 0;
}
}
ns_list_foreach_safe(coap_blockwise_payload_s, tmp, &handle->linked_list_blockwise_received_payloads) {
if (tmp->coap == handle) {
if (tmp->addr_ptr) {
handle->sn_coap_protocol_free(tmp->addr_ptr);
tmp->addr_ptr = 0;
}
if (tmp->payload_ptr) {
handle->sn_coap_protocol_free(tmp->payload_ptr);
tmp->payload_ptr = 0;
}
ns_list_remove(&handle->linked_list_blockwise_received_payloads, tmp);
handle->sn_coap_protocol_free(tmp);
tmp = 0;
}
}
#endif
handle->sn_coap_protocol_free(handle);
handle = 0;
return 0;
}
struct coap_s *sn_coap_protocol_init(void *(*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void *),
uint8_t (*used_tx_callback_ptr)(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *),
int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *, void *param))
{
/* Check paramters */
if ((used_malloc_func_ptr == NULL) || (used_free_func_ptr == NULL) || (used_tx_callback_ptr == NULL)) {
return NULL;
}
struct coap_s *handle;
handle = used_malloc_func_ptr(sizeof(struct coap_s));
if (handle == NULL) {
return NULL;
}
memset(handle, 0, sizeof(struct coap_s));
/* * * Handle tx callback * * */
handle->sn_coap_tx_callback = used_tx_callback_ptr;
handle->sn_coap_protocol_free = used_free_func_ptr;
handle->sn_coap_protocol_malloc = used_malloc_func_ptr;
/* * * Handle rx callback * * */
/* If pointer = 0, then re-sending does not return error when failed */
handle->sn_coap_rx_callback = used_rx_callback_ptr;
// Handles internally all GET req responses
handle->sn_coap_internal_block2_resp_handling = true;
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
/* * * * Create Linked list for storing active resending messages * * * */
ns_list_init(&handle->linked_list_resent_msgs);
handle->sn_coap_resending_queue_msgs = SN_COAP_RESENDING_QUEUE_SIZE_MSGS;
handle->sn_coap_resending_queue_bytes = SN_COAP_RESENDING_QUEUE_SIZE_BYTES;
handle->sn_coap_resending_intervall = DEFAULT_RESPONSE_TIMEOUT;
handle->sn_coap_resending_count = SN_COAP_RESENDING_MAX_COUNT;
#endif /* ENABLE_RESENDINGS */
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
/* * * * Create Linked list for storing Duplication info * * * */
ns_list_init(&handle->linked_list_duplication_msgs);
handle->sn_coap_duplication_buffer_size = SN_COAP_DUPLICATION_MAX_MSGS_COUNT;
#endif
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
ns_list_init(&handle->linked_list_blockwise_sent_msgs);
ns_list_init(&handle->linked_list_blockwise_received_payloads);
handle->sn_coap_block_data_size = SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE;
#endif /* ENABLE_RESENDINGS */
/* Randomize global message ID */
randLIB_seed_random();
message_id = randLIB_get_16bit();
if (message_id == 0) {
message_id = 1;
}
return handle;
}
int8_t sn_coap_protocol_handle_block2_response_internally(struct coap_s *handle, uint8_t build_response)
{
if (handle == NULL) {
return -1;
}
handle->sn_coap_internal_block2_resp_handling = build_response;
return 0;
}
int8_t sn_coap_protocol_set_block_size(struct coap_s *handle, uint16_t block_size)
{
(void) handle;
(void) block_size;
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
if (handle == NULL) {
return -1;
}
switch (block_size) {
case 0:
case 16:
case 32:
case 64:
case 128:
case 256:
case 512:
case 1024:
handle->sn_coap_block_data_size = block_size;
return 0;
default:
break;
}
#endif
return -1;
}
void sn_coap_protocol_clear_sent_blockwise_messages(struct coap_s *handle)
{
(void) handle;
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
if (handle == NULL) {
return;
}
/* Loop all stored Blockwise messages in Linked list */
ns_list_foreach_safe(coap_blockwise_msg_s, removed_blocwise_msg_ptr, &handle->linked_list_blockwise_sent_msgs) {
sn_coap_protocol_linked_list_blockwise_msg_remove(handle, removed_blocwise_msg_ptr);
}
#endif
}
int8_t sn_coap_protocol_set_duplicate_buffer_size(struct coap_s *handle, uint8_t message_count)
{
(void) handle;
(void) message_count;
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
if (handle == NULL) {
return -1;
}
if (message_count <= SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT) {
handle->sn_coap_duplication_buffer_size = message_count;
return 0;
}
#endif
return -1;
}
int8_t sn_coap_protocol_set_retransmission_parameters(struct coap_s *handle,
uint8_t resending_count, uint8_t resending_intervall)
{
#if ENABLE_RESENDINGS
if (handle == NULL) {
return -1;
}
if (resending_count <= SN_COAP_MAX_ALLOWED_RESENDING_COUNT &&
resending_intervall <= SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT) {
handle->sn_coap_resending_count = resending_count;
if (resending_intervall == 0) {
handle->sn_coap_resending_intervall = 1;
} else {
handle->sn_coap_resending_intervall = resending_intervall;
}
return 0;
}
#endif
return -1;
}
int8_t sn_coap_protocol_set_retransmission_buffer(struct coap_s *handle,
uint8_t buffer_size_messages, uint16_t buffer_size_bytes)
{
#if ENABLE_RESENDINGS
if (handle == NULL) {
return -1;
}
if (buffer_size_bytes <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES &&
buffer_size_messages <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS ) {
handle->sn_coap_resending_queue_bytes = buffer_size_bytes;
handle->sn_coap_resending_queue_msgs = buffer_size_messages;
return 0;
}
#endif
return -1;
}
void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle)
{
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
if (handle == NULL) {
return;
}
ns_list_foreach_safe(coap_send_msg_s, tmp, &handle->linked_list_resent_msgs) {
ns_list_remove(&handle->linked_list_resent_msgs, tmp);
sn_coap_protocol_release_allocated_send_msg_mem(handle, tmp);
--handle->count_resent_msgs;
}
#endif
}
int8_t sn_coap_protocol_delete_retransmission(struct coap_s *handle, uint16_t msg_id)
{
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
if (handle == NULL) {
return -1;
}
ns_list_foreach_safe(coap_send_msg_s, tmp, &handle->linked_list_resent_msgs) {
if (tmp->send_msg_ptr && tmp->send_msg_ptr->packet_ptr ) {
uint16_t temp_msg_id = (tmp->send_msg_ptr->packet_ptr[2] << 8);
temp_msg_id += (uint16_t)tmp->send_msg_ptr->packet_ptr[3];
if(temp_msg_id == msg_id){
ns_list_remove(&handle->linked_list_resent_msgs, tmp);
--handle->count_resent_msgs;
sn_coap_protocol_release_allocated_send_msg_mem(handle, tmp);
return 0;
}
}
}
#endif
return -2;
}
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
int8_t prepare_blockwise_message(struct coap_s *handle, sn_coap_hdr_s *src_coap_msg_ptr)
{
if ((src_coap_msg_ptr->payload_len > SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE) &&
(src_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) &&
(handle->sn_coap_block_data_size > 0)) {
/* * * * Add Blockwise option to send CoAP message * * */
/* Allocate memory for less used options */
if (sn_coap_parser_alloc_options(handle, src_coap_msg_ptr) == NULL) {
tr_error("prepare_blockwise_message - failed to allocate options!");
return -2;
}
/* Check if Request message */
if (src_coap_msg_ptr->msg_code < COAP_MSG_CODE_RESPONSE_CREATED) {
/* Add Blockwise option, use Block1 because Request payload */
src_coap_msg_ptr->options_list_ptr->block1 = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
src_coap_msg_ptr->options_list_ptr->block1 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
/* Add size1 parameter */
src_coap_msg_ptr->options_list_ptr->use_size1 = true;
src_coap_msg_ptr->options_list_ptr->use_size2 = false;
src_coap_msg_ptr->options_list_ptr->size1 = src_coap_msg_ptr->payload_len;
} else { /* Response message */
/* Add Blockwise option, use Block2 because Response payload */
src_coap_msg_ptr->options_list_ptr->block2 = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
src_coap_msg_ptr->options_list_ptr->block2 |= sn_coap_convert_block_size(handle->sn_coap_block_data_size);
src_coap_msg_ptr->options_list_ptr->use_size1 = false;
src_coap_msg_ptr->options_list_ptr->use_size2 = true;
src_coap_msg_ptr->options_list_ptr->size2 = src_coap_msg_ptr->payload_len;
}
}
return 0;
}
#endif
int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr,
uint8_t *dst_packet_data_ptr, sn_coap_hdr_s *src_coap_msg_ptr, void *param)
{
int16_t byte_count_built = 0;
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
uint16_t original_payload_len = 0;
#endif
/* * * * Check given pointers * * * */
if ((dst_addr_ptr == NULL) || (dst_packet_data_ptr == NULL) || (src_coap_msg_ptr == NULL) || handle == NULL) {
return -2;
}
if (dst_addr_ptr->addr_ptr == NULL) {
return -2;
}
/* Check if built Message type is else than Acknowledgement or Reset i.e. message type is Confirmable or Non-confirmable */
/* (for Acknowledgement and Reset messages is written same Message ID than was in the Request message) */
if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT &&
src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET &&
src_coap_msg_ptr->msg_id == 0) {
/* * * * Generate new Message ID and increase it by one * * * */
src_coap_msg_ptr->msg_id = message_id;
message_id++;
if (message_id == 0) {
message_id = 1;
}
}
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
/* If blockwising needed */
if ((src_coap_msg_ptr->payload_len > SN_COAP_MAX_NONBLOCKWISE_PAYLOAD_SIZE) &&
(src_coap_msg_ptr->payload_len > handle->sn_coap_block_data_size) &&
(handle->sn_coap_block_data_size > 0)) {
/* Store original Payload length */
original_payload_len = src_coap_msg_ptr->payload_len;
/* Change Payload length of send message because Payload is blockwised */
src_coap_msg_ptr->payload_len = handle->sn_coap_block_data_size;
}
#endif
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * Build Packet data from CoAP message by using CoAP Header builder * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
byte_count_built = sn_coap_builder_2(dst_packet_data_ptr, src_coap_msg_ptr, handle->sn_coap_block_data_size);
if (byte_count_built < 0) {
tr_error("sn_coap_protocol_build - failed to build message!");
return byte_count_built;
}
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
/* Check if built Message type was confirmable, only these messages are resent */
if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
/* Store message to Linked list for resending purposes */
uint32_t resend_time = sn_coap_calculate_new_resend_time(handle->system_time, handle->sn_coap_resending_intervall, 0);
if (sn_coap_protocol_linked_list_send_msg_store(handle, dst_addr_ptr, byte_count_built, dst_packet_data_ptr,
resend_time,
param) == 0) {
return -4;
}
}
#endif /* ENABLE_RESENDINGS */
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT &&
handle->sn_coap_duplication_buffer_size != 0) {
coap_duplication_info_s* info = sn_coap_protocol_linked_list_duplication_info_search(handle,
dst_addr_ptr,
src_coap_msg_ptr->msg_id);
/* Update package data to duplication info struct if it's not there yet */
if (info && info->packet_ptr == NULL) {
info->packet_ptr = handle->sn_coap_protocol_malloc(byte_count_built);
if (info->packet_ptr) {
memcpy(info->packet_ptr, dst_packet_data_ptr, byte_count_built);
info->packet_len = byte_count_built;
} else {
tr_error("sn_coap_protocol_build - failed to allocate duplication info!");
return -4;
}
}
}
#endif
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
/* If blockwising needed */
if ((original_payload_len > handle->sn_coap_block_data_size) && (handle->sn_coap_block_data_size > 0)) {
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * Manage rest blockwise messages sending by storing them to Linked list * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
coap_blockwise_msg_s *stored_blockwise_msg_ptr;
stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
if (!stored_blockwise_msg_ptr) {
//block paylaod save failed, only first block can be build. Perhaps we should return error.
tr_error("sn_coap_protocol_build - blockwise message allocation failed!");
return byte_count_built;
}
memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
/* Fill struct */
stored_blockwise_msg_ptr->timestamp = handle->system_time;
stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr);
if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
stored_blockwise_msg_ptr = 0;
tr_error("sn_coap_protocol_build - block header copy failed!");
return -2;
}
stored_blockwise_msg_ptr->coap_msg_ptr->payload_len = original_payload_len;
stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = handle->sn_coap_protocol_malloc(stored_blockwise_msg_ptr->coap_msg_ptr->payload_len);
if (!stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr) {
//block payload save failed, only first block can be build. Perhaps we should return error.
sn_coap_parser_release_allocated_coap_msg_mem(handle, stored_blockwise_msg_ptr->coap_msg_ptr);
handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
stored_blockwise_msg_ptr = 0;
tr_error("sn_coap_protocol_build - block payload allocation failed!");
return byte_count_built;
}
memcpy(stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr, src_coap_msg_ptr->payload_ptr, stored_blockwise_msg_ptr->coap_msg_ptr->payload_len);
stored_blockwise_msg_ptr->coap = handle;
stored_blockwise_msg_ptr->param = param;
stored_blockwise_msg_ptr->msg_id = stored_blockwise_msg_ptr->coap_msg_ptr->msg_id;
ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
}
else if (src_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
/* Add message to linked list - response can be in blocks and we need header to build response.. */
coap_blockwise_msg_s *stored_blockwise_msg_ptr;
stored_blockwise_msg_ptr = handle->sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
if (!stored_blockwise_msg_ptr) {
tr_error("sn_coap_protocol_build - blockwise (GET) allocation failed!");
return byte_count_built;
}
memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
/* Fill struct */
stored_blockwise_msg_ptr->timestamp = handle->system_time;
stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(handle, src_coap_msg_ptr);
if( stored_blockwise_msg_ptr->coap_msg_ptr == NULL ){
handle->sn_coap_protocol_free(stored_blockwise_msg_ptr);
stored_blockwise_msg_ptr = 0;
tr_error("sn_coap_protocol_build - blockwise (GET) copy header failed!");
return -2;
}
stored_blockwise_msg_ptr->coap = handle;
stored_blockwise_msg_ptr->param = param;
stored_blockwise_msg_ptr->msg_id = stored_blockwise_msg_ptr->coap_msg_ptr->msg_id;
ns_list_add_to_end(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_ptr);
}
#endif /* SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
/* * * * Return built CoAP message Packet data length * * * */
return byte_count_built;
}
sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr, void *param)
{
sn_coap_hdr_s *returned_dst_coap_msg_ptr = NULL;
coap_version_e coap_version = COAP_VERSION_UNKNOWN;
/* * * * Check given pointer * * * */
if (src_addr_ptr == NULL || src_addr_ptr->addr_ptr == NULL ||
packet_data_ptr == NULL || handle == NULL) {
return NULL;
}
/* * * * Parse Packet data to CoAP message by using CoAP Header parser * * * */
returned_dst_coap_msg_ptr = sn_coap_parser(handle, packet_data_len, packet_data_ptr, &coap_version);
/* Check status of returned pointer */
if (returned_dst_coap_msg_ptr == NULL) {
/* Memory allocation error in parser */
tr_error("sn_coap_protocol_parse - allocation fail in parser!");
return NULL;
}
/* * * * Send bad request response if parsing fails * * * */
if (returned_dst_coap_msg_ptr->coap_status == COAP_STATUS_PARSER_ERROR_IN_HEADER) {
sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
tr_error("sn_coap_protocol_parse - COAP_STATUS_PARSER_ERROR_IN_HEADER");
return NULL;
}
/* * * * Check validity of parsed Header values * * * */
if (sn_coap_header_validity_check(returned_dst_coap_msg_ptr, coap_version) != 0) {
/* If message code is in a reserved class (1, 6 or 7), send reset. Message code class is 3 MSB of the message code byte */
if (((returned_dst_coap_msg_ptr->msg_code >> 5) == 1) || // if class == 1
((returned_dst_coap_msg_ptr->msg_code >> 5) == 6) || // if class == 6
((returned_dst_coap_msg_ptr->msg_code >> 5) == 7)) { // if class == 7
tr_error("sn_coap_protocol_parse - message code not valid!");
sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
}
/* Release memory of CoAP message */
sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
/* Return NULL because Header validity check failed */
return NULL;
}
/* Check if we need to send reset message */
/* A recipient MUST acknowledge a Confirmable message with an Acknowledgement
message or, if it lacks context to process the message properly
(including the case where the message is Empty, uses a code with a
reserved class (1, 6 or 7), or has a message format error), MUST
reject it; rejecting a Confirmable message is effected by sending a
matching Reset message and otherwise ignoring it. */
if (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
/* CoAP ping */
if (returned_dst_coap_msg_ptr->msg_code == COAP_MSG_CODE_EMPTY) {
sn_coap_protocol_send_rst(handle, returned_dst_coap_msg_ptr->msg_id, src_addr_ptr, param);
/* Release memory of CoAP message */
sn_coap_parser_release_allocated_coap_msg_mem(handle, returned_dst_coap_msg_ptr);
/* Return NULL because Header validity check failed */
return NULL;
}
}
#if !SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is used, this part of code will not be compiled */
/* If blockwising used in received message */
if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
(returned_dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE ||
returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) {
/* Set returned status to User */
returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED;
tr_error("sn_coap_protocol_parse - COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED!");
//todo: send response -> not implemented
return returned_dst_coap_msg_ptr;
}
#endif /* !SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE */
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT/* If Message duplication is used, this part of code will not be compiled */
/* * * * Manage received CoAP message duplicate detection * * * */
/* If no message duplication detected */
if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE ||
returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_NON_CONFIRMABLE) &&
handle->sn_coap_duplication_buffer_size != 0) {
if (sn_coap_protocol_linked_list_duplication_info_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id) == NULL) {
/* * * No Message duplication: Store received message for detecting later duplication * * */
/* Get count of stored duplication messages */
uint16_t stored_duplication_msgs_count = handle->count_duplication_msgs;
/* Check if there is no room to store message for duplication detection purposes */
if (stored_duplication_msgs_count >= handle->sn_coap_duplication_buffer_size) {
tr_debug("sn_coap_protocol_parse - duplicate list full, dropping oldest");
/* Get oldest stored duplication message */
coap_duplication_info_s *stored_duplication_info_ptr = ns_list_get_first(&handle->linked_list_duplication_msgs);
/* Remove oldest stored duplication message for getting room for new duplication message */
sn_coap_protocol_linked_list_duplication_info_remove(handle,
stored_duplication_info_ptr->address->addr_ptr,
stored_duplication_info_ptr->address->port,
stored_duplication_info_ptr->msg_id);
}
/* Store Duplication info to Linked list */
sn_coap_protocol_linked_list_duplication_info_store(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id, param);
} else { /* * * Message duplication detected * * */
/* Set returned status to User */
returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_DUPLICATED_MSG;
coap_duplication_info_s* response = sn_coap_protocol_linked_list_duplication_info_search(handle,
src_addr_ptr,
returned_dst_coap_msg_ptr->msg_id);
/* Send ACK response */
if (response) {
/* Check that response has been created */
if (response->packet_ptr) {
response->coap->sn_coap_tx_callback(response->packet_ptr,
response->packet_len, response->address, response->param);
}
}
return returned_dst_coap_msg_ptr;
}
}
#endif
/*** And here we check if message was block message ***/
/*** If so, we call own block handling function and ***/
/*** return to caller. ***/
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
(returned_dst_coap_msg_ptr->options_list_ptr->block1 != COAP_OPTION_BLOCK_NONE ||
returned_dst_coap_msg_ptr->options_list_ptr->block2 != COAP_OPTION_BLOCK_NONE)) {
returned_dst_coap_msg_ptr = sn_coap_handle_blockwise_message(handle, src_addr_ptr, returned_dst_coap_msg_ptr, param);
} else {
/* Get ... */
coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = NULL;
ns_list_foreach(coap_blockwise_msg_s, msg, &handle->linked_list_blockwise_sent_msgs) {
if (returned_dst_coap_msg_ptr->msg_id == msg->coap_msg_ptr->msg_id) {
stored_blockwise_msg_temp_ptr = msg;
break;
}
}
/* Remove from the list if not an notification message.
* Initial notification message is needed for sending rest of the blocks (GET request).
*/
bool remove_from_the_list = false;
if (stored_blockwise_msg_temp_ptr) {
if (stored_blockwise_msg_temp_ptr->coap_msg_ptr &&
stored_blockwise_msg_temp_ptr->coap_msg_ptr->options_list_ptr &&
stored_blockwise_msg_temp_ptr->coap_msg_ptr->options_list_ptr->observe != COAP_OBSERVE_NONE) {
remove_from_the_list = false;
} else {
remove_from_the_list = true;
}
}
if (remove_from_the_list) {
ns_list_remove(&handle->linked_list_blockwise_sent_msgs, stored_blockwise_msg_temp_ptr);
if (stored_blockwise_msg_temp_ptr->coap_msg_ptr) {
if(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr){
handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr);
stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = 0;
}
sn_coap_parser_release_allocated_coap_msg_mem(stored_blockwise_msg_temp_ptr->coap, stored_blockwise_msg_temp_ptr->coap_msg_ptr);
}
handle->sn_coap_protocol_free(stored_blockwise_msg_temp_ptr);
stored_blockwise_msg_temp_ptr = 0;
}
}
if (!returned_dst_coap_msg_ptr) {
tr_error("sn_coap_protocol_parse - returned_dst_coap_msg_ptr null!");
return NULL;
}
#endif
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
/* Check if received Message type was acknowledgement */
if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT) || (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET)) {
/* * * * Manage CoAP message resending by removing active resending message from Linked list * * */
/* Get node count i.e. count of active resending messages */
uint16_t stored_resending_msgs_count = handle->count_resent_msgs;
/* Check if there is ongoing active message resendings */
if (stored_resending_msgs_count > 0) {
sn_nsdl_transmit_s *removed_msg_ptr = NULL;
/* Check if received message was confirmation for some active resending message */
removed_msg_ptr = sn_coap_protocol_linked_list_send_msg_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
if (removed_msg_ptr != NULL) {
/* Remove resending message from active message resending Linked list */
sn_coap_protocol_linked_list_send_msg_remove(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
}
}
}
#endif /* ENABLE_RESENDINGS */
/* * * * Return parsed CoAP message * * * */
return returned_dst_coap_msg_ptr;
}
int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time)
{
if( !handle ){
return -1;
}
/* * * * Store current System time * * * */
handle->system_time = current_time;
#if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
/* * * * Handle block transfer timed outs * * * */
sn_coap_protocol_handle_blockwise_timout(handle);
#endif
#if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
/* * * * Remove old duplication messages * * * */
sn_coap_protocol_linked_list_duplication_info_remove_old_ones(handle);
#endif
#if ENABLE_RESENDINGS
/* Check if there is ongoing active message sendings */
/* foreach_safe isn't sufficient because callback routine could cancel messages. */
rescan:
ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
// First check that msg belongs to handle
if( stored_msg_ptr->coap == handle ){
/* Check if it is time to send this message */
if (current_time >= stored_msg_ptr->resending_time) {
/* * * Increase Resending counter * * */
stored_msg_ptr->resending_counter++;
/* Check if all re-sendings have been done */
if (stored_msg_ptr->resending_counter > handle->sn_coap_resending_count) {
coap_version_e coap_version = COAP_VERSION_UNKNOWN;
/* Get message ID from stored sending message */
uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
/* Remove message from Linked list */
ns_list_remove(&handle->linked_list_resent_msgs, stored_msg_ptr);
--handle->count_resent_msgs;
/* If RX callback have been defined.. */
if (stored_msg_ptr->coap->sn_coap_rx_callback != 0) {
sn_coap_hdr_s *tmp_coap_hdr_ptr;
/* Parse CoAP message, set status and call RX callback */
tmp_coap_hdr_ptr = sn_coap_parser(stored_msg_ptr->coap, stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->packet_ptr, &coap_version);
if (tmp_coap_hdr_ptr != 0) {
tmp_coap_hdr_ptr->coap_status = COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED;
stored_msg_ptr->coap->sn_coap_rx_callback(tmp_coap_hdr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, stored_msg_ptr->param);
sn_coap_parser_release_allocated_coap_msg_mem(stored_msg_ptr->coap, tmp_coap_hdr_ptr);
}
}
/* Free memory of stored message */
sn_coap_protocol_release_allocated_send_msg_mem(handle, stored_msg_ptr);
} else {
/* Send message */
stored_msg_ptr->coap->sn_coap_tx_callback(stored_msg_ptr->send_msg_ptr->packet_ptr,
stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->dst_addr_ptr, stored_msg_ptr->param);
/* * * Count new Resending time * * */
stored_msg_ptr->resending_time = sn_coap_calculate_new_resend_time(current_time,
handle->sn_coap_resending_intervall,
stored_msg_ptr->resending_counter);
}
/* Callback routine could have wiped the list (eg as a response to sending failed) */
/* Be super cautious and rescan from the start */
goto rescan;
}
}
}
#endif /* ENABLE_RESENDINGS */
return 0;
}
#if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
/**************************************************************************//**
* \fn static uint8_t sn_coap_protocol_linked_list_send_msg_store(sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len, uint8_t *send_packet_data_ptr, uint32_t sending_time)
*
* \brief Stores message to Linked list for sending purposes.
* \param *dst_addr_ptr is pointer to destination address where CoAP message will be sent
*
* \param send_packet_data_len is length of Packet data to be stored
*
* \param *send_packet_data_ptr is Packet data to be stored
*
* \param sending_time is stored sending time
*
* \return 0 Allocation or buffer limit reached
*
* \return 1 Msg stored properly
*****************************************************************************/
static uint8_t sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len,
uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param)
{
coap_send_msg_s *stored_msg_ptr = NULL;
/* If both queue parameters are "0" or resending count is "0", then re-sending is disabled */
if (((handle->sn_coap_resending_queue_msgs == 0) && (handle->sn_coap_resending_queue_bytes == 0)) || (handle->sn_coap_resending_count == 0)) {
return 1;
}
if (handle->sn_coap_resending_queue_msgs > 0) {
if (handle->count_resent_msgs >= handle->sn_coap_resending_queue_msgs) {
tr_error("sn_coap_protocol_linked_list_send_msg_store - resend queue full!");
return 0;
}
}
/* Count resending queue size, if buffer size is defined */
if (handle->sn_coap_resending_queue_bytes > 0) {
if ((sn_coap_count_linked_list_size(&handle->linked_list_resent_msgs) + send_packet_data_len) > handle->sn_coap_resending_queue_bytes) {
tr_error("sn_coap_protocol_linked_list_send_msg_store - resend buffer size reached!");
return 0;
}
}
/* Allocating memory for stored message */
stored_msg_ptr = sn_coap_protocol_allocate_mem_for_msg(handle, dst_addr_ptr, send_packet_data_len);
if (stored_msg_ptr == 0) {
tr_error("sn_coap_protocol_linked_list_send_msg_store - failed to allocate message!");
return 0;
}
/* Filling of coap_send_msg_s with initialization values */
stored_msg_ptr->resending_counter = 0;
stored_msg_ptr->resending_time = sending_time;
/* Filling of sn_nsdl_transmit_s */
stored_msg_ptr->send_msg_ptr->protocol = SN_NSDL_PROTOCOL_COAP;
stored_msg_ptr->send_msg_ptr->packet_len = send_packet_data_len;
memcpy(stored_msg_ptr->send_msg_ptr->packet_ptr, send_packet_data_ptr, send_packet_data_len);
/* Filling of sn_nsdl_addr_s */
stored_msg_ptr->send_msg_ptr->dst_addr_ptr->type = dst_addr_ptr->type;
stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_len = dst_addr_ptr->addr_len;
memcpy(stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_len);
stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port = dst_addr_ptr->port;
stored_msg_ptr->coap = handle;
stored_msg_ptr->param = param;
/* Storing Resending message to Linked list */
ns_list_add_to_end(&handle->linked_list_resent_msgs, stored_msg_ptr);
++handle->count_resent_msgs;
return 1;
}
/**************************************************************************//**
* \fn static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
*
* \brief Searches stored resending message from Linked list
*
* \param *src_addr_ptr is searching key for searched message
*
* \param msg_id is searching key for searched message
*
* \return Return value is pointer to found stored resending message in Linked
* list or NULL if message not found
*****************************************************************************/
static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,
sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
{
/* Loop all stored resending messages Linked list */
ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
/* Get message ID from stored resending message */
uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
/* If message's Message ID is same than is searched */
if (temp_msg_id == msg_id) {
/* If message's Source address is same than is searched */
if (0 == memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len)) {
/* If message's Source address port is same than is searched */
if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port) {
/* * * Message found, return pointer to that stored resending message * * * */
return stored_msg_ptr->send_msg_ptr;
}
}
}
}
/* Message not found */
return NULL;
}
/**************************************************************************//**
* \fn static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
*
* \brief Removes stored resending message from Linked list
*
* \param *src_addr_ptr is searching key for searched message
* \param msg_id is searching key for removed message
*****************************************************************************/
static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
{
/* Loop all stored resending messages in Linked list */
ns_list_foreach(coap_send_msg_s, stored_msg_ptr, &handle->linked_list_resent_msgs) {
/* Get message ID from stored resending message */
uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];