Skip to content

Commit 1edc4a8

Browse files
author
Arto Kinnunen
authored
New API to clear requests by service ID (ARMmbed#108)
Add new API to delete requests from specific service ID.
1 parent 61ecb6b commit 1edc4a8

File tree

11 files changed

+123
-6
lines changed

11 files changed

+123
-6
lines changed

coap-service/coap_service_api.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_
282282
*/
283283
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);
284284

285-
286-
287285
/**
288286
* \brief Delete CoAP request transaction
289287
*
@@ -297,6 +295,15 @@ extern int8_t coap_service_response_send_by_msg_id(int8_t service_id, uint8_t op
297295
*/
298296
extern int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id);
299297

298+
/**
299+
* \brief Delete CoAP requests from service id
300+
*
301+
* Removes pending CoAP requests from service specified by service_id.
302+
*
303+
* \param service_id Id number of the current service.
304+
*/
305+
extern void coap_service_request_delete_by_service_id(int8_t service_id);
306+
300307
/**
301308
* \brief Set DTLS handshake timeout values
302309
*

source/coap_message_handler.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ static coap_transaction_t *transaction_find_by_address(uint8_t *address_ptr, uin
9898
return this;
9999
}
100100

101+
static coap_transaction_t *transaction_find_by_service_id(int8_t service_id)
102+
{
103+
coap_transaction_t *this = NULL;
104+
ns_list_foreach(coap_transaction_t, cur_ptr, &request_list) {
105+
if (cur_ptr->service_id == service_id) {
106+
this = cur_ptr;
107+
break;
108+
}
109+
}
110+
return this;
111+
}
112+
101113
/* retransmission valid time is calculated to be max. time that CoAP message sending can take: */
102114
/* Number of retransmisisons, each retransmission is 2 * previous retransmisison time */
103115
/* + random factor (max. 1.5) */
@@ -160,6 +172,21 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
160172
}
161173
}
162174

175+
static void transactions_delete_all_by_service_id(int8_t service_id)
176+
{
177+
coap_transaction_t *transaction = transaction_find_by_service_id(service_id);
178+
179+
while (transaction) {
180+
ns_list_remove(&request_list, transaction);
181+
if (transaction->resp_cb) {
182+
transaction->resp_cb(transaction->service_id, transaction->remote_address, transaction->remote_port, NULL);
183+
}
184+
sn_coap_protocol_delete_retransmission(coap_service_handle->coap, transaction->msg_id);
185+
transaction_free(transaction);
186+
transaction = transaction_find_by_service_id(service_id);
187+
}
188+
}
189+
163190
static int8_t coap_rx_function(sn_coap_hdr_s *resp_ptr, sn_nsdl_addr_s *address_ptr, void *param)
164191
{
165192
coap_transaction_t *this = NULL;
@@ -551,6 +578,7 @@ int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t se
551578
tr_error("invalid params");
552579
return -1;
553580
}
581+
554582
sn_coap_protocol_delete_retransmission(handle->coap, msg_id);
555583

556584
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
562590
return 0;
563591
}
564592

593+
int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id)
594+
{
595+
tr_debug("Service %d, delete all CoAP requests", service_id);
596+
597+
if (!handle) {
598+
tr_error("invalid params");
599+
return -1;
600+
}
601+
602+
transactions_delete_all_by_service_id(service_id);
603+
604+
return 0;
605+
}
606+
565607
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time){
566608

567609
if( !handle ){

source/coap_service_api.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,11 @@ int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id)
542542
return coap_message_handler_request_delete(coap_service_handle, service_id, msg_id);
543543
}
544544

545+
void coap_service_request_delete_by_service_id(int8_t service_id)
546+
{
547+
coap_message_handler_request_delete_by_service_id(coap_service_handle, service_id);
548+
}
549+
545550
int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max)
546551
{
547552
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
@@ -96,6 +96,8 @@ extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int
9696

9797
extern int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t service_id, uint16_t msg_id);
9898

99+
extern int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id);
100+
99101
extern int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time);
100102

101103
extern void transaction_delete(coap_transaction_t *this);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ TEST(coap_message_handler, test_coap_message_handler_request_delete)
6363
CHECK(test_coap_message_handler_request_delete());
6464
}
6565

