@@ -74,12 +74,13 @@ static coap_transaction_t *transaction_create(void)
7474 if (this ) {
7575 memset (this , 0 , sizeof (coap_transaction_t ));
7676 this -> client_request = true;// default to client initiated method
77+ this -> create_time = coap_service_get_internal_timer_ticks ();
7778 ns_list_add_to_start (& request_list , this );
7879 }
7980
8081 return this ;
8182}
82- static void transaction_delete (coap_transaction_t * this )
83+ void transaction_delete (coap_transaction_t * this )
8384{
8485 if (this ) {
8586 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_
111112 return 0 ;
112113}
113114
114- static void coap_service_build_content_format (sn_coap_hdr_s * header , sn_coap_content_format_e format );
115-
116115coap_msg_handler_t * coap_message_handler_init (void * (* used_malloc_func_ptr )(uint16_t ), void (* used_free_func_ptr )(void * ),
117116 uint8_t (* used_tx_callback_ptr )(uint8_t * , uint16_t , sn_nsdl_addr_s * , void * )){
118117
@@ -280,7 +279,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
280279 request .msg_code = msg_code ;
281280 request .uri_path_ptr = (uint8_t * )uri ;
282281 request .uri_path_len = strlen (uri );
283- coap_service_build_content_format ( & request , cont_type ) ;
282+ request . content_format = cont_type ;
284283
285284 do {
286285 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
310309 return transaction_ptr -> msg_id ;
311310}
312311
313- //TODO: refactor this to use nsdl
314312int8_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 )
315313{
316314 coap_transaction_t * transaction_ptr ;
@@ -344,46 +342,34 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
344342 }
345343 response -> payload_len = payload_len ;
346344 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 ;
348346
349347 data_len = sn_coap_builder_calc_needed_packet_data_size (response );
350348 data_ptr = own_alloc (data_len );
351349 if (data_len > 0 && !data_ptr ) {
352350 sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , response );
353- //TODO deallocate stuff i quess
354351 return -1 ;
355352 }
356353 sn_coap_protocol_build (handle -> coap , & dst_addr , data_ptr , response , transaction_ptr );
357354 sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , response );
358355 handle -> sn_coap_tx_callback (data_ptr , data_len , & dst_addr , transaction_ptr );
359356 sn_coap_parser_release_allocated_coap_msg_mem (handle -> coap , request_ptr );
360- transaction_delete (transaction_ptr );
361357 own_free (data_ptr );
362358 return 0 ;
363359}
364360
365361int8_t coap_message_handler_exec (coap_msg_handler_t * handle , uint32_t current_time ){
362+
366363 if ( !handle ){
367364 return -1 ;
368365 }
369- return sn_coap_protocol_exec (handle -> coap , current_time );
370- }
371366
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 );
389375}
0 commit comments