From bdd8e41db42bd77b8a412a8d3f1ec9595a4e0ae3 Mon Sep 17 00:00:00 2001 From: Vijay Date: Fri, 19 Dec 2014 10:43:18 -0800 Subject: [PATCH] CA Integration: Sending observe register/deregister request from client to server using CA header options Change-Id: I78ae3d916607ef994cb8d042611f7771b1ada501 Signed-off-by: Vijay --- .../csdk/stack/include/internal/ocobserve.h | 15 +++++++++ resource/csdk/stack/src/ocobserve.c | 31 +++++++++++++++++++ resource/csdk/stack/src/ocstack.c | 18 +++++++++-- 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/resource/csdk/stack/include/internal/ocobserve.h b/resource/csdk/stack/include/internal/ocobserve.h index ba8730669..50db45d34 100644 --- a/resource/csdk/stack/include/internal/ocobserve.h +++ b/resource/csdk/stack/include/internal/ocobserve.h @@ -27,6 +27,13 @@ #define MAX_OBSERVER_FAILED_COMM (2) #define MAX_OBSERVER_NON_COUNT (3) +#ifdef CA_INT +// NOTE: These values are based on the observe option values as defined in the CoAP RFC +// The values should not be changed unless there is a change in the RFC. +#define OC_RESOURCE_OBSERVE_REGISTER (0) +#define OC_RESOURCE_OBSERVE_DEREGISTER (1) +#endif // CA_INT + /* This information is stored for each registerd observer */ typedef struct ResourceObserver { // Observation Identifier for request @@ -83,4 +90,12 @@ ResourceObserver* GetObserverUsingToken (const OCCoAPToken * token); ResourceObserver* GetObserverUsingId (const OCObservationId observeId); +#ifdef CA_INT +OCStackResult +CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt, + OCHeaderOption *ocHdrOpt, + uint8_t numOptions, + uint8_t observeFlag); +#endif // CA_INT + #endif //OC_OBSERVE_H diff --git a/resource/csdk/stack/src/ocobserve.c b/resource/csdk/stack/src/ocobserve.c index e8e58d436..6053120b0 100644 --- a/resource/csdk/stack/src/ocobserve.c +++ b/resource/csdk/stack/src/ocobserve.c @@ -394,3 +394,34 @@ void DeleteObserverList() } serverObsList = NULL; } + +#ifdef CA_INT +OCStackResult +CreateObserveHeaderOption (CAHeaderOption_t **caHdrOpt, + OCHeaderOption *ocHdrOpt, + uint8_t numOptions, + uint8_t observeFlag) +{ + CAHeaderOption_t *tmpHdrOpt = NULL; + + tmpHdrOpt = (CAHeaderOption_t *) OCMalloc ((numOptions+1)*sizeof(CAHeaderOption_t)); + if (NULL == tmpHdrOpt) + { + return OC_STACK_NO_MEMORY; + } + tmpHdrOpt[0].protocolID = CA_COAP_ID; + // TODO-CA: COAP_OPTION_OBSERVE is defined in CoAP header files which will be abstracted + // from resource model. We have to define a new macro for this in the stack layer. + tmpHdrOpt[0].optionID = COAP_OPTION_OBSERVE; + // Length is one byte + tmpHdrOpt[0].optionLength = 1; + tmpHdrOpt[0].optionData[0] = observeFlag; + for (uint8_t i = 0; i < numOptions; i++) + { + memcpy (&(tmpHdrOpt[i+1]), &(ocHdrOpt[i]), sizeof(CAHeaderOption_t)); + } + + *caHdrOpt = tmpHdrOpt; + return OC_STACK_OK; +} +#endif // CA_INT diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index 3324da5ad..e5b5d73e6 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -1242,8 +1242,22 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ // TODO-CA: Map QoS to the right CA msg type requestData.type = CA_MSG_NONCONFIRM; requestData.token = caToken; - requestData.options = (CAHeaderOption_t*)options; - requestData.numOptions = numOptions; + if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL)) + { + result = CreateObserveHeaderOption (&(requestData.options), options, + numOptions, OC_RESOURCE_OBSERVE_REGISTER); + if (result != OC_STACK_OK) + { + goto exit; + } + hdrOptionMemAlloc = 1; + requestData.numOptions = numOptions + 1; + } + else + { + requestData.options = (CAHeaderOption_t*)options; + requestData.numOptions = numOptions; + } requestData.payload = (char *)request; requestInfo.info = requestData; -- 2.34.1