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(CATransportAdapter_t connType, uint16_t *port)
72 CAEndpoint_t* info = NULL;
74 OCStackResult ret = OC_STACK_ERROR;
76 CAResult_t caResult = CAGetNetworkInformation(&info, &size);
77 if ((caResult == CA_STATUS_OK) && info && size)
81 if ((info[size].flags & CA_SECURE) && info[size].adapter == connType)
83 if (info[size].adapter == CA_ADAPTER_IP)
85 *port = info[size].port;
98 * Function will extract 0, 1 or 2 filters from query.
99 * More than 2 filters or unsupported filters will result in error.
100 * If both filters are of the same supported type, the 2nd one will be picked.
101 * Resource and device filters in the SAME query are NOT validated
102 * and resources will likely not clear filters.
104 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
109 char *restOfQuery = NULL;
110 int numKeyValuePairsParsed = 0;
115 OC_LOG_V(INFO, TAG, "Extracting params from %s", query);
117 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
121 if (numKeyValuePairsParsed >= 2)
123 OC_LOG(ERROR, TAG, PCF("More than 2 queries params in URI."));
124 return OC_STACK_INVALID_QUERY;
127 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
131 return OC_STACK_INVALID_QUERY;
133 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
135 *filterOne = value; // if
137 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
139 *filterTwo = value; // rt
143 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
144 return OC_STACK_INVALID_QUERY;
146 ++numKeyValuePairsParsed;
148 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
151 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
155 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
157 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
159 return OC_WELL_KNOWN_URI;
161 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
163 return OC_DEVICE_URI;
165 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
167 return OC_PLATFORM_URI;
169 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
171 return OC_RESOURCE_TYPES_URI;
174 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
178 #endif //WITH_PRESENCE
179 return OC_UNKNOWN_URI;
182 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
183 char **filterOne, char **filterTwo)
185 if(!filterOne || !filterTwo)
187 return OC_STACK_INVALID_PARAM;
194 if (uri == OC_PRESENCE)
196 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
197 OC_LOG(INFO, TAG, PCF("OC_PRESENCE Request for virtual resource."));
202 OCStackResult result = OC_STACK_OK;
206 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
212 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
213 OCDiscoveryPayload* payload, CATransportAdapter_t adapter )
215 if (!resourcePtr || !payload)
217 return OC_STACK_INVALID_PARAM;
220 if (resourcePtr->resourceProperties & OC_SECURE)
222 if(GetSecurePortInfo (adapter, &port) != OC_STACK_OK)
228 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
233 uint8_t IsCollectionResource (OCResource *resource)
240 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
242 if (resource->rsrcResources[i])
250 OCResource *FindResourceByUri(const char* resourceUri)
257 OCResource * pointer = headResource;
260 if (strcmp(resourceUri, pointer->uri) == 0)
264 pointer = pointer->next;
266 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
271 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
272 ResourceHandling *handling,
273 OCResource **resource)
275 if(!request || !handling || !resource)
277 return OC_STACK_INVALID_PARAM;
280 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
282 const OCDevAddr *devAddr = &request->devAddr;
284 // Check if virtual resource
285 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
287 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
288 *handling = OC_RESOURCE_VIRTUAL;
289 *resource = headResource;
292 if (strlen((const char*)(request->resourceUrl)) == 0)
294 // Resource URL not specified
295 *handling = OC_RESOURCE_NOT_SPECIFIED;
296 return OC_STACK_NO_RESOURCE;
300 OCResource *resourcePtr = NULL;
301 resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
302 *resource = resourcePtr;
305 if(defaultDeviceHandler)
307 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
311 // Resource does not exist
312 // and default device handler does not exist
313 *handling = OC_RESOURCE_NOT_SPECIFIED;
314 return OC_STACK_NO_RESOURCE;
317 // secure resource will entertain only authorized requests
318 if ((resourcePtr->resourceProperties & OC_SECURE) && ((devAddr->flags & OC_FLAG_SECURE) == 0))
320 OC_LOG(ERROR, TAG, PCF("Un-authorized request. Ignoring"));
321 return OC_STACK_RESOURCE_ERROR;
324 if (IsCollectionResource (resourcePtr))
326 // Collection resource
327 if (resourcePtr->entityHandler != defaultResourceEHandler)
329 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
334 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
340 // Resource not a collection
341 if (resourcePtr->entityHandler != defaultResourceEHandler)
343 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
348 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
355 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
357 OCStackResult result;
362 result = OC_STACK_OK;
365 result = OC_STACK_SLOW_RESOURCE;
368 result = OC_STACK_ERROR;
370 case OC_EH_FORBIDDEN:
371 result = OC_STACK_RESOURCE_ERROR;
373 case OC_EH_RESOURCE_CREATED:
374 result = OC_STACK_RESOURCE_CREATED;
376 case OC_EH_RESOURCE_DELETED:
377 result = OC_STACK_RESOURCE_DELETED;
379 case OC_EH_RESOURCE_NOT_FOUND:
380 result = OC_STACK_NO_RESOURCE;
383 result = OC_STACK_ERROR;
389 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
396 // Null or empty is analogous to no filter.
397 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
402 OCResourceType *resourceTypePtr = resource->rsrcType;
404 while (resourceTypePtr)
406 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
410 resourceTypePtr = resourceTypePtr->next;
413 OC_LOG_V(INFO, TAG, PCF("%s does not contain rt=%s."), resource->uri, resourceTypeFilter);
417 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
424 // Null or empty is analogous to no filter.
425 if (interfaceFilter == NULL || *interfaceFilter == 0)
430 OCResourceInterface *interfacePtr = resource->rsrcInterface;
434 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
438 interfacePtr = interfacePtr->next;
441 OC_LOG_V(INFO, TAG, PCF("%s does not contain if=%s."), resource->uri, interfaceFilter);
446 * If the filters are null, they will be assumed to NOT be present
447 * and the resource will not be matched against them.
448 * Function will return true if all non null AND non empty filters passed in find a match.
450 static bool includeThisResourceInResponse(OCResource *resource,
451 char *interfaceFilter,
452 char *resourceTypeFilter)
456 OC_LOG(ERROR, TAG, PCF("Invalid resource"));
460 if ( !(resource->resourceProperties & OC_ACTIVE) ||
461 !(resource->resourceProperties & OC_DISCOVERABLE))
463 OC_LOG_V(INFO, TAG, PCF("%s not ACTIVE or DISCOVERABLE"), resource->uri);
467 return resourceMatchesIFFilter(resource, interfaceFilter) &&
468 resourceMatchesRTFilter(resource, resourceTypeFilter);
472 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
473 OCPayload *discoveryPayload)
475 OCEntityHandlerResponse response = {};
477 response.ehResult = OC_EH_OK;
478 response.payload = discoveryPayload;
479 response.persistentBufferFlag = 0;
480 response.requestHandle = (OCRequestHandle) request;
481 response.resourceHandle = (OCResourceHandle) resource;
483 return OCDoResponse(&response);
486 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
488 if (!request || !resource)
490 return OC_STACK_INVALID_PARAM;
493 OCStackResult discoveryResult = OC_STACK_ERROR;
494 OCPayload* payload = NULL;
495 char *filterOne = NULL;
496 char *filterTwo = NULL;
498 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
500 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
503 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
505 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
506 &filterOne, &filterTwo);
507 if (discoveryResult != OC_STACK_OK)
509 OC_LOG_V(ERROR, TAG, "Error (%d) validating query.\n", discoveryResult);
510 return discoveryResult;
512 payload = (OCPayload*)OCDiscoveryPayloadCreate();
516 return OC_STACK_NO_MEMORY;
520 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
522 if(includeThisResourceInResponse(resource, filterOne, filterTwo))
524 discoveryResult = BuildVirtualResourceResponse(resource,
525 (OCDiscoveryPayload*)payload,
526 (CATransportAdapter_t)request->devAddr.adapter);
530 else if (virtualUriInRequest == OC_DEVICE_URI)
532 payload = (OCPayload*)OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
533 OCGetServerInstanceID(), savedDeviceInfo.deviceName,
534 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
537 discoveryResult = OC_STACK_NO_MEMORY;
540 else if (virtualUriInRequest == OC_PLATFORM_URI)
542 OCPlatformPayload* payload = OCPlatformPayloadCreate(
543 OC_RSRVD_PLATFORM_URI,
547 discoveryResult = OC_STACK_NO_MEMORY;
554 if(resource->resourceProperties & OC_ACTIVE)
556 discoveryResult = SendPresenceNotification(resource->rsrcType,
557 OC_PRESENCE_TRIGGER_CHANGE);
562 // Presence uses observer notification api to respond via SendPresenceNotification.
563 if (virtualUriInRequest != OC_PRESENCE)
565 if(discoveryResult == OC_STACK_OK)
567 discoveryResult = SendNonPersistantDiscoveryResponse(request, resource,
569 OCPayloadDestroy(payload);
573 OC_LOG_V(ERROR, TAG, "Error (%d) building (%d) discovery response. "\
574 "Not responding to request.", discoveryResult, virtualUriInRequest);
578 return discoveryResult;
582 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
586 return OC_STACK_INVALID_PARAM;
589 OCStackResult result = OC_STACK_OK;
590 OCEntityHandlerResult ehResult = OC_EH_ERROR;
591 OCEntityHandlerRequest ehRequest = {};
593 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
594 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
595 request->method, (OCResourceHandle) NULL, request->query,
596 request->payload, request->payloadSize,
597 request->numRcvdVendorSpecificHeaderOptions,
598 request->rcvdVendorSpecificHeaderOptions,
599 (OCObserveAction)request->observationOption, (OCObservationId)0);
600 VERIFY_SUCCESS(result, OC_STACK_OK);
602 // At this point we know for sure that defaultDeviceHandler exists
603 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
604 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
605 if(ehResult == OC_EH_SLOW)
607 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
608 request->slowFlag = 1;
610 else if(ehResult == OC_EH_ERROR)
612 FindAndDeleteServerRequest(request);
614 result = EntityHandlerCodeToOCStackCode(ehResult);
620 HandleResourceWithEntityHandler (OCServerRequest *request,
621 OCResource *resource,
622 uint8_t collectionResource)
624 if(!request || ! resource)
626 return OC_STACK_INVALID_PARAM;
629 OCStackResult result = OC_STACK_ERROR;
630 OCEntityHandlerResult ehResult = OC_EH_ERROR;
631 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
632 ResourceObserver *resObs = NULL;
634 OCEntityHandlerRequest ehRequest = {};
636 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
638 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
639 request->method, (OCResourceHandle) resource, request->query,
640 request->payload, request->payloadSize, request->numRcvdVendorSpecificHeaderOptions,
641 request->rcvdVendorSpecificHeaderOptions,
642 (OCObserveAction)request->observationOption, 0);
643 VERIFY_SUCCESS(result, OC_STACK_OK);
645 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
647 OC_LOG(INFO, TAG, PCF("No observation requested"));
648 ehFlag = OC_REQUEST_FLAG;
650 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
652 OC_LOG(INFO, TAG, PCF("Observation registration requested"));
654 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
655 VERIFY_SUCCESS(result, OC_STACK_OK);
657 result = AddObserver ((const char*)(request->resourceUrl),
658 (const char *)(request->query),
659 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
660 resource, request->qos,
663 if(result == OC_STACK_OK)
665 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
666 request->observeResult = OC_STACK_OK;
667 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
671 result = OC_STACK_OK;
673 // The error in observeResult for the request will be used when responding to this
674 // request by omitting the observation option/sequence number.
675 request->observeResult = OC_STACK_ERROR;
676 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
677 ehFlag = OC_REQUEST_FLAG;
681 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
684 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
686 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
690 // Stack does not contain this observation request
691 // Either token is incorrect or observation list is corrupted
692 result = OC_STACK_ERROR;
695 ehRequest.obsInfo.obsId = resObs->observeId;
696 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
698 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
700 if(result == OC_STACK_OK)
702 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
703 request->observeResult = OC_STACK_OK;
707 result = OC_STACK_OK;
708 request->observeResult = OC_STACK_ERROR;
709 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
714 result = OC_STACK_ERROR;
718 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
719 if(ehResult == OC_EH_SLOW)
721 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
722 request->slowFlag = 1;
724 else if(ehResult == OC_EH_ERROR)
726 FindAndDeleteServerRequest(request);
728 result = EntityHandlerCodeToOCStackCode(ehResult);
734 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
735 OCResource *resource)
737 if(!request || !resource)
739 return OC_STACK_INVALID_PARAM;
742 OCStackResult result = OC_STACK_ERROR;
743 OCEntityHandlerRequest ehRequest = {};
745 result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
746 request->method, (OCResourceHandle) resource, request->query,
747 request->payload, request->payloadSize, request->numRcvdVendorSpecificHeaderOptions,
748 request->rcvdVendorSpecificHeaderOptions,
749 (OCObserveAction)request->observationOption, (OCObservationId) 0);
750 if(result != OC_STACK_OK)
755 return (DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest));
759 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
761 OCStackResult ret = OC_STACK_OK;
765 case OC_RESOURCE_VIRTUAL:
767 ret = HandleVirtualResource (request, resource);
770 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
772 ret = HandleDefaultDeviceEntityHandler(request);
775 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
777 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
778 return OC_STACK_ERROR;
780 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
782 ret = HandleResourceWithEntityHandler (request, resource, 0);
785 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
787 ret = HandleResourceWithEntityHandler (request, resource, 1);
790 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
792 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
795 case OC_RESOURCE_NOT_SPECIFIED:
797 ret = OC_STACK_NO_RESOURCE;
802 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
803 return OC_STACK_ERROR;
809 void DeletePlatformInfo()
811 OC_LOG(INFO, TAG, PCF("Deleting platform info."));
813 OICFree(savedPlatformInfo.platformID);
814 savedPlatformInfo.platformID = NULL;
816 OICFree(savedPlatformInfo.manufacturerName);
817 savedPlatformInfo.manufacturerName = NULL;
819 OICFree(savedPlatformInfo.manufacturerUrl);
820 savedPlatformInfo.manufacturerUrl = NULL;
822 OICFree(savedPlatformInfo.modelNumber);
823 savedPlatformInfo.modelNumber = NULL;
825 OICFree(savedPlatformInfo.dateOfManufacture);
826 savedPlatformInfo.dateOfManufacture = NULL;
828 OICFree(savedPlatformInfo.platformVersion);
829 savedPlatformInfo.platformVersion = NULL;
831 OICFree(savedPlatformInfo.operatingSystemVersion);
832 savedPlatformInfo.operatingSystemVersion = NULL;
834 OICFree(savedPlatformInfo.hardwareVersion);
835 savedPlatformInfo.hardwareVersion = NULL;
837 OICFree(savedPlatformInfo.firmwareVersion);
838 savedPlatformInfo.firmwareVersion = NULL;
840 OICFree(savedPlatformInfo.supportUrl);
841 savedPlatformInfo.supportUrl = NULL;
843 OICFree(savedPlatformInfo.systemTime);
844 savedPlatformInfo.systemTime = NULL;
847 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
849 savedPlatformInfo.platformID = OICStrdup(info.platformID);
850 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
851 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
852 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
853 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
854 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
855 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
856 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
857 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
858 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
859 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
861 if ((!savedPlatformInfo.platformID && info.platformID)||
862 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
863 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
864 (!savedPlatformInfo.modelNumber && info.modelNumber)||
865 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
866 (!savedPlatformInfo.platformVersion && info.platformVersion)||
867 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
868 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
869 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
870 (!savedPlatformInfo.supportUrl && info.supportUrl)||
871 (!savedPlatformInfo.systemTime && info.systemTime))
873 DeletePlatformInfo();
874 return OC_STACK_INVALID_PARAM;
881 OCStackResult SavePlatformInfo(OCPlatformInfo info)
883 DeletePlatformInfo();
885 OCStackResult res = DeepCopyPlatFormInfo(info);
887 if (res != OC_STACK_OK)
889 OC_LOG_V(ERROR, TAG, PCF("Failed to save platform info. errno(%d)"), res);
893 OC_LOG(ERROR, TAG, PCF("Platform info saved."));
899 void DeleteDeviceInfo()
901 OC_LOG(INFO, TAG, PCF("Deleting device info."));
903 OICFree(savedDeviceInfo.deviceName);
904 savedDeviceInfo.deviceName = NULL;
907 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
909 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
911 if(!savedDeviceInfo.deviceName && info.deviceName)
914 return OC_STACK_NO_MEMORY;
920 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
922 OCStackResult res = OC_STACK_OK;
926 res = DeepCopyDeviceInfo(info);
928 VERIFY_SUCCESS(res, OC_STACK_OK);
930 if(OCGetServerInstanceID() == NULL)
932 OC_LOG(INFO, TAG, PCF("Device ID generation failed"));
933 res = OC_STACK_ERROR;
937 OC_LOG(INFO, TAG, PCF("Device initialized successfully."));