66+
TEST(coap_message_handler, test_coap_message_handler_request_delete_by_service_id)
67+
{
68+
CHECK(test_coap_message_handler_request_delete_by_service_id());
69+
}
70+
6671
TEST(coap_message_handler, test_coap_message_handler_exec)
6772
{
6873
CHECK(test_coap_message_handler_exec());

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,8 @@ bool test_coap_message_handler_request_send()
233233

234234
uint8_t buf[16];
235235
memset(&buf, 1, 16);
236-
char uri[3];
237-
uri[0] = "r";
238-
uri[1] = "s";
239-
uri[2] = "\0";
236+
char uri[3] = "rs";
237+
240238
if( 0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL))
241239
return false;
242240

@@ -330,6 +328,40 @@ bool test_coap_message_handler_request_delete()
330328
return true;
331329
}
332330

331+
bool test_coap_message_handler_request_delete_by_service_id()
332+
{
333+
retCounter = 1;
334+
sn_coap_protocol_stub.expectedCoap = (struct coap_s*)malloc(sizeof(struct coap_s));
335+
memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
336+
nsdynmemlib_stub.returnCounter = 1;
337+
coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
338+
coap_service_handle = handle;
339+
340+
uint8_t buf[16];
341+
memset(&buf, 1, 16);
342+
char uri[3] = "rs";
343+
344+
if( 0 == coap_message_handler_request_delete_by_service_id(NULL, 1))
345+
return false;
346+
347+
if( 0 != coap_message_handler_request_delete_by_service_id(handle, 1))
348+
return false;
349+
350+
sn_coap_builder_stub.expectedUint16 = 1;
351+
nsdynmemlib_stub.returnCounter = 3;
352+
if( 2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv))
353+
return false;
354+
355+
if( 0 != coap_message_handler_request_delete_by_service_id(handle, 3))
356+
return false;
357+
358+
free(sn_coap_protocol_stub.expectedCoap);
359+
sn_coap_protocol_stub.expectedCoap = NULL;
360+
coap_message_handler_destroy(handle);
361+
coap_service_handle = NULL;
362+
return true;
363+
}
364+
333365
bool test_coap_message_handler_response_send()
334366
{
335367
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_coap_msg_process();
3030
bool test_coap_message_handler_request_send();
3131
bool test_coap_message_handler_response_send();
3232
bool test_coap_message_handler_request_delete();
33+
bool test_coap_message_handler_request_delete_by_service_id();
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
@@ -68,6 +68,11 @@ TEST(coap_service_api, test_coap_service_request_delete)
6868
CHECK(test_coap_service_request_delete());
6969
}
7070

71+
TEST(coap_service_api, test_coap_service_request_delete_by_service_id)
72+
{
73+
CHECK(test_coap_service_request_delete_by_service_id());
74+
}
75+
7176
TEST(coap_service_api, test_coap_service_response_send)
7277
{
7378
CHECK(test_coap_service_response_send());

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ bool test_coap_service_request_delete()
236236
return true;
237237
}
238238

239+
bool test_coap_service_request_delete_by_service_id()
240+
{
241+
coap_service_request_delete_by_service_id(0);
242+
return true;
243+
}
244+
239245
bool test_coap_service_response_send()
240246
{
241247
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
@@ -39,6 +39,8 @@ bool test_coap_service_request_send();
3939

4040
bool test_coap_service_request_delete();
4141

42+
bool test_coap_service_request_delete_by_service_id();
43+
4244
bool test_coap_service_response_send();
4345

4446
bool test_coap_callbacks();

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
3939
{
4040

4141
}
42+
43+
void transactions_delete_all_by_service_id(int8_t service_id)
44+
{
45+
}
46+
4247
int8_t coap_message_handler_destroy(coap_msg_handler_t *handle)
4348
{
4449
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
7883
return 0;
7984
}
8085

86+
int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id)
87+
{
88+
return 0;
89+
}
90+
8191
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time)
8292
{
8393
return coap_message_handler_stub.int8_value;

0 commit comments

Comments
 (0)