Squashed 'features/nanostack/coap-service/' changes from cbe656a..bc331ca

bc331ca Delete transaction when handling response (#110)
0292600 Use callback function when deleting request (#109)
1edc4a8 New API to clear requests by service ID (#108)
61ecb6b Session cleanup timer fixes (#105)

git-subtree-dir: features/nanostack/coap-service
git-subtree-split: bc331cabb13e9b0e64a5ed74d712a3978aa270d6
pull/8647/head
Arto Kinnunen 2018-11-05 14:22:43 +02:00
parent bee5d601f8
commit 6a6dc452aa
12 changed files with 127 additions and 8 deletions

View File

@ -282,8 +282,6 @@ extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_
*/
extern int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t options, uint16_t msg_id, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len);
/**
* \brief Delete CoAP request transaction
*
@ -297,6 +295,15 @@ extern int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t op
*/
extern int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id);
/**
* \brief Delete CoAP requests from service id
*
* Removes pending CoAP requests from service specified by service_id.
*
* \param service_id Id number of the current service.
*/
extern void coap_service_request_delete_by_service_id(int8_t service_id);
/**
* \brief Set DTLS handshake timeout values
*

View File

@ -1001,7 +1001,7 @@ void coap_connection_handler_exec(uint32_t time)
{
if(ns_list_count(&secure_session_list)){
// Seek & destroy old sessions where close notify have been sent
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
ns_list_foreach_safe(secure_session_t, cur_ptr, &secure_session_list) {
if(cur_ptr->session_state == SECURE_SESSION_CLOSED) {
if((cur_ptr->last_contact_time + CLOSED_SECURE_SESSION_TIMEOUT) <= time){
secure_session_delete(cur_ptr);

View File

@ -98,6 +98,18 @@ static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uin
return this;
}
static coap_transaction_t *transaction_find_by_service_id(int8_t service_id)
{
coap_transaction_t *this = NULL;
ns_list_foreach(coap_transaction_t, cur_ptr, &request_list) {
if (cur_ptr->service_id == service_id) {
this = cur_ptr;
break;
}
}
return this;
}
/* retransmission valid time is calculated to be max. time that CoAP message sending can take: */
/* Number of retransmisisons, each retransmission is 2 * previous retransmisison time */
/* + random factor (max. 1.5) */
@ -160,6 +172,21 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
}
}
static void transactions_delete_all_by_service_id(int8_t service_id)
{
coap_transaction_t *transaction = transaction_find_by_service_id(service_id);
while (transaction) {
ns_list_remove(&request_list, transaction);
if (transaction->resp_cb) {
transaction->resp_cb(transaction->service_id, transaction->remote_address, transaction->remote_port, NULL);
}
sn_coap_protocol_delete_retransmission(coap_service_handle->coap, transaction->msg_id);
transaction_free(transaction);
transaction = transaction_find_by_service_id(service_id);
}
}
static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ptr, void *param)
{
coap_transaction_t *this = NULL;
@ -322,6 +349,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
goto exit;
/* Response received */
} else {
transaction_delete(transaction_ptr); // transaction_ptr not needed in response
if (coap_message->token_ptr) {
this = transaction_find_client_by_token(coap_message->token_ptr, coap_message->token_len, source_addr_ptr, port);
}
@ -551,6 +579,7 @@ int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t se
tr_error("invalid params");
return -1;
}
sn_coap_protocol_delete_retransmission(handle->coap, msg_id);
transaction_ptr = transaction_find_client(msg_id);
@ -558,10 +587,28 @@ int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t se
tr_error("response transaction not found");
return -2;
}
if (transaction_ptr->resp_cb) {
transaction_ptr->resp_cb(transaction_ptr->service_id, transaction_ptr->remote_address, transaction_ptr->remote_port, NULL);
}
transaction_delete(transaction_ptr);
return 0;
}
int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id)
{
tr_debug("Service %d, delete all CoAP requests", service_id);
if (!handle) {
tr_error("invalid params");
return -1;
}
transactions_delete_all_by_service_id(service_id);
return 0;
}
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time){
if( !handle ){

View File

@ -201,9 +201,10 @@ static void service_event_handler(arm_event_s *event)
tr_debug("service tasklet initialised");
/*initialize coap service and listen socket*/
}
if (event->event_type == ARM_LIB_SYSTEM_TIMER_EVENT && event->event_id == COAP_TICK_TIMER) {
coap_message_handler_exec(coap_service_handle, coap_ticks++);
if(coap_ticks && !coap_ticks % SECURE_SESSION_CLEAN_INTERVAL){
if(coap_ticks && !(coap_ticks % SECURE_SESSION_CLEAN_INTERVAL)){
coap_connection_handler_exec(coap_ticks);
}
}
@ -541,6 +542,11 @@ int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id)
return coap_message_handler_request_delete(coap_service_handle, service_id, msg_id);
}
void coap_service_request_delete_by_service_id(int8_t service_id)
{
coap_message_handler_request_delete_by_service_id(coap_service_handle, service_id);
}
int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max)
{
coap_service_t *this = service_find(service_id);

View File

@ -96,6 +96,8 @@ extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int
extern int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id);
extern int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id);
extern int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time);
extern void transaction_delete(coap_transaction_t *this);

