CA Integration: Enabling OCDoResource to send messages
authorVijay <vijay.s.kesavan@intel.com>
Sun, 14 Dec 2014 05:36:02 +0000 (21:36 -0800)
committersudarshan prasad <sudarshan.prasad@intel.com>
Thu, 18 Dec 2014 14:48:22 +0000 (06:48 -0800)
Change-Id: I289340c8dc05cebe4bc1dfc2e7acdf73decc536b
Signed-off-by: Vijay <vijay.s.kesavan@intel.com>
resource/csdk/stack/include/internal/occlientcb.h
resource/csdk/stack/src/occlientcb.c
resource/csdk/stack/src/ocstack.c

index 5fc14d8..25d15c0 100644 (file)
@@ -25,6 +25,9 @@
 #include <ocstack.h>
 #include <occoaptoken.h>
 #include <ocresource.h>
+#ifdef CA_INT
+#include "cacommon.h"
+#endif
 
 typedef struct OCPresence {
     // This is the TTL associated with presence
@@ -49,7 +52,11 @@ typedef struct ClientCB {
     // callback method to delete context data
     OCClientContextDeleter deleteCallback;
     //  when a response is recvd with this token, above callback will be invoked
+    #ifdef CA_INT
+    CAToken_t token;
+    #else // CA_INT
     OCCoAPToken token;
+    #endif // CA_INT
     // Invocation handle tied to original call to OCDoResource()
     OCDoHandle handle;
     // This is used to determine if all responses should be consumed or not.
@@ -93,9 +100,16 @@ extern struct ClientCB *cbList;
  * @retval OC_STACK_OK for Success, otherwise some error value
  */
 //------------------------------------------------------------------------
-OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
-        OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
-        unsigned char * requestUri, unsigned char * resourceType);
+OCStackResult
+#ifdef CA_INT
+AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
+             CAToken_t * token, OCDoHandle *handle, OCMethod method,
+             unsigned char * requestUri, unsigned char * resourceTypeName);
+#else // CA_INT
+AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
+             OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
+             unsigned char * requestUri, unsigned char * resourceTypeName);
+#endif // CA_INT
 
 //-- DeleteClientCB -----------------------------------------------------------
 /** @ingroup ocstack
@@ -126,7 +140,11 @@ void DeleteClientCB(ClientCB *cbNode);
  * @retval address of the node if found, otherwise NULL
  */
 //------------------------------------------------------------------------
+#ifdef CA_INT
+ClientCB* GetClientCB(CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri);
+#else // CA_INT
 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri);
