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"
41 #ifdef ROUTING_GATEWAY
42 #include "routingmanager.h"
46 #define TAG "ocresource"
47 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
48 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
50 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
51 TAG, #arg " is NULL"); return (retVal); } }
53 extern OCResource *headResource;
54 static OCPlatformInfo savedPlatformInfo = {0};
55 static OCDeviceInfo savedDeviceInfo = {0};
57 //-----------------------------------------------------------------------------
58 // Default resource entity handler function
59 //-----------------------------------------------------------------------------
60 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
61 OCEntityHandlerRequest * request, void* callbackParam)
63 //TODO ("Implement me!!!!");
64 // TODO: remove silence unused param warnings
68 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
71 /* This method will retrieve the port at which the secure resource is hosted */
72 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
76 if (endpoint->adapter == OC_ADAPTER_IP)
78 if (endpoint->flags & OC_IP_USE_V6)
80 p = caglobals.ip.u6s.port;
82 else if (endpoint->flags & OC_IP_USE_V4)
84 p = caglobals.ip.u4s.port;
93 * Function will extract 0, 1 or 2 filters from query.
94 * More than 2 filters or unsupported filters will result in error.
95 * If both filters are of the same supported type, the 2nd one will be picked.
96 * Resource and device filters in the SAME query are NOT validated
97 * and resources will likely not clear filters.
99 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
104 char *restOfQuery = NULL;
105 int numKeyValuePairsParsed = 0;
110 OC_LOG_V(INFO, TAG, "Extracting params from %s", query);
112 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
116 if (numKeyValuePairsParsed >= 2)
118 OC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
119 return OC_STACK_INVALID_QUERY;
122 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
126 return OC_STACK_INVALID_QUERY;
128 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
130 *filterOne = value; // if
132 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
134 *filterTwo = value; // rt
138 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
139 return OC_STACK_INVALID_QUERY;
141 ++numKeyValuePairsParsed;
143 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
146 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
150 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
152 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
154 return OC_WELL_KNOWN_URI;
156 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
158 return OC_DEVICE_URI;
160 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
162 return OC_PLATFORM_URI;
164 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
166 return OC_RESOURCE_TYPES_URI;
168 #ifdef ROUTING_GATEWAY
169 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
171 return OC_GATEWAY_URI;
175 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
179 #endif //WITH_PRESENCE
180 return OC_UNKNOWN_URI;
183 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
184 char **filterOne, char **filterTwo)
186 if(!filterOne || !filterTwo)
188 return OC_STACK_INVALID_PARAM;
195 if (uri == OC_PRESENCE)
197 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
198 OC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
203 OCStackResult result = OC_STACK_OK;
207 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
213 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
214 OCRepPayload** payload)
216 OCRepPayload *tempPayload = OCRepPayloadCreate();
220 OCRepPayloadDestroy(tempPayload);
221 return OC_STACK_INVALID_PARAM;
226 return OC_STACK_NO_MEMORY;
229 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
231 OCResourceType *resType = resourcePtr->rsrcType;
234 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
235 resType = resType->next;
238 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
241 OCRepPayloadAddInterface(tempPayload, resInterface->name);
242 resInterface = resInterface->next;
245 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
248 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
249 resAttrib->attrValue);
250 resAttrib = resAttrib->next;
255 *payload = tempPayload;
259 OCRepPayloadAppend(*payload, tempPayload);
265 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
266 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
268 if (!resourcePtr || !payload)
270 return OC_STACK_INVALID_PARAM;
273 if (resourcePtr->resourceProperties & OC_SECURE)
275 if (GetSecurePortInfo(devAddr, &port) != OC_STACK_OK)
281 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
286 uint8_t IsCollectionResource (OCResource *resource)
293 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
295 if (resource->rsrcResources[i])
303 OCResource *FindResourceByUri(const char* resourceUri)
310 OCResource * pointer = headResource;
313 if (strcmp(resourceUri, pointer->uri) == 0)
317 pointer = pointer->next;
319 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
324 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
325 ResourceHandling *handling,
326 OCResource **resource)
328 if(!request || !handling || !resource)
330 return OC_STACK_INVALID_PARAM;
333 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
335 // Check if virtual resource
336 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
338 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
339 *handling = OC_RESOURCE_VIRTUAL;
340 *resource = headResource;
343 if (strlen((const char*)(request->resourceUrl)) == 0)
345 // Resource URL not specified
346 *handling = OC_RESOURCE_NOT_SPECIFIED;
347 return OC_STACK_NO_RESOURCE;
351 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
352 *resource = resourcePtr;
355 if(defaultDeviceHandler)
357 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
361 // Resource does not exist
362 // and default device handler does not exist
363 *handling = OC_RESOURCE_NOT_SPECIFIED;
364 return OC_STACK_NO_RESOURCE;
367 if (IsCollectionResource (resourcePtr))
369 // Collection resource
370 if (resourcePtr->entityHandler != defaultResourceEHandler)
372 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
377 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
383 // Resource not a collection
384 if (resourcePtr->entityHandler != defaultResourceEHandler)
386 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
391 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
398 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
400 OCStackResult result;
405 result = OC_STACK_OK;
408 result = OC_STACK_SLOW_RESOURCE;
411 result = OC_STACK_ERROR;
413 case OC_EH_FORBIDDEN:
414 result = OC_STACK_RESOURCE_ERROR;
416 case OC_EH_RESOURCE_CREATED:
417 result = OC_STACK_RESOURCE_CREATED;
419 case OC_EH_RESOURCE_DELETED:
420 result = OC_STACK_RESOURCE_DELETED;
422 case OC_EH_RESOURCE_NOT_FOUND:
423 result = OC_STACK_NO_RESOURCE;
426 result = OC_STACK_ERROR;
432 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
439 // Null or empty is analogous to no filter.
440 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
445 OCResourceType *resourceTypePtr = resource->rsrcType;
447 while (resourceTypePtr)
449 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
453 resourceTypePtr = resourceTypePtr->next;
456 OC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
460 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
467 // Null or empty is analogous to no filter.
468 if (interfaceFilter == NULL || *interfaceFilter == 0)
473 OCResourceInterface *interfacePtr = resource->rsrcInterface;
477 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
481 interfacePtr = interfacePtr->next;
484 OC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
489 * If the filters are null, they will be assumed to NOT be present
490 * and the resource will not be matched against them.
491 * Function will return true if all non null AND non empty filters passed in find a match.
493 static bool includeThisResourceInResponse(OCResource *resource,
494 char *interfaceFilter,
495 char *resourceTypeFilter)
499 OC_LOG(ERROR, TAG, "Invalid resource");
503 if ( resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
506 * At least one valid filter should be available to
507 * include the resource in discovery response
509 if (!((interfaceFilter && *interfaceFilter ) ||
510 (resourceTypeFilter && *resourceTypeFilter)))
512 OC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
513 resource", resource->uri);
517 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
518 !(resource->resourceProperties & OC_DISCOVERABLE))
520 OC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
524 return resourceMatchesIFFilter(resource, interfaceFilter) &&
525 resourceMatchesRTFilter(resource, resourceTypeFilter);
529 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
530 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
532 OCEntityHandlerResponse response = {0};
534 response.ehResult = ehResult;
535 response.payload = discoveryPayload;
536 response.persistentBufferFlag = 0;
537 response.requestHandle = (OCRequestHandle) request;
538 response.resourceHandle = (OCResourceHandle) resource;
540 return OCDoResponse(&response);
543 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
545 if (!request || !resource)
547 return OC_STACK_INVALID_PARAM;
550 OCStackResult discoveryResult = OC_STACK_ERROR;
552 bool bMulticast = false; // Was the discovery request a multicast request?
553 OCPayload* payload = NULL;
555 OC_LOG(INFO, TAG, "Entering HandleVirtualResource");
557 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
559 // Step 1: Generate the response to discovery request
560 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
562 char *filterOne = NULL;
563 char *filterTwo = NULL;
565 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
566 &filterOne, &filterTwo);
568 if (discoveryResult == OC_STACK_OK)
570 payload = (OCPayload*)OCDiscoveryPayloadCreate();
574 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
576 if(includeThisResourceInResponse(resource, filterOne, filterTwo))
578 discoveryResult = BuildVirtualResourceResponse(resource,
579 (OCDiscoveryPayload*)payload,
583 // Set discoveryResult appropriately if no 'valid' resources are available.
584 if (((OCDiscoveryPayload*)payload)->resources == NULL)
586 discoveryResult = OC_STACK_NO_RESOURCE;
591 discoveryResult = OC_STACK_NO_MEMORY;
596 OC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
599 else if (virtualUriInRequest == OC_DEVICE_URI)
601 const OicUuid_t* deviceId = OCGetServerInstanceID();
604 discoveryResult = OC_STACK_ERROR;
608 payload = (OCPayload*) OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
609 (const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
610 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
613 discoveryResult = OC_STACK_NO_MEMORY;
617 discoveryResult = OC_STACK_OK;
621 else if (virtualUriInRequest == OC_PLATFORM_URI)
623 payload = (OCPayload*)OCPlatformPayloadCreate(
624 OC_RSRVD_PLATFORM_URI,
628 discoveryResult = OC_STACK_NO_MEMORY;
632 discoveryResult = OC_STACK_OK;
635 #ifdef ROUTING_GATEWAY
636 else if (OC_GATEWAY_URI == virtualUriInRequest)
638 // Received request for a gateway
639 OC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
640 discoveryResult = RMHandleGatewayRequest(request, resource);
646 * Step 2: Send the discovery response
648 * Iotivity should respond to discovery requests in below manner:
649 * 1)If query filter matching fails and discovery request is multicast,
650 * it should NOT send any response.
651 * 2)If query filter matching fails and discovery request is unicast,
652 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
653 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
654 * request is multicast, it should NOT send any response.
655 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
656 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
660 if ((virtualUriInRequest == OC_PRESENCE) &&
661 (resource->resourceProperties & OC_ACTIVE))
663 // Presence uses observer notification api to respond via SendPresenceNotification.
664 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
668 #ifdef ROUTING_GATEWAY
669 // Gateway uses the RMHandleGatewayRequest to respond to the request.
670 if (OC_GATEWAY != virtualUriInRequest)
673 if(discoveryResult == OC_STACK_OK)
675 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
677 else if(bMulticast == false)
679 OC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
680 discovery request", discoveryResult, virtualUriInRequest);
681 SendNonPersistantDiscoveryResponse(request, resource, NULL,
682 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
686 // Ignoring the discovery request as per RFC 7252, Section #8.2
687 OC_LOG(INFO, TAG, "Silently ignoring the request since device does not have \
688 any useful data to send");
692 OCPayloadDestroy(payload);
698 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
702 return OC_STACK_INVALID_PARAM;
705 OCStackResult result = OC_STACK_OK;
706 OCEntityHandlerResult ehResult = OC_EH_ERROR;
707 OCEntityHandlerRequest ehRequest = {0};
709 OC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
710 result = FormOCEntityHandlerRequest(&ehRequest,
711 (OCRequestHandle) request,
714 (OCResourceHandle) NULL, request->query,
715 PAYLOAD_TYPE_REPRESENTATION,
717 request->payloadSize,
718 request->numRcvdVendorSpecificHeaderOptions,
719 request->rcvdVendorSpecificHeaderOptions,
720 (OCObserveAction)request->observationOption,
722 VERIFY_SUCCESS(result, OC_STACK_OK);
724 // At this point we know for sure that defaultDeviceHandler exists
725 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
726 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
727 if(ehResult == OC_EH_SLOW)
729 OC_LOG(INFO, TAG, "This is a slow resource");
730 request->slowFlag = 1;
732 else if(ehResult == OC_EH_ERROR)
734 FindAndDeleteServerRequest(request);
736 result = EntityHandlerCodeToOCStackCode(ehResult);
738 OCPayloadDestroy(ehRequest.payload);
743 HandleResourceWithEntityHandler (OCServerRequest *request,
744 OCResource *resource,
745 uint8_t collectionResource)
747 if(!request || ! resource)
749 return OC_STACK_INVALID_PARAM;
752 OCStackResult result = OC_STACK_ERROR;
753 OCEntityHandlerResult ehResult = OC_EH_ERROR;
754 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
755 ResourceObserver *resObs = NULL;
757 OCEntityHandlerRequest ehRequest = {0};
759 OC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
760 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
761 // check the security resource
762 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
764 type = PAYLOAD_TYPE_SECURITY;
767 result = FormOCEntityHandlerRequest(&ehRequest,
768 (OCRequestHandle)request,
771 (OCResourceHandle)resource,
775 request->payloadSize,
776 request->numRcvdVendorSpecificHeaderOptions,
777 request->rcvdVendorSpecificHeaderOptions,
778 (OCObserveAction)request->observationOption,
780 VERIFY_SUCCESS(result, OC_STACK_OK);
782 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
784 OC_LOG(INFO, TAG, "No observation requested");
785 ehFlag = OC_REQUEST_FLAG;
787 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
789 OC_LOG(INFO, TAG, "Observation registration requested");
791 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
792 VERIFY_SUCCESS(result, OC_STACK_OK);
794 result = AddObserver ((const char*)(request->resourceUrl),
795 (const char *)(request->query),
796 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
797 resource, request->qos, request->acceptFormat,
800 if(result == OC_STACK_OK)
802 OC_LOG(INFO, TAG, "Added observer successfully");
803 request->observeResult = OC_STACK_OK;
804 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
808 result = OC_STACK_OK;
810 // The error in observeResult for the request will be used when responding to this
811 // request by omitting the observation option/sequence number.
812 request->observeResult = OC_STACK_ERROR;
813 OC_LOG(ERROR, TAG, "Observer Addition failed");
814 ehFlag = OC_REQUEST_FLAG;
818 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
821 OC_LOG(INFO, TAG, "Deregistering observation requested");
823 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
827 // Stack does not contain this observation request
828 // Either token is incorrect or observation list is corrupted
829 result = OC_STACK_ERROR;
832 ehRequest.obsInfo.obsId = resObs->observeId;
833 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
835 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
837 if(result == OC_STACK_OK)
839 OC_LOG(INFO, TAG, "Removed observer successfully");
840 request->observeResult = OC_STACK_OK;
844 result = OC_STACK_OK;
845 request->observeResult = OC_STACK_ERROR;
846 OC_LOG(ERROR, TAG, "Observer Removal failed");
851 result = OC_STACK_ERROR;
855 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
856 if(ehResult == OC_EH_SLOW)
858 OC_LOG(INFO, TAG, "This is a slow resource");
859 request->slowFlag = 1;
861 else if(ehResult == OC_EH_ERROR)
863 FindAndDeleteServerRequest(request);
865 result = EntityHandlerCodeToOCStackCode(ehResult);
867 OCPayloadDestroy(ehRequest.payload);
872 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
873 OCResource *resource)
875 if(!request || !resource)
877 return OC_STACK_INVALID_PARAM;
880 OCStackResult result = OC_STACK_ERROR;
881 OCEntityHandlerRequest ehRequest = {0};
883 result = FormOCEntityHandlerRequest(&ehRequest,
884 (OCRequestHandle)request,
887 (OCResourceHandle)resource,
889 PAYLOAD_TYPE_REPRESENTATION,
891 request->payloadSize,
892 request->numRcvdVendorSpecificHeaderOptions,
893 request->rcvdVendorSpecificHeaderOptions,
894 (OCObserveAction)request->observationOption,
896 if(result == OC_STACK_OK)
898 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
901 OCPayloadDestroy(ehRequest.payload);
906 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
908 OCStackResult ret = OC_STACK_OK;
912 case OC_RESOURCE_VIRTUAL:
914 ret = HandleVirtualResource (request, resource);
917 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
919 ret = HandleDefaultDeviceEntityHandler(request);
922 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
924 OC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
925 return OC_STACK_ERROR;
927 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
929 ret = HandleResourceWithEntityHandler (request, resource, 0);
932 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
934 ret = HandleResourceWithEntityHandler (request, resource, 1);
937 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
939 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
942 case OC_RESOURCE_NOT_SPECIFIED:
944 ret = OC_STACK_NO_RESOURCE;
949 OC_LOG(INFO, TAG, "Invalid Resource Determination");
950 return OC_STACK_ERROR;
956 void DeletePlatformInfo()
958 OC_LOG(INFO, TAG, "Deleting platform info.");
960 OICFree(savedPlatformInfo.platformID);
961 savedPlatformInfo.platformID = NULL;
963 OICFree(savedPlatformInfo.manufacturerName);
964 savedPlatformInfo.manufacturerName = NULL;
966 OICFree(savedPlatformInfo.manufacturerUrl);
967 savedPlatformInfo.manufacturerUrl = NULL;
969 OICFree(savedPlatformInfo.modelNumber);
970 savedPlatformInfo.modelNumber = NULL;
972 OICFree(savedPlatformInfo.dateOfManufacture);
973 savedPlatformInfo.dateOfManufacture = NULL;
975 OICFree(savedPlatformInfo.platformVersion);
976 savedPlatformInfo.platformVersion = NULL;
978 OICFree(savedPlatformInfo.operatingSystemVersion);
979 savedPlatformInfo.operatingSystemVersion = NULL;
981 OICFree(savedPlatformInfo.hardwareVersion);
982 savedPlatformInfo.hardwareVersion = NULL;
984 OICFree(savedPlatformInfo.firmwareVersion);
985 savedPlatformInfo.firmwareVersion = NULL;
987 OICFree(savedPlatformInfo.supportUrl);
988 savedPlatformInfo.supportUrl = NULL;
990 OICFree(savedPlatformInfo.systemTime);
991 savedPlatformInfo.systemTime = NULL;
994 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
996 savedPlatformInfo.platformID = OICStrdup(info.platformID);
997 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
998 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
999 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
1000 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
1001 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
1002 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
1003 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
1004 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
1005 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
1006 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
1008 if ((!savedPlatformInfo.platformID && info.platformID)||
1009 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
1010 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
1011 (!savedPlatformInfo.modelNumber && info.modelNumber)||
1012 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
1013 (!savedPlatformInfo.platformVersion && info.platformVersion)||
1014 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
1015 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
1016 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
1017 (!savedPlatformInfo.supportUrl && info.supportUrl)||
1018 (!savedPlatformInfo.systemTime && info.systemTime))
1020 DeletePlatformInfo();
1021 return OC_STACK_INVALID_PARAM;
1028 OCStackResult SavePlatformInfo(OCPlatformInfo info)
1030 DeletePlatformInfo();
1032 OCStackResult res = DeepCopyPlatFormInfo(info);
1034 if (res != OC_STACK_OK)
1036 OC_LOG_V(ERROR, TAG, "Failed to save platform info. errno(%d)", res);
1040 OC_LOG(INFO, TAG, "Platform info saved.");
1046 void DeleteDeviceInfo()
1048 OC_LOG(INFO, TAG, "Deleting device info.");
1050 OICFree(savedDeviceInfo.deviceName);
1051 savedDeviceInfo.deviceName = NULL;
1054 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1056 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1058 if(!savedDeviceInfo.deviceName && info.deviceName)
1061 return OC_STACK_NO_MEMORY;
1067 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1069 OCStackResult res = OC_STACK_OK;
1073 res = DeepCopyDeviceInfo(info);
1075 VERIFY_SUCCESS(res, OC_STACK_OK);
1077 if(OCGetServerInstanceID() == NULL)
1079 OC_LOG(INFO, TAG, "Device ID generation failed");
1080 res = OC_STACK_ERROR;
1084 OC_LOG(INFO, TAG, "Device initialized successfully.");