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

Commit d6a4ece

Browse files
author
Jaakko Kukkohovi
authored
Add application definable context to nsdl handle (#96)
Added application definable context field to nsdl handle. This is useful for example when interfacing with c++ objects where a pointer to object is set as the context, and in the callback functions the context pointer can be used to call methods for the correct instance of the c++ object.
1 parent e502b67 commit d6a4ece

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed

nsdl-c/sn_nsdl_lib.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,35 @@ extern int8_t sn_nsdl_set_block_size(struct nsdl_s *handle, uint16_t block_size)
723723
*/
724724
extern int8_t sn_nsdl_set_duplicate_buffer_size(struct nsdl_s *handle, uint8_t message_count);
725725

726+
/**
727+
* \fn void *sn_nsdl_set_context(const struct nsdl_s *handle, void *context)
728+
*
729+
* \brief Set the application defined context parameter for given handle.
730+
* This is useful for example when interfacing with c++ objects where a
731+
* pointer to object is set as the context, and in the callback functions
732+
* the context pointer can be used to call methods for the correct instance
733+
* of the c++ object.
734+
*
735+
* \param *handle Pointer to library handle
736+
* \param *context Pointer to the application defined context
737+
* \return 0 = success, -1 = failure
738+
*/
739+
extern int8_t sn_nsdl_set_context(struct nsdl_s * const handle, void * const context);
740+
741+
/**
742+
* \fn void *sn_nsdl_get_context(const struct nsdl_s *handle)
743+
*
744+
* \brief Get the application defined context parameter for given handle.
745+
* This is useful for example when interfacing with c++ objects where a
746+
* pointer to object is set as the context, and in the callback functions
747+
* the context pointer can be used to call methods for the correct instance
748+
* of the c++ object.
749+
*
750+
* \param *handle Pointer to library handle
751+
* \return Pointer to the application defined context
752+
*/
753+
extern void *sn_nsdl_get_context(const struct nsdl_s * const handle);
754+
726755
#ifdef __cplusplus
727756
}
728757
#endif

source/libNsdl/src/include/sn_grs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ struct nsdl_s {
7979
uint8_t *oma_bs_address_ptr; /* Bootstrap address pointer. If null, no bootstrap in use */
8080
sn_nsdl_ep_parameters_s *ep_information_ptr; // Endpoint parameters, Name, Domain etc..
8181
sn_nsdl_oma_server_info_t *nsp_address_ptr; // NSP server address information
82+
/* Application definable context. This is useful for example when interfacing with c++ objects where a pointer to object is set as the
83+
* context, and in the callback functions the context pointer can be used to call methods for the correct instance of the c++ object. */
84+
void *context;
8285

8386
void (*sn_nsdl_oma_bs_done_cb)(sn_nsdl_oma_server_info_t *server_info_ptr); /* Callback to inform application when bootstrap is done */
8487
void *(*sn_nsdl_alloc)(uint16_t);

source/libNsdl/src/sn_nsdl.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ struct nsdl_s *sn_nsdl_init(uint8_t (*sn_nsdl_tx_cb)(struct nsdl_s *, sn_nsdl_ca
223223
handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
224224
// By default bootstrap msgs are handled in nsdl
225225
handle->handle_bootstrap_msg = true;
226+
handle->context = NULL;
227+
226228
return handle;
227229
}
228230

@@ -2585,3 +2587,20 @@ bool sn_nsdl_check_uint_overflow(uint16_t resource_size, uint16_t param_a, uint1
25852587
}
25862588
}
25872589
}
2590+
2591+
extern int8_t sn_nsdl_set_context(struct nsdl_s * const handle, void * const context)
2592+
{
2593+
if (handle == NULL) {
2594+
return SN_NSDL_FAILURE;
2595+
}
2596+
handle->context = context;
2597+
return SN_NSDL_SUCCESS;
2598+
}
2599+
2600+
extern void *sn_nsdl_get_context(const struct nsdl_s * const handle)
2601+
{
2602+
if (handle == NULL) {
2603+
return NULL;
2604+
}
2605+
return handle->context;
2606+
}

test/nsdl-c/unittest/sn_nsdl/sn_nsdltest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,14 @@ TEST(sn_nsdl, test_sn_nsdl_set_duplicate_buffer_size)
186186
CHECK(test_sn_nsdl_set_duplicate_buffer_size());
187187
}
188188

189+
TEST(sn_nsdl, test_sn_nsdl_get_context)
190+
{
191+
CHECK(test_sn_nsdl_get_context());
192+
}
193+
194+
TEST(sn_nsdl, test_sn_nsdl_set_context)
195+
{
196+
CHECK(test_sn_nsdl_set_context());
197+
}
189198

190199

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015 ARM. All rights reserved.
2+
* Copyright (c) 2015 ARM. All rights reserved.
33
*/
44
#include "test_sn_nsdl.h"
55
#include <string.h>
@@ -2972,3 +2972,51 @@ bool test_sn_nsdl_set_duplicate_buffer_size()
29722972
sn_nsdl_destroy(handle);
29732973
return true;
29742974
}
2975+
2976+
bool test_sn_nsdl_set_context()
2977+
{
2978+
struct nsdl_s* handle = NULL;
2979+
if (sn_nsdl_set_context(handle,NULL) == 0){
2980+
printf("\n\neka\n\n");
2981+
return false;
2982+
}
2983+
retCounter = 4;
2984+
sn_grs_stub.expectedGrs = (struct grs_s *)malloc(sizeof(struct grs_s));
2985+
memset(sn_grs_stub.expectedGrs,0, sizeof(struct grs_s));
2986+
handle = sn_nsdl_init(&nsdl_tx_callback, &nsdl_rx_callback, &myMalloc, &myFree);
2987+
2988+
if (sn_nsdl_set_context(handle,NULL) != 0){
2989+
printf("\n\ntoka\n\n");
2990+
return false;
2991+
}
2992+
2993+
int somecontext = 1;
2994+
if (sn_nsdl_set_context(handle,&somecontext) != 0){
2995+
printf("\n\nkolmas\n\n");
2996+
return false;
2997+
}
2998+
sn_nsdl_destroy(handle);
2999+
return true;
3000+
}
3001+
3002+
bool test_sn_nsdl_get_context()
3003+
{
3004+
struct nsdl_s* handle = NULL;
3005+
if (sn_nsdl_get_context(handle) != NULL){
3006+
return false;
3007+
}
3008+
3009+
retCounter = 4;
3010+
sn_grs_stub.expectedGrs = (struct grs_s *)malloc(sizeof(struct grs_s));
3011+
memset(sn_grs_stub.expectedGrs,0, sizeof(struct grs_s));
3012+
handle = sn_nsdl_init(&nsdl_tx_callback, &nsdl_rx_callback, &myMalloc, &myFree);
3013+
3014+
int somecontext = 1;
3015+
sn_nsdl_set_context(handle,&somecontext);
3016+
if (sn_nsdl_get_context(handle) != &somecontext){
3017+
return false;
3018+
}
3019+
3020+
sn_nsdl_destroy(handle);
3021+
return true;
3022+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ bool test_sn_nsdl_set_block_size();
7878

7979
bool test_sn_nsdl_set_duplicate_buffer_size();
8080

81+
bool test_sn_nsdl_set_context();
82+
83+
bool test_sn_nsdl_get_context();
84+
8185
#ifdef __cplusplus
8286
}
8387
#endif

0 commit comments

Comments
 (0)