hello

Dependents:   nespresso_demo nespresso_endpoint EnvoyNespressoEndpointColorDetectorV2

Fork of nsdl by Robert Taylor

Committer:
bjblazkowicz
Date:
Mon Jul 14 12:33:59 2014 +0000
Revision:
2:05e4cd1a1542
Parent:
0:f6e4e1bbb3fe
WIP - registration is broken in this commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GeofferyOmlette 0:f6e4e1bbb3fe 1 /**
GeofferyOmlette 0:f6e4e1bbb3fe 2 * \file sn_coap_protocol.c
GeofferyOmlette 0:f6e4e1bbb3fe 3 *
GeofferyOmlette 0:f6e4e1bbb3fe 4 * \brief CoAP Protocol implementation
GeofferyOmlette 0:f6e4e1bbb3fe 5 *
GeofferyOmlette 0:f6e4e1bbb3fe 6 * Functionality: CoAP Protocol
GeofferyOmlette 0:f6e4e1bbb3fe 7 *
GeofferyOmlette 0:f6e4e1bbb3fe 8 * Created on: Jul 19, 2011
GeofferyOmlette 0:f6e4e1bbb3fe 9 * Author: tero
GeofferyOmlette 0:f6e4e1bbb3fe 10 *
GeofferyOmlette 0:f6e4e1bbb3fe 11 * \note Supports draft-ietf-core-coap-18
GeofferyOmlette 0:f6e4e1bbb3fe 12 */
GeofferyOmlette 0:f6e4e1bbb3fe 13
GeofferyOmlette 0:f6e4e1bbb3fe 14
GeofferyOmlette 0:f6e4e1bbb3fe 15 /* * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 16 /* * * * INCLUDE FILES * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 17 /* * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 18
GeofferyOmlette 0:f6e4e1bbb3fe 19 #include <stdio.h>
GeofferyOmlette 0:f6e4e1bbb3fe 20 #include <stdlib.h> /* For libary malloc() */
GeofferyOmlette 0:f6e4e1bbb3fe 21 #include <string.h> /* For memset() and memcpy() */
GeofferyOmlette 0:f6e4e1bbb3fe 22 #ifndef REAL_EMBEDDED
GeofferyOmlette 0:f6e4e1bbb3fe 23 #include <time.h>
GeofferyOmlette 0:f6e4e1bbb3fe 24 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 25
GeofferyOmlette 0:f6e4e1bbb3fe 26 #include "nsdl_types.h"
GeofferyOmlette 0:f6e4e1bbb3fe 27 #include "sn_nsdl.h"
GeofferyOmlette 0:f6e4e1bbb3fe 28 #include "sn_coap_header.h"
GeofferyOmlette 0:f6e4e1bbb3fe 29 #include "sn_coap_protocol.h"
GeofferyOmlette 0:f6e4e1bbb3fe 30 #include "sn_coap_header_internal.h"
GeofferyOmlette 0:f6e4e1bbb3fe 31 #include "sn_coap_protocol_internal.h"
GeofferyOmlette 0:f6e4e1bbb3fe 32 #include "sn_linked_list.h"
GeofferyOmlette 0:f6e4e1bbb3fe 33
GeofferyOmlette 0:f6e4e1bbb3fe 34 /* * * * * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 35 /* * * * LOCAL FUNCTION PROTOTYPES * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 36 /* * * * * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 37
GeofferyOmlette 0:f6e4e1bbb3fe 38
GeofferyOmlette 0:f6e4e1bbb3fe 39 static void sn_coap_protocol_linked_list_ack_info_store(uint16_t msg_id, uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 40 static int32_t sn_coap_protocol_linked_list_ack_info_search(uint16_t msg_id, uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 41 static void sn_coap_protocol_linked_list_ack_info_remove(uint16_t msg_id, sn_nsdl_addr_s *addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 42 static void sn_coap_protocol_linked_list_ack_info_remove_old_ones(void);
GeofferyOmlette 0:f6e4e1bbb3fe 43 static void sn_coap_protocol_send_rst(uint16_t msg_id, sn_nsdl_addr_s *addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 44 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 45 static void sn_coap_protocol_linked_list_duplication_info_store(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 46 static int8_t sn_coap_protocol_linked_list_duplication_info_search(sn_nsdl_addr_s *scr_addr_ptr, uint16_t msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 47 static void sn_coap_protocol_linked_list_duplication_info_remove(uint8_t *scr_addr_ptr, uint16_t port, uint16_t msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 48 static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(void);
GeofferyOmlette 0:f6e4e1bbb3fe 49 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 50 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 51 static void sn_coap_protocol_linked_list_blockwise_msg_remove_current();
GeofferyOmlette 0:f6e4e1bbb3fe 52 static void sn_coap_protocol_linked_list_blockwise_payload_store(sn_nsdl_addr_s *addr_ptr, uint16_t stored_payload_len, uint8_t *stored_payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 53 static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length);
GeofferyOmlette 0:f6e4e1bbb3fe 54 static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest();
GeofferyOmlette 0:f6e4e1bbb3fe 55 static uint16_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(sn_nsdl_addr_s *src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 56 static void sn_coap_protocol_linked_list_blockwise_remove_old_data(void);
GeofferyOmlette 0:f6e4e1bbb3fe 57 static sn_coap_hdr_s *sn_coap_handle_blockwise_message(sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 58 static int8_t sn_coap_convert_block_size(uint16_t block_size);
GeofferyOmlette 0:f6e4e1bbb3fe 59 static sn_coap_hdr_s *sn_coap_protocol_copy_header(sn_coap_hdr_s *source_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 60 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 61 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 62 static void 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);
GeofferyOmlette 0:f6e4e1bbb3fe 63 static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 64 static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 65 static coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len);
GeofferyOmlette 0:f6e4e1bbb3fe 66 static void sn_coap_protocol_release_allocated_send_msg_mem(coap_send_msg_s *freed_send_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 67 static uint16_t sn_coap_count_linked_list_size(sn_linked_list_t *linked_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 68 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 69 static void coap_protocol_free_lists(void);
GeofferyOmlette 0:f6e4e1bbb3fe 70
GeofferyOmlette 0:f6e4e1bbb3fe 71 /* * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 72 /* * * * GLOBAL DECLARATIONS * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 73 /* * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 74
GeofferyOmlette 0:f6e4e1bbb3fe 75 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 76 SN_MEM_ATTR_COAP_PROTOCOL_DECL static sn_linked_list_t *global_linked_list_resent_msgs_ptr = NULL; /* Active resending messages are stored to this Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 77 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 78 SN_MEM_ATTR_COAP_PROTOCOL_DECL static sn_linked_list_t *global_linked_list_ack_info_ptr = NULL; /* Message Acknowledgement info is stored to this Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 79 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 80 SN_MEM_ATTR_COAP_PROTOCOL_DECL static sn_linked_list_t *global_linked_list_duplication_msgs_ptr = NULL; /* Messages for duplicated messages detection is stored to this Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 81 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 82 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwise is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 83 SN_MEM_ATTR_COAP_PROTOCOL_DECL static sn_linked_list_t *global_linked_list_blockwise_sent_msgs_ptr = NULL; /* Blockwise message to to be sent is stored to this Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 84 SN_MEM_ATTR_COAP_PROTOCOL_DECL static sn_linked_list_t *global_linked_list_blockwise_received_payloads_ptr = NULL; /* Blockwise payload to to be received is stored to this Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 85 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 86
GeofferyOmlette 0:f6e4e1bbb3fe 87 SN_MEM_ATTR_COAP_PROTOCOL_DECL static uint16_t global_message_id = 100; /* Increasing Message ID which is written to CoAP message header in building */
GeofferyOmlette 0:f6e4e1bbb3fe 88 SN_MEM_ATTR_COAP_PROTOCOL_DECL static uint32_t global_system_time = 0; /* System time seconds */
GeofferyOmlette 0:f6e4e1bbb3fe 89
GeofferyOmlette 0:f6e4e1bbb3fe 90
GeofferyOmlette 0:f6e4e1bbb3fe 91 SN_MEM_ATTR_COAP_PROTOCOL_DECL uint16_t sn_coap_block_data_size = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 92
GeofferyOmlette 0:f6e4e1bbb3fe 93 SN_MEM_ATTR_COAP_PROTOCOL_DECL uint8_t sn_coap_resending_queue_msgs = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 94 SN_MEM_ATTR_COAP_PROTOCOL_DECL uint8_t sn_coap_resending_queue_bytes = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 95 SN_MEM_ATTR_COAP_PROTOCOL_DECL uint8_t sn_coap_resending_count = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 96 SN_MEM_ATTR_COAP_PROTOCOL_DECL uint8_t sn_coap_resending_intervall = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 97
GeofferyOmlette 0:f6e4e1bbb3fe 98 SN_MEM_ATTR_COAP_PROTOCOL_DECL uint8_t sn_coap_duplication_buffer_size = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 99
GeofferyOmlette 0:f6e4e1bbb3fe 100
GeofferyOmlette 0:f6e4e1bbb3fe 101 SN_MEM_ATTR_COAP_PROTOCOL_DECL static void *(*sn_coap_protocol_malloc)(uint16_t) = NULL; /* Function pointer for used malloc() function */
GeofferyOmlette 0:f6e4e1bbb3fe 102 SN_MEM_ATTR_COAP_PROTOCOL_DECL static void (*sn_coap_protocol_free)(void*) = NULL; /* Function pointer for used free() function */
GeofferyOmlette 0:f6e4e1bbb3fe 103 SN_MEM_ATTR_COAP_PROTOCOL_DECL static uint8_t (*sn_coap_tx_callback)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *) = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 104 SN_MEM_ATTR_COAP_PROTOCOL_DECL static int8_t (*sn_coap_rx_callback)(sn_coap_hdr_s *, sn_nsdl_addr_s *) = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 105
GeofferyOmlette 0:f6e4e1bbb3fe 106
GeofferyOmlette 0:f6e4e1bbb3fe 107 static uint8_t resource_path_ptr[] = {'r','d'};
GeofferyOmlette 0:f6e4e1bbb3fe 108 static uint8_t ep_name_parameter_string[] = {'h','='};
GeofferyOmlette 0:f6e4e1bbb3fe 109 static uint8_t resource_type_parameter[] = {'r','t','='};
GeofferyOmlette 0:f6e4e1bbb3fe 110
GeofferyOmlette 0:f6e4e1bbb3fe 111
GeofferyOmlette 0:f6e4e1bbb3fe 112 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 113 int8_t sn_coap_register(sn_coap_hdr_s *coap_hdr_ptr, registration_info_t *endpoint_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 114 {
GeofferyOmlette 0:f6e4e1bbb3fe 115 uint8_t *temp_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 116
GeofferyOmlette 0:f6e4e1bbb3fe 117 coap_hdr_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
GeofferyOmlette 0:f6e4e1bbb3fe 118 coap_hdr_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
GeofferyOmlette 0:f6e4e1bbb3fe 119 coap_hdr_ptr->msg_id = global_message_id++;
GeofferyOmlette 0:f6e4e1bbb3fe 120
GeofferyOmlette 0:f6e4e1bbb3fe 121 coap_hdr_ptr->uri_path_len = sizeof(resource_path_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 122 coap_hdr_ptr->uri_path_ptr = resource_path_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 123
GeofferyOmlette 0:f6e4e1bbb3fe 124 /* Payload copy */
GeofferyOmlette 0:f6e4e1bbb3fe 125 coap_hdr_ptr->payload_len = endpoint_info_ptr->links_len;
GeofferyOmlette 0:f6e4e1bbb3fe 126 coap_hdr_ptr->payload_ptr = sn_coap_protocol_malloc(coap_hdr_ptr->payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 127 if(!coap_hdr_ptr->payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 128 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 129 memcpy(coap_hdr_ptr->payload_ptr, endpoint_info_ptr->links_ptr, coap_hdr_ptr->payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 130
GeofferyOmlette 0:f6e4e1bbb3fe 131 /* Options allocation */
GeofferyOmlette 0:f6e4e1bbb3fe 132 if(!coap_hdr_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 133 {
GeofferyOmlette 0:f6e4e1bbb3fe 134 coap_hdr_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 135 if(!coap_hdr_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 136 {
GeofferyOmlette 0:f6e4e1bbb3fe 137 sn_coap_protocol_free(coap_hdr_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 138 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 139 }
GeofferyOmlette 0:f6e4e1bbb3fe 140 }
GeofferyOmlette 0:f6e4e1bbb3fe 141 memset(coap_hdr_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 142
GeofferyOmlette 0:f6e4e1bbb3fe 143 /* Uri query filling */
GeofferyOmlette 0:f6e4e1bbb3fe 144 coap_hdr_ptr->options_list_ptr->uri_query_len = sizeof(ep_name_parameter_string) + endpoint_info_ptr->endpoint_len +
GeofferyOmlette 0:f6e4e1bbb3fe 145 sizeof(resource_type_parameter) + endpoint_info_ptr->endpoint_type_len + 1; // + 1 for '&'
GeofferyOmlette 0:f6e4e1bbb3fe 146
GeofferyOmlette 0:f6e4e1bbb3fe 147 coap_hdr_ptr->options_list_ptr->uri_query_ptr = sn_coap_protocol_malloc(coap_hdr_ptr->options_list_ptr->uri_query_len);
GeofferyOmlette 0:f6e4e1bbb3fe 148 if(!coap_hdr_ptr->options_list_ptr->uri_query_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 149 {
GeofferyOmlette 0:f6e4e1bbb3fe 150 sn_coap_protocol_free(coap_hdr_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 151 sn_coap_protocol_free(coap_hdr_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 152 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 153 }
GeofferyOmlette 0:f6e4e1bbb3fe 154
GeofferyOmlette 0:f6e4e1bbb3fe 155 temp_ptr = coap_hdr_ptr->options_list_ptr->uri_query_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 156
GeofferyOmlette 0:f6e4e1bbb3fe 157 memcpy(temp_ptr, ep_name_parameter_string, sizeof(ep_name_parameter_string));
GeofferyOmlette 0:f6e4e1bbb3fe 158 temp_ptr += sizeof(ep_name_parameter_string);
GeofferyOmlette 0:f6e4e1bbb3fe 159
GeofferyOmlette 0:f6e4e1bbb3fe 160 memcpy(temp_ptr, endpoint_info_ptr->endpoint_ptr, endpoint_info_ptr->endpoint_len);
GeofferyOmlette 0:f6e4e1bbb3fe 161 temp_ptr += endpoint_info_ptr->endpoint_len;
GeofferyOmlette 0:f6e4e1bbb3fe 162
GeofferyOmlette 0:f6e4e1bbb3fe 163 *temp_ptr++ = '&';
GeofferyOmlette 0:f6e4e1bbb3fe 164
GeofferyOmlette 0:f6e4e1bbb3fe 165 memcpy(temp_ptr, resource_type_parameter, sizeof(resource_type_parameter));
GeofferyOmlette 0:f6e4e1bbb3fe 166 temp_ptr += sizeof(resource_type_parameter);
GeofferyOmlette 0:f6e4e1bbb3fe 167
GeofferyOmlette 0:f6e4e1bbb3fe 168 memcpy(temp_ptr, endpoint_info_ptr->endpoint_type_ptr, endpoint_info_ptr->endpoint_type_len);
GeofferyOmlette 0:f6e4e1bbb3fe 169 temp_ptr += endpoint_info_ptr->endpoint_type_len;
GeofferyOmlette 0:f6e4e1bbb3fe 170
GeofferyOmlette 0:f6e4e1bbb3fe 171 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 172 }
GeofferyOmlette 0:f6e4e1bbb3fe 173
GeofferyOmlette 0:f6e4e1bbb3fe 174
GeofferyOmlette 0:f6e4e1bbb3fe 175 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 176 int8_t sn_coap_register_update(sn_coap_hdr_s *coap_hdr_ptr, uint8_t *location, uint8_t length)
GeofferyOmlette 0:f6e4e1bbb3fe 177 {
GeofferyOmlette 0:f6e4e1bbb3fe 178 coap_hdr_ptr->msg_code = COAP_MSG_CODE_REQUEST_PUT;
GeofferyOmlette 0:f6e4e1bbb3fe 179 coap_hdr_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
GeofferyOmlette 0:f6e4e1bbb3fe 180 coap_hdr_ptr->msg_id = global_message_id++;
GeofferyOmlette 0:f6e4e1bbb3fe 181 coap_hdr_ptr->uri_path_len = length;
GeofferyOmlette 0:f6e4e1bbb3fe 182 coap_hdr_ptr->uri_path_ptr = location;
GeofferyOmlette 0:f6e4e1bbb3fe 183
GeofferyOmlette 0:f6e4e1bbb3fe 184 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 185 }
GeofferyOmlette 0:f6e4e1bbb3fe 186
GeofferyOmlette 0:f6e4e1bbb3fe 187
GeofferyOmlette 0:f6e4e1bbb3fe 188 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 189 int8_t sn_coap_deregister(sn_coap_hdr_s *coap_hdr_ptr, uint8_t *location, uint8_t length)
GeofferyOmlette 0:f6e4e1bbb3fe 190 {
GeofferyOmlette 0:f6e4e1bbb3fe 191 coap_hdr_ptr->msg_code = COAP_MSG_CODE_REQUEST_DELETE;
GeofferyOmlette 0:f6e4e1bbb3fe 192 coap_hdr_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
GeofferyOmlette 0:f6e4e1bbb3fe 193 coap_hdr_ptr->msg_id = global_message_id++;
GeofferyOmlette 0:f6e4e1bbb3fe 194 coap_hdr_ptr->uri_path_len = length;
GeofferyOmlette 0:f6e4e1bbb3fe 195 coap_hdr_ptr->uri_path_ptr = location;
GeofferyOmlette 0:f6e4e1bbb3fe 196
GeofferyOmlette 0:f6e4e1bbb3fe 197 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 198 }
GeofferyOmlette 0:f6e4e1bbb3fe 199
GeofferyOmlette 0:f6e4e1bbb3fe 200
GeofferyOmlette 0:f6e4e1bbb3fe 201 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 202 int8_t sn_coap_protocol_destroy(void)
GeofferyOmlette 0:f6e4e1bbb3fe 203 {
GeofferyOmlette 0:f6e4e1bbb3fe 204 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 205
GeofferyOmlette 0:f6e4e1bbb3fe 206 sn_coap_protocol_clear_retransmission_buffer();
GeofferyOmlette 0:f6e4e1bbb3fe 207 sn_coap_protocol_free(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 208 global_linked_list_resent_msgs_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 209
GeofferyOmlette 0:f6e4e1bbb3fe 210 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 211
GeofferyOmlette 0:f6e4e1bbb3fe 212 if(global_linked_list_ack_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 213 {
GeofferyOmlette 0:f6e4e1bbb3fe 214 uint16_t size = sn_linked_list_count_nodes(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 215 uint16_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 216 coap_ack_info_s*tmp;
GeofferyOmlette 0:f6e4e1bbb3fe 217
GeofferyOmlette 0:f6e4e1bbb3fe 218 for(i=0;i<size;i++)
GeofferyOmlette 0:f6e4e1bbb3fe 219 {
GeofferyOmlette 0:f6e4e1bbb3fe 220 tmp = sn_linked_list_get_first_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 221
GeofferyOmlette 0:f6e4e1bbb3fe 222 if(tmp)
GeofferyOmlette 0:f6e4e1bbb3fe 223 {
GeofferyOmlette 0:f6e4e1bbb3fe 224 if(tmp->token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 225 {
GeofferyOmlette 0:f6e4e1bbb3fe 226 sn_coap_protocol_free(tmp->token_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 227 tmp->token_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 228 }
GeofferyOmlette 0:f6e4e1bbb3fe 229 if(tmp->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 230 {
GeofferyOmlette 0:f6e4e1bbb3fe 231 sn_coap_protocol_free(tmp->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 232 tmp->addr_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 233 }
GeofferyOmlette 0:f6e4e1bbb3fe 234 sn_linked_list_remove_current_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 235 sn_coap_protocol_free(tmp);
GeofferyOmlette 0:f6e4e1bbb3fe 236 tmp = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 237 }
GeofferyOmlette 0:f6e4e1bbb3fe 238 }
GeofferyOmlette 0:f6e4e1bbb3fe 239
GeofferyOmlette 0:f6e4e1bbb3fe 240 if(!sn_linked_list_count_nodes(global_linked_list_ack_info_ptr))
GeofferyOmlette 0:f6e4e1bbb3fe 241 {
GeofferyOmlette 0:f6e4e1bbb3fe 242 sn_coap_protocol_free(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 243 global_linked_list_ack_info_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 244 }
GeofferyOmlette 0:f6e4e1bbb3fe 245 }
GeofferyOmlette 0:f6e4e1bbb3fe 246
GeofferyOmlette 0:f6e4e1bbb3fe 247 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 248 if(global_linked_list_duplication_msgs_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 249 {
GeofferyOmlette 0:f6e4e1bbb3fe 250
GeofferyOmlette 0:f6e4e1bbb3fe 251 }
GeofferyOmlette 0:f6e4e1bbb3fe 252 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 253
GeofferyOmlette 0:f6e4e1bbb3fe 254 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwise is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 255 if(global_linked_list_blockwise_sent_msgs_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 256 {
GeofferyOmlette 0:f6e4e1bbb3fe 257
GeofferyOmlette 0:f6e4e1bbb3fe 258 }
GeofferyOmlette 0:f6e4e1bbb3fe 259 if(global_linked_list_blockwise_received_payloads_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 260 {
GeofferyOmlette 0:f6e4e1bbb3fe 261
GeofferyOmlette 0:f6e4e1bbb3fe 262 }
GeofferyOmlette 0:f6e4e1bbb3fe 263 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 264 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 265 }
GeofferyOmlette 0:f6e4e1bbb3fe 266
GeofferyOmlette 0:f6e4e1bbb3fe 267
GeofferyOmlette 0:f6e4e1bbb3fe 268 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 269 void coap_protocol_free_lists(void)
GeofferyOmlette 0:f6e4e1bbb3fe 270 {
GeofferyOmlette 0:f6e4e1bbb3fe 271 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 272 if(NULL != global_linked_list_resent_msgs_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 273 {
GeofferyOmlette 0:f6e4e1bbb3fe 274 sn_linked_list_free(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 275 global_linked_list_resent_msgs_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 276 }
GeofferyOmlette 0:f6e4e1bbb3fe 277 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 278 if(NULL != global_linked_list_ack_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 279 {
GeofferyOmlette 0:f6e4e1bbb3fe 280 sn_linked_list_free(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 281 global_linked_list_ack_info_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 282 }
GeofferyOmlette 0:f6e4e1bbb3fe 283 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
GeofferyOmlette 0:f6e4e1bbb3fe 284 if(NULL != global_linked_list_duplication_msgs_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 285 {
GeofferyOmlette 0:f6e4e1bbb3fe 286 sn_linked_list_free(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 287 global_linked_list_duplication_msgs_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 288 }
GeofferyOmlette 0:f6e4e1bbb3fe 289 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 290 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE
GeofferyOmlette 0:f6e4e1bbb3fe 291 if(NULL != global_linked_list_blockwise_sent_msgs_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 292 {
GeofferyOmlette 0:f6e4e1bbb3fe 293 sn_linked_list_free(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 294 global_linked_list_blockwise_sent_msgs_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 295 }
GeofferyOmlette 0:f6e4e1bbb3fe 296 if(NULL != global_linked_list_blockwise_received_payloads_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 297 {
GeofferyOmlette 0:f6e4e1bbb3fe 298 sn_linked_list_free(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 299 global_linked_list_blockwise_received_payloads_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 300 }
GeofferyOmlette 0:f6e4e1bbb3fe 301 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 302
GeofferyOmlette 0:f6e4e1bbb3fe 303 }
GeofferyOmlette 0:f6e4e1bbb3fe 304
GeofferyOmlette 0:f6e4e1bbb3fe 305 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 306 int8_t sn_coap_protocol_init(void* (*used_malloc_func_ptr)(uint16_t), void (*used_free_func_ptr)(void*),
GeofferyOmlette 0:f6e4e1bbb3fe 307 uint8_t (*used_tx_callback_ptr)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *),
GeofferyOmlette 0:f6e4e1bbb3fe 308 int8_t (*used_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *))
GeofferyOmlette 0:f6e4e1bbb3fe 309 {
GeofferyOmlette 0:f6e4e1bbb3fe 310 /* * * Handling malloc() * * */
GeofferyOmlette 0:f6e4e1bbb3fe 311 if (used_malloc_func_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 312 sn_coap_protocol_malloc = used_malloc_func_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 313 else
GeofferyOmlette 0:f6e4e1bbb3fe 314 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 315
GeofferyOmlette 0:f6e4e1bbb3fe 316 /* * * Handling free() * * */
GeofferyOmlette 0:f6e4e1bbb3fe 317 if (used_free_func_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 318 sn_coap_protocol_free = used_free_func_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 319 else
GeofferyOmlette 0:f6e4e1bbb3fe 320 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 321
GeofferyOmlette 0:f6e4e1bbb3fe 322 /* * * Handle tx callback * * */
GeofferyOmlette 0:f6e4e1bbb3fe 323 if(used_tx_callback_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 324 sn_coap_tx_callback = used_tx_callback_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 325 else
GeofferyOmlette 0:f6e4e1bbb3fe 326 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 327
GeofferyOmlette 0:f6e4e1bbb3fe 328 /* * * Handle rx callback * * */
GeofferyOmlette 0:f6e4e1bbb3fe 329 /* If pointer = 0, then re-sending does not return error when failed */
GeofferyOmlette 0:f6e4e1bbb3fe 330 sn_coap_rx_callback = used_rx_callback_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 331
GeofferyOmlette 0:f6e4e1bbb3fe 332 sn_linked_list_init(sn_coap_protocol_malloc, sn_coap_protocol_free);
GeofferyOmlette 0:f6e4e1bbb3fe 333
GeofferyOmlette 0:f6e4e1bbb3fe 334 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 335
GeofferyOmlette 0:f6e4e1bbb3fe 336 /* * * * Create Linked list for storing active resending messages * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 337 sn_coap_resending_queue_msgs = SN_COAP_RESENDING_QUEUE_SIZE_MSGS;
GeofferyOmlette 0:f6e4e1bbb3fe 338 sn_coap_resending_queue_bytes = SN_COAP_RESENDING_QUEUE_SIZE_BYTES;
GeofferyOmlette 0:f6e4e1bbb3fe 339 sn_coap_resending_intervall = DEFAULT_RESPONSE_TIMEOUT;
GeofferyOmlette 0:f6e4e1bbb3fe 340 sn_coap_resending_count = SN_COAP_RESENDING_MAX_COUNT;
GeofferyOmlette 0:f6e4e1bbb3fe 341
GeofferyOmlette 0:f6e4e1bbb3fe 342 /* Check that Linked list is not already created */
GeofferyOmlette 0:f6e4e1bbb3fe 343 if (global_linked_list_resent_msgs_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 344 {
GeofferyOmlette 0:f6e4e1bbb3fe 345 global_linked_list_resent_msgs_ptr = sn_linked_list_create();
GeofferyOmlette 0:f6e4e1bbb3fe 346
GeofferyOmlette 0:f6e4e1bbb3fe 347 if(global_linked_list_resent_msgs_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 348 {
GeofferyOmlette 0:f6e4e1bbb3fe 349 coap_protocol_free_lists();
GeofferyOmlette 0:f6e4e1bbb3fe 350 return (-1);
GeofferyOmlette 0:f6e4e1bbb3fe 351 }
GeofferyOmlette 0:f6e4e1bbb3fe 352
GeofferyOmlette 0:f6e4e1bbb3fe 353 }
GeofferyOmlette 0:f6e4e1bbb3fe 354
GeofferyOmlette 0:f6e4e1bbb3fe 355 #endif /* ENABLE_RESENDINGS */
GeofferyOmlette 0:f6e4e1bbb3fe 356
GeofferyOmlette 0:f6e4e1bbb3fe 357 /* * * * Create Linked list for storing Acknowledgement info, if not already created * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 358 if (global_linked_list_ack_info_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 359 {
GeofferyOmlette 0:f6e4e1bbb3fe 360 global_linked_list_ack_info_ptr = sn_linked_list_create();
GeofferyOmlette 0:f6e4e1bbb3fe 361
GeofferyOmlette 0:f6e4e1bbb3fe 362 if(global_linked_list_ack_info_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 363 {
GeofferyOmlette 0:f6e4e1bbb3fe 364 coap_protocol_free_lists();
GeofferyOmlette 0:f6e4e1bbb3fe 365 return (-1);
GeofferyOmlette 0:f6e4e1bbb3fe 366 }
GeofferyOmlette 0:f6e4e1bbb3fe 367
GeofferyOmlette 0:f6e4e1bbb3fe 368 }
GeofferyOmlette 0:f6e4e1bbb3fe 369
GeofferyOmlette 0:f6e4e1bbb3fe 370 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 371 /* * * * Create Linked list for storing Duplication info * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 372 sn_coap_duplication_buffer_size = SN_COAP_DUPLICATION_MAX_MSGS_COUNT;
GeofferyOmlette 0:f6e4e1bbb3fe 373
GeofferyOmlette 0:f6e4e1bbb3fe 374 /* Check that Linked list is not already created */
GeofferyOmlette 0:f6e4e1bbb3fe 375 if (global_linked_list_duplication_msgs_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 376 {
GeofferyOmlette 0:f6e4e1bbb3fe 377 global_linked_list_duplication_msgs_ptr = sn_linked_list_create();
GeofferyOmlette 0:f6e4e1bbb3fe 378
GeofferyOmlette 0:f6e4e1bbb3fe 379 if(global_linked_list_duplication_msgs_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 380 {
GeofferyOmlette 0:f6e4e1bbb3fe 381 coap_protocol_free_lists();
GeofferyOmlette 0:f6e4e1bbb3fe 382 return (-1);
GeofferyOmlette 0:f6e4e1bbb3fe 383 }
GeofferyOmlette 0:f6e4e1bbb3fe 384 }
GeofferyOmlette 0:f6e4e1bbb3fe 385 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 386
GeofferyOmlette 0:f6e4e1bbb3fe 387 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 388
GeofferyOmlette 0:f6e4e1bbb3fe 389 sn_coap_block_data_size = SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE;
GeofferyOmlette 0:f6e4e1bbb3fe 390
GeofferyOmlette 0:f6e4e1bbb3fe 391 /* * * * Create Linked list for storing sent blockwise messages, if not already created * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 392 if (global_linked_list_blockwise_sent_msgs_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 393 {
GeofferyOmlette 0:f6e4e1bbb3fe 394 global_linked_list_blockwise_sent_msgs_ptr = sn_linked_list_create();
GeofferyOmlette 0:f6e4e1bbb3fe 395
GeofferyOmlette 0:f6e4e1bbb3fe 396 if(global_linked_list_blockwise_sent_msgs_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 397 {
GeofferyOmlette 0:f6e4e1bbb3fe 398 coap_protocol_free_lists();
GeofferyOmlette 0:f6e4e1bbb3fe 399 return (-1);
GeofferyOmlette 0:f6e4e1bbb3fe 400 }
GeofferyOmlette 0:f6e4e1bbb3fe 401
GeofferyOmlette 0:f6e4e1bbb3fe 402 }
GeofferyOmlette 0:f6e4e1bbb3fe 403
GeofferyOmlette 0:f6e4e1bbb3fe 404 /* * * * Create Linked list for storing received blockwise payload, if not already created * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 405 if (global_linked_list_blockwise_received_payloads_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 406 {
GeofferyOmlette 0:f6e4e1bbb3fe 407 global_linked_list_blockwise_received_payloads_ptr = sn_linked_list_create();
GeofferyOmlette 0:f6e4e1bbb3fe 408
GeofferyOmlette 0:f6e4e1bbb3fe 409 if(global_linked_list_blockwise_received_payloads_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 410 {
GeofferyOmlette 0:f6e4e1bbb3fe 411 coap_protocol_free_lists();
GeofferyOmlette 0:f6e4e1bbb3fe 412 return (-1);
GeofferyOmlette 0:f6e4e1bbb3fe 413 }
GeofferyOmlette 0:f6e4e1bbb3fe 414 }
GeofferyOmlette 0:f6e4e1bbb3fe 415 #endif /* ENABLE_RESENDINGS */
GeofferyOmlette 0:f6e4e1bbb3fe 416
GeofferyOmlette 0:f6e4e1bbb3fe 417 /* Randomize global message ID */
GeofferyOmlette 0:f6e4e1bbb3fe 418 #ifndef REAL_EMBEDDED
GeofferyOmlette 0:f6e4e1bbb3fe 419 srand(time(NULL));
GeofferyOmlette 0:f6e4e1bbb3fe 420 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 421 {
GeofferyOmlette 0:f6e4e1bbb3fe 422 uint8_t random_number = rand();
GeofferyOmlette 0:f6e4e1bbb3fe 423 global_message_id = 100 + random_number;
GeofferyOmlette 0:f6e4e1bbb3fe 424 }
GeofferyOmlette 0:f6e4e1bbb3fe 425
GeofferyOmlette 0:f6e4e1bbb3fe 426 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 427 }
GeofferyOmlette 0:f6e4e1bbb3fe 428
GeofferyOmlette 0:f6e4e1bbb3fe 429
GeofferyOmlette 0:f6e4e1bbb3fe 430 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 431 int8_t sn_coap_protocol_set_block_size(uint16_t block_size)
GeofferyOmlette 0:f6e4e1bbb3fe 432 {
GeofferyOmlette 0:f6e4e1bbb3fe 433 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE
GeofferyOmlette 0:f6e4e1bbb3fe 434 switch(block_size)
GeofferyOmlette 0:f6e4e1bbb3fe 435 {
GeofferyOmlette 0:f6e4e1bbb3fe 436 case 0:
GeofferyOmlette 0:f6e4e1bbb3fe 437 case 16:
GeofferyOmlette 0:f6e4e1bbb3fe 438 case 32:
GeofferyOmlette 0:f6e4e1bbb3fe 439 case 64:
GeofferyOmlette 0:f6e4e1bbb3fe 440 case 128:
GeofferyOmlette 0:f6e4e1bbb3fe 441 case 256:
GeofferyOmlette 0:f6e4e1bbb3fe 442 case 512:
GeofferyOmlette 0:f6e4e1bbb3fe 443 case 1024:
GeofferyOmlette 0:f6e4e1bbb3fe 444 sn_coap_block_data_size = block_size;
GeofferyOmlette 0:f6e4e1bbb3fe 445 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 446 default:
GeofferyOmlette 0:f6e4e1bbb3fe 447 break;
GeofferyOmlette 0:f6e4e1bbb3fe 448 }
GeofferyOmlette 0:f6e4e1bbb3fe 449 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 450 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 451
GeofferyOmlette 0:f6e4e1bbb3fe 452 }
GeofferyOmlette 0:f6e4e1bbb3fe 453
GeofferyOmlette 0:f6e4e1bbb3fe 454
GeofferyOmlette 0:f6e4e1bbb3fe 455 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 456 int8_t sn_coap_protocol_set_duplicate_buffer_size(uint8_t message_count)
GeofferyOmlette 0:f6e4e1bbb3fe 457 {
GeofferyOmlette 0:f6e4e1bbb3fe 458 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
GeofferyOmlette 0:f6e4e1bbb3fe 459 if(message_count <= SN_COAP_MAX_ALLOWED_DUPLICATION_MESSAGE_COUNT)
GeofferyOmlette 0:f6e4e1bbb3fe 460 {
GeofferyOmlette 0:f6e4e1bbb3fe 461 sn_coap_duplication_buffer_size = message_count;
GeofferyOmlette 0:f6e4e1bbb3fe 462 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 463 }
GeofferyOmlette 0:f6e4e1bbb3fe 464 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 465 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 466 }
GeofferyOmlette 0:f6e4e1bbb3fe 467
GeofferyOmlette 0:f6e4e1bbb3fe 468
GeofferyOmlette 0:f6e4e1bbb3fe 469 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 470 int8_t sn_coap_protocol_set_retransmission_parameters(uint8_t resending_count, uint8_t resending_intervall)
GeofferyOmlette 0:f6e4e1bbb3fe 471 {
GeofferyOmlette 0:f6e4e1bbb3fe 472 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 473 if(resending_count <= SN_COAP_MAX_ALLOWED_RESENDING_COUNT &&
GeofferyOmlette 0:f6e4e1bbb3fe 474 resending_intervall <= SN_COAP_MAX_ALLOWED_RESPONSE_TIMEOUT)
GeofferyOmlette 0:f6e4e1bbb3fe 475 {
GeofferyOmlette 0:f6e4e1bbb3fe 476 sn_coap_resending_count = resending_count;
GeofferyOmlette 0:f6e4e1bbb3fe 477
GeofferyOmlette 0:f6e4e1bbb3fe 478 if(resending_intervall == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 479 sn_coap_resending_intervall = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 480 else
GeofferyOmlette 0:f6e4e1bbb3fe 481 sn_coap_resending_intervall = resending_intervall;
GeofferyOmlette 0:f6e4e1bbb3fe 482 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 483 }
GeofferyOmlette 0:f6e4e1bbb3fe 484 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 485 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 486 }
GeofferyOmlette 0:f6e4e1bbb3fe 487
GeofferyOmlette 0:f6e4e1bbb3fe 488
GeofferyOmlette 0:f6e4e1bbb3fe 489 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 490 int8_t sn_coap_protocol_set_retransmission_buffer(uint8_t buffer_size_messages, uint16_t buffer_size_bytes)
GeofferyOmlette 0:f6e4e1bbb3fe 491 {
GeofferyOmlette 0:f6e4e1bbb3fe 492 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 493 if(buffer_size_bytes <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_BYTES)
GeofferyOmlette 0:f6e4e1bbb3fe 494 sn_coap_resending_queue_bytes = buffer_size_bytes;
GeofferyOmlette 0:f6e4e1bbb3fe 495 if(buffer_size_bytes <= SN_COAP_MAX_ALLOWED_RESENDING_BUFF_SIZE_MSGS)
GeofferyOmlette 0:f6e4e1bbb3fe 496 sn_coap_resending_queue_msgs = buffer_size_messages;
GeofferyOmlette 0:f6e4e1bbb3fe 497
GeofferyOmlette 0:f6e4e1bbb3fe 498 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 499 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 500
GeofferyOmlette 0:f6e4e1bbb3fe 501 }
GeofferyOmlette 0:f6e4e1bbb3fe 502
GeofferyOmlette 0:f6e4e1bbb3fe 503 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 504 void sn_coap_protocol_clear_retransmission_buffer(void)
GeofferyOmlette 0:f6e4e1bbb3fe 505 {
GeofferyOmlette 0:f6e4e1bbb3fe 506 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 507 if(global_linked_list_resent_msgs_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 508 {
GeofferyOmlette 0:f6e4e1bbb3fe 509 uint16_t size = sn_linked_list_count_nodes(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 510 uint16_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 511 coap_send_msg_s*tmp;
GeofferyOmlette 0:f6e4e1bbb3fe 512 for(i=0;i<size;i++)
GeofferyOmlette 0:f6e4e1bbb3fe 513 {
GeofferyOmlette 0:f6e4e1bbb3fe 514 tmp = sn_linked_list_get_first_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 515
GeofferyOmlette 0:f6e4e1bbb3fe 516 if(tmp)
GeofferyOmlette 0:f6e4e1bbb3fe 517 {
GeofferyOmlette 0:f6e4e1bbb3fe 518 if(tmp->send_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 519 {
GeofferyOmlette 0:f6e4e1bbb3fe 520 if(tmp->send_msg_ptr->dst_addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 521 {
GeofferyOmlette 0:f6e4e1bbb3fe 522 if(tmp->send_msg_ptr->dst_addr_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 523 {
GeofferyOmlette 0:f6e4e1bbb3fe 524 sn_coap_protocol_free(tmp->send_msg_ptr->dst_addr_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 525 tmp->send_msg_ptr->dst_addr_ptr->addr_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 526 }
GeofferyOmlette 0:f6e4e1bbb3fe 527 if(tmp->send_msg_ptr->dst_addr_ptr->socket_information)
GeofferyOmlette 0:f6e4e1bbb3fe 528 {
GeofferyOmlette 0:f6e4e1bbb3fe 529 sn_coap_protocol_free(tmp->send_msg_ptr->dst_addr_ptr->socket_information);
GeofferyOmlette 0:f6e4e1bbb3fe 530 tmp->send_msg_ptr->dst_addr_ptr->socket_information = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 531 }
GeofferyOmlette 0:f6e4e1bbb3fe 532 sn_coap_protocol_free(tmp->send_msg_ptr->dst_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 533 tmp->send_msg_ptr->dst_addr_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 534 }
GeofferyOmlette 0:f6e4e1bbb3fe 535 if(tmp->send_msg_ptr->packet_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 536 {
GeofferyOmlette 0:f6e4e1bbb3fe 537 sn_coap_protocol_free(tmp->send_msg_ptr->packet_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 538 tmp->send_msg_ptr->packet_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 539 }
GeofferyOmlette 0:f6e4e1bbb3fe 540 sn_coap_protocol_free(tmp->send_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 541 tmp->send_msg_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 542 }
GeofferyOmlette 0:f6e4e1bbb3fe 543 sn_linked_list_remove_current_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 544 sn_coap_protocol_free(tmp);
GeofferyOmlette 0:f6e4e1bbb3fe 545 tmp = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 546 }
GeofferyOmlette 0:f6e4e1bbb3fe 547 }
GeofferyOmlette 0:f6e4e1bbb3fe 548 }
GeofferyOmlette 0:f6e4e1bbb3fe 549 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 550
GeofferyOmlette 0:f6e4e1bbb3fe 551 }
GeofferyOmlette 0:f6e4e1bbb3fe 552
GeofferyOmlette 0:f6e4e1bbb3fe 553
GeofferyOmlette 0:f6e4e1bbb3fe 554 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 555 int16_t sn_coap_protocol_build(sn_nsdl_addr_s *dst_addr_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 556 uint8_t *dst_packet_data_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 557 sn_coap_hdr_s *src_coap_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 558 {
GeofferyOmlette 0:f6e4e1bbb3fe 559 int32_t message_id = -3;
GeofferyOmlette 0:f6e4e1bbb3fe 560 int16_t byte_count_built = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 561 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 562 uint16_t original_payload_len = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 563 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 564
GeofferyOmlette 0:f6e4e1bbb3fe 565 /* * * * Check given pointers * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 566 if ((dst_addr_ptr == NULL) || (dst_packet_data_ptr == NULL) || (src_coap_msg_ptr == NULL))
GeofferyOmlette 0:f6e4e1bbb3fe 567 return -2;
GeofferyOmlette 0:f6e4e1bbb3fe 568
GeofferyOmlette 0:f6e4e1bbb3fe 569 if(dst_addr_ptr->addr_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 570 return -2;
GeofferyOmlette 0:f6e4e1bbb3fe 571
GeofferyOmlette 0:f6e4e1bbb3fe 572 /* Check if built Message type is Reset message or Message code is some of response messages or empty */
GeofferyOmlette 0:f6e4e1bbb3fe 573 /* (for these messages CoAP writes same Message ID which was stored earlier from request message) */
GeofferyOmlette 0:f6e4e1bbb3fe 574 if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET ||
GeofferyOmlette 0:f6e4e1bbb3fe 575 (src_coap_msg_ptr->msg_code >= COAP_MSG_CODE_RESPONSE_CREATED || src_coap_msg_ptr->msg_code == COAP_MSG_CODE_EMPTY))
GeofferyOmlette 0:f6e4e1bbb3fe 576 {
GeofferyOmlette 0:f6e4e1bbb3fe 577 /* Check if there is Token option in built CoAP message */
GeofferyOmlette 0:f6e4e1bbb3fe 578 /* (only these messages can be acknowledged because Token option is used as key for stored messages) */
GeofferyOmlette 0:f6e4e1bbb3fe 579 if (src_coap_msg_ptr->token_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 580 {
GeofferyOmlette 0:f6e4e1bbb3fe 581 /* Search Message ID from Linked list with Token option and Address as key */
GeofferyOmlette 0:f6e4e1bbb3fe 582 message_id = sn_coap_protocol_linked_list_ack_info_search(src_coap_msg_ptr->msg_id, src_coap_msg_ptr->token_len,
GeofferyOmlette 0:f6e4e1bbb3fe 583 src_coap_msg_ptr->token_ptr, dst_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 584 }
GeofferyOmlette 0:f6e4e1bbb3fe 585 else
GeofferyOmlette 0:f6e4e1bbb3fe 586 {
GeofferyOmlette 0:f6e4e1bbb3fe 587 message_id = sn_coap_protocol_linked_list_ack_info_search(src_coap_msg_ptr->msg_id, 0, NULL, dst_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 588 }
GeofferyOmlette 0:f6e4e1bbb3fe 589
GeofferyOmlette 0:f6e4e1bbb3fe 590 /* If Message ID found */
GeofferyOmlette 0:f6e4e1bbb3fe 591 if (message_id >= 0)
GeofferyOmlette 0:f6e4e1bbb3fe 592 {
GeofferyOmlette 0:f6e4e1bbb3fe 593 /* * * * Manage received CoAP message acknowledgement * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 594 /* Piggy-backed response message found */
GeofferyOmlette 0:f6e4e1bbb3fe 595
GeofferyOmlette 0:f6e4e1bbb3fe 596 /* Client Server */
GeofferyOmlette 0:f6e4e1bbb3fe 597
GeofferyOmlette 0:f6e4e1bbb3fe 598 /* ------------------> Confirmable request message (CoAP stores Acknowledgement info to Linked list) */
GeofferyOmlette 0:f6e4e1bbb3fe 599
GeofferyOmlette 0:f6e4e1bbb3fe 600 /* <------------------ THIS IS DONE HERE: Piggy-backed acknowledgement response message (CoAP writes same
GeofferyOmlette 0:f6e4e1bbb3fe 601 Message ID than was in Request message).
GeofferyOmlette 0:f6e4e1bbb3fe 602 User has written correct Token option to the response message. */
GeofferyOmlette 0:f6e4e1bbb3fe 603
GeofferyOmlette 0:f6e4e1bbb3fe 604 if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET)
GeofferyOmlette 0:f6e4e1bbb3fe 605 {
GeofferyOmlette 0:f6e4e1bbb3fe 606 /* Now is built Piggy-backed Acknowledgement response message */
GeofferyOmlette 0:f6e4e1bbb3fe 607 src_coap_msg_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
GeofferyOmlette 0:f6e4e1bbb3fe 608 }
GeofferyOmlette 0:f6e4e1bbb3fe 609
GeofferyOmlette 0:f6e4e1bbb3fe 610 /* Remove Acknowledgement info from Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 611 sn_coap_protocol_linked_list_ack_info_remove(src_coap_msg_ptr->msg_id, dst_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 612 }
GeofferyOmlette 0:f6e4e1bbb3fe 613 else if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET)
GeofferyOmlette 0:f6e4e1bbb3fe 614 {
GeofferyOmlette 0:f6e4e1bbb3fe 615 /* There was not found Message ID for Reset message */
GeofferyOmlette 0:f6e4e1bbb3fe 616 return -3;
GeofferyOmlette 0:f6e4e1bbb3fe 617 }
GeofferyOmlette 0:f6e4e1bbb3fe 618 }
GeofferyOmlette 0:f6e4e1bbb3fe 619
GeofferyOmlette 0:f6e4e1bbb3fe 620 /* Check if built Message type is else than Acknowledgement or Reset i.e. message type is Confirmable or Non-confirmable */
GeofferyOmlette 0:f6e4e1bbb3fe 621 /* (for Acknowledgement and Reset messages is written same Message ID than was in the Request message) */
GeofferyOmlette 0:f6e4e1bbb3fe 622 if (src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT &&
GeofferyOmlette 0:f6e4e1bbb3fe 623 src_coap_msg_ptr->msg_type != COAP_MSG_TYPE_RESET &&
GeofferyOmlette 0:f6e4e1bbb3fe 624 src_coap_msg_ptr->msg_id == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 625 {
GeofferyOmlette 0:f6e4e1bbb3fe 626 /* * * * Generate new Message ID and increase it by one * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 627 if(0 > message_id)
GeofferyOmlette 0:f6e4e1bbb3fe 628 {
GeofferyOmlette 0:f6e4e1bbb3fe 629 src_coap_msg_ptr->msg_id = global_message_id;
GeofferyOmlette 0:f6e4e1bbb3fe 630 global_message_id++;
GeofferyOmlette 0:f6e4e1bbb3fe 631 }
GeofferyOmlette 0:f6e4e1bbb3fe 632 }
GeofferyOmlette 0:f6e4e1bbb3fe 633
GeofferyOmlette 0:f6e4e1bbb3fe 634 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 635
GeofferyOmlette 0:f6e4e1bbb3fe 636 /* If blockwising needed */
GeofferyOmlette 0:f6e4e1bbb3fe 637 if ((src_coap_msg_ptr->payload_len > sn_coap_block_data_size) && (sn_coap_block_data_size > 0))
GeofferyOmlette 0:f6e4e1bbb3fe 638 {
GeofferyOmlette 0:f6e4e1bbb3fe 639 /* * * * Add Blockwise option to send CoAP message * * */
GeofferyOmlette 0:f6e4e1bbb3fe 640
GeofferyOmlette 0:f6e4e1bbb3fe 641 if (src_coap_msg_ptr->options_list_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 642 {
GeofferyOmlette 0:f6e4e1bbb3fe 643 /* Allocate memory for less used options */
GeofferyOmlette 0:f6e4e1bbb3fe 644 src_coap_msg_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 645
GeofferyOmlette 0:f6e4e1bbb3fe 646 if (src_coap_msg_ptr->options_list_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 647 {
GeofferyOmlette 0:f6e4e1bbb3fe 648 return -2;
GeofferyOmlette 0:f6e4e1bbb3fe 649 }
GeofferyOmlette 0:f6e4e1bbb3fe 650 memset(src_coap_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 651 }
GeofferyOmlette 0:f6e4e1bbb3fe 652
GeofferyOmlette 0:f6e4e1bbb3fe 653
GeofferyOmlette 0:f6e4e1bbb3fe 654 /* Check if Request message */
GeofferyOmlette 0:f6e4e1bbb3fe 655 if (src_coap_msg_ptr->msg_code < COAP_MSG_CODE_RESPONSE_CREATED )
GeofferyOmlette 0:f6e4e1bbb3fe 656 {
GeofferyOmlette 0:f6e4e1bbb3fe 657 /* Add Blockwise option, use Block1 because Request payload */
GeofferyOmlette 0:f6e4e1bbb3fe 658 src_coap_msg_ptr->options_list_ptr->block1_len = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 659 src_coap_msg_ptr->options_list_ptr->block1_ptr = sn_coap_protocol_malloc(1);
GeofferyOmlette 0:f6e4e1bbb3fe 660
GeofferyOmlette 0:f6e4e1bbb3fe 661 if (src_coap_msg_ptr->options_list_ptr->block1_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 662 {
GeofferyOmlette 0:f6e4e1bbb3fe 663 sn_coap_protocol_free(src_coap_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 664
GeofferyOmlette 0:f6e4e1bbb3fe 665 return -2;
GeofferyOmlette 0:f6e4e1bbb3fe 666 }
GeofferyOmlette 0:f6e4e1bbb3fe 667
GeofferyOmlette 0:f6e4e1bbb3fe 668 *(src_coap_msg_ptr->options_list_ptr->block1_ptr) = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
GeofferyOmlette 0:f6e4e1bbb3fe 669 *(src_coap_msg_ptr->options_list_ptr->block1_ptr) |= sn_coap_convert_block_size(sn_coap_block_data_size);
GeofferyOmlette 0:f6e4e1bbb3fe 670
GeofferyOmlette 0:f6e4e1bbb3fe 671 }
GeofferyOmlette 0:f6e4e1bbb3fe 672 else /* Response message */
GeofferyOmlette 0:f6e4e1bbb3fe 673 {
GeofferyOmlette 0:f6e4e1bbb3fe 674 /* Add Blockwise option, use Block2 because Response payload */
GeofferyOmlette 0:f6e4e1bbb3fe 675 src_coap_msg_ptr->options_list_ptr->block2_len = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 676 src_coap_msg_ptr->options_list_ptr->block2_ptr = sn_coap_protocol_malloc(1);
GeofferyOmlette 0:f6e4e1bbb3fe 677
GeofferyOmlette 0:f6e4e1bbb3fe 678 if (src_coap_msg_ptr->options_list_ptr->block2_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 679 {
GeofferyOmlette 0:f6e4e1bbb3fe 680 sn_coap_protocol_free(src_coap_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 681 return -2;
GeofferyOmlette 0:f6e4e1bbb3fe 682 }
GeofferyOmlette 0:f6e4e1bbb3fe 683
GeofferyOmlette 0:f6e4e1bbb3fe 684 *(src_coap_msg_ptr->options_list_ptr->block2_ptr) = 0x08; /* First block (BLOCK NUMBER, 4 MSB bits) + More to come (MORE, 1 bit) */
GeofferyOmlette 0:f6e4e1bbb3fe 685 *(src_coap_msg_ptr->options_list_ptr->block2_ptr) |= sn_coap_convert_block_size(sn_coap_block_data_size);
GeofferyOmlette 0:f6e4e1bbb3fe 686 }
GeofferyOmlette 0:f6e4e1bbb3fe 687
GeofferyOmlette 0:f6e4e1bbb3fe 688 /* Store original Payload length */
GeofferyOmlette 0:f6e4e1bbb3fe 689 original_payload_len = src_coap_msg_ptr->payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 690
GeofferyOmlette 0:f6e4e1bbb3fe 691 /* Change Payload length of send message because Payload is blockwised */
GeofferyOmlette 0:f6e4e1bbb3fe 692 src_coap_msg_ptr->payload_len = sn_coap_block_data_size;
GeofferyOmlette 0:f6e4e1bbb3fe 693 }
GeofferyOmlette 0:f6e4e1bbb3fe 694
GeofferyOmlette 0:f6e4e1bbb3fe 695 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 696 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 697 /* * * * Build Packet data from CoAP message by using CoAP Header builder * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 698 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 699
GeofferyOmlette 0:f6e4e1bbb3fe 700 byte_count_built = sn_coap_builder(dst_packet_data_ptr, src_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 701
GeofferyOmlette 0:f6e4e1bbb3fe 702 if (byte_count_built < 0)
GeofferyOmlette 0:f6e4e1bbb3fe 703 {
GeofferyOmlette 0:f6e4e1bbb3fe 704 return byte_count_built;
GeofferyOmlette 0:f6e4e1bbb3fe 705 }
GeofferyOmlette 0:f6e4e1bbb3fe 706
GeofferyOmlette 0:f6e4e1bbb3fe 707 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 708
GeofferyOmlette 0:f6e4e1bbb3fe 709 /* Check if built Message type was confirmable, only these messages are resent */
GeofferyOmlette 0:f6e4e1bbb3fe 710 if (src_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE)
GeofferyOmlette 0:f6e4e1bbb3fe 711 {
GeofferyOmlette 0:f6e4e1bbb3fe 712 /* Store message to Linked list for resending purposes */
GeofferyOmlette 0:f6e4e1bbb3fe 713 sn_coap_protocol_linked_list_send_msg_store(dst_addr_ptr, byte_count_built, dst_packet_data_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 714 global_system_time + (uint32_t)(sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR));
GeofferyOmlette 0:f6e4e1bbb3fe 715 }
GeofferyOmlette 0:f6e4e1bbb3fe 716
GeofferyOmlette 0:f6e4e1bbb3fe 717 #endif /* ENABLE_RESENDINGS */
GeofferyOmlette 0:f6e4e1bbb3fe 718
GeofferyOmlette 0:f6e4e1bbb3fe 719 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 720
GeofferyOmlette 0:f6e4e1bbb3fe 721 /* If blockwising needed */
GeofferyOmlette 0:f6e4e1bbb3fe 722 if ((original_payload_len > sn_coap_block_data_size) && (sn_coap_block_data_size > 0))
GeofferyOmlette 0:f6e4e1bbb3fe 723 {
GeofferyOmlette 0:f6e4e1bbb3fe 724
GeofferyOmlette 0:f6e4e1bbb3fe 725 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 726 /* * * * Manage rest blockwise messages sending by storing them to Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 727 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 728
GeofferyOmlette 0:f6e4e1bbb3fe 729 coap_blockwise_msg_s *stored_blockwise_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 730
GeofferyOmlette 0:f6e4e1bbb3fe 731 stored_blockwise_msg_ptr = sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 732 if(!stored_blockwise_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 733 {
GeofferyOmlette 0:f6e4e1bbb3fe 734 //block paylaod save failed, only first block can be build. Perhaps we should return error.
GeofferyOmlette 0:f6e4e1bbb3fe 735 return byte_count_built;
GeofferyOmlette 0:f6e4e1bbb3fe 736 }
GeofferyOmlette 0:f6e4e1bbb3fe 737 memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 738
GeofferyOmlette 0:f6e4e1bbb3fe 739 /* Fill struct */
GeofferyOmlette 0:f6e4e1bbb3fe 740 stored_blockwise_msg_ptr->timestamp = global_system_time;
GeofferyOmlette 0:f6e4e1bbb3fe 741
GeofferyOmlette 0:f6e4e1bbb3fe 742 stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(src_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 743
GeofferyOmlette 0:f6e4e1bbb3fe 744 stored_blockwise_msg_ptr->coap_msg_ptr->payload_len = original_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 745 stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr = sn_coap_protocol_malloc(stored_blockwise_msg_ptr->coap_msg_ptr->payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 746 if(!stored_blockwise_msg_ptr->coap_msg_ptr->payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 747 {
GeofferyOmlette 0:f6e4e1bbb3fe 748 //block paylaod save failed, only first block can be build. Perhaps we should return error.
GeofferyOmlette 0:f6e4e1bbb3fe 749 sn_coap_protocol_free(stored_blockwise_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 750 return byte_count_built;
GeofferyOmlette 0:f6e4e1bbb3fe 751 }
GeofferyOmlette 0:f6e4e1bbb3fe 752 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);
GeofferyOmlette 0:f6e4e1bbb3fe 753
GeofferyOmlette 0:f6e4e1bbb3fe 754 sn_linked_list_add_node(global_linked_list_blockwise_sent_msgs_ptr, stored_blockwise_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 755 }
GeofferyOmlette 0:f6e4e1bbb3fe 756
GeofferyOmlette 0:f6e4e1bbb3fe 757 else if(src_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
GeofferyOmlette 0:f6e4e1bbb3fe 758 {
GeofferyOmlette 0:f6e4e1bbb3fe 759 /* Add message to linked list - response can be in blocks and we need header to build response.. */
GeofferyOmlette 0:f6e4e1bbb3fe 760 coap_blockwise_msg_s *stored_blockwise_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 761
GeofferyOmlette 0:f6e4e1bbb3fe 762 stored_blockwise_msg_ptr = sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 763 if(!stored_blockwise_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 764 {
GeofferyOmlette 0:f6e4e1bbb3fe 765 return byte_count_built;
GeofferyOmlette 0:f6e4e1bbb3fe 766 }
GeofferyOmlette 0:f6e4e1bbb3fe 767 memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 768
GeofferyOmlette 0:f6e4e1bbb3fe 769 /* Fill struct */
GeofferyOmlette 0:f6e4e1bbb3fe 770 stored_blockwise_msg_ptr->timestamp = global_system_time;
GeofferyOmlette 0:f6e4e1bbb3fe 771
GeofferyOmlette 0:f6e4e1bbb3fe 772 stored_blockwise_msg_ptr->coap_msg_ptr = sn_coap_protocol_copy_header(src_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 773
GeofferyOmlette 0:f6e4e1bbb3fe 774 sn_linked_list_add_node(global_linked_list_blockwise_sent_msgs_ptr, stored_blockwise_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 775 }
GeofferyOmlette 0:f6e4e1bbb3fe 776
GeofferyOmlette 0:f6e4e1bbb3fe 777 #endif /* SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE */
GeofferyOmlette 0:f6e4e1bbb3fe 778
GeofferyOmlette 0:f6e4e1bbb3fe 779 /* * * * Return built CoAP message Packet data length * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 780 return byte_count_built;
GeofferyOmlette 0:f6e4e1bbb3fe 781 }
GeofferyOmlette 0:f6e4e1bbb3fe 782
GeofferyOmlette 0:f6e4e1bbb3fe 783 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 784 sn_coap_hdr_s *sn_coap_protocol_parse(sn_nsdl_addr_s *src_addr_ptr, uint16_t packet_data_len, uint8_t *packet_data_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 785 {
GeofferyOmlette 0:f6e4e1bbb3fe 786 sn_coap_hdr_s *returned_dst_coap_msg_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 787 coap_version_e coap_version = COAP_VERSION_UNKNOWN;
GeofferyOmlette 0:f6e4e1bbb3fe 788
GeofferyOmlette 0:f6e4e1bbb3fe 789 /* * * * Check given pointer * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 790 if (src_addr_ptr == NULL || src_addr_ptr->addr_ptr == NULL ||
GeofferyOmlette 0:f6e4e1bbb3fe 791 packet_data_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 792 {
GeofferyOmlette 0:f6e4e1bbb3fe 793 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 794 }
GeofferyOmlette 0:f6e4e1bbb3fe 795
GeofferyOmlette 0:f6e4e1bbb3fe 796 /* * * * Parse Packet data to CoAP message by using CoAP Header parser * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 797 returned_dst_coap_msg_ptr = sn_coap_parser(packet_data_len, packet_data_ptr, &coap_version);
GeofferyOmlette 0:f6e4e1bbb3fe 798
GeofferyOmlette 0:f6e4e1bbb3fe 799 /* Check status of returned pointer */
GeofferyOmlette 0:f6e4e1bbb3fe 800 if (returned_dst_coap_msg_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 801 {
GeofferyOmlette 0:f6e4e1bbb3fe 802 /* Memory allocation error in parser */
GeofferyOmlette 0:f6e4e1bbb3fe 803 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 804 }
GeofferyOmlette 0:f6e4e1bbb3fe 805
GeofferyOmlette 0:f6e4e1bbb3fe 806 /* * * * Check validity of parsed Header values * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 807 if (sn_coap_header_validity_check(returned_dst_coap_msg_ptr, coap_version) != 0)
GeofferyOmlette 0:f6e4e1bbb3fe 808 {
GeofferyOmlette 0:f6e4e1bbb3fe 809 /* 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 */
GeofferyOmlette 0:f6e4e1bbb3fe 810 if(((returned_dst_coap_msg_ptr->msg_code >> 5) == 1) || // if class == 1
GeofferyOmlette 0:f6e4e1bbb3fe 811 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 6) || // if class == 6
GeofferyOmlette 0:f6e4e1bbb3fe 812 ((returned_dst_coap_msg_ptr->msg_code >> 5) == 7)) // if class == 7
GeofferyOmlette 0:f6e4e1bbb3fe 813 {
GeofferyOmlette 0:f6e4e1bbb3fe 814 sn_coap_protocol_send_rst(returned_dst_coap_msg_ptr->msg_id, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 815 }
GeofferyOmlette 0:f6e4e1bbb3fe 816
GeofferyOmlette 0:f6e4e1bbb3fe 817 /* Release memory of CoAP message */
GeofferyOmlette 0:f6e4e1bbb3fe 818 sn_coap_parser_release_allocated_coap_msg_mem(returned_dst_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 819
GeofferyOmlette 0:f6e4e1bbb3fe 820 /* Return NULL because Header validity check failed */
GeofferyOmlette 0:f6e4e1bbb3fe 821 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 822 }
GeofferyOmlette 0:f6e4e1bbb3fe 823
GeofferyOmlette 0:f6e4e1bbb3fe 824 /* Check if we need to send reset message */
GeofferyOmlette 0:f6e4e1bbb3fe 825 /* A recipient MUST acknowledge a Confirmable message with an Acknowledgement
GeofferyOmlette 0:f6e4e1bbb3fe 826 message or, if it lacks context to process the message properly
GeofferyOmlette 0:f6e4e1bbb3fe 827 (including the case where the message is Empty, uses a code with a
GeofferyOmlette 0:f6e4e1bbb3fe 828 reserved class (1, 6 or 7), or has a message format error), MUST
GeofferyOmlette 0:f6e4e1bbb3fe 829 reject it; rejecting a Confirmable message is effected by sending a
GeofferyOmlette 0:f6e4e1bbb3fe 830 matching Reset message and otherwise ignoring it. */
GeofferyOmlette 0:f6e4e1bbb3fe 831 if(returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE)
GeofferyOmlette 0:f6e4e1bbb3fe 832 {
GeofferyOmlette 0:f6e4e1bbb3fe 833 /* CoAP ping */
GeofferyOmlette 0:f6e4e1bbb3fe 834 if(returned_dst_coap_msg_ptr->msg_code == COAP_MSG_CODE_EMPTY)
GeofferyOmlette 0:f6e4e1bbb3fe 835 {
GeofferyOmlette 0:f6e4e1bbb3fe 836 sn_coap_protocol_send_rst(returned_dst_coap_msg_ptr->msg_id, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 837
GeofferyOmlette 0:f6e4e1bbb3fe 838 /* Release memory of CoAP message */
GeofferyOmlette 0:f6e4e1bbb3fe 839 sn_coap_parser_release_allocated_coap_msg_mem(returned_dst_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 840
GeofferyOmlette 0:f6e4e1bbb3fe 841 /* Return NULL because Header validity check failed */
GeofferyOmlette 0:f6e4e1bbb3fe 842 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 843 }
GeofferyOmlette 0:f6e4e1bbb3fe 844 }
GeofferyOmlette 0:f6e4e1bbb3fe 845
GeofferyOmlette 0:f6e4e1bbb3fe 846 #if !SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is used, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 847 /* If blockwising used in received message */
GeofferyOmlette 0:f6e4e1bbb3fe 848 if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
GeofferyOmlette 0:f6e4e1bbb3fe 849 (returned_dst_coap_msg_ptr->options_list_ptr->block1_ptr != NULL ||
GeofferyOmlette 0:f6e4e1bbb3fe 850 returned_dst_coap_msg_ptr->options_list_ptr->block2_ptr != NULL))
GeofferyOmlette 0:f6e4e1bbb3fe 851 {
GeofferyOmlette 0:f6e4e1bbb3fe 852 /* Set returned status to User */
GeofferyOmlette 0:f6e4e1bbb3fe 853 returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED;
GeofferyOmlette 0:f6e4e1bbb3fe 854 //todo: send response -> not implemented
GeofferyOmlette 0:f6e4e1bbb3fe 855 return returned_dst_coap_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 856 }
GeofferyOmlette 0:f6e4e1bbb3fe 857 #endif /* !SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE */
GeofferyOmlette 0:f6e4e1bbb3fe 858
GeofferyOmlette 0:f6e4e1bbb3fe 859 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication is used, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 860
GeofferyOmlette 0:f6e4e1bbb3fe 861 /* * * * Manage received CoAP message duplicate detection * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 862
GeofferyOmlette 0:f6e4e1bbb3fe 863 /* Check if duplication message detected */
GeofferyOmlette 0:f6e4e1bbb3fe 864 ret_status = sn_coap_protocol_linked_list_duplication_info_search(src_addr_ptr, msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 865
GeofferyOmlette 0:f6e4e1bbb3fe 866 /* If no message duplication detected */
GeofferyOmlette 0:f6e4e1bbb3fe 867 if (ret_status == -1)
GeofferyOmlette 0:f6e4e1bbb3fe 868 {
GeofferyOmlette 0:f6e4e1bbb3fe 869 /* * * No Message duplication: Store received message for detecting later duplication * * */
GeofferyOmlette 0:f6e4e1bbb3fe 870
GeofferyOmlette 0:f6e4e1bbb3fe 871 /* Get count of stored duplication messages */
GeofferyOmlette 0:f6e4e1bbb3fe 872 uint16_t stored_duplication_msgs_count = sn_linked_list_count_nodes(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 873
GeofferyOmlette 0:f6e4e1bbb3fe 874 /* Check if there is no room to store message for duplication detection purposes */
GeofferyOmlette 0:f6e4e1bbb3fe 875 if (stored_duplication_msgs_count >= sn_coap_duplication_buffer_size)
GeofferyOmlette 0:f6e4e1bbb3fe 876 {
GeofferyOmlette 0:f6e4e1bbb3fe 877 /* Get oldest stored duplication message */
GeofferyOmlette 0:f6e4e1bbb3fe 878 coap_duplication_info_s *stored_duplication_info_ptr = sn_linked_list_get_last_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 879
GeofferyOmlette 0:f6e4e1bbb3fe 880 /* Remove oldest stored duplication message for getting room for new duplication message */
GeofferyOmlette 0:f6e4e1bbb3fe 881 sn_coap_protocol_linked_list_duplication_info_remove(stored_duplication_info_ptr->addr_ptr, stored_duplication_info_ptr->port, stored_duplication_info_ptr->msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 882 }
GeofferyOmlette 0:f6e4e1bbb3fe 883
GeofferyOmlette 0:f6e4e1bbb3fe 884 /* Store Duplication info to Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 885 sn_coap_protocol_linked_list_duplication_info_store(src_addr_ptr, msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 886 }
GeofferyOmlette 0:f6e4e1bbb3fe 887 else /* * * Message duplication detected * * */
GeofferyOmlette 0:f6e4e1bbb3fe 888 {
GeofferyOmlette 0:f6e4e1bbb3fe 889 /* Set returned status to User */
GeofferyOmlette 0:f6e4e1bbb3fe 890 returned_dst_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_DUPLICATED_MSG;
GeofferyOmlette 0:f6e4e1bbb3fe 891
GeofferyOmlette 0:f6e4e1bbb3fe 892 /* Because duplicate message, return with coap_status set */
GeofferyOmlette 0:f6e4e1bbb3fe 893 return returned_dst_coap_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 894 }
GeofferyOmlette 0:f6e4e1bbb3fe 895 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 896
GeofferyOmlette 0:f6e4e1bbb3fe 897
GeofferyOmlette 0:f6e4e1bbb3fe 898 /*** And here we check if message was block message ***/
GeofferyOmlette 0:f6e4e1bbb3fe 899 /*** If so, we call own block handling function and ***/
GeofferyOmlette 0:f6e4e1bbb3fe 900 /*** return to caller. ***/
GeofferyOmlette 0:f6e4e1bbb3fe 901 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE
GeofferyOmlette 0:f6e4e1bbb3fe 902
GeofferyOmlette 0:f6e4e1bbb3fe 903 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 904
GeofferyOmlette 0:f6e4e1bbb3fe 905 if (returned_dst_coap_msg_ptr->options_list_ptr != NULL &&
GeofferyOmlette 0:f6e4e1bbb3fe 906 (returned_dst_coap_msg_ptr->options_list_ptr->block1_ptr != NULL ||
GeofferyOmlette 0:f6e4e1bbb3fe 907 returned_dst_coap_msg_ptr->options_list_ptr->block2_ptr != NULL))
GeofferyOmlette 0:f6e4e1bbb3fe 908 {
GeofferyOmlette 0:f6e4e1bbb3fe 909 returned_dst_coap_msg_ptr = sn_coap_handle_blockwise_message(src_addr_ptr, returned_dst_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 910 }
GeofferyOmlette 0:f6e4e1bbb3fe 911 else
GeofferyOmlette 0:f6e4e1bbb3fe 912 {
GeofferyOmlette 0:f6e4e1bbb3fe 913 stored_blockwise_msg_temp_ptr = sn_linked_list_get_last_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 914
GeofferyOmlette 0:f6e4e1bbb3fe 915 /* Get ... */
GeofferyOmlette 0:f6e4e1bbb3fe 916 while(stored_blockwise_msg_temp_ptr && (returned_dst_coap_msg_ptr->msg_id != stored_blockwise_msg_temp_ptr->coap_msg_ptr->msg_id))
GeofferyOmlette 0:f6e4e1bbb3fe 917 {
GeofferyOmlette 0:f6e4e1bbb3fe 918 stored_blockwise_msg_temp_ptr = sn_linked_list_get_previous_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 919 }
GeofferyOmlette 0:f6e4e1bbb3fe 920
GeofferyOmlette 0:f6e4e1bbb3fe 921 if(stored_blockwise_msg_temp_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 922 {
GeofferyOmlette 0:f6e4e1bbb3fe 923 sn_linked_list_remove_current_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 924
GeofferyOmlette 0:f6e4e1bbb3fe 925 if(stored_blockwise_msg_temp_ptr->coap_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 926 sn_coap_parser_release_allocated_coap_msg_mem(stored_blockwise_msg_temp_ptr->coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 927
GeofferyOmlette 0:f6e4e1bbb3fe 928 sn_coap_protocol_free(stored_blockwise_msg_temp_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 929 }
GeofferyOmlette 0:f6e4e1bbb3fe 930 }
GeofferyOmlette 0:f6e4e1bbb3fe 931
GeofferyOmlette 0:f6e4e1bbb3fe 932 if(!returned_dst_coap_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 933 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 934
GeofferyOmlette 0:f6e4e1bbb3fe 935 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 936
GeofferyOmlette 0:f6e4e1bbb3fe 937 /* Check if received Message type was confirmable */
GeofferyOmlette 0:f6e4e1bbb3fe 938 if (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE)
GeofferyOmlette 0:f6e4e1bbb3fe 939 {
GeofferyOmlette 0:f6e4e1bbb3fe 940 if (returned_dst_coap_msg_ptr->token_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 941 {
GeofferyOmlette 0:f6e4e1bbb3fe 942 /* * * * Manage received CoAP message acknowledgement * * */
GeofferyOmlette 0:f6e4e1bbb3fe 943
GeofferyOmlette 0:f6e4e1bbb3fe 944 /* Client Server */
GeofferyOmlette 0:f6e4e1bbb3fe 945
GeofferyOmlette 0:f6e4e1bbb3fe 946 /* ------------------> THIS IS DONE HERE: Confirmable request (CoAP stores Acknowledgement info to Linked list) */
GeofferyOmlette 0:f6e4e1bbb3fe 947
GeofferyOmlette 0:f6e4e1bbb3fe 948 /* <------------------ Piggy-backed acknowledgement response message (CoAP writes same Message ID
GeofferyOmlette 0:f6e4e1bbb3fe 949 than was in Request message).
GeofferyOmlette 0:f6e4e1bbb3fe 950 User has written correct Token option to the response message. */
GeofferyOmlette 0:f6e4e1bbb3fe 951
GeofferyOmlette 0:f6e4e1bbb3fe 952 /* Store message's Acknowledgement info to Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 953 sn_coap_protocol_linked_list_ack_info_store(returned_dst_coap_msg_ptr->msg_id, returned_dst_coap_msg_ptr->token_len, returned_dst_coap_msg_ptr->token_ptr, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 954 }
GeofferyOmlette 0:f6e4e1bbb3fe 955 else
GeofferyOmlette 0:f6e4e1bbb3fe 956 {
GeofferyOmlette 0:f6e4e1bbb3fe 957 sn_coap_protocol_linked_list_ack_info_store(returned_dst_coap_msg_ptr->msg_id, 0, NULL, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 958 }
GeofferyOmlette 0:f6e4e1bbb3fe 959 }
GeofferyOmlette 0:f6e4e1bbb3fe 960
GeofferyOmlette 0:f6e4e1bbb3fe 961
GeofferyOmlette 0:f6e4e1bbb3fe 962 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 963
GeofferyOmlette 0:f6e4e1bbb3fe 964 /* Check if received Message type was acknowledgement */
GeofferyOmlette 0:f6e4e1bbb3fe 965 if ((returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT) || (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET))
GeofferyOmlette 0:f6e4e1bbb3fe 966 {
GeofferyOmlette 0:f6e4e1bbb3fe 967 /* * * * Manage CoAP message resending by removing active resending message from Linked list * * */
GeofferyOmlette 0:f6e4e1bbb3fe 968
GeofferyOmlette 0:f6e4e1bbb3fe 969 /* Get node count i.e. count of active resending messages */
GeofferyOmlette 0:f6e4e1bbb3fe 970 uint16_t stored_resending_msgs_count = sn_linked_list_count_nodes(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 971
GeofferyOmlette 0:f6e4e1bbb3fe 972 /* Check if there is ongoing active message resendings */
GeofferyOmlette 0:f6e4e1bbb3fe 973 if (stored_resending_msgs_count > 0)
GeofferyOmlette 0:f6e4e1bbb3fe 974 {
GeofferyOmlette 0:f6e4e1bbb3fe 975 sn_nsdl_transmit_s *removed_msg_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 976
GeofferyOmlette 0:f6e4e1bbb3fe 977 /* Check if received message was confirmation for some active resending message */
GeofferyOmlette 0:f6e4e1bbb3fe 978
GeofferyOmlette 0:f6e4e1bbb3fe 979 removed_msg_ptr = sn_coap_protocol_linked_list_send_msg_search(src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 980
GeofferyOmlette 0:f6e4e1bbb3fe 981 if (removed_msg_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 982 {
GeofferyOmlette 0:f6e4e1bbb3fe 983 /* Remove resending message from active message resending Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 984 sn_coap_protocol_linked_list_send_msg_remove(src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 985 }
GeofferyOmlette 0:f6e4e1bbb3fe 986 }
GeofferyOmlette 0:f6e4e1bbb3fe 987 }
GeofferyOmlette 0:f6e4e1bbb3fe 988 #endif /* ENABLE_RESENDINGS */
GeofferyOmlette 0:f6e4e1bbb3fe 989
GeofferyOmlette 0:f6e4e1bbb3fe 990 /* * * * Return parsed CoAP message * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 991 return (returned_dst_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 992 }
GeofferyOmlette 0:f6e4e1bbb3fe 993
GeofferyOmlette 0:f6e4e1bbb3fe 994 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 995 int8_t sn_coap_protocol_exec(uint32_t current_time)
GeofferyOmlette 0:f6e4e1bbb3fe 996 {
GeofferyOmlette 0:f6e4e1bbb3fe 997 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 998 uint8_t stored_resending_msgs_count;
GeofferyOmlette 0:f6e4e1bbb3fe 999 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 1000
GeofferyOmlette 0:f6e4e1bbb3fe 1001 /* * * * Store current System time * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1002 global_system_time = current_time;
GeofferyOmlette 0:f6e4e1bbb3fe 1003
GeofferyOmlette 0:f6e4e1bbb3fe 1004 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE
GeofferyOmlette 0:f6e4e1bbb3fe 1005 /* * * * Remove old blocwise data * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1006 sn_coap_protocol_linked_list_blockwise_remove_old_data();
GeofferyOmlette 0:f6e4e1bbb3fe 1007 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 1008
GeofferyOmlette 0:f6e4e1bbb3fe 1009
GeofferyOmlette 0:f6e4e1bbb3fe 1010 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT
GeofferyOmlette 0:f6e4e1bbb3fe 1011 /* * * * Remove old duplication messages * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1012 sn_coap_protocol_linked_list_duplication_info_remove_old_ones();
GeofferyOmlette 0:f6e4e1bbb3fe 1013 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 1014
GeofferyOmlette 0:f6e4e1bbb3fe 1015 /* Remove old Acknowledgement infos */
GeofferyOmlette 0:f6e4e1bbb3fe 1016 sn_coap_protocol_linked_list_ack_info_remove_old_ones();
GeofferyOmlette 0:f6e4e1bbb3fe 1017
GeofferyOmlette 0:f6e4e1bbb3fe 1018 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 1019 /* Check if there is ongoing active message sendings */
GeofferyOmlette 0:f6e4e1bbb3fe 1020 stored_resending_msgs_count = sn_linked_list_count_nodes(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1021
GeofferyOmlette 0:f6e4e1bbb3fe 1022 if (stored_resending_msgs_count > 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1023 {
GeofferyOmlette 0:f6e4e1bbb3fe 1024 coap_send_msg_s *stored_msg_ptr = sn_linked_list_get_last_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1025 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1026
GeofferyOmlette 0:f6e4e1bbb3fe 1027 /* Loop all resending messages */
GeofferyOmlette 0:f6e4e1bbb3fe 1028 for (i = 0; i < stored_resending_msgs_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1029 {
GeofferyOmlette 0:f6e4e1bbb3fe 1030 /* Check if it is time to send this message */
GeofferyOmlette 0:f6e4e1bbb3fe 1031 if (current_time >= stored_msg_ptr->resending_time)
GeofferyOmlette 0:f6e4e1bbb3fe 1032 {
GeofferyOmlette 0:f6e4e1bbb3fe 1033 /* * * Increase Resending counter * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1034 stored_msg_ptr->resending_counter++;
GeofferyOmlette 0:f6e4e1bbb3fe 1035
GeofferyOmlette 0:f6e4e1bbb3fe 1036 /* Check if all re-sendings have been done */
GeofferyOmlette 0:f6e4e1bbb3fe 1037 if (stored_msg_ptr->resending_counter > sn_coap_resending_count)
GeofferyOmlette 0:f6e4e1bbb3fe 1038 {
GeofferyOmlette 0:f6e4e1bbb3fe 1039 sn_coap_hdr_s *tmp_coap_hdr_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 1040 coap_version_e coap_version = COAP_VERSION_UNKNOWN;
GeofferyOmlette 0:f6e4e1bbb3fe 1041
GeofferyOmlette 0:f6e4e1bbb3fe 1042 /* Get message ID from stored sending message */
GeofferyOmlette 0:f6e4e1bbb3fe 1043 uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
GeofferyOmlette 0:f6e4e1bbb3fe 1044 temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
GeofferyOmlette 0:f6e4e1bbb3fe 1045
GeofferyOmlette 0:f6e4e1bbb3fe 1046 /* If RX callback have been fedined.. */
GeofferyOmlette 0:f6e4e1bbb3fe 1047 if(sn_coap_rx_callback != 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1048 {
GeofferyOmlette 0:f6e4e1bbb3fe 1049 /* Parse CoAP message, set status and call RX callback */
GeofferyOmlette 0:f6e4e1bbb3fe 1050 tmp_coap_hdr_ptr = sn_coap_parser(stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->packet_ptr, &coap_version);
GeofferyOmlette 0:f6e4e1bbb3fe 1051
GeofferyOmlette 0:f6e4e1bbb3fe 1052 if(tmp_coap_hdr_ptr != 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1053 {
GeofferyOmlette 0:f6e4e1bbb3fe 1054 tmp_coap_hdr_ptr->coap_status = COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED;
GeofferyOmlette 0:f6e4e1bbb3fe 1055
GeofferyOmlette 0:f6e4e1bbb3fe 1056 sn_coap_rx_callback(tmp_coap_hdr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1057
GeofferyOmlette 0:f6e4e1bbb3fe 1058 sn_coap_parser_release_allocated_coap_msg_mem(tmp_coap_hdr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1059 }
GeofferyOmlette 0:f6e4e1bbb3fe 1060 }
GeofferyOmlette 0:f6e4e1bbb3fe 1061
GeofferyOmlette 0:f6e4e1bbb3fe 1062 /* Remove message from Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1063 sn_coap_protocol_linked_list_send_msg_remove(stored_msg_ptr->send_msg_ptr->dst_addr_ptr, temp_msg_id);
GeofferyOmlette 0:f6e4e1bbb3fe 1064 }
GeofferyOmlette 0:f6e4e1bbb3fe 1065 else
GeofferyOmlette 0:f6e4e1bbb3fe 1066 {
GeofferyOmlette 0:f6e4e1bbb3fe 1067 /* Send message */
GeofferyOmlette 0:f6e4e1bbb3fe 1068 sn_coap_tx_callback(stored_msg_ptr->send_msg_ptr->protocol, stored_msg_ptr->send_msg_ptr->packet_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 1069 stored_msg_ptr->send_msg_ptr->packet_len, stored_msg_ptr->send_msg_ptr->dst_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1070
GeofferyOmlette 0:f6e4e1bbb3fe 1071 /* * * Count new Resending time * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1072 stored_msg_ptr->resending_time = current_time + (((uint32_t)(sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR)) <<
GeofferyOmlette 0:f6e4e1bbb3fe 1073 stored_msg_ptr->resending_counter);
GeofferyOmlette 0:f6e4e1bbb3fe 1074 }
GeofferyOmlette 0:f6e4e1bbb3fe 1075
GeofferyOmlette 0:f6e4e1bbb3fe 1076 }
GeofferyOmlette 0:f6e4e1bbb3fe 1077
GeofferyOmlette 0:f6e4e1bbb3fe 1078 /* Get next stored sending message from Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1079 stored_msg_ptr = sn_linked_list_get_previous_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1080 }
GeofferyOmlette 0:f6e4e1bbb3fe 1081 }
GeofferyOmlette 0:f6e4e1bbb3fe 1082
GeofferyOmlette 0:f6e4e1bbb3fe 1083 #endif /* ENABLE_RESENDINGS */
GeofferyOmlette 0:f6e4e1bbb3fe 1084
GeofferyOmlette 0:f6e4e1bbb3fe 1085 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1086 }
GeofferyOmlette 0:f6e4e1bbb3fe 1087
GeofferyOmlette 0:f6e4e1bbb3fe 1088 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 1089
GeofferyOmlette 0:f6e4e1bbb3fe 1090 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1091 * \fn static void 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)
GeofferyOmlette 0:f6e4e1bbb3fe 1092 *
GeofferyOmlette 0:f6e4e1bbb3fe 1093 * \brief Stores message to Linked list for sending purposes.
GeofferyOmlette 0:f6e4e1bbb3fe 1094
GeofferyOmlette 0:f6e4e1bbb3fe 1095 * \param *dst_addr_ptr is pointer to destination address where CoAP message will be sent
GeofferyOmlette 0:f6e4e1bbb3fe 1096 *
GeofferyOmlette 0:f6e4e1bbb3fe 1097 * \param send_packet_data_len is length of Packet data to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1098 *
GeofferyOmlette 0:f6e4e1bbb3fe 1099 * \param *send_packet_data_ptr is Packet data to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1100 *
GeofferyOmlette 0:f6e4e1bbb3fe 1101 * \param sending_time is stored sending time
GeofferyOmlette 0:f6e4e1bbb3fe 1102 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1103 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1104 static void sn_coap_protocol_linked_list_send_msg_store(sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len,
GeofferyOmlette 0:f6e4e1bbb3fe 1105 uint8_t *send_packet_data_ptr, uint32_t sending_time)
GeofferyOmlette 0:f6e4e1bbb3fe 1106 {
GeofferyOmlette 0:f6e4e1bbb3fe 1107
GeofferyOmlette 0:f6e4e1bbb3fe 1108 coap_send_msg_s *stored_msg_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1109
GeofferyOmlette 0:f6e4e1bbb3fe 1110 /* If both queue parameters are "0" or resending count is "0", then re-sending is disabled */
GeofferyOmlette 0:f6e4e1bbb3fe 1111 if(((sn_coap_resending_queue_msgs == 0) && (sn_coap_resending_queue_bytes == 0)) || (sn_coap_resending_count == 0))
GeofferyOmlette 0:f6e4e1bbb3fe 1112 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1113
GeofferyOmlette 0:f6e4e1bbb3fe 1114 if (sn_coap_resending_queue_msgs > 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1115 {
GeofferyOmlette 0:f6e4e1bbb3fe 1116 if(sn_linked_list_count_nodes(global_linked_list_resent_msgs_ptr) >= sn_coap_resending_queue_msgs)
GeofferyOmlette 0:f6e4e1bbb3fe 1117 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1118 }
GeofferyOmlette 0:f6e4e1bbb3fe 1119
GeofferyOmlette 0:f6e4e1bbb3fe 1120 /* Count resending queue size, if buffer size is defined */
GeofferyOmlette 0:f6e4e1bbb3fe 1121 if(sn_coap_resending_queue_bytes > 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1122 {
GeofferyOmlette 0:f6e4e1bbb3fe 1123 if((sn_coap_count_linked_list_size(global_linked_list_resent_msgs_ptr) + send_packet_data_len) > sn_coap_resending_queue_bytes)
GeofferyOmlette 0:f6e4e1bbb3fe 1124 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1125 }
GeofferyOmlette 0:f6e4e1bbb3fe 1126
GeofferyOmlette 0:f6e4e1bbb3fe 1127 /* Allocating memory for stored message */
GeofferyOmlette 0:f6e4e1bbb3fe 1128 stored_msg_ptr = sn_coap_protocol_allocate_mem_for_msg(dst_addr_ptr, send_packet_data_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1129
GeofferyOmlette 0:f6e4e1bbb3fe 1130 if(stored_msg_ptr == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1131 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1132
GeofferyOmlette 0:f6e4e1bbb3fe 1133 /* Filling of coap_send_msg_s with initialization values */
GeofferyOmlette 0:f6e4e1bbb3fe 1134 stored_msg_ptr->resending_counter = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1135 stored_msg_ptr->resending_time = sending_time;
GeofferyOmlette 0:f6e4e1bbb3fe 1136
GeofferyOmlette 0:f6e4e1bbb3fe 1137 /* Filling of sn_nsdl_transmit_s */
GeofferyOmlette 0:f6e4e1bbb3fe 1138 stored_msg_ptr->send_msg_ptr->protocol = SN_NSDL_PROTOCOL_COAP;
GeofferyOmlette 0:f6e4e1bbb3fe 1139 stored_msg_ptr->send_msg_ptr->packet_len = send_packet_data_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1140 memcpy(stored_msg_ptr->send_msg_ptr->packet_ptr, send_packet_data_ptr, send_packet_data_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1141
GeofferyOmlette 0:f6e4e1bbb3fe 1142 /* Filling of sn_nsdl_addr_s */
GeofferyOmlette 0:f6e4e1bbb3fe 1143 stored_msg_ptr->send_msg_ptr->dst_addr_ptr->type = dst_addr_ptr->type;
GeofferyOmlette 0:f6e4e1bbb3fe 1144 stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_len = dst_addr_ptr->addr_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1145 memcpy(stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_ptr, dst_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1146 stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port = dst_addr_ptr->port;
GeofferyOmlette 0:f6e4e1bbb3fe 1147
GeofferyOmlette 0:f6e4e1bbb3fe 1148 /* Storing Resending message to Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1149 if(sn_linked_list_add_node(global_linked_list_resent_msgs_ptr, stored_msg_ptr) != 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1150 sn_coap_protocol_release_allocated_send_msg_mem(stored_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1151
GeofferyOmlette 0:f6e4e1bbb3fe 1152 }
GeofferyOmlette 0:f6e4e1bbb3fe 1153
GeofferyOmlette 0:f6e4e1bbb3fe 1154 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1155 * \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)
GeofferyOmlette 0:f6e4e1bbb3fe 1156 *
GeofferyOmlette 0:f6e4e1bbb3fe 1157 * \brief Searches stored resending message from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1158 *
GeofferyOmlette 0:f6e4e1bbb3fe 1159 * \param *src_addr_ptr is searching key for searched message
GeofferyOmlette 0:f6e4e1bbb3fe 1160 *
GeofferyOmlette 0:f6e4e1bbb3fe 1161 * \param msg_id is searching key for searched message
GeofferyOmlette 0:f6e4e1bbb3fe 1162 *
GeofferyOmlette 0:f6e4e1bbb3fe 1163 * \return Return value is pointer to found stored resending message in Linked
GeofferyOmlette 0:f6e4e1bbb3fe 1164 * list or NULL if message not found
GeofferyOmlette 0:f6e4e1bbb3fe 1165 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1166 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1167 static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1168 {
GeofferyOmlette 0:f6e4e1bbb3fe 1169 coap_send_msg_s *stored_msg_ptr = sn_linked_list_get_last_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1170 uint16_t stored_resending_msgs_count = sn_linked_list_count_nodes(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1171 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1172
GeofferyOmlette 0:f6e4e1bbb3fe 1173 /* Loop all stored resending messages Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1174 for (i = 0; i < stored_resending_msgs_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1175 {
GeofferyOmlette 0:f6e4e1bbb3fe 1176 /* Get message ID from stored resending message */
GeofferyOmlette 0:f6e4e1bbb3fe 1177 uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
GeofferyOmlette 0:f6e4e1bbb3fe 1178 temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
GeofferyOmlette 0:f6e4e1bbb3fe 1179
GeofferyOmlette 0:f6e4e1bbb3fe 1180 /* If message's Message ID is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1181 if (temp_msg_id == msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1182 {
GeofferyOmlette 0:f6e4e1bbb3fe 1183 int8_t mem_cmp_result = memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1184
GeofferyOmlette 0:f6e4e1bbb3fe 1185 /* If message's Source address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1186 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1187 {
GeofferyOmlette 0:f6e4e1bbb3fe 1188 /* If message's Source address port is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1189 if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1190 {
GeofferyOmlette 0:f6e4e1bbb3fe 1191 /* * * Message found, return pointer to that stored resending message * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1192 return stored_msg_ptr->send_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 1193 }
GeofferyOmlette 0:f6e4e1bbb3fe 1194 }
GeofferyOmlette 0:f6e4e1bbb3fe 1195 }
GeofferyOmlette 0:f6e4e1bbb3fe 1196
GeofferyOmlette 0:f6e4e1bbb3fe 1197 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1198 stored_msg_ptr = sn_linked_list_get_previous_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1199 }
GeofferyOmlette 0:f6e4e1bbb3fe 1200
GeofferyOmlette 0:f6e4e1bbb3fe 1201 /* Message not found */
GeofferyOmlette 0:f6e4e1bbb3fe 1202 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1203 }
GeofferyOmlette 0:f6e4e1bbb3fe 1204 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1205 * \fn static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1206 *
GeofferyOmlette 0:f6e4e1bbb3fe 1207 * \brief Removes stored resending message from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1208 *
GeofferyOmlette 0:f6e4e1bbb3fe 1209 * \param *src_addr_ptr is searching key for searched message
GeofferyOmlette 0:f6e4e1bbb3fe 1210 * \param msg_id is searching key for removed message
GeofferyOmlette 0:f6e4e1bbb3fe 1211 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1212 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1213 static void sn_coap_protocol_linked_list_send_msg_remove(sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1214 {
GeofferyOmlette 0:f6e4e1bbb3fe 1215 coap_send_msg_s *stored_msg_ptr = sn_linked_list_get_last_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1216 uint16_t stored_resending_msgs_count = sn_linked_list_count_nodes(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1217 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1218
GeofferyOmlette 0:f6e4e1bbb3fe 1219 /* Loop all stored resending messages in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1220 for (i = 0; i < stored_resending_msgs_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1221 {
GeofferyOmlette 0:f6e4e1bbb3fe 1222 /* Get message ID from stored resending message */
GeofferyOmlette 0:f6e4e1bbb3fe 1223 uint16_t temp_msg_id = (stored_msg_ptr->send_msg_ptr->packet_ptr[2] << 8);
GeofferyOmlette 0:f6e4e1bbb3fe 1224 temp_msg_id += (uint16_t)stored_msg_ptr->send_msg_ptr->packet_ptr[3];
GeofferyOmlette 0:f6e4e1bbb3fe 1225
GeofferyOmlette 0:f6e4e1bbb3fe 1226 /* If message's Message ID is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1227 if (temp_msg_id == msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1228 {
GeofferyOmlette 0:f6e4e1bbb3fe 1229 int8_t mem_cmp_result = memcmp(src_addr_ptr->addr_ptr, stored_msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, src_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1230
GeofferyOmlette 0:f6e4e1bbb3fe 1231 /* If message's Source address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1232 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1233 {
GeofferyOmlette 0:f6e4e1bbb3fe 1234 /* If message's Source address port is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1235 if (stored_msg_ptr->send_msg_ptr->dst_addr_ptr->port == src_addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1236 {
GeofferyOmlette 0:f6e4e1bbb3fe 1237 /* * * Message found * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1238
GeofferyOmlette 0:f6e4e1bbb3fe 1239 /* Free memory of stored message */
GeofferyOmlette 0:f6e4e1bbb3fe 1240 sn_coap_protocol_release_allocated_send_msg_mem(stored_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1241
GeofferyOmlette 0:f6e4e1bbb3fe 1242 /* Remove message from Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1243 stored_msg_ptr = sn_linked_list_remove_current_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1244
GeofferyOmlette 0:f6e4e1bbb3fe 1245 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1246 }
GeofferyOmlette 0:f6e4e1bbb3fe 1247 }
GeofferyOmlette 0:f6e4e1bbb3fe 1248 }
GeofferyOmlette 0:f6e4e1bbb3fe 1249
GeofferyOmlette 0:f6e4e1bbb3fe 1250 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1251 stored_msg_ptr = sn_linked_list_get_previous_node(global_linked_list_resent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1252 }
GeofferyOmlette 0:f6e4e1bbb3fe 1253 }
GeofferyOmlette 0:f6e4e1bbb3fe 1254 #endif /* ENABLE_RESENDINGS */
GeofferyOmlette 0:f6e4e1bbb3fe 1255
GeofferyOmlette 0:f6e4e1bbb3fe 1256 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1257 * \fn static void sn_coap_protocol_linked_list_ack_info_store(uint16_t msg_id, uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1258 *
GeofferyOmlette 0:f6e4e1bbb3fe 1259 * \brief Stores Acknowledgement info to Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1260 *
GeofferyOmlette 0:f6e4e1bbb3fe 1261 * \param msg_id is Message ID to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1262 *
GeofferyOmlette 0:f6e4e1bbb3fe 1263 * \param token_len is length of Token to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1264 *
GeofferyOmlette 0:f6e4e1bbb3fe 1265 * \param *token_ptr is pointer to Token data to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1266 *
GeofferyOmlette 0:f6e4e1bbb3fe 1267 * \param *addr_ptr is pointer to Address information to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1268 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1269 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1270 static void sn_coap_protocol_linked_list_ack_info_store(uint16_t msg_id, uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1271 {
GeofferyOmlette 0:f6e4e1bbb3fe 1272 coap_ack_info_s *stored_ack_info_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1273
GeofferyOmlette 0:f6e4e1bbb3fe 1274 /* Remove oldest ack infos from linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1275 if(sn_linked_list_count_nodes(global_linked_list_ack_info_ptr) >= SN_COAP_ACK_INFO_MAX_COUNT_MESSAGES_SAVED)
GeofferyOmlette 0:f6e4e1bbb3fe 1276 {
GeofferyOmlette 0:f6e4e1bbb3fe 1277 stored_ack_info_ptr = sn_linked_list_get_last_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1278
GeofferyOmlette 0:f6e4e1bbb3fe 1279 if(stored_ack_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1280 {
GeofferyOmlette 0:f6e4e1bbb3fe 1281 sn_linked_list_remove_current_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1282
GeofferyOmlette 0:f6e4e1bbb3fe 1283 if(stored_ack_info_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1284 sn_coap_protocol_free(stored_ack_info_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1285
GeofferyOmlette 0:f6e4e1bbb3fe 1286 if(stored_ack_info_ptr->token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1287 sn_coap_protocol_free(stored_ack_info_ptr->token_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1288
GeofferyOmlette 0:f6e4e1bbb3fe 1289 sn_coap_protocol_free(stored_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1290
GeofferyOmlette 0:f6e4e1bbb3fe 1291 stored_ack_info_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1292 }
GeofferyOmlette 0:f6e4e1bbb3fe 1293 }
GeofferyOmlette 0:f6e4e1bbb3fe 1294
GeofferyOmlette 0:f6e4e1bbb3fe 1295 /* * * * Allocating memory for stored Acknowledgement info * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1296
GeofferyOmlette 0:f6e4e1bbb3fe 1297 /* Allocate memory for stored Acknowledgement info's structure */
GeofferyOmlette 0:f6e4e1bbb3fe 1298 stored_ack_info_ptr = sn_coap_protocol_malloc(sizeof(coap_ack_info_s));
GeofferyOmlette 0:f6e4e1bbb3fe 1299
GeofferyOmlette 0:f6e4e1bbb3fe 1300 if (stored_ack_info_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1301 {
GeofferyOmlette 0:f6e4e1bbb3fe 1302 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1303 }
GeofferyOmlette 0:f6e4e1bbb3fe 1304
GeofferyOmlette 0:f6e4e1bbb3fe 1305 if(token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1306 {
GeofferyOmlette 0:f6e4e1bbb3fe 1307
GeofferyOmlette 0:f6e4e1bbb3fe 1308 /* Allocate memory for stored Acknowledgement info's token */
GeofferyOmlette 0:f6e4e1bbb3fe 1309 stored_ack_info_ptr->token_ptr = sn_coap_protocol_malloc(token_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1310
GeofferyOmlette 0:f6e4e1bbb3fe 1311 if (stored_ack_info_ptr->token_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1312 {
GeofferyOmlette 0:f6e4e1bbb3fe 1313 sn_coap_protocol_free(stored_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1314
GeofferyOmlette 0:f6e4e1bbb3fe 1315 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1316 }
GeofferyOmlette 0:f6e4e1bbb3fe 1317
GeofferyOmlette 0:f6e4e1bbb3fe 1318 }
GeofferyOmlette 0:f6e4e1bbb3fe 1319
GeofferyOmlette 0:f6e4e1bbb3fe 1320 /* Allocate memory for stored Acknowledgement info's address */
GeofferyOmlette 0:f6e4e1bbb3fe 1321 stored_ack_info_ptr->addr_ptr = sn_coap_protocol_malloc(addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1322
GeofferyOmlette 0:f6e4e1bbb3fe 1323 if (stored_ack_info_ptr->addr_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1324 {
GeofferyOmlette 0:f6e4e1bbb3fe 1325 sn_coap_protocol_free(stored_ack_info_ptr->token_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1326 sn_coap_protocol_free(stored_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1327 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1328 }
GeofferyOmlette 0:f6e4e1bbb3fe 1329
GeofferyOmlette 0:f6e4e1bbb3fe 1330 /* * * * Filling fields of stored Acknowledgement info * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1331
GeofferyOmlette 0:f6e4e1bbb3fe 1332 stored_ack_info_ptr->timestamp = global_system_time;
GeofferyOmlette 0:f6e4e1bbb3fe 1333 stored_ack_info_ptr->msg_id = msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 1334 stored_ack_info_ptr->token_len = token_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1335 if(token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1336 {
GeofferyOmlette 0:f6e4e1bbb3fe 1337 memcpy(stored_ack_info_ptr->token_ptr, token_ptr, token_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1338 }
GeofferyOmlette 0:f6e4e1bbb3fe 1339 else
GeofferyOmlette 0:f6e4e1bbb3fe 1340 {
GeofferyOmlette 0:f6e4e1bbb3fe 1341 stored_ack_info_ptr->token_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1342 }
GeofferyOmlette 0:f6e4e1bbb3fe 1343 memcpy(stored_ack_info_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1344 stored_ack_info_ptr->port = addr_ptr->port;
GeofferyOmlette 0:f6e4e1bbb3fe 1345
GeofferyOmlette 0:f6e4e1bbb3fe 1346 /* * * * Storing Acknowledgement info to Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1347
GeofferyOmlette 0:f6e4e1bbb3fe 1348 if(sn_linked_list_add_node(global_linked_list_ack_info_ptr, stored_ack_info_ptr) != 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1349 {
GeofferyOmlette 0:f6e4e1bbb3fe 1350 sn_coap_protocol_free(stored_ack_info_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1351 sn_coap_protocol_free(stored_ack_info_ptr->token_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1352 sn_coap_protocol_free(stored_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1353 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1354 }
GeofferyOmlette 0:f6e4e1bbb3fe 1355 }
GeofferyOmlette 0:f6e4e1bbb3fe 1356
GeofferyOmlette 0:f6e4e1bbb3fe 1357 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1358 * \fn static int32_t sn_coap_protocol_linked_list_ack_info_search(uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1359 *
GeofferyOmlette 0:f6e4e1bbb3fe 1360 * \brief Searches stored Message ID from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1361 *
GeofferyOmlette 0:f6e4e1bbb3fe 1362 * \param token_len is length of Token key to be searched
GeofferyOmlette 0:f6e4e1bbb3fe 1363 *
GeofferyOmlette 0:f6e4e1bbb3fe 1364 * \param *token_ptr is pointer to Token key to be searched
GeofferyOmlette 0:f6e4e1bbb3fe 1365 *
GeofferyOmlette 0:f6e4e1bbb3fe 1366 * \param *addr_ptr is pointer to Address key to be searched
GeofferyOmlette 0:f6e4e1bbb3fe 1367 *
GeofferyOmlette 0:f6e4e1bbb3fe 1368 * \return Return value is found Message ID. If Message ID not found, -1 is returned.
GeofferyOmlette 0:f6e4e1bbb3fe 1369 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1370 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1371 static int32_t sn_coap_protocol_linked_list_ack_info_search(uint16_t msg_id, uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1372 {
GeofferyOmlette 0:f6e4e1bbb3fe 1373 coap_ack_info_s *stored_ack_info_ptr = sn_linked_list_get_last_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1374 uint16_t stored_ack_info_count = sn_linked_list_count_nodes(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1375 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1376 uint8_t mem_cmp_result = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1377
GeofferyOmlette 0:f6e4e1bbb3fe 1378 if(!addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1379 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 1380
GeofferyOmlette 0:f6e4e1bbb3fe 1381 if(!addr_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1382 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 1383
GeofferyOmlette 0:f6e4e1bbb3fe 1384 /* Loop all nodes in Linked list for searching Message ID */
GeofferyOmlette 0:f6e4e1bbb3fe 1385 for (i = 0; i < stored_ack_info_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1386 {
GeofferyOmlette 0:f6e4e1bbb3fe 1387 if(!stored_ack_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1388 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 1389
GeofferyOmlette 0:f6e4e1bbb3fe 1390 /* If message's Token option is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1391 if(msg_id == stored_ack_info_ptr->msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1392 {
GeofferyOmlette 0:f6e4e1bbb3fe 1393 if(stored_ack_info_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1394 {
GeofferyOmlette 0:f6e4e1bbb3fe 1395 mem_cmp_result = memcmp(addr_ptr->addr_ptr, stored_ack_info_ptr->addr_ptr, addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1396
GeofferyOmlette 0:f6e4e1bbb3fe 1397 /* If message's Source address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1398 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1399 {
GeofferyOmlette 0:f6e4e1bbb3fe 1400 /* If message's Source address port is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1401 if (stored_ack_info_ptr->port == addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1402 {
GeofferyOmlette 0:f6e4e1bbb3fe 1403 if(stored_ack_info_ptr->token_ptr && token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1404 {
GeofferyOmlette 0:f6e4e1bbb3fe 1405 if(stored_ack_info_ptr->token_len == token_len)
GeofferyOmlette 0:f6e4e1bbb3fe 1406 {
GeofferyOmlette 0:f6e4e1bbb3fe 1407 mem_cmp_result = memcmp(token_ptr, stored_ack_info_ptr->token_ptr, token_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1408
GeofferyOmlette 0:f6e4e1bbb3fe 1409 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1410 {
GeofferyOmlette 0:f6e4e1bbb3fe 1411 /* ACK found and token match */
GeofferyOmlette 0:f6e4e1bbb3fe 1412 return stored_ack_info_ptr->msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 1413 }
GeofferyOmlette 0:f6e4e1bbb3fe 1414
GeofferyOmlette 0:f6e4e1bbb3fe 1415 }
GeofferyOmlette 0:f6e4e1bbb3fe 1416 return (-2); /* Token does not match */
GeofferyOmlette 0:f6e4e1bbb3fe 1417 }
GeofferyOmlette 0:f6e4e1bbb3fe 1418 else
GeofferyOmlette 0:f6e4e1bbb3fe 1419 {
GeofferyOmlette 0:f6e4e1bbb3fe 1420 /* * * Correct Acknowledgement info found * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1421 return stored_ack_info_ptr->msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 1422 }
GeofferyOmlette 0:f6e4e1bbb3fe 1423 }
GeofferyOmlette 0:f6e4e1bbb3fe 1424 }
GeofferyOmlette 0:f6e4e1bbb3fe 1425 }
GeofferyOmlette 0:f6e4e1bbb3fe 1426 }
GeofferyOmlette 0:f6e4e1bbb3fe 1427 /* Get next stored Acknowledgement info to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1428 stored_ack_info_ptr = sn_linked_list_get_previous_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1429 }
GeofferyOmlette 0:f6e4e1bbb3fe 1430
GeofferyOmlette 0:f6e4e1bbb3fe 1431 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 1432 }
GeofferyOmlette 0:f6e4e1bbb3fe 1433
GeofferyOmlette 0:f6e4e1bbb3fe 1434 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1435 * \fn static void sn_coap_protocol_linked_list_ack_info_remove(uint8_t token_len, uint8_t *token_ptr, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1436 *
GeofferyOmlette 0:f6e4e1bbb3fe 1437 * \brief Removes stored Acknowledgement info from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1438 *
GeofferyOmlette 0:f6e4e1bbb3fe 1439 * \param token_len is length of Token key to be removed
GeofferyOmlette 0:f6e4e1bbb3fe 1440 *
GeofferyOmlette 0:f6e4e1bbb3fe 1441 * \param *token_ptr is pointer to Token key to be removed
GeofferyOmlette 0:f6e4e1bbb3fe 1442 *
GeofferyOmlette 0:f6e4e1bbb3fe 1443 * \param *addr_ptr is pointer to Address key to be removed
GeofferyOmlette 0:f6e4e1bbb3fe 1444 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1445 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1446 static void sn_coap_protocol_linked_list_ack_info_remove(uint16_t msg_id, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1447 {
GeofferyOmlette 0:f6e4e1bbb3fe 1448 uint16_t stored_ack_info_count = sn_linked_list_count_nodes(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1449 coap_ack_info_s *stored_ack_info_ptr = sn_linked_list_get_last_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1450 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1451
GeofferyOmlette 0:f6e4e1bbb3fe 1452 if(!addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1453 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1454
GeofferyOmlette 0:f6e4e1bbb3fe 1455 if(!addr_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1456 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1457
GeofferyOmlette 0:f6e4e1bbb3fe 1458 /* Loop all stored Acknowledgement infos in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1459 for (i = 0; i < stored_ack_info_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1460 {
GeofferyOmlette 0:f6e4e1bbb3fe 1461 if(!stored_ack_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1462 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1463
GeofferyOmlette 0:f6e4e1bbb3fe 1464 /* If message's Token option is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1465 if (msg_id == stored_ack_info_ptr->msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1466 {
GeofferyOmlette 0:f6e4e1bbb3fe 1467
GeofferyOmlette 0:f6e4e1bbb3fe 1468 if (stored_ack_info_ptr->port == addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1469 {
GeofferyOmlette 0:f6e4e1bbb3fe 1470 if(stored_ack_info_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1471 {
GeofferyOmlette 0:f6e4e1bbb3fe 1472 /* If message's Address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1473 if (!memcmp(addr_ptr->addr_ptr, stored_ack_info_ptr->addr_ptr, addr_ptr->addr_len))
GeofferyOmlette 0:f6e4e1bbb3fe 1474 {
GeofferyOmlette 0:f6e4e1bbb3fe 1475 /* * * * Correct Acknowledgement info found, remove it from Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1476 stored_ack_info_ptr = sn_linked_list_remove_current_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1477
GeofferyOmlette 0:f6e4e1bbb3fe 1478 /* Free memory of stored Acknowledgement info */
GeofferyOmlette 0:f6e4e1bbb3fe 1479 if(stored_ack_info_ptr->token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1480 sn_coap_protocol_free(stored_ack_info_ptr->token_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1481
GeofferyOmlette 0:f6e4e1bbb3fe 1482 sn_coap_protocol_free(stored_ack_info_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1483 sn_coap_protocol_free(stored_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1484
GeofferyOmlette 0:f6e4e1bbb3fe 1485 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1486 }
GeofferyOmlette 0:f6e4e1bbb3fe 1487 }
GeofferyOmlette 0:f6e4e1bbb3fe 1488 }
GeofferyOmlette 0:f6e4e1bbb3fe 1489
GeofferyOmlette 0:f6e4e1bbb3fe 1490 }
GeofferyOmlette 0:f6e4e1bbb3fe 1491
GeofferyOmlette 0:f6e4e1bbb3fe 1492 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1493 stored_ack_info_ptr = sn_linked_list_get_previous_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1494 }
GeofferyOmlette 0:f6e4e1bbb3fe 1495 }
GeofferyOmlette 0:f6e4e1bbb3fe 1496
GeofferyOmlette 0:f6e4e1bbb3fe 1497 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1498 * \fn static void sn_coap_protocol_linked_list_ack_info_remove_old_ones(void)
GeofferyOmlette 0:f6e4e1bbb3fe 1499 *
GeofferyOmlette 0:f6e4e1bbb3fe 1500 * \brief Removes old stored Acknowledgement infos from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1501 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1502 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1503 static void sn_coap_protocol_linked_list_ack_info_remove_old_ones(void)
GeofferyOmlette 0:f6e4e1bbb3fe 1504 {
GeofferyOmlette 0:f6e4e1bbb3fe 1505 coap_ack_info_s *removed_ack_info_ptr = sn_linked_list_get_first_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1506
GeofferyOmlette 0:f6e4e1bbb3fe 1507 /* Loop all stored Acknowledgement infos in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1508 while(removed_ack_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1509 {
GeofferyOmlette 0:f6e4e1bbb3fe 1510 if ((global_system_time - removed_ack_info_ptr->timestamp) > SN_COAP_ACK_INFO_MAX_TIME_MSGS_STORED)
GeofferyOmlette 0:f6e4e1bbb3fe 1511 {
GeofferyOmlette 0:f6e4e1bbb3fe 1512 /* * * * Old Acknowledgement info found, remove it from Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1513 removed_ack_info_ptr = sn_linked_list_remove_current_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1514
GeofferyOmlette 0:f6e4e1bbb3fe 1515 /* Free memory of stored Acknowledgement info */
GeofferyOmlette 0:f6e4e1bbb3fe 1516 if(removed_ack_info_ptr->token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1517 {
GeofferyOmlette 0:f6e4e1bbb3fe 1518 sn_coap_protocol_free(removed_ack_info_ptr->token_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1519 }
GeofferyOmlette 0:f6e4e1bbb3fe 1520
GeofferyOmlette 0:f6e4e1bbb3fe 1521 if(removed_ack_info_ptr->addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1522 {
GeofferyOmlette 0:f6e4e1bbb3fe 1523 sn_coap_protocol_free(removed_ack_info_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1524 }
GeofferyOmlette 0:f6e4e1bbb3fe 1525
GeofferyOmlette 0:f6e4e1bbb3fe 1526 sn_coap_protocol_free(removed_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1527
GeofferyOmlette 0:f6e4e1bbb3fe 1528 /* Remove current node moved list automatically to next node. That is why we can fetch it now by calling get current node. */
GeofferyOmlette 0:f6e4e1bbb3fe 1529 removed_ack_info_ptr = sn_linked_list_get_current_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1530 }
GeofferyOmlette 0:f6e4e1bbb3fe 1531 else
GeofferyOmlette 0:f6e4e1bbb3fe 1532 {
GeofferyOmlette 0:f6e4e1bbb3fe 1533 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1534 removed_ack_info_ptr = sn_linked_list_get_next_node(global_linked_list_ack_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1535 }
GeofferyOmlette 0:f6e4e1bbb3fe 1536 }
GeofferyOmlette 0:f6e4e1bbb3fe 1537 }
GeofferyOmlette 0:f6e4e1bbb3fe 1538
GeofferyOmlette 0:f6e4e1bbb3fe 1539 static void sn_coap_protocol_send_rst(uint16_t msg_id, sn_nsdl_addr_s *addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1540 {
GeofferyOmlette 0:f6e4e1bbb3fe 1541 uint8_t packet_ptr[4];
GeofferyOmlette 0:f6e4e1bbb3fe 1542
GeofferyOmlette 0:f6e4e1bbb3fe 1543 /* Add CoAP version and message type */
GeofferyOmlette 0:f6e4e1bbb3fe 1544 packet_ptr[0] = COAP_VERSION_1;
GeofferyOmlette 0:f6e4e1bbb3fe 1545 packet_ptr[0] |= COAP_MSG_TYPE_RESET;
GeofferyOmlette 0:f6e4e1bbb3fe 1546
GeofferyOmlette 0:f6e4e1bbb3fe 1547 /* Add message code */
GeofferyOmlette 0:f6e4e1bbb3fe 1548 packet_ptr[1] = COAP_MSG_CODE_EMPTY;
GeofferyOmlette 0:f6e4e1bbb3fe 1549
GeofferyOmlette 0:f6e4e1bbb3fe 1550 /* Add message ID */
GeofferyOmlette 0:f6e4e1bbb3fe 1551 packet_ptr[2] = msg_id >> 8;
GeofferyOmlette 0:f6e4e1bbb3fe 1552 packet_ptr[3] = (uint8_t)msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 1553
GeofferyOmlette 0:f6e4e1bbb3fe 1554 /* Send RST */
GeofferyOmlette 0:f6e4e1bbb3fe 1555 sn_coap_tx_callback(SN_NSDL_PROTOCOL_COAP, packet_ptr, 4, addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1556
GeofferyOmlette 0:f6e4e1bbb3fe 1557 }
GeofferyOmlette 0:f6e4e1bbb3fe 1558 #if SN_COAP_DUPLICATION_MAX_MSGS_COUNT /* If Message duplication detection is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 1559
GeofferyOmlette 0:f6e4e1bbb3fe 1560 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1561 * \fn static void sn_coap_protocol_linked_list_duplication_info_store(sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1562 *
GeofferyOmlette 0:f6e4e1bbb3fe 1563 * \brief Stores Duplication info to Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1564 *
GeofferyOmlette 0:f6e4e1bbb3fe 1565 * \param msg_id is Message ID to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1566 * \param *addr_ptr is pointer to Address information to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1567 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1568 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1569 static void sn_coap_protocol_linked_list_duplication_info_store(sn_nsdl_addr_s *addr_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 1570 uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1571 {
GeofferyOmlette 0:f6e4e1bbb3fe 1572 coap_duplication_info_s *stored_duplication_info_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1573
GeofferyOmlette 0:f6e4e1bbb3fe 1574 /* * * * Allocating memory for stored Duplication info * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1575
GeofferyOmlette 0:f6e4e1bbb3fe 1576 /* Allocate memory for stored Duplication info's structure */
GeofferyOmlette 0:f6e4e1bbb3fe 1577 stored_duplication_info_ptr = sn_coap_protocol_malloc(sizeof(coap_duplication_info_s));
GeofferyOmlette 0:f6e4e1bbb3fe 1578
GeofferyOmlette 0:f6e4e1bbb3fe 1579 if (stored_duplication_info_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1580 {
GeofferyOmlette 0:f6e4e1bbb3fe 1581 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1582 }
GeofferyOmlette 0:f6e4e1bbb3fe 1583
GeofferyOmlette 0:f6e4e1bbb3fe 1584 /* Allocate memory for stored Duplication info's address */
GeofferyOmlette 0:f6e4e1bbb3fe 1585 stored_duplication_info_ptr->addr_ptr = sn_coap_protocol_malloc(addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1586
GeofferyOmlette 0:f6e4e1bbb3fe 1587 if (stored_duplication_info_ptr->addr_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1588 {
GeofferyOmlette 0:f6e4e1bbb3fe 1589 sn_coap_protocol_free(stored_duplication_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1590
GeofferyOmlette 0:f6e4e1bbb3fe 1591 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1592 }
GeofferyOmlette 0:f6e4e1bbb3fe 1593
GeofferyOmlette 0:f6e4e1bbb3fe 1594 /* * * * Filling fields of stored Duplication info * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1595
GeofferyOmlette 0:f6e4e1bbb3fe 1596 stored_duplication_info_ptr->timestamp = global_system_time;
GeofferyOmlette 0:f6e4e1bbb3fe 1597 stored_duplication_info_ptr->addr_len = addr_ptr->addr_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1598 memcpy(stored_duplication_info_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1599 stored_duplication_info_ptr->port = addr_ptr->port;
GeofferyOmlette 0:f6e4e1bbb3fe 1600 stored_duplication_info_ptr->msg_id = msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 1601
GeofferyOmlette 0:f6e4e1bbb3fe 1602 /* * * * Storing Duplication info to Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1603
GeofferyOmlette 0:f6e4e1bbb3fe 1604 sn_linked_list_add_node(global_linked_list_duplication_msgs_ptr, stored_duplication_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1605 }
GeofferyOmlette 0:f6e4e1bbb3fe 1606
GeofferyOmlette 0:f6e4e1bbb3fe 1607 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1608 * \fn static int8_t sn_coap_protocol_linked_list_duplication_info_search(sn_nsdl_addr_s *addr_ptr, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1609 *
GeofferyOmlette 0:f6e4e1bbb3fe 1610 * \brief Searches stored message from Linked list (Address and Message ID as key)
GeofferyOmlette 0:f6e4e1bbb3fe 1611 *
GeofferyOmlette 0:f6e4e1bbb3fe 1612 * \param *addr_ptr is pointer to Address key to be searched
GeofferyOmlette 0:f6e4e1bbb3fe 1613 * \param msg_id is Message ID key to be searched
GeofferyOmlette 0:f6e4e1bbb3fe 1614 *
GeofferyOmlette 0:f6e4e1bbb3fe 1615 * \return Return value is 0 when message found and -1 if not found
GeofferyOmlette 0:f6e4e1bbb3fe 1616 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1617 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1618 static int8_t sn_coap_protocol_linked_list_duplication_info_search(sn_nsdl_addr_s *addr_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 1619 uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1620 {
GeofferyOmlette 0:f6e4e1bbb3fe 1621 coap_duplication_info_s *stored_duplication_info_ptr = sn_linked_list_get_last_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1622 uint16_t stored_duplication_msgs_count = sn_linked_list_count_nodes(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1623 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1624
GeofferyOmlette 0:f6e4e1bbb3fe 1625 /* Loop all nodes in Linked list for searching Message ID */
GeofferyOmlette 0:f6e4e1bbb3fe 1626 for (i = 0; i < stored_duplication_msgs_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1627 {
GeofferyOmlette 0:f6e4e1bbb3fe 1628 /* If message's Message ID is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1629 if (stored_duplication_info_ptr->msg_id == msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1630 {
GeofferyOmlette 0:f6e4e1bbb3fe 1631 int8_t mem_cmp_result = memcmp(addr_ptr->addr_ptr, stored_duplication_info_ptr->addr_ptr, addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1632
GeofferyOmlette 0:f6e4e1bbb3fe 1633 /* If message's Source address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1634 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1635 {
GeofferyOmlette 0:f6e4e1bbb3fe 1636 /* If message's Source address port is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1637 if (stored_duplication_info_ptr->port == addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1638 {
GeofferyOmlette 0:f6e4e1bbb3fe 1639 /* * * Correct Duplication info found * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1640 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1641 }
GeofferyOmlette 0:f6e4e1bbb3fe 1642 }
GeofferyOmlette 0:f6e4e1bbb3fe 1643 }
GeofferyOmlette 0:f6e4e1bbb3fe 1644 /* Get next stored Duplication info to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1645 stored_duplication_info_ptr = sn_linked_list_get_previous_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1646 }
GeofferyOmlette 0:f6e4e1bbb3fe 1647
GeofferyOmlette 0:f6e4e1bbb3fe 1648 return -1;
GeofferyOmlette 0:f6e4e1bbb3fe 1649 }
GeofferyOmlette 0:f6e4e1bbb3fe 1650
GeofferyOmlette 0:f6e4e1bbb3fe 1651 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1652 * \fn static void sn_coap_protocol_linked_list_duplication_info_remove(uint8_t *addr_ptr, uint16_t port, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1653 *
GeofferyOmlette 0:f6e4e1bbb3fe 1654 * \brief Removes stored Duplication info from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1655 *
GeofferyOmlette 0:f6e4e1bbb3fe 1656 * \param *addr_ptr is pointer to Address key to be removed
GeofferyOmlette 0:f6e4e1bbb3fe 1657 *
GeofferyOmlette 0:f6e4e1bbb3fe 1658 * \param port is Port key to be removed
GeofferyOmlette 0:f6e4e1bbb3fe 1659 *
GeofferyOmlette 0:f6e4e1bbb3fe 1660 * \param msg_id is Message ID key to be removed
GeofferyOmlette 0:f6e4e1bbb3fe 1661 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1662 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1663 static void sn_coap_protocol_linked_list_duplication_info_remove(uint8_t *addr_ptr, uint16_t port, uint16_t msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1664 {
GeofferyOmlette 0:f6e4e1bbb3fe 1665 coap_duplication_info_s *removed_duplication_info_ptr = sn_linked_list_get_last_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1666 uint16_t stored_duplication_msgs_count = sn_linked_list_count_nodes(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1667 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1668
GeofferyOmlette 0:f6e4e1bbb3fe 1669 /* Loop all stored duplication messages in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1670 for (i = 0; i < stored_duplication_msgs_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1671 {
GeofferyOmlette 0:f6e4e1bbb3fe 1672 int8_t mem_cmp_result = memcmp(addr_ptr, removed_duplication_info_ptr->addr_ptr, removed_duplication_info_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1673
GeofferyOmlette 0:f6e4e1bbb3fe 1674 /* If message's Address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1675 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1676 {
GeofferyOmlette 0:f6e4e1bbb3fe 1677 /* If message's Address prt is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1678 if (removed_duplication_info_ptr->port == port)
GeofferyOmlette 0:f6e4e1bbb3fe 1679 {
GeofferyOmlette 0:f6e4e1bbb3fe 1680 /* If Message ID is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1681 if (removed_duplication_info_ptr->msg_id == msg_id)
GeofferyOmlette 0:f6e4e1bbb3fe 1682 {
GeofferyOmlette 0:f6e4e1bbb3fe 1683 /* * * * Correct Duplication info found, remove it from Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1684 removed_duplication_info_ptr = sn_linked_list_remove_current_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1685
GeofferyOmlette 0:f6e4e1bbb3fe 1686 /* Free memory of stored Duplication info */
GeofferyOmlette 0:f6e4e1bbb3fe 1687 sn_coap_protocol_free(removed_duplication_info_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1688 sn_coap_protocol_free(removed_duplication_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1689
GeofferyOmlette 0:f6e4e1bbb3fe 1690 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1691 }
GeofferyOmlette 0:f6e4e1bbb3fe 1692 }
GeofferyOmlette 0:f6e4e1bbb3fe 1693 }
GeofferyOmlette 0:f6e4e1bbb3fe 1694
GeofferyOmlette 0:f6e4e1bbb3fe 1695 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1696 removed_duplication_info_ptr = sn_linked_list_get_previous_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1697 }
GeofferyOmlette 0:f6e4e1bbb3fe 1698 }
GeofferyOmlette 0:f6e4e1bbb3fe 1699
GeofferyOmlette 0:f6e4e1bbb3fe 1700 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1701 * \fn static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(void)
GeofferyOmlette 0:f6e4e1bbb3fe 1702 *
GeofferyOmlette 0:f6e4e1bbb3fe 1703 * \brief Removes old stored Duplication detection infos from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1704 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1705 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1706 static void sn_coap_protocol_linked_list_duplication_info_remove_old_ones(void)
GeofferyOmlette 0:f6e4e1bbb3fe 1707 {
GeofferyOmlette 0:f6e4e1bbb3fe 1708 coap_duplication_info_s *removed_duplication_info_ptr = sn_linked_list_get_first_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1709
GeofferyOmlette 0:f6e4e1bbb3fe 1710 /* Loop all stored duplication messages in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1711 while(removed_duplication_info_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1712 {
GeofferyOmlette 0:f6e4e1bbb3fe 1713 if ((global_system_time - removed_duplication_info_ptr->timestamp) > SN_COAP_DUPLICATION_MAX_TIME_MSGS_STORED)
GeofferyOmlette 0:f6e4e1bbb3fe 1714 {
GeofferyOmlette 0:f6e4e1bbb3fe 1715 /* * * * Old Duplication info found, remove it from Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1716 removed_duplication_info_ptr = sn_linked_list_remove_current_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1717
GeofferyOmlette 0:f6e4e1bbb3fe 1718 /* Free memory of stored Duplication info */
GeofferyOmlette 0:f6e4e1bbb3fe 1719 sn_coap_protocol_free(removed_duplication_info_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1720 sn_coap_protocol_free(removed_duplication_info_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1721
GeofferyOmlette 0:f6e4e1bbb3fe 1722 removed_duplication_info_ptr = sn_linked_list_get_current_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1723 }
GeofferyOmlette 0:f6e4e1bbb3fe 1724 else
GeofferyOmlette 0:f6e4e1bbb3fe 1725 {
GeofferyOmlette 0:f6e4e1bbb3fe 1726 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1727 removed_duplication_info_ptr = sn_linked_list_get_next_node(global_linked_list_duplication_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1728 }
GeofferyOmlette 0:f6e4e1bbb3fe 1729 }
GeofferyOmlette 0:f6e4e1bbb3fe 1730 }
GeofferyOmlette 0:f6e4e1bbb3fe 1731
GeofferyOmlette 0:f6e4e1bbb3fe 1732 #endif /* SN_COAP_DUPLICATION_MAX_MSGS_COUNT */
GeofferyOmlette 0:f6e4e1bbb3fe 1733
GeofferyOmlette 0:f6e4e1bbb3fe 1734 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE
GeofferyOmlette 0:f6e4e1bbb3fe 1735 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1736 * \fn static void sn_coap_protocol_linked_list_blockwise_msg_remove_current()
GeofferyOmlette 0:f6e4e1bbb3fe 1737 *
GeofferyOmlette 0:f6e4e1bbb3fe 1738 * \brief Removes current stored blockwise message from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1739 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1740 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1741 static void sn_coap_protocol_linked_list_blockwise_msg_remove_current()
GeofferyOmlette 0:f6e4e1bbb3fe 1742 {
GeofferyOmlette 0:f6e4e1bbb3fe 1743 coap_blockwise_msg_s *removed_msg_ptr = sn_linked_list_remove_current_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1744
GeofferyOmlette 0:f6e4e1bbb3fe 1745 if (removed_msg_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1746 {
GeofferyOmlette 0:f6e4e1bbb3fe 1747 if(removed_msg_ptr->coap_msg_ptr->payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1748 sn_coap_protocol_free(removed_msg_ptr->coap_msg_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1749
GeofferyOmlette 0:f6e4e1bbb3fe 1750 sn_coap_parser_release_allocated_coap_msg_mem(removed_msg_ptr->coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1751
GeofferyOmlette 0:f6e4e1bbb3fe 1752 sn_coap_protocol_free(removed_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1753 }
GeofferyOmlette 0:f6e4e1bbb3fe 1754 }
GeofferyOmlette 0:f6e4e1bbb3fe 1755
GeofferyOmlette 0:f6e4e1bbb3fe 1756 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1757 * \fn static void sn_coap_protocol_linked_list_blockwise_payload_store(sn_nsdl_addr_s *addr_ptr, uint16_t stored_payload_len, uint8_t *stored_payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1758 *
GeofferyOmlette 0:f6e4e1bbb3fe 1759 * \brief Stores blockwise payload to Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1760 *
GeofferyOmlette 0:f6e4e1bbb3fe 1761 * \param *addr_ptr is pointer to Address information to be stored
GeofferyOmlette 0:f6e4e1bbb3fe 1762 * \param stored_payload_len is length of stored Payload
GeofferyOmlette 0:f6e4e1bbb3fe 1763 * \param *stored_payload_ptr is pointer to stored Payload
GeofferyOmlette 0:f6e4e1bbb3fe 1764 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1765 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1766 static void sn_coap_protocol_linked_list_blockwise_payload_store(sn_nsdl_addr_s *addr_ptr,//TODO: addr + header parametreiksi, blokin offset talteen.
GeofferyOmlette 0:f6e4e1bbb3fe 1767 uint16_t stored_payload_len,
GeofferyOmlette 0:f6e4e1bbb3fe 1768 uint8_t *stored_payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1769 {
GeofferyOmlette 0:f6e4e1bbb3fe 1770 if(!addr_ptr || !stored_payload_len || !stored_payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1771 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1772
GeofferyOmlette 0:f6e4e1bbb3fe 1773 coap_blockwise_payload_s *stored_blockwise_payload_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1774
GeofferyOmlette 0:f6e4e1bbb3fe 1775 /* * * * Allocating memory for stored Payload * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1776
GeofferyOmlette 0:f6e4e1bbb3fe 1777 /* Allocate memory for stored Payload's structure */
GeofferyOmlette 0:f6e4e1bbb3fe 1778 stored_blockwise_payload_ptr = sn_coap_protocol_malloc(sizeof(coap_blockwise_payload_s));
GeofferyOmlette 0:f6e4e1bbb3fe 1779
GeofferyOmlette 0:f6e4e1bbb3fe 1780 if (stored_blockwise_payload_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1781 {
GeofferyOmlette 0:f6e4e1bbb3fe 1782 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1783 }
GeofferyOmlette 0:f6e4e1bbb3fe 1784
GeofferyOmlette 0:f6e4e1bbb3fe 1785 /* Allocate memory for stored Payload's data */
GeofferyOmlette 0:f6e4e1bbb3fe 1786 stored_blockwise_payload_ptr->payload_ptr = sn_coap_protocol_malloc(stored_payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1787
GeofferyOmlette 0:f6e4e1bbb3fe 1788 if (stored_blockwise_payload_ptr->payload_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1789 {
GeofferyOmlette 0:f6e4e1bbb3fe 1790 sn_coap_protocol_free(stored_blockwise_payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1791
GeofferyOmlette 0:f6e4e1bbb3fe 1792 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1793 }
GeofferyOmlette 0:f6e4e1bbb3fe 1794
GeofferyOmlette 0:f6e4e1bbb3fe 1795 /* Allocate memory for stored Payload's address */
GeofferyOmlette 0:f6e4e1bbb3fe 1796 stored_blockwise_payload_ptr->addr_ptr = sn_coap_protocol_malloc(addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1797
GeofferyOmlette 0:f6e4e1bbb3fe 1798 if (stored_blockwise_payload_ptr->addr_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1799 {
GeofferyOmlette 0:f6e4e1bbb3fe 1800 sn_coap_protocol_free(stored_blockwise_payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1801 sn_coap_protocol_free(stored_blockwise_payload_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1802
GeofferyOmlette 0:f6e4e1bbb3fe 1803 return;
GeofferyOmlette 0:f6e4e1bbb3fe 1804 }
GeofferyOmlette 0:f6e4e1bbb3fe 1805
GeofferyOmlette 0:f6e4e1bbb3fe 1806 /* * * * Filling fields of stored Payload * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1807
GeofferyOmlette 0:f6e4e1bbb3fe 1808 stored_blockwise_payload_ptr->timestamp = global_system_time;
GeofferyOmlette 0:f6e4e1bbb3fe 1809
GeofferyOmlette 0:f6e4e1bbb3fe 1810 memcpy(stored_blockwise_payload_ptr->addr_ptr, addr_ptr->addr_ptr, addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1811 stored_blockwise_payload_ptr->port = addr_ptr->port;
GeofferyOmlette 0:f6e4e1bbb3fe 1812 memcpy(stored_blockwise_payload_ptr->payload_ptr, stored_payload_ptr, stored_payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1813 stored_blockwise_payload_ptr->payload_len = stored_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1814
GeofferyOmlette 0:f6e4e1bbb3fe 1815 /* * * * Storing Payload to Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1816
GeofferyOmlette 0:f6e4e1bbb3fe 1817 sn_linked_list_add_node(global_linked_list_blockwise_received_payloads_ptr, stored_blockwise_payload_ptr);//TODO: hukkAS
GeofferyOmlette 0:f6e4e1bbb3fe 1818 }
GeofferyOmlette 0:f6e4e1bbb3fe 1819
GeofferyOmlette 0:f6e4e1bbb3fe 1820 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1821 * \fn static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length)
GeofferyOmlette 0:f6e4e1bbb3fe 1822 *
GeofferyOmlette 0:f6e4e1bbb3fe 1823 * \brief Searches stored blockwise payload from Linked list (Address as key)
GeofferyOmlette 0:f6e4e1bbb3fe 1824 *
GeofferyOmlette 0:f6e4e1bbb3fe 1825 * \param *addr_ptr is pointer to Address key to be searched
GeofferyOmlette 0:f6e4e1bbb3fe 1826 * \param *payload_length is pointer to returned Payload length
GeofferyOmlette 0:f6e4e1bbb3fe 1827 *
GeofferyOmlette 0:f6e4e1bbb3fe 1828 * \return Return value is pointer to found stored blockwise payload in Linked
GeofferyOmlette 0:f6e4e1bbb3fe 1829 * list or NULL if payload not found
GeofferyOmlette 0:f6e4e1bbb3fe 1830 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1831 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1832 static uint8_t *sn_coap_protocol_linked_list_blockwise_payload_search(sn_nsdl_addr_s *src_addr_ptr, uint16_t *payload_length)
GeofferyOmlette 0:f6e4e1bbb3fe 1833 {
GeofferyOmlette 0:f6e4e1bbb3fe 1834 coap_blockwise_payload_s *stored_payload_info_ptr = sn_linked_list_get_last_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1835 uint16_t stored_blockwise_payloads_count = sn_linked_list_count_nodes(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1836 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1837
GeofferyOmlette 0:f6e4e1bbb3fe 1838 /* Loop all stored blockwise payloads in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1839 for (i = 0; i < stored_blockwise_payloads_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1840 {
GeofferyOmlette 0:f6e4e1bbb3fe 1841 int8_t mem_cmp_result = memcmp(src_addr_ptr->addr_ptr, stored_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1842
GeofferyOmlette 0:f6e4e1bbb3fe 1843 /* If payload's Source address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1844 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1845 {
GeofferyOmlette 0:f6e4e1bbb3fe 1846 /* If payload's Source address port is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1847 if (stored_payload_info_ptr->port == src_addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1848 {
GeofferyOmlette 0:f6e4e1bbb3fe 1849 /* * * Correct Payload found * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1850 *payload_length = stored_payload_info_ptr->payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1851
GeofferyOmlette 0:f6e4e1bbb3fe 1852 return stored_payload_info_ptr->payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 1853 }
GeofferyOmlette 0:f6e4e1bbb3fe 1854 }
GeofferyOmlette 0:f6e4e1bbb3fe 1855
GeofferyOmlette 0:f6e4e1bbb3fe 1856 /* Get next stored payload to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1857 stored_payload_info_ptr = sn_linked_list_get_previous_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1858 }
GeofferyOmlette 0:f6e4e1bbb3fe 1859
GeofferyOmlette 0:f6e4e1bbb3fe 1860 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1861 }
GeofferyOmlette 0:f6e4e1bbb3fe 1862
GeofferyOmlette 0:f6e4e1bbb3fe 1863 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1864 * \fn static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest()
GeofferyOmlette 0:f6e4e1bbb3fe 1865 *
GeofferyOmlette 0:f6e4e1bbb3fe 1866 * \brief Removes current stored blockwise paylod from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1867 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1868 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1869 static void sn_coap_protocol_linked_list_blockwise_payload_remove_oldest()
GeofferyOmlette 0:f6e4e1bbb3fe 1870 {
GeofferyOmlette 0:f6e4e1bbb3fe 1871 coap_blockwise_payload_s *removed_payload_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1872
GeofferyOmlette 0:f6e4e1bbb3fe 1873 /* Set Linked list to point oldest node */
GeofferyOmlette 0:f6e4e1bbb3fe 1874 sn_linked_list_get_last_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1875
GeofferyOmlette 0:f6e4e1bbb3fe 1876 /* Remove oldest node in Linked list*/
GeofferyOmlette 0:f6e4e1bbb3fe 1877 removed_payload_ptr = sn_linked_list_remove_current_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1878
GeofferyOmlette 0:f6e4e1bbb3fe 1879 /* Free memory of stored payload */
GeofferyOmlette 0:f6e4e1bbb3fe 1880 if (removed_payload_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1881 {
GeofferyOmlette 0:f6e4e1bbb3fe 1882 if (removed_payload_ptr->addr_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1883 {
GeofferyOmlette 0:f6e4e1bbb3fe 1884 sn_coap_protocol_free(removed_payload_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1885 }
GeofferyOmlette 0:f6e4e1bbb3fe 1886
GeofferyOmlette 0:f6e4e1bbb3fe 1887 if (removed_payload_ptr->payload_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1888 {
GeofferyOmlette 0:f6e4e1bbb3fe 1889 sn_coap_protocol_free(removed_payload_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1890 }
GeofferyOmlette 0:f6e4e1bbb3fe 1891
GeofferyOmlette 0:f6e4e1bbb3fe 1892 sn_coap_protocol_free(removed_payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1893 }
GeofferyOmlette 0:f6e4e1bbb3fe 1894 }
GeofferyOmlette 0:f6e4e1bbb3fe 1895
GeofferyOmlette 0:f6e4e1bbb3fe 1896 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1897 * \fn static void sn_coap_protocol_linked_list_blockwise_payload_remove_current()
GeofferyOmlette 0:f6e4e1bbb3fe 1898 *
GeofferyOmlette 0:f6e4e1bbb3fe 1899 * \brief Removes current stored blockwise paylod from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1900 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1901 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1902 static void sn_coap_protocol_linked_list_blockwise_payload_remove_current()
GeofferyOmlette 0:f6e4e1bbb3fe 1903 {
GeofferyOmlette 0:f6e4e1bbb3fe 1904 coap_blockwise_payload_s *removed_payload_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 1905
GeofferyOmlette 0:f6e4e1bbb3fe 1906 /* Remove oldest node in Linked list*/
GeofferyOmlette 0:f6e4e1bbb3fe 1907 removed_payload_ptr = sn_linked_list_remove_current_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1908
GeofferyOmlette 0:f6e4e1bbb3fe 1909 /* Free memory of stored payload */
GeofferyOmlette 0:f6e4e1bbb3fe 1910 if (removed_payload_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1911 {
GeofferyOmlette 0:f6e4e1bbb3fe 1912 if (removed_payload_ptr->addr_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1913 {
GeofferyOmlette 0:f6e4e1bbb3fe 1914 sn_coap_protocol_free(removed_payload_ptr->addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1915 }
GeofferyOmlette 0:f6e4e1bbb3fe 1916
GeofferyOmlette 0:f6e4e1bbb3fe 1917 if (removed_payload_ptr->payload_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 1918 {
GeofferyOmlette 0:f6e4e1bbb3fe 1919 sn_coap_protocol_free(removed_payload_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1920 }
GeofferyOmlette 0:f6e4e1bbb3fe 1921
GeofferyOmlette 0:f6e4e1bbb3fe 1922 sn_coap_protocol_free(removed_payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1923 }
GeofferyOmlette 0:f6e4e1bbb3fe 1924 }
GeofferyOmlette 0:f6e4e1bbb3fe 1925
GeofferyOmlette 0:f6e4e1bbb3fe 1926 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1927 * \fn static uint16_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(sn_nsdl_addr_s *src_addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1928 *
GeofferyOmlette 0:f6e4e1bbb3fe 1929 * \brief Counts length of Payloads in Linked list (Address as key)
GeofferyOmlette 0:f6e4e1bbb3fe 1930 *
GeofferyOmlette 0:f6e4e1bbb3fe 1931 * \param *addr_ptr is pointer to Address key
GeofferyOmlette 0:f6e4e1bbb3fe 1932 *
GeofferyOmlette 0:f6e4e1bbb3fe 1933 * \return Return value is length of Payloads as bytes
GeofferyOmlette 0:f6e4e1bbb3fe 1934 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1935 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1936 static uint16_t sn_coap_protocol_linked_list_blockwise_payloads_get_len(sn_nsdl_addr_s *src_addr_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1937 {
GeofferyOmlette 0:f6e4e1bbb3fe 1938 coap_blockwise_payload_s *searched_payload_info_ptr = sn_linked_list_get_last_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1939 uint16_t stored_blockwise_payloads_count = sn_linked_list_count_nodes(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1940 uint8_t i = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1941 uint16_t ret_whole_payload_len = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 1942
GeofferyOmlette 0:f6e4e1bbb3fe 1943 /* Loop all stored blockwise payloads in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1944 for (i = 0; i < stored_blockwise_payloads_count; i++)
GeofferyOmlette 0:f6e4e1bbb3fe 1945 {
GeofferyOmlette 0:f6e4e1bbb3fe 1946 int8_t mem_cmp_result = memcmp(src_addr_ptr->addr_ptr, searched_payload_info_ptr->addr_ptr, src_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 1947
GeofferyOmlette 0:f6e4e1bbb3fe 1948 /* If payload's Source address is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1949 if (mem_cmp_result == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 1950 {
GeofferyOmlette 0:f6e4e1bbb3fe 1951 /* If payload's Source address port is same than is searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1952 if (searched_payload_info_ptr->port == src_addr_ptr->port)
GeofferyOmlette 0:f6e4e1bbb3fe 1953 {
GeofferyOmlette 0:f6e4e1bbb3fe 1954 /* * * Correct Payload found * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1955 ret_whole_payload_len += searched_payload_info_ptr->payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1956 }
GeofferyOmlette 0:f6e4e1bbb3fe 1957 }
GeofferyOmlette 0:f6e4e1bbb3fe 1958
GeofferyOmlette 0:f6e4e1bbb3fe 1959 /* Get next stored payload to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1960 searched_payload_info_ptr = sn_linked_list_get_previous_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1961 }
GeofferyOmlette 0:f6e4e1bbb3fe 1962
GeofferyOmlette 0:f6e4e1bbb3fe 1963 return ret_whole_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 1964 }
GeofferyOmlette 0:f6e4e1bbb3fe 1965
GeofferyOmlette 0:f6e4e1bbb3fe 1966 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 1967 * \fn static void sn_coap_protocol_linked_list_blockwise_remove_old_data(void)
GeofferyOmlette 0:f6e4e1bbb3fe 1968 *
GeofferyOmlette 0:f6e4e1bbb3fe 1969 * \brief Removes old stored Blockwise messages and payloads from Linked list
GeofferyOmlette 0:f6e4e1bbb3fe 1970 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 1971 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 1972 static void sn_coap_protocol_linked_list_blockwise_remove_old_data(void)
GeofferyOmlette 0:f6e4e1bbb3fe 1973 {
GeofferyOmlette 0:f6e4e1bbb3fe 1974 coap_blockwise_msg_s *removed_blocwise_msg_ptr = sn_linked_list_get_first_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1975 coap_blockwise_payload_s *removed_blocwise_payload_ptr = sn_linked_list_get_first_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1976
GeofferyOmlette 0:f6e4e1bbb3fe 1977 /* Loop all stored Blockwise messages in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1978 while(removed_blocwise_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1979 {
GeofferyOmlette 0:f6e4e1bbb3fe 1980 if ((global_system_time - removed_blocwise_msg_ptr->timestamp) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED)
GeofferyOmlette 0:f6e4e1bbb3fe 1981 {
GeofferyOmlette 0:f6e4e1bbb3fe 1982 /* * * * Old Blockise message found, remove it from Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1983 sn_coap_protocol_linked_list_blockwise_msg_remove_current();
GeofferyOmlette 0:f6e4e1bbb3fe 1984 removed_blocwise_msg_ptr = sn_linked_list_get_current_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1985 }
GeofferyOmlette 0:f6e4e1bbb3fe 1986 else
GeofferyOmlette 0:f6e4e1bbb3fe 1987 {
GeofferyOmlette 0:f6e4e1bbb3fe 1988 /* Get next stored message to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 1989 removed_blocwise_msg_ptr = sn_linked_list_get_next_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 1990 }
GeofferyOmlette 0:f6e4e1bbb3fe 1991 }
GeofferyOmlette 0:f6e4e1bbb3fe 1992
GeofferyOmlette 0:f6e4e1bbb3fe 1993 /* Loop all stored Blockwise payloads in Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 1994 while(removed_blocwise_payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 1995 {
GeofferyOmlette 0:f6e4e1bbb3fe 1996 if ((global_system_time - removed_blocwise_payload_ptr->timestamp) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED)
GeofferyOmlette 0:f6e4e1bbb3fe 1997 {
GeofferyOmlette 0:f6e4e1bbb3fe 1998 /* * * * Old Blockise payload found, remove it from Linked list * * * */
GeofferyOmlette 0:f6e4e1bbb3fe 1999 sn_coap_protocol_linked_list_blockwise_payload_remove_current();
GeofferyOmlette 0:f6e4e1bbb3fe 2000 removed_blocwise_payload_ptr = sn_linked_list_get_current_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2001
GeofferyOmlette 0:f6e4e1bbb3fe 2002 }
GeofferyOmlette 0:f6e4e1bbb3fe 2003 else
GeofferyOmlette 0:f6e4e1bbb3fe 2004 {
GeofferyOmlette 0:f6e4e1bbb3fe 2005 /* Get next stored payload to be searched */
GeofferyOmlette 0:f6e4e1bbb3fe 2006 removed_blocwise_payload_ptr = sn_linked_list_get_next_node(global_linked_list_blockwise_received_payloads_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2007 }
GeofferyOmlette 0:f6e4e1bbb3fe 2008 }
GeofferyOmlette 0:f6e4e1bbb3fe 2009 }
GeofferyOmlette 0:f6e4e1bbb3fe 2010
GeofferyOmlette 0:f6e4e1bbb3fe 2011 #endif /* SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE */
GeofferyOmlette 0:f6e4e1bbb3fe 2012
GeofferyOmlette 0:f6e4e1bbb3fe 2013
GeofferyOmlette 0:f6e4e1bbb3fe 2014 #if ENABLE_RESENDINGS /* If Message resending is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 2015 /***************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 2016 * \fn int8_t sn_coap_protocol_allocate_mem_for_msg(sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len, coap_send_msg_s *msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2017 *
GeofferyOmlette 0:f6e4e1bbb3fe 2018 * \brief Allocates memory for given message (send or blockwise message)
GeofferyOmlette 0:f6e4e1bbb3fe 2019 *
GeofferyOmlette 0:f6e4e1bbb3fe 2020 * \param *dst_addr_ptr is pointer to destination address where message will be sent
GeofferyOmlette 0:f6e4e1bbb3fe 2021 * \param packet_data_len is length of allocated Packet data
GeofferyOmlette 0:f6e4e1bbb3fe 2022 *
GeofferyOmlette 0:f6e4e1bbb3fe 2023 * \return pointer to allocated struct
GeofferyOmlette 0:f6e4e1bbb3fe 2024 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 2025 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 2026 coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len)
GeofferyOmlette 0:f6e4e1bbb3fe 2027 {
GeofferyOmlette 0:f6e4e1bbb3fe 2028
GeofferyOmlette 0:f6e4e1bbb3fe 2029 coap_send_msg_s *msg_ptr = sn_coap_protocol_malloc(sizeof(coap_send_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2030
GeofferyOmlette 0:f6e4e1bbb3fe 2031 if (msg_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2032 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2033
GeofferyOmlette 0:f6e4e1bbb3fe 2034 memset(msg_ptr, 0, sizeof(coap_send_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2035
GeofferyOmlette 0:f6e4e1bbb3fe 2036 msg_ptr->send_msg_ptr = sn_coap_protocol_malloc(sizeof(sn_nsdl_transmit_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2037
GeofferyOmlette 0:f6e4e1bbb3fe 2038 if (msg_ptr->send_msg_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2039 {
GeofferyOmlette 0:f6e4e1bbb3fe 2040 sn_coap_protocol_release_allocated_send_msg_mem(msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2041 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2042 }
GeofferyOmlette 0:f6e4e1bbb3fe 2043
GeofferyOmlette 0:f6e4e1bbb3fe 2044 memset(msg_ptr->send_msg_ptr, 0 ,sizeof(sn_nsdl_transmit_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2045
GeofferyOmlette 0:f6e4e1bbb3fe 2046 msg_ptr->send_msg_ptr->dst_addr_ptr = sn_coap_protocol_malloc(sizeof(sn_nsdl_addr_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2047
GeofferyOmlette 0:f6e4e1bbb3fe 2048 if (msg_ptr->send_msg_ptr->dst_addr_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2049 {
GeofferyOmlette 0:f6e4e1bbb3fe 2050 sn_coap_protocol_release_allocated_send_msg_mem(msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2051 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2052 }
GeofferyOmlette 0:f6e4e1bbb3fe 2053
GeofferyOmlette 0:f6e4e1bbb3fe 2054 memset(msg_ptr->send_msg_ptr->dst_addr_ptr, 0, sizeof(sn_nsdl_addr_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2055
GeofferyOmlette 0:f6e4e1bbb3fe 2056 msg_ptr->send_msg_ptr->packet_ptr = sn_coap_protocol_malloc(packet_data_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2057
GeofferyOmlette 0:f6e4e1bbb3fe 2058 if (msg_ptr->send_msg_ptr->packet_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2059 {
GeofferyOmlette 0:f6e4e1bbb3fe 2060 sn_coap_protocol_release_allocated_send_msg_mem(msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2061 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2062 }
GeofferyOmlette 0:f6e4e1bbb3fe 2063
GeofferyOmlette 0:f6e4e1bbb3fe 2064 msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr = sn_coap_protocol_malloc(dst_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2065
GeofferyOmlette 0:f6e4e1bbb3fe 2066 if (msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2067 {
GeofferyOmlette 0:f6e4e1bbb3fe 2068 sn_coap_protocol_release_allocated_send_msg_mem(msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2069 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2070 }
GeofferyOmlette 0:f6e4e1bbb3fe 2071
GeofferyOmlette 0:f6e4e1bbb3fe 2072 memset(msg_ptr->send_msg_ptr->dst_addr_ptr->addr_ptr, 0, dst_addr_ptr->addr_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2073
GeofferyOmlette 0:f6e4e1bbb3fe 2074 return msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2075 }
GeofferyOmlette 0:f6e4e1bbb3fe 2076
GeofferyOmlette 0:f6e4e1bbb3fe 2077
GeofferyOmlette 0:f6e4e1bbb3fe 2078 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 2079 * \fn static void sn_coap_protocol_release_allocated_send_msg_mem(coap_send_msg_s *freed_send_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2080 *
GeofferyOmlette 0:f6e4e1bbb3fe 2081 * \brief Releases memory of given Sending message (coap_send_msg_s)
GeofferyOmlette 0:f6e4e1bbb3fe 2082 *
GeofferyOmlette 0:f6e4e1bbb3fe 2083 * \param *freed_send_msg_ptr is pointer to released Sending message
GeofferyOmlette 0:f6e4e1bbb3fe 2084 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 2085 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 2086 static void sn_coap_protocol_release_allocated_send_msg_mem(coap_send_msg_s *freed_send_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2087 {
GeofferyOmlette 0:f6e4e1bbb3fe 2088 if (freed_send_msg_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2089 {
GeofferyOmlette 0:f6e4e1bbb3fe 2090 sn_coap_builder_release_allocated_send_msg_mem(freed_send_msg_ptr->send_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2091 sn_coap_protocol_free(freed_send_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2092 }
GeofferyOmlette 0:f6e4e1bbb3fe 2093 }
GeofferyOmlette 0:f6e4e1bbb3fe 2094
GeofferyOmlette 0:f6e4e1bbb3fe 2095 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 2096 * \fn static uint16_t sn_coap_count_linked_list_size(sn_linked_list_t *linked_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2097 *
GeofferyOmlette 0:f6e4e1bbb3fe 2098 * \brief Counts total message size of all messages in linked list
GeofferyOmlette 0:f6e4e1bbb3fe 2099 *
GeofferyOmlette 0:f6e4e1bbb3fe 2100 * \param sn_linked_list_t *linked_list_ptr pointer to linked list
GeofferyOmlette 0:f6e4e1bbb3fe 2101 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 2102 static uint16_t sn_coap_count_linked_list_size(sn_linked_list_t *linked_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2103 {
GeofferyOmlette 0:f6e4e1bbb3fe 2104 uint16_t total_size = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2105 uint16_t message_count = sn_linked_list_count_nodes(linked_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2106 coap_send_msg_s *stored_msg_ptr = sn_linked_list_get_first_node(linked_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2107
GeofferyOmlette 0:f6e4e1bbb3fe 2108 while(message_count--)
GeofferyOmlette 0:f6e4e1bbb3fe 2109 {
GeofferyOmlette 0:f6e4e1bbb3fe 2110 if(stored_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2111 {
GeofferyOmlette 0:f6e4e1bbb3fe 2112 if(stored_msg_ptr->send_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2113 total_size += stored_msg_ptr->send_msg_ptr->packet_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2114 }
GeofferyOmlette 0:f6e4e1bbb3fe 2115 stored_msg_ptr = sn_linked_list_get_next_node(linked_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2116 }
GeofferyOmlette 0:f6e4e1bbb3fe 2117
GeofferyOmlette 0:f6e4e1bbb3fe 2118 return total_size;
GeofferyOmlette 0:f6e4e1bbb3fe 2119 }
GeofferyOmlette 0:f6e4e1bbb3fe 2120
GeofferyOmlette 0:f6e4e1bbb3fe 2121 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 2122 #if SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
GeofferyOmlette 0:f6e4e1bbb3fe 2123
GeofferyOmlette 0:f6e4e1bbb3fe 2124 /**************************************************************************//**
GeofferyOmlette 0:f6e4e1bbb3fe 2125 * \fn static int8_t sn_coap_handle_blockwise_message(void)
GeofferyOmlette 0:f6e4e1bbb3fe 2126 *
GeofferyOmlette 0:f6e4e1bbb3fe 2127 * \brief Handles all received blockwise messages
GeofferyOmlette 0:f6e4e1bbb3fe 2128 *
GeofferyOmlette 0:f6e4e1bbb3fe 2129 * \param *src_addr_ptr pointer to source address information struct
GeofferyOmlette 0:f6e4e1bbb3fe 2130 * \param *received_coap_msg_ptr pointer to parsed CoAP message structure
GeofferyOmlette 0:f6e4e1bbb3fe 2131 *****************************************************************************/
GeofferyOmlette 0:f6e4e1bbb3fe 2132 SN_MEM_ATTR_COAP_PROTOCOL_FUNC
GeofferyOmlette 0:f6e4e1bbb3fe 2133 static sn_coap_hdr_s *sn_coap_handle_blockwise_message(sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *received_coap_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2134 {
GeofferyOmlette 0:f6e4e1bbb3fe 2135 sn_coap_hdr_s *src_coap_blockwise_ack_msg_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2136 uint16_t dst_packed_data_needed_mem = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2137 uint8_t *dst_ack_packet_data_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2138 uint8_t block_temp = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2139
GeofferyOmlette 0:f6e4e1bbb3fe 2140 uint16_t original_payload_len = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2141 uint8_t *original_payload_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2142
GeofferyOmlette 0:f6e4e1bbb3fe 2143 /* Block1 Option in a request (e.g., PUT or POST) */
GeofferyOmlette 0:f6e4e1bbb3fe 2144 // Blocked request sending, received ACK, sending next block..
GeofferyOmlette 0:f6e4e1bbb3fe 2145 if(received_coap_msg_ptr->options_list_ptr->block1_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2146 {
GeofferyOmlette 0:f6e4e1bbb3fe 2147 if(received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE)
GeofferyOmlette 0:f6e4e1bbb3fe 2148 {
GeofferyOmlette 0:f6e4e1bbb3fe 2149 if(*(received_coap_msg_ptr->options_list_ptr->block1_ptr + (received_coap_msg_ptr->options_list_ptr->block1_len - 1)) & 0x08)
GeofferyOmlette 0:f6e4e1bbb3fe 2150 {
GeofferyOmlette 0:f6e4e1bbb3fe 2151 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = sn_linked_list_get_last_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2152
GeofferyOmlette 0:f6e4e1bbb3fe 2153 /* Get */
GeofferyOmlette 0:f6e4e1bbb3fe 2154 while(stored_blockwise_msg_temp_ptr && (received_coap_msg_ptr->msg_id != stored_blockwise_msg_temp_ptr->coap_msg_ptr->msg_id))
GeofferyOmlette 0:f6e4e1bbb3fe 2155 {
GeofferyOmlette 0:f6e4e1bbb3fe 2156 stored_blockwise_msg_temp_ptr = sn_linked_list_get_previous_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2157 }
GeofferyOmlette 0:f6e4e1bbb3fe 2158
GeofferyOmlette 0:f6e4e1bbb3fe 2159 if(stored_blockwise_msg_temp_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2160 {
GeofferyOmlette 0:f6e4e1bbb3fe 2161 /* Build response message */
GeofferyOmlette 0:f6e4e1bbb3fe 2162
GeofferyOmlette 0:f6e4e1bbb3fe 2163 uint16_t block_size = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 2164 uint32_t block_number = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2165
GeofferyOmlette 0:f6e4e1bbb3fe 2166 /* Get block option parameters from received message */
GeofferyOmlette 0:f6e4e1bbb3fe 2167 if(received_coap_msg_ptr->options_list_ptr->block1_len == 3)
GeofferyOmlette 0:f6e4e1bbb3fe 2168 {
GeofferyOmlette 0:f6e4e1bbb3fe 2169 block_number = *(received_coap_msg_ptr->options_list_ptr->block1_ptr) << 12;
GeofferyOmlette 0:f6e4e1bbb3fe 2170 block_number |= *(received_coap_msg_ptr->options_list_ptr->block1_ptr + 1) << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2171 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block1_ptr + 2)) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2172 }
GeofferyOmlette 0:f6e4e1bbb3fe 2173
GeofferyOmlette 0:f6e4e1bbb3fe 2174 else if(received_coap_msg_ptr->options_list_ptr->block1_len == 2)
GeofferyOmlette 0:f6e4e1bbb3fe 2175 {
GeofferyOmlette 0:f6e4e1bbb3fe 2176 block_number = *(received_coap_msg_ptr->options_list_ptr->block1_ptr) << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2177 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block1_ptr + 1)) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2178 }
GeofferyOmlette 0:f6e4e1bbb3fe 2179 else if(received_coap_msg_ptr->options_list_ptr->block1_len == 1)
GeofferyOmlette 0:f6e4e1bbb3fe 2180 {
GeofferyOmlette 0:f6e4e1bbb3fe 2181 block_number = (*received_coap_msg_ptr->options_list_ptr->block1_ptr) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2182 }
GeofferyOmlette 0:f6e4e1bbb3fe 2183
GeofferyOmlette 0:f6e4e1bbb3fe 2184
GeofferyOmlette 0:f6e4e1bbb3fe 2185 block_temp = *(received_coap_msg_ptr->options_list_ptr->block1_ptr + (received_coap_msg_ptr->options_list_ptr->block1_len - 1)) & 0x07;
GeofferyOmlette 0:f6e4e1bbb3fe 2186 block_size = block_size << (block_temp + 4);
GeofferyOmlette 0:f6e4e1bbb3fe 2187
GeofferyOmlette 0:f6e4e1bbb3fe 2188
GeofferyOmlette 0:f6e4e1bbb3fe 2189 /* Build next block message */
GeofferyOmlette 0:f6e4e1bbb3fe 2190 src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2191
GeofferyOmlette 0:f6e4e1bbb3fe 2192 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2193 {
GeofferyOmlette 0:f6e4e1bbb3fe 2194 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2195 {
GeofferyOmlette 0:f6e4e1bbb3fe 2196 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2197 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2198
GeofferyOmlette 0:f6e4e1bbb3fe 2199 }
GeofferyOmlette 0:f6e4e1bbb3fe 2200 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2201 {
GeofferyOmlette 0:f6e4e1bbb3fe 2202 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2203 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2204 }
GeofferyOmlette 0:f6e4e1bbb3fe 2205 }
GeofferyOmlette 0:f6e4e1bbb3fe 2206 else
GeofferyOmlette 0:f6e4e1bbb3fe 2207 {
GeofferyOmlette 0:f6e4e1bbb3fe 2208 src_coap_blockwise_ack_msg_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2209 if(!src_coap_blockwise_ack_msg_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2210 {
GeofferyOmlette 0:f6e4e1bbb3fe 2211 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2212 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2213 }
GeofferyOmlette 0:f6e4e1bbb3fe 2214 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, (sizeof(sn_coap_options_list_s)));
GeofferyOmlette 0:f6e4e1bbb3fe 2215 }
GeofferyOmlette 0:f6e4e1bbb3fe 2216
GeofferyOmlette 0:f6e4e1bbb3fe 2217 block_number++;
GeofferyOmlette 0:f6e4e1bbb3fe 2218
GeofferyOmlette 0:f6e4e1bbb3fe 2219 if(block_number <= 0x0f)
GeofferyOmlette 0:f6e4e1bbb3fe 2220 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 2221 else if(block_number <= 0x0fff)
GeofferyOmlette 0:f6e4e1bbb3fe 2222 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = 2;
GeofferyOmlette 0:f6e4e1bbb3fe 2223 else if(block_number <= 0x0fffff)
GeofferyOmlette 0:f6e4e1bbb3fe 2224 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = 3;
GeofferyOmlette 0:f6e4e1bbb3fe 2225
GeofferyOmlette 0:f6e4e1bbb3fe 2226 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = sn_coap_protocol_malloc(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2227
GeofferyOmlette 0:f6e4e1bbb3fe 2228 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 2229 {
GeofferyOmlette 0:f6e4e1bbb3fe 2230 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2231 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2232 }
GeofferyOmlette 0:f6e4e1bbb3fe 2233
GeofferyOmlette 0:f6e4e1bbb3fe 2234 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len - 1)) = block_temp;
GeofferyOmlette 0:f6e4e1bbb3fe 2235 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2236
GeofferyOmlette 0:f6e4e1bbb3fe 2237
GeofferyOmlette 0:f6e4e1bbb3fe 2238 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len == 3)
GeofferyOmlette 0:f6e4e1bbb3fe 2239 {
GeofferyOmlette 0:f6e4e1bbb3fe 2240 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + 2) = block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2241 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + 1) |= block_number >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2242 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number >> 12;
GeofferyOmlette 0:f6e4e1bbb3fe 2243 }
GeofferyOmlette 0:f6e4e1bbb3fe 2244 else if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len == 2)
GeofferyOmlette 0:f6e4e1bbb3fe 2245 {
GeofferyOmlette 0:f6e4e1bbb3fe 2246 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + 1) |= block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2247 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2248 }
GeofferyOmlette 0:f6e4e1bbb3fe 2249 else
GeofferyOmlette 0:f6e4e1bbb3fe 2250 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr |= block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2251
GeofferyOmlette 0:f6e4e1bbb3fe 2252 original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2253 original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2254
GeofferyOmlette 0:f6e4e1bbb3fe 2255 if((block_size * (block_number+1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len)
GeofferyOmlette 0:f6e4e1bbb3fe 2256 {
GeofferyOmlette 0:f6e4e1bbb3fe 2257 src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * (block_number));
GeofferyOmlette 0:f6e4e1bbb3fe 2258 src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
GeofferyOmlette 0:f6e4e1bbb3fe 2259 }
GeofferyOmlette 0:f6e4e1bbb3fe 2260
GeofferyOmlette 0:f6e4e1bbb3fe 2261 /* Not last block */
GeofferyOmlette 0:f6e4e1bbb3fe 2262 else
GeofferyOmlette 0:f6e4e1bbb3fe 2263 {
GeofferyOmlette 0:f6e4e1bbb3fe 2264 /* set more - bit */
GeofferyOmlette 0:f6e4e1bbb3fe 2265 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len-1)) |= 0x08;
GeofferyOmlette 0:f6e4e1bbb3fe 2266 src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
GeofferyOmlette 0:f6e4e1bbb3fe 2267 src_coap_blockwise_ack_msg_ptr->payload_ptr = src_coap_blockwise_ack_msg_ptr->payload_ptr + (block_size * block_number);
GeofferyOmlette 0:f6e4e1bbb3fe 2268 }
GeofferyOmlette 0:f6e4e1bbb3fe 2269
GeofferyOmlette 0:f6e4e1bbb3fe 2270 /* Build and send block message */
GeofferyOmlette 0:f6e4e1bbb3fe 2271 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2272
GeofferyOmlette 0:f6e4e1bbb3fe 2273 dst_ack_packet_data_ptr = sn_coap_protocol_malloc(dst_packed_data_needed_mem);
GeofferyOmlette 0:f6e4e1bbb3fe 2274 if(!dst_ack_packet_data_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2275 {
GeofferyOmlette 0:f6e4e1bbb3fe 2276 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2277 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2278 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2279 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2280 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2281 }
GeofferyOmlette 0:f6e4e1bbb3fe 2282 src_coap_blockwise_ack_msg_ptr->msg_id = global_message_id++;
GeofferyOmlette 0:f6e4e1bbb3fe 2283
GeofferyOmlette 0:f6e4e1bbb3fe 2284 sn_coap_builder(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2285
GeofferyOmlette 0:f6e4e1bbb3fe 2286 sn_coap_tx_callback(SN_NSDL_PROTOCOL_COAP, dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2287
GeofferyOmlette 0:f6e4e1bbb3fe 2288 sn_coap_protocol_free(dst_ack_packet_data_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2289
GeofferyOmlette 0:f6e4e1bbb3fe 2290 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2291 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2292
GeofferyOmlette 0:f6e4e1bbb3fe 2293 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
GeofferyOmlette 0:f6e4e1bbb3fe 2294 }
GeofferyOmlette 0:f6e4e1bbb3fe 2295 }
GeofferyOmlette 0:f6e4e1bbb3fe 2296 else
GeofferyOmlette 0:f6e4e1bbb3fe 2297 {
GeofferyOmlette 0:f6e4e1bbb3fe 2298 sn_coap_protocol_linked_list_blockwise_msg_remove_current();
GeofferyOmlette 0:f6e4e1bbb3fe 2299 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
GeofferyOmlette 0:f6e4e1bbb3fe 2300 }
GeofferyOmlette 0:f6e4e1bbb3fe 2301 }
GeofferyOmlette 0:f6e4e1bbb3fe 2302
GeofferyOmlette 0:f6e4e1bbb3fe 2303 // Blocked request receiving
GeofferyOmlette 0:f6e4e1bbb3fe 2304 else
GeofferyOmlette 0:f6e4e1bbb3fe 2305 {
GeofferyOmlette 0:f6e4e1bbb3fe 2306 if(received_coap_msg_ptr->payload_len > sn_coap_block_data_size)
GeofferyOmlette 0:f6e4e1bbb3fe 2307 received_coap_msg_ptr->payload_len = sn_coap_block_data_size;
GeofferyOmlette 0:f6e4e1bbb3fe 2308
GeofferyOmlette 0:f6e4e1bbb3fe 2309 sn_coap_protocol_linked_list_blockwise_payload_store(src_addr_ptr, received_coap_msg_ptr->payload_len, received_coap_msg_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2310 /* If not last block (more value is set) */
GeofferyOmlette 0:f6e4e1bbb3fe 2311 /* Block option length can be 1-3 bytes. First 4-20 bits are for block number. Last 4 bits are ALWAYS more bit + block size. */
GeofferyOmlette 0:f6e4e1bbb3fe 2312 if(*(received_coap_msg_ptr->options_list_ptr->block1_ptr + (received_coap_msg_ptr->options_list_ptr->block1_len - 1)) & 0x08)
GeofferyOmlette 0:f6e4e1bbb3fe 2313 {
GeofferyOmlette 0:f6e4e1bbb3fe 2314 //send ack
GeofferyOmlette 0:f6e4e1bbb3fe 2315 src_coap_blockwise_ack_msg_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2316
GeofferyOmlette 0:f6e4e1bbb3fe 2317 if (src_coap_blockwise_ack_msg_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2318 {
GeofferyOmlette 0:f6e4e1bbb3fe 2319 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2320 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2321 }
GeofferyOmlette 0:f6e4e1bbb3fe 2322
GeofferyOmlette 0:f6e4e1bbb3fe 2323 memset(src_coap_blockwise_ack_msg_ptr, 0, sizeof(sn_coap_hdr_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2324
GeofferyOmlette 0:f6e4e1bbb3fe 2325 src_coap_blockwise_ack_msg_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2326
GeofferyOmlette 0:f6e4e1bbb3fe 2327 if (src_coap_blockwise_ack_msg_ptr->options_list_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2328 {
GeofferyOmlette 0:f6e4e1bbb3fe 2329 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2330 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2331 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2332 }
GeofferyOmlette 0:f6e4e1bbb3fe 2333
GeofferyOmlette 0:f6e4e1bbb3fe 2334 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2335
GeofferyOmlette 0:f6e4e1bbb3fe 2336 if(received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET)
GeofferyOmlette 0:f6e4e1bbb3fe 2337 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
GeofferyOmlette 0:f6e4e1bbb3fe 2338 else if(received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST)
GeofferyOmlette 0:f6e4e1bbb3fe 2339 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CREATED;
GeofferyOmlette 0:f6e4e1bbb3fe 2340 else if(received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
GeofferyOmlette 0:f6e4e1bbb3fe 2341 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
GeofferyOmlette 0:f6e4e1bbb3fe 2342 else if(received_coap_msg_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE)
GeofferyOmlette 0:f6e4e1bbb3fe 2343 src_coap_blockwise_ack_msg_ptr->msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
GeofferyOmlette 0:f6e4e1bbb3fe 2344
GeofferyOmlette 0:f6e4e1bbb3fe 2345 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len = received_coap_msg_ptr->options_list_ptr->block1_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2346 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = sn_coap_protocol_malloc(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2347 if(!src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2348 {
GeofferyOmlette 0:f6e4e1bbb3fe 2349 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2350 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2351 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2352 }
GeofferyOmlette 0:f6e4e1bbb3fe 2353
GeofferyOmlette 0:f6e4e1bbb3fe 2354 src_coap_blockwise_ack_msg_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
GeofferyOmlette 0:f6e4e1bbb3fe 2355
GeofferyOmlette 0:f6e4e1bbb3fe 2356 memcpy(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr, received_coap_msg_ptr->options_list_ptr->block1_ptr, received_coap_msg_ptr->options_list_ptr->block1_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2357
GeofferyOmlette 0:f6e4e1bbb3fe 2358 /* Check block size */
GeofferyOmlette 0:f6e4e1bbb3fe 2359 block_temp = (*(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len - 1)) & 0x07);
GeofferyOmlette 0:f6e4e1bbb3fe 2360 if(block_temp > sn_coap_convert_block_size(sn_coap_block_data_size))
GeofferyOmlette 0:f6e4e1bbb3fe 2361 {
GeofferyOmlette 0:f6e4e1bbb3fe 2362 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len - 1)) &= 0xF8;
GeofferyOmlette 0:f6e4e1bbb3fe 2363 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_len - 1)) |= sn_coap_convert_block_size(sn_coap_block_data_size);
GeofferyOmlette 0:f6e4e1bbb3fe 2364 }
GeofferyOmlette 0:f6e4e1bbb3fe 2365
GeofferyOmlette 0:f6e4e1bbb3fe 2366 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 2367
GeofferyOmlette 0:f6e4e1bbb3fe 2368 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2369
GeofferyOmlette 0:f6e4e1bbb3fe 2370 dst_ack_packet_data_ptr = sn_coap_protocol_malloc(dst_packed_data_needed_mem);
GeofferyOmlette 0:f6e4e1bbb3fe 2371 if(!dst_ack_packet_data_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2372 {
GeofferyOmlette 0:f6e4e1bbb3fe 2373 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2374 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2375 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2376 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2377 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2378 }
GeofferyOmlette 0:f6e4e1bbb3fe 2379
GeofferyOmlette 0:f6e4e1bbb3fe 2380 sn_coap_builder(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2381
GeofferyOmlette 0:f6e4e1bbb3fe 2382 sn_coap_tx_callback(SN_NSDL_PROTOCOL_COAP, dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2383
GeofferyOmlette 0:f6e4e1bbb3fe 2384 sn_coap_parser_release_allocated_coap_msg_mem(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2385 sn_coap_protocol_free(dst_ack_packet_data_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2386
GeofferyOmlette 0:f6e4e1bbb3fe 2387 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
GeofferyOmlette 0:f6e4e1bbb3fe 2388
GeofferyOmlette 0:f6e4e1bbb3fe 2389 }
GeofferyOmlette 0:f6e4e1bbb3fe 2390 else
GeofferyOmlette 0:f6e4e1bbb3fe 2391 {
GeofferyOmlette 0:f6e4e1bbb3fe 2392 /* * * This is the last block when whole Blockwise payload from received * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2393 /* * * blockwise messages is gathered and returned to User * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2394
GeofferyOmlette 0:f6e4e1bbb3fe 2395 /* Store last Blockwise payload to Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 2396 uint16_t payload_len = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2397 uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(src_addr_ptr, &payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2398 uint16_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2399 uint8_t *temp_whole_payload_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2400
GeofferyOmlette 0:f6e4e1bbb3fe 2401 temp_whole_payload_ptr = sn_coap_protocol_malloc(whole_payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2402 if(!temp_whole_payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2403 {
GeofferyOmlette 0:f6e4e1bbb3fe 2404 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2405 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2406 }
GeofferyOmlette 0:f6e4e1bbb3fe 2407
GeofferyOmlette 0:f6e4e1bbb3fe 2408 received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2409 received_coap_msg_ptr->payload_len = whole_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2410
GeofferyOmlette 0:f6e4e1bbb3fe 2411 /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
GeofferyOmlette 0:f6e4e1bbb3fe 2412 while (payload_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2413 {
GeofferyOmlette 0:f6e4e1bbb3fe 2414 memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2415
GeofferyOmlette 0:f6e4e1bbb3fe 2416 temp_whole_payload_ptr += payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2417
GeofferyOmlette 0:f6e4e1bbb3fe 2418 sn_coap_protocol_linked_list_blockwise_payload_remove_oldest();
GeofferyOmlette 0:f6e4e1bbb3fe 2419 payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(src_addr_ptr, &payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2420 }
GeofferyOmlette 0:f6e4e1bbb3fe 2421 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
GeofferyOmlette 0:f6e4e1bbb3fe 2422 }
GeofferyOmlette 0:f6e4e1bbb3fe 2423 }
GeofferyOmlette 0:f6e4e1bbb3fe 2424 }
GeofferyOmlette 0:f6e4e1bbb3fe 2425
GeofferyOmlette 0:f6e4e1bbb3fe 2426
GeofferyOmlette 0:f6e4e1bbb3fe 2427 /* Block2 Option in a response (e.g., a 2.05 response for GET) */
GeofferyOmlette 0:f6e4e1bbb3fe 2428 /* Message ID must be same than in received message */
GeofferyOmlette 0:f6e4e1bbb3fe 2429 else
GeofferyOmlette 0:f6e4e1bbb3fe 2430 {
GeofferyOmlette 0:f6e4e1bbb3fe 2431 //This is response to request we made
GeofferyOmlette 0:f6e4e1bbb3fe 2432 if(received_coap_msg_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE)
GeofferyOmlette 0:f6e4e1bbb3fe 2433 {
GeofferyOmlette 0:f6e4e1bbb3fe 2434 uint32_t block_number = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2435 coap_blockwise_msg_s *previous_blockwise_msg_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2436
GeofferyOmlette 0:f6e4e1bbb3fe 2437 /* Store blockwise payload to Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 2438 //todo: add block number to stored values - just to make sure all packets are in order
GeofferyOmlette 0:f6e4e1bbb3fe 2439 sn_coap_protocol_linked_list_blockwise_payload_store(src_addr_ptr, received_coap_msg_ptr->payload_len, received_coap_msg_ptr->payload_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2440
GeofferyOmlette 0:f6e4e1bbb3fe 2441 /* If not last block (more value is set) */
GeofferyOmlette 0:f6e4e1bbb3fe 2442 if(*(received_coap_msg_ptr->options_list_ptr->block2_ptr + (received_coap_msg_ptr->options_list_ptr->block2_len - 1)) & 0x08)
GeofferyOmlette 0:f6e4e1bbb3fe 2443 {
GeofferyOmlette 0:f6e4e1bbb3fe 2444 //build and send ack
GeofferyOmlette 0:f6e4e1bbb3fe 2445 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING;
GeofferyOmlette 0:f6e4e1bbb3fe 2446
GeofferyOmlette 0:f6e4e1bbb3fe 2447 previous_blockwise_msg_ptr = sn_linked_list_get_first_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2448
GeofferyOmlette 0:f6e4e1bbb3fe 2449 while(previous_blockwise_msg_ptr && (received_coap_msg_ptr->msg_id != previous_blockwise_msg_ptr->coap_msg_ptr->msg_id))
GeofferyOmlette 0:f6e4e1bbb3fe 2450 {
GeofferyOmlette 0:f6e4e1bbb3fe 2451 previous_blockwise_msg_ptr = sn_linked_list_get_next_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2452 }
GeofferyOmlette 0:f6e4e1bbb3fe 2453
GeofferyOmlette 0:f6e4e1bbb3fe 2454 if(!previous_blockwise_msg_ptr || !previous_blockwise_msg_ptr->coap_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2455 {
GeofferyOmlette 0:f6e4e1bbb3fe 2456 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2457 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2458 }
GeofferyOmlette 0:f6e4e1bbb3fe 2459
GeofferyOmlette 0:f6e4e1bbb3fe 2460 src_coap_blockwise_ack_msg_ptr = previous_blockwise_msg_ptr->coap_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2461
GeofferyOmlette 0:f6e4e1bbb3fe 2462 sn_linked_list_remove_current_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2463 sn_coap_protocol_free(previous_blockwise_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2464
GeofferyOmlette 0:f6e4e1bbb3fe 2465 if(src_coap_blockwise_ack_msg_ptr->payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2466 {
GeofferyOmlette 0:f6e4e1bbb3fe 2467 src_coap_blockwise_ack_msg_ptr->payload_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2468 src_coap_blockwise_ack_msg_ptr->payload_len = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2469 }
GeofferyOmlette 0:f6e4e1bbb3fe 2470
GeofferyOmlette 0:f6e4e1bbb3fe 2471 /* * * Then build CoAP Acknowledgement message * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2472 if(!src_coap_blockwise_ack_msg_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2473 {
GeofferyOmlette 0:f6e4e1bbb3fe 2474 src_coap_blockwise_ack_msg_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2475 if(!src_coap_blockwise_ack_msg_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2476 {
GeofferyOmlette 0:f6e4e1bbb3fe 2477 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2478 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2479 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2480 }
GeofferyOmlette 0:f6e4e1bbb3fe 2481 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2482 }
GeofferyOmlette 0:f6e4e1bbb3fe 2483
GeofferyOmlette 0:f6e4e1bbb3fe 2484 src_coap_blockwise_ack_msg_ptr->msg_id = global_message_id++;
GeofferyOmlette 0:f6e4e1bbb3fe 2485
GeofferyOmlette 0:f6e4e1bbb3fe 2486 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2487 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2488
GeofferyOmlette 0:f6e4e1bbb3fe 2489 /* Update block option */
GeofferyOmlette 0:f6e4e1bbb3fe 2490 block_temp = *(received_coap_msg_ptr->options_list_ptr->block2_ptr + ( received_coap_msg_ptr->options_list_ptr->block2_len - 1)) & 0x07;
GeofferyOmlette 0:f6e4e1bbb3fe 2491
GeofferyOmlette 0:f6e4e1bbb3fe 2492 if(received_coap_msg_ptr->options_list_ptr->block2_len == 3)
GeofferyOmlette 0:f6e4e1bbb3fe 2493 {
GeofferyOmlette 0:f6e4e1bbb3fe 2494 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 12;
GeofferyOmlette 0:f6e4e1bbb3fe 2495 block_number |= *(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1) << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2496 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 2)) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2497 }
GeofferyOmlette 0:f6e4e1bbb3fe 2498
GeofferyOmlette 0:f6e4e1bbb3fe 2499 else if(received_coap_msg_ptr->options_list_ptr->block2_len == 2)
GeofferyOmlette 0:f6e4e1bbb3fe 2500 {
GeofferyOmlette 0:f6e4e1bbb3fe 2501 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2502 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1)) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2503 }
GeofferyOmlette 0:f6e4e1bbb3fe 2504 else if(received_coap_msg_ptr->options_list_ptr->block2_len == 1)
GeofferyOmlette 0:f6e4e1bbb3fe 2505 {
GeofferyOmlette 0:f6e4e1bbb3fe 2506 block_number = (*received_coap_msg_ptr->options_list_ptr->block2_ptr) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2507 }
GeofferyOmlette 0:f6e4e1bbb3fe 2508
GeofferyOmlette 0:f6e4e1bbb3fe 2509 block_number ++;
GeofferyOmlette 0:f6e4e1bbb3fe 2510
GeofferyOmlette 0:f6e4e1bbb3fe 2511 if(block_number <= 0x0f)
GeofferyOmlette 0:f6e4e1bbb3fe 2512 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 2513 else if(block_number <= 0x0fff)
GeofferyOmlette 0:f6e4e1bbb3fe 2514 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = 2;
GeofferyOmlette 0:f6e4e1bbb3fe 2515 else if(block_number <= 0x0fffff)
GeofferyOmlette 0:f6e4e1bbb3fe 2516 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = 3;
GeofferyOmlette 0:f6e4e1bbb3fe 2517
GeofferyOmlette 0:f6e4e1bbb3fe 2518 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = sn_coap_protocol_malloc(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2519
GeofferyOmlette 0:f6e4e1bbb3fe 2520 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr == 0)
GeofferyOmlette 0:f6e4e1bbb3fe 2521 {
GeofferyOmlette 0:f6e4e1bbb3fe 2522 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2523 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2524 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2525 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2526 }
GeofferyOmlette 0:f6e4e1bbb3fe 2527
GeofferyOmlette 0:f6e4e1bbb3fe 2528 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len - 1)) = block_temp;
GeofferyOmlette 0:f6e4e1bbb3fe 2529
GeofferyOmlette 0:f6e4e1bbb3fe 2530 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len == 3)
GeofferyOmlette 0:f6e4e1bbb3fe 2531 {
GeofferyOmlette 0:f6e4e1bbb3fe 2532 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + 2) = block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2533 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + 1) |= block_number >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2534 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr |= block_number >> 12;
GeofferyOmlette 0:f6e4e1bbb3fe 2535 }
GeofferyOmlette 0:f6e4e1bbb3fe 2536 else if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len == 2)
GeofferyOmlette 0:f6e4e1bbb3fe 2537 {
GeofferyOmlette 0:f6e4e1bbb3fe 2538 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + 1) = block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2539 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr |= block_number >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2540 }
GeofferyOmlette 0:f6e4e1bbb3fe 2541 else
GeofferyOmlette 0:f6e4e1bbb3fe 2542 *src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr |= block_number << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2543
GeofferyOmlette 0:f6e4e1bbb3fe 2544 /* Then get needed memory count for Packet data */
GeofferyOmlette 0:f6e4e1bbb3fe 2545 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2546
GeofferyOmlette 0:f6e4e1bbb3fe 2547 /* Then allocate memory for Packet data */
GeofferyOmlette 0:f6e4e1bbb3fe 2548 dst_ack_packet_data_ptr = sn_coap_protocol_malloc(dst_packed_data_needed_mem);
GeofferyOmlette 0:f6e4e1bbb3fe 2549 memset(dst_ack_packet_data_ptr, 0, dst_packed_data_needed_mem);
GeofferyOmlette 0:f6e4e1bbb3fe 2550
GeofferyOmlette 0:f6e4e1bbb3fe 2551 if (dst_ack_packet_data_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2552 {
GeofferyOmlette 0:f6e4e1bbb3fe 2553 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2554 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2555 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2556 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2557 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2558 }
GeofferyOmlette 0:f6e4e1bbb3fe 2559
GeofferyOmlette 0:f6e4e1bbb3fe 2560 /* * * Then build Acknowledgement message to Packed data * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2561 if ((sn_coap_builder(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr)) < 0)
GeofferyOmlette 0:f6e4e1bbb3fe 2562 {
GeofferyOmlette 0:f6e4e1bbb3fe 2563 sn_coap_protocol_free(dst_ack_packet_data_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2564 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2565 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2566 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2567 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2568 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2569 }
GeofferyOmlette 0:f6e4e1bbb3fe 2570
GeofferyOmlette 0:f6e4e1bbb3fe 2571 /* * * Save to linked list * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2572 coap_blockwise_msg_s *stored_blockwise_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2573
GeofferyOmlette 0:f6e4e1bbb3fe 2574 stored_blockwise_msg_ptr = sn_coap_protocol_malloc(sizeof(coap_blockwise_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2575 if(!stored_blockwise_msg_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2576 {
GeofferyOmlette 0:f6e4e1bbb3fe 2577 sn_coap_parser_release_allocated_coap_msg_mem(received_coap_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2578 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2579 }
GeofferyOmlette 0:f6e4e1bbb3fe 2580 memset(stored_blockwise_msg_ptr, 0, sizeof(coap_blockwise_msg_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2581
GeofferyOmlette 0:f6e4e1bbb3fe 2582 stored_blockwise_msg_ptr->timestamp = global_system_time;
GeofferyOmlette 0:f6e4e1bbb3fe 2583
GeofferyOmlette 0:f6e4e1bbb3fe 2584 stored_blockwise_msg_ptr->coap_msg_ptr = src_coap_blockwise_ack_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2585
GeofferyOmlette 0:f6e4e1bbb3fe 2586 sn_linked_list_add_node(global_linked_list_blockwise_sent_msgs_ptr, stored_blockwise_msg_ptr); // todo check return value
GeofferyOmlette 0:f6e4e1bbb3fe 2587
GeofferyOmlette 0:f6e4e1bbb3fe 2588
GeofferyOmlette 0:f6e4e1bbb3fe 2589 /* * * Then release memory of CoAP Acknowledgement message * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2590 sn_coap_tx_callback(SN_NSDL_PROTOCOL_COAP, dst_ack_packet_data_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 2591 dst_packed_data_needed_mem, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2592
GeofferyOmlette 0:f6e4e1bbb3fe 2593 #if ENABLE_RESENDINGS
GeofferyOmlette 0:f6e4e1bbb3fe 2594 sn_coap_protocol_linked_list_send_msg_store(src_addr_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 2595 dst_packed_data_needed_mem,
GeofferyOmlette 0:f6e4e1bbb3fe 2596 dst_ack_packet_data_ptr,
GeofferyOmlette 0:f6e4e1bbb3fe 2597 global_system_time + (uint32_t)(sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR));
GeofferyOmlette 0:f6e4e1bbb3fe 2598 #endif
GeofferyOmlette 0:f6e4e1bbb3fe 2599 sn_coap_protocol_free(dst_ack_packet_data_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2600 }
GeofferyOmlette 0:f6e4e1bbb3fe 2601
GeofferyOmlette 0:f6e4e1bbb3fe 2602 //Last block received
GeofferyOmlette 0:f6e4e1bbb3fe 2603 else
GeofferyOmlette 0:f6e4e1bbb3fe 2604 {
GeofferyOmlette 0:f6e4e1bbb3fe 2605 /* * * This is the last block when whole Blockwise payload from received * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2606 /* * * blockwise messages is gathered and returned to User * * */
GeofferyOmlette 0:f6e4e1bbb3fe 2607
GeofferyOmlette 0:f6e4e1bbb3fe 2608 /* Store last Blockwise payload to Linked list */
GeofferyOmlette 0:f6e4e1bbb3fe 2609 uint16_t payload_len = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2610 uint8_t *payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(src_addr_ptr, &payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2611 uint16_t whole_payload_len = sn_coap_protocol_linked_list_blockwise_payloads_get_len(src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2612 uint8_t *temp_whole_payload_ptr = NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2613
GeofferyOmlette 0:f6e4e1bbb3fe 2614 temp_whole_payload_ptr = sn_coap_protocol_malloc(whole_payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2615 if(!temp_whole_payload_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2616 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2617
GeofferyOmlette 0:f6e4e1bbb3fe 2618 received_coap_msg_ptr->payload_ptr = temp_whole_payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2619 received_coap_msg_ptr->payload_len = whole_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2620
GeofferyOmlette 0:f6e4e1bbb3fe 2621 /* Copy stored Blockwise payloads to returned whole Blockwise payload pointer */
GeofferyOmlette 0:f6e4e1bbb3fe 2622 while (payload_ptr != NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2623 {
GeofferyOmlette 0:f6e4e1bbb3fe 2624 memcpy(temp_whole_payload_ptr, payload_ptr, payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2625
GeofferyOmlette 0:f6e4e1bbb3fe 2626 temp_whole_payload_ptr += payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2627
GeofferyOmlette 0:f6e4e1bbb3fe 2628 sn_coap_protocol_linked_list_blockwise_payload_remove_oldest();
GeofferyOmlette 0:f6e4e1bbb3fe 2629 payload_ptr = sn_coap_protocol_linked_list_blockwise_payload_search(src_addr_ptr, &payload_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2630 }
GeofferyOmlette 0:f6e4e1bbb3fe 2631 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED;
GeofferyOmlette 0:f6e4e1bbb3fe 2632
GeofferyOmlette 0:f6e4e1bbb3fe 2633 //todo: remove previous msg from list
GeofferyOmlette 0:f6e4e1bbb3fe 2634 }
GeofferyOmlette 0:f6e4e1bbb3fe 2635
GeofferyOmlette 0:f6e4e1bbb3fe 2636 }
GeofferyOmlette 0:f6e4e1bbb3fe 2637
GeofferyOmlette 0:f6e4e1bbb3fe 2638 //Now we send data to request
GeofferyOmlette 0:f6e4e1bbb3fe 2639 else
GeofferyOmlette 0:f6e4e1bbb3fe 2640 {
GeofferyOmlette 0:f6e4e1bbb3fe 2641 //Get message by using block number
GeofferyOmlette 0:f6e4e1bbb3fe 2642 coap_blockwise_msg_s *stored_blockwise_msg_temp_ptr = sn_linked_list_get_last_node(global_linked_list_blockwise_sent_msgs_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2643 if(stored_blockwise_msg_temp_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2644 {
GeofferyOmlette 0:f6e4e1bbb3fe 2645 uint16_t block_size = 1;
GeofferyOmlette 0:f6e4e1bbb3fe 2646 uint32_t block_number = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2647
GeofferyOmlette 0:f6e4e1bbb3fe 2648 /* Resolve block parameters */
GeofferyOmlette 0:f6e4e1bbb3fe 2649 if(received_coap_msg_ptr->options_list_ptr->block2_len == 3)
GeofferyOmlette 0:f6e4e1bbb3fe 2650 {
GeofferyOmlette 0:f6e4e1bbb3fe 2651 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 12;
GeofferyOmlette 0:f6e4e1bbb3fe 2652 block_number |= *(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1) << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2653 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 2)) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2654 }
GeofferyOmlette 0:f6e4e1bbb3fe 2655
GeofferyOmlette 0:f6e4e1bbb3fe 2656 else if(received_coap_msg_ptr->options_list_ptr->block2_len == 2)
GeofferyOmlette 0:f6e4e1bbb3fe 2657 {
GeofferyOmlette 0:f6e4e1bbb3fe 2658 block_number = *(received_coap_msg_ptr->options_list_ptr->block2_ptr) << 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2659 block_number |= (*(received_coap_msg_ptr->options_list_ptr->block2_ptr + 1)) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2660 }
GeofferyOmlette 0:f6e4e1bbb3fe 2661 else if(received_coap_msg_ptr->options_list_ptr->block2_len == 1)
GeofferyOmlette 0:f6e4e1bbb3fe 2662 {
GeofferyOmlette 0:f6e4e1bbb3fe 2663 block_number = (*received_coap_msg_ptr->options_list_ptr->block2_ptr) >> 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2664 }
GeofferyOmlette 0:f6e4e1bbb3fe 2665
GeofferyOmlette 0:f6e4e1bbb3fe 2666 block_temp = *(received_coap_msg_ptr->options_list_ptr->block2_ptr + (received_coap_msg_ptr->options_list_ptr->block2_len - 1)) & 0x07;
GeofferyOmlette 0:f6e4e1bbb3fe 2667 block_size = block_size << (block_temp + 4);
GeofferyOmlette 0:f6e4e1bbb3fe 2668
GeofferyOmlette 0:f6e4e1bbb3fe 2669 /* Build response message */
GeofferyOmlette 0:f6e4e1bbb3fe 2670 src_coap_blockwise_ack_msg_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2671
GeofferyOmlette 0:f6e4e1bbb3fe 2672 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2673 {
GeofferyOmlette 0:f6e4e1bbb3fe 2674 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2675 {
GeofferyOmlette 0:f6e4e1bbb3fe 2676 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2677 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block1_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2678
GeofferyOmlette 0:f6e4e1bbb3fe 2679 }
GeofferyOmlette 0:f6e4e1bbb3fe 2680 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2681 {
GeofferyOmlette 0:f6e4e1bbb3fe 2682 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2683 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2684 }
GeofferyOmlette 0:f6e4e1bbb3fe 2685 }
GeofferyOmlette 0:f6e4e1bbb3fe 2686 else
GeofferyOmlette 0:f6e4e1bbb3fe 2687 {
GeofferyOmlette 0:f6e4e1bbb3fe 2688 src_coap_blockwise_ack_msg_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2689 if(!src_coap_blockwise_ack_msg_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2690 {
GeofferyOmlette 0:f6e4e1bbb3fe 2691 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2692 }
GeofferyOmlette 0:f6e4e1bbb3fe 2693 memset(src_coap_blockwise_ack_msg_ptr->options_list_ptr, 0, (sizeof(sn_coap_options_list_s)));
GeofferyOmlette 0:f6e4e1bbb3fe 2694 }
GeofferyOmlette 0:f6e4e1bbb3fe 2695
GeofferyOmlette 0:f6e4e1bbb3fe 2696
GeofferyOmlette 0:f6e4e1bbb3fe 2697 src_coap_blockwise_ack_msg_ptr->msg_id = received_coap_msg_ptr->msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 2698
GeofferyOmlette 0:f6e4e1bbb3fe 2699 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len = received_coap_msg_ptr->options_list_ptr->block2_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2700 src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr = sn_coap_protocol_malloc(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2701
GeofferyOmlette 0:f6e4e1bbb3fe 2702 if(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr == NULL)
GeofferyOmlette 0:f6e4e1bbb3fe 2703 {
GeofferyOmlette 0:f6e4e1bbb3fe 2704 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr->options_list_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2705 sn_coap_protocol_free(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2706 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2707 }
GeofferyOmlette 0:f6e4e1bbb3fe 2708 memcpy(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr, received_coap_msg_ptr->options_list_ptr->block2_ptr, src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2709
GeofferyOmlette 0:f6e4e1bbb3fe 2710 /* * Payload part * */
GeofferyOmlette 0:f6e4e1bbb3fe 2711
GeofferyOmlette 0:f6e4e1bbb3fe 2712 /* Check if last block */
GeofferyOmlette 0:f6e4e1bbb3fe 2713
GeofferyOmlette 0:f6e4e1bbb3fe 2714 original_payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2715 original_payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2716
GeofferyOmlette 0:f6e4e1bbb3fe 2717 if((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len)
GeofferyOmlette 0:f6e4e1bbb3fe 2718 {
GeofferyOmlette 0:f6e4e1bbb3fe 2719 src_coap_blockwise_ack_msg_ptr->payload_len = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len - (block_size * block_number);
GeofferyOmlette 0:f6e4e1bbb3fe 2720 src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
GeofferyOmlette 0:f6e4e1bbb3fe 2721 }
GeofferyOmlette 0:f6e4e1bbb3fe 2722 /* Not last block */
GeofferyOmlette 0:f6e4e1bbb3fe 2723 else
GeofferyOmlette 0:f6e4e1bbb3fe 2724 {
GeofferyOmlette 0:f6e4e1bbb3fe 2725 /* set more - bit */
GeofferyOmlette 0:f6e4e1bbb3fe 2726 *(src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_ptr + (src_coap_blockwise_ack_msg_ptr->options_list_ptr->block2_len-1)) |= 0x08;
GeofferyOmlette 0:f6e4e1bbb3fe 2727 src_coap_blockwise_ack_msg_ptr->payload_len = block_size;
GeofferyOmlette 0:f6e4e1bbb3fe 2728 src_coap_blockwise_ack_msg_ptr->payload_ptr = stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr + (block_size * block_number);
GeofferyOmlette 0:f6e4e1bbb3fe 2729 }
GeofferyOmlette 0:f6e4e1bbb3fe 2730
GeofferyOmlette 0:f6e4e1bbb3fe 2731 /* Build and send block message */
GeofferyOmlette 0:f6e4e1bbb3fe 2732 dst_packed_data_needed_mem = sn_coap_builder_calc_needed_packet_data_size(src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2733
GeofferyOmlette 0:f6e4e1bbb3fe 2734 dst_ack_packet_data_ptr = sn_coap_protocol_malloc(dst_packed_data_needed_mem);
GeofferyOmlette 0:f6e4e1bbb3fe 2735 if(!dst_ack_packet_data_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2736 {
GeofferyOmlette 0:f6e4e1bbb3fe 2737 return NULL;
GeofferyOmlette 0:f6e4e1bbb3fe 2738 }
GeofferyOmlette 0:f6e4e1bbb3fe 2739
GeofferyOmlette 0:f6e4e1bbb3fe 2740 sn_coap_builder(dst_ack_packet_data_ptr, src_coap_blockwise_ack_msg_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2741
GeofferyOmlette 0:f6e4e1bbb3fe 2742 sn_coap_tx_callback(SN_NSDL_PROTOCOL_COAP, dst_ack_packet_data_ptr, dst_packed_data_needed_mem, src_addr_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2743
GeofferyOmlette 0:f6e4e1bbb3fe 2744 sn_coap_protocol_free(dst_ack_packet_data_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2745
GeofferyOmlette 0:f6e4e1bbb3fe 2746 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len = original_payload_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2747 stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_ptr = original_payload_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2748
GeofferyOmlette 0:f6e4e1bbb3fe 2749 if((block_size * (block_number + 1)) > stored_blockwise_msg_temp_ptr->coap_msg_ptr->payload_len)
GeofferyOmlette 0:f6e4e1bbb3fe 2750 sn_coap_protocol_linked_list_blockwise_msg_remove_current();
GeofferyOmlette 0:f6e4e1bbb3fe 2751
GeofferyOmlette 0:f6e4e1bbb3fe 2752 received_coap_msg_ptr->coap_status = COAP_STATUS_PARSER_BLOCKWISE_ACK;
GeofferyOmlette 0:f6e4e1bbb3fe 2753 }
GeofferyOmlette 0:f6e4e1bbb3fe 2754 }
GeofferyOmlette 0:f6e4e1bbb3fe 2755 }
GeofferyOmlette 0:f6e4e1bbb3fe 2756 return received_coap_msg_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2757 }
GeofferyOmlette 0:f6e4e1bbb3fe 2758
GeofferyOmlette 0:f6e4e1bbb3fe 2759 static int8_t sn_coap_convert_block_size(uint16_t block_size)
GeofferyOmlette 0:f6e4e1bbb3fe 2760 {
GeofferyOmlette 0:f6e4e1bbb3fe 2761 if(block_size == 16)
GeofferyOmlette 0:f6e4e1bbb3fe 2762 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2763 else if(block_size == 32)
GeofferyOmlette 0:f6e4e1bbb3fe 2764 return 1;
GeofferyOmlette 0:f6e4e1bbb3fe 2765 else if(block_size == 64)
GeofferyOmlette 0:f6e4e1bbb3fe 2766 return 2;
GeofferyOmlette 0:f6e4e1bbb3fe 2767 else if(block_size == 128)
GeofferyOmlette 0:f6e4e1bbb3fe 2768 return 3;
GeofferyOmlette 0:f6e4e1bbb3fe 2769 else if(block_size == 256)
GeofferyOmlette 0:f6e4e1bbb3fe 2770 return 4;
GeofferyOmlette 0:f6e4e1bbb3fe 2771 else if(block_size == 512)
GeofferyOmlette 0:f6e4e1bbb3fe 2772 return 5;
GeofferyOmlette 0:f6e4e1bbb3fe 2773 else if(block_size == 1024)
GeofferyOmlette 0:f6e4e1bbb3fe 2774 return 6;
GeofferyOmlette 0:f6e4e1bbb3fe 2775
GeofferyOmlette 0:f6e4e1bbb3fe 2776 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2777 }
GeofferyOmlette 0:f6e4e1bbb3fe 2778
GeofferyOmlette 0:f6e4e1bbb3fe 2779 static sn_coap_hdr_s *sn_coap_protocol_copy_header(sn_coap_hdr_s *source_header_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2780 {
GeofferyOmlette 0:f6e4e1bbb3fe 2781 sn_coap_hdr_s *destination_header_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2782
GeofferyOmlette 0:f6e4e1bbb3fe 2783 destination_header_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_hdr_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2784 if(!destination_header_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2785 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2786 memset(destination_header_ptr, 0, sizeof(sn_coap_hdr_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2787
GeofferyOmlette 0:f6e4e1bbb3fe 2788 destination_header_ptr->coap_status = source_header_ptr->coap_status;
GeofferyOmlette 0:f6e4e1bbb3fe 2789 destination_header_ptr->msg_type = source_header_ptr->msg_type;
GeofferyOmlette 0:f6e4e1bbb3fe 2790 destination_header_ptr->msg_code = source_header_ptr->msg_code;
GeofferyOmlette 0:f6e4e1bbb3fe 2791 destination_header_ptr->msg_id = source_header_ptr->msg_id;
GeofferyOmlette 0:f6e4e1bbb3fe 2792
GeofferyOmlette 0:f6e4e1bbb3fe 2793 if(source_header_ptr->uri_path_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2794 {
GeofferyOmlette 0:f6e4e1bbb3fe 2795 destination_header_ptr->uri_path_len = source_header_ptr->uri_path_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2796 destination_header_ptr->uri_path_ptr = sn_coap_protocol_malloc(source_header_ptr->uri_path_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2797 if(!destination_header_ptr->uri_path_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2798 {
GeofferyOmlette 0:f6e4e1bbb3fe 2799 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2800 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2801 }
GeofferyOmlette 0:f6e4e1bbb3fe 2802 memcpy(destination_header_ptr->uri_path_ptr, source_header_ptr->uri_path_ptr, source_header_ptr->uri_path_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2803 }
GeofferyOmlette 0:f6e4e1bbb3fe 2804
GeofferyOmlette 0:f6e4e1bbb3fe 2805 if(source_header_ptr->token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2806 {
GeofferyOmlette 0:f6e4e1bbb3fe 2807 destination_header_ptr->token_len = source_header_ptr->token_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2808 destination_header_ptr->token_ptr = sn_coap_protocol_malloc(source_header_ptr->token_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2809 if(!destination_header_ptr->token_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2810 {
GeofferyOmlette 0:f6e4e1bbb3fe 2811 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2812 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2813 }
GeofferyOmlette 0:f6e4e1bbb3fe 2814 memcpy(destination_header_ptr->token_ptr, source_header_ptr->token_ptr, source_header_ptr->token_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2815 }
GeofferyOmlette 0:f6e4e1bbb3fe 2816
GeofferyOmlette 0:f6e4e1bbb3fe 2817 if(source_header_ptr->content_type_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2818 {
GeofferyOmlette 0:f6e4e1bbb3fe 2819 destination_header_ptr->content_type_len = source_header_ptr->content_type_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2820 destination_header_ptr->content_type_ptr = sn_coap_protocol_malloc(source_header_ptr->content_type_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2821 if(!destination_header_ptr->content_type_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2822 {
GeofferyOmlette 0:f6e4e1bbb3fe 2823 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2824 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2825 }
GeofferyOmlette 0:f6e4e1bbb3fe 2826 memcpy(destination_header_ptr->content_type_ptr, source_header_ptr->content_type_ptr, source_header_ptr->content_type_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2827 }
GeofferyOmlette 0:f6e4e1bbb3fe 2828
GeofferyOmlette 0:f6e4e1bbb3fe 2829 /* Options list */
GeofferyOmlette 0:f6e4e1bbb3fe 2830 if(source_header_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2831 {
GeofferyOmlette 0:f6e4e1bbb3fe 2832 destination_header_ptr->options_list_ptr = sn_coap_protocol_malloc(sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2833 if(!destination_header_ptr->options_list_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2834 {
GeofferyOmlette 0:f6e4e1bbb3fe 2835 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2836 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2837 }
GeofferyOmlette 0:f6e4e1bbb3fe 2838 memset(destination_header_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
GeofferyOmlette 0:f6e4e1bbb3fe 2839
GeofferyOmlette 0:f6e4e1bbb3fe 2840
GeofferyOmlette 0:f6e4e1bbb3fe 2841 if(source_header_ptr->options_list_ptr->max_age_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2842 {
GeofferyOmlette 0:f6e4e1bbb3fe 2843 destination_header_ptr->options_list_ptr->max_age_len = source_header_ptr->options_list_ptr->max_age_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2844 destination_header_ptr->options_list_ptr->max_age_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->max_age_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2845 if(!destination_header_ptr->options_list_ptr->max_age_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2846 {
GeofferyOmlette 0:f6e4e1bbb3fe 2847 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2848 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2849 }
GeofferyOmlette 0:f6e4e1bbb3fe 2850 memcpy(destination_header_ptr->options_list_ptr->max_age_ptr, source_header_ptr->options_list_ptr->max_age_ptr, source_header_ptr->options_list_ptr->max_age_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2851 }
GeofferyOmlette 0:f6e4e1bbb3fe 2852
GeofferyOmlette 0:f6e4e1bbb3fe 2853 if(source_header_ptr->options_list_ptr->proxy_uri_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2854 {
GeofferyOmlette 0:f6e4e1bbb3fe 2855 destination_header_ptr->options_list_ptr->proxy_uri_len = source_header_ptr->options_list_ptr->proxy_uri_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2856 destination_header_ptr->options_list_ptr->proxy_uri_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->proxy_uri_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2857 if(!destination_header_ptr->options_list_ptr->proxy_uri_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2858 {
GeofferyOmlette 0:f6e4e1bbb3fe 2859 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2860 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2861 }
GeofferyOmlette 0:f6e4e1bbb3fe 2862 memcpy(destination_header_ptr->options_list_ptr->proxy_uri_ptr, source_header_ptr->options_list_ptr->proxy_uri_ptr, source_header_ptr->options_list_ptr->proxy_uri_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2863 }
GeofferyOmlette 0:f6e4e1bbb3fe 2864
GeofferyOmlette 0:f6e4e1bbb3fe 2865 if(source_header_ptr->options_list_ptr->etag_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2866 {
GeofferyOmlette 0:f6e4e1bbb3fe 2867 destination_header_ptr->options_list_ptr->etag_len = source_header_ptr->options_list_ptr->etag_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2868 destination_header_ptr->options_list_ptr->etag_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->etag_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2869 if(!destination_header_ptr->options_list_ptr->etag_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2870 {
GeofferyOmlette 0:f6e4e1bbb3fe 2871 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2872 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2873 }
GeofferyOmlette 0:f6e4e1bbb3fe 2874 memcpy(destination_header_ptr->options_list_ptr->etag_ptr, source_header_ptr->options_list_ptr->etag_ptr, source_header_ptr->options_list_ptr->etag_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2875 }
GeofferyOmlette 0:f6e4e1bbb3fe 2876
GeofferyOmlette 0:f6e4e1bbb3fe 2877 if(source_header_ptr->options_list_ptr->uri_host_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2878 {
GeofferyOmlette 0:f6e4e1bbb3fe 2879 destination_header_ptr->options_list_ptr->uri_host_len = source_header_ptr->options_list_ptr->uri_host_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2880 destination_header_ptr->options_list_ptr->uri_host_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_host_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2881 if(!destination_header_ptr->options_list_ptr->uri_host_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2882 {
GeofferyOmlette 0:f6e4e1bbb3fe 2883 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2884 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2885 }
GeofferyOmlette 0:f6e4e1bbb3fe 2886 memcpy(destination_header_ptr->options_list_ptr->uri_host_ptr, source_header_ptr->options_list_ptr->uri_host_ptr, source_header_ptr->options_list_ptr->uri_host_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2887 }
GeofferyOmlette 0:f6e4e1bbb3fe 2888
GeofferyOmlette 0:f6e4e1bbb3fe 2889 if(source_header_ptr->options_list_ptr->location_path_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2890 {
GeofferyOmlette 0:f6e4e1bbb3fe 2891 destination_header_ptr->options_list_ptr->location_path_len = source_header_ptr->options_list_ptr->location_path_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2892 destination_header_ptr->options_list_ptr->location_path_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_path_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2893 if(!destination_header_ptr->options_list_ptr->location_path_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2894 {
GeofferyOmlette 0:f6e4e1bbb3fe 2895 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2896 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2897 }
GeofferyOmlette 0:f6e4e1bbb3fe 2898 memcpy(destination_header_ptr->options_list_ptr->location_path_ptr, source_header_ptr->options_list_ptr->location_path_ptr, source_header_ptr->options_list_ptr->location_path_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2899 }
GeofferyOmlette 0:f6e4e1bbb3fe 2900
GeofferyOmlette 0:f6e4e1bbb3fe 2901 if(source_header_ptr->options_list_ptr->uri_port_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2902 {
GeofferyOmlette 0:f6e4e1bbb3fe 2903 destination_header_ptr->options_list_ptr->uri_port_len = source_header_ptr->options_list_ptr->uri_port_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2904 destination_header_ptr->options_list_ptr->uri_port_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_port_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2905 if(!destination_header_ptr->options_list_ptr->uri_port_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2906 {
GeofferyOmlette 0:f6e4e1bbb3fe 2907 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2908 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2909 }
GeofferyOmlette 0:f6e4e1bbb3fe 2910 memcpy(destination_header_ptr->options_list_ptr->uri_port_ptr, source_header_ptr->options_list_ptr->uri_port_ptr, source_header_ptr->options_list_ptr->uri_port_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2911 }
GeofferyOmlette 0:f6e4e1bbb3fe 2912
GeofferyOmlette 0:f6e4e1bbb3fe 2913 if(source_header_ptr->options_list_ptr->location_query_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2914 {
GeofferyOmlette 0:f6e4e1bbb3fe 2915 destination_header_ptr->options_list_ptr->location_query_len = source_header_ptr->options_list_ptr->location_query_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2916 destination_header_ptr->options_list_ptr->location_query_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->location_query_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2917 if(!destination_header_ptr->options_list_ptr->location_query_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2918 {
GeofferyOmlette 0:f6e4e1bbb3fe 2919 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2920 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2921 }
GeofferyOmlette 0:f6e4e1bbb3fe 2922 memcpy(destination_header_ptr->options_list_ptr->location_query_ptr, source_header_ptr->options_list_ptr->location_query_ptr, source_header_ptr->options_list_ptr->location_query_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2923 }
GeofferyOmlette 0:f6e4e1bbb3fe 2924
GeofferyOmlette 0:f6e4e1bbb3fe 2925 if(source_header_ptr->options_list_ptr->observe_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2926 {
GeofferyOmlette 0:f6e4e1bbb3fe 2927 destination_header_ptr->options_list_ptr->observe_len = source_header_ptr->options_list_ptr->observe_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2928 destination_header_ptr->options_list_ptr->observe_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->observe_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2929 if(!destination_header_ptr->options_list_ptr->observe_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2930 {
GeofferyOmlette 0:f6e4e1bbb3fe 2931 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2932 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2933 }
GeofferyOmlette 0:f6e4e1bbb3fe 2934 memcpy(destination_header_ptr->options_list_ptr->observe_ptr, source_header_ptr->options_list_ptr->observe_ptr, source_header_ptr->options_list_ptr->observe_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2935 }
GeofferyOmlette 0:f6e4e1bbb3fe 2936
GeofferyOmlette 0:f6e4e1bbb3fe 2937 if(source_header_ptr->options_list_ptr->accept_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2938 {
GeofferyOmlette 0:f6e4e1bbb3fe 2939 destination_header_ptr->options_list_ptr->accept_len = source_header_ptr->options_list_ptr->accept_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2940 destination_header_ptr->options_list_ptr->accept_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->accept_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2941 if(!destination_header_ptr->options_list_ptr->accept_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2942 {
GeofferyOmlette 0:f6e4e1bbb3fe 2943 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2944 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2945 }
GeofferyOmlette 0:f6e4e1bbb3fe 2946 memcpy(destination_header_ptr->options_list_ptr->accept_ptr, source_header_ptr->options_list_ptr->accept_ptr, source_header_ptr->options_list_ptr->accept_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2947 }
GeofferyOmlette 0:f6e4e1bbb3fe 2948
GeofferyOmlette 0:f6e4e1bbb3fe 2949 if(source_header_ptr->options_list_ptr->uri_query_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2950 {
GeofferyOmlette 0:f6e4e1bbb3fe 2951 destination_header_ptr->options_list_ptr->uri_query_len = source_header_ptr->options_list_ptr->uri_query_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2952 destination_header_ptr->options_list_ptr->uri_query_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->uri_query_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2953 if(!destination_header_ptr->options_list_ptr->uri_query_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2954 {
GeofferyOmlette 0:f6e4e1bbb3fe 2955 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2956 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2957 }
GeofferyOmlette 0:f6e4e1bbb3fe 2958 memcpy(destination_header_ptr->options_list_ptr->uri_query_ptr, source_header_ptr->options_list_ptr->uri_query_ptr, source_header_ptr->options_list_ptr->uri_query_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2959 }
GeofferyOmlette 0:f6e4e1bbb3fe 2960
GeofferyOmlette 0:f6e4e1bbb3fe 2961 if(source_header_ptr->options_list_ptr->block1_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2962 {
GeofferyOmlette 0:f6e4e1bbb3fe 2963 destination_header_ptr->options_list_ptr->block1_len = source_header_ptr->options_list_ptr->block1_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2964 destination_header_ptr->options_list_ptr->block1_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->block1_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2965 if(!destination_header_ptr->options_list_ptr->block1_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2966 {
GeofferyOmlette 0:f6e4e1bbb3fe 2967 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2968 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2969 }
GeofferyOmlette 0:f6e4e1bbb3fe 2970 memcpy(destination_header_ptr->options_list_ptr->block1_ptr, source_header_ptr->options_list_ptr->block1_ptr, source_header_ptr->options_list_ptr->block1_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2971 }
GeofferyOmlette 0:f6e4e1bbb3fe 2972
GeofferyOmlette 0:f6e4e1bbb3fe 2973 if(source_header_ptr->options_list_ptr->block2_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2974 {
GeofferyOmlette 0:f6e4e1bbb3fe 2975 destination_header_ptr->options_list_ptr->block2_len = source_header_ptr->options_list_ptr->block2_len;
GeofferyOmlette 0:f6e4e1bbb3fe 2976 destination_header_ptr->options_list_ptr->block2_ptr = sn_coap_protocol_malloc(source_header_ptr->options_list_ptr->block2_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2977 if(!destination_header_ptr->options_list_ptr->block2_ptr)
GeofferyOmlette 0:f6e4e1bbb3fe 2978 {
GeofferyOmlette 0:f6e4e1bbb3fe 2979 sn_coap_parser_release_allocated_coap_msg_mem(destination_header_ptr);
GeofferyOmlette 0:f6e4e1bbb3fe 2980 return 0;
GeofferyOmlette 0:f6e4e1bbb3fe 2981 }
GeofferyOmlette 0:f6e4e1bbb3fe 2982 memcpy(destination_header_ptr->options_list_ptr->block2_ptr, source_header_ptr->options_list_ptr->block2_ptr, source_header_ptr->options_list_ptr->block2_len);
GeofferyOmlette 0:f6e4e1bbb3fe 2983 }
GeofferyOmlette 0:f6e4e1bbb3fe 2984 }
GeofferyOmlette 0:f6e4e1bbb3fe 2985
GeofferyOmlette 0:f6e4e1bbb3fe 2986 return destination_header_ptr;
GeofferyOmlette 0:f6e4e1bbb3fe 2987 }
GeofferyOmlette 0:f6e4e1bbb3fe 2988 #endif