1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "ocpayload.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27 #include "ocstackinternal.h"
28 #include "ocresource.h"
30 #include "rdpayload.h"
32 #define TAG "OIC_RI_PAYLOAD"
34 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val);
36 void OCPayloadDestroy(OCPayload* payload)
45 case PAYLOAD_TYPE_REPRESENTATION:
46 OCRepPayloadDestroy((OCRepPayload*)payload);
48 case PAYLOAD_TYPE_DISCOVERY:
49 OCDiscoveryPayloadDestroy((OCDiscoveryPayload*)payload);
51 case PAYLOAD_TYPE_DEVICE:
52 OCDevicePayloadDestroy((OCDevicePayload*)payload);
54 case PAYLOAD_TYPE_PLATFORM:
55 OCPlatformPayloadDestroy((OCPlatformPayload*)payload);
57 case PAYLOAD_TYPE_PRESENCE:
58 OCPresencePayloadDestroy((OCPresencePayload*)payload);
60 case PAYLOAD_TYPE_SECURITY:
61 OCSecurityPayloadDestroy((OCSecurityPayload*)payload);
64 OCRDPayloadDestroy((OCRDPayload*)payload);
67 OIC_LOG_V(ERROR, TAG, "Unsupported payload type in destroy: %d", payload->type);
72 OCRepPayload* OCRepPayloadCreate()
74 OCRepPayload* payload = (OCRepPayload*)OICCalloc(1, sizeof(OCRepPayload));
81 payload->base.type = PAYLOAD_TYPE_REPRESENTATION;
86 void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child)
95 parent = parent->next;
102 static OCRepPayloadValue* OCRepPayloadFindValue(const OCRepPayload* payload, const char* name)
104 if (!payload || !name)
109 OCRepPayloadValue* val = payload->values;
112 if (0 == strcmp(val->name, name))
122 static void OCCopyPropertyValueArray(OCRepPayloadValue* dest, OCRepPayloadValue* source)
124 if (!dest || !source)
129 size_t dimTotal = calcDimTotal(source->arr.dimensions);
130 switch(source->arr.type)
133 dest->arr.iArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
134 memcpy(dest->arr.iArray, source->arr.iArray, dimTotal * sizeof(int64_t));
136 case OCREP_PROP_DOUBLE:
137 dest->arr.dArray = (double*)OICMalloc(dimTotal * sizeof(double));
138 memcpy(dest->arr.dArray, source->arr.dArray, dimTotal * sizeof(double));
140 case OCREP_PROP_BOOL:
141 dest->arr.bArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
142 memcpy(dest->arr.bArray, source->arr.bArray, dimTotal * sizeof(bool));
144 case OCREP_PROP_STRING:
145 dest->arr.strArray = (char**)OICMalloc(dimTotal * sizeof(char*));
146 for(size_t i = 0; i < dimTotal; ++i)
148 dest->arr.strArray[i] = OICStrdup(source->arr.strArray[i]);
151 case OCREP_PROP_OBJECT:
152 dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
153 for(size_t i = 0; i < dimTotal; ++i)
155 dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
158 case OCREP_PROP_ARRAY:
159 dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
160 for(size_t i = 0; i < dimTotal; ++i)
162 dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
166 OIC_LOG(ERROR, TAG, "CopyPropertyValueArray invalid type");
171 static void OCCopyPropertyValue (OCRepPayloadValue *dest, OCRepPayloadValue *source)
173 if (!source || !dest)
180 case OCREP_PROP_STRING:
181 dest->str = OICStrdup(source->str);
183 case OCREP_PROP_OBJECT:
184 dest->obj = OCRepPayloadClone(source->obj);
186 case OCREP_PROP_ARRAY:
187 OCCopyPropertyValueArray(dest, source);
190 // Nothing to do for the trivially copyable types.
195 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
202 if (val->type == OCREP_PROP_STRING)
206 else if (val->type == OCREP_PROP_BYTE_STRING)
208 OICFree(val->ocByteStr.bytes);
210 else if (val->type == OCREP_PROP_OBJECT)
212 OCRepPayloadDestroy(val->obj);
214 else if (val->type == OCREP_PROP_ARRAY)
216 size_t dimTotal = calcDimTotal(val->arr.dimensions);
217 switch(val->arr.type)
220 case OCREP_PROP_DOUBLE:
221 case OCREP_PROP_BOOL:
222 // Since this is a union, iArray will
223 // point to all of the above
224 OICFree(val->arr.iArray);
226 case OCREP_PROP_STRING:
227 for(size_t i = 0; i< dimTotal; ++i)
229 OICFree(val->arr.strArray[i]);
231 OICFree(val->arr.strArray);
233 case OCREP_PROP_BYTE_STRING:
234 for (size_t i = 0; i< dimTotal; ++i)
236 OICFree(val->arr.ocByteStrArray[i].bytes);
238 OICFree(val->arr.ocByteStrArray);
240 case OCREP_PROP_OBJECT: // This case is the temporary fix for string input
241 for(size_t i = 0; i< dimTotal; ++i)
243 OCRepPayloadDestroy(val->arr.objArray[i]);
245 OICFree(val->arr.objArray);
247 case OCREP_PROP_NULL:
248 case OCREP_PROP_ARRAY:
249 OIC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
250 inside an array: %d", val->arr.type);
256 static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
264 OCFreeRepPayloadValueContents(val);
265 OCFreeRepPayloadValue(val->next);
268 static OCRepPayloadValue* OCRepPayloadValueClone (OCRepPayloadValue* source)
275 OCRepPayloadValue *sourceIter = source;
276 OCRepPayloadValue *destIter = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
282 OCRepPayloadValue *headOfClone = destIter;
284 // Copy payload type and non pointer types in union.
285 *destIter = *sourceIter;
286 destIter->name = OICStrdup (sourceIter->name);
287 OCCopyPropertyValue (destIter, sourceIter);
289 sourceIter = sourceIter->next;
293 destIter->next = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
296 OCFreeRepPayloadValue (headOfClone);
300 *(destIter->next) = *sourceIter;
301 destIter->next->name = OICStrdup (sourceIter->name);
302 OCCopyPropertyValue (destIter->next, sourceIter);
304 sourceIter = sourceIter->next;
305 destIter = destIter->next;
310 static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
311 OCRepPayloadPropType type)
313 if (!payload || !name)
318 OCRepPayloadValue* val = payload->values;
321 payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
322 if (!payload->values)
326 payload->values->name = OICStrdup(name);
327 if (!payload->values->name)
329 OICFree(payload->values);
330 payload->values = NULL;
333 payload->values->type =type;
334 return payload->values;
339 if (0 == strcmp(val->name, name))
341 OCFreeRepPayloadValueContents(val);
345 else if (val->next == NULL)
347 val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
352 val->next->name = OICStrdup(name);
353 if (!val->next->name)
359 val->next->type =type;
366 OIC_LOG(ERROR, TAG, "FindAndSetValue reached point after while loop, pointer corruption?");
370 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType)
372 return OCRepPayloadAddResourceTypeAsOwner(payload, OICStrdup(resourceType));
375 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
377 if (!payload || !resourceType)
384 OCStringLL* cur = payload->types;
389 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
396 cur->next->value = resourceType;
401 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
406 payload->types->value = resourceType;
411 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* interface)
413 return OCRepPayloadAddInterfaceAsOwner(payload, OICStrdup(interface));
416 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
418 if (!payload || !interface)
423 if (payload->interfaces)
425 OCStringLL* cur = payload->interfaces;
430 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
436 cur->next->value = interface;
441 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
442 if (!payload->interfaces)
446 payload->interfaces->value = interface;
451 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
457 OICFree(payload->uri);
458 payload->uri = OICStrdup(uri);
459 return payload->uri != NULL;
462 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name)
464 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
471 return val->type == OCREP_PROP_NULL;
474 static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
475 void* value, OCRepPayloadPropType type)
477 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
485 val->i = *(int64_t*)value;
487 case OCREP_PROP_DOUBLE:
488 val->d = *(double*)value;
490 case OCREP_PROP_BOOL:
491 val->b = *(bool*)value;
493 case OCREP_PROP_OBJECT:
494 val->obj = (OCRepPayload*)value;
496 case OCREP_PROP_STRING:
497 val->str = (char*)value;
498 return val->str != NULL;
499 case OCREP_PROP_BYTE_STRING:
500 val->ocByteStr = *(OCByteString*)value;
502 case OCREP_PROP_NULL:
504 case OCREP_PROP_ARRAY:
512 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
514 return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
517 bool OCRepPayloadSetPropInt(OCRepPayload* payload,
518 const char* name, int64_t value)
520 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
523 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
525 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
527 if (!val || val->type != OCREP_PROP_INT)
536 bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
537 const char* name, double value)
539 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
542 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
544 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
546 if (!val || val->type != OCREP_PROP_DOUBLE)
555 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
557 char* temp = OICStrdup(value);
558 bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
567 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
569 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
572 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
574 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
576 if (!val || val->type != OCREP_PROP_STRING)
581 *value = OICStrdup(val->str);
582 return *value != NULL;
585 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value)
587 if (!value.bytes || !value.len)
592 OCByteString ocByteStr = {
593 .bytes = (uint8_t*)OICMalloc(value.len * sizeof(uint8_t)),
596 if (!ocByteStr.bytes)
600 memcpy(ocByteStr.bytes, value.bytes, ocByteStr.len);
602 bool b = OCRepPayloadSetPropByteStringAsOwner(payload, name, &ocByteStr);
606 OICFree(ocByteStr.bytes);
611 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name, OCByteString* value)
613 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_BYTE_STRING);
616 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name, OCByteString* value)
618 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
620 if (!val || val->type != OCREP_PROP_BYTE_STRING)
630 value->bytes = (uint8_t*)OICMalloc(val->ocByteStr.len * sizeof(uint8_t));
635 value->len = val->ocByteStr.len;
636 memcpy(value->bytes, val->ocByteStr.bytes, value->len);
641 bool OCRepPayloadSetPropBool(OCRepPayload* payload,
642 const char* name, bool value)
644 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
647 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
649 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
651 if (!val || val->type != OCREP_PROP_BOOL)
660 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value)
662 OCRepPayload* temp = OCRepPayloadClone(value);
663 bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
667 OCRepPayloadDestroy(temp);
672 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
674 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
677 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
679 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
681 if (!val || val->type != OCREP_PROP_OBJECT)
686 *value = OCRepPayloadClone(val->obj);
687 return *value != NULL;
690 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
692 if (dimensions[0] == 0)
698 for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
700 total *= dimensions[i];
706 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
707 OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
709 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
716 val->arr.type = OCREP_PROP_BYTE_STRING;
717 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
718 val->arr.ocByteStrArray = array;
723 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
724 const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
731 size_t dimTotal = calcDimTotal(dimensions);
737 OCByteString* newArray = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
744 for (size_t i = 0; i < dimTotal; ++i)
746 newArray[i].bytes = (uint8_t*)OICMalloc(array[i].len * sizeof(uint8_t));
747 if (NULL == newArray[i].bytes)
749 for (size_t j = 0; j < i; ++j)
751 OICFree(newArray[j].bytes);
757 newArray[i].len = array[i].len;
758 memcpy(newArray[i].bytes, array[i].bytes, newArray[i].len);
761 bool b = OCRepPayloadSetByteStringArrayAsOwner(payload, name, newArray, dimensions);
764 for (size_t i = 0; i < dimTotal; ++i)
766 OICFree(newArray[i].bytes);
774 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
775 OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
777 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
779 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BYTE_STRING
780 || !val->arr.ocByteStrArray)
785 size_t dimTotal = calcDimTotal(val->arr.dimensions);
791 *array = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
797 for (size_t i = 0; i < dimTotal; ++i)
799 OCByteString* tmp = &(*array)[i];
800 tmp->bytes = (uint8_t*)OICMalloc(val->arr.ocByteStrArray[i].len * sizeof(uint8_t));
801 if (NULL == tmp->bytes)
803 for (size_t j = 0; j < i; ++j)
805 OCByteString* tmp = &(*array)[j];
813 tmp->len = val->arr.ocByteStrArray[i].len;
814 memcpy(tmp->bytes, val->arr.ocByteStrArray[i].bytes, tmp->len);
817 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
822 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
823 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
825 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
832 val->arr.type = OCREP_PROP_INT;
833 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
834 val->arr.iArray = array;
839 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
840 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
842 size_t dimTotal = calcDimTotal(dimensions);
848 int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
855 memcpy(newArray, array, dimTotal * sizeof(int64_t));
858 bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
866 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
867 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
869 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
871 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
877 size_t dimTotal = calcDimTotal(val->arr.dimensions);
882 *array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
888 memcpy(*array, val->arr.iArray, dimTotal * sizeof(int64_t));
889 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
893 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
894 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
896 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
903 val->arr.type = OCREP_PROP_DOUBLE;
904 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
905 val->arr.dArray = array;
909 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
910 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
912 size_t dimTotal = calcDimTotal(dimensions);
918 double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
925 memcpy(newArray, array, dimTotal * sizeof(double));
927 bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
935 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
936 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
938 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
940 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_DOUBLE
946 size_t dimTotal = calcDimTotal(val->arr.dimensions);
951 *array = (double*)OICMalloc(dimTotal * sizeof(double));
957 memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
958 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
962 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
963 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
965 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
972 val->arr.type = OCREP_PROP_STRING;
973 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
974 val->arr.strArray = array;
978 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
979 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
981 size_t dimTotal = calcDimTotal(dimensions);
987 char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
994 for(size_t i = 0; i < dimTotal; ++i)
996 newArray[i] = OICStrdup(array[i]);
999 bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
1003 for(size_t i = 0; i < dimTotal; ++i)
1005 OICFree(newArray[i]);
1012 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
1013 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1015 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1017 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
1018 || !val->arr.strArray)
1023 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1028 *array = (char**)OICMalloc(dimTotal * sizeof(char*));
1034 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1036 for(size_t i = 0; i < dimTotal; ++i)
1038 (*array)[i] = OICStrdup(val->arr.strArray[i]);
1045 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
1046 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1049 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1056 val->arr.type = OCREP_PROP_BOOL;
1057 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1058 val->arr.bArray = array;
1062 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
1063 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1065 size_t dimTotal = calcDimTotal(dimensions);
1071 bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
1078 memcpy(newArray, array, dimTotal * sizeof(bool));
1081 bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
1089 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
1090 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1092 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1094 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
1095 || !val->arr.bArray)
1100 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1105 *array = (bool*)OICMalloc(dimTotal * sizeof(bool));
1111 memcpy(*array, val->arr.bArray, dimTotal * sizeof(bool));
1112 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1116 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
1117 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1119 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1126 val->arr.type = OCREP_PROP_OBJECT;
1127 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1128 val->arr.objArray = array;
1133 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
1134 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1136 size_t dimTotal = calcDimTotal(dimensions);
1142 OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1149 for(size_t i = 0; i < dimTotal; ++i)
1151 newArray[i] = OCRepPayloadClone(array[i]);
1154 bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
1158 for(size_t i = 0; i < dimTotal; ++i)
1160 OCRepPayloadDestroy(newArray[i]);
1167 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
1168 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1170 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1172 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
1173 || !val->arr.objArray)
1178 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1183 *array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1189 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1191 for(size_t i = 0; i < dimTotal; ++i)
1193 (*array)[i] = OCRepPayloadClone(val->arr.objArray[i]);
1199 void OCFreeOCStringLL(OCStringLL* ll)
1206 OCFreeOCStringLL(ll->next);
1211 OCStringLL* CloneOCStringLL (OCStringLL* ll)
1218 OCStringLL *sourceIter = ll;
1220 OCStringLL *destIter = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1225 destIter->value = OICStrdup (sourceIter->value);
1227 OCStringLL *headOfClone = destIter;
1229 sourceIter = sourceIter->next;
1233 destIter->next = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1234 if (!destIter->next)
1236 OCFreeOCStringLL (headOfClone);
1239 destIter->next->value = OICStrdup (sourceIter->value);
1241 destIter = destIter->next;
1242 sourceIter = sourceIter->next;
1247 OCRepPayload* OCRepPayloadClone (const OCRepPayload* payload)
1254 OCRepPayload *clone = OCRepPayloadCreate();
1261 clone->uri = OICStrdup (payload->uri);
1262 clone->types = CloneOCStringLL (payload->types);
1263 clone->interfaces = CloneOCStringLL (payload->interfaces);
1264 clone->values = OCRepPayloadValueClone (payload->values);
1270 void OCRepPayloadDestroy(OCRepPayload* payload)
1277 OICFree(payload->uri);
1278 OCFreeOCStringLL(payload->types);
1279 OCFreeOCStringLL(payload->interfaces);
1280 OCFreeRepPayloadValue(payload->values);
1281 OCRepPayloadDestroy(payload->next);
1285 OCDiscoveryPayload* OCDiscoveryPayloadCreate()
1287 OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
1294 payload->base.type = PAYLOAD_TYPE_DISCOVERY;
1299 OCSecurityPayload* OCSecurityPayloadCreate(const char* securityData)
1301 OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1308 payload->base.type = PAYLOAD_TYPE_SECURITY;
1309 payload->securityData = OICStrdup(securityData);
1314 void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
1321 OICFree(payload->securityData);
1325 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload)
1328 OCResourcePayload* p = payload->resources;
1337 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index)
1340 OCResourcePayload* p = payload->resources;
1353 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t securePort,
1356 OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
1362 pl->uri = OICStrdup(res->uri);
1366 OCDiscoveryResourceDestroy(pl);
1371 OCResourceType* typePtr = res->rsrcType;
1373 if (typePtr != NULL)
1375 pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1378 OCDiscoveryResourceDestroy(pl);
1381 pl->types->value = OICStrdup(typePtr->resourcetypename);
1382 if (!pl->types->value)
1384 OCDiscoveryResourceDestroy(pl);
1388 OCStringLL* cur = pl->types;
1389 typePtr = typePtr->next;
1392 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1395 OCDiscoveryResourceDestroy(pl);
1398 cur->next->value = OICStrdup(typePtr->resourcetypename);
1399 if (!cur->next->value)
1401 OCDiscoveryResourceDestroy(pl);
1405 typePtr = typePtr->next;
1410 OCResourceInterface* ifPtr = res->rsrcInterface;
1413 pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1414 if (!pl->interfaces)
1416 OCDiscoveryResourceDestroy(pl);
1419 pl->interfaces->value = OICStrdup(ifPtr->name);
1420 if (!pl->interfaces->value)
1422 OCDiscoveryResourceDestroy(pl);
1426 OCStringLL* cur = pl->interfaces;
1427 ifPtr = ifPtr->next;
1430 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1433 OCDiscoveryResourceDestroy(pl);
1436 cur->next->value = OICStrdup(ifPtr->name);
1437 if (!cur->next->value)
1439 OCDiscoveryResourceDestroy(pl);
1443 ifPtr = ifPtr->next;
1447 pl->bitmap = res->resourceProperties & (OC_OBSERVABLE | OC_DISCOVERABLE);
1448 pl->secure = (res->resourceProperties & OC_SECURE) != 0;
1449 pl->port = securePort;
1451 pl->tcpPort = tcpPort;
1456 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1457 uint16_t securePort, uint16_t tcpPort)
1459 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, securePort, tcpPort));
1462 bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, const char *value)
1464 char *dup = OICStrdup(value);
1465 VERIFY_PARAM_NON_NULL(TAG, dup, "Failed copying string");
1466 VERIFY_PARAM_NON_NULL(TAG, value, "Invalid Parameters");
1470 *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1471 VERIFY_PARAM_NON_NULL(TAG, dup, "Failed allocating memory");
1472 (*stringLL)->value = dup;
1477 OCStringLL *temp = *stringLL;
1482 temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1483 VERIFY_PARAM_NON_NULL(TAG, temp->next, "Failed allocating memory");
1484 temp->next->value = dup;
1492 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
1499 if (!payload->resources)
1501 payload->resources = res;
1505 OCResourcePayload* p = payload->resources;
1514 void OCDiscoveryResourceDestroy(OCResourcePayload* payload)
1521 OICFree(payload->uri);
1522 OCFreeOCStringLL(payload->types);
1523 OCFreeOCStringLL(payload->interfaces);
1524 OCDiscoveryResourceDestroy(payload->next);
1528 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
1534 OICFree(payload->sid);
1535 OICFree(payload->baseURI);
1536 OICFree(payload->uri);
1537 OICFree(payload->type);
1538 OICFree(payload->name);
1539 OCFreeOCStringLL(payload->interface);
1540 OCDiscoveryResourceDestroy(payload->resources);
1544 OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
1545 const OCStringLL *types, const char* specVer, const char* dmVer)
1548 OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
1555 payload->base.type = PAYLOAD_TYPE_DEVICE;
1556 payload->sid = OICStrdup(sid);
1557 if (sid && !payload->sid)
1562 payload->deviceName = OICStrdup(dname);
1563 if (dname && !payload->deviceName)
1568 payload->specVersion = OICStrdup(specVer);
1569 if (specVer && !payload->specVersion)
1574 payload->dataModelVersion = OICStrdup(dmVer);
1575 if (dmVer && !payload->dataModelVersion)
1580 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_DEFAULT);
1581 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_READ);
1583 payload->types = CloneOCStringLL((OCStringLL *)types);
1584 if (types && !payload->types)
1592 OCDevicePayloadDestroy((OCDevicePayload*)payload);
1596 void OCDevicePayloadDestroy(OCDevicePayload* payload)
1603 OICFree(payload->sid);
1604 OICFree(payload->deviceName);
1605 OICFree(payload->specVersion);
1606 OICFree(payload->dataModelVersion);
1607 OCFreeOCStringLL(payload->types);
1608 OCFreeOCStringLL(payload->interfaces);
1612 static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
1614 if (!platformInfo || !target)
1619 target->info.platformID = OICStrdup(platformInfo->platformID);
1620 target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
1621 target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
1622 target->info.modelNumber = OICStrdup(platformInfo->modelNumber);
1623 target->info.dateOfManufacture = OICStrdup(platformInfo->dateOfManufacture);
1624 target->info.platformVersion = OICStrdup(platformInfo->platformVersion);
1625 target->info.operatingSystemVersion = OICStrdup(platformInfo->operatingSystemVersion);
1626 target->info.hardwareVersion = OICStrdup(platformInfo->hardwareVersion);
1627 target->info.firmwareVersion = OICStrdup(platformInfo->firmwareVersion);
1628 target->info.supportUrl = OICStrdup(platformInfo->supportUrl);
1629 target->info.systemTime = OICStrdup(platformInfo->systemTime);
1632 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo)
1634 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1640 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1642 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1643 if (!payload->interfaces)
1647 payload->interfaces->value = OICStrdup(OC_RSRVD_INTERFACE_READ);
1648 payload->rt = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
1649 payload->info = *platformInfo;
1654 OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo)
1656 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1663 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1664 payload->rt = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
1666 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_DEFAULT);
1667 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_READ);
1669 OCCopyPlatformInfo(platformInfo, payload);
1674 void OCPlatformInfoDestroy(OCPlatformInfo *info)
1676 OICFree(info->platformID);
1677 OICFree(info->manufacturerName);
1678 OICFree(info->manufacturerUrl);
1679 OICFree(info->modelNumber);
1680 OICFree(info->dateOfManufacture);
1681 OICFree(info->platformVersion);
1682 OICFree(info->operatingSystemVersion);
1683 OICFree(info->hardwareVersion);
1684 OICFree(info->firmwareVersion);
1685 OICFree(info->supportUrl);
1686 OICFree(info->systemTime);
1689 void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
1695 OICFree(payload->uri);
1696 OCPlatformInfoDestroy(&payload->info);
1697 OICFree(payload->rt);
1698 OCFreeOCStringLL(payload->interfaces);
1702 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
1703 OCPresenceTrigger trigger, const char* resourceType)
1705 OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
1711 payload->base.type = PAYLOAD_TYPE_PRESENCE;
1712 payload->sequenceNumber = seqNum;
1713 payload->maxAge = maxAge;
1714 payload->trigger = trigger;
1715 payload->resourceType = OICStrdup(resourceType);
1719 void OCPresencePayloadDestroy(OCPresencePayload* payload)
1725 OICFree(payload->resourceType);