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_ARRAY:
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]);
159 OIC_LOG(ERROR, TAG, "CopyPropertyValueArray invalid type");
164 static void OCCopyPropertyValue (OCRepPayloadValue *dest, OCRepPayloadValue *source)
166 if (!source || !dest)
173 case OCREP_PROP_STRING:
174 dest->str = OICStrdup(source->str);
176 case OCREP_PROP_OBJECT:
177 dest->obj = OCRepPayloadClone(source->obj);
179 case OCREP_PROP_ARRAY:
180 OCCopyPropertyValueArray(dest, source);
183 // Nothing to do for the trivially copyable types.
188 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
195 if (val->type == OCREP_PROP_STRING)
199 else if (val->type == OCREP_PROP_BYTE_STRING)
201 OICFree(val->ocByteStr.bytes);
203 else if (val->type == OCREP_PROP_OBJECT)
205 OCRepPayloadDestroy(val->obj);
207 else if (val->type == OCREP_PROP_ARRAY)
209 size_t dimTotal = calcDimTotal(val->arr.dimensions);
210 switch(val->arr.type)
213 case OCREP_PROP_DOUBLE:
214 case OCREP_PROP_BOOL:
215 // Since this is a union, iArray will
216 // point to all of the above
217 OICFree(val->arr.iArray);
219 case OCREP_PROP_STRING:
220 for(size_t i = 0; i< dimTotal; ++i)
222 OICFree(val->arr.strArray[i]);
224 OICFree(val->arr.strArray);
226 case OCREP_PROP_BYTE_STRING:
227 for (size_t i = 0; i< dimTotal; ++i)
229 OICFree(val->arr.ocByteStrArray[i].bytes);
231 OICFree(val->arr.ocByteStrArray);
233 case OCREP_PROP_OBJECT:
234 for(size_t i = 0; i< dimTotal; ++i)
236 OCRepPayloadDestroy(val->arr.objArray[i]);
238 OICFree(val->arr.objArray);
240 case OCREP_PROP_NULL:
241 case OCREP_PROP_ARRAY:
242 OIC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
243 inside an array: %d", val->arr.type);
249 static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
257 OCFreeRepPayloadValueContents(val);
258 OCFreeRepPayloadValue(val->next);
261 static OCRepPayloadValue* OCRepPayloadValueClone (OCRepPayloadValue* source)
268 OCRepPayloadValue *sourceIter = source;
269 OCRepPayloadValue *destIter = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
275 OCRepPayloadValue *headOfClone = destIter;
277 // Copy payload type and non pointer types in union.
278 *destIter = *sourceIter;
279 destIter->name = OICStrdup (sourceIter->name);
280 OCCopyPropertyValue (destIter, sourceIter);
282 sourceIter = sourceIter->next;
286 destIter->next = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
289 OCFreeRepPayloadValue (headOfClone);
293 *(destIter->next) = *sourceIter;
294 destIter->next->name = OICStrdup (sourceIter->name);
295 OCCopyPropertyValue (destIter->next, sourceIter);
297 sourceIter = sourceIter->next;
298 destIter = destIter->next;
303 static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
304 OCRepPayloadPropType type)
306 if (!payload || !name)
311 OCRepPayloadValue* val = payload->values;
314 payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
315 if (!payload->values)
319 payload->values->name = OICStrdup(name);
320 if (!payload->values->name)
322 OICFree(payload->values);
323 payload->values = NULL;
326 payload->values->type =type;
327 return payload->values;
332 if (0 == strcmp(val->name, name))
334 OCFreeRepPayloadValueContents(val);
338 else if (val->next == NULL)
340 val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
345 val->next->name = OICStrdup(name);
346 if (!val->next->name)
352 val->next->type =type;
359 OIC_LOG(ERROR, TAG, "FindAndSetValue reached point after while loop, pointer corruption?");
363 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType)
365 return OCRepPayloadAddResourceTypeAsOwner(payload, OICStrdup(resourceType));
368 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
370 if (!payload || !resourceType)
377 OCStringLL* cur = payload->types;
382 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
389 cur->next->value = resourceType;
394 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
399 payload->types->value = resourceType;
404 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* interface)
406 return OCRepPayloadAddInterfaceAsOwner(payload, OICStrdup(interface));
409 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
411 if (!payload || !interface)
416 if (payload->interfaces)
418 OCStringLL* cur = payload->interfaces;
423 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
429 cur->next->value = interface;
434 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
435 if (!payload->interfaces)
439 payload->interfaces->value = interface;
444 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
450 OICFree(payload->uri);
451 payload->uri = OICStrdup(uri);
452 return payload->uri != NULL;
455 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name)
457 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
464 return val->type == OCREP_PROP_NULL;
467 static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
468 void* value, OCRepPayloadPropType type)
470 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
478 val->i = *(int64_t*)value;
480 case OCREP_PROP_DOUBLE:
481 val->d = *(double*)value;
483 case OCREP_PROP_BOOL:
484 val->b = *(bool*)value;
486 case OCREP_PROP_OBJECT:
487 val->obj = (OCRepPayload*)value;
489 case OCREP_PROP_STRING:
490 val->str = (char*)value;
491 return val->str != NULL;
492 case OCREP_PROP_BYTE_STRING:
493 val->ocByteStr = *(OCByteString*)value;
495 case OCREP_PROP_NULL:
497 case OCREP_PROP_ARRAY:
505 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
507 return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
510 bool OCRepPayloadSetPropInt(OCRepPayload* payload,
511 const char* name, int64_t value)
513 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
516 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
518 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
520 if (!val || val->type != OCREP_PROP_INT)
529 bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
530 const char* name, double value)
532 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
535 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
537 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
539 if (!val || val->type != OCREP_PROP_DOUBLE)
548 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
550 char* temp = OICStrdup(value);
551 bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
560 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
562 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
565 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
567 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
569 if (!val || val->type != OCREP_PROP_STRING)
574 *value = OICStrdup(val->str);
575 return *value != NULL;
578 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value)
580 if (!value.bytes || !value.len)
585 OCByteString ocByteStr = {
586 .bytes = (uint8_t*)OICMalloc(value.len * sizeof(uint8_t)),
589 if (!ocByteStr.bytes)
593 memcpy(ocByteStr.bytes, value.bytes, ocByteStr.len);
595 bool b = OCRepPayloadSetPropByteStringAsOwner(payload, name, &ocByteStr);
599 OICFree(ocByteStr.bytes);
604 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name, OCByteString* value)
606 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_BYTE_STRING);
609 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name, OCByteString* value)
611 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
613 if (!val || val->type != OCREP_PROP_BYTE_STRING)
623 value->bytes = (uint8_t*)OICMalloc(val->ocByteStr.len * sizeof(uint8_t));
628 value->len = val->ocByteStr.len;
629 memcpy(value->bytes, val->ocByteStr.bytes, value->len);
634 bool OCRepPayloadSetPropBool(OCRepPayload* payload,
635 const char* name, bool value)
637 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
640 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
642 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
644 if (!val || val->type != OCREP_PROP_BOOL)
653 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value)
655 OCRepPayload* temp = OCRepPayloadClone(value);
656 bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
660 OCRepPayloadDestroy(temp);
665 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
667 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
670 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
672 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
674 if (!val || val->type != OCREP_PROP_OBJECT)
679 *value = OCRepPayloadClone(val->obj);
680 return *value != NULL;
683 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
685 if (dimensions[0] == 0)
691 for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
693 total *= dimensions[i];
699 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
700 OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
702 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
709 val->arr.type = OCREP_PROP_BYTE_STRING;
710 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
711 val->arr.ocByteStrArray = array;
716 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
717 const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
724 size_t dimTotal = calcDimTotal(dimensions);
730 OCByteString* newArray = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
737 for (size_t i = 0; i < dimTotal; ++i)
739 newArray[i].bytes = (uint8_t*)OICMalloc(array[i].len * sizeof(uint8_t));
740 if (NULL == newArray[i].bytes)
742 for (size_t j = 0; j < i; ++j)
744 OICFree(newArray[j].bytes);
750 newArray[i].len = array[i].len;
751 memcpy(newArray[i].bytes, array[i].bytes, newArray[i].len);
754 bool b = OCRepPayloadSetByteStringArrayAsOwner(payload, name, newArray, dimensions);
757 for (size_t i = 0; i < dimTotal; ++i)
759 OICFree(newArray[i].bytes);
767 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
768 OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
770 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
772 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BYTE_STRING
773 || !val->arr.ocByteStrArray)
778 size_t dimTotal = calcDimTotal(val->arr.dimensions);
784 *array = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
790 for (size_t i = 0; i < dimTotal; ++i)
792 OCByteString* tmp = &(*array)[i];
793 tmp->bytes = (uint8_t*)OICMalloc(val->arr.ocByteStrArray[i].len * sizeof(uint8_t));
794 if (NULL == tmp->bytes)
796 for (size_t j = 0; j < i; ++j)
798 OCByteString* tmp = &(*array)[j];
806 tmp->len = val->arr.ocByteStrArray[i].len;
807 memcpy(tmp->bytes, val->arr.ocByteStrArray[i].bytes, tmp->len);
810 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
815 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
816 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
818 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
825 val->arr.type = OCREP_PROP_INT;
826 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
827 val->arr.iArray = array;
832 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
833 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
835 size_t dimTotal = calcDimTotal(dimensions);
841 int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
848 memcpy(newArray, array, dimTotal * sizeof(int64_t));
851 bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
859 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
860 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
862 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
864 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
870 size_t dimTotal = calcDimTotal(val->arr.dimensions);
875 *array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
881 memcpy(*array, val->arr.iArray, dimTotal * sizeof(int64_t));
882 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
886 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
887 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
889 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
896 val->arr.type = OCREP_PROP_DOUBLE;
897 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
898 val->arr.dArray = array;
902 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
903 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
905 size_t dimTotal = calcDimTotal(dimensions);
911 double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
918 memcpy(newArray, array, dimTotal * sizeof(double));
920 bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
928 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
929 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
931 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
933 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_DOUBLE
939 size_t dimTotal = calcDimTotal(val->arr.dimensions);
944 *array = (double*)OICMalloc(dimTotal * sizeof(double));
950 memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
951 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
955 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
956 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
958 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
965 val->arr.type = OCREP_PROP_STRING;
966 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
967 val->arr.strArray = array;
971 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
972 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
974 size_t dimTotal = calcDimTotal(dimensions);
980 char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
987 for(size_t i = 0; i < dimTotal; ++i)
989 newArray[i] = OICStrdup(array[i]);
992 bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
996 for(size_t i = 0; i < dimTotal; ++i)
998 OICFree(newArray[i]);
1005 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
1006 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1008 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1010 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
1011 || !val->arr.strArray)
1016 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1021 *array = (char**)OICMalloc(dimTotal * sizeof(char*));
1027 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1029 for(size_t i = 0; i < dimTotal; ++i)
1031 (*array)[i] = OICStrdup(val->arr.strArray[i]);
1038 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
1039 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1042 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1049 val->arr.type = OCREP_PROP_BOOL;
1050 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1051 val->arr.bArray = array;
1055 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
1056 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1058 size_t dimTotal = calcDimTotal(dimensions);
1064 bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
1071 memcpy(newArray, array, dimTotal * sizeof(bool));
1074 bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
1082 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
1083 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1085 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1087 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
1088 || !val->arr.bArray)
1093 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1098 *array = (bool*)OICMalloc(dimTotal * sizeof(bool));
1104 memcpy(*array, val->arr.bArray, dimTotal * sizeof(bool));
1105 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1109 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
1110 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1112 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1119 val->arr.type = OCREP_PROP_OBJECT;
1120 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1121 val->arr.objArray = array;
1126 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
1127 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1129 size_t dimTotal = calcDimTotal(dimensions);
1135 OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1142 for(size_t i = 0; i < dimTotal; ++i)
1144 newArray[i] = OCRepPayloadClone(array[i]);
1147 bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
1151 for(size_t i = 0; i < dimTotal; ++i)
1153 OCRepPayloadDestroy(newArray[i]);
1160 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
1161 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1163 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1165 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
1166 || !val->arr.objArray)
1171 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1176 *array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1182 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1184 for(size_t i = 0; i < dimTotal; ++i)
1186 (*array)[i] = OCRepPayloadClone(val->arr.objArray[i]);
1192 void OCFreeOCStringLL(OCStringLL* ll)
1199 OCFreeOCStringLL(ll->next);
1204 OCStringLL* CloneOCStringLL (OCStringLL* ll)
1211 OCStringLL *sourceIter = ll;
1213 OCStringLL *destIter = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1218 destIter->value = OICStrdup (sourceIter->value);
1220 OCStringLL *headOfClone = destIter;
1222 sourceIter = sourceIter->next;
1226 destIter->next = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1227 if (!destIter->next)
1229 OCFreeOCStringLL (headOfClone);
1232 destIter->next->value = OICStrdup (sourceIter->value);
1234 destIter = destIter->next;
1235 sourceIter = sourceIter->next;
1240 OCRepPayload* OCRepPayloadClone (const OCRepPayload* payload)
1247 OCRepPayload *clone = OCRepPayloadCreate();
1254 clone->uri = OICStrdup (payload->uri);
1255 clone->types = CloneOCStringLL (payload->types);
1256 clone->interfaces = CloneOCStringLL (payload->interfaces);
1257 clone->values = OCRepPayloadValueClone (payload->values);
1263 void OCRepPayloadDestroy(OCRepPayload* payload)
1270 OICFree(payload->uri);
1271 OCFreeOCStringLL(payload->types);
1272 OCFreeOCStringLL(payload->interfaces);
1273 OCFreeRepPayloadValue(payload->values);
1274 OCRepPayloadDestroy(payload->next);
1278 OCDiscoveryPayload* OCDiscoveryPayloadCreate()
1280 OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
1287 payload->base.type = PAYLOAD_TYPE_DISCOVERY;
1292 OCSecurityPayload* OCSecurityPayloadCreate(const char* securityData)
1294 OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1301 payload->base.type = PAYLOAD_TYPE_SECURITY;
1302 payload->securityData = OICStrdup(securityData);
1307 void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
1314 OICFree(payload->securityData);
1318 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload)
1321 OCResourcePayload* p = payload->resources;
1330 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index)
1333 OCResourcePayload* p = payload->resources;
1346 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t port)
1348 OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
1354 pl->uri = OICStrdup(res->uri);
1358 OCDiscoveryResourceDestroy(pl);
1363 OCResourceType* typePtr = res->rsrcType;
1365 if (typePtr != NULL)
1367 pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1370 OCDiscoveryResourceDestroy(pl);
1373 pl->types->value = OICStrdup(typePtr->resourcetypename);
1374 if (!pl->types->value)
1376 OCDiscoveryResourceDestroy(pl);
1380 OCStringLL* cur = pl->types;
1381 typePtr = typePtr->next;
1384 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1387 OCDiscoveryResourceDestroy(pl);
1390 cur->next->value = OICStrdup(typePtr->resourcetypename);
1391 if (!cur->next->value)
1393 OCDiscoveryResourceDestroy(pl);
1397 typePtr = typePtr->next;
1402 OCResourceInterface* ifPtr = res->rsrcInterface;
1405 pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1406 if (!pl->interfaces)
1408 OCDiscoveryResourceDestroy(pl);
1411 pl->interfaces->value = OICStrdup(ifPtr->name);
1412 if (!pl->interfaces->value)
1414 OCDiscoveryResourceDestroy(pl);
1418 OCStringLL* cur = pl->interfaces;
1419 ifPtr = ifPtr->next;
1422 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1425 OCDiscoveryResourceDestroy(pl);
1428 cur->next->value = OICStrdup(ifPtr->name);
1429 if (!cur->next->value)
1431 OCDiscoveryResourceDestroy(pl);
1435 ifPtr = ifPtr->next;
1439 pl->bitmap = res->resourceProperties & (OC_OBSERVABLE | OC_DISCOVERABLE);
1440 pl->secure = (res->resourceProperties & OC_SECURE) != 0;
1446 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1449 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, port));
1452 bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, const char *value)
1454 char *dup = OICStrdup(value);
1455 VERIFY_PARAM_NON_NULL(TAG, dup, "Failed copying string");
1456 VERIFY_PARAM_NON_NULL(TAG, value, "Invalid Parameters");
1460 *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1461 VERIFY_PARAM_NON_NULL(TAG, dup, "Failed allocating memory");
1462 (*stringLL)->value = dup;
1467 OCStringLL *temp = *stringLL;
1472 temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1473 VERIFY_PARAM_NON_NULL(TAG, temp->next, "Failed allocating memory");
1474 temp->next->value = dup;
1482 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
1489 if (!payload->resources)
1491 payload->resources = res;
1495 OCResourcePayload* p = payload->resources;
1504 void OCDiscoveryResourceDestroy(OCResourcePayload* payload)
1511 OICFree(payload->uri);
1512 OCFreeOCStringLL(payload->types);
1513 OCFreeOCStringLL(payload->interfaces);
1514 OCDiscoveryResourceDestroy(payload->next);
1518 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
1524 OICFree(payload->sid);
1525 OCDiscoveryResourceDestroy(payload->resources);
1529 OCDevicePayload* OCDevicePayloadCreate(const uint8_t* sid, const char* dname,
1530 const char* specVer, const char* dmVer)
1533 OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
1540 payload->base.type = PAYLOAD_TYPE_DEVICE;
1544 payload->sid = (uint8_t*)OICMalloc(UUID_SIZE);
1549 memcpy(payload->sid, sid, UUID_SIZE);
1552 payload->deviceName = OICStrdup(dname);
1553 if (dname && !payload->deviceName)
1558 payload->specVersion = OICStrdup(specVer);
1559 if (specVer && !payload->specVersion)
1564 payload->dataModelVersion = OICStrdup(dmVer);
1565 if (dmVer && !payload->dataModelVersion)
1573 OCDevicePayloadDestroy((OCDevicePayload*)payload);
1577 void OCDevicePayloadDestroy(OCDevicePayload* payload)
1584 OICFree(payload->sid);
1585 OICFree(payload->deviceName);
1586 OICFree(payload->specVersion);
1587 OICFree(payload->dataModelVersion);
1591 static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
1593 if (!platformInfo || !target)
1598 target->info.platformID = OICStrdup(platformInfo->platformID);
1599 target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
1600 target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
1601 target->info.modelNumber = OICStrdup(platformInfo->modelNumber);
1602 target->info.dateOfManufacture = OICStrdup(platformInfo->dateOfManufacture);
1603 target->info.platformVersion = OICStrdup(platformInfo->platformVersion);
1604 target->info.operatingSystemVersion = OICStrdup(platformInfo->operatingSystemVersion);
1605 target->info.hardwareVersion = OICStrdup(platformInfo->hardwareVersion);
1606 target->info.firmwareVersion = OICStrdup(platformInfo->firmwareVersion);
1607 target->info.supportUrl = OICStrdup(platformInfo->supportUrl);
1608 target->info.systemTime = OICStrdup(platformInfo->systemTime);
1611 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo)
1613 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1619 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1620 payload->info = *platformInfo;
1625 OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo)
1627 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1634 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1635 OCCopyPlatformInfo(platformInfo, payload);
1640 void OCPlatformInfoDestroy(OCPlatformInfo *info)
1642 OICFree(info->platformID);
1643 OICFree(info->manufacturerName);
1644 OICFree(info->manufacturerUrl);
1645 OICFree(info->modelNumber);
1646 OICFree(info->dateOfManufacture);
1647 OICFree(info->platformVersion);
1648 OICFree(info->operatingSystemVersion);
1649 OICFree(info->hardwareVersion);
1650 OICFree(info->firmwareVersion);
1651 OICFree(info->supportUrl);
1652 OICFree(info->systemTime);
1655 void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
1661 OICFree(payload->uri);
1662 OCPlatformInfoDestroy(&payload->info);
1666 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
1667 OCPresenceTrigger trigger, const char* resourceType)
1669 OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
1675 payload->base.type = PAYLOAD_TYPE_PRESENCE;
1676 payload->sequenceNumber = seqNum;
1677 payload->maxAge = maxAge;
1678 payload->trigger = trigger;
1679 payload->resourceType = OICStrdup(resourceType);
1683 void OCPresencePayloadDestroy(OCPresencePayload* payload)
1689 OICFree(payload->resourceType);