Skip to content

Commit 9c88fcd

Browse files
author
Tero Heinonen
authored
Delete transactions that are not supposed to get reply (#37)
* Delete transactions that are not supposed to get reply When sending non-confirmable request that is not supposed to get reply, it must be deleted after sending. Also some general code-cleaning included. * Remove outdated transactions from queue If transaction does not get response, it will be deleted from queue.
1 parent b3419e0 commit 9c88fcd

File tree

4 files changed

+33
-40
lines changed

4 files changed

+33
-40
lines changed

source/coap_message_handler.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
116115
coap_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
314312
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)
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

365361
int8_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
}

source/coap_service_api.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_ad
151151
}
152152
memcpy(transaction_ptr->data_ptr, data_ptr, data_len);
153153
transaction_ptr->data_len = data_len;
154+
} else if (transaction_ptr->resp_cb == NULL ){
155+
transaction_delete(transaction_ptr);
154156
}
155157

156158
return 0;
@@ -191,7 +193,7 @@ static int16_t coap_msg_process_callback(int8_t socket_id, sn_coap_hdr_s *coap_m
191193
}
192194

193195
uri_registration_t *uri_reg_ptr = uri_registration_find(this, coap_message->uri_path_ptr, coap_message->uri_path_len);
194-
if (transaction_ptr && uri_reg_ptr && uri_reg_ptr->request_recv_cb) {
196+
if (uri_reg_ptr && uri_reg_ptr->request_recv_cb) {
195197
tr_debug("Service %d, call request recv cb uri %.*s", this->service_id, coap_message->uri_path_len, coap_message->uri_path_ptr);
196198

197199
if ((this->service_options & COAP_SERVICE_OPTIONS_SECURE_BYPASS) == COAP_SERVICE_OPTIONS_SECURE_BYPASS ) {//TODO Add secure bypass option
@@ -240,13 +242,6 @@ static int send_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port,
240242
return -1;
241243
}
242244

243-
//static void sec_conn_closed_cb(int8_t socket_id)
244-
//{
245-
// coap_service_t *this = service_find_by_socket(socket_id);
246-
247-
// tr_debug("Secure socket was closed by end device");
248-
//}
249-
250245
static void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t port, uint8_t keyblock[static 40])
251246
{
252247
//TODO: this is not enough if shared socket. Inform all!
@@ -255,7 +250,6 @@ static void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t p
255250
this->coap_security_done_cb(this->service_id, address, keyblock);
256251
}
257252

258-
//TODO refactor this away. There should be no transaction_ptr(s) before done_cb has been called
259253
//TODO: send all unsend transactions if more than 1
260254
coap_transaction_t *transaction_ptr = coap_message_handler_find_transaction(address, port);
261255
if (transaction_ptr && transaction_ptr->data_ptr) {
@@ -269,7 +263,9 @@ static void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t p
269263
ns_dyn_mem_free(transaction_ptr->data_ptr);
270264
transaction_ptr->data_ptr = NULL;
271265
transaction_ptr->data_len = 0;
272-
//TODO: who deletes transaction incase no response is required
266+
if (transaction_ptr->resp_cb == NULL) {
267+
transaction_delete(transaction_ptr);
268+
}
273269
}
274270
}
275271

source/include/coap_message_handler.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include "sn_nsdl.h"
2525
#include "ns_list.h"
2626

27+
#define TRANSACTION_LIFETIME 180
28+
2729
/**
2830
* \brief Service message response receive callback.
2931
*
@@ -47,14 +49,16 @@ typedef struct coap_msg_handler_s {
4749

4850
typedef struct coap_transaction {
4951
uint8_t remote_address[16];
50-
uint16_t remote_port;
5152
uint8_t token[4];
53+
uint32_t create_time;
54+
uint16_t remote_port;
5255
uint16_t msg_id;
56+
uint16_t data_len;
5357
int8_t service_id;
5458
uint8_t options;
55-
bool client_request: 1;
5659
uint8_t *data_ptr;
57-
uint16_t data_len;
60+
bool client_request: 1;
61+
5862
coap_message_handler_response_recv *resp_cb;
5963
ns_list_link_t link;
6064
} coap_transaction_t;
@@ -81,4 +85,6 @@ extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int
8185

8286
extern int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time);
8387

88+
extern void transaction_delete(coap_transaction_t *this);
89+
8490
#endif

test/coap-service/unittest/stub/coap_message_handler_stub.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ coap_msg_handler_t *coap_message_handler_init(void *(*used_malloc_func_ptr)(uint
1717
return coap_message_handler_stub.coap_ptr;
1818
}
1919

20+
void transaction_delete(coap_transaction_t *this)
21+
{
22+
23+
}
24+
2025
int8_t coap_message_handler_destroy(coap_msg_handler_t *handle)
2126
{
2227
return coap_message_handler_stub.int8_value;

0 commit comments

Comments
 (0)