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

Commit 858d9f6

Browse files
authored
Merge pull request #110 from ARMmbed/counterpart_of_put_api
nsdl: add sn_grs_pop_resource function
2 parents 63a12d3 + a5e35ef commit 858d9f6

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

nsdl-c/sn_nsdl_lib.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,15 @@ extern int8_t sn_nsdl_update_resource(struct nsdl_s *handle, sn_nsdl_dynamic_res
400400
#endif
401401

402402
/**
403-
* \fn extern int8_t sn_nsdl_put_resource(struct nsdl_s *handle, const sn_nsdl_resource_parameters_s *res);
403+
* \fn extern int8_t sn_nsdl_put_resource(struct nsdl_s *handle, const sn_nsdl_dynamic_resource_parameters_s *res);
404404
*
405405
* \brief Resource putting function.
406406
*
407407
* Used to put a static or dynamic CoAP resource without creating copy of it.
408408
* NOTE: Remember that only resource will be owned, not data that it contains
409+
* NOTE: The resource may be removed from list by sn_nsdl_pop_resource().
409410
*
410-
* \param *res Pointer to a structure of type sn_nsdl_resource_info_t that contains the information
411+
* \param *res Pointer to a structure of type sn_nsdl_dynamic_resource_parameters_s that contains the information
411412
* about the resource.
412413
*
413414
* \return 0 Success
@@ -418,6 +419,23 @@ extern int8_t sn_nsdl_update_resource(struct nsdl_s *handle, sn_nsdl_dynamic_res
418419
*/
419420
extern int8_t sn_nsdl_put_resource(struct nsdl_s *handle, sn_nsdl_dynamic_resource_parameters_s *res);
420421

422+
/**
423+
* \fn extern int8_t sn_nsdl_pop_resource(struct nsdl_s *handle, const sn_nsdl_dynamic_resource_parameters_s *res);
424+
*
425+
* \brief Resource popping function.
426+
*
427+
* Used to remove a static or dynamic CoAP resource from lists without deleting it.
428+
* NOTE: This function is a counterpart of sn_nsdl_put_resource().
429+
*
430+
* \param *res Pointer to a structure of type sn_nsdl_dynamic_resource_parameters_s that contains the information
431+
* about the resource.
432+
*
433+
* \return 0 Success
434+
* \return -1 Failure
435+
* \return -3 Invalid path
436+
*/
437+
extern int8_t sn_nsdl_pop_resource(struct nsdl_s *handle, sn_nsdl_dynamic_resource_parameters_s *res);
438+
421439
/**
422440
* \fn extern int8_t sn_nsdl_delete_resource(struct nsdl_s *handle, uint8_t pathlen, uint8_t *path)
423441
*

source/libNsdl/src/include/sn_grs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ extern int8_t sn_grs_send_coap_message(struct
132132
sn_nsdl_addr_s *address_ptr,
133133
sn_coap_hdr_s *coap_hdr_ptr);
134134
extern int8_t sn_grs_put_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *res);
135+
extern int8_t sn_grs_pop_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *res);
135136
extern int8_t sn_grs_delete_resource(struct grs_s *handle, uint16_t pathlen, uint8_t *path);
136137
extern void sn_grs_mark_resources_as_registered(struct nsdl_s *handle);
137138
#ifndef MEMORY_OPTIMIZED_API

source/libNsdl/src/sn_grs.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,29 @@ int8_t sn_grs_put_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parame
510510
return SN_NSDL_SUCCESS;
511511
}
512512

513+
int8_t sn_grs_pop_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
514+
{
515+
if (!res || !handle) {
516+
return SN_NSDL_FAILURE;
517+
}
518+
519+
/* Check path validity */
520+
if (!res->static_resource_parameters->pathlen || !res->static_resource_parameters->path) {
521+
return SN_GRS_INVALID_PATH;
522+
}
523+
524+
/* Check if resource exists on list. */
525+
if (sn_grs_search_resource(handle,
526+
res->static_resource_parameters->pathlen,
527+
res->static_resource_parameters->path, SN_GRS_SEARCH_METHOD) == (sn_nsdl_dynamic_resource_parameters_s *)NULL) {
528+
return SN_NSDL_FAILURE;
529+
}
530+
531+
ns_list_remove(&handle->resource_root_list, res);
532+
--handle->resource_root_count;
533+
534+
return SN_NSDL_SUCCESS;
535+
}
513536

514537
/**
515538
* \fn extern int8_t sn_grs_process_coap(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src)

source/libNsdl/src/sn_nsdl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,15 @@ extern int8_t sn_nsdl_put_resource(struct nsdl_s *handle, sn_nsdl_dynamic_resour
16241624
return sn_grs_put_resource(handle->grs, res);
16251625
}
16261626

1627+
extern int8_t sn_nsdl_pop_resource(struct nsdl_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
1628+
{
1629+
if (!handle) {
1630+
return SN_NSDL_FAILURE;
1631+
}
1632+
1633+
return sn_grs_pop_resource(handle->grs, res);
1634+
}
1635+
16271636
extern int8_t sn_nsdl_delete_resource(struct nsdl_s *handle, uint16_t pathlen, uint8_t *path)
16281637
{
16291638
/* Check parameters */

test/nsdl-c/unittest/sn_nsdl/test_sn_nsdl.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,6 +2623,24 @@ bool test_sn_nsdl_put_resource()
26232623
return false;
26242624
}
26252625

2626+
/* Add real data and pop it back too. */
2627+
sn_nsdl_static_resource_parameters_s static_res = {
2628+
.path = "hello",
2629+
.pathlen = 5
2630+
};
2631+
2632+
sn_nsdl_dynamic_resource_parameters_s dyn_res = {
2633+
.static_resource_parameters = &static_res
2634+
};
2635+
2636+
if( 0 != sn_nsdl_put_resource(handle, &dyn_res) ){
2637+
return false;
2638+
}
2639+
2640+
if( 0 != sn_nsdl_pop_resource(handle, &dyn_res) ){
2641+
return false;
2642+
}
2643+
26262644
sn_nsdl_destroy(handle);
26272645
return true;
26282646
}

test/nsdl-c/unittest/stubs/sn_grs_stub.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ int8_t sn_grs_put_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parame
144144
return sn_grs_stub.expectedInt8;
145145
}
146146

147+
int8_t sn_grs_pop_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
148+
{
149+
if( sn_grs_stub.int8SuccessCounter > 0 ){
150+
sn_grs_stub.int8SuccessCounter++;
151+
return SN_NSDL_SUCCESS;
152+
}
153+
return sn_grs_stub.expectedInt8;
154+
}
155+
147156
extern int8_t sn_grs_process_coap(struct nsdl_s *nsdl_handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *src_addr_ptr)
148157
{
149158
return sn_grs_stub.expectedInt8;

0 commit comments

Comments
 (0)