View File

@ -63,6 +63,11 @@ TEST(coap_message_handler, test_coap_message_handler_request_delete)
CHECK(test_coap_message_handler_request_delete());
}
TEST(coap_message_handler, test_coap_message_handler_request_delete_by_service_id)
{
CHECK(test_coap_message_handler_request_delete_by_service_id());
}
TEST(coap_message_handler, test_coap_message_handler_exec)
{
CHECK(test_coap_message_handler_exec());

View File

@ -233,10 +233,8 @@ bool test_coap_message_handler_request_send()
uint8_t buf[16];
memset(&buf, 1, 16);
char uri[3];
uri[0] = "r";
uri[1] = "s";
uri[2] = "\0";
char uri[3] = "rs";
if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL))
return false;
@ -330,6 +328,40 @@ bool test_coap_message_handler_request_delete()
return true;
}
bool test_coap_message_handler_request_delete_by_service_id()
{
retCounter = 1;
sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s));
memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
nsdynmemlib_stub.returnCounter = 1;
coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
coap_service_handle = handle;
uint8_t buf[16];
memset(&buf, 1, 16);
char uri[3] = "rs";
if( 0 == coap_message_handler_request_delete_by_service_id(NULL, 1))
return false;
if( 0 != coap_message_handler_request_delete_by_service_id(handle, 1))
return false;
sn_coap_builder_stub.expectedUint16 = 1;
nsdynmemlib_stub.returnCounter = 3;
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
return false;
if( 0 != coap_message_handler_request_delete_by_service_id(handle, 3))
return false;
free(sn_coap_protocol_stub.expectedCoap);
sn_coap_protocol_stub.expectedCoap = NULL;
coap_message_handler_destroy(handle);
coap_service_handle = NULL;
return true;
}
bool test_coap_message_handler_response_send()
{
if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0))

View File

@ -30,6 +30,7 @@ bool test_coap_message_handler_coap_msg_process();
bool test_coap_message_handler_request_send();
bool test_coap_message_handler_response_send();
bool test_coap_message_handler_request_delete();
bool test_coap_message_handler_request_delete_by_service_id();
bool test_coap_message_handler_exec();
#ifdef __cplusplus

View File

@ -68,6 +68,11 @@ TEST(coap_service_api, test_coap_service_request_delete)
CHECK(test_coap_service_request_delete());
}
TEST(coap_service_api, test_coap_service_request_delete_by_service_id)
{
CHECK(test_coap_service_request_delete_by_service_id());
}
TEST(coap_service_api, test_coap_service_response_send)
{
CHECK(test_coap_service_response_send());

View File

@ -236,6 +236,12 @@ bool test_coap_service_request_delete()
return true;
}
bool test_coap_service_request_delete_by_service_id()
{
coap_service_request_delete_by_service_id(0);
return true;
}
bool test_coap_service_response_send()
{
uint8_t buf[16];

View File

@ -39,6 +39,8 @@ bool test_coap_service_request_send();
bool test_coap_service_request_delete();
bool test_coap_service_request_delete_by_service_id();
bool test_coap_service_response_send();
bool test_coap_callbacks();

View File

@ -39,6 +39,7 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
{
}
int8_t coap_message_handler_destroy(coap_msg_handler_t *handle)
{
return coap_message_handler_stub.int8_value;
@ -78,6 +79,11 @@ int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t se
return 0;
}
int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id)
{
return 0;
}
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time)
{
return coap_message_handler_stub.int8_value;