Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 3b172c6

Browse files
author
Antti Yli-Tokola
committed
Notify client when observation is cancelled with RESET message
1 parent 0e3a26f commit 3b172c6

File tree

12 files changed

+301
-27
lines changed

12 files changed

+301
-27
lines changed

nsdl-c/sn_nsdl_lib.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,50 @@ extern void sn_nsdl_nsp_lost(struct nsdl_s *handle);
341341
* \param *observe_ptr Pointer to observe number to be sent
342342
* \param observe_len Observe number len
343343
* \param message_type Observation message type (confirmable or non-confirmable)
344-
* \param contetnt_type Observation message payload contetnt type
344+
* \param content_type Observation message payload contetnt type
345345
*
346346
* \return !0 Success, observation messages message ID
347347
* \return 0 Failure
348348
*/
349349
extern uint16_t sn_nsdl_send_observation_notification(struct nsdl_s *handle, uint8_t *token_ptr, uint8_t token_len,
350350
uint8_t *payload_ptr, uint16_t payload_len,
351351
uint8_t *observe_ptr, uint8_t observe_len,
352-
sn_coap_msg_type_e message_type, uint8_t content_type);
352+
sn_coap_msg_type_e message_type,
353+
uint8_t content_type);
354+
355+
/**
356+
* \fn extern uint16_t sn_nsdl_send_observation_notification_with_uri_path(struct nsdl_s *handle, uint8_t *token_ptr, uint8_t token_len,
357+
* uint8_t *payload_ptr, uint16_t payload_len,
358+
* uint8_t *observe_ptr, uint8_t observe_len,
359+
* sn_coap_msg_type_e message_type, uint8_t content_type,
360+
* uint8_t *uri_path_ptr,
361+
* uint16_t uri_path_len)
362+
*
363+
*
364+
* \brief Sends observation message to mbed Device Server with uri path
365+
*
366+
* \param *handle Pointer to nsdl-library handle
367+
* \param *token_ptr Pointer to token to be used
368+
* \param token_len Token length
369+
* \param *payload_ptr Pointer to payload to be sent
370+
* \param payload_len Payload length
371+
* \param *observe_ptr Pointer to observe number to be sent
372+
* \param observe_len Observe number len
373+
* \param message_type Observation message type (confirmable or non-confirmable)
374+
* \param content_type Observation message payload contetnt type
375+
* \param uri_path_ptr Pointer to uri path to be sent
376+
* \param uri_path_len Uri path len
377+
*
378+
* \return !0 Success, observation messages message ID
379+
* \return 0 Failure
380+
*/
381+
extern uint16_t sn_nsdl_send_observation_notification_with_uri_path(struct nsdl_s *handle, uint8_t *token_ptr, uint8_t token_len,
382+
uint8_t *payload_ptr, uint16_t payload_len,
383+
uint8_t *observe_ptr, uint8_t observe_len,
384+
sn_coap_msg_type_e message_type,
385+
uint8_t content_type,
386+
uint8_t *uri_path_ptr,
387+
uint16_t uri_path_len);
353388

354389
/**
355390
* \fn extern uint32_t sn_nsdl_get_version(void)

source/libCoap/src/include/sn_coap_header_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ typedef struct sn_nsdl_transmit_ {
6464

6565
uint16_t packet_len;
6666
uint8_t *packet_ptr;
67+
uint8_t *uri_path_ptr;
68+
uint8_t uri_path_len;
6769
} sn_nsdl_transmit_s;
6870

6971
/* * * * * * * * * * * * * * * * * * * * * * */

