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 VERIFY_PARAM_NON_NULL(TAG, dest->arr.iArray, "Failed allocating memory");
135 memcpy(dest->arr.iArray, source->arr.iArray, dimTotal * sizeof(int64_t));
137 case OCREP_PROP_DOUBLE:
138 dest->arr.dArray = (double*)OICMalloc(dimTotal * sizeof(double));
139 VERIFY_PARAM_NON_NULL(TAG, dest->arr.dArray, "Failed allocating memory");
140 memcpy(dest->arr.dArray, source->arr.dArray, dimTotal * sizeof(double));
142 case OCREP_PROP_BOOL:
143 dest->arr.bArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
144 VERIFY_PARAM_NON_NULL(TAG, dest->arr.bArray, "Failed allocating memory");
145 memcpy(dest->arr.bArray, source->arr.bArray, dimTotal * sizeof(bool));
147 case OCREP_PROP_STRING:
148 dest->arr.strArray = (char**)OICMalloc(dimTotal * sizeof(char*));
149 VERIFY_PARAM_NON_NULL(TAG, dest->arr.strArray, "Failed allocating memory");
150 for(size_t i = 0; i < dimTotal; ++i)
152 dest->arr.strArray[i] = OICStrdup(source->arr.strArray[i]);
155 case OCREP_PROP_OBJECT:
156 dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
157 VERIFY_PARAM_NON_NULL(TAG, dest->arr.objArray, "Failed allocating memory");
158 for(size_t i = 0; i < dimTotal; ++i)
160 dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
163 case OCREP_PROP_ARRAY:
164 dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
165 VERIFY_PARAM_NON_NULL(TAG, dest->arr.objArray, "Failed allocating memory");
166 for(size_t i = 0; i < dimTotal; ++i)
168 dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
172 OIC_LOG(ERROR, TAG, "CopyPropertyValueArray invalid type");
179 static void OCCopyPropertyValue (OCRepPayloadValue *dest, OCRepPayloadValue *source)
181 if (!source || !dest)
188 case OCREP_PROP_STRING:
189 dest->str = OICStrdup(source->str);
191 case OCREP_PROP_OBJECT:
192 dest->obj = OCRepPayloadClone(source->obj);
194 case OCREP_PROP_ARRAY:
195 OCCopyPropertyValueArray(dest, source);
198 // Nothing to do for the trivially copyable types.
203 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
210 if (val->type == OCREP_PROP_STRING)
214 else if (val->type == OCREP_PROP_BYTE_STRING)
216 OICFree(val->ocByteStr.bytes);
218 else if (val->type == OCREP_PROP_OBJECT)
220 OCRepPayloadDestroy(val->obj);
222 else if (val->type == OCREP_PROP_ARRAY)
224 size_t dimTotal = calcDimTotal(val->arr.dimensions);
225 switch(val->arr.type)
228 case OCREP_PROP_DOUBLE:
229 case OCREP_PROP_BOOL:
230 // Since this is a union, iArray will
231 // point to all of the above
232 OICFree(val->arr.iArray);
234 case OCREP_PROP_STRING:
235 for(size_t i = 0; i< dimTotal; ++i)
237 OICFree(val->arr.strArray[i]);
239 OICFree(val->arr.strArray);
241 case OCREP_PROP_BYTE_STRING:
242 for (size_t i = 0; i< dimTotal; ++i)
244 OICFree(val->arr.ocByteStrArray[i].bytes);
246 OICFree(val->arr.ocByteStrArray);
248 case OCREP_PROP_OBJECT: // This case is the temporary fix for string input
249 for(size_t i = 0; i< dimTotal; ++i)
251 OCRepPayloadDestroy(val->arr.objArray[i]);
253 OICFree(val->arr.objArray);
255 case OCREP_PROP_NULL:
256 case OCREP_PROP_ARRAY:
257 OIC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
258 inside an array: %d", val->arr.type);
264 static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
272 OCFreeRepPayloadValueContents(val);
273 OCFreeRepPayloadValue(val->next);
276 static OCRepPayloadValue* OCRepPayloadValueClone (OCRepPayloadValue* source)
283 OCRepPayloadValue *sourceIter = source;
284 OCRepPayloadValue *destIter = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
290 OCRepPayloadValue *headOfClone = destIter;
292 // Copy payload type and non pointer types in union.
293 *destIter = *sourceIter;
294 destIter->name = OICStrdup (sourceIter->name);
295 OCCopyPropertyValue (destIter, sourceIter);
297 sourceIter = sourceIter->next;
301 destIter->next = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
304 OCFreeRepPayloadValue (headOfClone);
308 *(destIter->next) = *sourceIter;
309 destIter->next->name = OICStrdup (sourceIter->name);
310 OCCopyPropertyValue (destIter->next, sourceIter);
312 sourceIter = sourceIter->next;
313 destIter = destIter->next;
318 static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
319 OCRepPayloadPropType type)
321 if (!payload || !name)
326 OCRepPayloadValue* val = payload->values;
329 payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
330 if (!payload->values)
334 payload->values->name = OICStrdup(name);
335 if (!payload->values->name)
337 OICFree(payload->values);
338 payload->values = NULL;
341 payload->values->type =type;
342 return payload->values;
347 if (0 == strcmp(val->name, name))
349 OCFreeRepPayloadValueContents(val);
353 else if (val->next == NULL)
355 val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
360 val->next->name = OICStrdup(name);
361 if (!val->next->name)
367 val->next->type =type;
374 OIC_LOG(ERROR, TAG, "FindAndSetValue reached point after while loop, pointer corruption?");
378 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType)
380 return OCRepPayloadAddResourceTypeAsOwner(payload, OICStrdup(resourceType));
383 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
385 if (!payload || !resourceType)
392 OCStringLL* cur = payload->types;
397 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
404 cur->next->value = resourceType;
409 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
414 payload->types->value = resourceType;
419 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* interface)
421 return OCRepPayloadAddInterfaceAsOwner(payload, OICStrdup(interface));
424 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
426 if (!payload || !interface)
431 if (payload->interfaces)
433 OCStringLL* cur = payload->interfaces;
438 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
444 cur->next->value = interface;
449 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
450 if (!payload->interfaces)
454 payload->interfaces->value = interface;
459 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
465 OICFree(payload->uri);
466 payload->uri = OICStrdup(uri);
467 return payload->uri != NULL;
470 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name)
472 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
479 return val->type == OCREP_PROP_NULL;
482 static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
483 void* value, OCRepPayloadPropType type)
485 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
493 val->i = *(int64_t*)value;
495 case OCREP_PROP_DOUBLE:
496 val->d = *(double*)value;
498 case OCREP_PROP_BOOL:
499 val->b = *(bool*)value;
501 case OCREP_PROP_OBJECT:
502 val->obj = (OCRepPayload*)value;
504 case OCREP_PROP_STRING:
505 val->str = (char*)value;
506 return val->str != NULL;
507 case OCREP_PROP_BYTE_STRING:
508 val->ocByteStr = *(OCByteString*)value;
510 case OCREP_PROP_NULL:
512 case OCREP_PROP_ARRAY:
520 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
522 return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
525 bool OCRepPayloadSetPropInt(OCRepPayload* payload,
526 const char* name, int64_t value)
528 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
531 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
533 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
535 if (!val || val->type != OCREP_PROP_INT)
544 bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
545 const char* name, double value)
547 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
550 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
552 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
556 if (val->type == OCREP_PROP_DOUBLE)
561 else if (val->type == OCREP_PROP_INT)
571 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
573 char* temp = OICStrdup(value);
574 bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
583 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
585 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
588 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
590 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
592 if (!val || val->type != OCREP_PROP_STRING)
597 *value = OICStrdup(val->str);
598 return *value != NULL;
601 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value)
603 if (!value.bytes || !value.len)
608 OCByteString ocByteStr = {
609 .bytes = (uint8_t*)OICMalloc(value.len * sizeof(uint8_t)),
612 if (!ocByteStr.bytes)
616 memcpy(ocByteStr.bytes, value.bytes, ocByteStr.len);
618 bool b = OCRepPayloadSetPropByteStringAsOwner(payload, name, &ocByteStr);
622 OICFree(ocByteStr.bytes);
627 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name, OCByteString* value)
629 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_BYTE_STRING);
632 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name, OCByteString* value)
634 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
636 if (!val || val->type != OCREP_PROP_BYTE_STRING)
646 value->bytes = (uint8_t*)OICMalloc(val->ocByteStr.len * sizeof(uint8_t));
651 value->len = val->ocByteStr.len;
652 memcpy(value->bytes, val->ocByteStr.bytes, value->len);
657 bool OCRepPayloadSetPropBool(OCRepPayload* payload,
658 const char* name, bool value)
660 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
663 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
665 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
667 if (!val || val->type != OCREP_PROP_BOOL)
676 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value)
678 OCRepPayload* temp = OCRepPayloadClone(value);
679 bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
683 OCRepPayloadDestroy(temp);
688 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
690 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
693 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
695 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
697 if (!val || val->type != OCREP_PROP_OBJECT)
702 *value = OCRepPayloadClone(val->obj);
703 return *value != NULL;
706 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
708 if (dimensions[0] == 0)
714 for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
716 total *= dimensions[i];
722 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
723 OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
725 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
732 val->arr.type = OCREP_PROP_BYTE_STRING;
733 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
734 val->arr.ocByteStrArray = array;
739 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
740 const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
747 size_t dimTotal = calcDimTotal(dimensions);
753 OCByteString* newArray = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
760 for (size_t i = 0; i < dimTotal; ++i)
762 newArray[i].bytes = (uint8_t*)OICMalloc(array[i].len * sizeof(uint8_t));
763 if (NULL == newArray[i].bytes)
765 for (size_t j = 0; j < i; ++j)
767 OICFree(newArray[j].bytes);
773 newArray[i].len = array[i].len;
774 memcpy(newArray[i].bytes, array[i].bytes, newArray[i].len);
777 bool b = OCRepPayloadSetByteStringArrayAsOwner(payload, name, newArray, dimensions);
780 for (size_t i = 0; i < dimTotal; ++i)
782 OICFree(newArray[i].bytes);
790 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
791 OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
793 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
795 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BYTE_STRING
796 || !val->arr.ocByteStrArray)
801 size_t dimTotal = calcDimTotal(val->arr.dimensions);
807 *array = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
813 for (size_t i = 0; i < dimTotal; ++i)
815 OCByteString* tmp = &(*array)[i];
816 tmp->bytes = (uint8_t*)OICMalloc(val->arr.ocByteStrArray[i].len * sizeof(uint8_t));
817 if (NULL == tmp->bytes)
819 for (size_t j = 0; j < i; ++j)
821 OCByteString* tmp = &(*array)[j];
829 tmp->len = val->arr.ocByteStrArray[i].len;
830 memcpy(tmp->bytes, val->arr.ocByteStrArray[i].bytes, tmp->len);
833 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
838 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
839 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
841 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
848 val->arr.type = OCREP_PROP_INT;
849 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
850 val->arr.iArray = array;
855 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
856 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
858 size_t dimTotal = calcDimTotal(dimensions);
864 int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
871 memcpy(newArray, array, dimTotal * sizeof(int64_t));
874 bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
882 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
883 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
885 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
887 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
893 size_t dimTotal = calcDimTotal(val->arr.dimensions);
898 *array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
904 memcpy(*array, val->arr.iArray, dimTotal * sizeof(int64_t));
905 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
909 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
910 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
912 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
919 val->arr.type = OCREP_PROP_DOUBLE;
920 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
921 val->arr.dArray = array;
925 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
926 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
928 size_t dimTotal = calcDimTotal(dimensions);
934 double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
941 memcpy(newArray, array, dimTotal * sizeof(double));
943 bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
951 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
952 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
954 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
956 if (!val || val->type != OCREP_PROP_ARRAY ||
957 (val->arr.type != OCREP_PROP_DOUBLE && val->arr.type != OCREP_PROP_INT)
963 size_t dimTotal = calcDimTotal(val->arr.dimensions);
968 *array = (double*)OICMalloc(dimTotal * sizeof(double));
974 if (val->arr.type != OCREP_PROP_DOUBLE)
976 memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
980 /* need to convert from integer */
982 for ( ; n < dimTotal; ++n)
984 (*array)[n] = val->arr.iArray[n];
987 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
991 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
992 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
994 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1001 val->arr.type = OCREP_PROP_STRING;
1002 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1003 val->arr.strArray = array;
1007 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
1008 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1010 size_t dimTotal = calcDimTotal(dimensions);
1016 char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
1023 for(size_t i = 0; i < dimTotal; ++i)
1025 newArray[i] = OICStrdup(array[i]);
1028 bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
1032 for(size_t i = 0; i < dimTotal; ++i)
1034 OICFree(newArray[i]);
1041 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
1042 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1044 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1046 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
1047 || !val->arr.strArray)
1052 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1057 *array = (char**)OICMalloc(dimTotal * sizeof(char*));
1063 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1065 for(size_t i = 0; i < dimTotal; ++i)
1067 (*array)[i] = OICStrdup(val->arr.strArray[i]);
1074 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
1075 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1078 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1085 val->arr.type = OCREP_PROP_BOOL;
1086 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1087 val->arr.bArray = array;
1091 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
1092 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1094 size_t dimTotal = calcDimTotal(dimensions);
1100 bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
1107 memcpy(newArray, array, dimTotal * sizeof(bool));
1110 bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
1118 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
1119 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1121 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1123 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
1124 || !val->arr.bArray)
1129 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1134 *array = (bool*)OICMalloc(dimTotal * sizeof(bool));
1140 memcpy(*array, val->arr.bArray, dimTotal * sizeof(bool));
1141 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1145 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
1146 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1148 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1155 val->arr.type = OCREP_PROP_OBJECT;
1156 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1157 val->arr.objArray = array;
1162 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
1163 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1165 size_t dimTotal = calcDimTotal(dimensions);
1171 OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1178 for(size_t i = 0; i < dimTotal; ++i)
1180 newArray[i] = OCRepPayloadClone(array[i]);
1183 bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
1187 for(size_t i = 0; i < dimTotal; ++i)
1189 OCRepPayloadDestroy(newArray[i]);
1196 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
1197 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1199 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1201 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
1202 || !val->arr.objArray)
1207 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1212 *array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1218 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1220 for(size_t i = 0; i < dimTotal; ++i)
1222 (*array)[i] = OCRepPayloadClone(val->arr.objArray[i]);
1228 void OCFreeOCStringLL(OCStringLL* ll)
1235 OCFreeOCStringLL(ll->next);
1240 OCStringLL* CloneOCStringLL (OCStringLL* ll)
1247 OCStringLL *sourceIter = ll;
1249 OCStringLL *destIter = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1254 destIter->value = OICStrdup (sourceIter->value);
1256 OCStringLL *headOfClone = destIter;
1258 sourceIter = sourceIter->next;
1262 destIter->next = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1263 if (!destIter->next)
1265 OCFreeOCStringLL (headOfClone);
1268 destIter->next->value = OICStrdup (sourceIter->value);
1270 destIter = destIter->next;
1271 sourceIter = sourceIter->next;
1276 OCRepPayload* OCRepPayloadClone (const OCRepPayload* payload)
1283 OCRepPayload *clone = OCRepPayloadCreate();
1290 clone->uri = OICStrdup (payload->uri);
1291 clone->types = CloneOCStringLL (payload->types);
1292 clone->interfaces = CloneOCStringLL (payload->interfaces);
1293 clone->values = OCRepPayloadValueClone (payload->values);
1299 void OCRepPayloadDestroy(OCRepPayload* payload)
1306 OICFree(payload->uri);
1307 OCFreeOCStringLL(payload->types);
1308 OCFreeOCStringLL(payload->interfaces);
1309 OCFreeRepPayloadValue(payload->values);
1310 OCRepPayloadDestroy(payload->next);
1314 OCDiscoveryPayload* OCDiscoveryPayloadCreate()
1316 OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
1323 payload->base.type = PAYLOAD_TYPE_DISCOVERY;
1328 OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size)
1330 OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1337 payload->base.type = PAYLOAD_TYPE_SECURITY;
1338 payload->securityData = (uint8_t *)OICCalloc(1, size);
1339 if (!payload->securityData)
1344 memcpy(payload->securityData, (uint8_t *)securityData, size);
1345 payload->payloadSize = size;
1350 void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
1357 OICFree(payload->securityData);
1361 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload)
1364 OCResourcePayload* p = payload->resources;
1373 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index)
1376 OCResourcePayload* p = payload->resources;
1389 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t securePort,
1392 OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
1398 pl->uri = OICStrdup(res->uri);
1402 OCDiscoveryResourceDestroy(pl);
1407 OCResourceType* typePtr = res->rsrcType;
1409 if (typePtr != NULL)
1411 pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1414 OCDiscoveryResourceDestroy(pl);
1417 pl->types->value = OICStrdup(typePtr->resourcetypename);
1418 if (!pl->types->value)
1420 OCDiscoveryResourceDestroy(pl);
1424 OCStringLL* cur = pl->types;
1425 typePtr = typePtr->next;
1428 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1431 OCDiscoveryResourceDestroy(pl);
1434 cur->next->value = OICStrdup(typePtr->resourcetypename);
1435 if (!cur->next->value)
1437 OCDiscoveryResourceDestroy(pl);
1441 typePtr = typePtr->next;
1446 OCResourceInterface* ifPtr = res->rsrcInterface;
1449 pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1450 if (!pl->interfaces)
1452 OCDiscoveryResourceDestroy(pl);
1455 pl->interfaces->value = OICStrdup(ifPtr->name);
1456 if (!pl->interfaces->value)
1458 OCDiscoveryResourceDestroy(pl);
1462 OCStringLL* cur = pl->interfaces;
1463 ifPtr = ifPtr->next;
1466 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1469 OCDiscoveryResourceDestroy(pl);
1472 cur->next->value = OICStrdup(ifPtr->name);
1473 if (!cur->next->value)
1475 OCDiscoveryResourceDestroy(pl);
1479 ifPtr = ifPtr->next;
1483 pl->bitmap = res->resourceProperties & (OC_OBSERVABLE | OC_DISCOVERABLE);
1484 pl->secure = (res->resourceProperties & OC_SECURE) != 0;
1485 pl->port = securePort;
1487 pl->tcpPort = tcpPort;
1492 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1493 uint16_t securePort, uint16_t tcpPort)
1495 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, securePort, tcpPort));
1498 bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, const char *value)
1500 char *dup = OICStrdup(value);
1501 VERIFY_PARAM_NON_NULL(TAG, dup, "Failed copying string");
1502 VERIFY_PARAM_NON_NULL(TAG, value, "Invalid Parameters");
1506 *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1507 VERIFY_PARAM_NON_NULL(TAG, *stringLL, "Failed allocating memory");
1508 (*stringLL)->value = dup;
1513 OCStringLL *temp = *stringLL;
1518 temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1519 VERIFY_PARAM_NON_NULL(TAG, temp->next, "Failed allocating memory");
1520 temp->next->value = dup;
1528 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
1535 if (!payload->resources)
1537 payload->resources = res;
1541 OCResourcePayload* p = payload->resources;
1550 void OCDiscoveryResourceDestroy(OCResourcePayload* payload)
1557 OICFree(payload->uri);
1558 OCFreeOCStringLL(payload->types);
1559 OCFreeOCStringLL(payload->interfaces);
1560 OCDiscoveryResourceDestroy(payload->next);
1564 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
1570 OICFree(payload->sid);
1571 OICFree(payload->baseURI);
1572 OICFree(payload->uri);
1573 OICFree(payload->type);
1574 OICFree(payload->name);
1575 OCFreeOCStringLL(payload->interface);
1576 OCDiscoveryResourceDestroy(payload->resources);
1580 OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
1581 const OCStringLL *types, const char* specVer, const char* dmVer)
1584 OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
1591 payload->base.type = PAYLOAD_TYPE_DEVICE;
1592 payload->sid = OICStrdup(sid);
1593 if (sid && !payload->sid)
1598 payload->deviceName = OICStrdup(dname);
1599 if (dname && !payload->deviceName)
1604 payload->specVersion = OICStrdup(specVer);
1605 if (specVer && !payload->specVersion)
1610 payload->dataModelVersion = OICStrdup(dmVer);
1611 if (dmVer && !payload->dataModelVersion)
1616 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_DEFAULT);
1617 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_READ);
1619 payload->types = CloneOCStringLL((OCStringLL *)types);
1620 if (types && !payload->types)
1628 OCDevicePayloadDestroy((OCDevicePayload*)payload);
1632 void OCDevicePayloadDestroy(OCDevicePayload* payload)
1639 OICFree(payload->sid);
1640 OICFree(payload->deviceName);
1641 OICFree(payload->specVersion);
1642 OICFree(payload->dataModelVersion);
1643 OCFreeOCStringLL(payload->types);
1644 OCFreeOCStringLL(payload->interfaces);
1648 static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
1650 if (!platformInfo || !target)
1655 target->info.platformID = OICStrdup(platformInfo->platformID);
1656 target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
1657 target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
1658 target->info.modelNumber = OICStrdup(platformInfo->modelNumber);
1659 target->info.dateOfManufacture = OICStrdup(platformInfo->dateOfManufacture);
1660 target->info.platformVersion = OICStrdup(platformInfo->platformVersion);
1661 target->info.operatingSystemVersion = OICStrdup(platformInfo->operatingSystemVersion);
1662 target->info.hardwareVersion = OICStrdup(platformInfo->hardwareVersion);
1663 target->info.firmwareVersion = OICStrdup(platformInfo->firmwareVersion);
1664 target->info.supportUrl = OICStrdup(platformInfo->supportUrl);
1665 target->info.systemTime = OICStrdup(platformInfo->systemTime);
1668 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo)
1670 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1676 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1678 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1679 if (!payload->interfaces)
1683 payload->interfaces->value = OICStrdup(OC_RSRVD_INTERFACE_READ);
1684 payload->rt = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
1685 payload->info = *platformInfo;
1690 OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo)
1692 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1699 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1700 payload->rt = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
1702 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_DEFAULT);
1703 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_READ);
1705 OCCopyPlatformInfo(platformInfo, payload);
1710 void OCPlatformInfoDestroy(OCPlatformInfo *info)
1712 OICFree(info->platformID);
1713 OICFree(info->manufacturerName);
1714 OICFree(info->manufacturerUrl);
1715 OICFree(info->modelNumber);
1716 OICFree(info->dateOfManufacture);
1717 OICFree(info->platformVersion);
1718 OICFree(info->operatingSystemVersion);
1719 OICFree(info->hardwareVersion);
1720 OICFree(info->firmwareVersion);
1721 OICFree(info->supportUrl);
1722 OICFree(info->systemTime);
1725 void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
1731 OICFree(payload->uri);
1732 OCPlatformInfoDestroy(&payload->info);
1733 OICFree(payload->rt);
1734 OCFreeOCStringLL(payload->interfaces);
1738 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
1739 OCPresenceTrigger trigger, const char* resourceType)
1741 OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
1747 payload->base.type = PAYLOAD_TYPE_PRESENCE;
1748 payload->sequenceNumber = seqNum;
1749 payload->maxAge = maxAge;
1750 payload->trigger = trigger;
1751 payload->resourceType = OICStrdup(resourceType);
1755 void OCPresencePayloadDestroy(OCPresencePayload* payload)
1761 OICFree(payload->resourceType);