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);
308 if (resourcePtr->tags->port == 0 && devAddr->port != 0)
310 resourcePtr->tags->port = devAddr->port;
313 OCDiscoveryCollectionPayloadAddResource(payload, resourcePtr->tags, resourcePtr->setLinks);
317 uint8_t IsCollectionResource (OCResource *resource)
324 for (int i = 0; i < MAX_CONTAINED_RESOURCES; i++)
326 if (resource->rsrcResources[i])
334 OCResource *FindResourceByUri(const char* resourceUri)
341 OCResource * pointer = headResource;
344 if (strcmp(resourceUri, pointer->uri) == 0)
348 pointer = pointer->next;
350 OC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
355 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
356 ResourceHandling *handling,
357 OCResource **resource)
359 if(!request || !handling || !resource)
361 return OC_STACK_INVALID_PARAM;
364 OC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
366 // Check if virtual resource
367 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
369 OC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
370 *handling = OC_RESOURCE_VIRTUAL;
371 *resource = headResource;
374 if (strlen((const char*)(request->resourceUrl)) == 0)
376 // Resource URL not specified
377 *handling = OC_RESOURCE_NOT_SPECIFIED;
378 return OC_STACK_NO_RESOURCE;
382 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
383 *resource = resourcePtr;
386 if(defaultDeviceHandler)
388 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
392 // Resource does not exist
393 // and default device handler does not exist
394 *handling = OC_RESOURCE_NOT_SPECIFIED;
395 return OC_STACK_NO_RESOURCE;
398 if (IsCollectionResource (resourcePtr))
400 // Collection resource
401 if (resourcePtr->entityHandler != defaultResourceEHandler)
403 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
408 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
414 // Resource not a collection
415 if (resourcePtr->entityHandler != defaultResourceEHandler)
417 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
422 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
429 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
431 OCStackResult result;
436 result = OC_STACK_OK;
439 result = OC_STACK_SLOW_RESOURCE;
442 result = OC_STACK_ERROR;
444 case OC_EH_FORBIDDEN:
445 result = OC_STACK_RESOURCE_ERROR;
447 case OC_EH_RESOURCE_CREATED:
448 result = OC_STACK_RESOURCE_CREATED;
450 case OC_EH_RESOURCE_DELETED:
451 result = OC_STACK_RESOURCE_DELETED;
453 case OC_EH_RESOURCE_NOT_FOUND:
454 result = OC_STACK_NO_RESOURCE;
457 result = OC_STACK_ERROR;
463 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
470 // Null or empty is analogous to no filter.
471 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
476 OCResourceType *resourceTypePtr = resource->rsrcType;
478 while (resourceTypePtr)
480 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
484 resourceTypePtr = resourceTypePtr->next;
487 OC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
491 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
498 // Null or empty is analogous to no filter.
499 if (interfaceFilter == NULL || *interfaceFilter == 0)
504 OCResourceInterface *interfacePtr = resource->rsrcInterface;
508 if (strcmp (interfacePtr->name, interfaceFilter) == 0)
512 interfacePtr = interfacePtr->next;
515 OC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
520 * If the filters are null, they will be assumed to NOT be present
521 * and the resource will not be matched against them.
522 * Function will return true if all non null AND non empty filters passed in find a match.
524 static bool includeThisResourceInResponse(OCResource *resource,
525 char *interfaceFilter,
526 char *resourceTypeFilter)
530 OC_LOG(ERROR, TAG, "Invalid resource");
534 if ( resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
537 * At least one valid filter should be available to
538 * include the resource in discovery response
540 if (!((interfaceFilter && *interfaceFilter ) ||
541 (resourceTypeFilter && *resourceTypeFilter)))
543 OC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
544 resource", resource->uri);
548 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
549 !(resource->resourceProperties & OC_DISCOVERABLE))
551 OC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
555 return resourceMatchesIFFilter(resource, interfaceFilter) &&
556 resourceMatchesRTFilter(resource, resourceTypeFilter);
560 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
561 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
563 OCEntityHandlerResponse response = {0};
565 response.ehResult = ehResult;
566 response.payload = discoveryPayload;
567 response.persistentBufferFlag = 0;
568 response.requestHandle = (OCRequestHandle) request;
569 response.resourceHandle = (OCResourceHandle) resource;
571 return OCDoResponse(&response);
575 static OCStackResult checkResourceExistsAtRD(const char *interfaceType, const char *resourceType,
576 OCResourceCollectionPayload **repPayload, OCDevAddr *devAddr)
578 if (OCRDCheckPublishedResource(interfaceType, resourceType, repPayload, devAddr) == OC_STACK_OK)
584 OC_LOG_V(ERROR, TAG, "The resource type or interface type doe not exist \
585 on the resource directory");
587 return OC_STACK_ERROR;
591 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
593 if (!request || !resource)
595 return OC_STACK_INVALID_PARAM;
598 OCStackResult discoveryResult = OC_STACK_ERROR;
600 bool bMulticast = false; // Was the discovery request a multicast request?
601 OCPayload* payload = NULL;
603 OC_LOG(INFO, TAG, "Entering HandleVirtualResource");
605 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
607 // Step 1: Generate the response to discovery request
608 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
610 char *filterOne = NULL;
611 char *filterTwo = NULL;
613 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
614 &filterOne, &filterTwo);
616 if (discoveryResult == OC_STACK_OK)
618 payload = (OCPayload*)OCDiscoveryPayloadCreate();
622 bool foundResourceAtRD = false;
623 for(;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
626 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
628 OCResourceCollectionPayload *repPayload;
630 discoveryResult = checkResourceExistsAtRD(filterOne, filterTwo, &repPayload, &devAddr);
631 if (discoveryResult != OC_STACK_OK)
635 discoveryResult = BuildVirtualCollectionResourceResponse(repPayload,
636 (OCDiscoveryPayload*)payload,
638 foundResourceAtRD = true;
641 if(!foundResourceAtRD && includeThisResourceInResponse(resource, filterOne, filterTwo))
643 discoveryResult = BuildVirtualResourceResponse(resource,
644 (OCDiscoveryPayload*)payload,
648 // Set discoveryResult appropriately if no 'valid' resources are available
649 if (((OCDiscoveryPayload*)payload)->resources == NULL && !foundResourceAtRD)
651 discoveryResult = OC_STACK_NO_RESOURCE;
656 discoveryResult = OC_STACK_NO_MEMORY;
661 OC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
664 else if (virtualUriInRequest == OC_DEVICE_URI)
666 const OicUuid_t* deviceId = OCGetServerInstanceID();
669 discoveryResult = OC_STACK_ERROR;
673 payload = (OCPayload*) OCDevicePayloadCreate((const uint8_t*) &deviceId->id, savedDeviceInfo.deviceName,
674 OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
677 discoveryResult = OC_STACK_NO_MEMORY;
681 discoveryResult = OC_STACK_OK;
685 else if (virtualUriInRequest == OC_PLATFORM_URI)
687 payload = (OCPayload*)OCPlatformPayloadCreate(&savedPlatformInfo);
690 discoveryResult = OC_STACK_NO_MEMORY;
694 discoveryResult = OC_STACK_OK;
697 #ifdef ROUTING_GATEWAY
698 else if (OC_GATEWAY_URI == virtualUriInRequest)
700 // Received request for a gateway
701 OC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
702 discoveryResult = RMHandleGatewayRequest(request, resource);
708 * Step 2: Send the discovery response
710 * Iotivity should respond to discovery requests in below manner:
711 * 1)If query filter matching fails and discovery request is multicast,
712 * it should NOT send any response.
713 * 2)If query filter matching fails and discovery request is unicast,
714 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
715 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
716 * request is multicast, it should NOT send any response.
717 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
718 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
722 if ((virtualUriInRequest == OC_PRESENCE) &&
723 (resource->resourceProperties & OC_ACTIVE))
725 // Presence uses observer notification api to respond via SendPresenceNotification.
726 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
730 #ifdef ROUTING_GATEWAY
731 // Gateway uses the RMHandleGatewayRequest to respond to the request.
732 if (OC_GATEWAY != virtualUriInRequest)
735 if(discoveryResult == OC_STACK_OK)
737 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
739 else if(bMulticast == false && (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
740 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
742 OC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
743 discovery request", discoveryResult, virtualUriInRequest);
744 SendNonPersistantDiscoveryResponse(request, resource, NULL,
745 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
749 // Ignoring the discovery request as per RFC 7252, Section #8.2
750 OC_LOG(INFO, TAG, "Silently ignoring the request since device does not have \
751 any useful data to send");
755 OCPayloadDestroy(payload);
761 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
765 return OC_STACK_INVALID_PARAM;
768 OCStackResult result = OC_STACK_OK;
769 OCEntityHandlerResult ehResult = OC_EH_ERROR;
770 OCEntityHandlerRequest ehRequest = {0};
772 OC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
773 result = FormOCEntityHandlerRequest(&ehRequest,
774 (OCRequestHandle) request,
777 (OCResourceHandle) NULL, request->query,
778 PAYLOAD_TYPE_REPRESENTATION,
780 request->payloadSize,
781 request->numRcvdVendorSpecificHeaderOptions,
782 request->rcvdVendorSpecificHeaderOptions,
783 (OCObserveAction)request->observationOption,
785 VERIFY_SUCCESS(result, OC_STACK_OK);
787 // At this point we know for sure that defaultDeviceHandler exists
788 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
789 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
790 if(ehResult == OC_EH_SLOW)
792 OC_LOG(INFO, TAG, "This is a slow resource");
793 request->slowFlag = 1;
795 else if(ehResult == OC_EH_ERROR)
797 FindAndDeleteServerRequest(request);
799 result = EntityHandlerCodeToOCStackCode(ehResult);
801 OCPayloadDestroy(ehRequest.payload);
806 HandleResourceWithEntityHandler (OCServerRequest *request,
807 OCResource *resource,
808 uint8_t collectionResource)
810 if(!request || ! resource)
812 return OC_STACK_INVALID_PARAM;
815 OCStackResult result = OC_STACK_ERROR;
816 OCEntityHandlerResult ehResult = OC_EH_ERROR;
817 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
818 ResourceObserver *resObs = NULL;
820 OCEntityHandlerRequest ehRequest = {0};
822 OC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
823 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
824 // check the security resource
825 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
827 type = PAYLOAD_TYPE_SECURITY;
831 if (request && strcmp(request->resourceUrl, OC_RSRVD_RD_URI) == 0)
833 type = PAYLOAD_TYPE_RD;
836 result = FormOCEntityHandlerRequest(&ehRequest,
837 (OCRequestHandle)request,
840 (OCResourceHandle)resource,
844 request->payloadSize,
845 request->numRcvdVendorSpecificHeaderOptions,
846 request->rcvdVendorSpecificHeaderOptions,
847 (OCObserveAction)request->observationOption,
849 VERIFY_SUCCESS(result, OC_STACK_OK);
851 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
853 OC_LOG(INFO, TAG, "No observation requested");
854 ehFlag = OC_REQUEST_FLAG;
856 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
858 OC_LOG(INFO, TAG, "Observation registration requested");
860 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
861 request->tokenLength);
865 OC_LOG (INFO, TAG, "Observer with this token already present");
866 OC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
867 OC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
868 OC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
870 // server requests are usually free'd when the response is sent out
871 // for the request in ocserverrequest.c : HandleSingleResponse()
872 // Since we are making an early return and not responding, the server request
873 // needs to be deleted.
874 FindAndDeleteServerRequest (request);
878 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
879 VERIFY_SUCCESS(result, OC_STACK_OK);
881 result = AddObserver ((const char*)(request->resourceUrl),
882 (const char *)(request->query),
883 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
884 resource, request->qos, request->acceptFormat,
887 if(result == OC_STACK_OK)
889 OC_LOG(INFO, TAG, "Added observer successfully");
890 request->observeResult = OC_STACK_OK;
891 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
895 result = OC_STACK_OK;
897 // The error in observeResult for the request will be used when responding to this
898 // request by omitting the observation option/sequence number.
899 request->observeResult = OC_STACK_ERROR;
900 OC_LOG(ERROR, TAG, "Observer Addition failed");
901 ehFlag = OC_REQUEST_FLAG;
905 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
908 OC_LOG(INFO, TAG, "Deregistering observation requested");
910 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
914 // Stack does not contain this observation request
915 // Either token is incorrect or observation list is corrupted
916 result = OC_STACK_ERROR;
919 ehRequest.obsInfo.obsId = resObs->observeId;
920 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
922 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
924 if(result == OC_STACK_OK)
926 OC_LOG(INFO, TAG, "Removed observer successfully");
927 request->observeResult = OC_STACK_OK;
931 result = OC_STACK_OK;
932 request->observeResult = OC_STACK_ERROR;
933 OC_LOG(ERROR, TAG, "Observer Removal failed");
938 result = OC_STACK_ERROR;
942 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
943 if(ehResult == OC_EH_SLOW)
945 OC_LOG(INFO, TAG, "This is a slow resource");
946 request->slowFlag = 1;
948 else if(ehResult == OC_EH_ERROR)
950 FindAndDeleteServerRequest(request);
952 result = EntityHandlerCodeToOCStackCode(ehResult);
954 OCPayloadDestroy(ehRequest.payload);
959 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
960 OCResource *resource)
962 if(!request || !resource)
964 return OC_STACK_INVALID_PARAM;
967 OCStackResult result = OC_STACK_ERROR;
968 OCEntityHandlerRequest ehRequest = {0};
970 result = FormOCEntityHandlerRequest(&ehRequest,
971 (OCRequestHandle)request,
974 (OCResourceHandle)resource,
976 PAYLOAD_TYPE_REPRESENTATION,
978 request->payloadSize,
979 request->numRcvdVendorSpecificHeaderOptions,
980 request->rcvdVendorSpecificHeaderOptions,
981 (OCObserveAction)request->observationOption,
983 if(result == OC_STACK_OK)
985 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
988 OCPayloadDestroy(ehRequest.payload);
993 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
995 OCStackResult ret = OC_STACK_OK;
999 case OC_RESOURCE_VIRTUAL:
1001 ret = HandleVirtualResource (request, resource);
1004 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1006 ret = HandleDefaultDeviceEntityHandler(request);
1009 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1011 OC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1012 return OC_STACK_ERROR;
1014 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1016 ret = HandleResourceWithEntityHandler (request, resource, 0);
1019 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1021 ret = HandleResourceWithEntityHandler (request, resource, 1);
1024 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1026 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1029 case OC_RESOURCE_NOT_SPECIFIED:
1031 ret = OC_STACK_NO_RESOURCE;
1036 OC_LOG(INFO, TAG, "Invalid Resource Determination");
1037 return OC_STACK_ERROR;
1043 void DeletePlatformInfo()
1045 OC_LOG(INFO, TAG, "Deleting platform info.");
1047 OICFree(savedPlatformInfo.platformID);
1048 savedPlatformInfo.platformID = NULL;
1050 OICFree(savedPlatformInfo.manufacturerName);
1051 savedPlatformInfo.manufacturerName = NULL;
1053 OICFree(savedPlatformInfo.manufacturerUrl);
1054 savedPlatformInfo.manufacturerUrl = NULL;
1056 OICFree(savedPlatformInfo.modelNumber);
1057 savedPlatformInfo.modelNumber = NULL;
1059 OICFree(savedPlatformInfo.dateOfManufacture);
1060 savedPlatformInfo.dateOfManufacture = NULL;
1062 OICFree(savedPlatformInfo.platformVersion);
1063 savedPlatformInfo.platformVersion = NULL;
1065 OICFree(savedPlatformInfo.operatingSystemVersion);
1066 savedPlatformInfo.operatingSystemVersion = NULL;
1068 OICFree(savedPlatformInfo.hardwareVersion);
1069 savedPlatformInfo.hardwareVersion = NULL;
1071 OICFree(savedPlatformInfo.firmwareVersion);
1072 savedPlatformInfo.firmwareVersion = NULL;
1074 OICFree(savedPlatformInfo.supportUrl);
1075 savedPlatformInfo.supportUrl = NULL;
1077 OICFree(savedPlatformInfo.systemTime);
1078 savedPlatformInfo.systemTime = NULL;
1081 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
1083 savedPlatformInfo.platformID = OICStrdup(info.platformID);
1084 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
1085 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
1086 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
1087 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
1088 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
1089 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
1090 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
1091 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
1092 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
1093 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
1095 if ((!savedPlatformInfo.platformID && info.platformID)||
1096 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
1097 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
1098 (!savedPlatformInfo.modelNumber && info.modelNumber)||
1099 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
1100 (!savedPlatformInfo.platformVersion && info.platformVersion)||
1101 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
1102 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
1103 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
1104 (!savedPlatformInfo.supportUrl && info.supportUrl)||
1105 (!savedPlatformInfo.systemTime && info.systemTime))
1107 DeletePlatformInfo();
1108 return OC_STACK_INVALID_PARAM;
1115 OCStackResult SavePlatformInfo(OCPlatformInfo info)
1117 DeletePlatformInfo();
1119 OCStackResult res = DeepCopyPlatFormInfo(info);
1121 if (res != OC_STACK_OK)
1123 OC_LOG_V(ERROR, TAG, "Failed to save platform info. errno(%d)", res);
1127 OC_LOG(INFO, TAG, "Platform info saved.");
1133 void DeleteDeviceInfo()
1135 OC_LOG(INFO, TAG, "Deleting device info.");
1137 OICFree(savedDeviceInfo.deviceName);
1138 savedDeviceInfo.deviceName = NULL;
1141 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1143 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1145 if(!savedDeviceInfo.deviceName && info.deviceName)
1148 return OC_STACK_NO_MEMORY;
1154 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1156 OCStackResult res = OC_STACK_OK;
1160 res = DeepCopyDeviceInfo(info);
1162 VERIFY_SUCCESS(res, OC_STACK_OK);
1164 if(OCGetServerInstanceID() == NULL)
1166 OC_LOG(INFO, TAG, "Device ID generation failed");
1167 res = OC_STACK_ERROR;
1171 OC_LOG(INFO, TAG, "Device initialized successfully.");