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
36 #include "ocresource.h"
37 #include "ocresourcehandler.h"
38 #include "ocobserve.h"
39 #include "occollection.h"
40 #include "oic_malloc.h"
41 #include "oic_string.h"
44 #include "ocpayload.h"
45 #include "secureresourcemanager.h"
47 #include "cainterface.h"
48 #include "rdpayload.h"
49 #include "ocpayload.h"
52 #include "rd_server.h"
55 #ifdef ROUTING_GATEWAY
56 #include "routingmanager.h"
60 #define TAG "OIC_RI_RESOURCE"
62 #define VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
63 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
65 #define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OIC_LOG((logLevel), \
66 TAG, #arg " is NULL"); return (retVal); } }
68 #include "platform_features.h"
70 extern OCResource *headResource;
71 static OCPlatformInfo savedPlatformInfo = {0};
72 static OCDeviceInfo savedDeviceInfo = {0};
75 * Prepares a Payload for response.
77 static OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
78 OCDiscoveryPayload* payload,
82 //-----------------------------------------------------------------------------
83 // Default resource entity handler function
84 //-----------------------------------------------------------------------------
85 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
86 OCEntityHandlerRequest * request, void* callbackParam)
88 //TODO ("Implement me!!!!");
89 // TODO: remove silence unused param warnings
93 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
96 /* This method will retrieve the port at which the secure resource is hosted */
97 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
101 if (endpoint->adapter == OC_ADAPTER_IP)
103 if (endpoint->flags & OC_IP_USE_V6)
105 p = caglobals.ip.u6s.port;
107 else if (endpoint->flags & OC_IP_USE_V4)
109 p = caglobals.ip.u4s.port;
118 /* This method will retrieve the tcp port */
119 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port)
123 if (endpoint->adapter == OC_ADAPTER_IP)
125 if (endpoint->flags & OC_IP_USE_V4)
127 p = caglobals.tcp.ipv4.port;
129 else if (endpoint->flags & OC_IP_USE_V6)
131 p = caglobals.tcp.ipv6.port;
141 * Function will extract 0, 1 or 2 filters from query.
142 * More than 2 filters or unsupported filters will result in error.
143 * If both filters are of the same supported type, the 2nd one will be picked.
144 * Resource and device filters in the SAME query are NOT validated
145 * and resources will likely not clear filters.
147 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
152 char *restOfQuery = NULL;
153 int numKeyValuePairsParsed = 0;
158 OIC_LOG_V(INFO, TAG, "Extracting params from %s", query);
160 char *keyValuePair = strtok_r (query, OC_QUERY_SEPARATOR, &restOfQuery);
164 if (numKeyValuePairsParsed >= 2)
166 OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
167 return OC_STACK_INVALID_QUERY;
170 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
174 return OC_STACK_INVALID_QUERY;
176 else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
178 *filterOne = value; // if
180 else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
182 *filterTwo = value; // rt
186 OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
187 return OC_STACK_INVALID_QUERY;
189 ++numKeyValuePairsParsed;
191 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
194 OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
198 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
200 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
202 return OC_WELL_KNOWN_URI;
204 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
206 return OC_DEVICE_URI;
208 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
210 return OC_PLATFORM_URI;
212 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
214 return OC_RESOURCE_TYPES_URI;
216 #ifdef ROUTING_GATEWAY
217 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
219 return OC_GATEWAY_URI;
223 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
227 #endif //WITH_PRESENCE
228 return OC_UNKNOWN_URI;
231 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
232 char **filterOne, char **filterTwo)
234 if(!filterOne || !filterTwo)
236 return OC_STACK_INVALID_PARAM;
243 if (uri == OC_PRESENCE)
245 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
246 OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
251 OCStackResult result = OC_STACK_OK;
255 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
261 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
262 OCRepPayload** payload)
264 OCRepPayload *tempPayload = OCRepPayloadCreate();
268 OCRepPayloadDestroy(tempPayload);
269 return OC_STACK_INVALID_PARAM;
274 return OC_STACK_NO_MEMORY;
277 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
279 OCResourceType *resType = resourcePtr->rsrcType;
282 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
283 resType = resType->next;
286 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
289 OCRepPayloadAddInterface(tempPayload, resInterface->name);
290 resInterface = resInterface->next;
293 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
296 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName,
297 resAttrib->attrValue);
298 resAttrib = resAttrib->next;
303 *payload = tempPayload;
307 OCRepPayloadAppend(*payload, tempPayload);
313 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
314 OCDiscoveryPayload *payload, OCDevAddr *devAddr, bool rdResponse)
316 if (!resourcePtr || !payload)
318 return OC_STACK_INVALID_PARAM;
320 uint16_t securePort = 0;
321 if (resourcePtr->resourceProperties & OC_SECURE)
323 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
331 securePort = devAddr->port;
334 uint16_t tcpPort = 0;
336 if (GetTCPPortInfo(devAddr, &tcpPort) != OC_STACK_OK)
342 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
346 uint8_t IsCollectionResource (OCResource *resource)
353 if(resource->rsrcChildResourcesHead != NULL)
361 OCResource *FindResourceByUri(const char* resourceUri)
368 OCResource * pointer = headResource;
371 if (strcmp(resourceUri, pointer->uri) == 0)
375 pointer = pointer->next;
377 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
382 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
383 ResourceHandling *handling,
384 OCResource **resource)
386 if(!request || !handling || !resource)
388 return OC_STACK_INVALID_PARAM;
391 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
393 // Check if virtual resource
394 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
396 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
397 *handling = OC_RESOURCE_VIRTUAL;
398 *resource = headResource;
401 if (strlen((const char*)(request->resourceUrl)) == 0)
403 // Resource URL not specified
404 *handling = OC_RESOURCE_NOT_SPECIFIED;
405 return OC_STACK_NO_RESOURCE;
409 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
410 *resource = resourcePtr;
413 if(defaultDeviceHandler)
415 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
419 // Resource does not exist
420 // and default device handler does not exist
421 *handling = OC_RESOURCE_NOT_SPECIFIED;
422 return OC_STACK_NO_RESOURCE;
425 if (IsCollectionResource (resourcePtr))
427 // Collection resource
428 if (resourcePtr->entityHandler != defaultResourceEHandler)
430 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
435 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
441 // Resource not a collection
442 if (resourcePtr->entityHandler != defaultResourceEHandler)
444 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
449 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
456 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
458 OCStackResult result;
463 result = OC_STACK_OK;
466 result = OC_STACK_SLOW_RESOURCE;
469 result = OC_STACK_ERROR;
471 case OC_EH_FORBIDDEN:
472 result = OC_STACK_RESOURCE_ERROR;
474 case OC_EH_RESOURCE_CREATED:
475 result = OC_STACK_RESOURCE_CREATED;
477 case OC_EH_RESOURCE_DELETED:
478 result = OC_STACK_RESOURCE_DELETED;
480 case OC_EH_RESOURCE_NOT_FOUND:
481 result = OC_STACK_NO_RESOURCE;
484 result = OC_STACK_ERROR;
490 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
497 // Null or empty is analogous to no filter.
498 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
503 OCResourceType *resourceTypePtr = resource->rsrcType;
505 while (resourceTypePtr)
507 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
511 resourceTypePtr = resourceTypePtr->next;
514 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
518 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
525 // Null or empty is analogous to no filter.
526 if (interfaceFilter == NULL || *interfaceFilter == 0)
531 OCResourceInterface *interfacePtr = resource->rsrcInterface;
535 if ((strcmp (interfacePtr->name, interfaceFilter) == 0) &&
536 (strcmp (OC_RSRVD_INTERFACE_LL, interfaceFilter) == 0 ||
537 strcmp (OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter) == 0))
541 interfacePtr = interfacePtr->next;
544 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
549 * If the filters are null, they will be assumed to NOT be present
550 * and the resource will not be matched against them.
551 * Function will return true if all non null AND non empty filters passed in find a match.
553 static bool includeThisResourceInResponse(OCResource *resource,
554 char *interfaceFilter,
555 char *resourceTypeFilter)
559 OIC_LOG(ERROR, TAG, "Invalid resource");
563 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
566 * At least one valid filter should be available to
567 * include the resource in discovery response
569 if (!(resourceTypeFilter && *resourceTypeFilter))
571 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
572 resource", resource->uri);
576 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
577 !(resource->resourceProperties & OC_DISCOVERABLE))
579 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
583 return resourceMatchesIFFilter(resource, interfaceFilter) &&
584 resourceMatchesRTFilter(resource, resourceTypeFilter);
588 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
589 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
591 OCEntityHandlerResponse response = {0};
593 response.ehResult = ehResult;
594 response.payload = discoveryPayload;
595 response.persistentBufferFlag = 0;
596 response.requestHandle = (OCRequestHandle) request;
597 response.resourceHandle = (OCResourceHandle) resource;
599 return OCDoResponse(&response);
603 static OCStackResult checkResourceExistsAtRD(const char *interfaceType, const char *resourceType,
604 OCResource **payload, OCDevAddr *devAddr)
606 OCResourceCollectionPayload *repPayload;
609 return OC_STACK_ERROR;
611 if (OCRDCheckPublishedResource(interfaceType, resourceType, &repPayload, devAddr) == OC_STACK_OK)
615 return OC_STACK_ERROR;
617 OCResource *ptr = ((OCResource *) OICCalloc(1, sizeof(OCResource)));
620 return OC_STACK_NO_MEMORY;
623 ptr->uri = OICStrdup(repPayload->setLinks->href);
626 return OC_STACK_NO_MEMORY;
628 OCStringLL *rt = repPayload->setLinks->rt;
631 OCResourceType *temp = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
635 return OC_STACK_NO_MEMORY;
638 temp->resourcetypename = OICStrdup(rt->value);
641 ptr->rsrcType = temp;
645 OCResourceType *type = ptr->rsrcType;
655 OCStringLL *itf = repPayload->setLinks->itf;
658 OCResourceInterface *temp = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
663 return OC_STACK_NO_MEMORY;
666 temp->name = OICStrdup(itf->value);
667 if (!ptr->rsrcInterface)
669 ptr->rsrcInterface = temp;
673 OCResourceInterface *type = ptr->rsrcInterface;
683 ptr->resourceProperties = (OCResourceProperty) repPayload->tags->bitmap;
685 OCFreeCollectionResource(repPayload);
691 OIC_LOG_V(ERROR, TAG, "The resource type or interface type doe not exist \
692 on the resource directory");
694 return OC_STACK_ERROR;
698 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
700 if (!request || !resource)
702 return OC_STACK_INVALID_PARAM;
705 OCStackResult discoveryResult = OC_STACK_ERROR;
707 bool bMulticast = false; // Was the discovery request a multicast request?
708 OCPayload* payload = NULL;
710 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
712 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
714 // Step 1: Generate the response to discovery request
715 if (virtualUriInRequest == OC_WELL_KNOWN_URI)
717 if (request->method == OC_REST_PUT || request->method == OC_REST_POST || request->method == OC_REST_DELETE)
719 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d", request->resourceUrl, request->method);
720 return OC_STACK_UNAUTHORIZED_REQ;
723 char *interfaceQuery = NULL;
724 char *resourceTypeQuery = NULL;
726 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
727 &interfaceQuery, &resourceTypeQuery);
728 bool interfaceQueryAllocated = false;
729 if (!interfaceQuery && !resourceTypeQuery)
731 interfaceQueryAllocated = true;
732 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
735 if (discoveryResult == OC_STACK_OK)
737 payload = (OCPayload *)OCDiscoveryPayloadCreate();
741 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
742 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
743 VERIFY_NON_NULL(discPayload->sid, ERROR, OC_STACK_NO_MEMORY);
745 const char* uid = OCGetServerInstanceIDString();
748 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
751 if (!resourceTypeQuery && interfaceQuery && (0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
753 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
756 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
758 if (resourceTypeQuery && resourceMatchesRTFilter(resource, resourceTypeQuery))
763 if (resource->resourceProperties & OC_DISCOVERABLE)
770 discoveryResult = BuildVirtualResourceResponse(resource,
771 discPayload, &request->devAddr, false);
777 if ((interfaceQuery && (0 != strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL))) ||
780 discPayload->uri = OICStrdup(OC_RSRVD_WELL_KNOWN_URI);
781 VERIFY_NON_NULL(discPayload->uri, ERROR, OC_STACK_NO_MEMORY);
782 if (savedDeviceInfo.deviceName)
784 discPayload->name = OICStrdup(savedDeviceInfo.deviceName);
785 VERIFY_NON_NULL(discPayload->name, ERROR, OC_STACK_NO_MEMORY);
787 discPayload->type = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
788 VERIFY_NON_NULL(discPayload->type, ERROR, OC_STACK_NO_MEMORY);
789 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
790 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
791 VERIFY_NON_NULL(discPayload->iface, ERROR, OC_STACK_NO_MEMORY);
793 bool foundResourceAtRD = false;
794 for (;resource && discoveryResult == OC_STACK_OK; resource = resource->next)
797 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
799 OCResource *resource1 = NULL;
801 discoveryResult = checkResourceExistsAtRD(interfaceQuery,
802 resourceTypeQuery, &resource1, &devAddr);
803 if (discoveryResult != OC_STACK_OK)
807 discoveryResult = BuildVirtualResourceResponse(resource1,
808 discPayload, &devAddr, true);
811 discPayload->baseURI = OICStrdup(devAddr.addr);
813 OICFree(resource1->uri);
814 for (OCResourceType *rsrcRt = resource1->rsrcType, *rsrcRtNext = NULL; rsrcRt; )
816 rsrcRtNext = rsrcRt->next;
817 OICFree(rsrcRt->resourcetypename);
822 for (OCResourceInterface *rsrcPtr = resource1->rsrcInterface, *rsrcNext = NULL; rsrcPtr; )
824 rsrcNext = rsrcPtr->next;
825 OICFree(rsrcPtr->name);
829 foundResourceAtRD = true;
832 if (!foundResourceAtRD && includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
834 discoveryResult = BuildVirtualResourceResponse(resource,
835 discPayload, &request->devAddr, false);
838 // Set discoveryResult appropriately if no 'valid' resources are available
839 if (discPayload->resources == NULL && !foundResourceAtRD)
841 discoveryResult = OC_STACK_NO_RESOURCE;
847 discoveryResult = OC_STACK_NO_MEMORY;
852 OIC_LOG_V(ERROR, TAG, "Error (%d) parsing query.", discoveryResult);
854 if (interfaceQueryAllocated)
856 OICFree(interfaceQuery);
859 else if (virtualUriInRequest == OC_DEVICE_URI)
861 if (request->method == OC_REST_PUT || request->method == OC_REST_POST || request->method == OC_REST_DELETE)
863 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d", request->resourceUrl, request->method);
864 return OC_STACK_UNAUTHORIZED_REQ;
867 const char* deviceId = OCGetServerInstanceIDString();
870 discoveryResult = OC_STACK_ERROR;
874 payload = (OCPayload*) OCDevicePayloadCreate(deviceId, savedDeviceInfo.deviceName,
875 savedDeviceInfo.types, savedDeviceInfo.specVersion, savedDeviceInfo.dataModleVersion);
878 discoveryResult = OC_STACK_NO_MEMORY;
882 discoveryResult = OC_STACK_OK;
886 else if (virtualUriInRequest == OC_PLATFORM_URI)
888 if (request->method == OC_REST_PUT || request->method == OC_REST_POST || request->method == OC_REST_DELETE)
890 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d", request->resourceUrl, request->method);
891 return OC_STACK_UNAUTHORIZED_REQ;
894 payload = (OCPayload*)OCPlatformPayloadCreate(&savedPlatformInfo);
897 discoveryResult = OC_STACK_NO_MEMORY;
901 discoveryResult = OC_STACK_OK;
904 #ifdef ROUTING_GATEWAY
905 else if (OC_GATEWAY_URI == virtualUriInRequest)
907 // Received request for a gateway
908 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
909 discoveryResult = RMHandleGatewayRequest(request, resource);
915 * Step 2: Send the discovery response
917 * Iotivity should respond to discovery requests in below manner:
918 * 1)If query filter matching fails and discovery request is multicast,
919 * it should NOT send any response.
920 * 2)If query filter matching fails and discovery request is unicast,
921 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
922 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
923 * request is multicast, it should NOT send any response.
924 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
925 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
929 if ((virtualUriInRequest == OC_PRESENCE) &&
930 (resource->resourceProperties & OC_ACTIVE))
932 // Presence uses observer notification api to respond via SendPresenceNotification.
933 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
937 #ifdef ROUTING_GATEWAY
938 // Gateway uses the RMHandleGatewayRequest to respond to the request.
939 if (OC_GATEWAY_URI != virtualUriInRequest)
942 if(discoveryResult == OC_STACK_OK)
944 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
946 else if(bMulticast == false && (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
947 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
949 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) \
950 discovery request", discoveryResult, virtualUriInRequest);
951 SendNonPersistantDiscoveryResponse(request, resource, NULL,
952 (discoveryResult == OC_STACK_NO_RESOURCE) ? OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
956 // Ignoring the discovery request as per RFC 7252, Section #8.2
957 OIC_LOG(INFO, TAG, "Silently ignoring the request since device does not have \
958 any useful data to send");
962 OCPayloadDestroy(payload);
968 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
972 return OC_STACK_INVALID_PARAM;
975 OCStackResult result = OC_STACK_OK;
976 OCEntityHandlerResult ehResult = OC_EH_ERROR;
977 OCEntityHandlerRequest ehRequest = {0};
979 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
980 result = FormOCEntityHandlerRequest(&ehRequest,
981 (OCRequestHandle) request,
984 (OCResourceHandle) NULL, request->query,
985 PAYLOAD_TYPE_REPRESENTATION,
987 request->payloadSize,
988 request->numRcvdVendorSpecificHeaderOptions,
989 request->rcvdVendorSpecificHeaderOptions,
990 (OCObserveAction)request->observationOption,
992 VERIFY_SUCCESS(result, OC_STACK_OK);
994 // At this point we know for sure that defaultDeviceHandler exists
995 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
996 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
997 if(ehResult == OC_EH_SLOW)
999 OIC_LOG(INFO, TAG, "This is a slow resource");
1000 request->slowFlag = 1;
1002 else if(ehResult == OC_EH_ERROR)
1004 FindAndDeleteServerRequest(request);
1006 result = EntityHandlerCodeToOCStackCode(ehResult);
1008 OCPayloadDestroy(ehRequest.payload);
1012 static OCStackResult
1013 HandleResourceWithEntityHandler (OCServerRequest *request,
1014 OCResource *resource,
1015 uint8_t collectionResource)
1017 if(!request || ! resource)
1019 return OC_STACK_INVALID_PARAM;
1022 OCStackResult result = OC_STACK_ERROR;
1023 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1024 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1025 ResourceObserver *resObs = NULL;
1027 OCEntityHandlerRequest ehRequest = {0};
1029 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1030 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1031 // check the security resource
1032 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1034 type = PAYLOAD_TYPE_SECURITY;
1038 if (request && strcmp(request->resourceUrl, OC_RSRVD_RD_URI) == 0)
1040 type = PAYLOAD_TYPE_RD;
1043 result = FormOCEntityHandlerRequest(&ehRequest,
1044 (OCRequestHandle)request,
1047 (OCResourceHandle)resource,
1051 request->payloadSize,
1052 request->numRcvdVendorSpecificHeaderOptions,
1053 request->rcvdVendorSpecificHeaderOptions,
1054 (OCObserveAction)request->observationOption,
1056 VERIFY_SUCCESS(result, OC_STACK_OK);
1058 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1060 OIC_LOG(INFO, TAG, "No observation requested");
1061 ehFlag = OC_REQUEST_FLAG;
1063 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER && !collectionResource)
1065 OIC_LOG(INFO, TAG, "Observation registration requested");
1067 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1068 request->tokenLength);
1072 OIC_LOG (INFO, TAG, "Observer with this token already present");
1073 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1074 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1075 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1077 // server requests are usually free'd when the response is sent out
1078 // for the request in ocserverrequest.c : HandleSingleResponse()
1079 // Since we are making an early return and not responding, the server request
1080 // needs to be deleted.
1081 FindAndDeleteServerRequest (request);
1085 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1086 VERIFY_SUCCESS(result, OC_STACK_OK);
1088 result = AddObserver ((const char*)(request->resourceUrl),
1089 (const char *)(request->query),
1090 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1091 resource, request->qos, request->acceptFormat,
1094 if(result == OC_STACK_OK)
1096 OIC_LOG(INFO, TAG, "Added observer successfully");
1097 request->observeResult = OC_STACK_OK;
1098 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1102 // The error in observeResult for the request will be used when responding to this
1103 // request by omitting the observation option/sequence number.
1104 request->observeResult = OC_STACK_ERROR;
1105 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1106 ehFlag = OC_REQUEST_FLAG;
1107 FindAndDeleteServerRequest(request);
1112 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER &&
1113 !collectionResource)
1115 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1117 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1121 // Stack does not contain this observation request
1122 // Either token is incorrect or observation list is corrupted
1123 result = OC_STACK_ERROR;
1126 ehRequest.obsInfo.obsId = resObs->observeId;
1127 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1129 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1131 if(result == OC_STACK_OK)
1133 OIC_LOG(INFO, TAG, "Removed observer successfully");
1134 request->observeResult = OC_STACK_OK;
1138 request->observeResult = OC_STACK_ERROR;
1139 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1140 FindAndDeleteServerRequest(request);
1146 result = OC_STACK_ERROR;
1150 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1151 if(ehResult == OC_EH_SLOW)
1153 OIC_LOG(INFO, TAG, "This is a slow resource");
1154 request->slowFlag = 1;
1156 else if(ehResult == OC_EH_ERROR)
1158 FindAndDeleteServerRequest(request);
1160 result = EntityHandlerCodeToOCStackCode(ehResult);
1162 OCPayloadDestroy(ehRequest.payload);
1166 static OCStackResult
1167 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
1168 OCResource *resource)
1170 if(!request || !resource)
1172 return OC_STACK_INVALID_PARAM;
1175 OCStackResult result = OC_STACK_ERROR;
1176 OCEntityHandlerRequest ehRequest = {0};
1178 result = FormOCEntityHandlerRequest(&ehRequest,
1179 (OCRequestHandle)request,
1182 (OCResourceHandle)resource,
1184 PAYLOAD_TYPE_REPRESENTATION,
1186 request->payloadSize,
1187 request->numRcvdVendorSpecificHeaderOptions,
1188 request->rcvdVendorSpecificHeaderOptions,
1189 (OCObserveAction)request->observationOption,
1190 (OCObservationId)0);
1191 if(result == OC_STACK_OK)
1193 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1196 OCPayloadDestroy(ehRequest.payload);
1201 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1203 OCStackResult ret = OC_STACK_OK;
1205 switch (resHandling)
1207 case OC_RESOURCE_VIRTUAL:
1209 ret = HandleVirtualResource (request, resource);
1212 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1214 ret = HandleDefaultDeviceEntityHandler(request);
1217 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1219 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1220 return OC_STACK_ERROR;
1222 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1224 ret = HandleResourceWithEntityHandler (request, resource, 0);
1227 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1229 ret = HandleResourceWithEntityHandler (request, resource, 1);
1232 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1234 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1237 case OC_RESOURCE_NOT_SPECIFIED:
1239 ret = OC_STACK_NO_RESOURCE;
1244 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1245 return OC_STACK_ERROR;
1251 void DeletePlatformInfo()
1253 OIC_LOG(INFO, TAG, "Deleting platform info.");
1255 OICFree(savedPlatformInfo.platformID);
1256 savedPlatformInfo.platformID = NULL;
1258 OICFree(savedPlatformInfo.manufacturerName);
1259 savedPlatformInfo.manufacturerName = NULL;
1261 OICFree(savedPlatformInfo.manufacturerUrl);
1262 savedPlatformInfo.manufacturerUrl = NULL;
1264 OICFree(savedPlatformInfo.modelNumber);
1265 savedPlatformInfo.modelNumber = NULL;
1267 OICFree(savedPlatformInfo.dateOfManufacture);
1268 savedPlatformInfo.dateOfManufacture = NULL;
1270 OICFree(savedPlatformInfo.platformVersion);
1271 savedPlatformInfo.platformVersion = NULL;
1273 OICFree(savedPlatformInfo.operatingSystemVersion);
1274 savedPlatformInfo.operatingSystemVersion = NULL;
1276 OICFree(savedPlatformInfo.hardwareVersion);
1277 savedPlatformInfo.hardwareVersion = NULL;
1279 OICFree(savedPlatformInfo.firmwareVersion);
1280 savedPlatformInfo.firmwareVersion = NULL;
1282 OICFree(savedPlatformInfo.supportUrl);
1283 savedPlatformInfo.supportUrl = NULL;
1285 OICFree(savedPlatformInfo.systemTime);
1286 savedPlatformInfo.systemTime = NULL;
1289 static OCStackResult DeepCopyPlatFormInfo(OCPlatformInfo info)
1291 savedPlatformInfo.platformID = OICStrdup(info.platformID);
1292 savedPlatformInfo.manufacturerName = OICStrdup(info.manufacturerName);
1293 savedPlatformInfo.manufacturerUrl = OICStrdup(info.manufacturerUrl);
1294 savedPlatformInfo.modelNumber = OICStrdup(info.modelNumber);
1295 savedPlatformInfo.dateOfManufacture = OICStrdup(info.dateOfManufacture);
1296 savedPlatformInfo.platformVersion = OICStrdup(info.platformVersion);
1297 savedPlatformInfo.operatingSystemVersion = OICStrdup(info.operatingSystemVersion);
1298 savedPlatformInfo.hardwareVersion = OICStrdup(info.hardwareVersion);
1299 savedPlatformInfo.firmwareVersion = OICStrdup(info.firmwareVersion);
1300 savedPlatformInfo.supportUrl = OICStrdup(info.supportUrl);
1301 savedPlatformInfo.systemTime = OICStrdup(info.systemTime);
1303 if ((!savedPlatformInfo.platformID && info.platformID)||
1304 (!savedPlatformInfo.manufacturerName && info.manufacturerName)||
1305 (!savedPlatformInfo.manufacturerUrl && info.manufacturerUrl)||
1306 (!savedPlatformInfo.modelNumber && info.modelNumber)||
1307 (!savedPlatformInfo.dateOfManufacture && info.dateOfManufacture)||
1308 (!savedPlatformInfo.platformVersion && info.platformVersion)||
1309 (!savedPlatformInfo.operatingSystemVersion && info.operatingSystemVersion)||
1310 (!savedPlatformInfo.hardwareVersion && info.hardwareVersion)||
1311 (!savedPlatformInfo.firmwareVersion && info.firmwareVersion)||
1312 (!savedPlatformInfo.supportUrl && info.supportUrl)||
1313 (!savedPlatformInfo.systemTime && info.systemTime))
1315 DeletePlatformInfo();
1316 return OC_STACK_INVALID_PARAM;
1323 OCStackResult SavePlatformInfo(OCPlatformInfo info)
1325 DeletePlatformInfo();
1327 OCStackResult res = DeepCopyPlatFormInfo(info);
1329 if (res != OC_STACK_OK)
1331 OIC_LOG_V(ERROR, TAG, "Failed to save platform info. errno(%d)", res);
1335 OIC_LOG(INFO, TAG, "Platform info saved.");
1341 void DeleteDeviceInfo()
1343 OIC_LOG(INFO, TAG, "Deleting device info.");
1345 OICFree(savedDeviceInfo.deviceName);
1346 OCFreeOCStringLL(savedDeviceInfo.types);
1347 OICFree(savedDeviceInfo.specVersion);
1348 OICFree(savedDeviceInfo.dataModleVersion);
1349 savedDeviceInfo.deviceName = NULL;
1350 savedDeviceInfo.specVersion = NULL;
1351 savedDeviceInfo.dataModleVersion = NULL;
1354 static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
1356 savedDeviceInfo.deviceName = OICStrdup(info.deviceName);
1358 if(!savedDeviceInfo.deviceName && info.deviceName)
1361 return OC_STACK_NO_MEMORY;
1366 savedDeviceInfo.types = CloneOCStringLL(info.types);
1367 if(!savedDeviceInfo.types && info.types)
1370 return OC_STACK_NO_MEMORY;
1374 if (info.specVersion)
1376 savedDeviceInfo.specVersion = OICStrdup(info.specVersion);
1377 if(!savedDeviceInfo.specVersion && info.specVersion)
1380 return OC_STACK_NO_MEMORY;
1385 savedDeviceInfo.specVersion = OICStrdup(OC_SPEC_VERSION);
1386 if(!savedDeviceInfo.specVersion && OC_SPEC_VERSION)
1389 return OC_STACK_NO_MEMORY;
1393 if (info.dataModleVersion)
1395 savedDeviceInfo.dataModleVersion = OICStrdup(info.dataModleVersion);
1396 if(!savedDeviceInfo.dataModleVersion && info.dataModleVersion)
1399 return OC_STACK_NO_MEMORY;
1404 savedDeviceInfo.dataModleVersion = OICStrdup(OC_DATA_MODEL_VERSION);
1405 if(!savedDeviceInfo.dataModleVersion && OC_DATA_MODEL_VERSION)
1408 return OC_STACK_NO_MEMORY;
1415 OCStackResult SaveDeviceInfo(OCDeviceInfo info)
1417 OCStackResult res = OC_STACK_OK;
1421 res = DeepCopyDeviceInfo(info);
1423 VERIFY_SUCCESS(res, OC_STACK_OK);
1425 if (OCGetServerInstanceIDString() == NULL)
1427 OIC_LOG(INFO, TAG, "Device ID generation failed");
1428 res = OC_STACK_ERROR;
1432 OIC_LOG(INFO, TAG, "Device initialized successfully.");