Fixing collections
authorOmkar Hegde <omkar.m.hegde@intel.com>
Fri, 17 Jul 2015 00:59:49 +0000 (17:59 -0700)
committerErich Keane <erich.keane@intel.com>
Sat, 18 Jul 2015 01:01:37 +0000 (01:01 +0000)
The result status was set incorrectly. Also, wrong type
of payload was being created in HandleBatchInterface()
and HandleLinkedListInterface(). Added a function to
create representation payload from resource pointer.
Also, changing HandleAggregateResponse() to handle
collections correctly. Some bug fixes for CBOR parsing
which affected collections.

Change-Id: Ia3c1fb4d144ead24e7efad0d915a01dad16d15e5
Signed-off-by: Omkar Hegde <omkar.m.hegde@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1700
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/stack/include/internal/ocresourcehandler.h
resource/csdk/stack/src/occollection.c
resource/csdk/stack/src/ocpayload.c
resource/csdk/stack/src/ocpayloadparse.c
resource/csdk/stack/src/ocresource.c
resource/csdk/stack/src/ocserverrequest.c

index 2d3d6d3..432b2f7 100644 (file)
@@ -144,8 +144,14 @@ void DeletePlatformInfo();
  */
 void DeleteDeviceInfo();
 
+/*
+ * Prepare payload for resource representation.
+ */
+OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
+                    OCRepPayload** payload);
+
 /**
- * Prepares a JSON string for response.
+ * Prepares a Payload for response.
  */
 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
                                            OCDiscoveryPayload* payload,
index 6f3d7a0..b1822de 100644 (file)
@@ -231,15 +231,11 @@ HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest, uint8_t filterOn, c
     OCStackResult ret = OC_STACK_OK;
     OCResource *collResource = (OCResource *)ehRequest->resource;
 
-    OCDiscoveryPayload* payload = OCDiscoveryPayloadCreate();
-    if(!payload)
-    {
-        ret = OC_STACK_NO_MEMORY;
-    }
+    OCRepPayload* payload = NULL;
 
     if(ret == OC_STACK_OK)
     {
-        ret = BuildVirtualResourceResponse(collResource, payload, &ehRequest->devAddr);
+        ret = BuildResponseRepresentation(collResource, &payload);
     }
 
     if (ret == OC_STACK_OK)
@@ -251,7 +247,7 @@ HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest, uint8_t filterOn, c
             {
                 //TODO : Add resource type filtering once collections
                 // start supporting queries.
-                ret = BuildVirtualResourceResponse(temp, payload, &ehRequest->devAddr);
+                ret = BuildResponseRepresentation(temp, &payload);
             }
         }
     }
@@ -266,18 +262,18 @@ HandleLinkedListInterface(OCEntityHandlerRequest *ehRequest, uint8_t filterOn, c
         response.resourceHandle = (OCResourceHandle) collResource;
         ret = OCDoResponse(&response);
     }
-    OCDiscoveryPayloadDestroy(payload);
+    OCRepPayloadDestroy(payload);
     return ret;
 }
 
 static OCStackResult
 HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
 {
-    OCStackResult stackRet = OC_STACK_ERROR;
+    OCStackResult stackRet = OC_STACK_OK;
     OCEntityHandlerResult ehResult = OC_EH_ERROR;
     OCResource * collResource = (OCResource *) ehRequest->resource;
 
-    OCDiscoveryPayload* payload = OCDiscoveryPayloadCreate();
+    OCRepPayload* payload = OCRepPayloadCreate();
     if(!payload)
     {
         stackRet = OC_STACK_NO_MEMORY;
@@ -285,7 +281,7 @@ HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
 
     if(stackRet == OC_STACK_OK)
     {
-        stackRet = BuildVirtualResourceResponse(collResource, payload, &ehRequest->devAddr);
+        OCRepPayloadSetUri(payload, collResource->uri);
     }
 
     if(stackRet == OC_STACK_OK)
@@ -298,7 +294,6 @@ HandleBatchInterface(OCEntityHandlerRequest *ehRequest)
         response.resourceHandle = (OCResourceHandle) collResource;
         stackRet = OCDoResponse(&response);
     }
-    OCDiscoveryPayloadDestroy(payload);
 
     if (stackRet == OC_STACK_OK)
     {
index ce82edc..afb8700 100644 (file)
@@ -91,6 +91,7 @@ void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child)
     }
 
     parent->next= child;
