@@ -74,12 +74,13 @@ static coap_transaction_t *transaction_create(void)
74
74
if (this ) {
75
75
memset (this , 0 , sizeof (coap_transaction_t ));
76
76
this -> client_request = true;// default to client initiated method
77
+ this -> create_time = coap_service_get_internal_timer_ticks ();
77
78
ns_list_add_to_start (& request_list , this );
78
79
}
79
80
80
81
return this ;
81
82
}
82
- static void transaction_delete (coap_transaction_t * this )
83
+ void transaction_delete (coap_transaction_t * this )
83
84
{
84
85
if (this ) {
85
86
ns_list_remove (& request_list , this );
@@ -111,8 +112,6 @@ static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_
111
112
return 0 ;
112
113
}
113
114
114
- static void coap_service_build_content_format (sn_coap_hdr_s * header , sn_coap_content_format_e format );
115
-
116
115
coap_msg_handler_t * coap_message_handler_init (void * (* used_malloc_func_ptr )(uint16_t ), void (* used_free_func_ptr )(void * ),
117
116
uint8_t (* used_tx_callback_ptr )(uint8_t * , uint16_t , sn_nsdl_addr_s * , void * )){
118
117
@@ -280,7 +279,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
280
279
request .msg_code = msg_code ;
281
280
request .uri_path_ptr = (uint8_t * )uri ;
282
281
request .uri_path_len = strlen (uri );
283
- coap_service_build_content_format ( & request , cont_type ) ;
282
+ request . content_format = cont_type ;
284
283
285
284
do {
286
285
randLIB_get_n_bytes_random (token ,4 );
@@ -310,7 +309,6 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
310
309
return transaction_ptr -> msg_id ;
311
310
}
312
311
313
- //TODO: refactor this to use nsdl
314
312
int8_t coap_message_handler_response_send (coap_msg_handler_t * handle , int8_t service_id , uint8_t options , sn_coap_hdr_s * request_ptr , sn_coap_msg_code_e message_code , sn_coap_content_format_e content_type , const uint8_t * payload_ptr , uint16_t payload_len )
315
313
{
316
314
coap_transaction_t * transaction_ptr ;
@@ -344,46 +342,34 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
344
342
}
345
343
response -> payload_len = payload_len ;
346
344
response -> payload_ptr = (uint8_t * ) payload_ptr ; // Cast away const and trust that nsdl doesn't modify...
347
- coap_service_build_content_format ( response , content_type ) ;
345
+ response -> content_format = content_type ;
348
346
349
347
data_len = sn_coap_builder_calc_needed_packet_data_size (response );
350
348
data_ptr = own_alloc (data_len );
351
349
if (data_len > 0 && !data_ptr ) {
352
350
sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , response );
353
- //TODO deallocate stuff i quess
354
351
return -1 ;
355
352
}
356
353
sn_coap_protocol_build (handle -> coap , & dst_addr , data_ptr , response , transaction_ptr );
357
354
sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , response );
358
355
handle -> sn_coap_tx_callback (data_ptr , data_len , & dst_addr , transaction_ptr );
359
356
sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , request_ptr );
360
- transaction_delete (transaction_ptr );
361
357
own_free (data_ptr );
362
358
return 0 ;
363
359
}
364
360
365
361
int8_t coap_message_handler_exec (coap_msg_handler_t * handle , uint32_t current_time ){
362
+
366
363
if ( !handle ){
367
364
return -1 ;
368
365
}
369
- return sn_coap_protocol_exec (handle -> coap , current_time );
370
- }
371
366
372
- static void coap_service_build_content_format (sn_coap_hdr_s * header , sn_coap_content_format_e format )
373
- {
374
- header -> content_format = format ;
375
-
376
- // if (format == COAP_CT_NONE) {
377
- // return;
378
- // }
379
- // if (format == 0) { /* text/plain */
380
- // header->content_type_len = 0;
381
- // } else if (format <= 0xff) {
382
- // header->content_type_ptr[0] = format;
383
- // header->content_type_len = 1;
384
- // } else {
385
- // header->content_type_ptr[0] = format >> 8;
386
- // header->content_type_ptr[1] = format & 0xff;
387
- // header->content_type_len = 2;
388
- // }
367
+ // Remove outdated transactions from queue
368
+ ns_list_foreach_safe (coap_transaction_t , transaction , & request_list ) {
369
+ if ((transaction -> create_time + TRANSACTION_LIFETIME ) < current_time ) {
370
+ transaction_delete (transaction );
371
+ }
372
+ }
373
+
374
+ return sn_coap_protocol_exec (handle -> coap , current_time );
389
375
}
0 commit comments