*/
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,
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)
{
//TODO : Add resource type filtering once collections
// start supporting queries.
- ret = BuildVirtualResourceResponse(temp, payload, &ehRequest->devAddr);
+ ret = BuildResponseRepresentation(temp, &payload);
}
}
}
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;
if(stackRet == OC_STACK_OK)
{
- stackRet = BuildVirtualResourceResponse(collResource, payload, &ehRequest->devAddr);
+ OCRepPayloadSetUri(payload, collResource->uri);
}
if(stackRet == OC_STACK_OK)
response.resourceHandle = (OCResourceHandle) collResource;
stackRet = OCDoResponse(&response);
}
- OCDiscoveryPayloadDestroy(payload);
if (stackRet == OC_STACK_OK)
{
}
parent->next= child;
+ child->next = NULL;
}
static OCRepPayloadValue* OCRepPayloadFindValue(const OCRepPayload* payload, const char* name)
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);
}
{
char* curIf;
err = err || cbor_value_dup_text_string(&ifArray, &curIf, &len, NULL);
+ err = err || cbor_value_advance(&ifArray);
OCRepPayloadAddInterfaceAsOwner(curPayload, curIf);
}
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)
{
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)--;