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
29 #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"
38 #include "secureresourcemanager.h"
40 #include "cainterface.h"
43 #define TAG "ocresource"
44 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
45 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
47 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
48 TAG, #arg " is NULL"); return (retVal); } }
50 extern OCResource *headResource;
51 static OCPlatformInfo savedPlatformInfo = {0};
52 static OCDeviceInfo savedDeviceInfo = {0};
54 //-----------------------------------------------------------------------------
55 // Default resource entity handler function
56 //-----------------------------------------------------------------------------
57 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
58 OCEntityHandlerRequest * request, void* callbackParam)
60 //TODO ("Implement me!!!!");
61 // TODO: remove silence unused param warnings
65 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
68 /* This method will retrieve the port at which the secure resource is hosted */
69 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
73 if (endpoint->adapter == OC_ADAPTER_IP)
75 if (endpoint->flags & OC_IP_USE_V6)
77 p = caglobals.ip.u6s.port;
79 else if (endpoint->flags & OC_IP_USE_V4)
81 p = caglobals.ip.u4s.port;
90 * Function will extract 0, 1 or 2 filters from query.
91 * More than 2 filters or unsupported filters will result in error.
92 * If both filters are of the same supported type, the 2nd one will be picked.
93 * Resource and device filters in the SAME query are NOT validated
94 * and resources will likely not clear filters.
96 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
101 char *restOfQuery = NULL;
102 int numKeyValuePairsParsed = 0;
107 OC_LOG_V(INFO, TAG, "Extracting params from %s", query);
109 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
113 if (numKeyValuePairsParsed >= 2)
115 OC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
116 return OC_STACK_INVALID_QUERY;
119 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
123 return OC_STACK_INVALID_QUERY;
125 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
127 *filterOne = value; // if
129 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
131 *filterTwo = value; // rt
135 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
136 return OC_STACK_INVALID_QUERY;
138 ++numKeyValuePairsParsed;
140 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
143 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
147 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
149 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
151 return OC_WELL_KNOWN_URI;
153 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
155 return OC_DEVICE_URI;
157 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
159 return OC_PLATFORM_URI;
161 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
163 return OC_RESOURCE_TYPES_URI;
166 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
170 #endif //WITH_PRESENCE
171 return OC_UNKNOWN_URI;
174 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
175 char **filterOne, char **filterTwo)
177 if(!filterOne || !filterTwo)
179 return OC_STACK_INVALID_PARAM;
186 if (uri == OC_PRESENCE)
188 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
189 OC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
194 OCStackResult result = OC_STACK_OK;
198 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
204 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
205 OCRepPayload** payload)
207 OCRepPayload *tempPayload = OCRepPayloadCreate();
211 OCRepPayloadDestroy(tempPayload);
212 return OC_STACK_INVALID_PARAM;
217 return OC_STACK_NO_MEMORY;
220 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
222 OCResourceType *resType = resourcePtr->rsrcType;
225 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
226 resType = resType->next;
229 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
232 OCRepPayloadAddInterface(tempPayload, resInterface->name);
233 resInterface = resInterface->next;
236 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
239 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
240 resAttrib->attrValue);
241 resAttrib = resAttrib->next;
246 *payload = tempPayload;
250 OCRepPayloadAppend(*payload, tempPayload);
256 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
257 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
259 if (!resourcePtr || !payload)
261 return OC_STACK_INVALID_PARAM;
264 if (resourcePtr->resourceProperties & OC_SECURE)
266 if (GetSecurePortInfo(devAddr, &port) != OC_STACK_OK)
272 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
277 uint8_t IsCollectionResource (OCResource *resource)
284 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
286 if (resource->rsrcResources[i])
294 OCResource *FindResourceByUri(const char* resourceUri)
301 OCResource * pointer = headResource;
304 if (strcmp(resourceUri, pointer->uri) == 0)
308 pointer = pointer->next;
310 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
315 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
316 ResourceHandling *handling,
317 OCResource **resource)
319 if(!request || !handling || !resource)
321 return OC_STACK_INVALID_PARAM;
324 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
326 // Check if virtual resource
327 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
329 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
330 *handling = OC_RESOURCE_VIRTUAL;
331 *resource = headResource;
334 if (strlen((const char*)(request->resourceUrl)) == 0)
336 // Resource URL not specified
337 *handling = OC_RESOURCE_NOT_SPECIFIED;
338 return OC_STACK_NO_RESOURCE;
342 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
343 *resource = resourcePtr;
346 if(defaultDeviceHandler)
348 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
352 // Resource does not exist
353 // and default device handler does not exist
354 *handling = OC_RESOURCE_NOT_SPECIFIED;
355 return OC_STACK_NO_RESOURCE;
358 if (IsCollectionResource (resourcePtr))
360 // Collection resource
361 if (resourcePtr->entityHandler != defaultResourceEHandler)
363 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
368 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
374 // Resource not a collection
375 if (resourcePtr->entityHandler != defaultResourceEHandler)
377 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
382 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
389 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
391 OCStackResult result;
396 result = OC_STACK_OK;
399 result = OC_STACK_SLOW_RESOURCE;
402 result = OC_STACK_ERROR;
404 case OC_EH_FORBIDDEN:
405 result = OC_STACK_RESOURCE_ERROR;
407 case OC_EH_RESOURCE_CREATED:
408 result = OC_STACK_RESOURCE_CREATED;
410 case OC_EH_RESOURCE_DELETED:
411 result = OC_STACK_RESOURCE_DELETED;
413 case OC_EH_RESOURCE_NOT_FOUND:
414 result = OC_STACK_NO_RESOURCE;
417 result = OC_STACK_ERROR;
423 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
430 // Null or empty is analogous to no filter.
431 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
436 OCResourceType *resourceTypePtr = resource->rsrcType;
438 while (resourceTypePtr)
440 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
444 resourceTypePtr = resourceTypePtr->next;
447 OC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
451 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
458 // Null or empty is analogous to no filter.
459 if (interfaceFilter == NULL || *interfaceFilter == 0)
464 OCResourceInterface *interfacePtr = resource->rsrcInterface;
468 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
472 interfacePtr = interfacePtr->next;
475 OC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
480 * If the filters are null, they will be assumed to NOT be present
481 * and the resource will not be matched against them.
482 * Function will return true if all non null AND non empty filters passed in find a match.
484 static bool includeThisResourceInResponse(OCResource *resource,
485 char *interfaceFilter,
486 char *resourceTypeFilter)
490 OC_LOG(ERROR, TAG, "Invalid resource");
494 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
497 * At least one valid filter should be available to
498 * include the resource in discovery response
500 if (!((interfaceFilter && *interfaceFilter ) ||
501 (resourceTypeFilter && *resourceTypeFilter)))
503 OC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
504 resource", resource->uri);
508 else if (!(resource->resourceProperties & OC_ACTIVE) ||
509 !(resource->resourceProperties & OC_DISCOVERABLE))
511 OC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
515 return resourceMatchesIFFilter(resource, interfaceFilter) &&
516 resourceMatchesRTFilter(resource, resourceTypeFilter);
520 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
521 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
523 OCEntityHandlerResponse response = {0};
525 response.ehResult = ehResult;
526 response.payload = discoveryPayload;
527 response.persistentBufferFlag = 0;
528 response.requestHandle = (OCRequestHandle) request;
529 response.resourceHandle = (OCResourceHandle) resource;
531 return OCDoResponse(&response);
534 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
536 if (!request || !resource)
538 return OC_STACK_INVALID_PARAM;
541 OCStackResult discoveryResult = OC_STACK_ERROR;
543 bool bMulticast = false; // Was the discovery request a multicast request?
544 OCPayload* payload = NULL;
546 OC_LOG(INFO, TAG, "Entering HandleVirtualResource");
548 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
550 // Step 1: Generate the response to discovery request
551 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
553 char *filterOne = NULL;
554 char *filterTwo = NULL;
556 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
557 &filterOne, &filterTwo);
559 if (discoveryResult == OC_STACK_OK)
561 payload = (OCPayload*)OCDiscoveryPayloadCreate();
565 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
567 if(includeThisResourceInResponse(resource, filterOne, filterTwo))
569 discoveryResult = BuildVirtualResourceResponse(resource,
570 (OCDiscoveryPayload*)payload,
574 // Set discoveryResult appropriately if no 'valid' resources are available.
575 if (((OCDiscoveryPayload*)payload)->resources == NULL)
577 discoveryResult = OC_STACK_NO_RESOURCE;
582 discoveryResult = OC_STACK_NO_MEMORY;
587 OC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
590 else if (virtualUriInRequest == OC_DEVICE_URI)
592 const OicUuid_t* deviceId = OCGetServerInstanceID();
595 discoveryResult = OC_STACK_ERROR;
599 payload = (OCPayload*) OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
600 (const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
601 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
604 discoveryResult = OC_STACK_NO_MEMORY;
608 discoveryResult = OC_STACK_OK;
612 else if (virtualUriInRequest == OC_PLATFORM_URI)
614 payload = (OCPayload*)OCPlatformPayloadCreate(
615 OC_RSRVD_PLATFORM_URI,
619 discoveryResult = OC_STACK_NO_MEMORY;
623 discoveryResult = OC_STACK_OK;
628 * Step 2: Send the discovery response
630 * Iotivity should respond to discovery requests in below manner:
631 * 1)If query filter matching fails and discovery request is multicast,
632 * it should NOT send any response.
633 * 2)If query filter matching fails and discovery request is unicast,
634 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
635 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
636 * request is multicast, it should NOT send any response.
637 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
638 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
642 if ((virtualUriInRequest == OC_PRESENCE) &&
643 (resource->resourceProperties & OC_ACTIVE))
645 // Presence uses observer notification api to respond via SendPresenceNotification.
646 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
651 if(discoveryResult == OC_STACK_OK)
653 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
655 else if(bMulticast == false)
657 OC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
658 discovery request", discoveryResult, virtualUriInRequest);
659 SendNonPersistantDiscoveryResponse(request, resource, NULL,
660 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
664 // Ignoring the discovery request as per RFC 7252, Section #8.2
665 OC_LOG(INFO, TAG, "Silently ignoring the request since device does not have \
666 any useful data to send");
670 OCPayloadDestroy(payload);
676 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
680 return OC_STACK_INVALID_PARAM;
683 OCStackResult result = OC_STACK_OK;
684 OCEntityHandlerResult ehResult = OC_EH_ERROR;
685 OCEntityHandlerRequest ehRequest = {0};
687 OC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
688 result = FormOCEntityHandlerRequest(&ehRequest,
689 (OCRequestHandle) request,
692 (OCResourceHandle) NULL, request->query,
693 PAYLOAD_TYPE_REPRESENTATION,
695 request->payloadSize,
696 request->numRcvdVendorSpecificHeaderOptions,
697 request->rcvdVendorSpecificHeaderOptions,
698 (OCObserveAction)request->observationOption,
700 VERIFY_SUCCESS(result, OC_STACK_OK);
702 // At this point we know for sure that defaultDeviceHandler exists
703 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
704 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
705 if(ehResult == OC_EH_SLOW)
707 OC_LOG(INFO, TAG, "This is a slow resource");
708 request->slowFlag = 1;
710 else if(ehResult == OC_EH_ERROR)
712 FindAndDeleteServerRequest(request);
714 result = EntityHandlerCodeToOCStackCode(ehResult);
716 OCPayloadDestroy(ehRequest.payload);
721 HandleResourceWithEntityHandler (OCServerRequest *request,
722 OCResource *resource,
723 uint8_t collectionResource)
725 if(!request || ! resource)
727 return OC_STACK_INVALID_PARAM;
730 OCStackResult result = OC_STACK_ERROR;
731 OCEntityHandlerResult ehResult = OC_EH_ERROR;
732 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
733 ResourceObserver *resObs = NULL;
735 OCEntityHandlerRequest ehRequest = {0};
737 OC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
738 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
739 // check the security resource
740 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
742 type = PAYLOAD_TYPE_SECURITY;
745 result = FormOCEntityHandlerRequest(&ehRequest,
746 (OCRequestHandle)request,
749 (OCResourceHandle)resource,
753 request->payloadSize,
754 request->numRcvdVendorSpecificHeaderOptions,
755 request->rcvdVendorSpecificHeaderOptions,
756 (OCObserveAction)request->observationOption,
758 VERIFY_SUCCESS(result, OC_STACK_OK);
760 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
762 OC_LOG(INFO, TAG, "No observation requested");
763 ehFlag = OC_REQUEST_FLAG;
765 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
767 OC_LOG(INFO, TAG, "Observation registration requested");
769 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
770 VERIFY_SUCCESS(result, OC_STACK_OK);
772 result = AddObserver ((const char*)(request->resourceUrl),
773 (const char *)(request->query),
774 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
775 resource, request->qos, request->acceptFormat,
778 if(result == OC_STACK_OK)
780 OC_LOG(INFO, TAG, "Added observer successfully");
781 request->observeResult = OC_STACK_OK;
782 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
786 result = OC_STACK_OK;
788 // The error in observeResult for the request will be used when responding to this
789 // request by omitting the observation option/sequence number.
790 request->observeResult = OC_STACK_ERROR;
791 OC_LOG(ERROR, TAG, "Observer Addition failed");
792 ehFlag = OC_REQUEST_FLAG;
796 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
799 OC_LOG(INFO, TAG, "Deregistering observation requested");
801 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
805 // Stack does not contain this observation request
806 // Either token is incorrect or observation list is corrupted
807 result = OC_STACK_ERROR;
810 ehRequest.obsInfo.obsId = resObs->observeId;
811 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
813 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
815 if(result == OC_STACK_OK)
817 OC_LOG(INFO, TAG, "Removed observer successfully");
818 request->observeResult = OC_STACK_OK;
822 result = OC_STACK_OK;
823 request->observeResult = OC_STACK_ERROR;
824 OC_LOG(ERROR, TAG, "Observer Removal failed");
829 result = OC_STACK_ERROR;
833 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
834 if(ehResult == OC_EH_SLOW)
836 OC_LOG(INFO, TAG, "This is a slow resource");
837 request->slowFlag = 1;
839 else if(ehResult == OC_EH_ERROR)
841 FindAndDeleteServerRequest(request);
843 result = EntityHandlerCodeToOCStackCode(ehResult);
845 OCPayloadDestroy(ehRequest.payload);
850 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
851 OCResource *resource)
853 if(!request || !resource)
855 return OC_STACK_INVALID_PARAM;
858 OCStackResult result = OC_STACK_ERROR;
859 OCEntityHandlerRequest ehRequest = {0};
861 result = FormOCEntityHandlerRequest(&ehRequest,
862 (OCRequestHandle)request,
865 (OCResourceHandle)resource,
867 PAYLOAD_TYPE_REPRESENTATION,
869 request->payloadSize,
870 request->numRcvdVendorSpecificHeaderOptions,
871 request->rcvdVendorSpecificHeaderOptions,
872 (OCObserveAction)request->observationOption,
874 if(result == OC_STACK_OK)
876 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
879 OCPayloadDestroy(ehRequest.payload);
884 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
886 OCStackResult ret = OC_STACK_OK;
890 case OC_RESOURCE_VIRTUAL:
892 ret = HandleVirtualResource (request, resource);
895 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
897 ret = HandleDefaultDeviceEntityHandler(request);
900 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
902 OC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
903 return OC_STACK_ERROR;
905 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
907 ret = HandleResourceWithEntityHandler (request, resource, 0);
910 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
912 ret = HandleResourceWithEntityHandler (request, resource, 1);
915 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
917 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
920 case OC_RESOURCE_NOT_SPECIFIED:
922 ret = OC_STACK_NO_RESOURCE;
927 OC_LOG(INFO, TAG, "Invalid Resource Determination");
928 return OC_STACK_ERROR;
934 void DeletePlatformInfo()
936 OC_LOG(INFO, TAG, "Deleting platform info.");
938 OICFree(savedPlatformInfo.platformID);
939 savedPlatformInfo.platformID = NULL;
941 OICFree(savedPlatformInfo.manufacturerName);
942 savedPlatformInfo.manufacturerName = NULL;
944 OICFree(savedPlatformInfo.manufacturerUrl);
945 savedPlatformInfo.manufacturerUrl = NULL;
947 OICFree(savedPlatformInfo.modelNumber);
948 savedPlatformInfo.modelNumber = NULL;
950 OICFree(savedPlatformInfo.dateOfManufacture);
951 savedPlatformInfo.dateOfManufacture = NULL;
953 OICFree(savedPlatformInfo.platformVersion);
954 savedPlatformInfo.platformVersion = NULL;
956 OICFree(savedPlatformInfo.operatingSystemVersion);
957 savedPlatformInfo.operatingSystemVersion = NULL;
959 OICFree(savedPlatformInfo.hardwareVersion);
960 savedPlatformInfo.hardwareVersion = NULL;
962 OICFree(savedPlatformInfo.firmwareVersion);
963 savedPlatformInfo.firmwareVersion = NULL;
965 OICFree(savedPlatformInfo.supportUrl);
966 savedPlatformInfo.supportUrl = NULL;
968 OICFree(savedPlatformInfo.systemTime);
969 savedPlatformInfo.systemTime = NULL;
972 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
974 savedPlatformInfo.platformID = OICStrdup(info.platformID);
975 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
976 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
977 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
978 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
979 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
980 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
981 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
982 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
983 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
984 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
986 if ((!savedPlatformInfo.platformID && info.platformID)||
987 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
988 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
989 (!savedPlatformInfo.modelNumber && info.modelNumber)||
990 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
991 (!savedPlatformInfo.platformVersion && info.platformVersion)||
992 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
993 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
994 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
995 (!savedPlatformInfo.supportUrl && info.supportUrl)||
996 (!savedPlatformInfo.systemTime && info.systemTime))
998 DeletePlatformInfo();
999 return OC_STACK_INVALID_PARAM;
1006 OCStackResult SavePlatformInfo(OCPlatformInfo info)
1008 DeletePlatformInfo();
1010 OCStackResult res = DeepCopyPlatFormInfo(info);
1012 if (res != OC_STACK_OK)
1014 OC_LOG_V(ERROR, TAG, "Failed to save platform info. errno(%d)", res);
1018 OC_LOG(INFO, TAG, "Platform info saved.");
1024 void DeleteDeviceInfo()
1026 OC_LOG(INFO, TAG, "Deleting device info.");
1028 OICFree(savedDeviceInfo.deviceName);
1029 savedDeviceInfo.deviceName = NULL;
1032 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1034 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1036 if(!savedDeviceInfo.deviceName && info.deviceName)
1039 return OC_STACK_NO_MEMORY;
1045 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1047 OCStackResult res = OC_STACK_OK;
1051 res = DeepCopyDeviceInfo(info);
1053 VERIFY_SUCCESS(res, OC_STACK_OK);
1055 if(OCGetServerInstanceID() == NULL)
1057 OC_LOG(INFO, TAG, "Device ID generation failed");
1058 res = OC_STACK_ERROR;
1062 OC_LOG(INFO, TAG, "Device initialized successfully.");