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"
47 #include "ocpayload.h"
48 #include "secureresourcemanager.h"
50 #include "cainterface.h"
51 #include "ocpayload.h"
52 #include "oickeepaliveinternal.h"
53 #include "platform_features.h"
54 #include "payload_logging.h"
55 #ifdef ROUTING_GATEWAY
56 #include "routingmanager.h"
60 #define TAG "OIC_RI_RESOURCE"
62 #define VERIFY_SUCCESS(op) { if (op != (OC_STACK_OK)) \
63 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
65 extern OCResource *headResource;
68 * Prepares a Payload for response.
70 static OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
71 OCDiscoveryPayload* payload,
74 //-----------------------------------------------------------------------------
75 // Default resource entity handler function
76 //-----------------------------------------------------------------------------
77 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
78 OCEntityHandlerRequest * request, void* callbackParam)
80 //TODO ("Implement me!!!!");
81 // TODO: remove silence unused param warnings
85 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
88 /* This method will retrieve the port at which the secure resource is hosted */
89 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
93 if (endpoint->adapter == OC_ADAPTER_IP)
95 if (endpoint->flags & OC_IP_USE_V6)
97 p = caglobals.ip.u6s.port;
99 else if (endpoint->flags & OC_IP_USE_V4)
101 p = caglobals.ip.u4s.port;
109 #if defined(TCP_ADAPTER) && !defined(DISABLE_TCP_SERVER)
110 /* This method will retrieve the tcp port */
111 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port, bool secured)
115 if (endpoint->adapter == OC_ADAPTER_IP)
117 if (endpoint->flags & OC_IP_USE_V4)
119 p = secured ? caglobals.tcp.ipv4s.port : caglobals.tcp.ipv4.port;
121 else if (endpoint->flags & OC_IP_USE_V6)
123 p = secured ? caglobals.tcp.ipv6s.port : caglobals.tcp.ipv6.port;
133 * Function will extract 0, 1 or 2 filters from query.
134 * More than 2 filters or unsupported filters will result in error.
135 * If both filters are of the same supported type, the 2nd one will be picked.
136 * Resource and device filters in the SAME query are NOT validated
137 * and resources will likely not clear filters.
139 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo)
143 OIC_LOG_V(ERROR, TAG, "Query is empty!");
144 return OC_STACK_INVALID_QUERY;
148 char *queryDup = NULL;
149 char *restOfQuery = NULL;
150 char *keyValuePair = NULL;
151 int numKeyValuePairsParsed = 0;
156 queryDup = OICStrdup(query);
157 if (NULL == queryDup)
159 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
160 return OC_STACK_NO_MEMORY;
163 OIC_LOG_V(INFO_PRIVATE, TAG, "Extracting params from %s", queryDup);
165 OCStackResult eCode = OC_STACK_INVALID_QUERY;
166 if (strnlen(queryDup, MAX_QUERY_LENGTH) >= MAX_QUERY_LENGTH)
168 OIC_LOG(ERROR, TAG, "Query exceeds maximum length.");
172 keyValuePair = strtok_r (queryDup, OC_QUERY_SEPARATOR, &restOfQuery);
176 if (numKeyValuePairsParsed >= 2)
178 OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
182 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
188 else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
190 *filterOne = value; // if
192 else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
194 *filterTwo = value; // rt
198 OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
201 ++numKeyValuePairsParsed;
203 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
208 *filterOne = OICStrdup(*filterOne);
209 if (NULL == *filterOne)
211 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
212 eCode = OC_STACK_NO_MEMORY;
219 *filterTwo = OICStrdup(*filterTwo);
220 if (NULL == *filterTwo)
222 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
224 eCode = OC_STACK_NO_MEMORY;
230 OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
240 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
242 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
244 return OC_WELL_KNOWN_URI;
246 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
248 return OC_DEVICE_URI;
250 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
252 return OC_PLATFORM_URI;
254 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
256 return OC_RESOURCE_TYPES_URI;
258 #ifdef ROUTING_GATEWAY
259 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
261 return OC_GATEWAY_URI;
265 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
269 #endif //WITH_PRESENCE
272 else if (0 == strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_MQ_URI))
274 return OC_MQ_BROKER_URI;
279 else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
281 return OC_KEEPALIVE_RESOURCE_URI;
285 return OC_UNKNOWN_URI;
288 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
289 char **filterOne, char **filterTwo)
291 if(!filterOne || !filterTwo)
293 return OC_STACK_INVALID_PARAM;
300 if (uri == OC_PRESENCE)
302 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
303 OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
308 OCStackResult result = OC_STACK_OK;
312 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
318 bool appendOCStringLL(OCRepPayload *device, OCStringLL *dmv)
321 for (OCStringLL *ll = dmv; ll; ll = ll->next, size++);
322 size_t dim[MAX_REP_ARRAY_DEPTH] = {size, 0, 0};
323 char **dt = (char **)OICMalloc(sizeof(char *) * size);
325 VERIFY_PARAM_NON_NULL(TAG, dt, "Data Model Version allocation failed.");
326 for (OCStringLL *ll = dmv; ll; ll = ll->next, i++)
328 dt[i] = OICStrdup(ll->value);
329 VERIFY_PARAM_NON_NULL(TAG, dt[i], "Data Model Version adding failed.");
331 if (!OCRepPayloadSetStringArrayAsOwner(device, OC_RSRVD_DATA_MODEL_VERSION, dt, dim))
338 for (int i = 0; i < size; i++)
346 static OCStackResult BuildDevicePlatformPayload(const OCResource *resourcePtr, OCRepPayload** payload, bool addDeviceId)
350 return OC_STACK_INVALID_PARAM;
353 OCRepPayload *tempPayload = OCRepPayloadCreate();
356 return OC_STACK_NO_MEMORY;
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);
370 OCResourceType *resType = resourcePtr->rsrcType;
373 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
374 resType = resType->next;
377 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
380 OCRepPayloadAddInterface(tempPayload, resInterface->name);
381 resInterface = resInterface->next;
384 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
387 if (resAttrib->attrName && resAttrib->attrValue)
389 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
391 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
394 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
400 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
403 resAttrib = resAttrib->next;
408 *payload = tempPayload;
412 OCRepPayloadAppend(*payload, tempPayload);
418 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
419 OCRepPayload** payload, OCDevAddr *devAddr)
423 return OC_STACK_INVALID_PARAM;
426 OCRepPayload *tempPayload = OCRepPayloadCreate();
429 return OC_STACK_NO_MEMORY;
432 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_HREF, resourcePtr->uri);
434 uint8_t numElement = 0;
435 if (OC_STACK_OK == OCGetNumberOfResourceTypes((OCResource *)resourcePtr, &numElement))
437 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
438 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
441 OIC_LOG(ERROR, TAG, "Resource type allocation failed.");
442 OCRepPayloadDestroy(tempPayload);
443 return OC_STACK_NO_MEMORY;
445 for (uint8_t i = 0; i < numElement; ++i)
447 const char *value = OCGetResourceTypeName((OCResource *)resourcePtr, i);
448 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
449 rt[i] = OICStrdup(value);
451 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_RESOURCE_TYPE, rt, rtDim);
455 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces((OCResource *)resourcePtr, &numElement))
457 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
458 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
461 OIC_LOG(ERROR, TAG, "Resource interface allocation failed.");
462 OCRepPayloadDestroy(tempPayload);
463 return OC_STACK_NO_MEMORY;
465 for (uint8_t i = 0; i < numElement; ++i)
467 const char *value = OCGetResourceInterfaceName((OCResource *)resourcePtr, i);
468 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
469 itf[i] = OICStrdup(value);
471 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
474 for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
476 if (resAttrib->attrName && resAttrib->attrValue)
478 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
480 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
483 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
489 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
496 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
497 OCRepPayload *policy = OCRepPayloadCreate();
500 OCPayloadDestroy((OCPayload *)tempPayload);
501 return OC_STACK_NO_MEMORY;
503 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
506 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
507 uint16_t securePort = 0;
508 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
512 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
514 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
519 *payload = tempPayload;
523 OCRepPayloadAppend(*payload, tempPayload);
529 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
530 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
532 if (!resourcePtr || !payload)
534 return OC_STACK_INVALID_PARAM;
536 uint16_t securePort = 0;
537 if (resourcePtr->resourceProperties & OC_SECURE)
539 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
545 #if defined(TCP_ADAPTER) && !defined(DISABLE_TCP_SERVER)
546 uint16_t tcpPort = 0;
547 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
549 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
551 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
557 OCResource *FindResourceByUri(const char* resourceUri)
564 OCResource * pointer = headResource;
567 if (strcmp(resourceUri, pointer->uri) == 0)
571 pointer = pointer->next;
573 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
577 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
578 ResourceHandling *handling,
579 OCResource **resource)
581 if(!request || !handling || !resource)
583 return OC_STACK_INVALID_PARAM;
586 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
588 // Check if virtual resource
589 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
591 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
592 *handling = OC_RESOURCE_VIRTUAL;
593 *resource = headResource;
596 if (strlen((const char*)(request->resourceUrl)) == 0)
598 // Resource URL not specified
599 *handling = OC_RESOURCE_NOT_SPECIFIED;
600 return OC_STACK_NO_RESOURCE;
604 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
605 *resource = resourcePtr;
608 if(defaultDeviceHandler)
610 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
614 // Resource does not exist
615 // and default device handler does not exist
616 *handling = OC_RESOURCE_NOT_SPECIFIED;
617 return OC_STACK_NO_RESOURCE;
620 if (resourcePtr && resourcePtr->rsrcChildResourcesHead != NULL)
622 // Collection resource
623 if (resourcePtr->entityHandler != defaultResourceEHandler)
625 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
630 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
636 // Resource not a collection
637 if (resourcePtr->entityHandler != defaultResourceEHandler)
639 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
644 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
651 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
653 OCStackResult result;
660 result = OC_STACK_OK;
663 result = OC_STACK_SLOW_RESOURCE;
666 result = OC_STACK_ERROR;
668 case OC_EH_FORBIDDEN:
669 result = OC_STACK_FORBIDDEN_REQ;
671 case OC_EH_RESOURCE_CREATED:
672 result = OC_STACK_RESOURCE_CREATED;
674 case OC_EH_RESOURCE_DELETED:
675 result = OC_STACK_RESOURCE_DELETED;
678 result = OC_STACK_RESOURCE_CHANGED;
680 case OC_EH_RESOURCE_NOT_FOUND:
681 result = OC_STACK_NO_RESOURCE;
683 case OC_EH_TOO_MANY_REQUESTS:
684 result = OC_STACK_TOO_MANY_REQUESTS;
686 case OC_EH_INTERNAL_SERVER_ERROR:
687 result = OC_STACK_INTERNAL_SERVER_ERROR;
689 case OC_EH_NOT_IMPLEMENTED:
690 result = OC_STACK_NOT_IMPLEMENTED;
692 case OC_EH_BAD_GATEWAY:
693 result = OC_STACK_BAD_GATEWAY;
695 case OC_EH_SERVICE_UNAVAILABLE:
696 result = OC_STACK_SERVICE_UNAVAILABLE;
698 case OC_EH_RETRANSMIT_TIMEOUT:
699 result = OC_STACK_GATEWAY_TIMEOUT;
701 case OC_EH_PROXY_NOT_SUPPORTED:
702 result = OC_STACK_PROXY_NOT_SUPPORTED;
705 result = OC_STACK_ERROR;
711 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
718 // Null is analogous to no filter.
719 if (NULL == resourceTypeFilter)
724 // Empty resourceType filter is analogous to error query
725 if (0 == strlen(resourceTypeFilter))
730 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
732 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
738 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
742 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
749 // Null is analogous to no filter.
750 if (NULL == interfaceFilter)
755 // Empty interface filter is analogous to error query
756 if (0 == strlen(interfaceFilter))
761 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
763 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
764 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
765 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
771 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
776 * If the filters are null, they will be assumed to NOT be present
777 * and the resource will not be matched against them.
778 * Function will return true if all non null AND non empty filters passed in find a match.
780 static bool includeThisResourceInResponse(OCResource *resource,
781 char *interfaceFilter,
782 char *resourceTypeFilter)
786 OIC_LOG(ERROR, TAG, "Invalid resource");
790 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
793 * At least one valid filter should be available to
794 * include the resource in discovery response
796 if (!(resourceTypeFilter && *resourceTypeFilter))
798 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
799 resource", resource->uri);
803 else if (!(resource->resourceProperties & OC_ACTIVE) ||
804 !(resource->resourceProperties & OC_DISCOVERABLE))
806 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
810 return resourceMatchesIFFilter(resource, interfaceFilter) &&
811 resourceMatchesRTFilter(resource, resourceTypeFilter);
814 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
815 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
817 OCEntityHandlerResponse response = {0};
819 response.ehResult = ehResult;
820 response.payload = discoveryPayload;
821 response.persistentBufferFlag = 0;
822 response.requestHandle = (OCRequestHandle) request->requestId;
823 response.resourceHandle = (OCResourceHandle) resource;
825 return OCDoResponse(&response);
828 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
829 OCServerRequest *request, OCResource *resource)
831 return FormOCEntityHandlerRequest(ehRequest,
832 (OCRequestHandle)request->requestId,
835 (OCResourceHandle)resource,
839 request->payloadSize,
840 request->numRcvdVendorSpecificHeaderOptions,
841 request->rcvdVendorSpecificHeaderOptions,
842 (OCObserveAction)request->observationOption,
849 * Find resource at the resource directory server. This resource is not local resource but a
852 * @param resource The resource to check the matching resource URI.
853 * @param interfaceQuery The interface query parameter.
854 * @param resourceTypeQuery The resourceType query parameter.
855 * @param discPayload The payload that will be added with the resource information if found at RD.
857 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
858 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
860 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
861 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
863 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
865 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
871 return OC_STACK_NO_RESOURCE;
876 * Creates a discovery payload and add device id information. This information is included in all
879 * @param payload payload that will have memory alllocated and device id information added.
881 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
882 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
884 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
888 OIC_LOG(DEBUG, TAG, "Payload is already allocated");
892 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
893 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
896 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
897 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
898 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
900 const char* uid = OCGetServerInstanceIDString();
903 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
909 OCPayloadDestroy(*payload);
910 return OC_STACK_NO_MEMORY;
914 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
916 * @param discPayload payload that will have the baseline information included.
918 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
919 * allocating the memory for the baseline information.
921 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
925 OIC_LOG(ERROR, TAG, "Payload is not allocated");
926 return OC_STACK_ERROR;
929 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **)&discPayload->name);
931 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
932 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
933 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
934 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
936 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
937 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
938 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
943 return OC_STACK_NO_MEMORY;
946 static bool isUnicast(OCServerRequest *request)
948 bool isMulticast = request->devAddr.flags & OC_MULTICAST;
949 return (isMulticast == false &&
950 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
951 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE));
954 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
956 if (!request || !resource)
958 return OC_STACK_INVALID_PARAM;
961 OCPayload* payload = NULL;
962 char *interfaceQuery = NULL;
963 char *resourceTypeQuery = NULL;
965 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
967 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
970 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
972 // Received request for a keepalive
973 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
974 return OCHandleKeepAliveRequest(request, resource);
978 OCStackResult discoveryResult = OC_STACK_ERROR;
979 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
980 request->method == OC_REST_DELETE)
982 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
983 request->resourceUrl, request->method);
984 return OC_STACK_UNAUTHORIZED_REQ;
987 // Step 1: Generate the response to discovery request
988 if (virtualUriInRequest == OC_WELL_KNOWN_URI
990 || virtualUriInRequest == OC_MQ_BROKER_URI
994 discoveryResult = getQueryParamsForFiltering(virtualUriInRequest, request->query,
995 &interfaceQuery, &resourceTypeQuery);
996 VERIFY_SUCCESS(discoveryResult);
998 if (!interfaceQuery && !resourceTypeQuery)
1000 // If no query is sent, default interface is used i.e. oic.if.ll.
1001 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
1004 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
1005 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
1006 VERIFY_SUCCESS(discoveryResult);
1008 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
1009 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
1011 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
1012 VERIFY_SUCCESS(discoveryResult);
1014 OCResourceProperty prop = OC_DISCOVERABLE;
1016 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
1018 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
1020 // This case will handle when no resource type and it is oic.if.ll.
1021 // Do not assume check if the query is ll
1022 if (!resourceTypeQuery &&
1023 (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
1025 // Only include discoverable type
1026 if (resource->resourceProperties & prop)
1028 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1031 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
1033 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1037 discoveryResult = OC_STACK_OK;
1040 if (discPayload->resources == NULL)
1042 discoveryResult = OC_STACK_NO_RESOURCE;
1045 else if (virtualUriInRequest == OC_DEVICE_URI)
1047 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1048 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1049 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1051 else if (virtualUriInRequest == OC_PLATFORM_URI)
1053 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1054 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1055 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1057 #ifdef ROUTING_GATEWAY
1058 else if (OC_GATEWAY_URI == virtualUriInRequest)
1060 // Received request for a gateway
1061 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1062 discoveryResult = RMHandleGatewayRequest(request, resource);
1067 * Step 2: Send the discovery response
1069 * Iotivity should respond to discovery requests in below manner:
1070 * 1)If query filter matching fails and discovery request is multicast,
1071 * it should NOT send any response.
1072 * 2)If query filter matching fails and discovery request is unicast,
1073 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1074 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1075 * request is multicast, it should NOT send any response.
1076 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1077 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1080 #ifdef WITH_PRESENCE
1081 if ((virtualUriInRequest == OC_PRESENCE) &&
1082 (resource->resourceProperties & OC_ACTIVE))
1084 // Need to send ACK when the request is CON.
1085 if (request->qos == OC_HIGH_QOS)
1087 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1088 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1089 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1090 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1092 FindAndDeleteServerRequest(request);
1094 // Presence uses observer notification api to respond via SendPresenceNotification.
1095 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1100 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1101 if (OC_GATEWAY_URI != virtualUriInRequest)
1104 OIC_LOG_PAYLOAD(DEBUG, payload);
1105 if(discoveryResult == OC_STACK_OK)
1108 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
1110 else // Error handling
1112 if (isUnicast(request))
1114 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
1115 discoveryResult, virtualUriInRequest);
1116 SendNonPersistantDiscoveryResponse(request, resource, NULL,
1117 (discoveryResult == OC_STACK_NO_RESOURCE) ?
1118 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1122 // Ignoring the discovery request as per RFC 7252, Section #8.2
1123 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1124 // the request should be removed.
1125 // since it never remove and causes a big memory waste.
1126 FindAndDeleteServerRequest(request);
1128 discoveryResult = OC_STACK_CONTINUE;
1135 OICFree(interfaceQuery);
1138 if (resourceTypeQuery)
1140 OICFree(resourceTypeQuery);
1142 OCPayloadDestroy(payload);
1144 // To ignore the message, OC_STACK_CONTINUE is sent
1145 return discoveryResult;
1148 static OCStackResult
1149 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
1153 return OC_STACK_INVALID_PARAM;
1156 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1157 OCEntityHandlerRequest ehRequest = {0};
1158 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1159 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
1160 VERIFY_SUCCESS(result);
1162 // At this point we know for sure that defaultDeviceHandler exists
1163 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1164 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1165 if(ehResult == OC_EH_SLOW)
1167 OIC_LOG(INFO, TAG, "This is a slow resource");
1168 request->slowFlag = 1;
1171 result = EntityHandlerCodeToOCStackCode(ehResult);
1173 // Clear server request on error case
1174 if (!OCResultToSuccess(result))
1176 FindAndDeleteServerRequest(request);
1180 OCPayloadDestroy(ehRequest.payload);
1184 static OCStackResult
1185 HandleResourceWithEntityHandler(OCServerRequest *request,
1186 OCResource *resource)
1188 if(!request || ! resource)
1190 return OC_STACK_INVALID_PARAM;
1193 OCStackResult result = OC_STACK_ERROR;
1194 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1195 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1196 ResourceObserver *resObs = NULL;
1198 OCEntityHandlerRequest ehRequest = {0};
1200 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1201 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1202 // check the security resource
1203 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1205 type = PAYLOAD_TYPE_SECURITY;
1208 result = EHRequest(&ehRequest, type, request, resource);
1209 VERIFY_SUCCESS(result);
1211 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1213 OIC_LOG(INFO, TAG, "No observation requested");
1214 ehFlag = OC_REQUEST_FLAG;
1216 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1218 OIC_LOG(INFO, TAG, "Observation registration requested");
1220 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1221 request->tokenLength);
1225 OIC_LOG (INFO, TAG, "Observer with this token already present");
1226 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1227 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1228 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1234 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1235 VERIFY_SUCCESS(result);
1237 result = AddObserver ((const char*)(request->resourceUrl),
1238 (const char *)(request->query),
1239 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1240 resource, request->qos, request->acceptFormat,
1243 if(result == OC_STACK_OK)
1245 OIC_LOG(INFO, TAG, "Added observer successfully");
1246 request->observeResult = OC_STACK_OK;
1247 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1249 else if (result == OC_STACK_RESOURCE_ERROR)
1251 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1252 request->observeResult = OC_STACK_ERROR;
1253 ehFlag = OC_REQUEST_FLAG;
1257 // The error in observeResult for the request will be used when responding to this
1258 // request by omitting the observation option/sequence number.
1259 request->observeResult = OC_STACK_ERROR;
1260 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1261 ehFlag = OC_REQUEST_FLAG;
1262 FindAndDeleteServerRequest(request);
1267 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1269 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1271 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1275 // Stack does not contain this observation request
1276 // Either token is incorrect or observation list is corrupted
1277 result = OC_STACK_NO_RESOURCE;
1280 ehRequest.obsInfo.obsId = resObs->observeId;
1281 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1283 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1285 if(result == OC_STACK_OK)
1287 OIC_LOG(INFO, TAG, "Removed observer successfully");
1288 request->observeResult = OC_STACK_OK;
1289 // There should be no observe option header for de-registration response.
1290 // Set as an invalid value here so we can detect it later and remove the field in response.
1291 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1295 request->observeResult = OC_STACK_ERROR;
1296 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1297 FindAndDeleteServerRequest(request);
1303 result = OC_STACK_ERROR;
1307 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1308 if(ehResult == OC_EH_SLOW)
1310 OIC_LOG(INFO, TAG, "This is a slow resource");
1311 request->slowFlag = 1;
1314 result = EntityHandlerCodeToOCStackCode(ehResult);
1316 // Clear server request on error case
1317 if (!OCResultToSuccess(result))
1319 FindAndDeleteServerRequest(request);
1323 OCPayloadDestroy(ehRequest.payload);
1324 FreeObserver(resObs);
1328 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
1329 OCResource *resource)
1331 if (!request || !resource)
1333 return OC_STACK_INVALID_PARAM;
1336 OCEntityHandlerRequest ehRequest = {0};
1337 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
1338 if(result == OC_STACK_OK)
1340 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1343 OCPayloadDestroy(ehRequest.payload);
1348 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1350 OCStackResult ret = OC_STACK_OK;
1352 switch (resHandling)
1354 case OC_RESOURCE_VIRTUAL:
1356 ret = HandleVirtualResource (request, resource);
1359 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1361 ret = HandleDefaultDeviceEntityHandler(request);
1364 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1366 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1367 return OC_STACK_ERROR;
1369 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1371 ret = HandleResourceWithEntityHandler (request, resource);
1374 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1376 ret = HandleResourceWithEntityHandler (request, resource);
1379 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1381 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1384 case OC_RESOURCE_NOT_SPECIFIED:
1386 ret = OC_STACK_NO_RESOURCE;
1391 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1392 return OC_STACK_ERROR;
1398 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1400 OCResource *resource = NULL;
1401 if (!info.platformID || !info.manufacturerName)
1403 OIC_LOG(ERROR, TAG, "No value specified.");
1406 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1408 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1411 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1412 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1413 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1414 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1415 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1416 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1417 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1418 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1420 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1424 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1427 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1430 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1431 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1432 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1433 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1434 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1435 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1436 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1437 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1438 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1439 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1440 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1441 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1442 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1446 return OC_STACK_INVALID_PARAM;
1449 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1451 OCStringLL *dataModelVersion = NULL;
1452 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1455 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1458 if (!info.deviceName || info.deviceName[0] == '\0')
1460 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1461 return OC_STACK_INVALID_PARAM;
1464 if (OCGetServerInstanceIDString() == NULL)
1466 OIC_LOG(INFO, TAG, "Device ID generation failed");
1470 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1471 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1475 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1478 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1479 info.specVersion: OC_SPEC_VERSION));
1480 if (info.dataModelVersions)
1482 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1486 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1487 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1489 OCFreeOCStringLL(dataModelVersion);
1492 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1496 if (dataModelVersion)
1498 OCFreeOCStringLL(dataModelVersion);
1500 return OC_STACK_ERROR;
1503 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1505 if (!resource || !attribute)
1507 return OC_STACK_INVALID_PARAM;
1509 if (0 == strlen(attribute))
1511 return OC_STACK_INVALID_PARAM;
1513 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1515 if (0 == strcmp(attribute, temp->attrName))
1517 // A special case as this type return OCStringLL
1518 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1520 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1525 *value = OICStrdup((char *)temp->attrValue);
1530 return OC_STACK_NO_RESOURCE;
1533 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1535 if (!prop || *value)
1537 return OC_STACK_INVALID_PARAM;
1539 if (strlen(prop) == 0)
1541 return OC_STACK_INVALID_PARAM;
1543 OCStackResult res = OC_STACK_NO_RESOURCE;
1544 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1546 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1547 OCResource *resource = FindResourceByUri(pathType);
1550 return OC_STACK_NO_RESOURCE;
1553 res = OCGetAttribute(resource, prop, value);
1558 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1560 // See if the attribute already exists in the list.
1561 OCAttribute *resAttrib;
1562 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1564 if (0 == strcmp(attribute, resAttrib->attrName))
1566 // Found, free the old value.
1567 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1569 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1573 OICFree((char *)resAttrib->attrValue);
1579 // If not already in the list, add it.
1580 if (NULL == resAttrib)
1582 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1583 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1584 resAttrib->attrName = OICStrdup(attribute);
1585 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1586 resAttrib->next = resource->rsrcAttributes;
1587 resource->rsrcAttributes = resAttrib;
1590 // Fill in the new value.
1591 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1593 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1597 resAttrib->attrValue = OICStrdup((char *)value);
1599 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1604 OCDeleteResourceAttributes(resAttrib);
1605 return OC_STACK_NO_MEMORY;
1609 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1611 if (!prop || !value)
1613 return OC_STACK_INVALID_PARAM;
1615 if (strlen(prop) == 0)
1617 return OC_STACK_INVALID_PARAM;
1620 OCStackResult res = OC_STACK_ERROR;
1621 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1623 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1624 OCResource *resource = FindResourceByUri(pathType);
1627 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1631 res = OCSetAttribute(resource, prop, value);