source/libCoap/src/include/sn_coap_protocol_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ extern "C" {
7272
/* Note: Current Coap implementation supports Blockwise transfers specification version draft-ietf-core-block-03 */
7373
/* Note: This define is common for both received and sent Blockwise messages */
7474
#ifndef SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE
75-
#define SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE 128 /**< Must be 2^x and x is at least 4. Suitable values: 0, 16, 32, 64, 128, 256, 512 and 1024 */
75+
#define SN_COAP_BLOCKWISE_MAX_PAYLOAD_SIZE 1024 /**< Must be 2^x and x is at least 4. Suitable values: 0, 16, 32, 64, 128, 256, 512 and 1024 */
7676
#endif
7777

7878
#ifndef SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED

source/libCoap/src/sn_coap_builder.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,20 @@ uint16_t sn_coap_builder_calc_needed_packet_data_size(sn_coap_hdr_s *src_coap_ms
170170
}
171171

172172
/* URI PATH - Repeatable option. Length of one option is 0-255 */
173-
if (src_coap_msg_ptr->uri_path_ptr != NULL) {
174-
repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->uri_path_len,
175-
src_coap_msg_ptr->uri_path_ptr, COAP_OPTION_URI_PATH);
176-
if (repeatable_option_size) {
177-
returned_byte_count += repeatable_option_size;
178-
} else {
179-
return 0;
173+
/* Do not add uri-path for notification message.
174+
* Uri-path is needed for cancelling observation with RESET message */
175+
if (!src_coap_msg_ptr->options_list_ptr ||
176+
(src_coap_msg_ptr->options_list_ptr && !src_coap_msg_ptr->options_list_ptr->observe_len && !src_coap_msg_ptr->options_list_ptr->observe_ptr)) {
177+
if (src_coap_msg_ptr->uri_path_ptr != NULL) {
178+
repeatable_option_size = sn_coap_builder_options_calc_option_size(src_coap_msg_ptr->uri_path_len,
179+
src_coap_msg_ptr->uri_path_ptr, COAP_OPTION_URI_PATH);
180+
if (repeatable_option_size) {
181+
returned_byte_count += repeatable_option_size;
182+
} else {
183+
return 0;
184+
}
180185
}
181186
}
182-
183187
/* CONTENT TYPE - Length of this option is 0-2 bytes */
184188
if (src_coap_msg_ptr->content_type_ptr != NULL) {
185189
returned_byte_count++;
@@ -610,7 +614,11 @@ static int8_t sn_coap_builder_options_build(uint8_t **dst_packet_data_pptr, sn_c
610614
&src_coap_msg_ptr->options_list_ptr->location_path_len, COAP_OPTION_LOCATION_PATH);
611615
}
612616
/* * * * Build Uri-Path option * * * */
613-
sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->uri_path_ptr,
617+
/* Do not add uri-path for notification message.
618+
* Uri-path is needed for cancelling observation with RESET message */
619+
if (!src_coap_msg_ptr->options_list_ptr ||
620+
(src_coap_msg_ptr->options_list_ptr && !src_coap_msg_ptr->options_list_ptr->observe_len && !src_coap_msg_ptr->options_list_ptr->observe_ptr))
621+
sn_coap_builder_options_build_add_multiple_option(dst_packet_data_pptr, &src_coap_msg_ptr->uri_path_ptr,
614622
&src_coap_msg_ptr->uri_path_len, COAP_OPTION_URI_PATH);
615623

616624
/* * * * Build Content-Type option * * * */

source/libCoap/src/sn_coap_protocol.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int8_t sn_coap_convert_block_size(uint16_t block_size);
6565
static sn_coap_hdr_s *sn_coap_protocol_copy_header(struct coap_s *handle, sn_coap_hdr_s *source_header_ptr);
6666
#endif
6767
#if ENABLE_RESENDINGS
68-
static void sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len, uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param);
68+
static void sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len, uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param, uint8_t *uri_path_ptr, uint8_t uri_path_len);
6969
static sn_nsdl_transmit_s *sn_coap_protocol_linked_list_send_msg_search(struct coap_s *handle,sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
7070
static void sn_coap_protocol_linked_list_send_msg_remove(struct coap_s *handle, sn_nsdl_addr_s *src_addr_ptr, uint16_t msg_id);
7171
static coap_send_msg_s *sn_coap_protocol_allocate_mem_for_msg(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t packet_data_len);
@@ -311,6 +311,10 @@ void sn_coap_protocol_clear_retransmission_buffer(struct coap_s *handle)
311311
handle->sn_coap_protocol_free(tmp->send_msg_ptr->packet_ptr);
312312
tmp->send_msg_ptr->packet_ptr = 0;
313313
}
314+
if (tmp->send_msg_ptr->uri_path_ptr) {
315+
handle->sn_coap_protocol_free(tmp->send_msg_ptr->uri_path_ptr);
316+
tmp->send_msg_ptr->uri_path_ptr = 0;
317+
}
314318
handle->sn_coap_protocol_free(tmp->send_msg_ptr);
315319
tmp->send_msg_ptr = 0;
316320
}
@@ -434,7 +438,7 @@ int16_t sn_coap_protocol_build(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_p
434438
/* Store message to Linked list for resending purposes */
435439
sn_coap_protocol_linked_list_send_msg_store(handle, dst_addr_ptr, byte_count_built, dst_packet_data_ptr,
436440
handle->system_time + (uint32_t)(handle->sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR),
437-
param);
441+
param, src_coap_msg_ptr->uri_path_ptr, src_coap_msg_ptr->uri_path_len);
438442
}
439443

