#include "ocstackinternal.h"
#include "ocresource.h"
#include "logger.h"
+#include "rdpayload.h"
+
+#define TAG "OIC_RI_PAYLOAD"
-#define TAG "OCPayload"
static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val);
void OCPayloadDestroy(OCPayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
case PAYLOAD_TYPE_SECURITY:
OCSecurityPayloadDestroy((OCSecurityPayload*)payload);
break;
+ case PAYLOAD_TYPE_RD:
+ OCRDPayloadDestroy((OCRDPayload*)payload);
+ break;
default:
- OC_LOG_V(ERROR, TAG, "Unsupported payload type in destroy: %d", payload->type);
+ OIC_LOG_V(ERROR, TAG, "Unsupported payload type in destroy: %d", payload->type);
OICFree(payload);
break;
}
{
OCRepPayload* payload = (OCRepPayload*)OICCalloc(1, sizeof(OCRepPayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child)
{
- if(!parent)
+ if (!parent)
{
return;
}
}
parent->next= child;
+ child->next = NULL;
}
static OCRepPayloadValue* OCRepPayloadFindValue(const OCRepPayload* payload, const char* name)
{
- if(!payload || !name)
+ if (!payload || !name)
{
return NULL;
}
OCRepPayloadValue* val = payload->values;
while(val)
{
- if(0 == strcmp(val->name, name))
+ if (0 == strcmp(val->name, name))
{
return val;
}
}
return NULL;
-
}
static void OCCopyPropertyValueArray(OCRepPayloadValue* dest, OCRepPayloadValue* source)
{
+ if (!dest || !source)
+ {
+ return;
+ }
+
size_t dimTotal = calcDimTotal(source->arr.dimensions);
switch(source->arr.type)
{
case OCREP_PROP_INT:
dest->arr.iArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
+ VERIFY_PARAM_NON_NULL(TAG, dest->arr.iArray, "Failed allocating memory");
memcpy(dest->arr.iArray, source->arr.iArray, dimTotal * sizeof(int64_t));
break;
case OCREP_PROP_DOUBLE:
dest->arr.dArray = (double*)OICMalloc(dimTotal * sizeof(double));
+ VERIFY_PARAM_NON_NULL(TAG, dest->arr.dArray, "Failed allocating memory");
memcpy(dest->arr.dArray, source->arr.dArray, dimTotal * sizeof(double));
break;
case OCREP_PROP_BOOL:
dest->arr.bArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
+ VERIFY_PARAM_NON_NULL(TAG, dest->arr.bArray, "Failed allocating memory");
memcpy(dest->arr.bArray, source->arr.bArray, dimTotal * sizeof(bool));
break;
case OCREP_PROP_STRING:
dest->arr.strArray = (char**)OICMalloc(dimTotal * sizeof(char*));
+ VERIFY_PARAM_NON_NULL(TAG, dest->arr.strArray, "Failed allocating memory");
for(size_t i = 0; i < dimTotal; ++i)
{
dest->arr.strArray[i] = OICStrdup(source->arr.strArray[i]);
}
break;
+ case OCREP_PROP_OBJECT:
+ dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
+ VERIFY_PARAM_NON_NULL(TAG, dest->arr.objArray, "Failed allocating memory");
+ for(size_t i = 0; i < dimTotal; ++i)
+ {
+ dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
+ }
+ break;
case OCREP_PROP_ARRAY:
dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
+ VERIFY_PARAM_NON_NULL(TAG, dest->arr.objArray, "Failed allocating memory");
for(size_t i = 0; i < dimTotal; ++i)
{
dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
}
break;
default:
- OC_LOG(ERROR, TAG, PCF("CopyPropertyValueArray invalid type"));
+ OIC_LOG(ERROR, TAG, "CopyPropertyValueArray invalid type");
break;
}
+exit:
+ return;
}
static void OCCopyPropertyValue (OCRepPayloadValue *dest, OCRepPayloadValue *source)
static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
{
- if(!val)
+ if (!val)
{
return;
}
- if(val->type == OCREP_PROP_STRING)
+ if (val->type == OCREP_PROP_STRING)
{
OICFree(val->str);
}
+ else if (val->type == OCREP_PROP_BYTE_STRING)
+ {
+ OICFree(val->ocByteStr.bytes);
+ }
else if (val->type == OCREP_PROP_OBJECT)
{
OCRepPayloadDestroy(val->obj);
OICFree(val->arr.iArray);
break;
case OCREP_PROP_STRING:
- for(size_t i = 0; i< dimTotal;++i)
+ for(size_t i = 0; i< dimTotal; ++i)
{
OICFree(val->arr.strArray[i]);
}
OICFree(val->arr.strArray);
break;
- case OCREP_PROP_OBJECT:
- for(size_t i = 0; i< dimTotal;++i)
+ case OCREP_PROP_BYTE_STRING:
+ for (size_t i = 0; i< dimTotal; ++i)
+ {
+ OICFree(val->arr.ocByteStrArray[i].bytes);
+ }
+ OICFree(val->arr.ocByteStrArray);
+ break;
+ case OCREP_PROP_OBJECT: // This case is the temporary fix for string input
+ for(size_t i = 0; i< dimTotal; ++i)
{
OCRepPayloadDestroy(val->arr.objArray[i]);
}
break;
case OCREP_PROP_NULL:
case OCREP_PROP_ARRAY:
- OC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
+ OIC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
inside an array: %d", val->arr.type);
break;
}
static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
{
- if(!val)
+ if (!val)
{
return;
}
static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
OCRepPayloadPropType type)
{
- if(!payload || !name)
+ if (!payload || !name)
{
return NULL;
}
OCRepPayloadValue* val = payload->values;
- if(val == NULL)
+ if (val == NULL)
{
payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
+ if (!payload->values)
+ {
+ return NULL;
+ }
payload->values->name = OICStrdup(name);
+ if (!payload->values->name)
+ {
+ OICFree(payload->values);
+ payload->values = NULL;
+ return NULL;
+ }
payload->values->type =type;
return payload->values;
}
while(val)
{
- if(0 == strcmp(val->name, name))
+ if (0 == strcmp(val->name, name))
{
OCFreeRepPayloadValueContents(val);
val->type = type;
return val;
}
- else if(val->next == NULL)
+ else if (val->next == NULL)
{
val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
+ if (!val->next)
+ {
+ return NULL;
+ }
val->next->name = OICStrdup(name);
+ if (!val->next->name)
+ {
+ OICFree(val->next);
+ val->next = NULL;
+ return NULL;
+ }
val->next->type =type;
return val->next;
}
val = val->next;
}
- OC_LOG(ERROR, TAG, PCF("FindAndSetValue reached point after while loop, pointer corruption?"));
+ OIC_LOG(ERROR, TAG, "FindAndSetValue reached point after while loop, pointer corruption?");
return NULL;
}
bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
{
- if(!payload || !resourceType)
+ if (!payload || !resourceType)
{
return false;
}
- if(payload->types)
+ if (payload->types)
{
OCStringLL* cur = payload->types;
while(cur->next)
}
cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
- if(!cur->next)
+ if (!cur->next)
{
return false;
}
else
{
payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
- if(!payload->types)
+ if (!payload->types)
{
return false;
}
bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
{
- if(!payload || !interface)
+ if (!payload || !interface)
{
return false;
}
- if(payload->interfaces)
+ if (payload->interfaces)
{
OCStringLL* cur = payload->interfaces;
while(cur->next)
}
cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
- if(!cur->next)
+ if (!cur->next)
{
return false;
}
else
{
payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
- if(!payload->interfaces)
+ if (!payload->interfaces)
{
return false;
}
bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
{
- if(!payload)
+ if (!payload)
{
return false;
}
-
+ OICFree(payload->uri);
payload->uri = OICStrdup(uri);
return payload->uri != NULL;
}
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val)
+ if (!val)
{
return false;
}
return val->type == OCREP_PROP_NULL;
}
+static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
+ void* value, OCRepPayloadPropType type)
+{
+ OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
+ if (!val)
+ {
+ return false;
+ }
+ switch(type)
+ {
+ case OCREP_PROP_INT:
+ val->i = *(int64_t*)value;
+ break;
+ case OCREP_PROP_DOUBLE:
+ val->d = *(double*)value;
+ break;
+ case OCREP_PROP_BOOL:
+ val->b = *(bool*)value;
+ break;
+ case OCREP_PROP_OBJECT:
+ val->obj = (OCRepPayload*)value;
+ break;
+ case OCREP_PROP_STRING:
+ val->str = (char*)value;
+ return val->str != NULL;
+ case OCREP_PROP_BYTE_STRING:
+ val->ocByteStr = *(OCByteString*)value;
+ break;
+ case OCREP_PROP_NULL:
+ return val != NULL;
+ case OCREP_PROP_ARRAY:
+ default:
+ return false;
+ }
+
+ return true;
+}
+
bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
{
- OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_NULL);
- return val != NULL;
+ return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
}
bool OCRepPayloadSetPropInt(OCRepPayload* payload,
const char* name, int64_t value)
{
- OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_INT);
-
- if(!val)
- {
- return false;
- }
-
- val->i = value;
- return true;
+ return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
}
bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_INT)
+ if (!val || val->type != OCREP_PROP_INT)
{
return false;
}
}
bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
- const char* name, double value)
+ const char* name, double value)
{
- OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_DOUBLE);
-
- if(!val )
- {
- return false;
- }
-
- val->d = value;
- return true;
+ return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
}
bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_DOUBLE)
+ if (val)
{
- return false;
+ if (val->type == OCREP_PROP_DOUBLE)
+ {
+ *value = val->d;
+ return true;
+ }
+ else if (val->type == OCREP_PROP_INT)
+ {
+ *value = val->i;
+ return true;
+ }
}
- *value = val->d;
- return true;
+ return false;
}
bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
char* temp = OICStrdup(value);
bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
- if(!b)
+ if (!b)
{
OICFree(temp);
}
bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
{
- OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_STRING);
+ return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
+}
+
+bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
+{
+ OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val)
+ if (!val || val->type != OCREP_PROP_STRING)
{
return false;
}
- val->str = value;
- return val->str != NULL;
+ *value = OICStrdup(val->str);
+ return *value != NULL;
}
-bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, const char** value)
+bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value)
{
- OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
+ if (!value.bytes || !value.len)
+ {
+ return false;
+ }
- if(!val || val->type != OCREP_PROP_STRING)
+ OCByteString ocByteStr = {
+ .bytes = (uint8_t*)OICMalloc(value.len * sizeof(uint8_t)),
+ .len = value.len };
+
+ if (!ocByteStr.bytes)
{
return false;
}
+ memcpy(ocByteStr.bytes, value.bytes, ocByteStr.len);
- *value = OICStrdup(val->str);
- return *value != NULL;
+ bool b = OCRepPayloadSetPropByteStringAsOwner(payload, name, &ocByteStr);
+
+ if (!b)
+ {
+ OICFree(ocByteStr.bytes);
+ }
+ return b;
}
-bool OCRepPayloadSetPropBool(OCRepPayload* payload,
- const char* name, bool value)
+bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name, OCByteString* value)
+{
+ return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_BYTE_STRING);
+}
+
+bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name, OCByteString* value)
{
- OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_BOOL);
+ OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
+
+ if (!val || val->type != OCREP_PROP_BYTE_STRING)
+ {
+ return false;
+ }
+
+ if (!value)
+ {
+ return false;
+ }
- if(!val)
+ value->bytes = (uint8_t*)OICMalloc(val->ocByteStr.len * sizeof(uint8_t));
+ if (!value->bytes)
{
return false;
}
+ value->len = val->ocByteStr.len;
+ memcpy(value->bytes, val->ocByteStr.bytes, value->len);
- val->b = value;
return true;
}
+bool OCRepPayloadSetPropBool(OCRepPayload* payload,
+ const char* name, bool value)
+{
+ return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
+}
+
bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_BOOL)
+ if (!val || val->type != OCREP_PROP_BOOL)
{
return false;
}
OCRepPayload* temp = OCRepPayloadClone(value);
bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
- if(!b)
+ if (!b)
{
OCRepPayloadDestroy(temp);
}
bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
{
- OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_OBJECT);
-
- if(!val)
- {
- return false;
- }
-
- val->obj = value;
- return true;
+ return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
}
bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_OBJECT)
+ if (!val || val->type != OCREP_PROP_OBJECT)
{
return false;
}
size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
- if(dimensions[0] == 0)
+ if (dimensions[0] == 0)
{
return 0;
}
return total;
}
+
+bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
+ OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
+{
+ OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
+
+ if (!val)
+ {
+ return false;
+ }
+
+ val->arr.type = OCREP_PROP_BYTE_STRING;
+ memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
+ val->arr.ocByteStrArray = array;
+
+ return true;
+}
+
+bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
+ const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
+{
+ if (!array)
+ {
+ return NULL;
+ }
+
+ size_t dimTotal = calcDimTotal(dimensions);
+ if (dimTotal == 0)
+ {
+ return false;
+ }
+
+ OCByteString* newArray = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
+
+ if (!newArray)
+ {
+ return false;
+ }
+
+ for (size_t i = 0; i < dimTotal; ++i)
+ {
+ newArray[i].bytes = (uint8_t*)OICMalloc(array[i].len * sizeof(uint8_t));
+ if (NULL == newArray[i].bytes)
+ {
+ for (size_t j = 0; j < i; ++j)
+ {
+ OICFree(newArray[j].bytes);
+ }
+
+ OICFree(newArray);
+ return false;
+ }
+ newArray[i].len = array[i].len;
+ memcpy(newArray[i].bytes, array[i].bytes, newArray[i].len);
+ }
+
+ bool b = OCRepPayloadSetByteStringArrayAsOwner(payload, name, newArray, dimensions);
+ if (!b)
+ {
+ for (size_t i = 0; i < dimTotal; ++i)
+ {
+ OICFree(newArray[i].bytes);
+ }
+
+ OICFree(newArray);
+ }
+ return b;
+}
+
+bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
+ OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
+{
+ OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
+
+ if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BYTE_STRING
+ || !val->arr.ocByteStrArray)
+ {
+ return false;
+ }
+
+ size_t dimTotal = calcDimTotal(val->arr.dimensions);
+ if (dimTotal == 0)
+ {
+ return false;
+ }
+
+ *array = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
+ if (!*array)
+ {
+ return false;
+ }
+
+ for (size_t i = 0; i < dimTotal; ++i)
+ {
+ OCByteString* tmp = &(*array)[i];
+ tmp->bytes = (uint8_t*)OICMalloc(val->arr.ocByteStrArray[i].len * sizeof(uint8_t));
+ if (NULL == tmp->bytes)
+ {
+ for (size_t j = 0; j < i; ++j)
+ {
+ OCByteString* tmp = &(*array)[j];
+ OICFree(tmp->bytes);
+ }
+ OICFree(*array);
+ *array = NULL;
+
+ return false;
+ }
+ tmp->len = val->arr.ocByteStrArray[i].len;
+ memcpy(tmp->bytes, val->arr.ocByteStrArray[i].bytes, tmp->len);
+ }
+
+ memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
+ return true;
+}
+
+
bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
- if(!val)
+ if (!val)
{
return false;
}
const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
size_t dimTotal = calcDimTotal(dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
- if(!newArray)
+ if (!newArray)
{
return false;
}
bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
- if(!b)
+ if (!b)
{
OICFree(newArray);
}
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
+ if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
|| !val->arr.iArray)
{
return false;
}
size_t dimTotal = calcDimTotal(val->arr.dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
*array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
- if(!*array)
+ if (!*array)
{
return false;
}
{
OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
- if(!val)
+ if (!val)
{
return false;
}
const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
size_t dimTotal = calcDimTotal(dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
- if(!newArray)
+ if (!newArray)
{
return false;
}
memcpy(newArray, array, dimTotal * sizeof(double));
bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
- if(!b)
+ if (!b)
{
OICFree(newArray);
}
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_DOUBLE
+ if (!val || val->type != OCREP_PROP_ARRAY ||
+ (val->arr.type != OCREP_PROP_DOUBLE && val->arr.type != OCREP_PROP_INT)
|| !val->arr.dArray)
{
return false;
}
size_t dimTotal = calcDimTotal(val->arr.dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
*array = (double*)OICMalloc(dimTotal * sizeof(double));
- if(!*array)
+ if (!*array)
{
return false;
}
- memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
+ if (val->arr.type != OCREP_PROP_DOUBLE)
+ {
+ memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
+ }
+ else
+ {
+ /* need to convert from integer */
+ size_t n = 0;
+ for ( ; n < dimTotal; ++n)
+ {
+ (*array)[n] = val->arr.iArray[n];
+ }
+ }
memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
return true;
}
{
OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
- if(!val)
+ if (!val)
{
return false;
}
const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
size_t dimTotal = calcDimTotal(dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
- if(!newArray)
+ if (!newArray)
{
return false;
}
bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
- if(!b)
+ if (!b)
{
for(size_t i = 0; i < dimTotal; ++i)
{
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
+ if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
|| !val->arr.strArray)
{
return false;
}
size_t dimTotal = calcDimTotal(val->arr.dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
*array = (char**)OICMalloc(dimTotal * sizeof(char*));
- if(!*array)
+ if (!*array)
{
return false;
}
OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
- if(!val)
+ if (!val)
{
return false;
}
const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
size_t dimTotal = calcDimTotal(dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
- if(!newArray)
+ if (!newArray)
{
return false;
}
bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
- if(!b)
+ if (!b)
{
OICFree(newArray);
}
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
+ if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
|| !val->arr.bArray)
{
return false;
}
size_t dimTotal = calcDimTotal(val->arr.dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
*array = (bool*)OICMalloc(dimTotal * sizeof(bool));
- if(!*array)
+ if (!*array)
{
return false;
}
{
OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
- if(!val)
+ if (!val)
{
return false;
}
const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
{
size_t dimTotal = calcDimTotal(dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
- if(!newArray)
+ if (!newArray)
{
return false;
}
bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
- if(!b)
+ if (!b)
{
for(size_t i = 0; i < dimTotal; ++i)
{
{
OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
- if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
+ if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
|| !val->arr.objArray)
{
return false;
}
size_t dimTotal = calcDimTotal(val->arr.dimensions);
- if(dimTotal == 0)
+ if (dimTotal == 0)
{
return false;
}
*array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
- if(!*array)
+ if (!*array)
{
return false;
}
void OCFreeOCStringLL(OCStringLL* ll)
{
- if(!ll)
+ if (!ll)
{
return;
}
void OCRepPayloadDestroy(OCRepPayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
{
OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
return payload;
}
-OCSecurityPayload* OCSecurityPayloadCreate(char* securityData)
+OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size)
{
OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
payload->base.type = PAYLOAD_TYPE_SECURITY;
- payload->securityData = OICStrdup(securityData);
+ payload->securityData = (uint8_t *)OICCalloc(1, size);
+ if (!payload->securityData)
+ {
+ OICFree(payload);
+ return NULL;
+ }
+ memcpy(payload->securityData, (uint8_t *)securityData, size);
+ payload->payloadSize = size;
return payload;
}
void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
OCResourcePayload* p = payload->resources;
while(p)
{
- if(i == index)
+ if (i == index)
{
return p;
}
static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t port)
{
OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
- if(!pl)
+ if (!pl)
{
return NULL;
}
pl->uri = OICStrdup(res->uri);
- pl->sid = (uint8_t*)OICCalloc(1, UUID_SIZE);
- memcpy(pl->sid, OCGetServerInstanceID(), UUID_SIZE);
+
+ if (!pl->uri)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
// types
OCResourceType* typePtr = res->rsrcType;
- if(typePtr != NULL)
+ if (typePtr != NULL)
{
pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
+ if (!pl->types)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
pl->types->value = OICStrdup(typePtr->resourcetypename);
+ if (!pl->types->value)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
OCStringLL* cur = pl->types;
typePtr = typePtr->next;
while(typePtr)
{
cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
+ if (!cur->next)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
cur->next->value = OICStrdup(typePtr->resourcetypename);
+ if (!cur->next->value)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
cur = cur->next;
typePtr = typePtr->next;
}
// interfaces
OCResourceInterface* ifPtr = res->rsrcInterface;
- if(ifPtr != NULL)
+ if (ifPtr != NULL)
{
pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
+ if (!pl->interfaces)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
pl->interfaces->value = OICStrdup(ifPtr->name);
+ if (!pl->interfaces->value)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
- OCStringLL* cur = pl->types;
+ OCStringLL* cur = pl->interfaces;
ifPtr = ifPtr->next;
- while(ifPtr)
+ while(ifPtr && cur)
{
cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
+ if (!cur->next)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
cur->next->value = OICStrdup(ifPtr->name);
+ if (!cur->next->value)
+ {
+ OCDiscoveryResourceDestroy(pl);
+ return NULL;
+ }
cur = cur->next;
ifPtr = ifPtr->next;
}
OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, port));
}
+bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, const char *value)
+{
+ char *dup = OICStrdup(value);
+ VERIFY_PARAM_NON_NULL(TAG, dup, "Failed copying string");
+ VERIFY_PARAM_NON_NULL(TAG, value, "Invalid Parameters");
+
+ if (!*stringLL)
+ {
+ *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ VERIFY_PARAM_NON_NULL(TAG, *stringLL, "Failed allocating memory");
+ (*stringLL)->value = dup;
+ return true;
+ }
+ else
+ {
+ OCStringLL *temp = *stringLL;
+ while(temp->next)
+ {
+ temp = temp->next;
+ }
+ temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ VERIFY_PARAM_NON_NULL(TAG, temp->next, "Failed allocating memory");
+ temp->next->value = dup;
+ return true;
+ }
+exit:
+ OICFree(dup);
+ return false;
+}
+
void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
{
- if(!payload->resources)
+ if (!payload)
+ {
+ return;
+ }
+
+ if (!payload->resources)
{
payload->resources = res;
}
}
}
-void FreeOCDiscoveryResource(OCResourcePayload* payload)
+void OCDiscoveryResourceDestroy(OCResourcePayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
OICFree(payload->uri);
- OICFree(payload->sid);
OCFreeOCStringLL(payload->types);
OCFreeOCStringLL(payload->interfaces);
- FreeOCDiscoveryResource(payload->next);
+ OCDiscoveryResourceDestroy(payload->next);
OICFree(payload);
}
void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
-
- FreeOCDiscoveryResource(payload->resources);
+ OICFree(payload->sid);
+ OICFree(payload->baseURI);
+ OICFree(payload->uri);
+ OICFree(payload->type);
+ OICFree(payload->name);
+ OCFreeOCStringLL(payload->interface);
+ OCDiscoveryResourceDestroy(payload->resources);
OICFree(payload);
}
-OCDevicePayload* OCDevicePayloadCreate(const char* uri, const uint8_t* sid, const char* dname,
- const char* specVer, const char* dmVer)
+OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
+ const OCStringLL *types, const char* specVer, const char* dmVer)
{
OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
payload->base.type = PAYLOAD_TYPE_DEVICE;
-
- payload->uri = OICStrdup(uri);
- if(uri && !payload->uri)
+ payload->sid = OICStrdup(sid);
+ if (sid && !payload->sid)
{
goto exit;
}
- if(sid)
- {
- payload->sid = (uint8_t*)OICMalloc(UUID_SIZE);
- if(!payload->sid)
- {
- goto exit;
- }
- memcpy(payload->sid, sid, UUID_SIZE);
- }
-
payload->deviceName = OICStrdup(dname);
- if(dname && !payload->deviceName)
+ if (dname && !payload->deviceName)
{
goto exit;
}
payload->specVersion = OICStrdup(specVer);
- if(specVer && !payload->specVersion)
+ if (specVer && !payload->specVersion)
{
goto exit;
}
payload->dataModelVersion = OICStrdup(dmVer);
- if(dmVer && !payload->dataModelVersion)
+ if (dmVer && !payload->dataModelVersion)
+ {
+ goto exit;
+ }
+
+ payload->types = CloneOCStringLL((OCStringLL *)types);
+ if (types && !payload->types)
{
goto exit;
}
void OCDevicePayloadDestroy(OCDevicePayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
- OICFree(payload->uri);
OICFree(payload->sid);
OICFree(payload->deviceName);
OICFree(payload->specVersion);
OICFree(payload->dataModelVersion);
+ OCFreeOCStringLL(payload->types);
OICFree(payload);
}
static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
{
+ if (!platformInfo || !target)
+ {
+ return;
+ }
+
target->info.platformID = OICStrdup(platformInfo->platformID);
target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
target->info.systemTime = OICStrdup(platformInfo->systemTime);
}
-OCPlatformPayload* OCPlatformPayloadCreateAsOwner(char* uri, OCPlatformInfo* platformInfo)
+OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo)
{
OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
payload->base.type = PAYLOAD_TYPE_PLATFORM;
- payload->uri = uri;
+
+ payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
+ if (!payload->interfaces)
+ {
+ return NULL;
+ }
+ payload->interfaces->value = OICStrdup(OC_RSRVD_INTERFACE_READ);
+ payload->rt = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
payload->info = *platformInfo;
return payload;
}
-OCPlatformPayload* OCPlatformPayloadCreate(const char* uri, const OCPlatformInfo* platformInfo)
+OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo)
{
OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
payload->base.type = PAYLOAD_TYPE_PLATFORM;
- payload->uri = OICStrdup(uri);
+
+ payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
+ if (!payload->interfaces)
+ {
+ return NULL;
+ }
+ payload->interfaces->value = OICStrdup(OC_RSRVD_INTERFACE_READ);
+ payload->rt = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
OCCopyPlatformInfo(platformInfo, payload);
return payload;
}
+void OCPlatformInfoDestroy(OCPlatformInfo *info)
+{
+ OICFree(info->platformID);
+ OICFree(info->manufacturerName);
+ OICFree(info->manufacturerUrl);
+ OICFree(info->modelNumber);
+ OICFree(info->dateOfManufacture);
+ OICFree(info->platformVersion);
+ OICFree(info->operatingSystemVersion);
+ OICFree(info->hardwareVersion);
+ OICFree(info->firmwareVersion);
+ OICFree(info->supportUrl);
+ OICFree(info->systemTime);
+}
+
void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}
OICFree(payload->uri);
- OICFree(payload->info.platformID);
- OICFree(payload->info.manufacturerName);
- OICFree(payload->info.manufacturerUrl);
- OICFree(payload->info.modelNumber);
- OICFree(payload->info.dateOfManufacture);
- OICFree(payload->info.platformVersion);
- OICFree(payload->info.operatingSystemVersion);
- OICFree(payload->info.hardwareVersion);
- OICFree(payload->info.firmwareVersion);
- OICFree(payload->info.supportUrl);
- OICFree(payload->info.systemTime);
+ OCPlatformInfoDestroy(&payload->info);
+ OICFree(payload->rt);
+ OCFreeOCStringLL(payload->interfaces);
OICFree(payload);
}
OCPresenceTrigger trigger, const char* resourceType)
{
OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
- if(!payload)
+ if (!payload)
{
return NULL;
}
void OCPresencePayloadDestroy(OCPresencePayload* payload)
{
- if(!payload)
+ if (!payload)
{
return;
}