20
20
#include "coap_service_api_internal.h"
21
21
#include "coap_message_handler.h"
22
22
#include "mbed-coap/sn_coap_protocol.h"
23
+ #include "source/include/sn_coap_protocol_internal.h"
23
24
#include "socket_api.h"
24
25
#include "ns_types.h"
25
26
#include "ns_list.h"
@@ -138,7 +139,6 @@ void transaction_delete(coap_transaction_t *this)
138
139
if (!coap_message_handler_transaction_valid (this )) {
139
140
return ;
140
141
}
141
-
142
142
ns_list_remove (& request_list , this );
143
143
transaction_free (this );
144
144
@@ -163,11 +163,14 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
163
163
static int8_t coap_rx_function (sn_coap_hdr_s * resp_ptr , sn_nsdl_addr_s * address_ptr , void * param )
164
164
{
165
165
coap_transaction_t * this = NULL ;
166
- (void )address_ptr ;
167
166
(void )param ;
168
167
168
+ if (resp_ptr -> coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_DONE ) {
169
+ return 0 ;
170
+ }
171
+
169
172
tr_warn ("transaction was not handled %d" , resp_ptr -> msg_id );
170
- if (!resp_ptr ) {
173
+ if (!resp_ptr || ! address_ptr ) {
171
174
return -1 ;
172
175
}
173
176
if (resp_ptr -> token_ptr ){
@@ -193,7 +196,7 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
193
196
}
194
197
195
198
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 ));
197
200
if (handle == NULL ) {
198
201
return NULL ;
199
202
}
@@ -207,13 +210,16 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
207
210
208
211
handle -> coap = sn_coap_protocol_init (used_malloc_func_ptr , used_free_func_ptr , used_tx_callback_ptr , & coap_rx_function );
209
212
if ( !handle -> coap ){
210
- used_free_func_ptr (handle );
213
+ ns_dyn_mem_free (handle );
211
214
return NULL ;
212
215
}
213
216
214
217
/* Set default buffer size for CoAP duplicate message detection */
215
218
sn_coap_protocol_set_duplicate_buffer_size (handle -> coap , DUPLICATE_MESSAGE_BUFFER_SIZE );
216
219
220
+ /* Set default blockwise message size. */
221
+ sn_coap_protocol_set_block_size (handle -> coap , DEFAULT_BLOCKWISE_DATA_SIZE );
222
+
217
223
/* Set default CoAP retransmission paramters */
218
224
sn_coap_protocol_set_retransmission_parameters (handle -> coap , COAP_RESENDING_COUNT , COAP_RESENDING_INTERVAL );
219
225
@@ -263,6 +269,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
263
269
sn_nsdl_addr_s src_addr ;
264
270
sn_coap_hdr_s * coap_message ;
265
271
int16_t ret_val = 0 ;
272
+ coap_transaction_t * this = NULL ;
266
273
267
274
if (!cb || !handle ) {
268
275
return -1 ;
@@ -273,8 +280,19 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
273
280
src_addr .type = SN_NSDL_ADDRESS_TYPE_IPV6 ;
274
281
src_addr .port = port ;
275
282
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 );
277
294
if (coap_message == NULL ) {
295
+ transaction_delete (transaction_ptr );
278
296
tr_err ("CoAP Parsing failed" );
279
297
return -1 ;
280
298
}
@@ -284,36 +302,26 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
284
302
/* Check, if coap itself sends response, or block receiving is ongoing... */
285
303
if (coap_message -> coap_status != COAP_STATUS_OK && coap_message -> coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED ) {
286
304
tr_debug ("CoAP library responds" );
305
+ transaction_delete (transaction_ptr );
287
306
ret_val = -1 ;
288
307
goto exit ;
289
308
}
290
309
291
310
/* Request received */
292
311
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 ;
313
317
}
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 ;
314
323
/* Response received */
315
324
} else {
316
- coap_transaction_t * this = NULL ;
317
325
if (coap_message -> token_ptr ) {
318
326
this = transaction_find_client_by_token (coap_message -> token_ptr , coap_message -> token_len , source_addr_ptr , port );
319
327
}
@@ -331,6 +339,10 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
331
339
}
332
340
333
341
exit :
342
+ if (coap_message -> coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED ) {
343
+ handle -> sn_coap_service_free (coap_message -> payload_ptr );
344
+ }
345
+
334
346
sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , coap_message );
335
347
336
348
return ret_val ;
@@ -350,7 +362,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
350
362
tr_debug ("Service %d, send CoAP request payload_len %d" , service_id , payload_len );
351
363
transaction_ptr = transaction_create ();
352
364
353
- if (!uri || !transaction_ptr ) {
365
+ if (!uri || !transaction_ptr || ! handle ) {
354
366
return 0 ;
355
367
}
356
368
@@ -383,7 +395,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
383
395
384
396
request .payload_len = payload_len ;
385
397
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 ));
387
402
data_ptr = own_alloc (data_len );
388
403
if (data_len > 0 && !data_ptr ){
389
404
transaction_delete (transaction_ptr );
@@ -408,6 +423,10 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
408
423
409
424
// Free allocated data
410
425
own_free (data_ptr );
426
+ if (request .options_list_ptr ) {
427
+ own_free (request .options_list_ptr );
428
+ }
429
+
411
430
if (request_response_cb == NULL ){
412
431
//No response expected
413
432
return 0 ;
@@ -426,8 +445,10 @@ static int8_t coap_message_handler_resp_build_and_send(coap_msg_handler_t *handl
426
445
dst_addr .addr_len = 16 ;
427
446
dst_addr .type = SN_NSDL_ADDRESS_TYPE_IPV6 ;
428
447
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 ));
431
452
data_ptr = own_alloc (data_len );
432
453
if (data_len > 0 && !data_ptr ) {
433
454
return -1 ;
0 commit comments