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_BYTE_STRING:
192 dest->ocByteStr.bytes = (uint8_t*)OICMalloc(source->ocByteStr.len * sizeof(uint8_t));
193 VERIFY_PARAM_NON_NULL(TAG, dest->ocByteStr.bytes, "Failed allocating memory");
194 dest->ocByteStr.len = source->ocByteStr.len;
195 memcpy(dest->ocByteStr.bytes, source->ocByteStr.bytes, dest->ocByteStr.len);
197 case OCREP_PROP_OBJECT:
198 dest->obj = OCRepPayloadClone(source->obj);
200 case OCREP_PROP_ARRAY:
201 OCCopyPropertyValueArray(dest, source);
204 // Nothing to do for the trivially copyable types.
211 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
218 if (val->type == OCREP_PROP_STRING)
222 else if (val->type == OCREP_PROP_BYTE_STRING)
224 OICFree(val->ocByteStr.bytes);
226 else if (val->type == OCREP_PROP_OBJECT)
228 OCRepPayloadDestroy(val->obj);
230 else if (val->type == OCREP_PROP_ARRAY)
232 size_t dimTotal = calcDimTotal(val->arr.dimensions);
233 switch(val->arr.type)
236 case OCREP_PROP_DOUBLE:
237 case OCREP_PROP_BOOL:
238 // Since this is a union, iArray will
239 // point to all of the above
240 OICFree(val->arr.iArray);
242 case OCREP_PROP_STRING:
243 for(size_t i = 0; i< dimTotal; ++i)
245 OICFree(val->arr.strArray[i]);
247 OICFree(val->arr.strArray);
249 case OCREP_PROP_BYTE_STRING:
250 for (size_t i = 0; i< dimTotal; ++i)
252 OICFree(val->arr.ocByteStrArray[i].bytes);
254 OICFree(val->arr.ocByteStrArray);
256 case OCREP_PROP_OBJECT: // This case is the temporary fix for string input
257 for(size_t i = 0; i< dimTotal; ++i)
259 OCRepPayloadDestroy(val->arr.objArray[i]);
261 OICFree(val->arr.objArray);
263 case OCREP_PROP_NULL:
264 case OCREP_PROP_ARRAY:
265 OIC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
266 inside an array: %d", val->arr.type);
272 static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
280 OCFreeRepPayloadValueContents(val);
281 OCFreeRepPayloadValue(val->next);
284 static OCRepPayloadValue* OCRepPayloadValueClone (OCRepPayloadValue* source)
291 OCRepPayloadValue *sourceIter = source;
292 OCRepPayloadValue *destIter = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
298 OCRepPayloadValue *headOfClone = destIter;
300 // Copy payload type and non pointer types in union.
301 *destIter = *sourceIter;
302 destIter->name = OICStrdup (sourceIter->name);
303 OCCopyPropertyValue (destIter, sourceIter);
305 sourceIter = sourceIter->next;
309 destIter->next = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
312 OCFreeRepPayloadValue (headOfClone);
316 *(destIter->next) = *sourceIter;
317 destIter->next->name = OICStrdup (sourceIter->name);
318 OCCopyPropertyValue (destIter->next, sourceIter);
320 sourceIter = sourceIter->next;
321 destIter = destIter->next;
326 static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
327 OCRepPayloadPropType type)
329 if (!payload || !name)
334 OCRepPayloadValue* val = payload->values;
337 payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
338 if (!payload->values)
342 payload->values->name = OICStrdup(name);
343 if (!payload->values->name)
345 OICFree(payload->values);
346 payload->values = NULL;
349 payload->values->type =type;
350 return payload->values;
355 if (0 == strcmp(val->name, name))
357 OCFreeRepPayloadValueContents(val);
361 else if (val->next == NULL)
363 val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
368 val->next->name = OICStrdup(name);
369 if (!val->next->name)
375 val->next->type =type;
382 OIC_LOG(ERROR, TAG, "FindAndSetValue reached point after while loop, pointer corruption?");
386 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType)
388 return OCRepPayloadAddResourceTypeAsOwner(payload, OICStrdup(resourceType));
391 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
393 if (!payload || !resourceType)
400 OCStringLL* cur = payload->types;
405 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
412 cur->next->value = resourceType;
417 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
422 payload->types->value = resourceType;
427 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* interface)
429 return OCRepPayloadAddInterfaceAsOwner(payload, OICStrdup(interface));
432 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
434 if (!payload || !interface)
439 if (payload->interfaces)
441 OCStringLL* cur = payload->interfaces;
446 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
452 cur->next->value = interface;
457 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
458 if (!payload->interfaces)
462 payload->interfaces->value = interface;
467 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
473 OICFree(payload->uri);
474 payload->uri = OICStrdup(uri);
475 return payload->uri != NULL;
478 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name)
480 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
487 return val->type == OCREP_PROP_NULL;
490 static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
491 void* value, OCRepPayloadPropType type)
493 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
501 val->i = *(int64_t*)value;
503 case OCREP_PROP_DOUBLE:
504 val->d = *(double*)value;
506 case OCREP_PROP_BOOL:
507 val->b = *(bool*)value;
509 case OCREP_PROP_OBJECT:
510 val->obj = (OCRepPayload*)value;
512 case OCREP_PROP_STRING:
513 val->str = (char*)value;
514 return val->str != NULL;
515 case OCREP_PROP_BYTE_STRING:
516 val->ocByteStr = *(OCByteString*)value;
518 case OCREP_PROP_NULL:
520 case OCREP_PROP_ARRAY:
528 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
530 return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
533 bool OCRepPayloadSetPropInt(OCRepPayload* payload,
534 const char* name, int64_t value)
536 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
539 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
541 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
543 if (!val || val->type != OCREP_PROP_INT)
552 bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
553 const char* name, double value)
555 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
558 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
560 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
564 if (val->type == OCREP_PROP_DOUBLE)
569 else if (val->type == OCREP_PROP_INT)
579 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
581 char* temp = OICStrdup(value);
582 bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
591 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
593 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
596 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
598 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
600 if (!val || val->type != OCREP_PROP_STRING)
605 *value = OICStrdup(val->str);
606 return *value != NULL;
609 bool OCRepPayloadSetPropByteString(OCRepPayload* payload, const char* name, OCByteString value)
611 if (!value.bytes || !value.len)
616 OCByteString ocByteStr = {
617 .bytes = (uint8_t*)OICMalloc(value.len * sizeof(uint8_t)),
620 if (!ocByteStr.bytes)
624 memcpy(ocByteStr.bytes, value.bytes, ocByteStr.len);
626 bool b = OCRepPayloadSetPropByteStringAsOwner(payload, name, &ocByteStr);
630 OICFree(ocByteStr.bytes);
635 bool OCRepPayloadSetPropByteStringAsOwner(OCRepPayload* payload, const char* name, OCByteString* value)
637 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_BYTE_STRING);
640 bool OCRepPayloadGetPropByteString(const OCRepPayload* payload, const char* name, OCByteString* value)
642 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
644 if (!val || val->type != OCREP_PROP_BYTE_STRING)
654 value->bytes = (uint8_t*)OICMalloc(val->ocByteStr.len * sizeof(uint8_t));
659 value->len = val->ocByteStr.len;
660 memcpy(value->bytes, val->ocByteStr.bytes, value->len);
665 bool OCRepPayloadSetPropBool(OCRepPayload* payload,
666 const char* name, bool value)
668 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
671 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
673 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
675 if (!val || val->type != OCREP_PROP_BOOL)
684 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value)
686 OCRepPayload* temp = OCRepPayloadClone(value);
687 bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
691 OCRepPayloadDestroy(temp);
696 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
698 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
701 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
703 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
705 if (!val || val->type != OCREP_PROP_OBJECT)
710 *value = OCRepPayloadClone(val->obj);
711 return *value != NULL;
714 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
716 if (dimensions[0] == 0)
722 for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
724 total *= dimensions[i];
730 bool OCRepPayloadSetByteStringArrayAsOwner(OCRepPayload* payload, const char* name,
731 OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
733 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
740 val->arr.type = OCREP_PROP_BYTE_STRING;
741 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
742 val->arr.ocByteStrArray = array;
747 bool OCRepPayloadSetByteStringArray(OCRepPayload* payload, const char* name,
748 const OCByteString* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
755 size_t dimTotal = calcDimTotal(dimensions);
761 OCByteString* newArray = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
768 for (size_t i = 0; i < dimTotal; ++i)
770 newArray[i].bytes = (uint8_t*)OICMalloc(array[i].len * sizeof(uint8_t));
771 if (NULL == newArray[i].bytes)
773 for (size_t j = 0; j < i; ++j)
775 OICFree(newArray[j].bytes);
781 newArray[i].len = array[i].len;
782 memcpy(newArray[i].bytes, array[i].bytes, newArray[i].len);
785 bool b = OCRepPayloadSetByteStringArrayAsOwner(payload, name, newArray, dimensions);
788 for (size_t i = 0; i < dimTotal; ++i)
790 OICFree(newArray[i].bytes);
798 bool OCRepPayloadGetByteStringArray(const OCRepPayload* payload, const char* name,
799 OCByteString** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
801 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
803 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BYTE_STRING
804 || !val->arr.ocByteStrArray)
809 size_t dimTotal = calcDimTotal(val->arr.dimensions);
815 *array = (OCByteString*)OICCalloc(dimTotal, sizeof(OCByteString));
821 for (size_t i = 0; i < dimTotal; ++i)
823 OCByteString* tmp = &(*array)[i];
824 tmp->bytes = (uint8_t*)OICMalloc(val->arr.ocByteStrArray[i].len * sizeof(uint8_t));
825 if (NULL == tmp->bytes)
827 for (size_t j = 0; j < i; ++j)
829 OCByteString* tmp = &(*array)[j];
837 tmp->len = val->arr.ocByteStrArray[i].len;
838 memcpy(tmp->bytes, val->arr.ocByteStrArray[i].bytes, tmp->len);
841 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
846 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
847 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
849 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
856 val->arr.type = OCREP_PROP_INT;
857 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
858 val->arr.iArray = array;
863 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
864 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
866 size_t dimTotal = calcDimTotal(dimensions);
872 int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
879 memcpy(newArray, array, dimTotal * sizeof(int64_t));
882 bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
890 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
891 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
893 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
895 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
901 size_t dimTotal = calcDimTotal(val->arr.dimensions);
906 *array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
912 memcpy(*array, val->arr.iArray, dimTotal * sizeof(int64_t));
913 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
917 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
918 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
920 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
927 val->arr.type = OCREP_PROP_DOUBLE;
928 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
929 val->arr.dArray = array;
933 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
934 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
936 size_t dimTotal = calcDimTotal(dimensions);
942 double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
949 memcpy(newArray, array, dimTotal * sizeof(double));
951 bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
959 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
960 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
962 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
964 if (!val || val->type != OCREP_PROP_ARRAY ||
965 (val->arr.type != OCREP_PROP_DOUBLE && val->arr.type != OCREP_PROP_INT)
971 size_t dimTotal = calcDimTotal(val->arr.dimensions);
976 *array = (double*)OICMalloc(dimTotal * sizeof(double));
982 if (val->arr.type != OCREP_PROP_DOUBLE)
984 memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
988 /* need to convert from integer */
990 for ( ; n < dimTotal; ++n)
992 (*array)[n] = val->arr.iArray[n];
995 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
999 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
1000 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1002 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1009 val->arr.type = OCREP_PROP_STRING;
1010 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1011 val->arr.strArray = array;
1015 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
1016 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1018 size_t dimTotal = calcDimTotal(dimensions);
1024 char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
1031 for(size_t i = 0; i < dimTotal; ++i)
1033 newArray[i] = OICStrdup(array[i]);
1036 bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
1040 for(size_t i = 0; i < dimTotal; ++i)
1042 OICFree(newArray[i]);
1049 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
1050 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1052 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1054 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
1055 || !val->arr.strArray)
1060 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1065 *array = (char**)OICMalloc(dimTotal * sizeof(char*));
1071 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1073 for(size_t i = 0; i < dimTotal; ++i)
1075 (*array)[i] = OICStrdup(val->arr.strArray[i]);
1082 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
1083 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1086 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1093 val->arr.type = OCREP_PROP_BOOL;
1094 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1095 val->arr.bArray = array;
1099 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
1100 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1102 size_t dimTotal = calcDimTotal(dimensions);
1108 bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
1115 memcpy(newArray, array, dimTotal * sizeof(bool));
1118 bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
1126 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
1127 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1129 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1131 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
1132 || !val->arr.bArray)
1137 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1142 *array = (bool*)OICMalloc(dimTotal * sizeof(bool));
1148 memcpy(*array, val->arr.bArray, dimTotal * sizeof(bool));
1149 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1153 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
1154 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1156 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
1163 val->arr.type = OCREP_PROP_OBJECT;
1164 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1165 val->arr.objArray = array;
1170 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
1171 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1173 size_t dimTotal = calcDimTotal(dimensions);
1179 OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1186 for(size_t i = 0; i < dimTotal; ++i)
1188 newArray[i] = OCRepPayloadClone(array[i]);
1191 bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
1195 for(size_t i = 0; i < dimTotal; ++i)
1197 OCRepPayloadDestroy(newArray[i]);
1204 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
1205 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
1207 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
1209 if (!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
1210 || !val->arr.objArray)
1215 size_t dimTotal = calcDimTotal(val->arr.dimensions);
1220 *array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
1226 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
1228 for(size_t i = 0; i < dimTotal; ++i)
1230 (*array)[i] = OCRepPayloadClone(val->arr.objArray[i]);
1236 void OCFreeOCStringLL(OCStringLL* ll)
1243 OCFreeOCStringLL(ll->next);
1248 OCStringLL* CloneOCStringLL (OCStringLL* ll)
1255 OCStringLL *sourceIter = ll;
1257 OCStringLL *destIter = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1262 destIter->value = OICStrdup (sourceIter->value);
1264 OCStringLL *headOfClone = destIter;
1266 sourceIter = sourceIter->next;
1270 destIter->next = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1271 if (!destIter->next)
1273 OCFreeOCStringLL (headOfClone);
1276 destIter->next->value = OICStrdup (sourceIter->value);
1278 destIter = destIter->next;
1279 sourceIter = sourceIter->next;
1284 OCRepPayload* OCRepPayloadClone (const OCRepPayload* payload)
1291 OCRepPayload *clone = OCRepPayloadCreate();
1298 clone->uri = OICStrdup (payload->uri);
1299 clone->types = CloneOCStringLL (payload->types);
1300 clone->interfaces = CloneOCStringLL (payload->interfaces);
1301 clone->values = OCRepPayloadValueClone (payload->values);
1307 void OCRepPayloadDestroy(OCRepPayload* payload)
1314 OICFree(payload->uri);
1315 OCFreeOCStringLL(payload->types);
1316 OCFreeOCStringLL(payload->interfaces);
1317 OCFreeRepPayloadValue(payload->values);
1318 OCRepPayloadDestroy(payload->next);
1322 OCDiscoveryPayload* OCDiscoveryPayloadCreate()
1324 OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
1331 payload->base.type = PAYLOAD_TYPE_DISCOVERY;
1336 OCSecurityPayload* OCSecurityPayloadCreate(const uint8_t* securityData, size_t size)
1338 OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1345 payload->base.type = PAYLOAD_TYPE_SECURITY;
1346 payload->securityData = (uint8_t *)OICCalloc(1, size);
1347 if (!payload->securityData)
1352 memcpy(payload->securityData, (uint8_t *)securityData, size);
1353 payload->payloadSize = size;
1358 void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
1365 OICFree(payload->securityData);
1369 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload)
1372 OCResourcePayload* p = payload->resources;
1381 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index)
1384 OCResourcePayload* p = payload->resources;
1398 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t securePort)
1400 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t securePort,
1404 OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
1410 pl->uri = OICStrdup(res->uri);
1414 OCDiscoveryResourceDestroy(pl);
1419 OCResourceType* typePtr = res->rsrcType;
1421 if (typePtr != NULL)
1423 pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1426 OCDiscoveryResourceDestroy(pl);
1429 pl->types->value = OICStrdup(typePtr->resourcetypename);
1430 if (!pl->types->value)
1432 OCDiscoveryResourceDestroy(pl);
1436 OCStringLL* cur = pl->types;
1437 typePtr = typePtr->next;
1440 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1443 OCDiscoveryResourceDestroy(pl);
1446 cur->next->value = OICStrdup(typePtr->resourcetypename);
1447 if (!cur->next->value)
1449 OCDiscoveryResourceDestroy(pl);
1453 typePtr = typePtr->next;
1458 OCResourceInterface* ifPtr = res->rsrcInterface;
1461 pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1462 if (!pl->interfaces)
1464 OCDiscoveryResourceDestroy(pl);
1467 pl->interfaces->value = OICStrdup(ifPtr->name);
1468 if (!pl->interfaces->value)
1470 OCDiscoveryResourceDestroy(pl);
1474 OCStringLL* cur = pl->interfaces;
1475 ifPtr = ifPtr->next;
1478 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1481 OCDiscoveryResourceDestroy(pl);
1484 cur->next->value = OICStrdup(ifPtr->name);
1485 if (!cur->next->value)
1487 OCDiscoveryResourceDestroy(pl);
1491 ifPtr = ifPtr->next;
1495 pl->bitmap = res->resourceProperties & (OC_OBSERVABLE | OC_DISCOVERABLE);
1496 pl->secure = (res->resourceProperties & OC_SECURE) != 0;
1497 pl->port = securePort;
1499 pl->tcpPort = tcpPort;
1505 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1506 uint16_t securePort)
1508 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, securePort));
1511 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1512 uint16_t securePort, uint16_t tcpPort)
1514 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, securePort, tcpPort));
1518 bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, const char *value)
1520 char *dup = OICStrdup(value);
1521 VERIFY_PARAM_NON_NULL(TAG, dup, "Failed copying string");
1522 VERIFY_PARAM_NON_NULL(TAG, value, "Invalid Parameters");
1526 *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1527 VERIFY_PARAM_NON_NULL(TAG, *stringLL, "Failed allocating memory");
1528 (*stringLL)->value = dup;
1533 OCStringLL *temp = *stringLL;
1538 temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
1539 VERIFY_PARAM_NON_NULL(TAG, temp->next, "Failed allocating memory");
1540 temp->next->value = dup;
1548 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
1555 if (!payload->resources)
1557 payload->resources = res;
1561 OCResourcePayload* p = payload->resources;
1570 void OCDiscoveryResourceDestroy(OCResourcePayload* payload)
1577 OICFree(payload->uri);
1578 OCFreeOCStringLL(payload->types);
1579 OCFreeOCStringLL(payload->interfaces);
1580 OCDiscoveryResourceDestroy(payload->next);
1584 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
1590 OICFree(payload->sid);
1591 OICFree(payload->baseURI);
1592 OICFree(payload->uri);
1593 OICFree(payload->type);
1594 OICFree(payload->name);
1595 OCFreeOCStringLL(payload->interface);
1596 OCDiscoveryResourceDestroy(payload->resources);
1600 OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
1601 const OCStringLL *types, const char* specVer, const char* dmVer)
1604 OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
1611 payload->base.type = PAYLOAD_TYPE_DEVICE;
1612 payload->sid = OICStrdup(sid);
1613 if (sid && !payload->sid)
1618 payload->deviceName = OICStrdup(dname);
1619 if (dname && !payload->deviceName)
1624 payload->specVersion = OICStrdup(specVer);
1625 if (specVer && !payload->specVersion)
1630 payload->dataModelVersion = OICStrdup(dmVer);
1631 if (dmVer && !payload->dataModelVersion)
1636 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_DEFAULT);
1637 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_READ);
1639 payload->types = CloneOCStringLL((OCStringLL *)types);
1640 if (types && !payload->types)
1648 OCDevicePayloadDestroy((OCDevicePayload*)payload);
1652 void OCDevicePayloadDestroy(OCDevicePayload* payload)
1659 OICFree(payload->sid);
1660 OICFree(payload->deviceName);
1661 OICFree(payload->specVersion);
1662 OICFree(payload->dataModelVersion);
1663 OCFreeOCStringLL(payload->types);
1664 OCFreeOCStringLL(payload->interfaces);
1668 static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
1670 if (!platformInfo || !target)
1675 target->info.platformID = OICStrdup(platformInfo->platformID);
1676 target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
1677 target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
1678 target->info.modelNumber = OICStrdup(platformInfo->modelNumber);
1679 target->info.dateOfManufacture = OICStrdup(platformInfo->dateOfManufacture);
1680 target->info.platformVersion = OICStrdup(platformInfo->platformVersion);
1681 target->info.operatingSystemVersion = OICStrdup(platformInfo->operatingSystemVersion);
1682 target->info.hardwareVersion = OICStrdup(platformInfo->hardwareVersion);
1683 target->info.firmwareVersion = OICStrdup(platformInfo->firmwareVersion);
1684 target->info.supportUrl = OICStrdup(platformInfo->supportUrl);
1685 target->info.systemTime = OICStrdup(platformInfo->systemTime);
1688 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(OCPlatformInfo* platformInfo)
1690 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1696 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1698 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1699 if (!payload->interfaces)
1703 payload->interfaces->value = OICStrdup(OC_RSRVD_INTERFACE_READ);
1704 payload->rt = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1709 payload->rt->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_PLATFORM);
1710 payload->info = *platformInfo;
1715 OCPlatformPayload* OCPlatformPayloadCreate(const OCPlatformInfo* platformInfo)
1717 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1724 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1725 OCResourcePayloadAddStringLL(&payload->rt, OC_RSRVD_RESOURCE_TYPE_PLATFORM);
1727 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_DEFAULT);
1728 OCResourcePayloadAddStringLL(&payload->interfaces, OC_RSRVD_INTERFACE_READ);
1730 OCCopyPlatformInfo(platformInfo, payload);
1735 void OCPlatformInfoDestroy(OCPlatformInfo *info)
1737 OICFree(info->platformID);
1738 OICFree(info->manufacturerName);
1739 OICFree(info->manufacturerUrl);
1740 OICFree(info->modelNumber);
1741 OICFree(info->dateOfManufacture);
1742 OICFree(info->platformVersion);
1743 OICFree(info->operatingSystemVersion);
1744 OICFree(info->hardwareVersion);
1745 OICFree(info->firmwareVersion);
1746 OICFree(info->supportUrl);
1747 OICFree(info->systemTime);
1750 void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
1756 OICFree(payload->uri);
1757 OCPlatformInfoDestroy(&payload->info);
1758 OCFreeOCStringLL(payload->rt);
1759 OCFreeOCStringLL(payload->interfaces);
1763 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
1764 OCPresenceTrigger trigger, const char* resourceType)
1766 OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
1772 payload->base.type = PAYLOAD_TYPE_PRESENCE;
1773 payload->sequenceNumber = seqNum;
1774 payload->maxAge = maxAge;
1775 payload->trigger = trigger;
1776 payload->resourceType = OICStrdup(resourceType);
1780 void OCPresencePayloadDestroy(OCPresencePayload* payload)
1786 OICFree(payload->resourceType);