440444
#endif /* ENABLE_RESENDINGS */
@@ -671,10 +675,18 @@ sn_coap_hdr_s *sn_coap_protocol_parse(struct coap_s *handle, sn_nsdl_addr_s *src
671675
sn_nsdl_transmit_s *removed_msg_ptr = NULL;
672676

673677
/* Check if received message was confirmation for some active resending message */
674-
675678
removed_msg_ptr = sn_coap_protocol_linked_list_send_msg_search(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
676679

677680
if (removed_msg_ptr != NULL) {
681+
if (returned_dst_coap_msg_ptr->msg_type == COAP_MSG_TYPE_RESET) {
682+
if(removed_msg_ptr->uri_path_len) {
683+
returned_dst_coap_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(removed_msg_ptr->uri_path_len);
684+
if (returned_dst_coap_msg_ptr->uri_path_ptr != NULL) {
685+
memcpy(returned_dst_coap_msg_ptr->uri_path_ptr, removed_msg_ptr->uri_path_ptr, removed_msg_ptr->uri_path_len);
686+
returned_dst_coap_msg_ptr->uri_path_len = removed_msg_ptr->uri_path_len;
687+
}
688+
}
689+
}
678690
/* Remove resending message from active message resending Linked list */
679691
sn_coap_protocol_linked_list_send_msg_remove(handle, src_addr_ptr, returned_dst_coap_msg_ptr->msg_id);
680692
}
@@ -777,7 +789,7 @@ int8_t sn_coap_protocol_exec(struct coap_s *handle, uint32_t current_time)
777789
*****************************************************************************/
778790

779791
static void sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, sn_nsdl_addr_s *dst_addr_ptr, uint16_t send_packet_data_len,
780-
uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param)
792+
uint8_t *send_packet_data_ptr, uint32_t sending_time, void *param, uint8_t *uri_path_ptr, uint8_t uri_path_len)
781793
{
782794

783795
coap_send_msg_s *stored_msg_ptr = NULL;
@@ -825,6 +837,16 @@ static void sn_coap_protocol_linked_list_send_msg_store(struct coap_s *handle, s
825837
stored_msg_ptr->coap = handle;
826838
stored_msg_ptr->param = param;
827839

840+
if (uri_path_len) {
841+
stored_msg_ptr->send_msg_ptr->uri_path_ptr = handle->sn_coap_protocol_malloc(uri_path_len);
842+
if (stored_msg_ptr->send_msg_ptr->uri_path_ptr == NULL){
843+
return;
844+
}
845+
stored_msg_ptr->send_msg_ptr->uri_path_len = uri_path_len;
846+
memcpy(stored_msg_ptr->send_msg_ptr->uri_path_ptr, uri_path_ptr, uri_path_len);
847+
}
848+
849+
828850
/* Storing Resending message to Linked list */
829851
ns_list_add_to_end(&handle->linked_list_resent_msgs, stored_msg_ptr);
830852
++handle->count_resent_msgs;
@@ -1401,6 +1423,11 @@ static void sn_coap_protocol_release_allocated_send_msg_mem(struct coap_s *handl
14011423
freed_send_msg_ptr->send_msg_ptr->packet_ptr = 0;
14021424
}
14031425

1426+
if (freed_send_msg_ptr->send_msg_ptr->uri_path_ptr != NULL) {
1427+
handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr->uri_path_ptr);
1428+
freed_send_msg_ptr->send_msg_ptr->uri_path_ptr = 0;
1429+
}
1430+
14041431
handle->sn_coap_protocol_free(freed_send_msg_ptr->send_msg_ptr);
14051432
freed_send_msg_ptr->send_msg_ptr = 0;
14061433
}
@@ -1916,7 +1943,7 @@ static sn_coap_hdr_s *sn_coap_handle_blockwise_message(struct coap_s *handle, sn
19161943
sn_coap_protocol_linked_list_send_msg_store(handle, src_addr_ptr,
19171944
dst_packed_data_needed_mem,
19181945
dst_ack_packet_data_ptr,
1919-
handle->system_time + (uint32_t)(handle->sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR), param);
1946+
handle->system_time + (uint32_t)(handle->sn_coap_resending_intervall * RESPONSE_RANDOM_FACTOR), param, NULL, 0);
19201947
#endif
19211948
handle->sn_coap_protocol_free(dst_ack_packet_data_ptr);
19221949
dst_ack_packet_data_ptr = 0;

source/libNsdl/src/sn_nsdl.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,81 @@ uint16_t sn_nsdl_send_observation_notification(struct nsdl_s *handle, uint8_t *t
569569
return return_msg_id;
570570
}
571571

