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 OCPayloadDestroy((OCPayload *)tempPayload);
366 return OC_STACK_ERROR;
368 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_DEVICE_ID, deviceId);
371 OCResourceType *resType = resourcePtr->rsrcType;
374 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
375 resType = resType->next;
378 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
381 OCRepPayloadAddInterface(tempPayload, resInterface->name);
382 resInterface = resInterface->next;
385 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
388 if (resAttrib->attrName && resAttrib->attrValue)
390 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
392 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
395 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
401 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
404 resAttrib = resAttrib->next;
409 *payload = tempPayload;
413 OCRepPayloadAppend(*payload, tempPayload);
419 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
420 OCRepPayload** payload, OCDevAddr *devAddr)
424 return OC_STACK_INVALID_PARAM;
427 OCRepPayload *tempPayload = OCRepPayloadCreate();
430 return OC_STACK_NO_MEMORY;
433 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_HREF, resourcePtr->uri);
435 uint8_t numElement = 0;
436 if (OC_STACK_OK == OCGetNumberOfResourceTypes((OCResource *)resourcePtr, &numElement))
438 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
439 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
442 OIC_LOG(ERROR, TAG, "Resource type allocation failed.");
443 OCRepPayloadDestroy(tempPayload);
444 return OC_STACK_NO_MEMORY;
446 for (uint8_t i = 0; i < numElement; ++i)
448 const char *value = OCGetResourceTypeName((OCResource *)resourcePtr, i);
449 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
450 rt[i] = OICStrdup(value);
452 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_RESOURCE_TYPE, rt, rtDim);
456 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces((OCResource *)resourcePtr, &numElement))
458 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
459 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
462 OIC_LOG(ERROR, TAG, "Resource interface allocation failed.");
463 OCRepPayloadDestroy(tempPayload);
464 return OC_STACK_NO_MEMORY;
466 for (uint8_t i = 0; i < numElement; ++i)
468 const char *value = OCGetResourceInterfaceName((OCResource *)resourcePtr, i);
469 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
470 itf[i] = OICStrdup(value);
472 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
475 for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
477 if (resAttrib->attrName && resAttrib->attrValue)
479 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
481 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
484 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
490 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
497 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
498 OCRepPayload *policy = OCRepPayloadCreate();
501 OCPayloadDestroy((OCPayload *)tempPayload);
502 return OC_STACK_NO_MEMORY;
504 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
507 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
508 uint16_t securePort = 0;
509 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
513 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
515 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
520 *payload = tempPayload;
524 OCRepPayloadAppend(*payload, tempPayload);
530 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
531 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
533 if (!resourcePtr || !payload)
535 return OC_STACK_INVALID_PARAM;
537 uint16_t securePort = 0;
538 if (resourcePtr->resourceProperties & OC_SECURE)
540 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
546 #if defined(TCP_ADAPTER) && !defined(DISABLE_TCP_SERVER)
547 uint16_t tcpPort = 0;
548 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
550 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
552 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
558 OCResource *FindResourceByUri(const char* resourceUri)
565 OCResource * pointer = headResource;
568 if (strcmp(resourceUri, pointer->uri) == 0)
572 pointer = pointer->next;
574 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
578 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
579 ResourceHandling *handling,
580 OCResource **resource)
582 if(!request || !handling || !resource)
584 return OC_STACK_INVALID_PARAM;
587 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
589 // Check if virtual resource
590 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
592 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
593 *handling = OC_RESOURCE_VIRTUAL;
594 *resource = headResource;
597 if (strlen((const char*)(request->resourceUrl)) == 0)
599 // Resource URL not specified
600 *handling = OC_RESOURCE_NOT_SPECIFIED;
601 return OC_STACK_NO_RESOURCE;
605 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
606 *resource = resourcePtr;
609 if(defaultDeviceHandler)
611 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
615 // Resource does not exist
616 // and default device handler does not exist
617 *handling = OC_RESOURCE_NOT_SPECIFIED;
618 return OC_STACK_NO_RESOURCE;
621 if (resourcePtr && resourcePtr->rsrcChildResourcesHead != NULL)
623 // Collection resource
624 if (resourcePtr->entityHandler != defaultResourceEHandler)
626 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
631 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
637 // Resource not a collection
638 if (resourcePtr->entityHandler != defaultResourceEHandler)
640 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
645 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
652 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
654 OCStackResult result;
661 result = OC_STACK_OK;
664 result = OC_STACK_SLOW_RESOURCE;
667 result = OC_STACK_ERROR;
669 case OC_EH_FORBIDDEN:
670 result = OC_STACK_FORBIDDEN_REQ;
672 case OC_EH_RESOURCE_CREATED:
673 result = OC_STACK_RESOURCE_CREATED;
675 case OC_EH_RESOURCE_DELETED:
676 result = OC_STACK_RESOURCE_DELETED;
679 result = OC_STACK_RESOURCE_CHANGED;
681 case OC_EH_RESOURCE_NOT_FOUND:
682 result = OC_STACK_NO_RESOURCE;
684 case OC_EH_TOO_MANY_REQUESTS:
685 result = OC_STACK_TOO_MANY_REQUESTS;
687 case OC_EH_INTERNAL_SERVER_ERROR:
688 result = OC_STACK_INTERNAL_SERVER_ERROR;
690 case OC_EH_NOT_IMPLEMENTED:
691 result = OC_STACK_NOT_IMPLEMENTED;
693 case OC_EH_BAD_GATEWAY:
694 result = OC_STACK_BAD_GATEWAY;
696 case OC_EH_SERVICE_UNAVAILABLE:
697 result = OC_STACK_SERVICE_UNAVAILABLE;
699 case OC_EH_RETRANSMIT_TIMEOUT:
700 result = OC_STACK_GATEWAY_TIMEOUT;
702 case OC_EH_PROXY_NOT_SUPPORTED:
703 result = OC_STACK_PROXY_NOT_SUPPORTED;
706 result = OC_STACK_ERROR;
712 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
719 // Null is analogous to no filter.
720 if (NULL == resourceTypeFilter)
725 // Empty resourceType filter is analogous to error query
726 if (0 == strlen(resourceTypeFilter))
731 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
733 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
739 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
743 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
750 // Null is analogous to no filter.
751 if (NULL == interfaceFilter)
756 // Empty interface filter is analogous to error query
757 if (0 == strlen(interfaceFilter))
762 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
764 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
765 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
766 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
772 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
777 * If the filters are null, they will be assumed to NOT be present
778 * and the resource will not be matched against them.
779 * Function will return true if all non null AND non empty filters passed in find a match.
781 static bool includeThisResourceInResponse(OCResource *resource,
782 char *interfaceFilter,
783 char *resourceTypeFilter)
787 OIC_LOG(ERROR, TAG, "Invalid resource");
791 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
794 * At least one valid filter should be available to
795 * include the resource in discovery response
797 if (!(resourceTypeFilter && *resourceTypeFilter))
799 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
800 resource", resource->uri);
804 else if (!(resource->resourceProperties & OC_ACTIVE) ||
805 !(resource->resourceProperties & OC_DISCOVERABLE))
807 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
811 return resourceMatchesIFFilter(resource, interfaceFilter) &&
812 resourceMatchesRTFilter(resource, resourceTypeFilter);
815 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
816 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
818 OCEntityHandlerResponse response = {0};
820 response.ehResult = ehResult;
821 response.payload = discoveryPayload;
822 response.persistentBufferFlag = 0;
823 response.requestHandle = (OCRequestHandle) request->requestId;
824 response.resourceHandle = (OCResourceHandle) resource;
826 return OCDoResponse(&response);
829 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
830 OCServerRequest *request, OCResource *resource)
832 return FormOCEntityHandlerRequest(ehRequest,
833 (OCRequestHandle)request->requestId,
836 (OCResourceHandle)resource,
840 request->payloadSize,
841 request->numRcvdVendorSpecificHeaderOptions,
842 request->rcvdVendorSpecificHeaderOptions,
843 (OCObserveAction)request->observationOption,
850 * Find resource at the resource directory server. This resource is not local resource but a
853 * @param resource The resource to check the matching resource URI.
854 * @param interfaceQuery The interface query parameter.
855 * @param resourceTypeQuery The resourceType query parameter.
856 * @param discPayload The payload that will be added with the resource information if found at RD.
858 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
859 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
861 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
862 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
864 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
866 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
872 return OC_STACK_NO_RESOURCE;
877 * Creates a discovery payload and add device id information. This information is included in all
880 * @param payload payload that will have memory alllocated and device id information added.
882 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
883 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
885 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
889 OIC_LOG(DEBUG, TAG, "Payload is already allocated");
893 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
894 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
897 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
898 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
899 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
901 const char* uid = OCGetServerInstanceIDString();
904 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
910 OCPayloadDestroy(*payload);
911 return OC_STACK_NO_MEMORY;
915 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
917 * @param discPayload payload that will have the baseline information included.
919 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
920 * allocating the memory for the baseline information.
922 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
926 OIC_LOG(ERROR, TAG, "Payload is not allocated");
927 return OC_STACK_ERROR;
930 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **)&discPayload->name);
932 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
933 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
934 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
935 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
937 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
938 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
939 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
944 return OC_STACK_NO_MEMORY;
947 static bool isUnicast(OCServerRequest *request)
949 bool isMulticast = request->devAddr.flags & OC_MULTICAST;
950 return (isMulticast == false &&
951 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
952 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE));
955 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
957 if (!request || !resource)
959 return OC_STACK_INVALID_PARAM;
962 OCPayload* payload = NULL;
963 char *interfaceQuery = NULL;
964 char *resourceTypeQuery = NULL;
966 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
968 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
971 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
973 // Received request for a keepalive
974 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
975 return OCHandleKeepAliveRequest(request, resource);
979 OCStackResult discoveryResult = OC_STACK_ERROR;
980 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
981 request->method == OC_REST_DELETE)
983 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
984 request->resourceUrl, request->method);
985 return OC_STACK_UNAUTHORIZED_REQ;
988 // Step 1: Generate the response to discovery request
989 if (virtualUriInRequest == OC_WELL_KNOWN_URI
991 || virtualUriInRequest == OC_MQ_BROKER_URI
995 discoveryResult = getQueryParamsForFiltering(virtualUriInRequest, request->query,
996 &interfaceQuery, &resourceTypeQuery);
997 VERIFY_SUCCESS(discoveryResult);
999 if (!interfaceQuery && !resourceTypeQuery)
1001 // If no query is sent, default interface is used i.e. oic.if.ll.
1002 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
1005 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
1006 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
1007 VERIFY_SUCCESS(discoveryResult);
1009 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
1010 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
1012 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
1013 VERIFY_SUCCESS(discoveryResult);
1015 OCResourceProperty prop = OC_DISCOVERABLE;
1017 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
1019 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
1021 // This case will handle when no resource type and it is oic.if.ll.
1022 // Do not assume check if the query is ll
1023 if (!resourceTypeQuery &&
1024 (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
1026 // Only include discoverable type
1027 if (resource->resourceProperties & prop)
1029 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1032 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
1034 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1038 discoveryResult = OC_STACK_OK;
1041 if (discPayload->resources == NULL)
1043 discoveryResult = OC_STACK_NO_RESOURCE;
1046 else if (virtualUriInRequest == OC_DEVICE_URI)
1048 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1049 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1050 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1052 else if (virtualUriInRequest == OC_PLATFORM_URI)
1054 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1055 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1056 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1058 #ifdef ROUTING_GATEWAY
1059 else if (OC_GATEWAY_URI == virtualUriInRequest)
1061 // Received request for a gateway
1062 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1063 discoveryResult = RMHandleGatewayRequest(request, resource);
1068 * Step 2: Send the discovery response
1070 * Iotivity should respond to discovery requests in below manner:
1071 * 1)If query filter matching fails and discovery request is multicast,
1072 * it should NOT send any response.
1073 * 2)If query filter matching fails and discovery request is unicast,
1074 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1075 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1076 * request is multicast, it should NOT send any response.
1077 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1078 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1081 #ifdef WITH_PRESENCE
1082 if ((virtualUriInRequest == OC_PRESENCE) &&
1083 (resource->resourceProperties & OC_ACTIVE))
1085 // Need to send ACK when the request is CON.
1086 if (request->qos == OC_HIGH_QOS)
1088 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1089 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1090 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1091 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1093 FindAndDeleteServerRequest(request);
1095 // Presence uses observer notification api to respond via SendPresenceNotification.
1096 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1101 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1102 if (OC_GATEWAY_URI != virtualUriInRequest)
1105 OIC_LOG_PAYLOAD(DEBUG, payload);
1106 if(discoveryResult == OC_STACK_OK)
1109 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
1111 else // Error handling
1113 if (isUnicast(request))
1115 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
1116 discoveryResult, virtualUriInRequest);
1117 SendNonPersistantDiscoveryResponse(request, resource, NULL,
1118 (discoveryResult == OC_STACK_NO_RESOURCE) ?
1119 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1123 // Ignoring the discovery request as per RFC 7252, Section #8.2
1124 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1125 // the request should be removed.
1126 // since it never remove and causes a big memory waste.
1127 FindAndDeleteServerRequest(request);
1129 discoveryResult = OC_STACK_CONTINUE;
1136 OICFree(interfaceQuery);
1139 if (resourceTypeQuery)
1141 OICFree(resourceTypeQuery);
1143 OCPayloadDestroy(payload);
1145 // To ignore the message, OC_STACK_CONTINUE is sent
1146 return discoveryResult;
1149 static OCStackResult
1150 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
1154 return OC_STACK_INVALID_PARAM;
1157 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1158 OCEntityHandlerRequest ehRequest = {0};
1159 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1160 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
1161 VERIFY_SUCCESS(result);
1163 // At this point we know for sure that defaultDeviceHandler exists
1164 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1165 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1166 if(ehResult == OC_EH_SLOW)
1168 OIC_LOG(INFO, TAG, "This is a slow resource");
1169 request->slowFlag = 1;
1172 result = EntityHandlerCodeToOCStackCode(ehResult);
1174 // Clear server request on error case
1175 if (!OCResultToSuccess(result))
1177 FindAndDeleteServerRequest(request);
1181 OCPayloadDestroy(ehRequest.payload);
1185 static OCStackResult
1186 HandleResourceWithEntityHandler(OCServerRequest *request,
1187 OCResource *resource)
1189 if(!request || ! resource)
1191 return OC_STACK_INVALID_PARAM;
1194 OCStackResult result = OC_STACK_ERROR;
1195 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1196 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1197 ResourceObserver *resObs = NULL;
1199 OCEntityHandlerRequest ehRequest = {0};
1201 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1202 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1203 // check the security resource
1204 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1206 type = PAYLOAD_TYPE_SECURITY;
1209 result = EHRequest(&ehRequest, type, request, resource);
1210 VERIFY_SUCCESS(result);
1212 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1214 OIC_LOG(INFO, TAG, "No observation requested");
1215 ehFlag = OC_REQUEST_FLAG;
1217 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1219 OIC_LOG(INFO, TAG, "Observation registration requested");
1221 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1222 request->tokenLength);
1226 OIC_LOG (INFO, TAG, "Observer with this token already present");
1227 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1228 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1229 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1235 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1236 VERIFY_SUCCESS(result);
1238 result = AddObserver ((const char*)(request->resourceUrl),
1239 (const char *)(request->query),
1240 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1241 resource, request->qos, request->acceptFormat,
1244 if(result == OC_STACK_OK)
1246 OIC_LOG(INFO, TAG, "Added observer successfully");
1247 request->observeResult = OC_STACK_OK;
1248 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1250 else if (result == OC_STACK_RESOURCE_ERROR)
1252 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1253 request->observeResult = OC_STACK_ERROR;
1254 ehFlag = OC_REQUEST_FLAG;
1258 // The error in observeResult for the request will be used when responding to this
1259 // request by omitting the observation option/sequence number.
1260 request->observeResult = OC_STACK_ERROR;
1261 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1262 ehFlag = OC_REQUEST_FLAG;
1263 FindAndDeleteServerRequest(request);
1268 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1270 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1272 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1276 // Stack does not contain this observation request
1277 // Either token is incorrect or observation list is corrupted
1278 result = OC_STACK_NO_RESOURCE;
1281 ehRequest.obsInfo.obsId = resObs->observeId;
1282 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1284 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1286 if(result == OC_STACK_OK)
1288 OIC_LOG(INFO, TAG, "Removed observer successfully");
1289 request->observeResult = OC_STACK_OK;
1290 // There should be no observe option header for de-registration response.
1291 // Set as an invalid value here so we can detect it later and remove the field in response.
1292 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1296 request->observeResult = OC_STACK_ERROR;
1297 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1298 FindAndDeleteServerRequest(request);
1304 result = OC_STACK_ERROR;
1308 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1309 if(ehResult == OC_EH_SLOW)
1311 OIC_LOG(INFO, TAG, "This is a slow resource");
1312 request->slowFlag = 1;
1315 result = EntityHandlerCodeToOCStackCode(ehResult);
1317 // Clear server request on error case
1318 if (!OCResultToSuccess(result))
1320 FindAndDeleteServerRequest(request);
1324 OCPayloadDestroy(ehRequest.payload);
1325 FreeObserver(resObs);
1329 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
1330 OCResource *resource)
1332 if (!request || !resource)
1334 return OC_STACK_INVALID_PARAM;
1337 OCEntityHandlerRequest ehRequest = {0};
1338 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
1339 if(result == OC_STACK_OK)
1341 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1344 OCPayloadDestroy(ehRequest.payload);
1349 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1351 OCStackResult ret = OC_STACK_OK;
1353 switch (resHandling)
1355 case OC_RESOURCE_VIRTUAL:
1357 ret = HandleVirtualResource (request, resource);
1360 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1362 ret = HandleDefaultDeviceEntityHandler(request);
1365 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1367 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1368 return OC_STACK_ERROR;
1370 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1372 ret = HandleResourceWithEntityHandler (request, resource);
1375 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1377 ret = HandleResourceWithEntityHandler (request, resource);
1380 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1382 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1385 case OC_RESOURCE_NOT_SPECIFIED:
1387 ret = OC_STACK_NO_RESOURCE;
1392 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1393 return OC_STACK_ERROR;
1399 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1401 OCResource *resource = NULL;
1402 if (!info.platformID || !info.manufacturerName)
1404 OIC_LOG(ERROR, TAG, "No value specified.");
1407 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1409 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1412 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1413 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1414 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1415 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1416 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1417 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1418 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1419 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1421 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1425 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1428 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1431 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1432 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1433 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1434 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1435 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1436 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1437 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1438 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1439 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1440 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1441 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1442 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1443 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1447 return OC_STACK_INVALID_PARAM;
1450 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1452 OCStringLL *dataModelVersion = NULL;
1453 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1456 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1459 if (!info.deviceName || info.deviceName[0] == '\0')
1461 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1462 return OC_STACK_INVALID_PARAM;
1465 if (OCGetServerInstanceIDString() == NULL)
1467 OIC_LOG(INFO, TAG, "Device ID generation failed");
1471 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1472 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1476 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1479 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1480 info.specVersion: OC_SPEC_VERSION));
1481 if (info.dataModelVersions)
1483 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1487 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1488 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1490 OCFreeOCStringLL(dataModelVersion);
1493 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1497 if (dataModelVersion)
1499 OCFreeOCStringLL(dataModelVersion);
1501 return OC_STACK_ERROR;
1504 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1506 if (!resource || !attribute)
1508 return OC_STACK_INVALID_PARAM;
1510 if (0 == strlen(attribute))
1512 return OC_STACK_INVALID_PARAM;
1514 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1516 if (0 == strcmp(attribute, temp->attrName))
1518 // A special case as this type return OCStringLL
1519 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1521 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1526 *value = OICStrdup((char *)temp->attrValue);
1531 return OC_STACK_NO_RESOURCE;
1534 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1536 if (!prop || *value)
1538 return OC_STACK_INVALID_PARAM;
1540 if (strlen(prop) == 0)
1542 return OC_STACK_INVALID_PARAM;
1544 OCStackResult res = OC_STACK_NO_RESOURCE;
1545 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1547 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1548 OCResource *resource = FindResourceByUri(pathType);
1551 return OC_STACK_NO_RESOURCE;
1554 res = OCGetAttribute(resource, prop, value);
1559 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1561 // See if the attribute already exists in the list.
1562 OCAttribute *resAttrib;
1563 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1565 if (0 == strcmp(attribute, resAttrib->attrName))
1567 // Found, free the old value.
1568 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1570 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1574 OICFree((char *)resAttrib->attrValue);
1580 // If not already in the list, add it.
1581 if (NULL == resAttrib)
1583 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1584 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1585 resAttrib->attrName = OICStrdup(attribute);
1586 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1587 resAttrib->next = resource->rsrcAttributes;
1588 resource->rsrcAttributes = resAttrib;
1591 // Fill in the new value.
1592 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1594 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1598 resAttrib->attrValue = OICStrdup((char *)value);
1600 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1605 OCDeleteResourceAttributes(resAttrib);
1606 return OC_STACK_NO_MEMORY;
1610 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1612 if (!prop || !value)
1614 return OC_STACK_INVALID_PARAM;
1616 if (strlen(prop) == 0)
1618 return OC_STACK_INVALID_PARAM;
1621 OCStackResult res = OC_STACK_ERROR;
1622 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1624 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1625 OCResource *resource = FindResourceByUri(pathType);
1628 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1632 res = OCSetAttribute(resource, prop, value);