Skip to content

Commit

Permalink
Squashed 'features/nanostack/coap-service/' changes from cbe656a..bc3…
Browse files Browse the repository at this point in the history
…31ca

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

git-subtree-dir: features/nanostack/coap-service
git-subtree-split: bc331ca
  • Loading branch information
Arto Kinnunen committed Nov 5, 2018
1 parent bee5d60 commit 6a6dc45
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 8 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
2 changes: 1 addition & 1 deletion source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 47 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 @@ -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);
}
Expand Down Expand Up @@ -551,17 +579,36 @@ 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);
if (!transaction_ptr) {
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 ){
Expand Down
8 changes: 7 additions & 1 deletion source/coap_service_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
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
6 changes: 6 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,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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6a6dc45

Please sign in to comment.