Skip to content

Commit

Permalink
Add transaction delete to CoAP service (ARMmbed#65)
Browse files Browse the repository at this point in the history
Allow deletion of ongoing transaction.
  • Loading branch information
Mika Tervonen authored Mar 21, 2017
1 parent feea33e commit dce323c
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 1 deletion.
24 changes: 24 additions & 0 deletions coap-service/coap_service_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,30 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co
*/
extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, 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
*
* Removes pending CoAP transaction from service.
*
* \param service_id Id number of the current service.
* \param msg_id Message ID number.
*
* \return -1 For failure
*- 0 For success
*/
extern int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id);
/**
* \brief Set DTLS handshake timeout values
*
* Configures the DTLS handshake timeout values.
*
* \param service_id Id number of the current service.
* \param min Initial timeout value.
* \param max Maximum value of timeout.
*
* \return -1 For failure
*- 0 For success
*/
extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max);
#ifdef __cplusplus
}
Expand Down
1 change: 0 additions & 1 deletion source/coap_connection_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ static secure_session_t *secure_session_find_by_timer_id(int8_t timer_id)

static bool is_secure_session_valid(secure_session_t *session)
{
secure_session_t *this = NULL;
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
if (cur_ptr == session) {
return true;
Expand Down
34 changes: 34 additions & 0 deletions source/coap_message_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ static coap_transaction_t *transaction_find_server(uint16_t msg_id)
return this;
}

static coap_transaction_t *transaction_find_client(uint16_t msg_id)
{
coap_transaction_t *this = NULL;
ns_list_foreach(coap_transaction_t, cur_ptr, &request_list) {
if (cur_ptr->msg_id == msg_id && cur_ptr->client_request) {
this = cur_ptr;
break;
}
}
return this;
}

static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uint16_t port)
{
coap_transaction_t *this = NULL;
Expand Down Expand Up @@ -391,6 +403,28 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
return 0;
}

int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id)
{
coap_transaction_t *transaction_ptr;
(void)service_id;


tr_debug("Service %d, delete CoAP request %d", service_id, msg_id);
if (!handle) {
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;
}
transaction_delete(transaction_ptr);
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 @@ -465,6 +465,11 @@ int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hd
return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len);
}

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);
}

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 @@ -83,6 +83,8 @@ extern uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, in
extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code,
sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len);

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_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 @@ -45,8 +45,14 @@ TEST(coap_message_handler, test_coap_message_handler_response_send)
CHECK(test_coap_message_handler_response_send());
}

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_exec)
{
CHECK(test_coap_message_handler_exec());
}


Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,39 @@ bool test_coap_message_handler_request_send()
return true;
}

bool test_coap_message_handler_request_delete()
{
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));
coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function);

uint8_t buf[16];
memset(&buf, 1, 16);
char uri[3];
uri[0] = "r";
uri[1] = "s";
uri[2] = "\0";
if( 0 == coap_message_handler_request_delete(NULL, 1, 1))
return false;

if( 0 == coap_message_handler_request_delete(handle, 1, 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(handle, 1, 2))
return false;

free(sn_coap_protocol_stub.expectedCoap);
sn_coap_protocol_stub.expectedCoap = NULL;
coap_message_handler_destroy(handle);
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_find_transaction();
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_exec();

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ TEST(coap_service_api, test_coap_service_request_send)
CHECK(test_coap_service_request_send());
}

TEST(coap_service_api, test_coap_service_request_delete)
{
CHECK(test_coap_service_request_delete());
}

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 @@ -215,6 +215,13 @@ bool test_coap_service_request_send()
return true;
}

bool test_coap_service_request_delete()
{
if( 0 != coap_service_request_delete(NULL,0))
return false;
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 @@ -38,6 +38,8 @@ bool test_coap_service_unregister_uri();

bool test_coap_service_request_send();

bool test_coap_service_request_delete();

bool test_coap_service_response_send();

bool test_coap_callbacks();
Expand Down
5 changes: 5 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 @@ -60,6 +60,11 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
return coap_message_handler_stub.int8_value;
}

int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_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 dce323c

Please sign in to comment.