Skip to content

Commit 6fd5003

Browse files
author
Arto Kinnunen
authored
Adjust request message validity lifetime (ARMmbed#93)
Some applications need to wait for COAP response before they can continue. The COAP response timeout could be shortened when remote device is not sending the response. Adjust request message lifetime based on sn_coap_protocol message builder return value.
1 parent f6281ed commit 6fd5003

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

source/coap_message_handler.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,20 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
389389
transaction_delete(transaction_ptr);
390390
return 0;
391391
}
392-
sn_coap_protocol_build(handle->coap, &dst_addr, data_ptr, &request, transaction_ptr);
392+
int16_t sn_coap_ret = sn_coap_protocol_build(handle->coap, &dst_addr, data_ptr, &request, transaction_ptr);
393+
if (sn_coap_ret == -4) {
394+
/*
395+
* Not able to add message to resend queue, adjust message lifetime to one resending
396+
*/
397+
transaction_ptr->valid_until = coap_service_get_internal_timer_ticks() + COAP_RESENDING_INTERVAL;
398+
} else if (sn_coap_ret < 0) {
399+
/*
400+
* Failed to build message, set transaction validity time to minimum to get this transaction cleared
401+
* immediately and callback called.
402+
*/
403+
transaction_ptr->valid_until = coap_service_get_internal_timer_ticks();
404+
}
405+
393406
transaction_ptr->msg_id = request.msg_id;
394407
handle->sn_coap_tx_callback(data_ptr, data_len, &dst_addr, transaction_ptr);
395408

test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,36 @@ bool test_coap_message_handler_request_send()
252252
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
253253
return false;
254254

255+
/* Clear all transactions */
256+
if( 0 != coap_message_handler_exec(handle, 0xffffffff))
257+
return false;
258+
259+
sn_coap_protocol_stub.expectedInt16 = -4;
260+
nsdynmemlib_stub.returnCounter = 3;
261+
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb))
262+
return false;
263+
264+
transaction_cb = 0;
265+
sn_coap_protocol_stub.expectedInt8 = 0;
266+
if( 0 != coap_message_handler_exec(handle, 12))
267+
return false;
268+
269+
if (transaction_cb != 1)
270+
return false;
271+
272+
sn_coap_protocol_stub.expectedInt16 = -2;
273+
nsdynmemlib_stub.returnCounter = 3;
274+
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb))
275+
return false;
276+
277+
transaction_cb = 0;
278+
if( 0 != coap_message_handler_exec(handle, 2)) {
279+
return false;
280+
}
281+
if (transaction_cb != 1)
282+
return false;
283+
284+
255285
free(sn_coap_protocol_stub.expectedCoap);
256286
sn_coap_protocol_stub.expectedCoap = NULL;
257287
coap_message_handler_destroy(handle);

0 commit comments

Comments
 (0)