Skip to content

Commit

Permalink
New API to clear requests by service ID (ARMmbed#108)
Browse files Browse the repository at this point in the history
Add new API to delete requests from specific service ID.
  • Loading branch information
Arto Kinnunen committed Oct 23, 2018
1 parent 61ecb6b commit 1edc4a8
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 6 deletions.
11 changes: 9 additions & 2 deletions coap-service/coap_service_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
*
Expand Down
42 changes: 42 additions & 0 deletions source/coap_message_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 ){
Expand Down
5 changes: 5 additions & 0 deletions source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions source/include/coap_message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions test/coap-service/unittest/stub/coap_message_handler_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1edc4a8

Please sign in to comment.