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 "OCPayload"
33 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val);
34 static void FreeOCDiscoveryResource(OCResourcePayload* payload);
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 OC_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))
123 static void OCCopyPropertyValueArray(OCRepPayloadValue* dest, OCRepPayloadValue* source)
130 size_t dimTotal = calcDimTotal(source->arr.dimensions);
131 switch(source->arr.type)
134 dest->arr.iArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
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 memcpy(dest->arr.dArray, source->arr.dArray, dimTotal * sizeof(double));
141 case OCREP_PROP_BOOL:
142 dest->arr.bArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
143 memcpy(dest->arr.bArray, source->arr.bArray, dimTotal * sizeof(bool));
145 case OCREP_PROP_STRING:
146 dest->arr.strArray = (char**)OICMalloc(dimTotal * sizeof(char*));
147 for(size_t i = 0; i < dimTotal; ++i)
149 dest->arr.strArray[i] = OICStrdup(source->arr.strArray[i]);
152 case OCREP_PROP_ARRAY:
153 dest->arr.objArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
154 for(size_t i = 0; i < dimTotal; ++i)
156 dest->arr.objArray[i] = OCRepPayloadClone(source->arr.objArray[i]);
160 OC_LOG(ERROR, TAG, "CopyPropertyValueArray invalid type");
165 static void OCCopyPropertyValue (OCRepPayloadValue *dest, OCRepPayloadValue *source)
167 if (!source || !dest)
174 case OCREP_PROP_STRING:
175 dest->str = OICStrdup(source->str);
177 case OCREP_PROP_OBJECT:
178 dest->obj = OCRepPayloadClone(source->obj);
180 case OCREP_PROP_ARRAY:
181 OCCopyPropertyValueArray(dest, source);
184 // Nothing to do for the trivially copyable types.
189 static void OCFreeRepPayloadValueContents(OCRepPayloadValue* val)
196 if(val->type == OCREP_PROP_STRING)
200 else if (val->type == OCREP_PROP_OBJECT)
202 OCRepPayloadDestroy(val->obj);
204 else if (val->type == OCREP_PROP_ARRAY)
206 size_t dimTotal = calcDimTotal(val->arr.dimensions);
207 switch(val->arr.type)
210 case OCREP_PROP_DOUBLE:
211 case OCREP_PROP_BOOL:
212 // Since this is a union, iArray will
213 // point to all of the above
214 OICFree(val->arr.iArray);
216 case OCREP_PROP_STRING:
217 for(size_t i = 0; i< dimTotal;++i)
219 OICFree(val->arr.strArray[i]);
221 OICFree(val->arr.strArray);
223 case OCREP_PROP_OBJECT:
224 for(size_t i = 0; i< dimTotal;++i)
226 OCRepPayloadDestroy(val->arr.objArray[i]);
228 OICFree(val->arr.objArray);
230 case OCREP_PROP_NULL:
231 case OCREP_PROP_ARRAY:
232 OC_LOG_V(ERROR, TAG, "FreeRepPayloadValueContents: Illegal type\
233 inside an array: %d", val->arr.type);
239 static void OCFreeRepPayloadValue(OCRepPayloadValue* val)
247 OCFreeRepPayloadValueContents(val);
248 OCFreeRepPayloadValue(val->next);
251 static OCRepPayloadValue* OCRepPayloadValueClone (OCRepPayloadValue* source)
258 OCRepPayloadValue *sourceIter = source;
259 OCRepPayloadValue *destIter = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
265 OCRepPayloadValue *headOfClone = destIter;
267 // Copy payload type and non pointer types in union.
268 *destIter = *sourceIter;
269 destIter->name = OICStrdup (sourceIter->name);
270 OCCopyPropertyValue (destIter, sourceIter);
272 sourceIter = sourceIter->next;
276 destIter->next = (OCRepPayloadValue*) OICCalloc(1, sizeof(OCRepPayloadValue));
279 OCFreeRepPayloadValue (headOfClone);
283 *(destIter->next) = *sourceIter;
284 destIter->next->name = OICStrdup (sourceIter->name);
285 OCCopyPropertyValue (destIter->next, sourceIter);
287 sourceIter = sourceIter->next;
288 destIter = destIter->next;
293 static OCRepPayloadValue* OCRepPayloadFindAndSetValue(OCRepPayload* payload, const char* name,
294 OCRepPayloadPropType type)
296 if(!payload || !name)
301 OCRepPayloadValue* val = payload->values;
304 payload->values = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
309 payload->values->name = OICStrdup(name);
310 if(!payload->values->name)
312 OICFree(payload->values);
313 payload->values = NULL;
316 payload->values->type =type;
317 return payload->values;
322 if(0 == strcmp(val->name, name))
324 OCFreeRepPayloadValueContents(val);
328 else if(val->next == NULL)
330 val->next = (OCRepPayloadValue*)OICCalloc(1, sizeof(OCRepPayloadValue));
335 val->next->name = OICStrdup(name);
342 val->next->type =type;
349 OC_LOG(ERROR, TAG, "FindAndSetValue reached point after while loop, pointer corruption?");
353 bool OCRepPayloadAddResourceType(OCRepPayload* payload, const char* resourceType)
355 return OCRepPayloadAddResourceTypeAsOwner(payload, OICStrdup(resourceType));
358 bool OCRepPayloadAddResourceTypeAsOwner(OCRepPayload* payload, char* resourceType)
360 if(!payload || !resourceType)
367 OCStringLL* cur = payload->types;
372 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
379 cur->next->value = resourceType;
384 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
389 payload->types->value = resourceType;
394 bool OCRepPayloadAddInterface(OCRepPayload* payload, const char* interface)
396 return OCRepPayloadAddInterfaceAsOwner(payload, OICStrdup(interface));
399 bool OCRepPayloadAddInterfaceAsOwner(OCRepPayload* payload, char* interface)
401 if(!payload || !interface)
406 if(payload->interfaces)
408 OCStringLL* cur = payload->interfaces;
413 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
419 cur->next->value = interface;
424 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
425 if(!payload->interfaces)
429 payload->interfaces->value = interface;
434 bool OCRepPayloadSetUri(OCRepPayload* payload, const char* uri)
441 payload->uri = OICStrdup(uri);
442 return payload->uri != NULL;
445 bool OCRepPayloadIsNull(const OCRepPayload* payload, const char* name)
447 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
454 return val->type == OCREP_PROP_NULL;
457 static bool OCRepPayloadSetProp(OCRepPayload* payload, const char* name,
458 void* value, OCRepPayloadPropType type)
460 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, type);
468 val->i = *(int64_t*)value;
470 case OCREP_PROP_DOUBLE:
471 val->d = *(double*)value;
473 case OCREP_PROP_BOOL:
474 val->b = *(bool*)value;
476 case OCREP_PROP_OBJECT:
477 val->obj = (OCRepPayload*)value;
479 case OCREP_PROP_STRING:
480 val->str = (char*)value;
481 return val->str != NULL;
482 case OCREP_PROP_NULL:
484 case OCREP_PROP_ARRAY:
492 bool OCRepPayloadSetNull(OCRepPayload* payload, const char* name)
494 return OCRepPayloadSetProp(payload, name, NULL, OCREP_PROP_NULL);
497 bool OCRepPayloadSetPropInt(OCRepPayload* payload,
498 const char* name, int64_t value)
500 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_INT);
503 bool OCRepPayloadGetPropInt(const OCRepPayload* payload, const char* name, int64_t* value)
505 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
507 if(!val || val->type != OCREP_PROP_INT)
516 bool OCRepPayloadSetPropDouble(OCRepPayload* payload,
517 const char* name, double value)
519 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_DOUBLE);
522 bool OCRepPayloadGetPropDouble(const OCRepPayload* payload, const char* name, double* value)
524 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
526 if(!val || val->type != OCREP_PROP_DOUBLE)
535 bool OCRepPayloadSetPropString(OCRepPayload* payload, const char* name, const char* value)
537 char* temp = OICStrdup(value);
538 bool b = OCRepPayloadSetPropStringAsOwner(payload, name, temp);
547 bool OCRepPayloadSetPropStringAsOwner(OCRepPayload* payload, const char* name, char* value)
549 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_STRING);
552 bool OCRepPayloadGetPropString(const OCRepPayload* payload, const char* name, char** value)
554 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
556 if(!val || val->type != OCREP_PROP_STRING)
561 *value = OICStrdup(val->str);
562 return *value != NULL;
565 bool OCRepPayloadSetPropBool(OCRepPayload* payload,
566 const char* name, bool value)
568 return OCRepPayloadSetProp(payload, name, &value, OCREP_PROP_BOOL);
571 bool OCRepPayloadGetPropBool(const OCRepPayload* payload, const char* name, bool* value)
573 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
575 if(!val || val->type != OCREP_PROP_BOOL)
584 bool OCRepPayloadSetPropObject(OCRepPayload* payload, const char* name, const OCRepPayload* value)
586 OCRepPayload* temp = OCRepPayloadClone(value);
587 bool b = OCRepPayloadSetPropObjectAsOwner(payload, name, temp);
591 OCRepPayloadDestroy(temp);
596 bool OCRepPayloadSetPropObjectAsOwner(OCRepPayload* payload, const char* name, OCRepPayload* value)
598 return OCRepPayloadSetProp(payload, name, value, OCREP_PROP_OBJECT);
601 bool OCRepPayloadGetPropObject(const OCRepPayload* payload, const char* name, OCRepPayload** value)
603 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
605 if(!val || val->type != OCREP_PROP_OBJECT)
610 *value = OCRepPayloadClone(val->obj);
611 return *value != NULL;
614 size_t calcDimTotal(const size_t dimensions[MAX_REP_ARRAY_DEPTH])
616 if(dimensions[0] == 0)
622 for(uint8_t i = 0; i < MAX_REP_ARRAY_DEPTH && dimensions[i] != 0; ++i)
624 total *= dimensions[i];
629 bool OCRepPayloadSetIntArrayAsOwner(OCRepPayload* payload, const char* name,
630 int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
632 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
639 val->arr.type = OCREP_PROP_INT;
640 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
641 val->arr.iArray = array;
646 bool OCRepPayloadSetIntArray(OCRepPayload* payload, const char* name,
647 const int64_t* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
649 size_t dimTotal = calcDimTotal(dimensions);
655 int64_t* newArray = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
662 memcpy(newArray, array, dimTotal * sizeof(int64_t));
665 bool b = OCRepPayloadSetIntArrayAsOwner(payload, name, newArray, dimensions);
673 bool OCRepPayloadGetIntArray(const OCRepPayload* payload, const char* name,
674 int64_t** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
676 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
678 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_INT
684 size_t dimTotal = calcDimTotal(val->arr.dimensions);
689 *array = (int64_t*)OICMalloc(dimTotal * sizeof(int64_t));
695 memcpy(*array, val->arr.iArray, dimTotal * sizeof(int64_t));
696 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
700 bool OCRepPayloadSetDoubleArrayAsOwner(OCRepPayload* payload, const char* name,
701 double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
703 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
710 val->arr.type = OCREP_PROP_DOUBLE;
711 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
712 val->arr.dArray = array;
716 bool OCRepPayloadSetDoubleArray(OCRepPayload* payload, const char* name,
717 const double* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
719 size_t dimTotal = calcDimTotal(dimensions);
725 double* newArray = (double*)OICMalloc(dimTotal * sizeof(double));
732 memcpy(newArray, array, dimTotal * sizeof(double));
734 bool b = OCRepPayloadSetDoubleArrayAsOwner(payload, name, newArray, dimensions);
742 bool OCRepPayloadGetDoubleArray(const OCRepPayload* payload, const char* name,
743 double** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
745 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
747 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_DOUBLE
753 size_t dimTotal = calcDimTotal(val->arr.dimensions);
758 *array = (double*)OICMalloc(dimTotal * sizeof(double));
764 memcpy(*array, val->arr.dArray, dimTotal * sizeof(double));
765 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
769 bool OCRepPayloadSetStringArrayAsOwner(OCRepPayload* payload, const char* name,
770 char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
772 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
779 val->arr.type = OCREP_PROP_STRING;
780 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
781 val->arr.strArray = array;
785 bool OCRepPayloadSetStringArray(OCRepPayload* payload, const char* name,
786 const char** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
788 size_t dimTotal = calcDimTotal(dimensions);
794 char** newArray = (char**)OICMalloc(dimTotal * sizeof(char*));
801 for(size_t i = 0; i < dimTotal; ++i)
803 newArray[i] = OICStrdup(array[i]);
806 bool b = OCRepPayloadSetStringArrayAsOwner(payload, name, newArray, dimensions);
810 for(size_t i = 0; i < dimTotal; ++i)
812 OICFree(newArray[i]);
819 bool OCRepPayloadGetStringArray(const OCRepPayload* payload, const char* name,
820 char*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
822 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
824 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_STRING
825 || !val->arr.strArray)
830 size_t dimTotal = calcDimTotal(val->arr.dimensions);
835 *array = (char**)OICMalloc(dimTotal * sizeof(char*));
841 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
843 for(size_t i = 0; i < dimTotal; ++i)
845 (*array)[i] = OICStrdup(val->arr.strArray[i]);
852 bool OCRepPayloadSetBoolArrayAsOwner(OCRepPayload* payload, const char* name,
853 bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
856 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
863 val->arr.type = OCREP_PROP_BOOL;
864 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
865 val->arr.bArray = array;
869 bool OCRepPayloadSetBoolArray(OCRepPayload* payload, const char* name,
870 const bool* array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
872 size_t dimTotal = calcDimTotal(dimensions);
878 bool* newArray = (bool*)OICMalloc(dimTotal * sizeof(bool));
885 memcpy(newArray, array, dimTotal * sizeof(bool));
888 bool b = OCRepPayloadSetBoolArrayAsOwner(payload, name, newArray, dimensions);
896 bool OCRepPayloadGetBoolArray(const OCRepPayload* payload, const char* name,
897 bool** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
899 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
901 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_BOOL
907 size_t dimTotal = calcDimTotal(val->arr.dimensions);
912 *array = (bool*)OICMalloc(dimTotal * sizeof(bool));
918 memcpy(*array, val->arr.bArray, dimTotal * sizeof(bool));
919 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
923 bool OCRepPayloadSetPropObjectArrayAsOwner(OCRepPayload* payload, const char* name,
924 OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
926 OCRepPayloadValue* val = OCRepPayloadFindAndSetValue(payload, name, OCREP_PROP_ARRAY);
933 val->arr.type = OCREP_PROP_OBJECT;
934 memcpy(val->arr.dimensions, dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
935 val->arr.objArray = array;
940 bool OCRepPayloadSetPropObjectArray(OCRepPayload* payload, const char* name,
941 const OCRepPayload** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
943 size_t dimTotal = calcDimTotal(dimensions);
949 OCRepPayload** newArray = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
956 for(size_t i = 0; i < dimTotal; ++i)
958 newArray[i] = OCRepPayloadClone(array[i]);
961 bool b = OCRepPayloadSetPropObjectArrayAsOwner(payload, name, newArray, dimensions);
965 for(size_t i = 0; i < dimTotal; ++i)
967 OCRepPayloadDestroy(newArray[i]);
974 bool OCRepPayloadGetPropObjectArray(const OCRepPayload* payload, const char* name,
975 OCRepPayload*** array, size_t dimensions[MAX_REP_ARRAY_DEPTH])
977 OCRepPayloadValue* val = OCRepPayloadFindValue(payload, name);
979 if(!val || val->type != OCREP_PROP_ARRAY || val->arr.type != OCREP_PROP_OBJECT
980 || !val->arr.objArray)
985 size_t dimTotal = calcDimTotal(val->arr.dimensions);
990 *array = (OCRepPayload**)OICMalloc(dimTotal * sizeof(OCRepPayload*));
996 memcpy(dimensions, val->arr.dimensions, MAX_REP_ARRAY_DEPTH * sizeof(size_t));
998 for(size_t i = 0; i < dimTotal; ++i)
1000 (*array)[i] = OCRepPayloadClone(val->arr.objArray[i]);
1006 void OCFreeOCStringLL(OCStringLL* ll)
1013 OCFreeOCStringLL(ll->next);
1018 OCStringLL* CloneOCStringLL (OCStringLL* ll)
1025 OCStringLL *sourceIter = ll;
1027 OCStringLL *destIter = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1032 destIter->value = OICStrdup (sourceIter->value);
1034 OCStringLL *headOfClone = destIter;
1036 sourceIter = sourceIter->next;
1040 destIter->next = (OCStringLL*)OICCalloc (1, sizeof (OCStringLL));
1041 if (!destIter->next)
1043 OCFreeOCStringLL (headOfClone);
1046 destIter->next->value = OICStrdup (sourceIter->value);
1048 destIter = destIter->next;
1049 sourceIter = sourceIter->next;
1054 OCRepPayload* OCRepPayloadClone (const OCRepPayload* payload)
1061 OCRepPayload *clone = OCRepPayloadCreate();
1068 clone->uri = OICStrdup (payload->uri);
1069 clone->types = CloneOCStringLL (payload->types);
1070 clone->interfaces = CloneOCStringLL (payload->interfaces);
1071 clone->values = OCRepPayloadValueClone (payload->values);
1077 void OCRepPayloadDestroy(OCRepPayload* payload)
1084 OICFree(payload->uri);
1085 OCFreeOCStringLL(payload->types);
1086 OCFreeOCStringLL(payload->interfaces);
1087 OCFreeRepPayloadValue(payload->values);
1088 OCRepPayloadDestroy(payload->next);
1092 OCDiscoveryPayload* OCDiscoveryPayloadCreate()
1094 OCDiscoveryPayload* payload = (OCDiscoveryPayload*)OICCalloc(1, sizeof(OCDiscoveryPayload));
1101 payload->base.type = PAYLOAD_TYPE_DISCOVERY;
1106 OCSecurityPayload* OCSecurityPayloadCreate(const char* securityData)
1108 OCSecurityPayload* payload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1115 payload->base.type = PAYLOAD_TYPE_SECURITY;
1116 payload->securityData = OICStrdup(securityData);
1121 void OCSecurityPayloadDestroy(OCSecurityPayload* payload)
1128 OICFree(payload->securityData);
1132 size_t OCDiscoveryPayloadGetResourceCount(OCDiscoveryPayload* payload)
1135 OCResourcePayload* p = payload->resources;
1144 OCResourcePayload* OCDiscoveryPayloadGetResource(OCDiscoveryPayload* payload, size_t index)
1147 OCResourcePayload* p = payload->resources;
1160 static OCResourcePayload* OCCopyResource(const OCResource* res, uint16_t port)
1162 OCResourcePayload* pl = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
1168 pl->uri = OICStrdup(res->uri);
1169 pl->sid = (uint8_t*)OICCalloc(1, UUID_SIZE);
1170 if(!pl->uri || ! pl->sid)
1172 FreeOCDiscoveryResource(pl);
1175 memcpy(pl->sid, OCGetServerInstanceID(), UUID_SIZE);
1178 OCResourceType* typePtr = res->rsrcType;
1182 pl->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1185 FreeOCDiscoveryResource(pl);
1188 pl->types->value = OICStrdup(typePtr->resourcetypename);
1189 if(!pl->types->value)
1191 FreeOCDiscoveryResource(pl);
1195 OCStringLL* cur = pl->types;
1196 typePtr = typePtr->next;
1199 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1202 FreeOCDiscoveryResource(pl);
1205 cur->next->value = OICStrdup(typePtr->resourcetypename);
1206 if(!cur->next->value)
1208 FreeOCDiscoveryResource(pl);
1212 typePtr = typePtr->next;
1217 OCResourceInterface* ifPtr = res->rsrcInterface;
1220 pl->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1223 FreeOCDiscoveryResource(pl);
1226 pl->interfaces->value = OICStrdup(ifPtr->name);
1227 if(!pl->interfaces->value)
1229 FreeOCDiscoveryResource(pl);
1233 OCStringLL* cur = pl->interfaces;
1234 ifPtr = ifPtr->next;
1237 cur->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1240 FreeOCDiscoveryResource(pl);
1243 cur->next->value = OICStrdup(ifPtr->name);
1244 if(!cur->next->value)
1246 FreeOCDiscoveryResource(pl);
1250 ifPtr = ifPtr->next;
1254 pl->bitmap = res->resourceProperties & (OC_OBSERVABLE | OC_DISCOVERABLE);
1255 pl->secure = (res->resourceProperties & OC_SECURE) != 0;
1261 void OCDiscoveryPayloadAddResource(OCDiscoveryPayload* payload, const OCResource* res,
1264 OCDiscoveryPayloadAddNewResource(payload, OCCopyResource(res, port));
1267 bool OCResourcePayloadAddResourceType(OCResourcePayload* payload, const char* resourceType)
1274 char* dup = OICStrdup(resourceType);
1281 if (!payload->types)
1283 payload->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1285 if (!payload->types)
1291 payload->types->value = dup;
1297 OCStringLL* temp = payload->types;
1304 temp->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1311 temp->next->value = dup;
1316 bool OCResourcePayloadAddInterface(OCResourcePayload* payload, const char* interface)
1323 char* dup = OICStrdup(interface);
1330 if (!payload->interfaces)
1332 payload->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1334 if (!payload->interfaces)
1340 payload->interfaces->value = dup;
1346 OCStringLL* temp = payload->interfaces;
1353 temp->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1360 temp->next->value = dup;
1365 void OCDiscoveryPayloadAddNewResource(OCDiscoveryPayload* payload, OCResourcePayload* res)
1367 if(!payload->resources)
1369 payload->resources = res;
1373 OCResourcePayload* p = payload->resources;
1382 static void FreeOCDiscoveryResource(OCResourcePayload* payload)
1389 OICFree(payload->uri);
1390 OICFree(payload->sid);
1391 OCFreeOCStringLL(payload->types);
1392 OCFreeOCStringLL(payload->interfaces);
1393 FreeOCDiscoveryResource(payload->next);
1397 void OCDiscoveryPayloadDestroy(OCDiscoveryPayload* payload)
1404 FreeOCDiscoveryResource(payload->resources);
1408 OCDevicePayload* OCDevicePayloadCreate(const char* uri, const uint8_t* sid, const char* dname,
1409 const char* specVer, const char* dmVer)
1412 OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
1419 payload->base.type = PAYLOAD_TYPE_DEVICE;
1421 payload->uri = OICStrdup(uri);
1422 if(uri && !payload->uri)
1429 payload->sid = (uint8_t*)OICMalloc(UUID_SIZE);
1434 memcpy(payload->sid, sid, UUID_SIZE);
1437 payload->deviceName = OICStrdup(dname);
1438 if(dname && !payload->deviceName)
1443 payload->specVersion = OICStrdup(specVer);
1444 if(specVer && !payload->specVersion)
1449 payload->dataModelVersion = OICStrdup(dmVer);
1450 if(dmVer && !payload->dataModelVersion)
1458 OCDevicePayloadDestroy((OCDevicePayload*)payload);
1462 void OCDevicePayloadDestroy(OCDevicePayload* payload)
1469 OICFree(payload->uri);
1470 OICFree(payload->sid);
1471 OICFree(payload->deviceName);
1472 OICFree(payload->specVersion);
1473 OICFree(payload->dataModelVersion);
1477 static void OCCopyPlatformInfo(const OCPlatformInfo* platformInfo, OCPlatformPayload* target)
1479 if(!platformInfo || !target)
1484 target->info.platformID = OICStrdup(platformInfo->platformID);
1485 target->info.manufacturerName = OICStrdup(platformInfo->manufacturerName);
1486 target->info.manufacturerUrl = OICStrdup(platformInfo->manufacturerUrl);
1487 target->info.modelNumber = OICStrdup(platformInfo->modelNumber);
1488 target->info.dateOfManufacture = OICStrdup(platformInfo->dateOfManufacture);
1489 target->info.platformVersion = OICStrdup(platformInfo->platformVersion);
1490 target->info.operatingSystemVersion = OICStrdup(platformInfo->operatingSystemVersion);
1491 target->info.hardwareVersion = OICStrdup(platformInfo->hardwareVersion);
1492 target->info.firmwareVersion = OICStrdup(platformInfo->firmwareVersion);
1493 target->info.supportUrl = OICStrdup(platformInfo->supportUrl);
1494 target->info.systemTime = OICStrdup(platformInfo->systemTime);
1497 OCPlatformPayload* OCPlatformPayloadCreateAsOwner(char* uri, OCPlatformInfo* platformInfo)
1499 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1505 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1507 payload->info = *platformInfo;
1512 OCPlatformPayload* OCPlatformPayloadCreate(const char* uri, const OCPlatformInfo* platformInfo)
1514 OCPlatformPayload* payload = (OCPlatformPayload*)OICCalloc(1, sizeof(OCPlatformPayload));
1521 payload->base.type = PAYLOAD_TYPE_PLATFORM;
1522 payload->uri = OICStrdup(uri);
1523 OCCopyPlatformInfo(platformInfo, payload);
1528 void OCPlatformPayloadDestroy(OCPlatformPayload* payload)
1534 OICFree(payload->uri);
1535 OICFree(payload->info.platformID);
1536 OICFree(payload->info.manufacturerName);
1537 OICFree(payload->info.manufacturerUrl);
1538 OICFree(payload->info.modelNumber);
1539 OICFree(payload->info.dateOfManufacture);
1540 OICFree(payload->info.platformVersion);
1541 OICFree(payload->info.operatingSystemVersion);
1542 OICFree(payload->info.hardwareVersion);
1543 OICFree(payload->info.firmwareVersion);
1544 OICFree(payload->info.supportUrl);
1545 OICFree(payload->info.systemTime);
1549 OCPresencePayload* OCPresencePayloadCreate(uint32_t seqNum, uint32_t maxAge,
1550 OCPresenceTrigger trigger, const char* resourceType)
1552 OCPresencePayload* payload = (OCPresencePayload*)OICCalloc(1, sizeof(OCPresencePayload));
1558 payload->base.type = PAYLOAD_TYPE_PRESENCE;
1559 payload->sequenceNumber = seqNum;
1560 payload->maxAge = maxAge;
1561 payload->trigger = trigger;
1562 payload->resourceType = OICStrdup(resourceType);
1566 void OCPresencePayloadDestroy(OCPresencePayload* payload)
1572 OICFree(payload->resourceType);