+    child->next = NULL;
 }
 
 static OCRepPayloadValue* OCRepPayloadFindValue(const OCRepPayload* payload, const char* name)
index 0bee1db..4959c0b 100644 (file)
@@ -676,7 +676,8 @@ static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue* repPar
             while(!err && cbor_value_is_valid(&rtArray))
             {
                 char* curRt;
-                cbor_value_dup_text_string(&rtArray, &curRt, &len, NULL);
+                err = err || cbor_value_dup_text_string(&rtArray, &curRt, &len, NULL);
+                err = err || cbor_value_advance(&rtArray);
                 OCRepPayloadAddResourceTypeAsOwner(curPayload, curRt);
             }
 
@@ -694,6 +695,7 @@ static bool OCParseSingleRepPayload(OCRepPayload** outPayload, CborValue* repPar
             {
                 char* curIf;
                 err = err || cbor_value_dup_text_string(&ifArray, &curIf, &len, NULL);
+                err = err || cbor_value_advance(&ifArray);
                 OCRepPayloadAddInterfaceAsOwner(curPayload, curIf);
             }
 
index 71db404..8a1d744 100644 (file)
@@ -202,6 +202,58 @@ static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *q
     return result;
 }
 
+OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
+                    OCRepPayload** payload)
+{
+    OCRepPayload *tempPayload = OCRepPayloadCreate();
+
+    if (!resourcePtr)
+    {
+        OCRepPayloadDestroy(tempPayload);
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    if(!tempPayload)
+    {
+        return OC_STACK_NO_MEMORY;
+    }
+
+    OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
+
+    OCResourceType *resType = resourcePtr->rsrcType;
+    while(resType)
+    {
+        OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
+        resType = resType->next;
+    }
+
+    OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
+    while(resInterface)
+    {
+        OCRepPayloadAddInterface(tempPayload, resInterface->name);
+        resInterface = resInterface->next;
+    }
+
+    OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
+    while(resAttrib)
+    {
+        OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
+                                resAttrib->attrValue);
+        resAttrib = resAttrib->next;
+    }
+
+    if(!*payload)
+    {
+        *payload = tempPayload;
+    }
+    else
+    {
+        OCRepPayloadAppend(*payload, tempPayload);
+    }
+
+    return OC_STACK_OK;
+}
+
 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
                         OCDiscoveryPayload *payload, OCDevAddr *devAddr)
 {
index 4f883a2..eed3051 100644 (file)
@@ -658,16 +658,24 @@ OCStackResult HandleAggregateResponse(OCEntityHandlerResponse * ehResponse)
             VERIFY_NON_NULL(serverResponse);
         }
 
-        if(serverResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION ||
-                ehResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
+        if(ehResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
         {
             stackRet = OC_STACK_ERROR;
             OC_LOG(ERROR, TAG, PCF("Error adding payload, as it was the incorrect type"));
             goto exit;
         }
 
-        OCRepPayloadAppend((OCRepPayload*)serverResponse->payload,
-                (OCRepPayload*)ehResponse->payload);
+        if(!serverResponse->payload)
+        {
+            serverResponse->payload = (OCPayload*)OCRepPayloadCreate();
+            serverResponse->payload = ehResponse->payload;
+        }
+        else
+        {
+            OCRepPayloadAppend((OCRepPayload*)serverResponse->payload,
+                    (OCRepPayload*)ehResponse->payload);
+        }
+
 
         (serverRequest->numResponses)--;