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
32 #include "iotivity_config.h"
40 #include "ocresource.h"
41 #include "ocresourcehandler.h"
42 #include "ocobserve.h"
43 #include "occollection.h"
44 #include "oic_malloc.h"
45 #include "oic_string.h"
48 #include "ocpayload.h"
49 #include "secureresourcemanager.h"
51 #include "cainterface.h"
52 #include "ocpayload.h"
53 #include "oickeepalive.h"
54 #include "platform_features.h"
55 #include "payload_logging.h"
56 #ifdef ROUTING_GATEWAY
57 #include "routingmanager.h"
61 #define TAG "OIC_RI_RESOURCE"
63 #define VERIFY_SUCCESS(op) { if (op != (OC_STACK_OK)) \
64 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
66 extern OCResource *headResource;
69 * Prepares a Payload for response.
71 static OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
72 OCDiscoveryPayload* payload,
75 //-----------------------------------------------------------------------------
76 // Default resource entity handler function
77 //-----------------------------------------------------------------------------
78 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
79 OCEntityHandlerRequest * request, void* callbackParam)
81 //TODO ("Implement me!!!!");
82 // TODO: remove silence unused param warnings
86 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
89 /* This method will retrieve the port at which the secure resource is hosted */
90 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
94 if (endpoint->adapter == OC_ADAPTER_IP)
96 if (endpoint->flags & OC_IP_USE_V6)
98 p = caglobals.ip.u6s.port;
100 else if (endpoint->flags & OC_IP_USE_V4)
102 p = caglobals.ip.u4s.port;
111 /* This method will retrieve the tcp port */
112 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port, bool secured)
116 if (endpoint->adapter == OC_ADAPTER_IP)
118 if (endpoint->flags & OC_IP_USE_V4)
120 p = secured ? caglobals.tcp.ipv4s.port : caglobals.tcp.ipv4.port;
122 else if (endpoint->flags & OC_IP_USE_V6)
124 p = secured ? caglobals.tcp.ipv6s.port : caglobals.tcp.ipv6.port;
134 * Function will extract 0, 1 or 2 filters from query.
135 * More than 2 filters or unsupported filters will result in error.
136 * If both filters are of the same supported type, the 2nd one will be picked.
137 * Resource and device filters in the SAME query are NOT validated
138 * and resources will likely not clear filters.
140 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
144 char *queryDup = NULL;
145 char *restOfQuery = NULL;
146 char *keyValuePair = NULL;
147 int numKeyValuePairsParsed = 0;
152 queryDup = OICStrdup(query);
153 if (NULL == queryDup)
155 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
156 return OC_STACK_NO_MEMORY;
159 OIC_LOG_V(INFO, TAG, "Extracting params from %s", queryDup);
161 OCStackResult eCode = OC_STACK_INVALID_QUERY;
162 if (strnlen(queryDup, MAX_QUERY_LENGTH) >= MAX_QUERY_LENGTH)
164 OIC_LOG(ERROR, TAG, "Query exceeds maximum length.");
168 keyValuePair = strtok_r (queryDup, OC_QUERY_SEPARATOR, &restOfQuery);
172 if (numKeyValuePairsParsed >= 2)
174 OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
178 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
184 else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
186 *filterOne = value; // if
188 else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
190 *filterTwo = value; // rt
194 OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
197 ++numKeyValuePairsParsed;
199 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
204 *filterOne = OICStrdup(*filterOne);
205 if (NULL == *filterOne)
207 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
208 eCode = OC_STACK_NO_MEMORY;
215 *filterTwo = OICStrdup(*filterTwo);
216 if (NULL == *filterTwo)
218 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
220 eCode = OC_STACK_NO_MEMORY;
226 OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
236 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
238 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
240 return OC_WELL_KNOWN_URI;
242 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
244 return OC_DEVICE_URI;
246 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
248 return OC_PLATFORM_URI;
250 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
252 return OC_RESOURCE_TYPES_URI;
254 #ifdef ROUTING_GATEWAY
255 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
257 return OC_GATEWAY_URI;
261 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
265 #endif //WITH_PRESENCE
268 else if (0 == strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_MQ_URI))
270 return OC_MQ_BROKER_URI;
275 else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
277 return OC_KEEPALIVE_RESOURCE_URI;
281 return OC_UNKNOWN_URI;
284 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
285 char **filterOne, char **filterTwo)
287 if(!filterOne || !filterTwo)
289 return OC_STACK_INVALID_PARAM;
296 if (uri == OC_PRESENCE)
298 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
299 OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
304 OCStackResult result = OC_STACK_OK;
308 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
314 bool appendOCStringLL(OCRepPayload *device, OCStringLL *dmv)
317 for (OCStringLL *ll = dmv; ll; ll = ll->next, size++);
318 size_t dim[MAX_REP_ARRAY_DEPTH] = {size, 0, 0};
319 char **dt = (char **)OICMalloc(sizeof(char *) * size);
321 VERIFY_PARAM_NON_NULL(TAG, dt, "Data Model Version allocation failed.");
322 for (OCStringLL *ll = dmv; ll; ll = ll->next, i++)
324 dt[i] = OICStrdup(ll->value);
325 VERIFY_PARAM_NON_NULL(TAG, dt[i], "Data Model Version adding failed.");
327 if (!OCRepPayloadSetStringArrayAsOwner(device, OC_RSRVD_DATA_MODEL_VERSION, dt, dim))
334 for (int i = 0; i < size; i++)
342 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
343 OCRepPayload** payload, OCDevAddr *devAddr, bool addDeviceId)
345 OCRepPayload *tempPayload = OCRepPayloadCreate();
349 OCRepPayloadDestroy(tempPayload);
350 return OC_STACK_INVALID_PARAM;
355 return OC_STACK_NO_MEMORY;
358 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
361 const char *deviceId = OCGetServerInstanceIDString();
364 OIC_LOG(ERROR, TAG, "Failed retrieving device id.");
365 return OC_STACK_ERROR;
367 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_DEVICE_ID, deviceId);
369 OCResourceType *resType = resourcePtr->rsrcType;
372 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
373 resType = resType->next;
376 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
379 OCRepPayloadAddInterface(tempPayload, resInterface->name);
380 resInterface = resInterface->next;
383 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
386 if (resAttrib->attrName && resAttrib->attrValue)
388 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
390 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
393 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
399 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
402 resAttrib = resAttrib->next;
407 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
408 OCRepPayload *policy = OCRepPayloadCreate();
411 OCPayloadDestroy((OCPayload *)tempPayload);
412 return OC_STACK_NO_MEMORY;
414 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
417 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
418 uint16_t securePort = 0;
419 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
423 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
425 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
430 *payload = tempPayload;
434 OCRepPayloadAppend(*payload, tempPayload);
440 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
441 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
443 if (!resourcePtr || !payload)
445 return OC_STACK_INVALID_PARAM;
447 uint16_t securePort = 0;
448 if (resourcePtr->resourceProperties & OC_SECURE)
450 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
457 uint16_t tcpPort = 0;
458 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
460 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
462 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
468 uint8_t IsCollectionResource (OCResource *resource)
475 if(resource->rsrcChildResourcesHead != NULL)
483 OCResource *FindResourceByUri(const char* resourceUri)
490 OCResource * pointer = headResource;
493 if (strcmp(resourceUri, pointer->uri) == 0)
497 pointer = pointer->next;
499 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
504 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
505 ResourceHandling *handling,
506 OCResource **resource)
508 if(!request || !handling || !resource)
510 return OC_STACK_INVALID_PARAM;
513 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
515 // Check if virtual resource
516 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
518 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
519 *handling = OC_RESOURCE_VIRTUAL;
520 *resource = headResource;
523 if (strlen((const char*)(request->resourceUrl)) == 0)
525 // Resource URL not specified
526 *handling = OC_RESOURCE_NOT_SPECIFIED;
527 return OC_STACK_NO_RESOURCE;
531 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
532 *resource = resourcePtr;
535 if(defaultDeviceHandler)
537 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
541 // Resource does not exist
542 // and default device handler does not exist
543 *handling = OC_RESOURCE_NOT_SPECIFIED;
544 return OC_STACK_NO_RESOURCE;
547 if (IsCollectionResource (resourcePtr))
549 // Collection resource
550 if (resourcePtr->entityHandler != defaultResourceEHandler)
552 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
557 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
563 // Resource not a collection
564 if (resourcePtr->entityHandler != defaultResourceEHandler)
566 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
571 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
578 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
580 OCStackResult result;
587 result = OC_STACK_OK;
590 result = OC_STACK_SLOW_RESOURCE;
593 result = OC_STACK_ERROR;
595 case OC_EH_FORBIDDEN:
596 result = OC_STACK_FORBIDDEN_REQ;
598 case OC_EH_INTERNAL_SERVER_ERROR:
599 result = OC_STACK_INTERNAL_SERVER_ERROR;
601 case OC_EH_RESOURCE_CREATED:
602 result = OC_STACK_RESOURCE_CREATED;
604 case OC_EH_RESOURCE_DELETED:
605 result = OC_STACK_RESOURCE_DELETED;
608 result = OC_STACK_RESOURCE_CHANGED;
610 case OC_EH_RESOURCE_NOT_FOUND:
611 result = OC_STACK_NO_RESOURCE;
614 result = OC_STACK_ERROR;
620 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
627 // Null or empty is analogous to no filter.
628 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
633 OCResourceType *resourceTypePtr = resource->rsrcType;
635 while (resourceTypePtr)
637 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
641 resourceTypePtr = resourceTypePtr->next;
644 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
648 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
655 // Null or empty is analogous to no filter.
656 if (interfaceFilter == NULL || *interfaceFilter == 0)
661 OCResourceInterface *interfacePtr = resource->rsrcInterface;
665 if (strcmp (interfacePtr->name, interfaceFilter) == 0 ||
666 strcmp (OC_RSRVD_INTERFACE_LL, interfaceFilter) == 0 ||
667 strcmp (OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter) == 0)
671 interfacePtr = interfacePtr->next;
674 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
679 * If the filters are null, they will be assumed to NOT be present
680 * and the resource will not be matched against them.
681 * Function will return true if all non null AND non empty filters passed in find a match.
683 static bool includeThisResourceInResponse(OCResource *resource,
684 char *interfaceFilter,
685 char *resourceTypeFilter)
689 OIC_LOG(ERROR, TAG, "Invalid resource");
693 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
696 * At least one valid filter should be available to
697 * include the resource in discovery response
699 if (!(resourceTypeFilter && *resourceTypeFilter))
701 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
702 resource", resource->uri);
706 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
707 !(resource->resourceProperties & OC_DISCOVERABLE))
709 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
713 return resourceMatchesIFFilter(resource, interfaceFilter) &&
714 resourceMatchesRTFilter(resource, resourceTypeFilter);
718 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
719 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
721 OCEntityHandlerResponse response = {0};
723 response.ehResult = ehResult;
724 response.payload = discoveryPayload;
725 response.persistentBufferFlag = 0;
726 response.requestHandle = (OCRequestHandle) request->requestId;
727 response.resourceHandle = (OCResourceHandle) resource;
729 return OCDoResponse(&response);
733 * Find resource at the resource directory server. This resource is not local resource but a
736 * @param resource The resource to check the matching resource URI.
737 * @param interfaceQuery The interface query parameter.
738 * @param resourceTypeQuery The resourceType query parameter.
739 * @param discPayload The payload that will be added with the resource information if found at RD.
741 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
742 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
744 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
745 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
747 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
749 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
755 return OC_STACK_NO_RESOURCE;
760 * Creates a discovery payload and add device id information. This information is included in all
763 * @param payload payload that will have memory alllocated and device id information added.
765 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
766 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
768 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
772 OIC_LOG_V(DEBUG, TAG, "Payload is already allocated");
776 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
777 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
780 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
781 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
782 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
784 const char* uid = OCGetServerInstanceIDString();
787 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
793 OCPayloadDestroy(*payload);
794 return OC_STACK_NO_MEMORY;
798 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
800 * @param discPayload payload that will have the baseline information included.
802 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
803 * allocating the memory for the baseline information.
805 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
807 discPayload->uri = OICStrdup(OC_RSRVD_WELL_KNOWN_URI);
808 VERIFY_PARAM_NON_NULL(TAG, discPayload->uri, "Failed adding href to discovery payload.");
810 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, "deviceName", (void **)&discPayload->name);
812 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
813 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
814 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
815 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
817 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
818 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
819 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
824 return OC_STACK_NO_MEMORY;
827 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
829 if (!request || !resource)
831 return OC_STACK_INVALID_PARAM;
834 OCPayload* payload = NULL;
835 char *interfaceQuery = NULL;
836 char *resourceTypeQuery = NULL;
837 char *dataModelVersions = NULL;
839 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
841 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
844 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
846 // Received request for a keepalive
847 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
848 return HandleKeepAliveRequest(request, resource);
852 OCStackResult discoveryResult = OC_STACK_ERROR;
853 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
854 request->method == OC_REST_DELETE)
856 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
857 request->resourceUrl, request->method);
858 return OC_STACK_UNAUTHORIZED_REQ;
861 // Step 1: Generate the response to discovery request
862 if (virtualUriInRequest == OC_WELL_KNOWN_URI
864 || virtualUriInRequest == OC_MQ_BROKER_URI
868 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
869 &interfaceQuery, &resourceTypeQuery);
870 VERIFY_SUCCESS(discoveryResult);
871 if (!interfaceQuery && !resourceTypeQuery)
873 // If no query is sent, default interface is used i.e. oic.if.ll.
874 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
877 bool baselineQuery = false;
878 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
880 baselineQuery = true;
883 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
884 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
885 VERIFY_SUCCESS(discoveryResult);
887 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
890 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
891 VERIFY_SUCCESS(discoveryResult);
893 OCResourceProperty prop = OC_DISCOVERABLE;
895 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
897 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
899 discoveryResult = OC_STACK_NO_RESOURCE;
901 discoveryResult = findResourceAtRD(resource, interfaceQuery, resourceTypeQuery,
904 if (OC_STACK_NO_RESOURCE == discoveryResult)
906 // This case will handle when no resource type and it is oic.if.ll.
907 if (!resourceTypeQuery && !baselineQuery && (resource->resourceProperties & prop))
909 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
911 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
913 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
917 discoveryResult = OC_STACK_OK;
921 if (discPayload->resources == NULL)
923 discoveryResult = OC_STACK_NO_RESOURCE;
926 else if (virtualUriInRequest == OC_DEVICE_URI)
928 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
929 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
930 discoveryResult = BuildResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, NULL, true);
933 else if (virtualUriInRequest == OC_PLATFORM_URI)
935 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
936 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
937 discoveryResult = BuildResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, NULL, false);
939 #ifdef ROUTING_GATEWAY
940 else if (OC_GATEWAY_URI == virtualUriInRequest)
942 // Received request for a gateway
943 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
944 discoveryResult = RMHandleGatewayRequest(request, resource);
949 * Step 2: Send the discovery response
951 * Iotivity should respond to discovery requests in below manner:
952 * 1)If query filter matching fails and discovery request is multicast,
953 * it should NOT send any response.
954 * 2)If query filter matching fails and discovery request is unicast,
955 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
956 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
957 * request is multicast, it should NOT send any response.
958 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
959 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
963 if ((virtualUriInRequest == OC_PRESENCE) &&
964 (resource->resourceProperties & OC_ACTIVE))
966 // Need to send ACK when the request is CON.
967 if (request->qos == OC_HIGH_QOS)
969 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
970 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
971 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
972 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
974 FindAndDeleteServerRequest(request);
976 // Presence uses observer notification api to respond via SendPresenceNotification.
977 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
982 // Gateway uses the RMHandleGatewayRequest to respond to the request.
983 if (OC_GATEWAY_URI != virtualUriInRequest)
986 OIC_LOG_PAYLOAD(DEBUG, payload);
987 if(discoveryResult == OC_STACK_OK)
989 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
991 else if(((request->devAddr.flags & OC_MULTICAST) == false) &&
992 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
993 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
995 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
996 discoveryResult, virtualUriInRequest);
997 SendNonPersistantDiscoveryResponse(request, resource, NULL,
998 (discoveryResult == OC_STACK_NO_RESOURCE) ?
999 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1003 // Ignoring the discovery request as per RFC 7252, Section #8.2
1004 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1005 // the request should be removed.
1006 // since it never remove and causes a big memory waste.
1007 FindAndDeleteServerRequest(request);
1014 OICFree(interfaceQuery);
1017 if (resourceTypeQuery)
1019 OICFree(resourceTypeQuery);
1021 OCPayloadDestroy(payload);
1022 if (dataModelVersions)
1024 OICFree(dataModelVersions);
1026 return discoveryResult;
1029 static OCStackResult
1030 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
1034 return OC_STACK_INVALID_PARAM;
1037 OCStackResult result = OC_STACK_OK;
1038 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1039 OCEntityHandlerRequest ehRequest = {0};
1041 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1042 result = FormOCEntityHandlerRequest(&ehRequest,
1043 (OCRequestHandle) request->requestId,
1046 (OCResourceHandle) NULL, request->query,
1047 PAYLOAD_TYPE_REPRESENTATION,
1049 request->payloadSize,
1050 request->numRcvdVendorSpecificHeaderOptions,
1051 request->rcvdVendorSpecificHeaderOptions,
1052 (OCObserveAction)request->observationOption,
1055 VERIFY_SUCCESS(result);
1057 // At this point we know for sure that defaultDeviceHandler exists
1058 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1059 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1060 if(ehResult == OC_EH_SLOW)
1062 OIC_LOG(INFO, TAG, "This is a slow resource");
1063 request->slowFlag = 1;
1065 else if(ehResult == OC_EH_ERROR)
1067 FindAndDeleteServerRequest(request);
1069 result = EntityHandlerCodeToOCStackCode(ehResult);
1071 OCPayloadDestroy(ehRequest.payload);
1075 static OCStackResult
1076 HandleResourceWithEntityHandler (OCServerRequest *request,
1077 OCResource *resource,
1078 uint8_t collectionResource)
1080 OC_UNUSED(collectionResource);
1082 if(!request || ! resource)
1084 return OC_STACK_INVALID_PARAM;
1087 OCStackResult result = OC_STACK_ERROR;
1088 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1089 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1090 ResourceObserver *resObs = NULL;
1092 OCEntityHandlerRequest ehRequest = {0};
1094 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1095 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1096 // check the security resource
1097 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1099 type = PAYLOAD_TYPE_SECURITY;
1102 result = FormOCEntityHandlerRequest(&ehRequest,
1103 (OCRequestHandle)request->requestId,
1106 (OCResourceHandle)resource,
1110 request->payloadSize,
1111 request->numRcvdVendorSpecificHeaderOptions,
1112 request->rcvdVendorSpecificHeaderOptions,
1113 (OCObserveAction)request->observationOption,
1116 VERIFY_SUCCESS(result);
1118 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1120 OIC_LOG(INFO, TAG, "No observation requested");
1121 ehFlag = OC_REQUEST_FLAG;
1123 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1125 OIC_LOG(INFO, TAG, "Observation registration requested");
1127 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1128 request->tokenLength);
1132 OIC_LOG (INFO, TAG, "Observer with this token already present");
1133 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1134 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1135 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1137 // server requests are usually free'd when the response is sent out
1138 // for the request in ocserverrequest.c : HandleSingleResponse()
1139 // Since we are making an early return and not responding, the server request
1140 // needs to be deleted.
1141 FindAndDeleteServerRequest (request);
1145 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1146 VERIFY_SUCCESS(result);
1148 result = AddObserver ((const char*)(request->resourceUrl),
1149 (const char *)(request->query),
1150 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1151 resource, request->qos, request->acceptFormat,
1154 if(result == OC_STACK_OK)
1156 OIC_LOG(INFO, TAG, "Added observer successfully");
1157 request->observeResult = OC_STACK_OK;
1158 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1160 else if (result == OC_STACK_RESOURCE_ERROR)
1162 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1163 request->observeResult = OC_STACK_ERROR;
1164 ehFlag = OC_REQUEST_FLAG;
1168 // The error in observeResult for the request will be used when responding to this
1169 // request by omitting the observation option/sequence number.
1170 request->observeResult = OC_STACK_ERROR;
1171 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1172 ehFlag = OC_REQUEST_FLAG;
1173 FindAndDeleteServerRequest(request);
1178 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1180 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1182 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1186 // Stack does not contain this observation request
1187 // Either token is incorrect or observation list is corrupted
1188 result = OC_STACK_ERROR;
1191 ehRequest.obsInfo.obsId = resObs->observeId;
1192 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1194 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1196 if(result == OC_STACK_OK)
1198 OIC_LOG(INFO, TAG, "Removed observer successfully");
1199 request->observeResult = OC_STACK_OK;
1200 // There should be no observe option header for de-registration response.
1201 // Set as an invalid value here so we can detect it later and remove the field in response.
1202 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1206 request->observeResult = OC_STACK_ERROR;
1207 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1208 FindAndDeleteServerRequest(request);
1214 result = OC_STACK_ERROR;
1218 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1219 if(ehResult == OC_EH_SLOW)
1221 OIC_LOG(INFO, TAG, "This is a slow resource");
1222 request->slowFlag = 1;
1224 else if(ehResult == OC_EH_ERROR)
1226 FindAndDeleteServerRequest(request);
1228 result = EntityHandlerCodeToOCStackCode(ehResult);
1230 OCPayloadDestroy(ehRequest.payload);
1234 static OCStackResult
1235 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
1236 OCResource *resource)
1238 if(!request || !resource)
1240 return OC_STACK_INVALID_PARAM;
1243 OCStackResult result = OC_STACK_ERROR;
1244 OCEntityHandlerRequest ehRequest = {0};
1246 result = FormOCEntityHandlerRequest(&ehRequest,
1247 (OCRequestHandle)request->requestId,
1250 (OCResourceHandle)resource,
1252 PAYLOAD_TYPE_REPRESENTATION,
1254 request->payloadSize,
1255 request->numRcvdVendorSpecificHeaderOptions,
1256 request->rcvdVendorSpecificHeaderOptions,
1257 (OCObserveAction)request->observationOption,
1260 if(result == OC_STACK_OK)
1262 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1265 OCPayloadDestroy(ehRequest.payload);
1270 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1272 OCStackResult ret = OC_STACK_OK;
1274 switch (resHandling)
1276 case OC_RESOURCE_VIRTUAL:
1278 ret = HandleVirtualResource (request, resource);
1281 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1283 ret = HandleDefaultDeviceEntityHandler(request);
1286 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1288 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1289 return OC_STACK_ERROR;
1291 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1293 ret = HandleResourceWithEntityHandler (request, resource, 0);
1296 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1298 ret = HandleResourceWithEntityHandler (request, resource, 1);
1301 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1303 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1306 case OC_RESOURCE_NOT_SPECIFIED:
1308 ret = OC_STACK_NO_RESOURCE;
1313 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1314 return OC_STACK_ERROR;
1320 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1322 OCResource *resource = NULL;
1323 if (!info.platformID || !info.manufacturerName)
1325 OIC_LOG(ERROR, TAG, "No value specified.");
1328 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1330 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1333 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1334 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1335 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1336 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1337 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1338 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1339 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1340 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1342 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1346 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1349 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1352 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1353 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1354 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1355 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1356 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1357 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1358 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1359 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1360 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1361 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1362 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1363 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1364 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1368 return OC_STACK_INVALID_PARAM;
1371 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1373 OCStringLL *dataModelVersion = NULL;
1374 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1377 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1380 if (!info.deviceName || info.deviceName[0] == '\0')
1382 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1383 return OC_STACK_INVALID_PARAM;
1386 if (OCGetServerInstanceIDString() == NULL)
1388 OIC_LOG(INFO, TAG, "Device ID generation failed");
1392 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1393 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1397 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1400 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1401 info.specVersion: OC_SPEC_VERSION));
1402 if (info.dataModelVersions)
1404 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1408 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1409 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1411 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1415 if (dataModelVersion)
1417 OCFreeOCStringLL(dataModelVersion);
1419 return OC_STACK_ERROR;
1422 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1424 if (!resource || !attribute)
1426 return OC_STACK_INVALID_PARAM;
1428 if (0 == strlen(attribute))
1430 return OC_STACK_INVALID_PARAM;
1432 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1434 if (0 == strcmp(attribute, temp->attrName))
1436 // A special case as this type return OCStringLL
1437 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1439 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1444 *value = OICStrdup((char *)temp->attrValue);
1449 return OC_STACK_NO_RESOURCE;
1452 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1454 if (!prop || *value)
1456 return OC_STACK_INVALID_PARAM;
1458 if (strlen(prop) == 0)
1460 return OC_STACK_INVALID_PARAM;
1462 OCStackResult res = OC_STACK_NO_RESOURCE;
1463 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1465 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1466 OCResource *resource = FindResourceByUri(pathType);
1469 return OC_STACK_NO_RESOURCE;
1472 res = OCGetAttribute(resource, prop, value);
1477 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1479 // See if the attribute already exists in the list.
1480 OCAttribute *resAttrib;
1481 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1483 if (0 == strcmp(attribute, resAttrib->attrName))
1485 // Found, free the old value.
1486 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1488 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1492 OICFree((char *)resAttrib->attrValue);
1498 // If not already in the list, add it.
1499 if (NULL == resAttrib)
1501 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1502 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1503 resAttrib->attrName = OICStrdup(attribute);
1504 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1505 resAttrib->next = resource->rsrcAttributes;
1506 resource->rsrcAttributes = resAttrib;
1509 // Fill in the new value.
1510 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1512 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1516 resAttrib->attrValue = OICStrdup((char *)value);
1518 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1523 OCDeleteResourceAttributes(resAttrib);
1524 return OC_STACK_NO_MEMORY;
1528 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1532 return OC_STACK_INVALID_PARAM;
1534 if (strlen(prop) == 0)
1536 return OC_STACK_INVALID_PARAM;
1539 OCStackResult res = OC_STACK_ERROR;
1540 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1542 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1543 OCResource *resource = FindResourceByUri(pathType);
1546 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1550 res = OCSetAttribute(resource, prop, value);