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 static const char * VIRTUAL_RSRCS[] =
66 //-----------------------------------------------------------------------------
67 // Default resource entity handler function
68 //-----------------------------------------------------------------------------
69 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
70 OCEntityHandlerRequest * request, void* callbackParam)
72 //TODO ("Implement me!!!!");
73 // TODO: remove silence unused param warnings
77 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
80 /* This method will retrieve the port at which the secure resource is hosted */
81 static OCStackResult GetSecurePortInfo(CATransportAdapter_t connType, uint16_t *port)
83 CAEndpoint_t* info = NULL;
85 OCStackResult ret = OC_STACK_ERROR;
87 CAResult_t caResult = CAGetNetworkInformation(&info, &size);
88 if ((caResult == CA_STATUS_OK) && info && size)
92 if ((info[size].flags & CA_SECURE) && info[size].adapter == connType)
94 if (info[size].adapter == CA_ADAPTER_IP)
96 *port = info[size].port;
109 * Function will extract 0, 1 or 2 filters from query.
110 * More than 2 filters or unsupported filters will result in error.
111 * If both filters are of the same supported type, the 2nd one will be picked.
112 * Resource and device filters in the SAME query are NOT validated
113 * and resources will likely not clear filters.
115 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
120 char *restOfQuery = NULL;
121 int numKeyValuePairsParsed = 0;
126 OC_LOG_V(INFO, TAG, PCF("Received query %s for param extraction"), query);
128 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
132 if (numKeyValuePairsParsed >= 2)
134 OC_LOG(ERROR, TAG, PCF("More than 2 queries params in URI."));
135 return OC_STACK_INVALID_QUERY;
138 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
142 return OC_STACK_INVALID_QUERY;
144 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
146 *filterOne = value; // if
148 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
150 *filterTwo = value; // rt
154 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
155 return OC_STACK_INVALID_QUERY;
157 ++numKeyValuePairsParsed;
159 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
162 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
166 static OCVirtualResources GetTypeOfVirtualURI(char *uriInRequest)
168 if (strcmp (uriInRequest, GetVirtualResourceUri(OC_WELL_KNOWN_URI)) == 0)
170 return OC_WELL_KNOWN_URI;
172 else if (strcmp (uriInRequest, GetVirtualResourceUri(OC_DEVICE_URI)) == 0)
174 return OC_DEVICE_URI;
176 else if (strcmp (uriInRequest, GetVirtualResourceUri(OC_PLATFORM_URI)) == 0)
178 return OC_PLATFORM_URI;
189 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
190 char **filterOne, char **filterTwo)
192 if(!filterOne || !filterTwo)
194 return OC_STACK_INVALID_PARAM;
201 if (uri == OC_PRESENCE)
203 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
204 OC_LOG(INFO, TAG, PCF("OC_PRESENCE Request for virtual resource."));
209 OCStackResult result = OC_STACK_OK;
213 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
219 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
220 OCDiscoveryPayload* payload, CATransportAdapter_t adapter )
222 if (!resourcePtr || !payload)
224 return OC_STACK_INVALID_PARAM;
227 if (resourcePtr->resourceProperties & OC_SECURE)
229 if(GetSecurePortInfo (adapter, &port) != OC_STACK_OK)
235 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
238 const char * GetVirtualResourceUri( OCVirtualResources resource)
240 if (resource < OC_MAX_VIRTUAL_RESOURCES)
242 return VIRTUAL_RSRCS[resource];
248 bool IsVirtualResource(const char* resourceUri)
255 for (int i = 0; i < OC_MAX_VIRTUAL_RESOURCES; i++)
257 if (strcmp(resourceUri, GetVirtualResourceUri((OCVirtualResources)i)) == 0)
265 uint8_t IsCollectionResource (OCResource *resource)
272 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
274 if (resource->rsrcResources[i])
282 OCResource *FindResourceByUri(const char* resourceUri)
289 OCResource * pointer = headResource;
292 if (strcmp(resourceUri, pointer->uri) == 0)
296 pointer = pointer->next;
298 OC_LOG(INFO, TAG, PCF("Resource not found"));
303 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
304 ResourceHandling *handling,
305 OCResource **resource)
307 if(!request || !handling || !resource)
309 return OC_STACK_INVALID_PARAM;
312 OC_LOG(INFO, TAG, PCF("Entering DetermineResourceHandling"));
314 const OCDevAddr *devAddr = &request->devAddr;
316 // Check if virtual resource
317 if (IsVirtualResource((const char*)request->resourceUrl))
319 *handling = OC_RESOURCE_VIRTUAL;
320 *resource = headResource;
323 if (strlen((const char*)(request->resourceUrl)) == 0)
325 // Resource URL not specified
326 *handling = OC_RESOURCE_NOT_SPECIFIED;
327 return OC_STACK_NO_RESOURCE;
331 OCResource *resourcePtr = NULL;
332 resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
333 *resource = resourcePtr;
336 if(defaultDeviceHandler)
338 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
342 // Resource does not exist
343 // and default device handler does not exist
344 *handling = OC_RESOURCE_NOT_SPECIFIED;
345 return OC_STACK_NO_RESOURCE;
348 // secure resource will entertain only authorized requests
349 if ((resourcePtr->resourceProperties & OC_SECURE) && ((devAddr->flags & OC_FLAG_SECURE) == 0))
351 OC_LOG(ERROR, TAG, PCF("Un-authorized request. Ignoring"));
352 return OC_STACK_RESOURCE_ERROR;
355 if (IsCollectionResource (resourcePtr))
357 // Collection resource
358 if (resourcePtr->entityHandler != defaultResourceEHandler)
360 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
365 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
371 // Resource not a collection
372 if (resourcePtr->entityHandler != defaultResourceEHandler)
374 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
379 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
386 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
388 OCStackResult result;
393 result = OC_STACK_OK;
396 result = OC_STACK_SLOW_RESOURCE;
399 result = OC_STACK_ERROR;
401 case OC_EH_FORBIDDEN:
402 result = OC_STACK_RESOURCE_ERROR;
404 case OC_EH_RESOURCE_CREATED:
405 result = OC_STACK_RESOURCE_CREATED;
407 case OC_EH_RESOURCE_DELETED:
408 result = OC_STACK_RESOURCE_DELETED;
410 case OC_EH_RESOURCE_NOT_FOUND:
411 result = OC_STACK_NO_RESOURCE;
414 result = OC_STACK_ERROR;
420 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
427 // Null or empty is analogous to no filter.
428 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
433 OCResourceType *resourceTypePtr = resource->rsrcType;
435 while (resourceTypePtr)
437 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
441 resourceTypePtr = resourceTypePtr->next;
444 OC_LOG_V(INFO, TAG, PCF("%s does not contain rt=%s."), resource->uri, resourceTypeFilter);
448 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
455 // Null or empty is analogous to no filter.
456 if (interfaceFilter == NULL || *interfaceFilter == 0)
461 OCResourceInterface *interfacePtr = resource->rsrcInterface;
465 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
469 interfacePtr = interfacePtr->next;
472 OC_LOG_V(INFO, TAG, PCF("%s does not contain if=%s."), resource->uri, interfaceFilter);
477 * If the filters are null, they will be assumed to NOT be present
478 * and the resource will not be matched against them.
479 * Function will return true if all non null AND non empty filters passed in find a match.
481 static bool includeThisResourceInResponse(OCResource *resource,
482 char *interfaceFilter,
483 char *resourceTypeFilter)
487 OC_LOG(ERROR, TAG, PCF("Invalid resource"));
491 if ( !(resource->resourceProperties & OC_ACTIVE) ||
492 !(resource->resourceProperties & OC_DISCOVERABLE))
494 OC_LOG_V(INFO, TAG, PCF("%s not ACTIVE or DISCOVERABLE"), resource->uri);
498 return resourceMatchesIFFilter(resource, interfaceFilter) &&
499 resourceMatchesRTFilter(resource, resourceTypeFilter);
503 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
504 OCPayload *discoveryPayload)
506 OCEntityHandlerResponse response = {};
508 response.ehResult = OC_EH_OK;
509 response.payload = discoveryPayload;
510 response.persistentBufferFlag = 0;
511 response.requestHandle = (OCRequestHandle) request;
512 response.resourceHandle = (OCResourceHandle) resource;
514 return OCDoResponse(&response);
517 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
519 if (!request || !resource)
521 return OC_STACK_INVALID_PARAM;
524 OCStackResult discoveryResult = OC_STACK_ERROR;
525 OCPayload* payload = NULL;
526 char *filterOne = NULL;
527 char *filterTwo = NULL;
529 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
531 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
534 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
536 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
537 &filterOne, &filterTwo);
538 if (discoveryResult != OC_STACK_OK)
540 OC_LOG_V(ERROR, TAG, "Error (%d) validating query.\n", discoveryResult);
541 return discoveryResult;
543 payload = (OCPayload*)OCDiscoveryPayloadCreate();
547 return OC_STACK_NO_MEMORY;
551 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
553 if(includeThisResourceInResponse(resource, filterOne, filterTwo))
555 discoveryResult = BuildVirtualResourceResponse(resource,
556 (OCDiscoveryPayload*)payload,
557 (CATransportAdapter_t)request->devAddr.adapter);
561 else if (virtualUriInRequest == OC_DEVICE_URI)
563 payload = (OCPayload*)OCDevicePayloadCreate(GetVirtualResourceUri(OC_DEVICE_URI),
564 OCGetServerInstanceID(), savedDeviceInfo.deviceName,
565 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
568 discoveryResult = OC_STACK_NO_MEMORY;
571 else if (virtualUriInRequest == OC_PLATFORM_URI)
573 OCPlatformPayload* payload = OCPlatformPayloadCreate(
574 GetVirtualResourceUri(OC_PLATFORM_URI),
578 discoveryResult = OC_STACK_NO_MEMORY;
585 if(resource->resourceProperties & OC_ACTIVE)
587 discoveryResult = SendPresenceNotification(resource->rsrcType,
588 OC_PRESENCE_TRIGGER_CHANGE);
593 // Presence uses observer notification api to respond via SendPresenceNotification.
594 if (virtualUriInRequest != OC_PRESENCE)
596 if(discoveryResult == OC_STACK_OK)
598 discoveryResult = SendNonPersistantDiscoveryResponse(request, resource,
600 OCPayloadDestroy(payload);
604 OC_LOG_V(ERROR, TAG, "Error (%d) building (%d) discovery response. "\
605 "Not responding to request.", discoveryResult, virtualUriInRequest);
609 return discoveryResult;
613 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
617 return OC_STACK_INVALID_PARAM;
620 OCStackResult result = OC_STACK_OK;
621 OCEntityHandlerResult ehResult = OC_EH_ERROR;
622 OCEntityHandlerRequest ehRequest = {};
624 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
625 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
626 request->method, (OCResourceHandle) NULL, request->query,
627 request->payload, request->payloadSize,
628 request->numRcvdVendorSpecificHeaderOptions,
629 request->rcvdVendorSpecificHeaderOptions,
630 (OCObserveAction)request->observationOption, (OCObservationId)0);
631 VERIFY_SUCCESS(result, OC_STACK_OK);
633 // At this point we know for sure that defaultDeviceHandler exists
634 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
635 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
636 if(ehResult == OC_EH_SLOW)
638 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
639 request->slowFlag = 1;
641 else if(ehResult == OC_EH_ERROR)
643 FindAndDeleteServerRequest(request);
645 result = EntityHandlerCodeToOCStackCode(ehResult);
651 HandleResourceWithEntityHandler (OCServerRequest *request,
652 OCResource *resource,
653 uint8_t collectionResource)
655 if(!request || ! resource)
657 return OC_STACK_INVALID_PARAM;
660 OCStackResult result = OC_STACK_ERROR;
661 OCEntityHandlerResult ehResult = OC_EH_ERROR;
662 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
663 ResourceObserver *resObs = NULL;
665 OCEntityHandlerRequest ehRequest = {};
667 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
669 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
670 request->method, (OCResourceHandle) resource, request->query,
671 request->payload, request->payloadSize, request->numRcvdVendorSpecificHeaderOptions,
672 request->rcvdVendorSpecificHeaderOptions,
673 (OCObserveAction)request->observationOption, 0);
674 VERIFY_SUCCESS(result, OC_STACK_OK);
676 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
678 OC_LOG(INFO, TAG, PCF("No observation requested"));
679 ehFlag = OC_REQUEST_FLAG;
681 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
683 OC_LOG(INFO, TAG, PCF("Observation registration requested"));
685 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
686 VERIFY_SUCCESS(result, OC_STACK_OK);
688 result = AddObserver ((const char*)(request->resourceUrl),
689 (const char *)(request->query),
690 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
691 resource, request->qos,
694 if(result == OC_STACK_OK)
696 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
697 request->observeResult = OC_STACK_OK;
698 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
702 result = OC_STACK_OK;
704 // The error in observeResult for the request will be used when responding to this
705 // request by omitting the observation option/sequence number.
706 request->observeResult = OC_STACK_ERROR;
707 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
708 ehFlag = OC_REQUEST_FLAG;
712 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
715 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
717 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
721 // Stack does not contain this observation request
722 // Either token is incorrect or observation list is corrupted
723 result = OC_STACK_ERROR;
726 ehRequest.obsInfo.obsId = resObs->observeId;
727 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
729 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
731 if(result == OC_STACK_OK)
733 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
734 request->observeResult = OC_STACK_OK;
738 result = OC_STACK_OK;
739 request->observeResult = OC_STACK_ERROR;
740 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
745 result = OC_STACK_ERROR;
749 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
750 if(ehResult == OC_EH_SLOW)
752 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
753 request->slowFlag = 1;
755 else if(ehResult == OC_EH_ERROR)
757 FindAndDeleteServerRequest(request);
759 result = EntityHandlerCodeToOCStackCode(ehResult);
765 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
766 OCResource *resource)
768 if(!request || !resource)
770 return OC_STACK_INVALID_PARAM;
773 OCStackResult result = OC_STACK_ERROR;
774 OCEntityHandlerRequest ehRequest = {};
776 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
777 request->method, (OCResourceHandle) resource, request->query,
778 request->payload, request->payloadSize, request->numRcvdVendorSpecificHeaderOptions,
779 request->rcvdVendorSpecificHeaderOptions,
780 (OCObserveAction)request->observationOption, (OCObservationId) 0);
781 if(result != OC_STACK_OK)
786 return (DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest));
790 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
792 OCStackResult ret = OC_STACK_OK;
796 case OC_RESOURCE_VIRTUAL:
798 ret = HandleVirtualResource (request, resource);
801 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
803 ret = HandleDefaultDeviceEntityHandler(request);
806 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
808 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
809 return OC_STACK_ERROR;
811 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
813 ret = HandleResourceWithEntityHandler (request, resource, 0);
816 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
818 ret = HandleResourceWithEntityHandler (request, resource, 1);
821 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
823 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
826 case OC_RESOURCE_NOT_SPECIFIED:
828 ret = OC_STACK_NO_RESOURCE;
833 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
834 return OC_STACK_ERROR;
840 void DeletePlatformInfo()
842 OC_LOG(INFO, TAG, PCF("Deleting platform info."));
844 OICFree(savedPlatformInfo.platformID);
845 savedPlatformInfo.platformID = NULL;
847 OICFree(savedPlatformInfo.manufacturerName);
848 savedPlatformInfo.manufacturerName = NULL;
850 OICFree(savedPlatformInfo.manufacturerUrl);
851 savedPlatformInfo.manufacturerUrl = NULL;
853 OICFree(savedPlatformInfo.modelNumber);
854 savedPlatformInfo.modelNumber = NULL;
856 OICFree(savedPlatformInfo.dateOfManufacture);
857 savedPlatformInfo.dateOfManufacture = NULL;
859 OICFree(savedPlatformInfo.platformVersion);
860 savedPlatformInfo.platformVersion = NULL;
862 OICFree(savedPlatformInfo.operatingSystemVersion);
863 savedPlatformInfo.operatingSystemVersion = NULL;
865 OICFree(savedPlatformInfo.hardwareVersion);
866 savedPlatformInfo.hardwareVersion = NULL;
868 OICFree(savedPlatformInfo.firmwareVersion);
869 savedPlatformInfo.firmwareVersion = NULL;
871 OICFree(savedPlatformInfo.supportUrl);
872 savedPlatformInfo.supportUrl = NULL;
874 OICFree(savedPlatformInfo.systemTime);
875 savedPlatformInfo.systemTime = NULL;
878 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
880 savedPlatformInfo.platformID = OICStrdup(info.platformID);
881 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
882 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
883 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
884 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
885 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
886 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
887 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
888 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
889 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
890 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
892 if ((!savedPlatformInfo.platformID && info.platformID)||
893 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
894 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
895 (!savedPlatformInfo.modelNumber && info.modelNumber)||
896 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
897 (!savedPlatformInfo.platformVersion && info.platformVersion)||
898 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
899 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
900 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
901 (!savedPlatformInfo.supportUrl && info.supportUrl)||
902 (!savedPlatformInfo.systemTime && info.systemTime))
904 DeletePlatformInfo();
905 return OC_STACK_INVALID_PARAM;
912 OCStackResult SavePlatformInfo(OCPlatformInfo info)
914 DeletePlatformInfo();
916 OCStackResult res = DeepCopyPlatFormInfo(info);
918 if (res != OC_STACK_OK)
920 OC_LOG_V(ERROR, TAG, PCF("Failed to save platform info. errno(%d)"), res);
924 OC_LOG(ERROR, TAG, PCF("Platform info saved."));
930 void DeleteDeviceInfo()
932 OC_LOG(INFO, TAG, PCF("Deleting device info."));
934 OICFree(savedDeviceInfo.deviceName);
935 savedDeviceInfo.deviceName = NULL;
938 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
940 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
942 if(!savedDeviceInfo.deviceName && info.deviceName)
945 return OC_STACK_NO_MEMORY;
951 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
953 OCStackResult res = OC_STACK_OK;
957 res = DeepCopyDeviceInfo(info);
959 VERIFY_SUCCESS(res, OC_STACK_OK);
961 if(OCGetServerInstanceID() == NULL)
963 OC_LOG(INFO, TAG, PCF("Device ID generation failed"));
964 res = OC_STACK_ERROR;
968 OC_LOG(INFO, TAG, PCF("Device initialized successfully."));