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 #include "rd_server.h"
46 #ifdef ROUTING_GATEWAY
47 #include "routingmanager.h"
51 #define TAG "ocresource"
52 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
53 {OC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
55 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
56 TAG, #arg " is NULL"); return (retVal); } }
58 extern OCResource *headResource;
59 static OCPlatformInfo savedPlatformInfo = {0};
60 static OCDeviceInfo savedDeviceInfo = {0};
62 //-----------------------------------------------------------------------------
63 // Default resource entity handler function
64 //-----------------------------------------------------------------------------
65 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
66 OCEntityHandlerRequest * request, void* callbackParam)
68 //TODO ("Implement me!!!!");
69 // TODO: remove silence unused param warnings
73 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
76 /* This method will retrieve the port at which the secure resource is hosted */
77 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
81 if (endpoint->adapter == OC_ADAPTER_IP)
83 if (endpoint->flags & OC_IP_USE_V6)
85 p = caglobals.ip.u6s.port;
87 else if (endpoint->flags & OC_IP_USE_V4)
89 p = caglobals.ip.u4s.port;
98 * Function will extract 0, 1 or 2 filters from query.
99 * More than 2 filters or unsupported filters will result in error.
100 * If both filters are of the same supported type, the 2nd one will be picked.
101 * Resource and device filters in the SAME query are NOT validated
102 * and resources will likely not clear filters.
104 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
109 char *restOfQuery = NULL;
110 int numKeyValuePairsParsed = 0;
115 OC_LOG_V(INFO, TAG, "Extracting params from %s", query);
117 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
121 if (numKeyValuePairsParsed >= 2)
123 OC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
124 return OC_STACK_INVALID_QUERY;
127 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
131 return OC_STACK_INVALID_QUERY;
133 else if (strcmp (key, OC_RSRVD_INTERFACE) == 0)
135 *filterOne = value; // if
137 else if (strcmp (key, OC_RSRVD_RESOURCE_TYPE) == 0)
139 *filterTwo = value; // rt
143 OC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
144 return OC_STACK_INVALID_QUERY;
146 ++numKeyValuePairsParsed;
148 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
151 OC_LOG_V(INFO, TAG, "Extracted params %s and %s.", *filterOne, *filterTwo);
155 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
157 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
159 return OC_WELL_KNOWN_URI;
161 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
163 return OC_DEVICE_URI;
165 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
167 return OC_PLATFORM_URI;
169 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
171 return OC_RESOURCE_TYPES_URI;
173 #ifdef ROUTING_GATEWAY
174 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
176 return OC_GATEWAY_URI;
180 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
184 #endif //WITH_PRESENCE
185 return OC_UNKNOWN_URI;
188 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
189 char **filterOne, char **filterTwo)
191 if(!filterOne || !filterTwo)
193 return OC_STACK_INVALID_PARAM;
200 if (uri == OC_PRESENCE)
202 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
203 OC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
208 OCStackResult result = OC_STACK_OK;
212 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
218 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
219 OCRepPayload** payload)
221 OCRepPayload *tempPayload = OCRepPayloadCreate();
225 OCRepPayloadDestroy(tempPayload);
226 return OC_STACK_INVALID_PARAM;
231 return OC_STACK_NO_MEMORY;
234 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
236 OCResourceType *resType = resourcePtr->rsrcType;
239 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
240 resType = resType->next;
243 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
246 OCRepPayloadAddInterface(tempPayload, resInterface->name);
247 resInterface = resInterface->next;
250 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
253 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
254 resAttrib->attrValue);
255 resAttrib = resAttrib->next;
260 *payload = tempPayload;
264 OCRepPayloadAppend(*payload, tempPayload);
270 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
271 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
273 if (!resourcePtr || !payload)
275 return OC_STACK_INVALID_PARAM;
278 if (resourcePtr->resourceProperties & OC_SECURE)
280 if (GetSecurePortInfo(devAddr, &port) != OC_STACK_OK)
286 OCDiscoveryPayloadAddResource(payload, resourcePtr, port);
291 uint8_t IsCollectionResource (OCResource *resource)
298 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
300 if (resource->rsrcResources[i])
308 OCResource *FindResourceByUri(const char* resourceUri)
315 OCResource * pointer = headResource;
318 if (strcmp(resourceUri, pointer->uri) == 0)
322 pointer = pointer->next;
324 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
329 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
330 ResourceHandling *handling,
331 OCResource **resource)
333 if(!request || !handling || !resource)
335 return OC_STACK_INVALID_PARAM;
338 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
340 // Check if virtual resource
341 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
343 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
344 *handling = OC_RESOURCE_VIRTUAL;
345 *resource = headResource;
348 if (strlen((const char*)(request->resourceUrl)) == 0)
350 // Resource URL not specified
351 *handling = OC_RESOURCE_NOT_SPECIFIED;
352 return OC_STACK_NO_RESOURCE;
356 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
357 *resource = resourcePtr;
360 if(defaultDeviceHandler)
362 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
366 // Resource does not exist
367 // and default device handler does not exist
368 *handling = OC_RESOURCE_NOT_SPECIFIED;
369 return OC_STACK_NO_RESOURCE;
372 if (IsCollectionResource (resourcePtr))
374 // Collection resource
375 if (resourcePtr->entityHandler != defaultResourceEHandler)
377 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
382 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
388 // Resource not a collection
389 if (resourcePtr->entityHandler != defaultResourceEHandler)
391 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
396 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
403 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
405 OCStackResult result;
410 result = OC_STACK_OK;
413 result = OC_STACK_SLOW_RESOURCE;
416 result = OC_STACK_ERROR;
418 case OC_EH_FORBIDDEN:
419 result = OC_STACK_RESOURCE_ERROR;
421 case OC_EH_RESOURCE_CREATED:
422 result = OC_STACK_RESOURCE_CREATED;
424 case OC_EH_RESOURCE_DELETED:
425 result = OC_STACK_RESOURCE_DELETED;
427 case OC_EH_RESOURCE_NOT_FOUND:
428 result = OC_STACK_NO_RESOURCE;
431 result = OC_STACK_ERROR;
437 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
444 // Null or empty is analogous to no filter.
445 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
450 OCResourceType *resourceTypePtr = resource->rsrcType;
452 while (resourceTypePtr)
454 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
458 resourceTypePtr = resourceTypePtr->next;
461 OC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
465 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
472 // Null or empty is analogous to no filter.
473 if (interfaceFilter == NULL || *interfaceFilter == 0)
478 OCResourceInterface *interfacePtr = resource->rsrcInterface;
482 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
486 interfacePtr = interfacePtr->next;
489 OC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
494 * If the filters are null, they will be assumed to NOT be present
495 * and the resource will not be matched against them.
496 * Function will return true if all non null AND non empty filters passed in find a match.
498 static bool includeThisResourceInResponse(OCResource *resource,
499 char *interfaceFilter,
500 char *resourceTypeFilter)
504 OC_LOG(ERROR, TAG, "Invalid resource");
508 if ( resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
511 * At least one valid filter should be available to
512 * include the resource in discovery response
514 if (!((interfaceFilter && *interfaceFilter ) ||
515 (resourceTypeFilter && *resourceTypeFilter)))
517 OC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
518 resource", resource->uri);
522 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
523 !(resource->resourceProperties & OC_DISCOVERABLE))
525 OC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
529 return resourceMatchesIFFilter(resource, interfaceFilter) &&
530 resourceMatchesRTFilter(resource, resourceTypeFilter);
534 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
535 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
537 OCEntityHandlerResponse response = {0};
539 response.ehResult = ehResult;
540 response.payload = discoveryPayload;
541 response.persistentBufferFlag = 0;
542 response.requestHandle = (OCRequestHandle) request;
543 response.resourceHandle = (OCResourceHandle) resource;
545 return OCDoResponse(&response);
549 static OCStackResult checkResourceExistsAtRD(const char *interfaceType, const char *resourceType, OCRepPayload **repPayload)
555 if (OCRDCheckPublishedResource(interfaceType, resourceType, &uri, &rt, &itf) == OC_STACK_OK)
557 if (!uri || !rt || !itf)
559 OC_LOG_V(ERROR, TAG, "Failed allocating memory.");
560 // if any of the parameter has memory allocated free each one of them.
564 return OC_STACK_NO_MEMORY;
567 OCRepPayload *rdResource = OICCalloc(1, sizeof(OCRepPayload));
570 OC_LOG_V(ERROR, TAG, "Failed allocating memory.");
574 return OC_STACK_NO_MEMORY;
577 rdResource->uri = uri;
579 rdResource->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
580 if(!rdResource->types)
582 OC_LOG_V(ERROR, TAG, "Failed allocating memory.");
587 return OC_STACK_NO_MEMORY;
589 rdResource->types->value = rt;
591 rdResource->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
592 if(!rdResource->interfaces)
594 OC_LOG_V(ERROR, TAG, "Failed allocating memory.");
599 return OC_STACK_NO_MEMORY;
601 rdResource->interfaces->value = itf;
603 *repPayload = rdResource;
609 OC_LOG_V(ERROR, TAG, "The resource type or interface type doe not exist \
610 on the resource directory");
612 return OC_STACK_ERROR;
616 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
618 if (!request || !resource)
620 return OC_STACK_INVALID_PARAM;
623 OCStackResult discoveryResult = OC_STACK_ERROR;
625 bool bMulticast = false; // Was the discovery request a multicast request?
626 OCPayload* payload = NULL;
628 OC_LOG(INFO, TAG, "Entering HandleVirtualResource");
630 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
632 // Step 1: Generate the response to discovery request
633 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
635 char *filterOne = NULL;
636 char *filterTwo = NULL;
638 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
639 &filterOne, &filterTwo);
641 if (discoveryResult == OC_STACK_OK)
643 payload = (OCPayload*)OCDiscoveryPayloadCreate();
647 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
649 bool foundResourceAtRD = false;
651 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
653 OCRepPayload *repPayload = NULL;
654 discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo, &repPayload);
655 if (discoveryResult != OC_STACK_OK)
659 discoveryResult = BuildVirtualResourceResponse((OCResource *)repPayload,
660 (OCDiscoveryPayload*)payload,
662 foundResourceAtRD = true;
665 if(!foundResourceAtRD && includeThisResourceInResponse(resource, filterOne, filterTwo))
667 discoveryResult = BuildVirtualResourceResponse(resource,
668 (OCDiscoveryPayload*)payload,
672 // Set discoveryResult appropriately if no 'valid' resources are available.
673 if (((OCDiscoveryPayload*)payload)->resources == NULL)
675 discoveryResult = OC_STACK_NO_RESOURCE;
680 discoveryResult = OC_STACK_NO_MEMORY;
685 OC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
688 else if (virtualUriInRequest == OC_DEVICE_URI)
690 const OicUuid_t* deviceId = OCGetServerInstanceID();
693 discoveryResult = OC_STACK_ERROR;
697 payload = (OCPayload*) OCDevicePayloadCreate(OC_RSRVD_DEVICE_URI,
698 (const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
699 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
702 discoveryResult = OC_STACK_NO_MEMORY;
706 discoveryResult = OC_STACK_OK;
710 else if (virtualUriInRequest == OC_PLATFORM_URI)
712 payload = (OCPayload*)OCPlatformPayloadCreate(
713 OC_RSRVD_PLATFORM_URI,
717 discoveryResult = OC_STACK_NO_MEMORY;
721 discoveryResult = OC_STACK_OK;
724 #ifdef ROUTING_GATEWAY
725 else if (OC_GATEWAY_URI == virtualUriInRequest)
727 // Received request for a gateway
728 OC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
729 discoveryResult = RMHandleGatewayRequest(request, resource);
735 * Step 2: Send the discovery response
737 * Iotivity should respond to discovery requests in below manner:
738 * 1)If query filter matching fails and discovery request is multicast,
739 * it should NOT send any response.
740 * 2)If query filter matching fails and discovery request is unicast,
741 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
742 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
743 * request is multicast, it should NOT send any response.
744 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
745 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
749 if ((virtualUriInRequest == OC_PRESENCE) &&
750 (resource->resourceProperties & OC_ACTIVE))
752 // Presence uses observer notification api to respond via SendPresenceNotification.
753 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
757 #ifdef ROUTING_GATEWAY
758 // Gateway uses the RMHandleGatewayRequest to respond to the request.
759 if (OC_GATEWAY != virtualUriInRequest)
762 if(discoveryResult == OC_STACK_OK)
764 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
766 else if(bMulticast == false)
768 OC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
769 discovery request", discoveryResult, virtualUriInRequest);
770 SendNonPersistantDiscoveryResponse(request, resource, NULL,
771 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
775 // Ignoring the discovery request as per RFC 7252, Section #8.2
776 OC_LOG(INFO, TAG, "Silently ignoring the request since device does not have \
777 any useful data to send");
781 OCPayloadDestroy(payload);
787 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
791 return OC_STACK_INVALID_PARAM;
794 OCStackResult result = OC_STACK_OK;
795 OCEntityHandlerResult ehResult = OC_EH_ERROR;
796 OCEntityHandlerRequest ehRequest = {0};
798 OC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
799 result = FormOCEntityHandlerRequest(&ehRequest,
800 (OCRequestHandle) request,
803 (OCResourceHandle) NULL, request->query,
804 PAYLOAD_TYPE_REPRESENTATION,
806 request->payloadSize,
807 request->numRcvdVendorSpecificHeaderOptions,
808 request->rcvdVendorSpecificHeaderOptions,
809 (OCObserveAction)request->observationOption,
811 VERIFY_SUCCESS(result, OC_STACK_OK);
813 // At this point we know for sure that defaultDeviceHandler exists
814 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
815 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
816 if(ehResult == OC_EH_SLOW)
818 OC_LOG(INFO, TAG, "This is a slow resource");
819 request->slowFlag = 1;
821 else if(ehResult == OC_EH_ERROR)
823 FindAndDeleteServerRequest(request);
825 result = EntityHandlerCodeToOCStackCode(ehResult);
827 OCPayloadDestroy(ehRequest.payload);
832 HandleResourceWithEntityHandler (OCServerRequest *request,
833 OCResource *resource,
834 uint8_t collectionResource)
836 if(!request || ! resource)
838 return OC_STACK_INVALID_PARAM;
841 OCStackResult result = OC_STACK_ERROR;
842 OCEntityHandlerResult ehResult = OC_EH_ERROR;
843 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
844 ResourceObserver *resObs = NULL;
846 OCEntityHandlerRequest ehRequest = {0};
848 OC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
849 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
850 // check the security resource
851 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
853 type = PAYLOAD_TYPE_SECURITY;
856 result = FormOCEntityHandlerRequest(&ehRequest,
857 (OCRequestHandle)request,
860 (OCResourceHandle)resource,
864 request->payloadSize,
865 request->numRcvdVendorSpecificHeaderOptions,
866 request->rcvdVendorSpecificHeaderOptions,
867 (OCObserveAction)request->observationOption,
869 VERIFY_SUCCESS(result, OC_STACK_OK);
871 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
873 OC_LOG(INFO, TAG, "No observation requested");
874 ehFlag = OC_REQUEST_FLAG;
876 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
878 OC_LOG(INFO, TAG, "Observation registration requested");
880 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
881 VERIFY_SUCCESS(result, OC_STACK_OK);
883 result = AddObserver ((const char*)(request->resourceUrl),
884 (const char *)(request->query),
885 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
886 resource, request->qos, request->acceptFormat,
889 if(result == OC_STACK_OK)
891 OC_LOG(INFO, TAG, "Added observer successfully");
892 request->observeResult = OC_STACK_OK;
893 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
897 result = OC_STACK_OK;
899 // The error in observeResult for the request will be used when responding to this
900 // request by omitting the observation option/sequence number.
901 request->observeResult = OC_STACK_ERROR;
902 OC_LOG(ERROR, TAG, "Observer Addition failed");
903 ehFlag = OC_REQUEST_FLAG;
907 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
910 OC_LOG(INFO, TAG, "Deregistering observation requested");
912 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
916 // Stack does not contain this observation request
917 // Either token is incorrect or observation list is corrupted
918 result = OC_STACK_ERROR;
921 ehRequest.obsInfo.obsId = resObs->observeId;
922 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
924 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
926 if(result == OC_STACK_OK)
928 OC_LOG(INFO, TAG, "Removed observer successfully");
929 request->observeResult = OC_STACK_OK;
933 result = OC_STACK_OK;
934 request->observeResult = OC_STACK_ERROR;
935 OC_LOG(ERROR, TAG, "Observer Removal failed");
940 result = OC_STACK_ERROR;
944 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
945 if(ehResult == OC_EH_SLOW)
947 OC_LOG(INFO, TAG, "This is a slow resource");
948 request->slowFlag = 1;
950 else if(ehResult == OC_EH_ERROR)
952 FindAndDeleteServerRequest(request);
954 result = EntityHandlerCodeToOCStackCode(ehResult);
956 OCPayloadDestroy(ehRequest.payload);
961 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
962 OCResource *resource)
964 if(!request || !resource)
966 return OC_STACK_INVALID_PARAM;
969 OCStackResult result = OC_STACK_ERROR;
970 OCEntityHandlerRequest ehRequest = {0};
972 result = FormOCEntityHandlerRequest(&ehRequest,
973 (OCRequestHandle)request,
976 (OCResourceHandle)resource,
978 PAYLOAD_TYPE_REPRESENTATION,
980 request->payloadSize,
981 request->numRcvdVendorSpecificHeaderOptions,
982 request->rcvdVendorSpecificHeaderOptions,
983 (OCObserveAction)request->observationOption,
985 if(result == OC_STACK_OK)
987 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
990 OCPayloadDestroy(ehRequest.payload);
995 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
997 OCStackResult ret = OC_STACK_OK;
1001 case OC_RESOURCE_VIRTUAL:
1003 ret = HandleVirtualResource (request, resource);
1006 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1008 ret = HandleDefaultDeviceEntityHandler(request);
1011 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1013 OC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1014 return OC_STACK_ERROR;
1016 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1018 ret = HandleResourceWithEntityHandler (request, resource, 0);
1021 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1023 ret = HandleResourceWithEntityHandler (request, resource, 1);
1026 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1028 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1031 case OC_RESOURCE_NOT_SPECIFIED:
1033 ret = OC_STACK_NO_RESOURCE;
1038 OC_LOG(INFO, TAG, "Invalid Resource Determination");
1039 return OC_STACK_ERROR;
1045 void DeletePlatformInfo()
1047 OC_LOG(INFO, TAG, "Deleting platform info.");
1049 OICFree(savedPlatformInfo.platformID);
1050 savedPlatformInfo.platformID = NULL;
1052 OICFree(savedPlatformInfo.manufacturerName);
1053 savedPlatformInfo.manufacturerName = NULL;
1055 OICFree(savedPlatformInfo.manufacturerUrl);
1056 savedPlatformInfo.manufacturerUrl = NULL;
1058 OICFree(savedPlatformInfo.modelNumber);
1059 savedPlatformInfo.modelNumber = NULL;
1061 OICFree(savedPlatformInfo.dateOfManufacture);
1062 savedPlatformInfo.dateOfManufacture = NULL;
1064 OICFree(savedPlatformInfo.platformVersion);
1065 savedPlatformInfo.platformVersion = NULL;
1067 OICFree(savedPlatformInfo.operatingSystemVersion);
1068 savedPlatformInfo.operatingSystemVersion = NULL;
1070 OICFree(savedPlatformInfo.hardwareVersion);
1071 savedPlatformInfo.hardwareVersion = NULL;
1073 OICFree(savedPlatformInfo.firmwareVersion);
1074 savedPlatformInfo.firmwareVersion = NULL;
1076 OICFree(savedPlatformInfo.supportUrl);
1077 savedPlatformInfo.supportUrl = NULL;
1079 OICFree(savedPlatformInfo.systemTime);
1080 savedPlatformInfo.systemTime = NULL;
1083 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
1085 savedPlatformInfo.platformID = OICStrdup(info.platformID);
1086 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
1087 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
1088 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
1089 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
1090 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
1091 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
1092 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
1093 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
1094 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
1095 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
1097 if ((!savedPlatformInfo.platformID && info.platformID)||
1098 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
1099 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
1100 (!savedPlatformInfo.modelNumber && info.modelNumber)||
1101 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
1102 (!savedPlatformInfo.platformVersion && info.platformVersion)||
1103 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
1104 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
1105 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
1106 (!savedPlatformInfo.supportUrl && info.supportUrl)||
1107 (!savedPlatformInfo.systemTime && info.systemTime))
1109 DeletePlatformInfo();
1110 return OC_STACK_INVALID_PARAM;
1117 OCStackResult SavePlatformInfo(OCPlatformInfo info)
1119 DeletePlatformInfo();
1121 OCStackResult res = DeepCopyPlatFormInfo(info);
1123 if (res != OC_STACK_OK)
1125 OC_LOG_V(ERROR, TAG, "Failed to save platform info. errno(%d)", res);
1129 OC_LOG(INFO, TAG, "Platform info saved.");
1135 void DeleteDeviceInfo()
1137 OC_LOG(INFO, TAG, "Deleting device info.");
1139 OICFree(savedDeviceInfo.deviceName);
1140 savedDeviceInfo.deviceName = NULL;
1143 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1145 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1147 if(!savedDeviceInfo.deviceName && info.deviceName)
1150 return OC_STACK_NO_MEMORY;
1156 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1158 OCStackResult res = OC_STACK_OK;
1162 res = DeepCopyDeviceInfo(info);
1164 VERIFY_SUCCESS(res, OC_STACK_OK);
1166 if(OCGetServerInstanceID() == NULL)
1168 OC_LOG(INFO, TAG, "Device ID generation failed");
1169 res = OC_STACK_ERROR;
1173 OC_LOG(INFO, TAG, "Device initialized successfully.");