572+
uint16_t sn_nsdl_send_observation_notification_with_uri_path(struct nsdl_s *handle, uint8_t *token_ptr, uint8_t token_len,
573+
uint8_t *payload_ptr, uint16_t payload_len,
574+
uint8_t *observe_ptr, uint8_t observe_len,
575+
sn_coap_msg_type_e message_type, uint8_t content_type,
576+
uint8_t *uri_path_ptr, uint16_t uri_path_len)
577+
{
578+
sn_coap_hdr_s *notification_message_ptr;
579+
uint16_t return_msg_id = 0;
580+
581+
/* Check parameters */
582+
if (handle == NULL) {
583+
return 0;
584+
}
585+
586+
/* Allocate and initialize memory for header struct */
587+
notification_message_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
588+
if (notification_message_ptr == NULL) {
589+
return 0;
590+
}
591+
592+
memset(notification_message_ptr, 0, sizeof(sn_coap_hdr_s));
593+
594+
notification_message_ptr->options_list_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
595+
if (notification_message_ptr->options_list_ptr == NULL) {
596+
handle->sn_nsdl_free(notification_message_ptr);
597+
return 0;
598+
}
599+
600+
memset(notification_message_ptr->options_list_ptr , 0, sizeof(sn_coap_options_list_s));
601+
602+
/* Fill header */
603+
notification_message_ptr->msg_type = message_type;
604+
notification_message_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
605+
606+
/* Fill token */
607+
notification_message_ptr->token_len = token_len;
608+
notification_message_ptr->token_ptr = token_ptr;
609+
610+
/* Fill payload */
611+
notification_message_ptr->payload_len = payload_len;
612+
notification_message_ptr->payload_ptr = payload_ptr;
613+
614+
/* Fill uri path */
615+
notification_message_ptr->uri_path_len = uri_path_len;
616+
notification_message_ptr->uri_path_ptr = uri_path_ptr;
617+
618+
/* Fill observe */
619+
notification_message_ptr->options_list_ptr->observe_len = observe_len;
620+
notification_message_ptr->options_list_ptr->observe_ptr = observe_ptr;
621+
622+
/* Fill content type */
623+
if (content_type) {
624+
notification_message_ptr->content_type_len = 1;
625+
notification_message_ptr->content_type_ptr = &content_type;
626+
}
627+
628+
/* Send message */
629+
if (sn_nsdl_send_coap_message(handle, handle->nsp_address_ptr->omalw_address_ptr, notification_message_ptr) == SN_NSDL_FAILURE) {
630+
return_msg_id = 0;
631+
} else {
632+
return_msg_id = notification_message_ptr->msg_id;
633+
}
634+
635+
/* Free memory */
636+
notification_message_ptr->uri_path_ptr = NULL;
637+
notification_message_ptr->payload_ptr = NULL;
638+
notification_message_ptr->options_list_ptr->observe_ptr = NULL;
639+
notification_message_ptr->token_ptr = NULL;
640+
notification_message_ptr->content_type_ptr = NULL;
641+
642+
sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, notification_message_ptr);
643+
644+
return return_msg_id;
645+
}
646+
572647
/* * * * * * * * * * */
573648
/* ~ OMA functions ~ */
574649
/* * * * * * * * * * */

test/nsdl-c/unittest/sn_coap_builder/libCoap_builder_test.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,6 @@ TEST(libCoap_builder, sn_coap_builder_calc_needed_packet_data_size)
356356
header.options_list_ptr->uri_query_len = 0;
357357
CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
358358

359-
free(header.options_list_ptr->observe_ptr);
360-
header.options_list_ptr->observe_ptr = NULL;
361359
header.options_list_ptr->observe = 1;
362360

363361
header.options_list_ptr->uri_query_len = 4;
@@ -370,9 +368,17 @@ TEST(libCoap_builder, sn_coap_builder_calc_needed_packet_data_size)
370368
header.options_list_ptr->block1_len = 0;
371369
CHECK(sn_coap_builder_calc_needed_packet_data_size(&header) == 0);
372370

371+
header.options_list_ptr->block1_len = 2;
372+
header.payload_len = 1;
373+
CHECK(431 == sn_coap_builder_calc_needed_packet_data_size(&header));
374+
375+
free(header.options_list_ptr->observe_ptr);
376+
header.options_list_ptr->observe_ptr = NULL;
377+
header.options_list_ptr->observe_len = 0;
373378
header.options_list_ptr->block1_len = 2;
374379
header.payload_len = 1;
375380
CHECK(722 == sn_coap_builder_calc_needed_packet_data_size(&header));
381+
376382
// <--
377383

378384
free(header.options_list_ptr->block1_ptr);

0 commit comments

Comments
 (0)