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 #include "rdpayload.h"
44 #include "rd_server.h"
47 #ifdef ROUTING_GATEWAY
48 #include "routingmanager.h"
52 #define TAG "ocresource"
53 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
54 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
56 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
57 TAG, #arg " is NULL"); return (retVal); } }
59 extern OCResource *headResource;
60 static OCPlatformInfo savedPlatformInfo = {0};
61 static OCDeviceInfo savedDeviceInfo = {0};
63 //-----------------------------------------------------------------------------
64 // Default resource entity handler function
65 //-----------------------------------------------------------------------------
66 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
67 OCEntityHandlerRequest * request, void* callbackParam)
69 //TODO ("Implement me!!!!");
70 // TODO: remove silence unused param warnings
74 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
77 /* This method will retrieve the port at which the secure resource is hosted */
78 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
82 if (endpoint->adapter == OC_ADAPTER_IP)
84 if (endpoint->flags & OC_IP_USE_V6)
86 p = caglobals.ip.u6s.port;
88 else if (endpoint->flags & OC_IP_USE_V4)
90 p = caglobals.ip.u4s.port;
99 * Function will extract 0, 1 or 2 filters from query.
100 * More than 2 filters or unsupported filters will result in error.
101 * If both filters are of the same supported type, the 2nd one will be picked.
102 * Resource and device filters in the SAME query are NOT validated
103 * and resources will likely not clear filters.
105 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
110 char *restOfQuery = NULL;
111 int numKeyValuePairsParsed = 0;
116 OC_LOG_V(INFO, TAG, "Extracting params from %s", query);
118 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
122 if (numKeyValuePairsParsed >= 2)
124 OC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
125 return OC_STACK_INVALID_QUERY;
128 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
132 return OC_STACK_INVALID_QUERY;
134 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
136 *filterOne = value; // if
138 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
140 *filterTwo = value; // rt
144 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
145 return OC_STACK_INVALID_QUERY;
147 ++numKeyValuePairsParsed;
149 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
152 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
156 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
158 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
160 return OC_WELL_KNOWN_URI;
162 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
164 return OC_DEVICE_URI;
166 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
168 return OC_PLATFORM_URI;
170 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
172 return OC_RESOURCE_TYPES_URI;
174 #ifdef ROUTING_GATEWAY
175 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
177 return OC_GATEWAY_URI;
181 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
185 #endif //WITH_PRESENCE
186 return OC_UNKNOWN_URI;
189 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
190 char **filterOne, char **filterTwo)
192 if(!filterOne || !filterTwo)
194 return OC_STACK_INVALID_PARAM;
201 if (uri == OC_PRESENCE)
203 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
204 OC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
209 OCStackResult result = OC_STACK_OK;
213 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
219 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
220 OCRepPayload** payload)
222 OCRepPayload *tempPayload = OCRepPayloadCreate();
226 OCRepPayloadDestroy(tempPayload);
227 return OC_STACK_INVALID_PARAM;
232 return OC_STACK_NO_MEMORY;
235 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
237 OCResourceType *resType = resourcePtr->rsrcType;
240 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
241 resType = resType->next;
244 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
247 OCRepPayloadAddInterface(tempPayload, resInterface->name);
248 resInterface = resInterface->next;
251 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
254 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
255 resAttrib->attrValue);
256 resAttrib = resAttrib->next;
261 *payload = tempPayload;
265 OCRepPayloadAppend(*payload, tempPayload);
271 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
272 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
274 if (!resourcePtr || !payload)
276 return OC_STACK_INVALID_PARAM;
279 if (resourcePtr->resourceProperties & OC_SECURE)
281 if (GetSecurePortInfo(devAddr, &port) != OC_STACK_OK)
287 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
291 OCStackResult BuildVirtualCollectionResourceResponse(const OCResourceCollectionPayload *resourcePtr,
292 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
294 if (!resourcePtr || !payload)
296 return OC_STACK_INVALID_PARAM;
298 if (resourcePtr->tags && (resourcePtr->tags->bitmap & OC_SECURE))
300 if (GetSecurePortInfo(devAddr, &resourcePtr->tags->port) != OC_STACK_OK)
302 OC_LOG(ERROR, TAG, "Failed setting secure port.");
305 if (resourcePtr->tags && !resourcePtr->tags->baseURI)
307 resourcePtr->tags->baseURI = OICStrdup(devAddr->addr);
309 OCDiscoveryCollectionPayloadAddResource(payload, resourcePtr->tags, resourcePtr->setLinks);
313 uint8_t IsCollectionResource (OCResource *resource)
320 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
322 if (resource->rsrcResources[i])
330 OCResource *FindResourceByUri(const char* resourceUri)
337 OCResource * pointer = headResource;
340 if (strcmp(resourceUri, pointer->uri) == 0)
344 pointer = pointer->next;
346 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
351 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
352 ResourceHandling *handling,
353 OCResource **resource)
355 if(!request || !handling || !resource)
357 return OC_STACK_INVALID_PARAM;
360 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
362 // Check if virtual resource
363 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
365 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
366 *handling = OC_RESOURCE_VIRTUAL;
367 *resource = headResource;
370 if (strlen((const char*)(request->resourceUrl)) == 0)
372 // Resource URL not specified
373 *handling = OC_RESOURCE_NOT_SPECIFIED;
374 return OC_STACK_NO_RESOURCE;
378 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
379 *resource = resourcePtr;
382 if(defaultDeviceHandler)
384 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
388 // Resource does not exist
389 // and default device handler does not exist
390 *handling = OC_RESOURCE_NOT_SPECIFIED;
391 return OC_STACK_NO_RESOURCE;
394 if (IsCollectionResource (resourcePtr))
396 // Collection resource
397 if (resourcePtr->entityHandler != defaultResourceEHandler)
399 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
404 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
410 // Resource not a collection
411 if (resourcePtr->entityHandler != defaultResourceEHandler)
413 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
418 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
425 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
427 OCStackResult result;
432 result = OC_STACK_OK;
435 result = OC_STACK_SLOW_RESOURCE;
438 result = OC_STACK_ERROR;
440 case OC_EH_FORBIDDEN:
441 result = OC_STACK_RESOURCE_ERROR;
443 case OC_EH_RESOURCE_CREATED:
444 result = OC_STACK_RESOURCE_CREATED;
446 case OC_EH_RESOURCE_DELETED:
447 result = OC_STACK_RESOURCE_DELETED;
449 case OC_EH_RESOURCE_NOT_FOUND:
450 result = OC_STACK_NO_RESOURCE;
453 result = OC_STACK_ERROR;
459 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
466 // Null or empty is analogous to no filter.
467 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
472 OCResourceType *resourceTypePtr = resource->rsrcType;
474 while (resourceTypePtr)
476 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
480 resourceTypePtr = resourceTypePtr->next;
483 OC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
487 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
494 // Null or empty is analogous to no filter.
495 if (interfaceFilter == NULL || *interfaceFilter == 0)
500 OCResourceInterface *interfacePtr = resource->rsrcInterface;
504 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
508 interfacePtr = interfacePtr->next;
511 OC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
516 * If the filters are null, they will be assumed to NOT be present
517 * and the resource will not be matched against them.
518 * Function will return true if all non null AND non empty filters passed in find a match.
520 static bool includeThisResourceInResponse(OCResource *resource,
521 char *interfaceFilter,
522 char *resourceTypeFilter)
526 OC_LOG(ERROR, TAG, "Invalid resource");
530 if ( resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
533 * At least one valid filter should be available to
534 * include the resource in discovery response
536 if (!((interfaceFilter && *interfaceFilter ) ||
537 (resourceTypeFilter && *resourceTypeFilter)))
539 OC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
540 resource", resource->uri);
544 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
545 !(resource->resourceProperties & OC_DISCOVERABLE))
547 OC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
551 return resourceMatchesIFFilter(resource, interfaceFilter) &&
552 resourceMatchesRTFilter(resource, resourceTypeFilter);
556 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
557 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
559 OCEntityHandlerResponse response = {0};
561 response.ehResult = ehResult;
562 response.payload = discoveryPayload;
563 response.persistentBufferFlag = 0;
564 response.requestHandle = (OCRequestHandle) request;
565 response.resourceHandle = (OCResourceHandle) resource;
567 return OCDoResponse(&response);
571 static OCStackResult checkResourceExistsAtRD(const char *interfaceType, const char *resourceType,
572 OCResourceCollectionPayload **repPayload)
574 if (OCRDCheckPublishedResource(interfaceType, resourceType, repPayload) == OC_STACK_OK)
580 OC_LOG_V(ERROR, TAG, "The resource type or interface type doe not exist \
581 on the resource directory");
583 return OC_STACK_ERROR;
587 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
589 if (!request || !resource)
591 return OC_STACK_INVALID_PARAM;
594 OCStackResult discoveryResult = OC_STACK_ERROR;
596 bool bMulticast = false; // Was the discovery request a multicast request?
597 OCPayload* payload = NULL;
599 OC_LOG(INFO, TAG, "Entering HandleVirtualResource");
601 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
603 // Step 1: Generate the response to discovery request
604 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
606 char *filterOne = NULL;
607 char *filterTwo = NULL;
609 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
610 &filterOne, &filterTwo);
612 if (discoveryResult == OC_STACK_OK)
614 payload = (OCPayload*)OCDiscoveryPayloadCreate();
618 bool foundResourceAtRD = false;
619 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
622 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
624 OCResourceCollectionPayload *repPayload;
625 discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo, &repPayload);
626 if (discoveryResult != OC_STACK_OK)
630 discoveryResult = BuildVirtualCollectionResourceResponse(repPayload,
631 (OCDiscoveryPayload*)payload,
633 foundResourceAtRD = true;
636 if(!foundResourceAtRD && includeThisResourceInResponse(resource, filterOne, filterTwo))
638 discoveryResult = BuildVirtualResourceResponse(resource,
639 (OCDiscoveryPayload*)payload,
643 // Set discoveryResult appropriately if no 'valid' resources are available
644 if (((OCDiscoveryPayload*)payload)->resources == NULL && !foundResourceAtRD)
646 discoveryResult = OC_STACK_NO_RESOURCE;
651 discoveryResult = OC_STACK_NO_MEMORY;
656 OC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
659 else if (virtualUriInRequest == OC_DEVICE_URI)
661 const OicUuid_t* deviceId = OCGetServerInstanceID();
664 discoveryResult = OC_STACK_ERROR;
668 payload = (OCPayload*) OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
669 (const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
670 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
673 discoveryResult = OC_STACK_NO_MEMORY;
677 discoveryResult = OC_STACK_OK;
681 else if (virtualUriInRequest == OC_PLATFORM_URI)
683 payload = (OCPayload*)OCPlatformPayloadCreate(
684 OC_RSRVD_PLATFORM_URI,
688 discoveryResult = OC_STACK_NO_MEMORY;
692 discoveryResult = OC_STACK_OK;
695 #ifdef ROUTING_GATEWAY
696 else if (OC_GATEWAY_URI == virtualUriInRequest)
698 // Received request for a gateway
699 OC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
700 discoveryResult = RMHandleGatewayRequest(request, resource);
706 * Step 2: Send the discovery response
708 * Iotivity should respond to discovery requests in below manner:
709 * 1)If query filter matching fails and discovery request is multicast,
710 * it should NOT send any response.
711 * 2)If query filter matching fails and discovery request is unicast,
712 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
713 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
714 * request is multicast, it should NOT send any response.
715 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
716 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
720 if ((virtualUriInRequest == OC_PRESENCE) &&
721 (resource->resourceProperties & OC_ACTIVE))
723 // Presence uses observer notification api to respond via SendPresenceNotification.
724 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
728 #ifdef ROUTING_GATEWAY
729 // Gateway uses the RMHandleGatewayRequest to respond to the request.
730 if (OC_GATEWAY != virtualUriInRequest)
733 if(discoveryResult == OC_STACK_OK)
735 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
737 else if(bMulticast == false)
739 OC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
740 discovery request", discoveryResult, virtualUriInRequest);
741 SendNonPersistantDiscoveryResponse(request, resource, NULL,
742 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
746 // Ignoring the discovery request as per RFC 7252, Section #8.2
747 OC_LOG(INFO, TAG, "Silently ignoring the request since device does not have \
748 any useful data to send");
752 OCPayloadDestroy(payload);
758 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
762 return OC_STACK_INVALID_PARAM;
765 OCStackResult result = OC_STACK_OK;
766 OCEntityHandlerResult ehResult = OC_EH_ERROR;
767 OCEntityHandlerRequest ehRequest = {0};
769 OC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
770 result = FormOCEntityHandlerRequest(&ehRequest,
771 (OCRequestHandle) request,
774 (OCResourceHandle) NULL, request->query,
775 PAYLOAD_TYPE_REPRESENTATION,
777 request->payloadSize,
778 request->numRcvdVendorSpecificHeaderOptions,
779 request->rcvdVendorSpecificHeaderOptions,
780 (OCObserveAction)request->observationOption,
782 VERIFY_SUCCESS(result, OC_STACK_OK);
784 // At this point we know for sure that defaultDeviceHandler exists
785 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
786 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
787 if(ehResult == OC_EH_SLOW)
789 OC_LOG(INFO, TAG, "This is a slow resource");
790 request->slowFlag = 1;
792 else if(ehResult == OC_EH_ERROR)
794 FindAndDeleteServerRequest(request);
796 result = EntityHandlerCodeToOCStackCode(ehResult);
798 OCPayloadDestroy(ehRequest.payload);
803 HandleResourceWithEntityHandler (OCServerRequest *request,
804 OCResource *resource,
805 uint8_t collectionResource)
807 if(!request || ! resource)
809 return OC_STACK_INVALID_PARAM;
812 OCStackResult result = OC_STACK_ERROR;
813 OCEntityHandlerResult ehResult = OC_EH_ERROR;
814 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
815 ResourceObserver *resObs = NULL;
817 OCEntityHandlerRequest ehRequest = {0};
819 OC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
820 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
821 // check the security resource
822 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
824 type = PAYLOAD_TYPE_SECURITY;
828 if (request && strcmp(request->resourceUrl, OC_RSRVD_RD_URI) == 0)
830 type = PAYLOAD_TYPE_RD;
833 result = FormOCEntityHandlerRequest(&ehRequest,
834 (OCRequestHandle)request,
837 (OCResourceHandle)resource,
841 request->payloadSize,
842 request->numRcvdVendorSpecificHeaderOptions,
843 request->rcvdVendorSpecificHeaderOptions,
844 (OCObserveAction)request->observationOption,
846 VERIFY_SUCCESS(result, OC_STACK_OK);
848 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
850 OC_LOG(INFO, TAG, "No observation requested");
851 ehFlag = OC_REQUEST_FLAG;
853 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
855 OC_LOG(INFO, TAG, "Observation registration requested");
857 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
858 VERIFY_SUCCESS(result, OC_STACK_OK);
860 result = AddObserver ((const char*)(request->resourceUrl),
861 (const char *)(request->query),
862 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
863 resource, request->qos, request->acceptFormat,
866 if(result == OC_STACK_OK)
868 OC_LOG(INFO, TAG, "Added observer successfully");
869 request->observeResult = OC_STACK_OK;
870 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
874 result = OC_STACK_OK;
876 // The error in observeResult for the request will be used when responding to this
877 // request by omitting the observation option/sequence number.
878 request->observeResult = OC_STACK_ERROR;
879 OC_LOG(ERROR, TAG, "Observer Addition failed");
880 ehFlag = OC_REQUEST_FLAG;
884 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
887 OC_LOG(INFO, TAG, "Deregistering observation requested");
889 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
893 // Stack does not contain this observation request
894 // Either token is incorrect or observation list is corrupted
895 result = OC_STACK_ERROR;
898 ehRequest.obsInfo.obsId = resObs->observeId;
899 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
901 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
903 if(result == OC_STACK_OK)
905 OC_LOG(INFO, TAG, "Removed observer successfully");
906 request->observeResult = OC_STACK_OK;
910 result = OC_STACK_OK;
911 request->observeResult = OC_STACK_ERROR;
912 OC_LOG(ERROR, TAG, "Observer Removal failed");
917 result = OC_STACK_ERROR;
921 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
922 if(ehResult == OC_EH_SLOW)
924 OC_LOG(INFO, TAG, "This is a slow resource");
925 request->slowFlag = 1;
927 else if(ehResult == OC_EH_ERROR)
929 FindAndDeleteServerRequest(request);
931 result = EntityHandlerCodeToOCStackCode(ehResult);
933 OCPayloadDestroy(ehRequest.payload);
938 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
939 OCResource *resource)
941 if(!request || !resource)
943 return OC_STACK_INVALID_PARAM;
946 OCStackResult result = OC_STACK_ERROR;
947 OCEntityHandlerRequest ehRequest = {0};
949 result = FormOCEntityHandlerRequest(&ehRequest,
950 (OCRequestHandle)request,
953 (OCResourceHandle)resource,
955 PAYLOAD_TYPE_REPRESENTATION,
957 request->payloadSize,
958 request->numRcvdVendorSpecificHeaderOptions,
959 request->rcvdVendorSpecificHeaderOptions,
960 (OCObserveAction)request->observationOption,
962 if(result == OC_STACK_OK)
964 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
967 OCPayloadDestroy(ehRequest.payload);
972 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
974 OCStackResult ret = OC_STACK_OK;
978 case OC_RESOURCE_VIRTUAL:
980 ret = HandleVirtualResource (request, resource);
983 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
985 ret = HandleDefaultDeviceEntityHandler(request);
988 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
990 OC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
991 return OC_STACK_ERROR;
993 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
995 ret = HandleResourceWithEntityHandler (request, resource, 0);
998 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1000 ret = HandleResourceWithEntityHandler (request, resource, 1);
1003 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1005 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1008 case OC_RESOURCE_NOT_SPECIFIED:
1010 ret = OC_STACK_NO_RESOURCE;
1015 OC_LOG(INFO, TAG, "Invalid Resource Determination");
1016 return OC_STACK_ERROR;
1022 void DeletePlatformInfo()
1024 OC_LOG(INFO, TAG, "Deleting platform info.");
1026 OICFree(savedPlatformInfo.platformID);
1027 savedPlatformInfo.platformID = NULL;
1029 OICFree(savedPlatformInfo.manufacturerName);
1030 savedPlatformInfo.manufacturerName = NULL;
1032 OICFree(savedPlatformInfo.manufacturerUrl);
1033 savedPlatformInfo.manufacturerUrl = NULL;
1035 OICFree(savedPlatformInfo.modelNumber);
1036 savedPlatformInfo.modelNumber = NULL;
1038 OICFree(savedPlatformInfo.dateOfManufacture);
1039 savedPlatformInfo.dateOfManufacture = NULL;
1041 OICFree(savedPlatformInfo.platformVersion);
1042 savedPlatformInfo.platformVersion = NULL;
1044 OICFree(savedPlatformInfo.operatingSystemVersion);
1045 savedPlatformInfo.operatingSystemVersion = NULL;
1047 OICFree(savedPlatformInfo.hardwareVersion);
1048 savedPlatformInfo.hardwareVersion = NULL;
1050 OICFree(savedPlatformInfo.firmwareVersion);
1051 savedPlatformInfo.firmwareVersion = NULL;
1053 OICFree(savedPlatformInfo.supportUrl);
1054 savedPlatformInfo.supportUrl = NULL;
1056 OICFree(savedPlatformInfo.systemTime);
1057 savedPlatformInfo.systemTime = NULL;
1060 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
1062 savedPlatformInfo.platformID = OICStrdup(info.platformID);
1063 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
1064 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
1065 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
1066 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
1067 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
1068 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
1069 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
1070 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
1071 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
1072 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
1074 if ((!savedPlatformInfo.platformID && info.platformID)||
1075 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
1076 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
1077 (!savedPlatformInfo.modelNumber && info.modelNumber)||
1078 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
1079 (!savedPlatformInfo.platformVersion && info.platformVersion)||
1080 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
1081 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
1082 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
1083 (!savedPlatformInfo.supportUrl && info.supportUrl)||
1084 (!savedPlatformInfo.systemTime && info.systemTime))
1086 DeletePlatformInfo();
1087 return OC_STACK_INVALID_PARAM;
1094 OCStackResult SavePlatformInfo(OCPlatformInfo info)
1096 DeletePlatformInfo();
1098 OCStackResult res = DeepCopyPlatFormInfo(info);
1100 if (res != OC_STACK_OK)
1102 OC_LOG_V(ERROR, TAG, "Failed to save platform info. errno(%d)", res);
1106 OC_LOG(INFO, TAG, "Platform info saved.");
1112 void DeleteDeviceInfo()
1114 OC_LOG(INFO, TAG, "Deleting device info.");
1116 OICFree(savedDeviceInfo.deviceName);
1117 savedDeviceInfo.deviceName = NULL;
1120 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1122 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1124 if(!savedDeviceInfo.deviceName && info.deviceName)
1127 return OC_STACK_NO_MEMORY;
1133 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1135 OCStackResult res = OC_STACK_OK;
1139 res = DeepCopyDeviceInfo(info);
1141 VERIFY_SUCCESS(res, OC_STACK_OK);
1143 if(OCGetServerInstanceID() == NULL)
1145 OC_LOG(INFO, TAG, "Device ID generation failed");
1146 res = OC_STACK_ERROR;
1150 OC_LOG(INFO, TAG, "Device initialized successfully.");