+#endif // CA_INT
 
 
 /**
index d55a37e..8e9c348 100644 (file)
 #include "ocmalloc.h"
 #include <string.h>
 
+#ifdef CA_INT
+    #include "cacommon.h"
+    #include "cainterface.h"
+#endif
+
 /// Module Name
 #define TAG PCF("occlientcb")
 
+#ifdef CA_INT
+// TODO-CA: This has been defined in CA layer as well, but is not exposed externally.
+// Need to expose it from CA and remove the definition below.
+#define CA_MAX_TOKEN_LEN   (8)
+#endif //CA_INT
+
 struct ClientCB *cbList = NULL;
 OCMulticastNode * mcPresenceNodes = NULL;
 
-OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
-        OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
-        unsigned char * requestUri, unsigned char * resourceTypeName) {
+OCStackResult
+#ifdef CA_INT
+AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
+             CAToken_t * token, OCDoHandle *handle, OCMethod method,
+             unsigned char * requestUri, unsigned char * resourceTypeName)
+#else // CA_INT
+AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
+             OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
+             unsigned char * requestUri, unsigned char * resourceTypeName)
+#endif // CA_INT
+{
 
     ClientCB *cbNode = NULL;
 
@@ -58,7 +77,12 @@ OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
             cbNode->callBack = cbData->cb;
             cbNode->context = cbData->context;
             cbNode->deleteCallback = cbData->cd;
+            #ifdef CA_INT
+            cbNode->token = (CAToken_t *)OCMalloc(CA_MAX_TOKEN_LEN);
+            memcpy(&(cbNode->token), token, CA_MAX_TOKEN_LEN);
+            #else // CA_INT
             memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
+            #endif // CA_INT
             cbNode->handle = *handle;
             cbNode->method = method;
             cbNode->sequenceNumber = 0;
@@ -98,7 +122,11 @@ void DeleteClientCB(ClientCB * cbNode) {
     if(cbNode) {
         LL_DELETE(cbList, cbNode);
         OC_LOG(INFO, TAG, PCF("deleting tokens"));
+        #ifdef CA_INT
+        CADestroyToken (cbNode->token);
+        #else
         OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
+        #endif // CA_INT
         OCFree(cbNode->handle);
         OCFree(cbNode->requestUri);
         if(cbNode->deleteCallback)
@@ -129,17 +157,29 @@ void DeleteClientCB(ClientCB * cbNode) {
     }
 }
 
-ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri) {
+#ifdef CA_INT
+ClientCB* GetClientCB(CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
+#else // CA_INT
+ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri)
+#endif // CA_INT
+{
     ClientCB* out = NULL;
     if(token) {
         LL_FOREACH(cbList, out) {
             OC_LOG(INFO, TAG, PCF("comparing tokens"));
+            #ifdef CA_INT
+            if(memcmp(out->token, token, CA_MAX_TOKEN_LEN) == 0)
+            {
+                return out;
+            }
+            #else // CA_INT
             OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
             OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
             if((out->token.tokenLength == token->tokenLength) &&
                 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
                 return out;
             }
+            #endif // CA_INT
         }
     }
     else if(handle) {
index e6f5632..2b23e95 100644 (file)
@@ -80,6 +80,7 @@ OCDeviceEntityHandler defaultDeviceHandler;
 
 //TODO: we should allow the server to define this
 #define MAX_OBSERVE_AGE (0x2FFFFUL)
+
 //-----------------------------------------------------------------------------
 // Externs
 //-----------------------------------------------------------------------------
@@ -207,6 +208,7 @@ OCStackResult CAResultToOCStackResult(CAResult_t caResult)
 void HandleCAResponses(const CARemoteEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
 {
     OC_LOG(INFO, TAG, PCF("Enter HandleCAResponses"));
+    printf ("Received payload: %s\n", (char *)responseInfo->info.payload);
     OC_LOG_V(INFO, TAG, PCF("Received payload: %s\n"), (char*)responseInfo->info.payload);
     OC_LOG(INFO, TAG, PCF("Exit HandleCAResponses"));
 }
@@ -217,6 +219,7 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
     CAInfo_t responseData;
     CAResponseInfo_t responseInfo;
 
+    OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
     // generate the pdu, if the request was CON, then the response is ACK, otherwire NON
     memset(&responseData, 0, sizeof(CAInfo_t));
     responseData.token = (requestInfo != NULL) ? requestInfo->info.token : "";
@@ -239,7 +242,6 @@ void HandleCARequests(const CARemoteEndpoint_t* endPoint, const CARequestInfo_t*
         OC_LOG(ERROR, TAG, PCF("CASendResponse error"));
     }
 
-    OC_LOG(INFO, TAG, PCF("Enter HandleCARequests"));
     OC_LOG(INFO, TAG, PCF("Exit HandleCARequests"));
 }
 
@@ -894,6 +896,15 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ
     unsigned char * resourceType = NULL;
     char * newUri = (char *)requiredUri;
     (void) referenceUri;
+#ifdef CA_INT
+    CARemoteEndpoint_t* endpoint = NULL;
+    CAResult_t caResult;
+    CAToken_t caToken = NULL;
+    CAInfo_t requestData;
+    CARequestInfo_t requestInfo;
+    // To track if memory is allocated for additional header options
+    uint8_t hdrOptionMemAlloc = 0;
+#endif // CA_INT
 
     OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
 
@@ -973,6 +984,58 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ
         goto exit;
     }
 
+#ifdef CA_INT
+    // CA integration logic - TODO: remove this comment after integration is done
+
+    // Assuming request is not multi-cast
+    // Create remote end point
+    caResult = CACreateRemoteEndpoint(newUri, &endpoint);
+    endpoint->connectivityType = CA_WIFI;
+    if (caResult != CA_STATUS_OK)
+    {
+        OC_LOG(ERROR, TAG, PCF("CACreateRemoteEndpoint error"));
+        goto exit;
+    }
+
+    // create token
+    caResult = CAGenerateToken(&caToken);
+
+    if (caResult != CA_STATUS_OK)
+    {
+        OC_LOG(ERROR, TAG, PCF("CAGenerateToken error"));
+        caToken = NULL;
+        goto exit;
+    }
+
+    memset(&requestData, 0, sizeof(CAInfo_t));
+    // TODO-CA: Map QoS to the right CA msg type
+    requestData.type = CA_MSG_CONFIRM;
+    requestData.token = caToken;
+    requestData.options = (CAHeaderOption_t*)options;
+    requestData.numOptions = numOptions;
+    requestData.payload = (char *)request;
+
+    memset(&requestInfo, 0, sizeof(CARequestInfo_t));
+    requestInfo.method = CA_GET;
+    requestInfo.info = requestData;
+
+    // send request
+    caResult = CASendRequest(endpoint, &requestInfo);
+    if (caResult != CA_STATUS_OK)
+    {
+        OC_LOG(ERROR, TAG, PCF("CASendRequest"));
+        goto exit;
+    }
+
+    if((result = AddClientCB(&clientCB, cbData, &caToken, *handle, method,
+                             requestUri, resourceType)) != OC_STACK_OK)
+    {
+        result = OC_STACK_NO_MEMORY;
+        goto exit;
+    }
+
+#else // CA_INT
+
     // Generate token which will be used by OCStack to match responses received
     // with the request
     OCGenerateCoAPToken(&token);
@@ -994,6 +1057,7 @@ OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requ
 
     // Make call to OCCoAP layer
     result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
+#endif // CA_INT
 
 exit:
     if(newUri != requiredUri)
@@ -1005,6 +1069,14 @@ exit:
         OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
         FindAndDeleteClientCB(clientCB);
     }
+#ifdef CA_INT
+    CADestroyRemoteEndpoint(endpoint);
+    if (hdrOptionMemAlloc)
+    {
+        OCFree(requestData.options);
+    }
+    //TODO-CA: CADestroyToken here
+#endif // CA_INT
     return result;
 }