Skip to content

Commit 6a6dc45

Browse files
author
Arto Kinnunen
committed
Squashed 'features/nanostack/coap-service/' changes from cbe656a..bc331ca
bc331ca Delete transaction when handling response (#110) 0292600 Use callback function when deleting request (#109) 1edc4a8 New API to clear requests by service ID (#108) 61ecb6b Session cleanup timer fixes (#105) git-subtree-dir: features/nanostack/coap-service git-subtree-split: bc331ca
1 parent bee5d60 commit 6a6dc45

12 files changed

+127
-8
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_connection_handler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ void coap_connection_handler_exec(uint32_t time)
10011001
{
10021002
if(ns_list_count(&secure_session_list)){
10031003
// Seek & destroy old sessions where close notify have been sent
1004-
ns_list_foreach(secure_session_t, cur_ptr, &secure_session_list) {
1004+
ns_list_foreach_safe(secure_session_t, cur_ptr, &secure_session_list) {
10051005
if(cur_ptr->session_state == SECURE_SESSION_CLOSED) {
10061006
if((cur_ptr->last_contact_time + CLOSED_SECURE_SESSION_TIMEOUT) <= time){
10071007
secure_session_delete(cur_ptr);

source/coap_message_handler.c

Lines changed: 47 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;
@@ -322,6 +349,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
322349
goto exit;
323350
/* Response received */
324351
} else {
352+
transaction_delete(transaction_ptr); // transaction_ptr not needed in response
325353
if (coap_message->token_ptr) {
326354
this = transaction_find_client_by_token(coap_message->token_ptr, coap_message->token_len, source_addr_ptr, port);
327355
}
@@ -551,17 +579,36 @@ int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t se
551579
tr_error("invalid params");
552580
return -1;
553581
}
582+
554583
sn_coap_protocol_delete_retransmission(handle->coap, msg_id);
555584

556585
transaction_ptr = transaction_find_client(msg_id);
557586
if (!transaction_ptr) {
558587
tr_error("response transaction not found");
559588
return -2;
560589
}
590+
591+
if (transaction_ptr->resp_cb) {
592+
transaction_ptr->resp_cb(transaction_ptr->service_id, transaction_ptr->remote_address, transaction_ptr->remote_port, NULL);
593+
}
561594
transaction_delete(transaction_ptr);
562595
return 0;
563596
}
564597

598+
int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id)
599+
{
600+
tr_debug("Service %d, delete all CoAP requests", service_id);
601+
602+
if (!handle) {
603+
tr_error("invalid params");
604+
return -1;
605+
}
606+
607+
transactions_delete_all_by_service_id(service_id);
608+
609+
return 0;
610+
}
611+
565612
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time){
566613

567614
if( !handle ){

source/coap_service_api.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ static void service_event_handler(arm_event_s *event)
201201
tr_debug("service tasklet initialised");
202202
/*initialize coap service and listen socket*/
203203
}
204+
204205
if (event->event_type == ARM_LIB_SYSTEM_TIMER_EVENT && event->event_id == COAP_TICK_TIMER) {
205206
coap_message_handler_exec(coap_service_handle, coap_ticks++);
206-
if(coap_ticks && !coap_ticks % SECURE_SESSION_CLEAN_INTERVAL){
207+
if(coap_ticks && !(coap_ticks % SECURE_SESSION_CLEAN_INTERVAL)){
207208
coap_connection_handler_exec(coap_ticks);
208209
}
209210
}
@@ -541,6 +542,11 @@ int8_t coap_service_request_delete(int8_t service_id, uint16_t msg_id)
541542
return coap_message_handler_request_delete(coap_service_handle, service_id, msg_id);
542543
}
543544

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+
544550
int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max)
545551
{
546552
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void transactions_delete_all(uint8_t *address_ptr, uint16_t port)
3939
{
4040

4141
}
42+
4243
int8_t coap_message_handler_destroy(coap_msg_handler_t *handle)
4344
{
4445
return coap_message_handler_stub.int8_value;
@@ -78,6 +79,11 @@ int8_t coap_message_handler_request_delete(coap_msg_handler_t *handle, int8_t se
7879
return 0;
7980
}
8081

82+
int8_t coap_message_handler_request_delete_by_service_id(coap_msg_handler_t *handle, int8_t service_id)
83+
{
84+
return 0;
85+
}
86+
8187
int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time)
8288
{
8389
return coap_message_handler_stub.int8_value;

0 commit comments

Comments
 (0)