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 "platform_features.h"
53 #include "payload_logging.h"
54 #include "ocendpoint.h"
55 #include "ocstackinternal.h"
57 #ifdef ROUTING_GATEWAY
58 #include "routingmanager.h"
62 #define TAG "OIC_RI_RESOURCE"
64 #define VERIFY_SUCCESS(op) { if (op != (OC_STACK_OK)) \
65 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
67 extern OCResource *headResource;
70 * Prepares a Payload for response.
72 static OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
73 OCDiscoveryPayload *payload,
75 CAEndpoint_t *networkInfo,
78 //-----------------------------------------------------------------------------
79 // Default resource entity handler function
80 //-----------------------------------------------------------------------------
81 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
82 OCEntityHandlerRequest * request, void* callbackParam)
84 //TODO ("Implement me!!!!");
85 // TODO: remove silence unused param warnings
89 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
92 /* This method will retrieve the port at which the secure resource is hosted */
93 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
97 if (endpoint->adapter == OC_ADAPTER_IP)
99 if (endpoint->flags & OC_IP_USE_V6)
101 p = caglobals.ip.u6s.port;
103 else if (endpoint->flags & OC_IP_USE_V4)
105 p = caglobals.ip.u4s.port;
114 /* This method will retrieve the tcp port */
115 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port)
119 if (endpoint->adapter == OC_ADAPTER_IP)
121 if (endpoint->flags & OC_IP_USE_V4)
123 p = caglobals.tcp.ipv4.port;
125 else if (endpoint->flags & OC_IP_USE_V6)
127 p = caglobals.tcp.ipv6.port;
137 * Function will extract 0, 1 or 2 filters from query.
138 * More than 2 filters or unsupported filters will result in error.
139 * If both filters are of the same supported type, the 2nd one will be picked.
140 * Resource and device filters in the SAME query are NOT validated
141 * and resources will likely not clear filters.
143 static OCStackResult ExtractFiltersFromQuery(char *query, char **filterOne, char **filterTwo)
147 char *queryDup = NULL;
148 char *restOfQuery = NULL;
149 char *keyValuePair = NULL;
150 int numKeyValuePairsParsed = 0;
155 queryDup = OICStrdup(query);
156 if (NULL == queryDup)
158 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
159 return OC_STACK_NO_MEMORY;
162 OIC_LOG_V(INFO, TAG, "Extracting params from %s", queryDup);
164 OCStackResult eCode = OC_STACK_INVALID_QUERY;
165 if (strnlen(queryDup, MAX_QUERY_LENGTH) >= MAX_QUERY_LENGTH)
167 OIC_LOG(ERROR, TAG, "Query exceeds maximum length.");
171 keyValuePair = strtok_r (queryDup, OC_QUERY_SEPARATOR, &restOfQuery);
175 if (numKeyValuePairsParsed >= 2)
177 OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
181 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
187 else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
189 *filterOne = value; // if
191 else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
193 *filterTwo = value; // rt
197 OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
200 ++numKeyValuePairsParsed;
202 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
207 *filterOne = OICStrdup(*filterOne);
208 if (NULL == *filterOne)
210 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
211 eCode = OC_STACK_NO_MEMORY;
218 *filterTwo = OICStrdup(*filterTwo);
219 if (NULL == *filterTwo)
221 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
223 eCode = OC_STACK_NO_MEMORY;
229 OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
239 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
241 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
243 return OC_WELL_KNOWN_URI;
245 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
247 return OC_DEVICE_URI;
249 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
251 return OC_PLATFORM_URI;
253 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
255 return OC_RESOURCE_TYPES_URI;
257 #ifdef ROUTING_GATEWAY
258 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
260 return OC_GATEWAY_URI;
264 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
268 #endif //WITH_PRESENCE
271 else if (0 == strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_MQ_URI))
273 return OC_MQ_BROKER_URI;
278 else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
280 return OC_KEEPALIVE_RESOURCE_URI;
284 return OC_UNKNOWN_URI;
287 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
288 char **filterOne, char **filterTwo)
290 if(!filterOne || !filterTwo)
292 return OC_STACK_INVALID_PARAM;
299 if (uri == OC_PRESENCE)
301 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
302 OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
307 OCStackResult result = OC_STACK_OK;
311 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
317 bool appendOCStringLL(OCRepPayload *device, OCStringLL *dmv)
320 for (OCStringLL *ll = dmv; ll; ll = ll->next, size++);
321 size_t dim[MAX_REP_ARRAY_DEPTH] = {size, 0, 0};
322 char **dt = (char **)OICMalloc(sizeof(char *) * size);
324 VERIFY_PARAM_NON_NULL(TAG, dt, "Data Model Version allocation failed.");
325 for (OCStringLL *ll = dmv; ll; ll = ll->next, i++)
327 dt[i] = OICStrdup(ll->value);
328 VERIFY_PARAM_NON_NULL(TAG, dt[i], "Data Model Version adding failed.");
330 if (!OCRepPayloadSetStringArrayAsOwner(device, OC_RSRVD_DATA_MODEL_VERSION, dt, dim))
337 for (int i = 0; i < size; i++)
345 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
346 OCRepPayload** payload, OCDevAddr *devAddr)
348 OCRepPayload *tempPayload = OCRepPayloadCreate();
352 OCRepPayloadDestroy(tempPayload);
353 return OC_STACK_INVALID_PARAM;
358 return OC_STACK_NO_MEMORY;
361 OCRepPayloadSetUri(tempPayload, resourcePtr->uri);
363 OCResourceType *resType = resourcePtr->rsrcType;
366 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
367 resType = resType->next;
370 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
373 OCRepPayloadAddInterface(tempPayload, resInterface->name);
374 resInterface = resInterface->next;
377 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
380 if (resAttrib->attrName && resAttrib->attrValue)
382 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
384 appendOCStringLL(tempPayload, (OCStringLL *)resAttrib->attrValue);
388 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
391 resAttrib = resAttrib->next;
394 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
395 OCRepPayload *policy = OCRepPayloadCreate();
398 OCPayloadDestroy((OCPayload *)tempPayload);
399 return OC_STACK_NO_MEMORY;
401 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
404 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
405 uint16_t securePort = 0;
406 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
410 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
412 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
416 *payload = tempPayload;
420 OCRepPayloadAppend(*payload, tempPayload);
426 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
427 OCDiscoveryPayload *payload,
429 CAEndpoint_t *networkInfo,
432 if (!resourcePtr || !payload || !networkInfo)
434 return OC_STACK_INVALID_PARAM;
436 uint16_t securePort = 0;
437 if (resourcePtr->resourceProperties & OC_SECURE)
439 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
445 bool isVirtual = false;
446 if (GetTypeOfVirtualURI(resourcePtr->uri) != OC_UNKNOWN_URI)
451 uint16_t tcpPort = 0;
452 if (GetTCPPortInfo(devAddr, &tcpPort) != OC_STACK_OK)
456 OCDiscoveryPayloadAddResourceWithEps(payload, resourcePtr, securePort,
457 isVirtual, networkInfo, infoSize, devAddr, tcpPort);
459 OCDiscoveryPayloadAddResourceWithEps(payload, resourcePtr, securePort,
460 isVirtual, networkInfo, infoSize, devAddr);
466 uint8_t IsCollectionResource (OCResource *resource)
473 if(resource->rsrcChildResourcesHead != NULL)
481 OCResource *FindResourceByUri(const char* resourceUri)
488 OCResource * pointer = headResource;
491 if (strcmp(resourceUri, pointer->uri) == 0)
495 pointer = pointer->next;
497 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
501 OCStackResult CheckRequestsEndpoint(const OCDevAddr *reqDevAddr,
502 OCTpsSchemeFlags resTpsFlags)
506 OIC_LOG(ERROR, TAG, "OCDevAddr* is NULL!!!");
507 return OC_STACK_INVALID_PARAM;
510 OCTpsSchemeFlags reqTpsFlags = OC_NO_TPS;
511 OCStackResult result = OCGetMatchedTpsFlags((CATransportAdapter_t)reqDevAddr->adapter,
512 (CATransportFlags_t)reqDevAddr->flags, &reqTpsFlags);
514 if (result != OC_STACK_OK)
516 OIC_LOG_V(ERROR, TAG, "Failed at get TPS flags. errcode is %d", result);
520 // bit compare between request tps flags and resource tps flags
521 if (reqTpsFlags & resTpsFlags)
523 OIC_LOG(INFO, TAG, "Request come from registered TPS");
528 OIC_LOG(ERROR, TAG, "Request come from unregistered TPS!!!");
529 return OC_STACK_BAD_ENDPOINT;
533 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
534 ResourceHandling *handling,
535 OCResource **resource)
537 if(!request || !handling || !resource)
539 return OC_STACK_INVALID_PARAM;
542 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
544 // Check if virtual resource
545 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
547 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
548 *handling = OC_RESOURCE_VIRTUAL;
549 *resource = headResource;
552 if (strlen((const char*)(request->resourceUrl)) == 0)
554 // Resource URL not specified
555 *handling = OC_RESOURCE_NOT_SPECIFIED;
556 return OC_STACK_NO_RESOURCE;
560 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
561 *resource = resourcePtr;
563 // Checking resource TPS flags if resource exist in stack.
566 OCStackResult result = CheckRequestsEndpoint(&(request->devAddr), resourcePtr->endpointType);
568 if (result != OC_STACK_OK)
570 if (result == OC_STACK_BAD_ENDPOINT)
572 OIC_LOG(ERROR, TAG, "Request come from bad endpoint. ignore request!!!");
573 return OC_STACK_BAD_ENDPOINT;
577 OIC_LOG_V(ERROR, TAG, "Failed at get tps flag errcode: %d", result);
585 if(defaultDeviceHandler)
587 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
591 // Resource does not exist
592 // and default device handler does not exist
593 *handling = OC_RESOURCE_NOT_SPECIFIED;
594 return OC_STACK_NO_RESOURCE;
597 if (IsCollectionResource (resourcePtr))
599 // Collection resource
600 if (resourcePtr->entityHandler != defaultResourceEHandler)
602 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
607 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
613 // Resource not a collection
614 if (resourcePtr->entityHandler != defaultResourceEHandler)
616 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
621 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
628 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
630 OCStackResult result;
637 result = OC_STACK_OK;
640 result = OC_STACK_SLOW_RESOURCE;
643 result = OC_STACK_ERROR;
645 case OC_EH_FORBIDDEN:
646 result = OC_STACK_FORBIDDEN_REQ;
648 case OC_EH_INTERNAL_SERVER_ERROR:
649 result = OC_STACK_INTERNAL_SERVER_ERROR;
651 case OC_EH_RESOURCE_CREATED:
652 result = OC_STACK_RESOURCE_CREATED;
654 case OC_EH_RESOURCE_DELETED:
655 result = OC_STACK_RESOURCE_DELETED;
658 result = OC_STACK_RESOURCE_CHANGED;
660 case OC_EH_RESOURCE_NOT_FOUND:
661 result = OC_STACK_NO_RESOURCE;
664 result = OC_STACK_ERROR;
670 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
677 // Null or empty is analogous to no filter.
678 if (resourceTypeFilter == NULL || *resourceTypeFilter == 0)
683 OCResourceType *resourceTypePtr = resource->rsrcType;
685 while (resourceTypePtr)
687 if (strcmp (resourceTypePtr->resourcetypename, resourceTypeFilter) == 0)
691 resourceTypePtr = resourceTypePtr->next;
694 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
698 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
705 // Null or empty is analogous to no filter.
706 if (interfaceFilter == NULL || *interfaceFilter == 0)
711 OCResourceInterface *interfacePtr = resource->rsrcInterface;
715 if (strcmp (interfacePtr->name, interfaceFilter) == 0 ||
716 strcmp (OC_RSRVD_INTERFACE_LL, interfaceFilter) == 0 ||
717 strcmp (OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter) == 0)
721 interfacePtr = interfacePtr->next;
724 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
729 * If the filters are null, they will be assumed to NOT be present
730 * and the resource will not be matched against them.
731 * Function will return true if all non null AND non empty filters passed in find a match.
733 static bool includeThisResourceInResponse(OCResource *resource,
734 char *interfaceFilter,
735 char *resourceTypeFilter)
739 OIC_LOG(ERROR, TAG, "Invalid resource");
743 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
746 * At least one valid filter should be available to
747 * include the resource in discovery response
749 if (!(resourceTypeFilter && *resourceTypeFilter))
751 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE \
752 resource", resource->uri);
756 else if ( !(resource->resourceProperties & OC_ACTIVE) ||
757 !(resource->resourceProperties & OC_DISCOVERABLE))
759 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
763 return resourceMatchesIFFilter(resource, interfaceFilter) &&
764 resourceMatchesRTFilter(resource, resourceTypeFilter);
768 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
769 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
771 OCEntityHandlerResponse response = {0};
773 response.ehResult = ehResult;
774 response.payload = discoveryPayload;
775 response.persistentBufferFlag = 0;
776 response.requestHandle = (OCRequestHandle) request;
777 response.resourceHandle = (OCResourceHandle) resource;
779 return OCDoResponse(&response);
783 * Find resource at the resource directory server. This resource is not local resource but a
786 * @param resource The resource to check the matching resource URI.
787 * @param interfaceQuery The interface query parameter.
788 * @param resourceTypeQuery The resourceType query parameter.
789 * @param discPayload The payload that will be added with the resource information if found at RD.
791 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
792 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
794 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
795 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
797 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
799 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
805 return OC_STACK_NO_RESOURCE;
810 * Creates a discovery payload and add device id information. This information is included in all
813 * @param payload payload that will have memory alllocated and device id information added.
815 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
816 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
818 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
822 OIC_LOG_V(DEBUG, TAG, "Payload is already allocated");
826 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
827 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
830 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
831 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
832 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
834 const char* uid = OCGetServerInstanceIDString();
837 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
843 OCPayloadDestroy(*payload);
844 return OC_STACK_NO_MEMORY;
848 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
850 * @param discPayload payload that will have the baseline information included.
852 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
853 * allocating the memory for the baseline information.
855 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
857 discPayload->uri = OICStrdup(OC_RSRVD_WELL_KNOWN_URI);
858 VERIFY_PARAM_NON_NULL(TAG, discPayload->uri, "Failed adding href to discovery payload.");
860 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, "deviceName", (void **)&discPayload->name);
862 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
863 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
864 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
865 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
867 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
868 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
869 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
874 return OC_STACK_NO_MEMORY;
877 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
879 if (!request || !resource)
881 return OC_STACK_INVALID_PARAM;
884 OCStackResult discoveryResult = OC_STACK_ERROR;
885 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
886 request->method == OC_REST_DELETE)
888 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
889 request->resourceUrl, request->method);
890 return OC_STACK_UNAUTHORIZED_REQ;
893 OCPayload* payload = NULL;
894 char *interfaceQuery = NULL;
895 char *resourceTypeQuery = NULL;
896 char *dataModelVersions = NULL;
898 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
900 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
902 // Step 1: Generate the response to discovery request
903 if (virtualUriInRequest == OC_WELL_KNOWN_URI
905 || virtualUriInRequest == OC_MQ_BROKER_URI
909 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
910 request->method == OC_REST_DELETE)
912 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
913 request->resourceUrl, request->method);
914 discoveryResult = OC_STACK_UNAUTHORIZED_REQ;
918 char *interfaceQuery = NULL;
919 char *resourceTypeQuery = NULL;
921 CAEndpoint_t *networkInfo = NULL;
922 uint32_t infoSize = 0;
924 CAResult_t caResult = CAGetNetworkInformation(&networkInfo, &infoSize);
925 if (CA_STATUS_OK != caResult || !networkInfo || infoSize == 0)
927 OIC_LOG(ERROR, TAG, "CAGetNetworkInformation has error on parsing network infomation");
928 return OC_STACK_ERROR;
931 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
932 &interfaceQuery, &resourceTypeQuery);
933 VERIFY_SUCCESS(discoveryResult);
934 if (!interfaceQuery && !resourceTypeQuery)
936 // If no query is sent, default interface is used i.e. oic.if.ll.
937 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
940 bool baselineQuery = false;
941 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
943 baselineQuery = true;
946 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
947 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
948 VERIFY_SUCCESS(discoveryResult);
950 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
953 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
954 VERIFY_SUCCESS(discoveryResult);
956 OCResourceProperty prop = OC_DISCOVERABLE;
958 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
960 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
962 discoveryResult = OC_STACK_NO_RESOURCE;
964 discoveryResult = findResourceAtRD(resource, interfaceQuery, resourceTypeQuery,
967 if (OC_STACK_NO_RESOURCE == discoveryResult)
969 // This case will handle when no resource type and it is oic.if.ll.
970 if (!resourceTypeQuery && !baselineQuery && (resource->resourceProperties & prop))
972 discoveryResult = BuildVirtualResourceResponse(resource,
978 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
980 discoveryResult = BuildVirtualResourceResponse(resource,
988 discoveryResult = OC_STACK_OK;
992 if (discPayload->resources == NULL)
994 discoveryResult = OC_STACK_NO_RESOURCE;
999 OICFree(networkInfo);
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 = BuildResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
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 = BuildResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
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);
1023 else if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
1025 // Received request for a keepalive
1026 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
1027 discoveryResult = HandleKeepAliveRequest(request, resource);
1031 * Step 2: Send the discovery response
1033 * Iotivity should respond to discovery requests in below manner:
1034 * 1)If query filter matching fails and discovery request is multicast,
1035 * it should NOT send any response.
1036 * 2)If query filter matching fails and discovery request is unicast,
1037 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1038 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1039 * request is multicast, it should NOT send any response.
1040 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1041 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1044 #ifdef WITH_PRESENCE
1045 if ((virtualUriInRequest == OC_PRESENCE) &&
1046 (resource->resourceProperties & OC_ACTIVE))
1048 // Need to send ACK when the request is CON.
1049 if (request->qos == OC_HIGH_QOS)
1051 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1052 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1053 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1054 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1056 FindAndDeleteServerRequest(request);
1058 // Presence uses observer notification api to respond via SendPresenceNotification.
1059 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1064 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1065 if (OC_GATEWAY_URI != virtualUriInRequest)
1069 // KeepAlive uses the HandleKeepAliveRequest to respond to the request.
1070 if (OC_KEEPALIVE_RESOURCE_URI != virtualUriInRequest)
1073 OIC_LOG_PAYLOAD(DEBUG, payload);
1074 if(discoveryResult == OC_STACK_OK)
1076 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
1078 else if(((request->devAddr.flags & OC_MULTICAST) == false) &&
1079 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
1080 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE))
1082 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
1083 discoveryResult, virtualUriInRequest);
1084 SendNonPersistantDiscoveryResponse(request, resource, NULL,
1085 (discoveryResult == OC_STACK_NO_RESOURCE) ?
1086 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1090 // Ignoring the discovery request as per RFC 7252, Section #8.2
1091 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1092 // the request should be removed.
1093 // since it never remove and causes a big memory waste.
1094 FindAndDeleteServerRequest(request);
1102 OICFree(interfaceQuery);
1105 if (resourceTypeQuery)
1107 OICFree(resourceTypeQuery);
1109 OCPayloadDestroy(payload);
1110 if (dataModelVersions)
1112 OICFree(dataModelVersions);
1114 return discoveryResult;
1117 static OCStackResult
1118 HandleDefaultDeviceEntityHandler (OCServerRequest *request)
1122 return OC_STACK_INVALID_PARAM;
1125 OCStackResult result = OC_STACK_OK;
1126 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1127 OCEntityHandlerRequest ehRequest = {0};
1129 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1130 result = FormOCEntityHandlerRequest(&ehRequest,
1131 (OCRequestHandle) request,
1134 (OCResourceHandle) NULL, request->query,
1135 PAYLOAD_TYPE_REPRESENTATION,
1137 request->payloadSize,
1138 request->numRcvdVendorSpecificHeaderOptions,
1139 request->rcvdVendorSpecificHeaderOptions,
1140 (OCObserveAction)request->observationOption,
1143 VERIFY_SUCCESS(result);
1145 // At this point we know for sure that defaultDeviceHandler exists
1146 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1147 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1148 if(ehResult == OC_EH_SLOW)
1150 OIC_LOG(INFO, TAG, "This is a slow resource");
1151 request->slowFlag = 1;
1153 else if(ehResult == OC_EH_ERROR)
1155 FindAndDeleteServerRequest(request);
1157 result = EntityHandlerCodeToOCStackCode(ehResult);
1159 OCPayloadDestroy(ehRequest.payload);
1163 static OCStackResult
1164 HandleResourceWithEntityHandler (OCServerRequest *request,
1165 OCResource *resource,
1166 uint8_t collectionResource)
1168 OC_UNUSED(collectionResource);
1170 if(!request || ! resource)
1172 return OC_STACK_INVALID_PARAM;
1175 OCStackResult result = OC_STACK_ERROR;
1176 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1177 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1178 ResourceObserver *resObs = NULL;
1180 OCEntityHandlerRequest ehRequest = {0};
1182 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1183 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1184 // check the security resource
1185 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1187 type = PAYLOAD_TYPE_SECURITY;
1191 result = FormOCEntityHandlerRequest(&ehRequest,
1192 (OCRequestHandle)request,
1195 (OCResourceHandle)resource,
1199 request->payloadSize,
1200 request->numRcvdVendorSpecificHeaderOptions,
1201 request->rcvdVendorSpecificHeaderOptions,
1202 (OCObserveAction)request->observationOption,
1205 VERIFY_SUCCESS(result);
1207 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1209 OIC_LOG(INFO, TAG, "No observation requested");
1210 ehFlag = OC_REQUEST_FLAG;
1212 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1214 OIC_LOG(INFO, TAG, "Observation registration requested");
1216 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1217 request->tokenLength);
1221 OIC_LOG (INFO, TAG, "Observer with this token already present");
1222 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1223 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1224 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1226 // server requests are usually free'd when the response is sent out
1227 // for the request in ocserverrequest.c : HandleSingleResponse()
1228 // Since we are making an early return and not responding, the server request
1229 // needs to be deleted.
1230 FindAndDeleteServerRequest (request);
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_ERROR;
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;
1313 else if(ehResult == OC_EH_ERROR)
1315 FindAndDeleteServerRequest(request);
1317 result = EntityHandlerCodeToOCStackCode(ehResult);
1319 OCPayloadDestroy(ehRequest.payload);
1323 static OCStackResult
1324 HandleCollectionResourceDefaultEntityHandler (OCServerRequest *request,
1325 OCResource *resource)
1327 if(!request || !resource)
1329 return OC_STACK_INVALID_PARAM;
1332 OCStackResult result = OC_STACK_ERROR;
1333 OCEntityHandlerRequest ehRequest = {0};
1335 result = FormOCEntityHandlerRequest(&ehRequest,
1336 (OCRequestHandle)request,
1339 (OCResourceHandle)resource,
1341 PAYLOAD_TYPE_REPRESENTATION,
1343 request->payloadSize,
1344 request->numRcvdVendorSpecificHeaderOptions,
1345 request->rcvdVendorSpecificHeaderOptions,
1346 (OCObserveAction)request->observationOption,
1349 if(result == OC_STACK_OK)
1351 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1354 OCPayloadDestroy(ehRequest.payload);
1359 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1361 OCStackResult ret = OC_STACK_OK;
1363 switch (resHandling)
1365 case OC_RESOURCE_VIRTUAL:
1367 ret = HandleVirtualResource (request, resource);
1370 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1372 ret = HandleDefaultDeviceEntityHandler(request);
1375 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1377 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1378 return OC_STACK_ERROR;
1380 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1382 ret = HandleResourceWithEntityHandler (request, resource, 0);
1385 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1387 ret = HandleResourceWithEntityHandler (request, resource, 1);
1390 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1392 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1395 case OC_RESOURCE_NOT_SPECIFIED:
1397 ret = OC_STACK_NO_RESOURCE;
1402 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1403 return OC_STACK_ERROR;
1409 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1411 OCResource *resource = NULL;
1412 if (!info.platformID || !info.manufacturerName)
1414 OIC_LOG(ERROR, TAG, "No value specified.");
1417 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1419 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1422 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1423 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1424 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1425 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1426 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1427 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1428 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1429 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1431 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1435 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1438 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1441 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1442 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1443 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1444 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1445 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1446 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1447 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1448 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1449 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1450 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1451 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1452 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1453 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1457 return OC_STACK_INVALID_PARAM;
1460 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1462 OCStringLL *dataModelVersion = NULL;
1463 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1466 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1469 if (!info.deviceName || info.deviceName[0] == '\0')
1471 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1472 return OC_STACK_INVALID_PARAM;
1475 if (OCGetServerInstanceIDString() == NULL)
1477 OIC_LOG(INFO, TAG, "Device ID generation failed");
1481 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1482 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1486 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1489 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1490 info.specVersion: OC_SPEC_VERSION));
1491 if (info.dataModelVersions)
1493 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1497 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1498 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1500 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1504 if (dataModelVersion)
1506 OCFreeOCStringLL(dataModelVersion);
1508 return OC_STACK_ERROR;
1511 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1513 if (!resource || !attribute)
1515 return OC_STACK_INVALID_PARAM;
1517 if (0 == strlen(attribute))
1519 return OC_STACK_INVALID_PARAM;
1521 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1523 if (0 == strcmp(attribute, temp->attrName))
1525 // A special case as this type return OCStringLL
1526 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1528 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1533 *value = OICStrdup((char *)temp->attrValue);
1538 return OC_STACK_NO_RESOURCE;
1541 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1543 if (!prop || *value)
1545 return OC_STACK_INVALID_PARAM;
1547 if (strlen(prop) == 0)
1549 return OC_STACK_INVALID_PARAM;
1551 OCStackResult res = OC_STACK_NO_RESOURCE;
1552 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1554 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1555 OCResource *resource = FindResourceByUri(pathType);
1558 return OC_STACK_NO_RESOURCE;
1561 res = OCGetAttribute(resource, prop, value);
1566 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1568 // See if the attribute already exists in the list.
1569 OCAttribute *resAttrib;
1570 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1572 if (0 == strcmp(attribute, resAttrib->attrName))
1574 // Found, free the old value.
1575 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1577 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1581 OICFree((char *)resAttrib->attrValue);
1587 // If not already in the list, add it.
1588 if (NULL == resAttrib)
1590 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1591 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1592 resAttrib->attrName = OICStrdup(attribute);
1593 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1594 resAttrib->next = resource->rsrcAttributes;
1595 resource->rsrcAttributes = resAttrib;
1598 // Fill in the new value.
1599 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1601 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1605 resAttrib->attrValue = OICStrdup((char *)value);
1607 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1612 OCDeleteResourceAttributes(resAttrib);
1613 return OC_STACK_NO_MEMORY;
1617 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1621 return OC_STACK_INVALID_PARAM;
1623 if (strlen(prop) == 0)
1625 return OC_STACK_INVALID_PARAM;
1628 OCStackResult res = OC_STACK_ERROR;
1629 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1631 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1632 OCResource *resource = FindResourceByUri(pathType);
1635 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1639 res = OCSetAttribute(resource, prop, value);