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 // Check if virtual resource
328 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
330 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
331 *handling = OC_RESOURCE_VIRTUAL;
332 *resource = headResource;
335 if (strlen((const char*)(request->resourceUrl)) == 0)
337 // Resource URL not specified
338 *handling = OC_RESOURCE_NOT_SPECIFIED;
339 return OC_STACK_NO_RESOURCE;
343 OCResource *resourcePtr = NULL;
344 resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
345 *resource = resourcePtr;
348 if(defaultDeviceHandler)
350 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
354 // Resource does not exist
355 // and default device handler does not exist
356 *handling = OC_RESOURCE_NOT_SPECIFIED;
357 return OC_STACK_NO_RESOURCE;
360 if (IsCollectionResource (resourcePtr))
362 // Collection resource
363 if (resourcePtr->entityHandler != defaultResourceEHandler)
365 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
370 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
376 // Resource not a collection
377 if (resourcePtr->entityHandler != defaultResourceEHandler)
379 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
384 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
391 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
393 OCStackResult result;
398 result = OC_STACK_OK;
401 result = OC_STACK_SLOW_RESOURCE;
404 result = OC_STACK_ERROR;
406 case OC_EH_FORBIDDEN:
407 result = OC_STACK_RESOURCE_ERROR;
409 case OC_EH_RESOURCE_CREATED:
410 result = OC_STACK_RESOURCE_CREATED;
412 case OC_EH_RESOURCE_DELETED:
413 result = OC_STACK_RESOURCE_DELETED;
415 case OC_EH_RESOURCE_NOT_FOUND:
416 result = OC_STACK_NO_RESOURCE;
419 result = OC_STACK_ERROR;
425 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
432 // Null or empty is analogous to no filter.
433 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
438 OCResourceType *resourceTypePtr = resource->rsrcType;
440 while (resourceTypePtr)
442 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
446 resourceTypePtr = resourceTypePtr->next;
449 OC_LOG_V(INFO, TAG, PCF("%s does not contain rt=%s."), resource->uri, resourceTypeFilter);
453 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
460 // Null or empty is analogous to no filter.
461 if (interfaceFilter == NULL || *interfaceFilter == 0)
466 OCResourceInterface *interfacePtr = resource->rsrcInterface;
470 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
474 interfacePtr = interfacePtr->next;
477 OC_LOG_V(INFO, TAG, PCF("%s does not contain if=%s."), resource->uri, interfaceFilter);
482 * If the filters are null, they will be assumed to NOT be present
483 * and the resource will not be matched against them.
484 * Function will return true if all non null AND non empty filters passed in find a match.
486 static bool includeThisResourceInResponse(OCResource *resource,
487 char *interfaceFilter,
488 char *resourceTypeFilter)
492 OC_LOG(ERROR, TAG, PCF("Invalid resource"));
496 if ( resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
499 * At least one valid filter should be available to
500 * include the resource in discovery response
502 if (!((interfaceFilter && *interfaceFilter ) ||
503 (resourceTypeFilter && *resourceTypeFilter)))
505 OC_LOG_V(INFO, TAG, PCF("%s no query string for EXPLICIT_DISCOVERABLE \
506 resource"), resource->uri);
510 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
511 !(resource->resourceProperties & OC_DISCOVERABLE))
513 OC_LOG_V(INFO, TAG, PCF("%s not ACTIVE or DISCOVERABLE"), resource->uri);
517 return resourceMatchesIFFilter(resource, interfaceFilter) &&
518 resourceMatchesRTFilter(resource, resourceTypeFilter);
522 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
523 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
525 OCEntityHandlerResponse response = {};
527 response.ehResult = ehResult;
528 response.payload = discoveryPayload;
529 response.persistentBufferFlag = 0;
530 response.requestHandle = (OCRequestHandle) request;
531 response.resourceHandle = (OCResourceHandle) resource;
533 return OCDoResponse(&response);
536 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
538 if (!request || !resource)
540 return OC_STACK_INVALID_PARAM;
543 OCStackResult discoveryResult = OC_STACK_ERROR;
545 bool bMulticast = false; // Was the discovery request a multicast request?
546 OCPayload* payload = NULL;
548 OC_LOG(INFO, TAG, PCF("Entering HandleVirtualResource"));
550 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
552 // Step 1: Generate the response to discovery request
553 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
555 char *filterOne = NULL;
556 char *filterTwo = NULL;
558 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
559 &filterOne, &filterTwo);
561 if (discoveryResult == OC_STACK_OK)
563 payload = (OCPayload*)OCDiscoveryPayloadCreate();
567 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
569 if(includeThisResourceInResponse(resource, filterOne, filterTwo))
571 discoveryResult = BuildVirtualResourceResponse(resource,
572 (OCDiscoveryPayload*)payload,
576 // Set discoveryResult appropriately if no 'valid' resources are available.
577 if (((OCDiscoveryPayload*)payload)->resources == NULL)
579 discoveryResult = OC_STACK_NO_RESOURCE;
584 discoveryResult = OC_STACK_NO_MEMORY;
589 OC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
592 else if (virtualUriInRequest == OC_DEVICE_URI)
594 payload = (OCPayload*)OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
595 OCGetServerInstanceID(), savedDeviceInfo.deviceName,
596 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
599 discoveryResult = OC_STACK_NO_MEMORY;
603 discoveryResult = OC_STACK_OK;
606 else if (virtualUriInRequest == OC_PLATFORM_URI)
608 payload = (OCPayload*)OCPlatformPayloadCreate(
609 OC_RSRVD_PLATFORM_URI,
613 discoveryResult = OC_STACK_NO_MEMORY;
617 discoveryResult = OC_STACK_OK;
622 * Step 2: Send the discovery response
624 * Iotivity should respond to discovery requests in below manner:
625 * 1)If query filter matching fails and discovery request is multicast,
626 * it should NOT send any response.
627 * 2)If query filter matching fails and discovery request is unicast,
628 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
629 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
630 * request is multicast, it should NOT send any response.
631 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
632 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
636 if ((virtualUriInRequest == OC_PRESENCE) &&
637 (resource->resourceProperties & OC_ACTIVE))
639 // Presence uses observer notification api to respond via SendPresenceNotification.
640 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
645 if(discoveryResult == OC_STACK_OK)
647 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
649 else if(bMulticast == false)
651 OC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
652 discovery request", discoveryResult, virtualUriInRequest);
653 SendNonPersistantDiscoveryResponse(request, resource, NULL,
654 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
658 // Ignoring the discovery request as per RFC 7252, Section #8.2
659 OC_LOG_V(INFO, TAG, "Silently ignoring the request since device does not have \
660 any useful data to send");
664 OCPayloadDestroy(payload);
670 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
674 return OC_STACK_INVALID_PARAM;
677 OCStackResult result = OC_STACK_OK;
678 OCEntityHandlerResult ehResult = OC_EH_ERROR;
679 OCEntityHandlerRequest ehRequest = {};
681 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithDefaultDeviceEntityHandler"));
682 result = FormOCEntityHandlerRequest(&ehRequest,
683 (OCRequestHandle) request,
686 (OCResourceHandle) NULL, request->query,
688 request->payloadSize,
689 request->numRcvdVendorSpecificHeaderOptions,
690 request->rcvdVendorSpecificHeaderOptions,
691 (OCObserveAction)request->observationOption,
693 VERIFY_SUCCESS(result, OC_STACK_OK);
695 // At this point we know for sure that defaultDeviceHandler exists
696 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
697 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
698 if(ehResult == OC_EH_SLOW)
700 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
701 request->slowFlag = 1;
703 else if(ehResult == OC_EH_ERROR)
705 FindAndDeleteServerRequest(request);
707 result = EntityHandlerCodeToOCStackCode(ehResult);
709 OCPayloadDestroy(ehRequest.payload);
714 HandleResourceWithEntityHandler (OCServerRequest *request,
715 OCResource *resource,
716 uint8_t collectionResource)
718 if(!request || ! resource)
720 return OC_STACK_INVALID_PARAM;
723 OCStackResult result = OC_STACK_ERROR;
724 OCEntityHandlerResult ehResult = OC_EH_ERROR;
725 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
726 ResourceObserver *resObs = NULL;
728 OCEntityHandlerRequest ehRequest = {};
730 OC_LOG(INFO, TAG, PCF("Entering HandleResourceWithEntityHandler"));
732 result = FormOCEntityHandlerRequest(&ehRequest,
733 (OCRequestHandle)request,
736 (OCResourceHandle)resource,
739 request->payloadSize,
740 request->numRcvdVendorSpecificHeaderOptions,
741 request->rcvdVendorSpecificHeaderOptions,
742 (OCObserveAction)request->observationOption,
744 VERIFY_SUCCESS(result, OC_STACK_OK);
746 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
748 OC_LOG(INFO, TAG, PCF("No observation requested"));
749 ehFlag = OC_REQUEST_FLAG;
751 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
753 OC_LOG(INFO, TAG, PCF("Observation registration requested"));
755 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
756 VERIFY_SUCCESS(result, OC_STACK_OK);
758 result = AddObserver ((const char*)(request->resourceUrl),
759 (const char *)(request->query),
760 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
761 resource, request->qos,
764 if(result == OC_STACK_OK)
766 OC_LOG(INFO, TAG, PCF("Added observer successfully"));
767 request->observeResult = OC_STACK_OK;
768 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
772 result = OC_STACK_OK;
774 // The error in observeResult for the request will be used when responding to this
775 // request by omitting the observation option/sequence number.
776 request->observeResult = OC_STACK_ERROR;
777 OC_LOG(ERROR, TAG, PCF("Observer Addition failed"));
778 ehFlag = OC_REQUEST_FLAG;
782 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
785 OC_LOG(INFO, TAG, PCF("Deregistering observation requested"));
787 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
791 // Stack does not contain this observation request
792 // Either token is incorrect or observation list is corrupted
793 result = OC_STACK_ERROR;
796 ehRequest.obsInfo.obsId = resObs->observeId;
797 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
799 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
801 if(result == OC_STACK_OK)
803 OC_LOG(INFO, TAG, PCF("Removed observer successfully"));
804 request->observeResult = OC_STACK_OK;
808 result = OC_STACK_OK;
809 request->observeResult = OC_STACK_ERROR;
810 OC_LOG(ERROR, TAG, PCF("Observer Removal failed"));
815 result = OC_STACK_ERROR;
819 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
820 if(ehResult == OC_EH_SLOW)
822 OC_LOG(INFO, TAG, PCF("This is a slow resource"));
823 request->slowFlag = 1;
825 else if(ehResult == OC_EH_ERROR)
827 FindAndDeleteServerRequest(request);
829 result = EntityHandlerCodeToOCStackCode(ehResult);
831 OCPayloadDestroy(ehRequest.payload);
836 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
837 OCResource *resource)
839 if(!request || !resource)
841 return OC_STACK_INVALID_PARAM;
844 OCStackResult result = OC_STACK_ERROR;
845 OCEntityHandlerRequest ehRequest = {};
847 result = FormOCEntityHandlerRequest(&ehRequest,
848 (OCRequestHandle)request,
851 (OCResourceHandle)resource,
854 request->payloadSize,
855 request->numRcvdVendorSpecificHeaderOptions,
856 request->rcvdVendorSpecificHeaderOptions,
857 (OCObserveAction)request->observationOption,
859 if(result == OC_STACK_OK)
861 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
864 OCPayloadDestroy(ehRequest.payload);
869 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
871 OCStackResult ret = OC_STACK_OK;
875 case OC_RESOURCE_VIRTUAL:
877 ret = HandleVirtualResource (request, resource);
880 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
882 ret = HandleDefaultDeviceEntityHandler(request);
885 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
887 OC_LOG(INFO, TAG, PCF("OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER"));
888 return OC_STACK_ERROR;
890 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
892 ret = HandleResourceWithEntityHandler (request, resource, 0);
895 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
897 ret = HandleResourceWithEntityHandler (request, resource, 1);
900 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
902 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
905 case OC_RESOURCE_NOT_SPECIFIED:
907 ret = OC_STACK_NO_RESOURCE;
912 OC_LOG(INFO, TAG, PCF("Invalid Resource Determination"));
913 return OC_STACK_ERROR;
919 void DeletePlatformInfo()
921 OC_LOG(INFO, TAG, PCF("Deleting platform info."));
923 OICFree(savedPlatformInfo.platformID);
924 savedPlatformInfo.platformID = NULL;
926 OICFree(savedPlatformInfo.manufacturerName);
927 savedPlatformInfo.manufacturerName = NULL;
929 OICFree(savedPlatformInfo.manufacturerUrl);
930 savedPlatformInfo.manufacturerUrl = NULL;
932 OICFree(savedPlatformInfo.modelNumber);
933 savedPlatformInfo.modelNumber = NULL;
935 OICFree(savedPlatformInfo.dateOfManufacture);
936 savedPlatformInfo.dateOfManufacture = NULL;
938 OICFree(savedPlatformInfo.platformVersion);
939 savedPlatformInfo.platformVersion = NULL;
941 OICFree(savedPlatformInfo.operatingSystemVersion);
942 savedPlatformInfo.operatingSystemVersion = NULL;
944 OICFree(savedPlatformInfo.hardwareVersion);
945 savedPlatformInfo.hardwareVersion = NULL;
947 OICFree(savedPlatformInfo.firmwareVersion);
948 savedPlatformInfo.firmwareVersion = NULL;
950 OICFree(savedPlatformInfo.supportUrl);
951 savedPlatformInfo.supportUrl = NULL;
953 OICFree(savedPlatformInfo.systemTime);
954 savedPlatformInfo.systemTime = NULL;
957 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
959 savedPlatformInfo.platformID = OICStrdup(info.platformID);
960 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
961 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
962 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
963 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
964 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
965 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
966 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
967 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
968 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
969 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
971 if ((!savedPlatformInfo.platformID && info.platformID)||
972 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
973 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
974 (!savedPlatformInfo.modelNumber && info.modelNumber)||
975 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
976 (!savedPlatformInfo.platformVersion && info.platformVersion)||
977 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
978 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
979 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
980 (!savedPlatformInfo.supportUrl && info.supportUrl)||
981 (!savedPlatformInfo.systemTime && info.systemTime))
983 DeletePlatformInfo();
984 return OC_STACK_INVALID_PARAM;
991 OCStackResult SavePlatformInfo(OCPlatformInfo info)
993 DeletePlatformInfo();
995 OCStackResult res = DeepCopyPlatFormInfo(info);
997 if (res != OC_STACK_OK)
999 OC_LOG_V(ERROR, TAG, PCF("Failed to save platform info. errno(%d)"), res);
1003 OC_LOG(ERROR, TAG, PCF("Platform info saved."));
1009 void DeleteDeviceInfo()
1011 OC_LOG(INFO, TAG, PCF("Deleting device info."));
1013 OICFree(savedDeviceInfo.deviceName);
1014 savedDeviceInfo.deviceName = NULL;
1017 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1019 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1021 if(!savedDeviceInfo.deviceName && info.deviceName)
1024 return OC_STACK_NO_MEMORY;
1030 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1032 OCStackResult res = OC_STACK_OK;
1036 res = DeepCopyDeviceInfo(info);
1038 VERIFY_SUCCESS(res, OC_STACK_OK);
1040 if(OCGetServerInstanceID() == NULL)
1042 OC_LOG(INFO, TAG, PCF("Device ID generation failed"));
1043 res = OC_STACK_ERROR;
1047 OC_LOG(INFO, TAG, PCF("Device initialized successfully."));