1 //******************************************************************
3 // Copyright 2014 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1-2001 base
24 // specification (excluding the XSI extension).
25 // For POSIX.1-2001 base specification,
26 // Refer http://pubs.opengroup.org/onlinepubs/009695399/
27 #define _POSIX_C_SOURCE 200112L
28 #include "ocresource.h"
30 #include "ocresourcehandler.h"
31 #include "ocobserve.h"
32 #include "occollection.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
37 #include "ocpayload.h"
40 #include "cainterface.h"
44 #define TAG PCF("ocresource")
45 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
46 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
48 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
49 TAG, PCF(#arg " is NULL")); return (retVal); } }
51 extern OCResource *headResource;
52 static OCPlatformInfo savedPlatformInfo = {};
53 static OCDeviceInfo savedDeviceInfo = {};
55 //-----------------------------------------------------------------------------
56 // Default resource entity handler function
57 //-----------------------------------------------------------------------------
58 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
59 OCEntityHandlerRequest * request, void* callbackParam)
61 //TODO ("Implement me!!!!");
62 // TODO: remove silence unused param warnings
66 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
69 /* This method will retrieve the port at which the secure resource is hosted */
70 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
74 if (endpoint->adapter == OC_ADAPTER_IP)
76 if (endpoint->flags & OC_IP_USE_V6)
78 p = caglobals.ip.u6s.port;
80 else if (endpoint->flags & OC_IP_USE_V4)
82 p = caglobals.ip.u4s.port;
91 * Function will extract 0, 1 or 2 filters from query.
92 * More than 2 filters or unsupported filters will result in error.
93 * If both filters are of the same supported type, the 2nd one will be picked.
94 * Resource and device filters in the SAME query are NOT validated
95 * and resources will likely not clear filters.
97 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
102 char *restOfQuery = NULL;
103 int numKeyValuePairsParsed = 0;
108 OC_LOG_V(INFO, TAG, "Extracting params from %s", query);
110 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
114 if (numKeyValuePairsParsed >= 2)
116 OC_LOG(ERROR, TAG, PCF("More than 2 queries params in URI."));
117 return OC_STACK_INVALID_QUERY;
120 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
124 return OC_STACK_INVALID_QUERY;
126 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
128 *filterOne = value; // if
130 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
132 *filterTwo = value; // rt
136 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
137 return OC_STACK_INVALID_QUERY;
139 ++numKeyValuePairsParsed;
141 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
144 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
148 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
150 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
152 return OC_WELL_KNOWN_URI;
154 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
156 return OC_DEVICE_URI;
158 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
160 return OC_PLATFORM_URI;
162 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
164 return OC_RESOURCE_TYPES_URI;
167 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
171 #endif //WITH_PRESENCE
172 return OC_UNKNOWN_URI;
175 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
176 char **filterOne, char **filterTwo)
178 if(!filterOne || !filterTwo)
180 return OC_STACK_INVALID_PARAM;
187 if (uri == OC_PRESENCE)
189 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
190 OC_LOG(INFO, TAG, PCF("OC_PRESENCE Request for virtual resource."));
195 OCStackResult result = OC_STACK_OK;
199 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
205 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
206 OCRepPayload** payload)
208 OCRepPayload *tempPayload = OCRepPayloadCreate();
212 OCRepPayloadDestroy(tempPayload);
213 return OC_STACK_INVALID_PARAM;
218 return OC_STACK_NO_MEMORY;
221 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
223 OCResourceType *resType = resourcePtr->rsrcType;
226 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
227 resType = resType->next;
230 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
233 OCRepPayloadAddInterface(tempPayload, resInterface->name);
234 resInterface = resInterface->next;
237 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
240 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
241 resAttrib->attrValue);
242 resAttrib = resAttrib->next;
247 *payload = tempPayload;
251 OCRepPayloadAppend(*payload, tempPayload);
257 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
258 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
260 if (!resourcePtr || !payload)
262 return OC_STACK_INVALID_PARAM;
265 if (resourcePtr->resourceProperties & OC_SECURE)
267 if (GetSecurePortInfo(devAddr, &port) != OC_STACK_OK)
273 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
278 uint8_t IsCollectionResource (OCResource *resource)
285 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
287 if (resource->rsrcResources[i])
295 OCResource *FindResourceByUri(const char* resourceUri)
302 OCResource * pointer = headResource;
305 if (strcmp(resourceUri, pointer->uri) == 0)
309 pointer = pointer->next;
311 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
316 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
317 ResourceHandling *handling,
318 OCResource **resource)
320 if(!request || !handling || !resource)
322 return OC_STACK_INVALID_PARAM;
325 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
327 const OCDevAddr *devAddr = &request->devAddr;
329 // Check if virtual resource
330 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
332 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
333 *handling = OC_RESOURCE_VIRTUAL;
334 *resource = headResource;
337 if (strlen((const char*)(request->resourceUrl)) == 0)
339 // Resource URL not specified
340 *handling = OC_RESOURCE_NOT_SPECIFIED;
341 return OC_STACK_NO_RESOURCE;
345 OCResource *resourcePtr = NULL;
346 resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
347 *resource = resourcePtr;
350 if(defaultDeviceHandler)
352 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
356 // Resource does not exist
357 // and default device handler does not exist
358 *handling = OC_RESOURCE_NOT_SPECIFIED;
359 return OC_STACK_NO_RESOURCE;
362 // secure resource will entertain only authorized requests
363 if ((resourcePtr->resourceProperties & OC_SECURE) && ((devAddr->flags & OC_FLAG_SECURE) == 0))
365 OC_LOG(ERROR, TAG, PCF("Un-authorized request. Ignoring"));
366 return OC_STACK_RESOURCE_ERROR;
369 if (IsCollectionResource (resourcePtr))
371 // Collection resource
372 if (resourcePtr->entityHandler != defaultResourceEHandler)
374 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
379 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
385 // Resource not a collection
386 if (resourcePtr->entityHandler != defaultResourceEHandler)
388 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
393 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
400 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
402 OCStackResult result;
407 result = OC_STACK_OK;
410 result = OC_STACK_SLOW_RESOURCE;
413 result = OC_STACK_ERROR;
415 case OC_EH_FORBIDDEN:
416 result = OC_STACK_RESOURCE_ERROR;
418 case OC_EH_RESOURCE_CREATED:
419 result = OC_STACK_RESOURCE_CREATED;
421 case OC_EH_RESOURCE_DELETED:
422 result = OC_STACK_RESOURCE_DELETED;
424 case OC_EH_RESOURCE_NOT_FOUND:
425 result = OC_STACK_NO_RESOURCE;
428 result = OC_STACK_ERROR;
434 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
441 // Null or empty is analogous to no filter.
442 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
447 OCResourceType *resourceTypePtr = resource->rsrcType;
449 while (resourceTypePtr)
451 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
455 resourceTypePtr = resourceTypePtr->next;
458 OC_LOG_V(INFO, TAG, PCF("%s does not contain rt=%s."), resource->uri, resourceTypeFilter);
462 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
469 // Null or empty is analogous to no filter.
470 if (interfaceFilter == NULL || *interfaceFilter == 0)
475 OCResourceInterface *interfacePtr = resource->rsrcInterface;
479 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
483 interfacePtr = interfacePtr->next;
486 OC_LOG_V(INFO, TAG, PCF("%s does not contain if=%s."), resource->uri, interfaceFilter);
491 * If the filters are null, they will be assumed to NOT be present
492 * and the resource will not be matched against them.
493 * Function will return true if all non null AND non empty filters passed in find a match.
495 static bool includeThisResourceInResponse(OCResource *resource,
496 char *interfaceFilter,
497 char *resourceTypeFilter)
501 OC_LOG(ERROR, TAG, PCF("Invalid resource"));
505 if ( !(resource->resourceProperties & OC_ACTIVE) ||
506 !(resource->resourceProperties & OC_DISCOVERABLE))
508 OC_LOG_V(INFO, TAG, PCF("%s not ACTIVE or DISCOVERABLE"), resource->uri);
512 return resourceMatchesIFFilter(resource, interfaceFilter) &&
513 resourceMatchesRTFilter(resource, resourceTypeFilter);
517 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
518 OCPayload *discoveryPayload)
520 OCEntityHandlerResponse response = {};
522 response.ehResult = OC_EH_OK;
523 response.payload = discoveryPayload;
524 response.persistentBufferFlag = 0;
525 response.requestHandle = (OCRequestHandle) request;
526 response.resourceHandle = (OCResourceHandle) resource;
528 return OCDoResponse(&response);
531 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
533 if (!request || !resource)
535 return OC_STACK_INVALID_PARAM;
538 OCStackResult discoveryResult = OC_STACK_ERROR;
539 OCPayload* payload = NULL;
540 char *filterOne = NULL;
541 char *filterTwo = NULL;
543 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
545 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
548 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
550 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
551 &filterOne, &filterTwo);
552 if (discoveryResult != OC_STACK_OK)
554 OC_LOG_V(ERROR, TAG, "Error (%d) validating query.\n", discoveryResult);
555 return discoveryResult;
557 payload = (OCPayload*)OCDiscoveryPayloadCreate();
561 return OC_STACK_NO_MEMORY;
565 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
567 if(includeThisResourceInResponse(resource, filterOne, filterTwo))
569 discoveryResult = BuildVirtualResourceResponse(resource,
570 (OCDiscoveryPayload*)payload,
575 else if (virtualUriInRequest == OC_DEVICE_URI)
577 payload = (OCPayload*)OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
578 OCGetServerInstanceID(), savedDeviceInfo.deviceName,
579 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
582 discoveryResult = OC_STACK_NO_MEMORY;
586 discoveryResult = OC_STACK_OK;
589 else if (virtualUriInRequest == OC_PLATFORM_URI)
591 payload = (OCPayload*)OCPlatformPayloadCreate(
592 OC_RSRVD_PLATFORM_URI,
596 discoveryResult = OC_STACK_NO_MEMORY;
600 discoveryResult = OC_STACK_OK;
607 if(resource->resourceProperties & OC_ACTIVE)
609 discoveryResult = SendPresenceNotification(resource->rsrcType,
610 OC_PRESENCE_TRIGGER_CHANGE);
615 // Presence uses observer notification api to respond via SendPresenceNotification.
616 if (virtualUriInRequest != OC_PRESENCE)
618 if(discoveryResult == OC_STACK_OK)
620 discoveryResult = SendNonPersistantDiscoveryResponse(request, resource,
622 OCPayloadDestroy(payload);
626 OC_LOG_V(ERROR, TAG, "Error (%d) building (%d) discovery response. "\
627 "Not responding to request.", discoveryResult, virtualUriInRequest);
631 return discoveryResult;
635 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
639 return OC_STACK_INVALID_PARAM;
642 OCStackResult result = OC_STACK_OK;
643 OCEntityHandlerResult ehResult = OC_EH_ERROR;
644 OCEntityHandlerRequest ehRequest = {};
646 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
647 result = FormOCEntityHandlerRequest(&ehRequest,
648 (OCRequestHandle) request,
651 (OCResourceHandle) NULL, request->query,
653 request->payloadSize,
654 request->numRcvdVendorSpecificHeaderOptions,
655 request->rcvdVendorSpecificHeaderOptions,
656 (OCObserveAction)request->observationOption,
658 VERIFY_SUCCESS(result, OC_STACK_OK);
660 // At this point we know for sure that defaultDeviceHandler exists
661 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
662 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
663 if(ehResult == OC_EH_SLOW)
665 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
666 request->slowFlag = 1;
668 else if(ehResult == OC_EH_ERROR)
670 FindAndDeleteServerRequest(request);
672 result = EntityHandlerCodeToOCStackCode(ehResult);
678 HandleResourceWithEntityHandler (OCServerRequest *request,
679 OCResource *resource,
680 uint8_t collectionResource)
682 if(!request || ! resource)
684 return OC_STACK_INVALID_PARAM;
687 OCStackResult result = OC_STACK_ERROR;
688 OCEntityHandlerResult ehResult = OC_EH_ERROR;
689 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
690 ResourceObserver *resObs = NULL;
692 OCEntityHandlerRequest ehRequest = {};
694 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
696 result = FormOCEntityHandlerRequest(&ehRequest,
697 (OCRequestHandle)request,
700 (OCResourceHandle)resource,
703 request->payloadSize,
704 request->numRcvdVendorSpecificHeaderOptions,
705 request->rcvdVendorSpecificHeaderOptions,
706 (OCObserveAction)request->observationOption,
708 VERIFY_SUCCESS(result, OC_STACK_OK);
710 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
712 OC_LOG(INFO, TAG, PCF("No observation requested"));
713 ehFlag = OC_REQUEST_FLAG;
715 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
717 OC_LOG(INFO, TAG, PCF("Observation registration requested"));
719 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
720 VERIFY_SUCCESS(result, OC_STACK_OK);
722 result = AddObserver ((const char*)(request->resourceUrl),
723 (const char *)(request->query),
724 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
725 resource, request->qos,
728 if(result == OC_STACK_OK)
730 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
731 request->observeResult = OC_STACK_OK;
732 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
736 result = OC_STACK_OK;
738 // The error in observeResult for the request will be used when responding to this
739 // request by omitting the observation option/sequence number.
740 request->observeResult = OC_STACK_ERROR;
741 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
742 ehFlag = OC_REQUEST_FLAG;
746 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
749 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
751 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
755 // Stack does not contain this observation request
756 // Either token is incorrect or observation list is corrupted
757 result = OC_STACK_ERROR;
760 ehRequest.obsInfo.obsId = resObs->observeId;
761 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
763 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
765 if(result == OC_STACK_OK)
767 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
768 request->observeResult = OC_STACK_OK;
772 result = OC_STACK_OK;
773 request->observeResult = OC_STACK_ERROR;
774 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
779 result = OC_STACK_ERROR;
783 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
784 if(ehResult == OC_EH_SLOW)
786 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
787 request->slowFlag = 1;
789 else if(ehResult == OC_EH_ERROR)
791 FindAndDeleteServerRequest(request);
793 result = EntityHandlerCodeToOCStackCode(ehResult);
799 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
800 OCResource *resource)
802 if(!request || !resource)
804 return OC_STACK_INVALID_PARAM;
807 OCStackResult result = OC_STACK_ERROR;
808 OCEntityHandlerRequest ehRequest = {};
810 result = FormOCEntityHandlerRequest(&ehRequest,
811 (OCRequestHandle)request,
814 (OCResourceHandle)resource,
817 request->payloadSize,
818 request->numRcvdVendorSpecificHeaderOptions,
819 request->rcvdVendorSpecificHeaderOptions,
820 (OCObserveAction)request->observationOption,
822 if(result != OC_STACK_OK)
827 return (DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest));
831 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
833 OCStackResult ret = OC_STACK_OK;
837 case OC_RESOURCE_VIRTUAL:
839 ret = HandleVirtualResource (request, resource);
842 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
844 ret = HandleDefaultDeviceEntityHandler(request);
847 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
849 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
850 return OC_STACK_ERROR;
852 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
854 ret = HandleResourceWithEntityHandler (request, resource, 0);
857 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
859 ret = HandleResourceWithEntityHandler (request, resource, 1);
862 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
864 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
867 case OC_RESOURCE_NOT_SPECIFIED:
869 ret = OC_STACK_NO_RESOURCE;
874 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
875 return OC_STACK_ERROR;
881 void DeletePlatformInfo()
883 OC_LOG(INFO, TAG, PCF("Deleting platform info."));
885 OICFree(savedPlatformInfo.platformID);
886 savedPlatformInfo.platformID = NULL;
888 OICFree(savedPlatformInfo.manufacturerName);
889 savedPlatformInfo.manufacturerName = NULL;
891 OICFree(savedPlatformInfo.manufacturerUrl);
892 savedPlatformInfo.manufacturerUrl = NULL;
894 OICFree(savedPlatformInfo.modelNumber);
895 savedPlatformInfo.modelNumber = NULL;
897 OICFree(savedPlatformInfo.dateOfManufacture);
898 savedPlatformInfo.dateOfManufacture = NULL;
900 OICFree(savedPlatformInfo.platformVersion);
901 savedPlatformInfo.platformVersion = NULL;
903 OICFree(savedPlatformInfo.operatingSystemVersion);
904 savedPlatformInfo.operatingSystemVersion = NULL;
906 OICFree(savedPlatformInfo.hardwareVersion);
907 savedPlatformInfo.hardwareVersion = NULL;
909 OICFree(savedPlatformInfo.firmwareVersion);
910 savedPlatformInfo.firmwareVersion = NULL;
912 OICFree(savedPlatformInfo.supportUrl);
913 savedPlatformInfo.supportUrl = NULL;
915 OICFree(savedPlatformInfo.systemTime);
916 savedPlatformInfo.systemTime = NULL;
919 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
921 savedPlatformInfo.platformID = OICStrdup(info.platformID);
922 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
923 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
924 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
925 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
926 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
927 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
928 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
929 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
930 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
931 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
933 if ((!savedPlatformInfo.platformID && info.platformID)||
934 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
935 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
936 (!savedPlatformInfo.modelNumber && info.modelNumber)||
937 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
938 (!savedPlatformInfo.platformVersion && info.platformVersion)||
939 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
940 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
941 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
942 (!savedPlatformInfo.supportUrl && info.supportUrl)||
943 (!savedPlatformInfo.systemTime && info.systemTime))
945 DeletePlatformInfo();
946 return OC_STACK_INVALID_PARAM;
953 OCStackResult SavePlatformInfo(OCPlatformInfo info)
955 DeletePlatformInfo();
957 OCStackResult res = DeepCopyPlatFormInfo(info);
959 if (res != OC_STACK_OK)
961 OC_LOG_V(ERROR, TAG, PCF("Failed to save platform info. errno(%d)"), res);
965 OC_LOG(ERROR, TAG, PCF("Platform info saved."));
971 void DeleteDeviceInfo()
973 OC_LOG(INFO, TAG, PCF("Deleting device info."));
975 OICFree(savedDeviceInfo.deviceName);
976 savedDeviceInfo.deviceName = NULL;
979 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
981 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
983 if(!savedDeviceInfo.deviceName && info.deviceName)
986 return OC_STACK_NO_MEMORY;
992 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
994 OCStackResult res = OC_STACK_OK;
998 res = DeepCopyDeviceInfo(info);
1000 VERIFY_SUCCESS(res, OC_STACK_OK);
1002 if(OCGetServerInstanceID() == NULL)
1004 OC_LOG(INFO, TAG, PCF("Device ID generation failed"));
1005 res = OC_STACK_ERROR;
1009 OC_LOG(INFO, TAG, PCF("Device initialized successfully."));