Skip to content

Commit dce323c

Browse files
author
Mika Tervonen
authored
Add transaction delete to CoAP service (ARMmbed#65)
Allow deletion of ongoing transaction.
1 parent feea33e commit dce323c

12 files changed

+124
-1
lines changed

coap-service/coap_service_api.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,30 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co
264264
*/
265265
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);
266266

267+
/**
268+
* \brief Delete CoAP request transaction
269+
*
270+
* Removes pending CoAP transaction from service.
271+
*
272+
* \param service_id Id number of the current service.
273+
* \param msg_id Message ID number.
274+
*
275+
* \return -1 For failure
276+
*- 0 For success
277+
*/
278+
extern int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id);
279+
/**
280+
* \brief Set DTLS handshake timeout values
281+
*
282+
* Configures the DTLS handshake timeout values.
283+
*
284+
* \param service_id Id number of the current service.
285+
* \param min Initial timeout value.
286+
* \param max Maximum value of timeout.
287+
*
288+
* \return -1 For failure
289+
*- 0 For success
290+
*/
267291
extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max);
268292
#ifdef __cplusplus
269293
}

source/coap_connection_handler.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ static secure_session_t *secure_session_find_by_timer_id(int8_t timer_id)
9999

100100
static bool is_secure_session_valid(secure_session_t *session)
101101
{
102-
secure_session_t *this = NULL;
103102
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
104103
if (cur_ptr == session) {
105104
return true;

source/coap_message_handler.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ static coap_transaction_t *transaction_find_server(uint16_t msg_id)
5757
return this;
5858
}
5959

60+
static coap_transaction_t *transaction_find_client(uint16_t msg_id)
61+
{
62+
coap_transaction_t *this = NULL;
63+
ns_list_foreach(coap_transaction_t, cur_ptr, &request_list) {
64+
if (cur_ptr->msg_id == msg_id && cur_ptr->client_request) {
65+
this = cur_ptr;
66+
break;
67+
}
68+
}
69+
return this;
70+
}
71+
6072
static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uint16_t port)
6173
{
6274
coap_transaction_t *this = NULL;
@@ -391,6 +403,28 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
391403
return 0;
392404
}
393405

406+
int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id)
407+
{
408+
coap_transaction_t *transaction_ptr;
409+
(void)service_id;
410+
411+
412+
tr_debug("Service %d, delete CoAP request %d", service_id, msg_id);
413+
if (!handle) {
414+
tr_error("invalid params");
415+
return -1;
416+
}
417+
sn_coap_protocol_delete_retransmission(handle->coap, msg_id);
418+
419+
transaction_ptr = transaction_find_client(msg_id);
420+
if (!transaction_ptr) {
421+
tr_error("response transaction not found");
422+
return -2;
423+
}
424+
transaction_delete(transaction_ptr);
425+
return 0;
426+
}
427+
394428
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time){
395429

396430
if( !handle ){

source/coap_service_api.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,11 @@ int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hd
465465
return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len);
466466
}
467467

468+
int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id)
469+
{
470+
return coap_message_handler_request_delete(coap_service_handle, service_id, msg_id);
471+
}
472+
468473
int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max)
469474
{
470475
coap_service_t *this = service_find(service_id);

source/include/coap_message_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ extern uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, in
8383
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,
8484
sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len);
8585

86+
extern int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id);
87+
8688
extern int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time);
8789

8890
extern void transaction_delete(coap_transaction_t *this);

test/coap-service/unittest/coap_message_handler/coap_message_handlertest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,14 @@ TEST(coap_message_handler, test_coap_message_handler_response_send)
4545
CHECK(test_coap_message_handler_response_send());
4646
}
4747

48+
TEST(coap_message_handler, test_coap_message_handler_request_delete)
49+
{
50+
CHECK(test_coap_message_handler_request_delete());
51+
}
52+
4853
TEST(coap_message_handler, test_coap_message_handler_exec)
4954
{
5055
CHECK(test_coap_message_handler_exec());
5156
}
5257

58+

test/coap-service/unittest/coap_message_handler/test_coap_message_handler.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,39 @@ bool test_coap_message_handler_request_send()
229229
return true;
230230
}
231231

232+
bool test_coap_message_handler_request_delete()
233+
{
234+
retCounter = 1;
235+
sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s));
236+
memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
237+
coap_msg_handler_t *handle = coap_message_handler_init(&own_alloc, &own_free, &coap_tx_function);
238+
239+
uint8_t buf[16];
240+
memset(&buf, 1, 16);
241+
char uri[3];
242+
uri[0] = "r";
243+
uri[1] = "s";
244+
uri[2] = "\0";
245+
if( 0 == coap_message_handler_request_delete(NULL, 1, 1))
246+
return false;
247+
248+
if( 0 == coap_message_handler_request_delete(handle, 1, 1))
249+
return false;
250+
251+
sn_coap_builder_stub.expectedUint16 = 1;
252+
nsdynmemlib_stub.returnCounter = 3;
253+
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
254+
return false;
255+
256+
if( 0 != coap_message_handler_request_delete(handle, 1, 2))
257+
return false;
258+
259+
free(sn_coap_protocol_stub.expectedCoap);
260+
sn_coap_protocol_stub.expectedCoap = NULL;
261+
coap_message_handler_destroy(handle);
262+
return true;
263+
}
264+
232265
bool test_coap_message_handler_response_send()
233266
{
234267
if( -1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1,3,NULL, 0))

test/coap-service/unittest/coap_message_handler/test_coap_message_handler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bool test_coap_message_handler_find_transaction();
3030
bool test_coap_message_handler_coap_msg_process();
3131
bool test_coap_message_handler_request_send();
3232
bool test_coap_message_handler_response_send();
33+
bool test_coap_message_handler_request_delete();
3334
bool test_coap_message_handler_exec();
3435

3536
#ifdef __cplusplus

test/coap-service/unittest/coap_service_api/coap_service_apitest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ TEST(coap_service_api, test_coap_service_request_send)
5050
CHECK(test_coap_service_request_send());
5151
}
5252

53+
TEST(coap_service_api, test_coap_service_request_delete)
54+
{
55+
CHECK(test_coap_service_request_delete());
56+
}
57+
5358
TEST(coap_service_api, test_coap_service_response_send)
5459
{
5560
CHECK(test_coap_service_response_send());

test/coap-service/unittest/coap_service_api/test_coap_service_api.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ bool test_coap_service_request_send()
215215
return true;
216216
}
217217

218+
bool test_coap_service_request_delete()
219+
{
220+
if( 0 != coap_service_request_delete(NULL,0))
221+
return false;
222+
return true;
223+
}
224+
218225
bool test_coap_service_response_send()
219226
{
220227
uint8_t buf[16];

test/coap-service/unittest/coap_service_api/test_coap_service_api.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ bool test_coap_service_unregister_uri();
3838

3939
bool test_coap_service_request_send();
4040

41+
bool test_coap_service_request_delete();
42+
4143
bool test_coap_service_response_send();
4244

4345
bool test_coap_callbacks();

test/coap-service/unittest/stub/coap_message_handler_stub.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
6060
return coap_message_handler_stub.int8_value;
6161
}
6262

63+
int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id)
64+
{
65+
return 0;
66+
}
67+
6368
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time)
6469
{
6570
return coap_message_handler_stub.int8_value;

0 commit comments

Comments
 (0)