From 1edc4a8dbbe5cdb3b7ae80e0e70ca6f0ad93c17c Mon Sep 17 00:00:00 2001 From: Arto Kinnunen Date: Tue, 23 Oct 2018 10:01:45 +0300 Subject: [PATCH] New API to clear requests by service ID (#108) Add new API to delete requests from specific service ID. --- coap-service/coap_service_api.h | 11 ++++- source/coap_message_handler.c | 42 +++++++++++++++++++ source/coap_service_api.c | 5 +++ source/include/coap_message_handler.h | 2 + .../coap_message_handlertest.cpp | 5 +++ .../test_coap_message_handler.c | 40 ++++++++++++++++-- .../test_coap_message_handler.h | 1 + .../coap_service_api/coap_service_apitest.cpp | 5 +++ .../coap_service_api/test_coap_service_api.c | 6 +++ .../coap_service_api/test_coap_service_api.h | 2 + .../unittest/stub/coap_message_handler_stub.c | 10 +++++ 11 files changed, 123 insertions(+), 6 deletions(-) diff --git a/coap-service/coap_service_api.h b/coap-service/coap_service_api.h index 623c381744a..4834acfb971 100644 --- a/coap-service/coap_service_api.h +++ b/coap-service/coap_service_api.h @@ -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 * diff --git a/source/coap_message_handler.c b/source/coap_message_handler.c index db2cc14ef66..16f8cddfe03 100644 --- a/source/coap_message_handler.c +++ b/source/coap_message_handler.c @@ -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; @@ -551,6 +578,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); @@ -562,6 +590,20 @@ 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) +{ + 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 ){ diff --git a/source/coap_service_api.c b/source/coap_service_api.c index 290b0edea11..f5c4cc3913f 100644 --- a/source/coap_service_api.c +++ b/source/coap_service_api.c @@ -542,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); diff --git a/source/include/coap_message_handler.h b/source/include/coap_message_handler.h index 9b54619ca79..377ca400c20 100644 --- a/source/include/coap_message_handler.h +++ b/source/include/coap_message_handler.h @@ -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); diff --git a/test/coap-service/unittest/coap_message_handler/coap_message_handlertest.cpp b/test/coap-service/unittest/coap_message_handler/coap_message_handlertest.cpp index e418ab375b3..1aed34f600b 100644 --- a/test/coap-service/unittest/coap_message_handler/coap_message_handlertest.cpp +++ b/test/coap-service/unittest/coap_message_handler/coap_message_handlertest.cpp @@ -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()); diff --git a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c index c00067fa49d..c4953c7e7a4 100644 --- a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c +++ b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c @@ -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)) diff --git a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.h b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.h index fc3f2ae6bc9..18f5c0fca79 100644 --- a/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.h +++ b/test/coap-service/unittest/coap_message_handler/test_coap_message_handler.h @@ -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 diff --git a/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp b/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp index 377f486a61f..818680d0f23 100644 --- a/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp +++ b/test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp @@ -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()); diff --git a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c index 00714a65b16..9d558ace7be 100644 --- a/test/coap-service/unittest/coap_service_api/test_coap_service_api.c +++ b/test/coap-service/unittest/coap_service_api/test_coap_service_api.c @@ -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]; diff --git a/test/coap-service/unittest/coap_service_api/test_coap_service_api.h b/test/coap-service/unittest/coap_service_api/test_coap_service_api.h index 9f83ca06a8e..ac148290ec9 100644 --- a/test/coap-service/unittest/coap_service_api/test_coap_service_api.h +++ b/test/coap-service/unittest/coap_service_api/test_coap_service_api.h @@ -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(); diff --git a/test/coap-service/unittest/stub/coap_message_handler_stub.c b/test/coap-service/unittest/stub/coap_message_handler_stub.c index 2116dd34709..db1471b5fe8 100644 --- a/test/coap-service/unittest/stub/coap_message_handler_stub.c +++ b/test/coap-service/unittest/stub/coap_message_handler_stub.c @@ -39,6 +39,11 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port) { } + +void transactions_delete_all_by_service_id(int8_t service_id) +{ +} + int8_t coap_message_handler_destroy(coap_msg_handler_t *handle) { return coap_message_handler_stub.int8_value; @@ -78,6 +83,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;