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 "oickeepalive.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;
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, 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)
421 OCRepPayload *tempPayload = OCRepPayloadCreate();
425 OCRepPayloadDestroy(tempPayload);
426 return OC_STACK_INVALID_PARAM;
431 return OC_STACK_NO_MEMORY;
434 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_HREF, resourcePtr->uri);
436 uint8_t numElement = 0;
437 if (OC_STACK_OK == OCGetNumberOfResourceTypes((OCResource *)resourcePtr, &numElement))
439 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
440 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
441 for (uint8_t i = 0; i < numElement; ++i)
443 const char *value = OCGetResourceTypeName((OCResource *)resourcePtr, i);
444 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
445 rt[i] = OICStrdup(value);
447 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_RESOURCE_TYPE, rt, rtDim);
451 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces((OCResource *)resourcePtr, &numElement))
453 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
454 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
455 for (uint8_t i = 0; i < numElement; ++i)
457 const char *value = OCGetResourceInterfaceName((OCResource *)resourcePtr, i);
458 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
459 itf[i] = OICStrdup(value);
461 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
464 for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
466 if (resAttrib->attrName && resAttrib->attrValue)
468 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
470 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
473 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
479 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
486 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
487 OCRepPayload *policy = OCRepPayloadCreate();
490 OCPayloadDestroy((OCPayload *)tempPayload);
491 return OC_STACK_NO_MEMORY;
493 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
496 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
497 uint16_t securePort = 0;
498 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
502 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
504 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
509 *payload = tempPayload;
513 OCRepPayloadAppend(*payload, tempPayload);
519 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
520 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
522 if (!resourcePtr || !payload)
524 return OC_STACK_INVALID_PARAM;
526 uint16_t securePort = 0;
527 if (resourcePtr->resourceProperties & OC_SECURE)
529 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
536 uint16_t tcpPort = 0;
537 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
539 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
541 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
547 OCResource *FindResourceByUri(const char* resourceUri)
554 OCResource * pointer = headResource;
557 if (strcmp(resourceUri, pointer->uri) == 0)
561 pointer = pointer->next;
563 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
567 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
568 ResourceHandling *handling,
569 OCResource **resource)
571 if(!request || !handling || !resource)
573 return OC_STACK_INVALID_PARAM;
576 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
578 // Check if virtual resource
579 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
581 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
582 *handling = OC_RESOURCE_VIRTUAL;
583 *resource = headResource;
586 if (strlen((const char*)(request->resourceUrl)) == 0)
588 // Resource URL not specified
589 *handling = OC_RESOURCE_NOT_SPECIFIED;
590 return OC_STACK_NO_RESOURCE;
594 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
595 *resource = resourcePtr;
598 if(defaultDeviceHandler)
600 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
604 // Resource does not exist
605 // and default device handler does not exist
606 *handling = OC_RESOURCE_NOT_SPECIFIED;
607 return OC_STACK_NO_RESOURCE;
610 if (resourcePtr && resourcePtr->rsrcChildResourcesHead != NULL)
612 // Collection resource
613 if (resourcePtr->entityHandler != defaultResourceEHandler)
615 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
620 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
626 // Resource not a collection
627 if (resourcePtr->entityHandler != defaultResourceEHandler)
629 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
634 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
641 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
643 OCStackResult result;
650 result = OC_STACK_OK;
653 result = OC_STACK_SLOW_RESOURCE;
656 result = OC_STACK_ERROR;
658 case OC_EH_FORBIDDEN:
659 result = OC_STACK_FORBIDDEN_REQ;
661 case OC_EH_INTERNAL_SERVER_ERROR:
662 result = OC_STACK_INTERNAL_SERVER_ERROR;
664 case OC_EH_RESOURCE_CREATED:
665 result = OC_STACK_RESOURCE_CREATED;
667 case OC_EH_RESOURCE_DELETED:
668 result = OC_STACK_RESOURCE_DELETED;
671 result = OC_STACK_RESOURCE_CHANGED;
673 case OC_EH_RESOURCE_NOT_FOUND:
674 result = OC_STACK_NO_RESOURCE;
677 result = OC_STACK_ERROR;
683 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
690 // Null or empty is analogous to no filter.
691 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
696 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
698 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
704 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
708 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
715 // Null or empty is analogous to no filter.
716 if (interfaceFilter == NULL || *interfaceFilter == 0)
721 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
723 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
724 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
725 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
731 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
736 * If the filters are null, they will be assumed to NOT be present
737 * and the resource will not be matched against them.
738 * Function will return true if all non null AND non empty filters passed in find a match.
740 static bool includeThisResourceInResponse(OCResource *resource,
741 char *interfaceFilter,
742 char *resourceTypeFilter)
746 OIC_LOG(ERROR, TAG, "Invalid resource");
750 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
753 * At least one valid filter should be available to
754 * include the resource in discovery response
756 if (!(resourceTypeFilter && *resourceTypeFilter))
758 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
759 resource", resource->uri);
763 else if (!(resource->resourceProperties & OC_ACTIVE) ||
764 !(resource->resourceProperties & OC_DISCOVERABLE))
766 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
770 return resourceMatchesIFFilter(resource, interfaceFilter) &&
771 resourceMatchesRTFilter(resource, resourceTypeFilter);
774 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
775 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
777 OCEntityHandlerResponse response = {0};
779 response.ehResult = ehResult;
780 response.payload = discoveryPayload;
781 response.persistentBufferFlag = 0;
782 response.requestHandle = (OCRequestHandle) request->requestId;
783 response.resourceHandle = (OCResourceHandle) resource;
785 return OCDoResponse(&response);
788 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
789 OCServerRequest *request, OCResource *resource)
791 return FormOCEntityHandlerRequest(ehRequest,
792 (OCRequestHandle)request->requestId,
795 (OCResourceHandle)resource,
799 request->payloadSize,
800 request->numRcvdVendorSpecificHeaderOptions,
801 request->rcvdVendorSpecificHeaderOptions,
802 (OCObserveAction)request->observationOption,
809 * Find resource at the resource directory server. This resource is not local resource but a
812 * @param resource The resource to check the matching resource URI.
813 * @param interfaceQuery The interface query parameter.
814 * @param resourceTypeQuery The resourceType query parameter.
815 * @param discPayload The payload that will be added with the resource information if found at RD.
817 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
818 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
820 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
821 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
823 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
825 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
831 return OC_STACK_NO_RESOURCE;
836 * Creates a discovery payload and add device id information. This information is included in all
839 * @param payload payload that will have memory alllocated and device id information added.
841 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
842 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
844 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
848 OIC_LOG_V(DEBUG, TAG, "Payload is already allocated");
852 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
853 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
856 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
857 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
858 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
860 const char* uid = OCGetServerInstanceIDString();
863 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
869 OCPayloadDestroy(*payload);
870 return OC_STACK_NO_MEMORY;
874 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
876 * @param discPayload payload that will have the baseline information included.
878 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
879 * allocating the memory for the baseline information.
881 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
883 discPayload->uri = OICStrdup(OC_RSRVD_WELL_KNOWN_URI);
884 VERIFY_PARAM_NON_NULL(TAG, discPayload->uri, "Failed adding href to discovery payload.");
886 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, "deviceName", (void **)&discPayload->name);
888 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
889 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
890 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
891 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
893 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
894 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
895 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
900 return OC_STACK_NO_MEMORY;
903 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
905 if (!request || !resource)
907 return OC_STACK_INVALID_PARAM;
910 OCPayload* payload = NULL;
911 char *interfaceQuery = NULL;
912 char *resourceTypeQuery = NULL;
913 char *dataModelVersions = NULL;
915 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
917 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
920 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
922 // Received request for a keepalive
923 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
924 return HandleKeepAliveRequest(request, resource);
928 OCStackResult discoveryResult = OC_STACK_ERROR;
929 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
930 request->method == OC_REST_DELETE)
932 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
933 request->resourceUrl, request->method);
934 return OC_STACK_UNAUTHORIZED_REQ;
937 // Step 1: Generate the response to discovery request
938 if (virtualUriInRequest == OC_WELL_KNOWN_URI
940 || virtualUriInRequest == OC_MQ_BROKER_URI
944 discoveryResult = getQueryParamsForFiltering(virtualUriInRequest, request->query,
945 &interfaceQuery, &resourceTypeQuery);
946 VERIFY_SUCCESS(discoveryResult);
947 if (!interfaceQuery && !resourceTypeQuery)
949 // If no query is sent, default interface is used i.e. oic.if.ll.
950 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
953 bool baselineQuery = false;
954 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
956 baselineQuery = true;
959 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
960 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
961 VERIFY_SUCCESS(discoveryResult);
963 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
966 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
967 VERIFY_SUCCESS(discoveryResult);
969 OCResourceProperty prop = OC_DISCOVERABLE;
971 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
973 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
975 discoveryResult = OC_STACK_NO_RESOURCE;
977 discoveryResult = findResourceAtRD(resource, interfaceQuery, resourceTypeQuery,
980 if (OC_STACK_NO_RESOURCE == discoveryResult)
982 // This case will handle when no resource type and it is oic.if.ll.
983 if (!resourceTypeQuery && !baselineQuery && (resource->resourceProperties & prop))
985 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
987 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
989 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
993 discoveryResult = OC_STACK_OK;
997 if (discPayload->resources == NULL)
999 discoveryResult = OC_STACK_NO_RESOURCE;
1002 else if (virtualUriInRequest == OC_DEVICE_URI)
1004 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1005 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1006 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1008 else if (virtualUriInRequest == OC_PLATFORM_URI)
1010 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1011 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1012 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1014 #ifdef ROUTING_GATEWAY
1015 else if (OC_GATEWAY_URI == virtualUriInRequest)
1017 // Received request for a gateway
1018 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1019 discoveryResult = RMHandleGatewayRequest(request, resource);
1024 * Step 2: Send the discovery response
1026 * Iotivity should respond to discovery requests in below manner:
1027 * 1)If query filter matching fails and discovery request is multicast,
1028 * it should NOT send any response.
1029 * 2)If query filter matching fails and discovery request is unicast,
1030 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1031 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1032 * request is multicast, it should NOT send any response.
1033 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1034 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1037 #ifdef WITH_PRESENCE
1038 if ((virtualUriInRequest == OC_PRESENCE) &&
1039 (resource->resourceProperties & OC_ACTIVE))
1041 // Need to send ACK when the request is CON.
1042 if (request->qos == OC_HIGH_QOS)
1044 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1045 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1046 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1047 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1049 FindAndDeleteServerRequest(request);
1051 // Presence uses observer notification api to respond via SendPresenceNotification.
1052 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1057 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1058 if (OC_GATEWAY_URI != virtualUriInRequest)
1061 OIC_LOG_PAYLOAD(DEBUG, payload);
1062 if(discoveryResult == OC_STACK_OK)
1064 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
1066 else if(((request->devAddr.flags & OC_MULTICAST) == false) &&
1067 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
1068 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
1070 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
1071 discoveryResult, virtualUriInRequest);
1072 SendNonPersistantDiscoveryResponse(request, resource, NULL,
1073 (discoveryResult == OC_STACK_NO_RESOURCE) ?
1074 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1078 // Ignoring the discovery request as per RFC 7252, Section #8.2
1079 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1080 // the request should be removed.
1081 // since it never remove and causes a big memory waste.
1082 FindAndDeleteServerRequest(request);
1089 OICFree(interfaceQuery);
1092 if (resourceTypeQuery)
1094 OICFree(resourceTypeQuery);
1096 OCPayloadDestroy(payload);
1097 if (dataModelVersions)
1099 OICFree(dataModelVersions);
1101 return discoveryResult;
1104 static OCStackResult
1105 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
1109 return OC_STACK_INVALID_PARAM;
1112 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1113 OCEntityHandlerRequest ehRequest = {0};
1114 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1115 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
1116 VERIFY_SUCCESS(result);
1118 // At this point we know for sure that defaultDeviceHandler exists
1119 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1120 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1121 if(ehResult == OC_EH_SLOW)
1123 OIC_LOG(INFO, TAG, "This is a slow resource");
1124 request->slowFlag = 1;
1126 else if(ehResult == OC_EH_ERROR)
1128 FindAndDeleteServerRequest(request);
1130 result = EntityHandlerCodeToOCStackCode(ehResult);
1132 OCPayloadDestroy(ehRequest.payload);
1136 static OCStackResult
1137 HandleResourceWithEntityHandler(OCServerRequest *request,
1138 OCResource *resource)
1140 if(!request || ! resource)
1142 return OC_STACK_INVALID_PARAM;
1145 OCStackResult result = OC_STACK_ERROR;
1146 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1147 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1148 ResourceObserver *resObs = NULL;
1150 OCEntityHandlerRequest ehRequest = {0};
1152 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1153 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1154 // check the security resource
1155 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1157 type = PAYLOAD_TYPE_SECURITY;
1160 result = EHRequest(&ehRequest, type, request, resource);
1161 VERIFY_SUCCESS(result);
1163 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1165 OIC_LOG(INFO, TAG, "No observation requested");
1166 ehFlag = OC_REQUEST_FLAG;
1168 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1170 OIC_LOG(INFO, TAG, "Observation registration requested");
1172 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1173 request->tokenLength);
1177 OIC_LOG (INFO, TAG, "Observer with this token already present");
1178 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1179 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1180 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1182 // server requests are usually free'd when the response is sent out
1183 // for the request in ocserverrequest.c : HandleSingleResponse()
1184 // Since we are making an early return and not responding, the server request
1185 // needs to be deleted.
1186 FindAndDeleteServerRequest (request);
1190 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1191 VERIFY_SUCCESS(result);
1193 result = AddObserver ((const char*)(request->resourceUrl),
1194 (const char *)(request->query),
1195 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1196 resource, request->qos, request->acceptFormat,
1199 if(result == OC_STACK_OK)
1201 OIC_LOG(INFO, TAG, "Added observer successfully");
1202 request->observeResult = OC_STACK_OK;
1203 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1205 else if (result == OC_STACK_RESOURCE_ERROR)
1207 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1208 request->observeResult = OC_STACK_ERROR;
1209 ehFlag = OC_REQUEST_FLAG;
1213 // The error in observeResult for the request will be used when responding to this
1214 // request by omitting the observation option/sequence number.
1215 request->observeResult = OC_STACK_ERROR;
1216 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1217 ehFlag = OC_REQUEST_FLAG;
1218 FindAndDeleteServerRequest(request);
1223 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1225 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1227 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1231 // Stack does not contain this observation request
1232 // Either token is incorrect or observation list is corrupted
1233 result = OC_STACK_ERROR;
1236 ehRequest.obsInfo.obsId = resObs->observeId;
1237 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1239 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1241 if(result == OC_STACK_OK)
1243 OIC_LOG(INFO, TAG, "Removed observer successfully");
1244 request->observeResult = OC_STACK_OK;
1245 // There should be no observe option header for de-registration response.
1246 // Set as an invalid value here so we can detect it later and remove the field in response.
1247 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1251 request->observeResult = OC_STACK_ERROR;
1252 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1253 FindAndDeleteServerRequest(request);
1259 result = OC_STACK_ERROR;
1263 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1264 if(ehResult == OC_EH_SLOW)
1266 OIC_LOG(INFO, TAG, "This is a slow resource");
1267 request->slowFlag = 1;
1269 else if(ehResult == OC_EH_ERROR)
1271 FindAndDeleteServerRequest(request);
1273 result = EntityHandlerCodeToOCStackCode(ehResult);
1275 OCPayloadDestroy(ehRequest.payload);
1279 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
1280 OCResource *resource)
1282 if (!request || !resource)
1284 return OC_STACK_INVALID_PARAM;
1287 OCEntityHandlerRequest ehRequest = {0};
1288 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
1289 if(result == OC_STACK_OK)
1291 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1294 OCPayloadDestroy(ehRequest.payload);
1299 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1301 OCStackResult ret = OC_STACK_OK;
1303 switch (resHandling)
1305 case OC_RESOURCE_VIRTUAL:
1307 ret = HandleVirtualResource (request, resource);
1310 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1312 ret = HandleDefaultDeviceEntityHandler(request);
1315 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1317 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1318 return OC_STACK_ERROR;
1320 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1322 ret = HandleResourceWithEntityHandler (request, resource);
1325 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1327 ret = HandleResourceWithEntityHandler (request, resource);
1330 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1332 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1335 case OC_RESOURCE_NOT_SPECIFIED:
1337 ret = OC_STACK_NO_RESOURCE;
1342 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1343 return OC_STACK_ERROR;
1349 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1351 OCResource *resource = NULL;
1352 if (!info.platformID || !info.manufacturerName)
1354 OIC_LOG(ERROR, TAG, "No value specified.");
1357 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1359 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1362 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1363 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1364 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1365 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1366 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1367 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1368 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1369 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1371 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1375 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1378 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1381 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1382 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1383 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1384 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1385 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1386 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1387 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1388 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1389 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1390 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1391 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1392 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1393 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1397 return OC_STACK_INVALID_PARAM;
1400 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1402 OCStringLL *dataModelVersion = NULL;
1403 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1406 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1409 if (!info.deviceName || info.deviceName[0] == '\0')
1411 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1412 return OC_STACK_INVALID_PARAM;
1415 if (OCGetServerInstanceIDString() == NULL)
1417 OIC_LOG(INFO, TAG, "Device ID generation failed");
1421 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1422 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1426 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1429 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1430 info.specVersion: OC_SPEC_VERSION));
1431 if (info.dataModelVersions)
1433 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1437 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1438 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1440 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1444 if (dataModelVersion)
1446 OCFreeOCStringLL(dataModelVersion);
1448 return OC_STACK_ERROR;
1451 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1453 if (!resource || !attribute)
1455 return OC_STACK_INVALID_PARAM;
1457 if (0 == strlen(attribute))
1459 return OC_STACK_INVALID_PARAM;
1461 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1463 if (0 == strcmp(attribute, temp->attrName))
1465 // A special case as this type return OCStringLL
1466 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1468 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1473 *value = OICStrdup((char *)temp->attrValue);
1478 return OC_STACK_NO_RESOURCE;
1481 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1483 if (!prop || *value)
1485 return OC_STACK_INVALID_PARAM;
1487 if (strlen(prop) == 0)
1489 return OC_STACK_INVALID_PARAM;
1491 OCStackResult res = OC_STACK_NO_RESOURCE;
1492 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1494 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1495 OCResource *resource = FindResourceByUri(pathType);
1498 return OC_STACK_NO_RESOURCE;
1501 res = OCGetAttribute(resource, prop, value);
1506 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1508 // See if the attribute already exists in the list.
1509 OCAttribute *resAttrib = NULL;
1510 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1512 if (0 == strcmp(attribute, resAttrib->attrName))
1514 // Found, free the old value.
1515 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1517 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1521 OICFree((char *)resAttrib->attrValue);
1527 // If not already in the list, add it.
1528 if (NULL == resAttrib)
1530 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1531 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1532 resAttrib->attrName = OICStrdup(attribute);
1533 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1534 resAttrib->next = resource->rsrcAttributes;
1535 resource->rsrcAttributes = resAttrib;
1538 // Fill in the new value.
1539 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1541 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1545 resAttrib->attrValue = OICStrdup((char *)value);
1547 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1552 OCDeleteResourceAttributes(resAttrib);
1553 return OC_STACK_NO_MEMORY;
1557 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1559 if (!prop || !value)
1561 return OC_STACK_INVALID_PARAM;
1563 if (strlen(prop) == 0)
1565 return OC_STACK_INVALID_PARAM;
1568 OCStackResult res = OC_STACK_ERROR;
1569 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1571 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1572 OCResource *resource = FindResourceByUri(pathType);
1575 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1579 res = OCSetAttribute(resource, prop, value);