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 // TODO: headResource needs to be accessed in a thread-safe manner
559 OCResource *FindResourceByUri(const char* resourceUri)
566 OCResource * pointer = headResource;
569 if (pointer->uri && strcmp(resourceUri, pointer->uri) == 0)
573 pointer = pointer->next;
575 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
579 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
580 ResourceHandling *handling,
581 OCResource **resource)
583 if(!request || !handling || !resource)
585 return OC_STACK_INVALID_PARAM;
588 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
590 // Check if virtual resource
591 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
593 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
594 *handling = OC_RESOURCE_VIRTUAL;
595 *resource = headResource;
598 if (strlen((const char*)(request->resourceUrl)) == 0)
600 // Resource URL not specified
601 *handling = OC_RESOURCE_NOT_SPECIFIED;
602 return OC_STACK_NO_RESOURCE;
606 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
607 *resource = resourcePtr;
610 if(defaultDeviceHandler)
612 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
616 // Resource does not exist
617 // and default device handler does not exist
618 *handling = OC_RESOURCE_NOT_SPECIFIED;
619 return OC_STACK_NO_RESOURCE;
622 if (resourcePtr->rsrcChildResourcesHead != NULL)
624 // Collection resource
625 if (resourcePtr->entityHandler != defaultResourceEHandler)
627 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
632 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
638 // Resource not a collection
639 if (resourcePtr->entityHandler != defaultResourceEHandler)
641 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
646 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
653 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
655 OCStackResult result;
662 result = OC_STACK_OK;
665 result = OC_STACK_SLOW_RESOURCE;
668 result = OC_STACK_ERROR;
670 case OC_EH_FORBIDDEN:
671 result = OC_STACK_FORBIDDEN_REQ;
673 case OC_EH_RESOURCE_CREATED:
674 result = OC_STACK_RESOURCE_CREATED;
676 case OC_EH_RESOURCE_DELETED:
677 result = OC_STACK_RESOURCE_DELETED;
680 result = OC_STACK_RESOURCE_CHANGED;
682 case OC_EH_RESOURCE_NOT_FOUND:
683 result = OC_STACK_NO_RESOURCE;
685 case OC_EH_TOO_MANY_REQUESTS:
686 result = OC_STACK_TOO_MANY_REQUESTS;
688 case OC_EH_INTERNAL_SERVER_ERROR:
689 result = OC_STACK_INTERNAL_SERVER_ERROR;
691 case OC_EH_NOT_IMPLEMENTED:
692 result = OC_STACK_NOT_IMPLEMENTED;
694 case OC_EH_BAD_GATEWAY:
695 result = OC_STACK_BAD_GATEWAY;
697 case OC_EH_SERVICE_UNAVAILABLE:
698 result = OC_STACK_SERVICE_UNAVAILABLE;
700 case OC_EH_RETRANSMIT_TIMEOUT:
701 result = OC_STACK_GATEWAY_TIMEOUT;
703 case OC_EH_PROXY_NOT_SUPPORTED:
704 result = OC_STACK_PROXY_NOT_SUPPORTED;
707 result = OC_STACK_INVALID_QUERY;
709 case OC_EH_UNAUTHORIZED_REQ:
710 result = OC_STACK_UNAUTHORIZED_REQ;
713 result = OC_STACK_INVALID_OPTION;
715 case OC_EH_TOO_LARGE:
716 result = OC_STACK_TOO_LARGE_REQ;
718 case OC_EH_NOT_ACCEPTABLE:
719 result = OC_STACK_NOT_ACCEPTABLE;
721 case OC_EH_METHOD_NOT_ALLOWED:
722 result = OC_STACK_METHOD_NOT_ALLOWED;
725 result = OC_STACK_ERROR;
731 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
738 // Null is analogous to no filter.
739 if (NULL == resourceTypeFilter)
744 // Empty resourceType filter is analogous to error query
745 if (0 == strlen(resourceTypeFilter))
750 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
752 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
758 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
762 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
769 // Null is analogous to no filter.
770 if (NULL == interfaceFilter)
775 // Empty interface filter is analogous to error query
776 if (0 == strlen(interfaceFilter))
781 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
783 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
784 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
785 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
791 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
796 * If the filters are null, they will be assumed to NOT be present
797 * and the resource will not be matched against them.
798 * Function will return true if all non null AND non empty filters passed in find a match.
800 static bool includeThisResourceInResponse(OCResource *resource,
801 char *interfaceFilter,
802 char *resourceTypeFilter)
806 OIC_LOG(ERROR, TAG, "Invalid resource");
810 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
813 * At least one valid filter should be available to
814 * include the resource in discovery response
816 if (!(resourceTypeFilter && *resourceTypeFilter))
818 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
819 resource", resource->uri);
823 else if (!(resource->resourceProperties & OC_ACTIVE) ||
824 !(resource->resourceProperties & OC_DISCOVERABLE))
826 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
830 return resourceMatchesIFFilter(resource, interfaceFilter) &&
831 resourceMatchesRTFilter(resource, resourceTypeFilter);
834 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
835 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
837 OCEntityHandlerResponse response = {0};
839 response.ehResult = ehResult;
840 response.payload = discoveryPayload;
841 response.persistentBufferFlag = 0;
842 response.requestHandle = (OCRequestHandle) request->requestId;
843 response.resourceHandle = (OCResourceHandle) resource;
845 return OCDoResponse(&response);
848 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
849 OCServerRequest *request, OCResource *resource)
851 return FormOCEntityHandlerRequest(ehRequest,
852 (OCRequestHandle)request->requestId,
855 (OCResourceHandle)resource,
859 request->payloadSize,
860 request->numRcvdVendorSpecificHeaderOptions,
861 request->rcvdVendorSpecificHeaderOptions,
862 (OCObserveAction)request->observationOption,
869 * Find resource at the resource directory server. This resource is not local resource but a
872 * @param resource The resource to check the matching resource URI.
873 * @param interfaceQuery The interface query parameter.
874 * @param resourceTypeQuery The resourceType query parameter.
875 * @param discPayload The payload that will be added with the resource information if found at RD.
877 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
878 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
880 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
881 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
883 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
885 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
891 return OC_STACK_NO_RESOURCE;
896 * Creates a discovery payload and add device id information. This information is included in all
899 * @param payload payload that will have memory alllocated and device id information added.
901 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
902 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
904 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
908 OIC_LOG(DEBUG, TAG, "Payload is already allocated");
912 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
913 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
916 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
917 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
918 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
920 const char* uid = OCGetServerInstanceIDString();
923 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
929 OCPayloadDestroy(*payload);
930 return OC_STACK_NO_MEMORY;
934 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
936 * @param discPayload payload that will have the baseline information included.
938 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
939 * allocating the memory for the baseline information.
941 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
945 OIC_LOG(ERROR, TAG, "Payload is not allocated");
946 return OC_STACK_ERROR;
949 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **)&discPayload->name);
951 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
952 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
953 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
954 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
956 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
957 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
958 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
963 return OC_STACK_NO_MEMORY;
966 static bool isUnicast(OCServerRequest *request)
968 bool isMulticast = request->devAddr.flags & OC_MULTICAST;
969 return (isMulticast == false &&
970 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
971 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE));
974 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
976 if (!request || !resource)
978 return OC_STACK_INVALID_PARAM;
981 OCPayload* payload = NULL;
982 char *interfaceQuery = NULL;
983 char *resourceTypeQuery = NULL;
985 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
987 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
990 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
992 // Received request for a keepalive
993 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
994 return OCHandleKeepAliveRequest(request, resource);
998 OCStackResult discoveryResult = OC_STACK_ERROR;
999 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
1000 request->method == OC_REST_DELETE)
1002 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
1003 request->resourceUrl, request->method);
1004 return OC_STACK_UNAUTHORIZED_REQ;
1007 // Step 1: Generate the response to discovery request
1008 if (virtualUriInRequest == OC_WELL_KNOWN_URI
1010 || virtualUriInRequest == OC_MQ_BROKER_URI
1014 discoveryResult = getQueryParamsForFiltering(virtualUriInRequest, request->query,
1015 &interfaceQuery, &resourceTypeQuery);
1016 VERIFY_SUCCESS(discoveryResult);
1018 if (!interfaceQuery && !resourceTypeQuery)
1020 // If no query is sent, default interface is used i.e. oic.if.ll.
1021 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
1024 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
1025 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
1026 VERIFY_SUCCESS(discoveryResult);
1028 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
1029 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
1031 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
1032 VERIFY_SUCCESS(discoveryResult);
1034 OCResourceProperty prop = OC_DISCOVERABLE;
1036 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
1038 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
1040 // This case will handle when no resource type and it is oic.if.ll.
1041 // Do not assume check if the query is ll
1042 if (!resourceTypeQuery &&
1043 (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
1045 // Only include discoverable type
1046 if (resource->resourceProperties & prop)
1048 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1051 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
1053 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1057 discoveryResult = OC_STACK_OK;
1060 if (discPayload->resources == NULL)
1062 discoveryResult = OC_STACK_NO_RESOURCE;
1065 else if (virtualUriInRequest == OC_DEVICE_URI)
1067 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1068 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1069 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1071 else if (virtualUriInRequest == OC_PLATFORM_URI)
1073 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1074 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1075 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1077 #ifdef ROUTING_GATEWAY
1078 else if (OC_GATEWAY_URI == virtualUriInRequest)
1080 // Received request for a gateway
1081 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1082 discoveryResult = RMHandleGatewayRequest(request, resource);
1087 * Step 2: Send the discovery response
1089 * Iotivity should respond to discovery requests in below manner:
1090 * 1)If query filter matching fails and discovery request is multicast,
1091 * it should NOT send any response.
1092 * 2)If query filter matching fails and discovery request is unicast,
1093 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1094 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1095 * request is multicast, it should NOT send any response.
1096 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1097 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1100 #ifdef WITH_PRESENCE
1101 if ((virtualUriInRequest == OC_PRESENCE) &&
1102 (resource->resourceProperties & OC_ACTIVE))
1104 // Need to send ACK when the request is CON.
1105 if (request->qos == OC_HIGH_QOS)
1107 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1108 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1109 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1110 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1112 FindAndDeleteServerRequest(request);
1114 // Presence uses observer notification api to respond via SendPresenceNotification.
1115 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1120 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1121 if (OC_GATEWAY_URI != virtualUriInRequest)
1124 OIC_LOG_PAYLOAD(DEBUG, payload);
1125 if(discoveryResult == OC_STACK_OK)
1128 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
1130 else // Error handling
1132 if (isUnicast(request))
1134 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
1135 discoveryResult, virtualUriInRequest);
1136 SendNonPersistantDiscoveryResponse(request, resource, NULL,
1137 (discoveryResult == OC_STACK_NO_RESOURCE) ?
1138 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1142 // Ignoring the discovery request as per RFC 7252, Section #8.2
1143 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1144 // the request should be removed.
1145 // since it never remove and causes a big memory waste.
1146 FindAndDeleteServerRequest(request);
1148 discoveryResult = OC_STACK_CONTINUE;
1155 OICFree(interfaceQuery);
1158 if (resourceTypeQuery)
1160 OICFree(resourceTypeQuery);
1162 OCPayloadDestroy(payload);
1164 // To ignore the message, OC_STACK_CONTINUE is sent
1165 return discoveryResult;
1168 static OCStackResult
1169 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
1173 return OC_STACK_INVALID_PARAM;
1176 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1177 OCEntityHandlerRequest ehRequest = {0};
1178 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1179 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
1180 VERIFY_SUCCESS(result);
1182 // At this point we know for sure that defaultDeviceHandler exists
1183 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1184 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1185 if(ehResult == OC_EH_SLOW)
1187 OIC_LOG(INFO, TAG, "This is a slow resource");
1188 request->slowFlag = 1;
1191 result = EntityHandlerCodeToOCStackCode(ehResult);
1193 // Clear server request on error case
1194 if (!OCResultToSuccess(result))
1196 FindAndDeleteServerRequest(request);
1200 OCPayloadDestroy(ehRequest.payload);
1204 static OCStackResult
1205 HandleResourceWithEntityHandler(OCServerRequest *request,
1206 OCResource *resource)
1208 if(!request || ! resource)
1210 return OC_STACK_INVALID_PARAM;
1213 OCStackResult result = OC_STACK_ERROR;
1214 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1215 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1216 ResourceObserver *resObs = NULL;
1218 OCEntityHandlerRequest ehRequest = {0};
1220 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1221 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1222 // check the security resource
1223 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1225 type = PAYLOAD_TYPE_SECURITY;
1228 result = EHRequest(&ehRequest, type, request, resource);
1229 VERIFY_SUCCESS(result);
1231 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1233 OIC_LOG(INFO, TAG, "No observation requested");
1234 ehFlag = OC_REQUEST_FLAG;
1236 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1238 OIC_LOG(INFO, TAG, "Observation registration requested");
1240 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1241 request->tokenLength);
1245 OIC_LOG (INFO, TAG, "Observer with this token already present");
1246 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1247 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1248 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1254 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1255 VERIFY_SUCCESS(result);
1257 result = AddObserver ((const char*)(request->resourceUrl),
1258 (const char *)(request->query),
1259 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1260 resource, request->qos, request->acceptFormat,
1263 if(result == OC_STACK_OK)
1265 OIC_LOG(INFO, TAG, "Added observer successfully");
1266 request->observeResult = OC_STACK_OK;
1267 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1269 else if (result == OC_STACK_RESOURCE_ERROR)
1271 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1272 request->observeResult = OC_STACK_ERROR;
1273 ehFlag = OC_REQUEST_FLAG;
1277 // The error in observeResult for the request will be used when responding to this
1278 // request by omitting the observation option/sequence number.
1279 request->observeResult = OC_STACK_ERROR;
1280 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1281 ehFlag = OC_REQUEST_FLAG;
1282 FindAndDeleteServerRequest(request);
1287 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1289 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1291 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1295 // Stack does not contain this observation request
1296 // Either token is incorrect or observation list is corrupted
1297 result = OC_STACK_NO_RESOURCE;
1300 ehRequest.obsInfo.obsId = resObs->observeId;
1301 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1303 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1305 if(result == OC_STACK_OK)
1307 OIC_LOG(INFO, TAG, "Removed observer successfully");
1308 request->observeResult = OC_STACK_OK;
1309 // There should be no observe option header for de-registration response.
1310 // Set as an invalid value here so we can detect it later and remove the field in response.
1311 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1315 request->observeResult = OC_STACK_ERROR;
1316 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1317 FindAndDeleteServerRequest(request);
1323 result = OC_STACK_ERROR;
1327 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1328 if(ehResult == OC_EH_SLOW)
1330 OIC_LOG(INFO, TAG, "This is a slow resource");
1331 request->slowFlag = 1;
1334 result = EntityHandlerCodeToOCStackCode(ehResult);
1336 // Clear server request on error case
1337 if (!OCResultToSuccess(result))
1339 FindAndDeleteServerRequest(request);
1343 OCPayloadDestroy(ehRequest.payload);
1344 FreeObserver(resObs);
1348 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
1349 OCResource *resource)
1351 if (!request || !resource)
1353 return OC_STACK_INVALID_PARAM;
1356 OCEntityHandlerRequest ehRequest = {0};
1357 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
1358 if(result == OC_STACK_OK)
1360 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1363 OCPayloadDestroy(ehRequest.payload);
1368 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1370 OCStackResult ret = OC_STACK_OK;
1372 switch (resHandling)
1374 case OC_RESOURCE_VIRTUAL:
1376 ret = HandleVirtualResource (request, resource);
1379 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1381 ret = HandleDefaultDeviceEntityHandler(request);
1384 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1386 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1387 return OC_STACK_ERROR;
1389 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1391 ret = HandleResourceWithEntityHandler (request, resource);
1394 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1396 ret = HandleResourceWithEntityHandler (request, resource);
1399 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1401 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1404 case OC_RESOURCE_NOT_SPECIFIED:
1406 ret = OC_STACK_NO_RESOURCE;
1411 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1412 return OC_STACK_ERROR;
1418 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1420 OCResource *resource = NULL;
1421 if (!info.platformID || !info.manufacturerName)
1423 OIC_LOG(ERROR, TAG, "No value specified.");
1426 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1428 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1431 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1432 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1433 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1434 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1435 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1436 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1437 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1438 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1440 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1444 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1447 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1450 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1451 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1452 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1453 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1454 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1455 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1456 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1457 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1458 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1459 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1460 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1461 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1462 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1466 return OC_STACK_INVALID_PARAM;
1469 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1471 OCStringLL *dataModelVersion = NULL;
1472 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1475 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1478 if (!info.deviceName || info.deviceName[0] == '\0')
1480 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1481 return OC_STACK_INVALID_PARAM;
1484 if (OCGetServerInstanceIDString() == NULL)
1486 OIC_LOG(INFO, TAG, "Device ID generation failed");
1490 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1491 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1495 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1498 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1499 info.specVersion: OC_SPEC_VERSION));
1500 if (info.dataModelVersions)
1502 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1506 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1507 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1509 OCFreeOCStringLL(dataModelVersion);
1512 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1516 if (dataModelVersion)
1518 OCFreeOCStringLL(dataModelVersion);
1520 return OC_STACK_ERROR;
1523 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1525 if (!resource || !attribute)
1527 return OC_STACK_INVALID_PARAM;
1529 if (0 == strlen(attribute))
1531 return OC_STACK_INVALID_PARAM;
1533 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1535 if (0 == strcmp(attribute, temp->attrName))
1537 // A special case as this type return OCStringLL
1538 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1540 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1545 *value = OICStrdup((char *)temp->attrValue);
1550 return OC_STACK_NO_RESOURCE;
1553 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1555 if (!prop || *value)
1557 return OC_STACK_INVALID_PARAM;
1559 if (strlen(prop) == 0)
1561 return OC_STACK_INVALID_PARAM;
1563 OCStackResult res = OC_STACK_NO_RESOURCE;
1564 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1566 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1567 OCResource *resource = FindResourceByUri(pathType);
1570 return OC_STACK_NO_RESOURCE;
1573 res = OCGetAttribute(resource, prop, value);
1578 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1580 // See if the attribute already exists in the list.
1581 OCAttribute *resAttrib;
1582 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1584 if (0 == strcmp(attribute, resAttrib->attrName))
1586 // Found, free the old value.
1587 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1589 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1593 OICFree((char *)resAttrib->attrValue);
1599 // If not already in the list, add it.
1600 if (NULL == resAttrib)
1602 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1603 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1604 resAttrib->attrName = OICStrdup(attribute);
1605 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1606 resAttrib->next = resource->rsrcAttributes;
1607 resource->rsrcAttributes = resAttrib;
1610 // Fill in the new value.
1611 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1613 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1617 resAttrib->attrValue = OICStrdup((char *)value);
1619 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1624 OCDeleteResourceAttributes(resAttrib);
1625 return OC_STACK_NO_MEMORY;
1629 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1631 if (!prop || !value)
1633 return OC_STACK_INVALID_PARAM;
1635 if (strlen(prop) == 0)
1637 return OC_STACK_INVALID_PARAM;
1640 OCStackResult res = OC_STACK_ERROR;
1641 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1643 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1644 OCResource *resource = FindResourceByUri(pathType);
1647 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1651 res = OCSetAttribute(resource, prop, value);