2020#include "coap_service_api_internal.h"
2121#include "coap_message_handler.h"
2222#include "mbed-coap/sn_coap_protocol.h"
23+ #include "source/include/sn_coap_protocol_internal.h"
2324#include "socket_api.h"
2425#include "ns_types.h"
2526#include "ns_list.h"
@@ -138,7 +139,6 @@ void transaction_delete(coap_transaction_t *this)
138139 if (!coap_message_handler_transaction_valid (this )) {
139140 return ;
140141 }
141-
142142 ns_list_remove (& request_list , this );
143143 transaction_free (this );
144144
@@ -163,11 +163,14 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
163163static int8_t coap_rx_function (sn_coap_hdr_s * resp_ptr , sn_nsdl_addr_s * address_ptr , void * param )
164164{
165165 coap_transaction_t * this = NULL ;
166- (void )address_ptr ;
167166 (void )param ;
168167
168+ if (resp_ptr -> coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_DONE ) {
169+ return 0 ;
170+ }
171+
169172 tr_warn ("transaction was not handled %d" , resp_ptr -> msg_id );
170- if (!resp_ptr ) {
173+ if (!resp_ptr || ! address_ptr ) {
171174 return -1 ;
172175 }
173176 if (resp_ptr -> token_ptr ){
@@ -193,7 +196,7 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
193196 }
194197
195198 coap_msg_handler_t * handle ;
196- handle = used_malloc_func_ptr (sizeof (coap_msg_handler_t ));
199+ handle = ns_dyn_mem_alloc (sizeof (coap_msg_handler_t ));
197200 if (handle == NULL ) {
198201 return NULL ;
199202 }
@@ -207,13 +210,16 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
207210
208211 handle -> coap = sn_coap_protocol_init (used_malloc_func_ptr , used_free_func_ptr , used_tx_callback_ptr , & coap_rx_function );
209212 if ( !handle -> coap ){
210- used_free_func_ptr (handle );
213+ ns_dyn_mem_free (handle );
211214 return NULL ;
212215 }
213216
214217 /* Set default buffer size for CoAP duplicate message detection */
215218 sn_coap_protocol_set_duplicate_buffer_size (handle -> coap , DUPLICATE_MESSAGE_BUFFER_SIZE );
216219
220+ /* Set default blockwise message size. */
221+ sn_coap_protocol_set_block_size (handle -> coap , DEFAULT_BLOCKWISE_DATA_SIZE );
222+
217223 /* Set default CoAP retransmission paramters */
218224 sn_coap_protocol_set_retransmission_parameters (handle -> coap , COAP_RESENDING_COUNT , COAP_RESENDING_INTERVAL );
219225
@@ -263,6 +269,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
263269 sn_nsdl_addr_s src_addr ;
264270 sn_coap_hdr_s * coap_message ;
265271 int16_t ret_val = 0 ;
272+ coap_transaction_t * this = NULL ;
266273
267274 if (!cb || !handle ) {
268275 return -1 ;
@@ -273,8 +280,19 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
273280 src_addr .type = SN_NSDL_ADDRESS_TYPE_IPV6 ;
274281 src_addr .port = port ;
275282
276- coap_message = sn_coap_protocol_parse (handle -> coap , & src_addr , data_len , data_ptr , NULL );
283+ coap_transaction_t * transaction_ptr = transaction_create ();
284+ if (!transaction_ptr ) {
285+ return -1 ;
286+ }
287+ transaction_ptr -> service_id = coap_service_id_find_by_socket (socket_id );
288+ transaction_ptr -> client_request = false;// this is server transaction
289+ memcpy (transaction_ptr -> local_address , * (dst_addr_ptr ) == 0xFF ? ns_in6addr_any : dst_addr_ptr , 16 );
290+ memcpy (transaction_ptr -> remote_address , source_addr_ptr , 16 );
291+ transaction_ptr -> remote_port = port ;
292+
293+ coap_message = sn_coap_protocol_parse (handle -> coap , & src_addr , data_len , data_ptr , transaction_ptr );
277294 if (coap_message == NULL ) {
295+ transaction_delete (transaction_ptr );
278296 tr_err ("CoAP Parsing failed" );
279297 return -1 ;
280298 }
@@ -284,36 +302,26 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
284302 /* Check, if coap itself sends response, or block receiving is ongoing... */
285303 if (coap_message -> coap_status != COAP_STATUS_OK && coap_message -> coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED ) {
286304 tr_debug ("CoAP library responds" );
305+ transaction_delete (transaction_ptr );
287306 ret_val = -1 ;
288307 goto exit ;
289308 }
290309
291310 /* Request received */
292311 if (coap_message -> msg_code > 0 && coap_message -> msg_code < 32 ) {
293- coap_transaction_t * transaction_ptr = transaction_create ();
294- if (transaction_ptr ) {
295- transaction_ptr -> service_id = coap_service_id_find_by_socket (socket_id );
296- transaction_ptr -> msg_id = coap_message -> msg_id ;
297- transaction_ptr -> client_request = false;// this is server transaction
298- transaction_ptr -> req_msg_type = coap_message -> msg_type ;
299- memcpy (transaction_ptr -> local_address , * (dst_addr_ptr ) == 0xFF ? ns_in6addr_any : dst_addr_ptr , 16 );
300- memcpy (transaction_ptr -> remote_address , source_addr_ptr , 16 );
301- if (coap_message -> token_len ) {
302- memcpy (transaction_ptr -> token , coap_message -> token_ptr , coap_message -> token_len );
303- transaction_ptr -> token_len = coap_message -> token_len ;
304- }
305- transaction_ptr -> remote_port = port ;
306- if (cb (socket_id , coap_message , transaction_ptr ) < 0 ) {
307- // negative return value = message ignored -> delete transaction
308- transaction_delete (transaction_ptr );
309- }
310- goto exit ;
311- } else {
312- ret_val = -1 ;
312+ transaction_ptr -> msg_id = coap_message -> msg_id ;
313+ transaction_ptr -> req_msg_type = coap_message -> msg_type ;
314+ if (coap_message -> token_len ) {
315+ memcpy (transaction_ptr -> token , coap_message -> token_ptr , coap_message -> token_len );
316+ transaction_ptr -> token_len = coap_message -> token_len ;
313317 }
318+ if (cb (socket_id , coap_message , transaction_ptr ) < 0 ) {
319+ // negative return value = message ignored -> delete transaction
320+ transaction_delete (transaction_ptr );
321+ }
322+ goto exit ;
314323 /* Response received */
315324 } else {
316- coap_transaction_t * this = NULL ;
317325 if (coap_message -> token_ptr ) {
318326 this = transaction_find_client_by_token (coap_message -> token_ptr , coap_message -> token_len , source_addr_ptr , port );
319327 }
@@ -331,6 +339,10 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
331339 }
332340
333341exit :
342+ if (coap_message -> coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED ) {
343+ handle -> sn_coap_service_free (coap_message -> payload_ptr );
344+ }
345+
334346 sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , coap_message );
335347
336348 return ret_val ;
@@ -350,7 +362,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
350362 tr_debug ("Service %d, send CoAP request payload_len %d" , service_id , payload_len );
351363 transaction_ptr = transaction_create ();
352364
353- if (!uri || !transaction_ptr ) {
365+ if (!uri || !transaction_ptr || ! handle ) {
354366 return 0 ;
355367 }
356368
@@ -383,7 +395,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
383395
384396 request .payload_len = payload_len ;
385397 request .payload_ptr = (uint8_t * ) payload_ptr ; // Cast away const and trust that nsdl doesn't modify...
386- data_len = sn_coap_builder_calc_needed_packet_data_size (& request );
398+
399+ prepare_blockwise_message (handle -> coap , & request );
400+
401+ data_len = sn_coap_builder_calc_needed_packet_data_size_2 (& request , sn_coap_protocol_get_configured_blockwise_size (handle -> coap ));
387402 data_ptr = own_alloc (data_len );
388403 if (data_len > 0 && !data_ptr ){
389404 transaction_delete (transaction_ptr );
@@ -408,6 +423,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
408423
409424 // Free allocated data
410425 own_free (data_ptr );
426+ if (request .options_list_ptr ) {
427+ own_free (request .options_list_ptr );
428+ }
429+
411430 if (request_response_cb == NULL ){
412431 //No response expected
413432 return 0 ;
@@ -426,8 +445,10 @@ static int8_t coap_message_handler_resp_build_and_send(coap_msg_handler_t *handl
426445 dst_addr .addr_len = 16 ;
427446 dst_addr .type = SN_NSDL_ADDRESS_TYPE_IPV6 ;
428447 dst_addr .port = transaction_ptr -> remote_port ;
429-
430- data_len = sn_coap_builder_calc_needed_packet_data_size (coap_msg_ptr );
448+ #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
449+ prepare_blockwise_message (handle -> coap , coap_msg_ptr );
450+ #endif
451+ data_len = sn_coap_builder_calc_needed_packet_data_size_2 (coap_msg_ptr , sn_coap_protocol_get_configured_blockwise_size (handle -> coap ));
431452 data_ptr = own_alloc (data_len );
432453 if (data_len > 0 && !data_ptr ) {
433454 return -1 ;
0 commit comments