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"
31 #define TAG "OCPayload"
32 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val);
33 static void FreeOCDiscoveryResource(OCResourcePayload* payload);
35 void OCPayloadDestroy(OCPayload* payload)
44 case PAYLOAD_TYPE_REPRESENTATION:
45 OCRepPayloadDestroy((OCRepPayload*)payload);
47 case PAYLOAD_TYPE_DISCOVERY:
48 OCDiscoveryPayloadDestroy((OCDiscoveryPayload*)payload);
50 case PAYLOAD_TYPE_DEVICE:
51 OCDevicePayloadDestroy((OCDevicePayload*)payload);
53 case PAYLOAD_TYPE_PLATFORM:
54 OCPlatformPayloadDestroy((OCPlatformPayload*)payload);
56 case PAYLOAD_TYPE_PRESENCE:
57 OCPresencePayloadDestroy((OCPresencePayload*)payload);
59 case PAYLOAD_TYPE_SECURITY:
60 OCSecurityPayloadDestroy((OCSecurityPayload*)payload);
63 OC_LOG_V(ERROR, TAG, "Unsupported payload type in destroy: %d", payload->type);
68 OCRepPayload* OCRepPayloadCreate()
70 OCRepPayload* payload = (OCRepPayload*)OICCalloc(1, sizeof(OCRepPayload));
77 payload->base.type = PAYLOAD_TYPE_REPRESENTATION;
82 void OCRepPayloadAppend(OCRepPayload* parent, OCRepPayload* child)
91 parent = parent->next;
98 static OCRepPayloadValue* OCRepPayloadFindValue(const OCRepPayload* payload, const char* name)
100 if(!payload || !name)
105 OCRepPayloadValue* val = payload->values;
108 if(0 == strcmp(val->name, name))
119 static void OCCopyPropertyValueArray(OCRepPayloadValue* dest, OCRepPayloadValue* source)
121 size_t dimTotal = calcDimTotal(source->arr.dimensions);
122 switch(source->arr.type)
125 dest->arr.iArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
126 memcpy(dest->arr.iArray, source->arr.iArray, dimTotal * sizeof(int64_t));
128 case OCREP_PROP_DOUBLE:
129 dest->arr.dArray = (double*)OICMalloc(dimTotal * sizeof(double));
130 memcpy(dest->arr.dArray, source->arr.dArray, dimTotal * sizeof(double));
132 case OCREP_PROP_BOOL:
133 dest->arr.bArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
134 memcpy(dest->arr.bArray, source->arr.bArray, dimTotal * sizeof(bool));
136 case OCREP_PROP_STRING:
137 dest->arr.strArray = (char**)OICMalloc(dimTotal * sizeof(char*));
138 for(size_t i = 0; i < dimTotal; ++i)
140 dest->arr.strArray[i] = OICStrdup(source->arr.strArray[i]);
143 case OCREP_PROP_ARRAY:
144 dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
145 for(size_t i = 0; i < dimTotal; ++i)
147 dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
151 OC_LOG(ERROR, TAG, PCF("CopyPropertyValueArray invalid type"));
156 static void OCCopyPropertyValue (OCRepPayloadValue *dest, OCRepPayloadValue *source)
158 if (!source || !dest)
165 case OCREP_PROP_STRING:
166 dest->str = OICStrdup(source->str);
168 case OCREP_PROP_OBJECT:
169 dest->obj = OCRepPayloadClone(source->obj);
171 case OCREP_PROP_ARRAY:
172 OCCopyPropertyValueArray(dest, source);
175 // Nothing to do for the trivially copyable types.
180 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
187 if(val->type == OCREP_PROP_STRING)
191 else if (val->type == OCREP_PROP_OBJECT)
193 OCRepPayloadDestroy(val->obj);
195 else if (val->type == OCREP_PROP_ARRAY)
197 size_t dimTotal = calcDimTotal(val->arr.dimensions);
198 switch(val->arr.type)
201 case OCREP_PROP_DOUBLE:
202 case OCREP_PROP_BOOL:
203 // Since this is a union, iArray will
204 // point to all of the above
205 OICFree(val->arr.iArray);
207 case OCREP_PROP_STRING:
208 for(size_t i = 0; i< dimTotal;++i)
210 OICFree(val->arr.strArray[i]);
212 OICFree(val->arr.strArray);
214 case OCREP_PROP_OBJECT:
215 for(size_t i = 0; i< dimTotal;++i)
217 OCRepPayloadDestroy(val->arr.objArray[i]);
219 OICFree(val->arr.objArray);
221 case OCREP_PROP_NULL:
222 case OCREP_PROP_ARRAY:
223 OC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
224 inside an array: %d", val->arr.type);
230 static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
238 OCFreeRepPayloadValueContents(val);
239 OCFreeRepPayloadValue(val->next);
242 static OCRepPayloadValue* OCRepPayloadValueClone (OCRepPayloadValue* source)
249 OCRepPayloadValue *sourceIter = source;
250 OCRepPayloadValue *destIter = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
256 OCRepPayloadValue *headOfClone = destIter;
258 // Copy payload type and non pointer types in union.
259 *destIter = *sourceIter;
260 destIter->name = OICStrdup (sourceIter->name);
261 OCCopyPropertyValue (destIter, sourceIter);
263 sourceIter = sourceIter->next;
267 destIter->next = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
270 OCFreeRepPayloadValue (headOfClone);
274 *(destIter->next) = *sourceIter;
275 destIter->next->name = OICStrdup (sourceIter->name);
276 OCCopyPropertyValue (destIter->next, sourceIter);
278 sourceIter = sourceIter->next;
279 destIter = destIter->next;
284 static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
285 OCRepPayloadPropType type)
287 if(!payload || !name)
292 OCRepPayloadValue* val = payload->values;
295 payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
300 payload->values->name = OICStrdup(name);
301 if(!payload->values->name)
303 OICFree(payload->values);
304 payload->values = NULL;
307 payload->values->type =type;
308 return payload->values;
313 if(0 == strcmp(val->name, name))
315 OCFreeRepPayloadValueContents(val);
319 else if(val->next == NULL)
321 val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
326 val->next->name = OICStrdup(name);
333 val->next->type =type;
340 OC_LOG(ERROR, TAG, PCF("FindAndSetValue reached point after while loop, pointer corruption?"));
344 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType)
346 return OCRepPayloadAddResourceTypeAsOwner(payload, OICStrdup(resourceType));
349 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
351 if(!payload || !resourceType)
358 OCStringLL* cur = payload->types;
363 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
370 cur->next->value = resourceType;
375 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
380 payload->types->value = resourceType;
385 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* interface)
387 return OCRepPayloadAddInterfaceAsOwner(payload, OICStrdup(interface));
390 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
392 if(!payload || !interface)
397 if(payload->interfaces)
399 OCStringLL* cur = payload->interfaces;
404 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
410 cur->next->value = interface;
415 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
416 if(!payload->interfaces)
420 payload->interfaces->value = interface;
425 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
432 payload->uri = OICStrdup(uri);
433 return payload->uri != NULL;
436 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name)
438 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
445 return val->type == OCREP_PROP_NULL;
448 static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
449 void* value, OCRepPayloadPropType type)
451 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
459 val->i = *(int64_t*)value;
461 case OCREP_PROP_DOUBLE:
462 val->d = *(double*)value;
464 case OCREP_PROP_BOOL:
465 val->b = *(bool*)value;
467 case OCREP_PROP_OBJECT:
468 val->obj = (OCRepPayload*)value;
470 case OCREP_PROP_STRING:
471 val->str = (char*)value;
472 return val->str != NULL;
473 case OCREP_PROP_NULL:
475 case OCREP_PROP_ARRAY:
483 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
485 return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
488 bool OCRepPayloadSetPropInt(OCRepPayload* payload,
489 const char* name, int64_t value)
491 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
494 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
496 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
498 if(!val || val->type != OCREP_PROP_INT)
507 bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
508 const char* name, double value)
510 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
513 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
515 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
517 if(!val || val->type != OCREP_PROP_DOUBLE)
526 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
528 char* temp = OICStrdup(value);
529 bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
538 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
540 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
543 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, const char** value)
545 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
547 if(!val || val->type != OCREP_PROP_STRING)
552 *value = OICStrdup(val->str);
553 return *value != NULL;
556 bool OCRepPayloadSetPropBool(OCRepPayload* payload,
557 const char* name, bool value)
559 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
562 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
564 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
566 if(!val || val->type != OCREP_PROP_BOOL)
575 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value)
577 OCRepPayload* temp = OCRepPayloadClone(value);
578 bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
582 OCRepPayloadDestroy(temp);
587 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
589 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
592 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
594 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
596 if(!val || val->type != OCREP_PROP_OBJECT)
601 *value = OCRepPayloadClone(val->obj);
602 return *value != NULL;
605 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
607 if(dimensions[0] == 0)
613 for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
615 total *= dimensions[i];
620 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
621 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
623 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
630 val->arr.type = OCREP_PROP_INT;
631 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
632 val->arr.iArray = array;
637 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
638 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
640 size_t dimTotal = calcDimTotal(dimensions);
646 int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
653 memcpy(newArray, array, dimTotal * sizeof(int64_t));
656 bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
664 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
665 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
667 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
669 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
675 size_t dimTotal = calcDimTotal(val->arr.dimensions);
680 *array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
686 memcpy(*array, val->arr.iArray, dimTotal * sizeof(int64_t));
687 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
691 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
692 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
694 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
701 val->arr.type = OCREP_PROP_DOUBLE;
702 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
703 val->arr.dArray = array;
707 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
708 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
710 size_t dimTotal = calcDimTotal(dimensions);
716 double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
723 memcpy(newArray, array, dimTotal * sizeof(double));
725 bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
733 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
734 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
736 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
738 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_DOUBLE
744 size_t dimTotal = calcDimTotal(val->arr.dimensions);
749 *array = (double*)OICMalloc(dimTotal * sizeof(double));
755 memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
756 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
760 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
761 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
763 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
770 val->arr.type = OCREP_PROP_STRING;
771 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
772 val->arr.strArray = array;
776 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
777 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
779 size_t dimTotal = calcDimTotal(dimensions);
785 char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
792 for(size_t i = 0; i < dimTotal; ++i)
794 newArray[i] = OICStrdup(array[i]);
797 bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
801 for(size_t i = 0; i < dimTotal; ++i)
803 OICFree(newArray[i]);
810 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
811 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
813 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
815 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
816 || !val->arr.strArray)
821 size_t dimTotal = calcDimTotal(val->arr.dimensions);
826 *array = (char**)OICMalloc(dimTotal * sizeof(char*));
832 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
834 for(size_t i = 0; i < dimTotal; ++i)
836 (*array)[i] = OICStrdup(val->arr.strArray[i]);
843 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
844 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
847 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
854 val->arr.type = OCREP_PROP_BOOL;
855 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
856 val->arr.bArray = array;
860 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
861 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
863 size_t dimTotal = calcDimTotal(dimensions);
869 bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
876 memcpy(newArray, array, dimTotal * sizeof(bool));
879 bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
887 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
888 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
890 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
892 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
898 size_t dimTotal = calcDimTotal(val->arr.dimensions);
903 *array = (bool*)OICMalloc(dimTotal * sizeof(bool));
909 memcpy(*array, val->arr.bArray, dimTotal * sizeof(bool));
910 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
914 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
915 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
917 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
924 val->arr.type = OCREP_PROP_OBJECT;
925 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
926 val->arr.objArray = array;
931 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
932 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
934 size_t dimTotal = calcDimTotal(dimensions);
940 OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
947 for(size_t i = 0; i < dimTotal; ++i)
949 newArray[i] = OCRepPayloadClone(array[i]);
952 bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
956 for(size_t i = 0; i < dimTotal; ++i)
958 OCRepPayloadDestroy(newArray[i]);
965 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
966 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
968 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
970 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
971 || !val->arr.objArray)
976 size_t dimTotal = calcDimTotal(val->arr.dimensions);
981 *array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
987 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
989 for(size_t i = 0; i < dimTotal; ++i)
991 (*array)[i] = OCRepPayloadClone(val->arr.objArray[i]);
997 void OCFreeOCStringLL(OCStringLL* ll)
1004 OCFreeOCStringLL(ll->next);
1009 OCStringLL* CloneOCStringLL (OCStringLL* ll)
1016 OCStringLL *sourceIter = ll;
1018 OCStringLL *destIter = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1023 destIter->value = OICStrdup (sourceIter->value);
1025 OCStringLL *headOfClone = destIter;
1027 sourceIter = sourceIter->next;
1031 destIter->next = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1032 if (!destIter->next)
1034 OCFreeOCStringLL (headOfClone);
1037 destIter->next->value = OICStrdup (sourceIter->value);
1039 destIter = destIter->next;
1040 sourceIter = sourceIter->next;
1045 OCRepPayload* OCRepPayloadClone (const OCRepPayload* payload)
1052 OCRepPayload *clone = OCRepPayloadCreate();
1059 clone->uri = OICStrdup (payload->uri);
1060 clone->types = CloneOCStringLL (payload->types);
1061 clone->interfaces = CloneOCStringLL (payload->interfaces);
1062 clone->values = OCRepPayloadValueClone (payload->values);
1068 void OCRepPayloadDestroy(OCRepPayload* payload)
1075 OICFree(payload->uri);
1076 OCFreeOCStringLL(payload->types);
1077 OCFreeOCStringLL(payload->interfaces);
1078 OCFreeRepPayloadValue(payload->values);
1079 OCRepPayloadDestroy(payload->next);
1083 OCDiscoveryPayload* OCDiscoveryPayloadCreate()
1085 OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
1092 payload->base.type = PAYLOAD_TYPE_DISCOVERY;
1097 OCSecurityPayload* OCSecurityPayloadCreate(const char* securityData)
1099 OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1106 payload->base.type = PAYLOAD_TYPE_SECURITY;
1107 payload->securityData = OICStrdup(securityData);
1112 void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
1119 OICFree(payload->securityData);
1123 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload)
1126 OCResourcePayload* p = payload->resources;
1135 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index)
1138 OCResourcePayload* p = payload->resources;
1151 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t port)
1153 OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
1159 pl->uri = OICStrdup(res->uri);
1160 pl->sid = (uint8_t*)OICCalloc(1, UUID_SIZE);
1161 if(!pl->uri || ! pl->sid)
1163 FreeOCDiscoveryResource(pl);
1166 memcpy(pl->sid, OCGetServerInstanceID(), UUID_SIZE);
1169 OCResourceType* typePtr = res->rsrcType;
1173 pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1176 FreeOCDiscoveryResource(pl);
1179 pl->types->value = OICStrdup(typePtr->resourcetypename);
1180 if(!pl->types->value)
1182 FreeOCDiscoveryResource(pl);
1186 OCStringLL* cur = pl->types;
1187 typePtr = typePtr->next;
1190 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1193 FreeOCDiscoveryResource(pl);
1196 cur->next->value = OICStrdup(typePtr->resourcetypename);
1197 if(!cur->next->value)
1199 FreeOCDiscoveryResource(pl);
1203 typePtr = typePtr->next;
1208 OCResourceInterface* ifPtr = res->rsrcInterface;
1211 pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1214 FreeOCDiscoveryResource(pl);
1217 pl->interfaces->value = OICStrdup(ifPtr->name);
1218 if(!pl->interfaces->value)
1220 FreeOCDiscoveryResource(pl);
1224 OCStringLL* cur = pl->interfaces;
1225 ifPtr = ifPtr->next;
1228 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1231 FreeOCDiscoveryResource(pl);
1234 cur->next->value = OICStrdup(ifPtr->name);
1235 if(!cur->next->value)
1237 FreeOCDiscoveryResource(pl);
1241 ifPtr = ifPtr->next;
1245 pl->bitmap = res->resourceProperties & (OC_OBSERVABLE | OC_DISCOVERABLE);
1246 pl->secure = (res->resourceProperties & OC_SECURE) != 0;
1252 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1255 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, port));
1258 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
1260 if(!payload->resources)
1262 payload->resources = res;
1266 OCResourcePayload* p = payload->resources;
1275 static void FreeOCDiscoveryResource(OCResourcePayload* payload)
1282 OICFree(payload->uri);
1283 OICFree(payload->sid);
1284 OCFreeOCStringLL(payload->types);
1285 OCFreeOCStringLL(payload->interfaces);
1286 FreeOCDiscoveryResource(payload->next);
1290 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
1297 FreeOCDiscoveryResource(payload->resources);
1301 OCDevicePayload* OCDevicePayloadCreate(const char* uri, const uint8_t* sid, const char* dname,
1302 const char* specVer, const char* dmVer)
1305 OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
1312 payload->base.type = PAYLOAD_TYPE_DEVICE;
1314 payload->uri = OICStrdup(uri);
1315 if(uri && !payload->uri)
1322 payload->sid = (uint8_t*)OICMalloc(UUID_SIZE);
1327 memcpy(payload->sid, sid, UUID_SIZE);
1330 payload->deviceName = OICStrdup(dname);
1331 if(dname && !payload->deviceName)
1336 payload->specVersion = OICStrdup(specVer);
1337 if(specVer && !payload->specVersion)
1342 payload->dataModelVersion = OICStrdup(dmVer);
1343 if(dmVer && !payload->dataModelVersion)
1351 OCDevicePayloadDestroy((OCDevicePayload*)payload);
1355 void OCDevicePayloadDestroy(OCDevicePayload* payload)
1362 OICFree(payload->uri);
1363 OICFree(payload->sid);
1364 OICFree(payload->deviceName);
1365 OICFree(payload->specVersion);
1366 OICFree(payload->dataModelVersion);
1370 static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
1372 target->info.platformID = OICStrdup(platformInfo->platformID);
1373 target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
1374 target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
1375 target->info.modelNumber = OICStrdup(platformInfo->modelNumber);
1376 target->info.dateOfManufacture = OICStrdup(platformInfo->dateOfManufacture);
1377 target->info.platformVersion = OICStrdup(platformInfo->platformVersion);
1378 target->info.operatingSystemVersion = OICStrdup(platformInfo->operatingSystemVersion);
1379 target->info.hardwareVersion = OICStrdup(platformInfo->hardwareVersion);
1380 target->info.firmwareVersion = OICStrdup(platformInfo->firmwareVersion);
1381 target->info.supportUrl = OICStrdup(platformInfo->supportUrl);
1382 target->info.systemTime = OICStrdup(platformInfo->systemTime);
1385 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(char* uri, OCPlatformInfo* platformInfo)
1387 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1393 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1395 payload->info = *platformInfo;
1400 OCPlatformPayload* OCPlatformPayloadCreate(const char* uri, const OCPlatformInfo* platformInfo)
1402 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1409 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1410 payload->uri = OICStrdup(uri);
1411 OCCopyPlatformInfo(platformInfo, payload);
1416 void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
1422 OICFree(payload->uri);
1423 OICFree(payload->info.platformID);
1424 OICFree(payload->info.manufacturerName);
1425 OICFree(payload->info.manufacturerUrl);
1426 OICFree(payload->info.modelNumber);
1427 OICFree(payload->info.dateOfManufacture);
1428 OICFree(payload->info.platformVersion);
1429 OICFree(payload->info.operatingSystemVersion);
1430 OICFree(payload->info.hardwareVersion);
1431 OICFree(payload->info.firmwareVersion);
1432 OICFree(payload->info.supportUrl);
1433 OICFree(payload->info.systemTime);
1437 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
1438 OCPresenceTrigger trigger, const char* resourceType)
1440 OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
1446 payload->base.type = PAYLOAD_TYPE_PRESENCE;
1447 payload->sequenceNumber = seqNum;
1448 payload->maxAge = maxAge;
1449 payload->trigger = trigger;
1450 payload->resourceType = OICStrdup(resourceType);
1454 void OCPresencePayloadDestroy(OCPresencePayload* payload)
1460 OICFree(payload->resourceType);