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;
110 /* This method will retrieve the tcp port */
111 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port, bool secured)
115 if (endpoint->adapter == OC_ADAPTER_IP)
117 if (endpoint->flags & OC_IP_USE_V4)
119 p = secured ? caglobals.tcp.ipv4s.port : caglobals.tcp.ipv4.port;
121 else if (endpoint->flags & OC_IP_USE_V6)
123 p = secured ? caglobals.tcp.ipv6s.port : caglobals.tcp.ipv6.port;
133 * Function will extract 0, 1 or 2 filters from query.
134 * More than 2 filters or unsupported filters will result in error.
135 * If both filters are of the same supported type, the 2nd one will be picked.
136 * Resource and device filters in the SAME query are NOT validated
137 * and resources will likely not clear filters.
139 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo)
143 OIC_LOG_V(ERROR, TAG, "Query is empty!");
144 return OC_STACK_INVALID_QUERY;
148 char *queryDup = NULL;
149 char *restOfQuery = NULL;
150 char *keyValuePair = NULL;
151 int numKeyValuePairsParsed = 0;
156 queryDup = OICStrdup(query);
157 if (NULL == queryDup)
159 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
160 return OC_STACK_NO_MEMORY;
163 OIC_LOG_V(INFO, TAG, "Extracting params from %s", queryDup);
165 OCStackResult eCode = OC_STACK_INVALID_QUERY;
166 if (strnlen(queryDup, MAX_QUERY_LENGTH) >= MAX_QUERY_LENGTH)
168 OIC_LOG(ERROR, TAG, "Query exceeds maximum length.");
172 keyValuePair = strtok_r (queryDup, OC_QUERY_SEPARATOR, &restOfQuery);
176 if (numKeyValuePairsParsed >= 2)
178 OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
182 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
188 else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
190 *filterOne = value; // if
192 else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
194 *filterTwo = value; // rt
198 OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
201 ++numKeyValuePairsParsed;
203 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
208 *filterOne = OICStrdup(*filterOne);
209 if (NULL == *filterOne)
211 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
212 eCode = OC_STACK_NO_MEMORY;
219 *filterTwo = OICStrdup(*filterTwo);
220 if (NULL == *filterTwo)
222 OIC_LOG_V(ERROR, TAG, "Creating duplicate string failed!");
224 eCode = OC_STACK_NO_MEMORY;
230 OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
240 static OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
242 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
244 return OC_WELL_KNOWN_URI;
246 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
248 return OC_DEVICE_URI;
250 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
252 return OC_PLATFORM_URI;
254 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
256 return OC_RESOURCE_TYPES_URI;
258 #ifdef ROUTING_GATEWAY
259 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
261 return OC_GATEWAY_URI;
265 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
269 #endif //WITH_PRESENCE
272 else if (0 == strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_MQ_URI))
274 return OC_MQ_BROKER_URI;
279 else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
281 return OC_KEEPALIVE_RESOURCE_URI;
285 return OC_UNKNOWN_URI;
288 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
289 char **filterOne, char **filterTwo)
291 if(!filterOne || !filterTwo)
293 return OC_STACK_INVALID_PARAM;
300 if (uri == OC_PRESENCE)
302 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
303 OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
308 OCStackResult result = OC_STACK_OK;
312 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
318 bool appendOCStringLL(OCRepPayload *device, OCStringLL *dmv)
321 for (OCStringLL *ll = dmv; ll; ll = ll->next, size++);
322 size_t dim[MAX_REP_ARRAY_DEPTH] = {size, 0, 0};
323 char **dt = (char **)OICMalloc(sizeof(char *) * size);
325 VERIFY_PARAM_NON_NULL(TAG, dt, "Data Model Version allocation failed.");
326 for (OCStringLL *ll = dmv; ll; ll = ll->next, i++)
328 dt[i] = OICStrdup(ll->value);
329 VERIFY_PARAM_NON_NULL(TAG, dt[i], "Data Model Version adding failed.");
331 if (!OCRepPayloadSetStringArrayAsOwner(device, OC_RSRVD_DATA_MODEL_VERSION, dt, dim))
338 for (int i = 0; i < size; i++)
346 static OCStackResult BuildDevicePlatformPayload(const OCResource *resourcePtr, OCRepPayload** payload, bool addDeviceId)
350 return OC_STACK_INVALID_PARAM;
353 OCRepPayload *tempPayload = OCRepPayloadCreate();
356 return OC_STACK_NO_MEMORY;
361 const char *deviceId = OCGetServerInstanceIDString();
364 OIC_LOG(ERROR, TAG, "Failed retrieving device id.");
365 return OC_STACK_ERROR;
367 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_DEVICE_ID, deviceId);
370 OCResourceType *resType = resourcePtr->rsrcType;
373 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
374 resType = resType->next;
377 OCResourceInterface *resInterface = resourcePtr->rsrcInterface;
380 OCRepPayloadAddInterface(tempPayload, resInterface->name);
381 resInterface = resInterface->next;
384 OCAttribute *resAttrib = resourcePtr->rsrcAttributes;
387 if (resAttrib->attrName && resAttrib->attrValue)
389 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
391 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
394 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
400 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
403 resAttrib = resAttrib->next;
408 *payload = tempPayload;
412 OCRepPayloadAppend(*payload, tempPayload);
418 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
419 OCRepPayload** payload, OCDevAddr *devAddr)
423 return OC_STACK_INVALID_PARAM;
426 OCRepPayload *tempPayload = OCRepPayloadCreate();
429 return OC_STACK_NO_MEMORY;
432 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_HREF, resourcePtr->uri);
434 uint8_t numElement = 0;
435 if (OC_STACK_OK == OCGetNumberOfResourceTypes((OCResource *)resourcePtr, &numElement))
437 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
438 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
441 OIC_LOG(ERROR, TAG, "Resource type allocation failed.");
442 OCRepPayloadDestroy(tempPayload);
443 return OC_STACK_NO_MEMORY;
445 for (uint8_t i = 0; i < numElement; ++i)
447 const char *value = OCGetResourceTypeName((OCResource *)resourcePtr, i);
448 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
449 rt[i] = OICStrdup(value);
451 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_RESOURCE_TYPE, rt, rtDim);
455 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces((OCResource *)resourcePtr, &numElement))
457 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
458 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
461 OIC_LOG(ERROR, TAG, "Resource interface allocation failed.");
462 OCRepPayloadDestroy(tempPayload);
463 return OC_STACK_NO_MEMORY;
465 for (uint8_t i = 0; i < numElement; ++i)
467 const char *value = OCGetResourceInterfaceName((OCResource *)resourcePtr, i);
468 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
469 itf[i] = OICStrdup(value);
471 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
474 for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
476 if (resAttrib->attrName && resAttrib->attrValue)
478 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
480 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
483 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
489 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
496 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
497 OCRepPayload *policy = OCRepPayloadCreate();
500 OCPayloadDestroy((OCPayload *)tempPayload);
501 return OC_STACK_NO_MEMORY;
503 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
506 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
507 uint16_t securePort = 0;
508 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
512 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
514 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
519 *payload = tempPayload;
523 OCRepPayloadAppend(*payload, tempPayload);
529 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
530 OCDiscoveryPayload *payload, OCDevAddr *devAddr)
532 if (!resourcePtr || !payload)
534 return OC_STACK_INVALID_PARAM;
536 uint16_t securePort = 0;
537 if (resourcePtr->resourceProperties & OC_SECURE)
539 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
546 uint16_t tcpPort = 0;
547 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
549 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort, tcpPort);
551 OCDiscoveryPayloadAddResource(payload, resourcePtr, securePort);
557 OCResource *FindResourceByUri(const char* resourceUri)
564 OCResource * pointer = headResource;
567 if (strcmp(resourceUri, pointer->uri) == 0)
571 pointer = pointer->next;
573 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
577 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
578 ResourceHandling *handling,
579 OCResource **resource)
581 if(!request || !handling || !resource)
583 return OC_STACK_INVALID_PARAM;
586 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
588 // Check if virtual resource
589 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
591 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
592 *handling = OC_RESOURCE_VIRTUAL;
593 *resource = headResource;
596 if (strlen((const char*)(request->resourceUrl)) == 0)
598 // Resource URL not specified
599 *handling = OC_RESOURCE_NOT_SPECIFIED;
600 return OC_STACK_NO_RESOURCE;
604 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
605 *resource = resourcePtr;
608 if(defaultDeviceHandler)
610 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
614 // Resource does not exist
615 // and default device handler does not exist
616 *handling = OC_RESOURCE_NOT_SPECIFIED;
617 return OC_STACK_NO_RESOURCE;
620 if (resourcePtr && resourcePtr->rsrcChildResourcesHead != NULL)
622 // Collection resource
623 if (resourcePtr->entityHandler != defaultResourceEHandler)
625 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
630 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
636 // Resource not a collection
637 if (resourcePtr->entityHandler != defaultResourceEHandler)
639 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
644 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
651 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
653 OCStackResult result;
660 result = OC_STACK_OK;
663 result = OC_STACK_SLOW_RESOURCE;
666 result = OC_STACK_ERROR;
668 case OC_EH_FORBIDDEN:
669 result = OC_STACK_FORBIDDEN_REQ;
671 case OC_EH_RESOURCE_CREATED:
672 result = OC_STACK_RESOURCE_CREATED;
674 case OC_EH_RESOURCE_DELETED:
675 result = OC_STACK_RESOURCE_DELETED;
678 result = OC_STACK_RESOURCE_CHANGED;
680 case OC_EH_RESOURCE_NOT_FOUND:
681 result = OC_STACK_NO_RESOURCE;
683 case OC_EH_INTERNAL_SERVER_ERROR:
684 result = OC_STACK_INTERNAL_SERVER_ERROR;
686 case OC_EH_NOT_IMPLEMENTED:
687 result = OC_STACK_NOT_IMPLEMENTED;
689 case OC_EH_BAD_GATEWAY:
690 result = OC_STACK_BAD_GATEWAY;
692 case OC_EH_SERVICE_UNAVAILABLE:
693 result = OC_STACK_SERVICE_UNAVAILABLE;
695 case OC_EH_RETRANSMIT_TIMEOUT:
696 result = OC_STACK_GATEWAY_TIMEOUT;
698 case OC_EH_PROXY_NOT_SUPPORTED:
699 result = OC_STACK_PROXY_NOT_SUPPORTED;
702 result = OC_STACK_ERROR;
708 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
715 // Null is analogous to no filter.
716 if (NULL == resourceTypeFilter)
721 // Empty resourceType filter is analogous to error query
722 if (0 == strlen(resourceTypeFilter))
727 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
729 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
735 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
739 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
746 // Null is analogous to no filter.
747 if (NULL == interfaceFilter)
752 // Empty interface filter is analogous to error query
753 if (0 == strlen(interfaceFilter))
758 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
760 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
761 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
762 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
768 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
773 * If the filters are null, they will be assumed to NOT be present
774 * and the resource will not be matched against them.
775 * Function will return true if all non null AND non empty filters passed in find a match.
777 static bool includeThisResourceInResponse(OCResource *resource,
778 char *interfaceFilter,
779 char *resourceTypeFilter)
783 OIC_LOG(ERROR, TAG, "Invalid resource");
787 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
790 * At least one valid filter should be available to
791 * include the resource in discovery response
793 if (!(resourceTypeFilter && *resourceTypeFilter))
795 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
796 resource", resource->uri);
800 else if (!(resource->resourceProperties & OC_ACTIVE) ||
801 !(resource->resourceProperties & OC_DISCOVERABLE))
803 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
807 return resourceMatchesIFFilter(resource, interfaceFilter) &&
808 resourceMatchesRTFilter(resource, resourceTypeFilter);
811 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
812 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
814 OCEntityHandlerResponse response = {0};
816 response.ehResult = ehResult;
817 response.payload = discoveryPayload;
818 response.persistentBufferFlag = 0;
819 response.requestHandle = (OCRequestHandle) request->requestId;
820 response.resourceHandle = (OCResourceHandle) resource;
822 return OCDoResponse(&response);
825 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
826 OCServerRequest *request, OCResource *resource)
828 return FormOCEntityHandlerRequest(ehRequest,
829 (OCRequestHandle)request->requestId,
832 (OCResourceHandle)resource,
836 request->payloadSize,
837 request->numRcvdVendorSpecificHeaderOptions,
838 request->rcvdVendorSpecificHeaderOptions,
839 (OCObserveAction)request->observationOption,
846 * Find resource at the resource directory server. This resource is not local resource but a
849 * @param resource The resource to check the matching resource URI.
850 * @param interfaceQuery The interface query parameter.
851 * @param resourceTypeQuery The resourceType query parameter.
852 * @param discPayload The payload that will be added with the resource information if found at RD.
854 * @return ::OC_STACK_OK if the resource is found else ::OC_STACK_NO_RESOURCE.
855 * In case if build is not with flag RD_SERVER, it returns ::OC_STACK_NO_RESOURCE.
857 static OCStackResult findResourceAtRD(const OCResource* resource, const char *interfaceQuery,
858 const char *resourceTypeQuery, OCDiscoveryPayload *discPayload)
860 if (strcmp(resource->uri, OC_RSRVD_RD_URI) == 0)
862 if (OC_STACK_OK == OCRDDatabaseCheckResources(interfaceQuery, resourceTypeQuery, discPayload))
868 return OC_STACK_NO_RESOURCE;
873 * Creates a discovery payload and add device id information. This information is included in all
876 * @param payload payload that will have memory alllocated and device id information added.
878 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
879 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
881 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
885 OIC_LOG(DEBUG, TAG, "Payload is already allocated");
889 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
890 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
893 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
894 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
895 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
897 const char* uid = OCGetServerInstanceIDString();
900 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
906 OCPayloadDestroy(*payload);
907 return OC_STACK_NO_MEMORY;
911 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
913 * @param discPayload payload that will have the baseline information included.
915 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
916 * allocating the memory for the baseline information.
918 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
922 OIC_LOG(ERROR, TAG, "Payload is not allocated");
923 return OC_STACK_ERROR;
926 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **)&discPayload->name);
928 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
929 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
930 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
931 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
933 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
934 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
935 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
940 return OC_STACK_NO_MEMORY;
943 static bool isUnicast(OCServerRequest *request)
945 bool isMulticast = request->devAddr.flags & OC_MULTICAST;
946 return (isMulticast == false &&
947 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
948 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE));
951 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
953 if (!request || !resource)
955 return OC_STACK_INVALID_PARAM;
958 OCPayload* payload = NULL;
959 char *interfaceQuery = NULL;
960 char *resourceTypeQuery = NULL;
962 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
964 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
967 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
969 // Received request for a keepalive
970 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
971 return OCHandleKeepAliveRequest(request, resource);
975 OCStackResult discoveryResult = OC_STACK_ERROR;
976 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
977 request->method == OC_REST_DELETE)
979 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
980 request->resourceUrl, request->method);
981 return OC_STACK_UNAUTHORIZED_REQ;
984 // Step 1: Generate the response to discovery request
985 if (virtualUriInRequest == OC_WELL_KNOWN_URI
987 || virtualUriInRequest == OC_MQ_BROKER_URI
991 discoveryResult = getQueryParamsForFiltering(virtualUriInRequest, request->query,
992 &interfaceQuery, &resourceTypeQuery);
993 VERIFY_SUCCESS(discoveryResult);
995 if (!interfaceQuery && !resourceTypeQuery)
997 // If no query is sent, default interface is used i.e. oic.if.ll.
998 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
1001 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
1002 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
1003 VERIFY_SUCCESS(discoveryResult);
1005 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
1006 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
1008 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
1009 VERIFY_SUCCESS(discoveryResult);
1011 OCResourceProperty prop = OC_DISCOVERABLE;
1013 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
1015 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
1017 // This case will handle when no resource type and it is oic.if.ll.
1018 // Do not assume check if the query is ll
1019 if (!resourceTypeQuery &&
1020 (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
1022 // Only include discoverable type
1023 if (resource->resourceProperties & prop)
1025 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1028 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
1030 discoveryResult = BuildVirtualResourceResponse(resource, discPayload, &request->devAddr);
1034 discoveryResult = OC_STACK_OK;
1037 if (discPayload->resources == NULL)
1039 discoveryResult = OC_STACK_NO_RESOURCE;
1042 else if (virtualUriInRequest == OC_DEVICE_URI)
1044 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1045 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1046 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1048 else if (virtualUriInRequest == OC_PLATFORM_URI)
1050 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1051 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1052 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1054 #ifdef ROUTING_GATEWAY
1055 else if (OC_GATEWAY_URI == virtualUriInRequest)
1057 // Received request for a gateway
1058 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1059 discoveryResult = RMHandleGatewayRequest(request, resource);
1064 * Step 2: Send the discovery response
1066 * Iotivity should respond to discovery requests in below manner:
1067 * 1)If query filter matching fails and discovery request is multicast,
1068 * it should NOT send any response.
1069 * 2)If query filter matching fails and discovery request is unicast,
1070 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1071 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1072 * request is multicast, it should NOT send any response.
1073 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1074 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1077 #ifdef WITH_PRESENCE
1078 if ((virtualUriInRequest == OC_PRESENCE) &&
1079 (resource->resourceProperties & OC_ACTIVE))
1081 // Need to send ACK when the request is CON.
1082 if (request->qos == OC_HIGH_QOS)
1084 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1085 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1086 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1087 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1089 FindAndDeleteServerRequest(request);
1091 // Presence uses observer notification api to respond via SendPresenceNotification.
1092 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1097 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1098 if (OC_GATEWAY_URI != virtualUriInRequest)
1101 OIC_LOG_PAYLOAD(DEBUG, payload);
1102 if(discoveryResult == OC_STACK_OK)
1105 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
1107 else // Error handling
1109 if (isUnicast(request))
1111 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
1112 discoveryResult, virtualUriInRequest);
1113 SendNonPersistantDiscoveryResponse(request, resource, NULL,
1114 (discoveryResult == OC_STACK_NO_RESOURCE) ?
1115 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
1119 // Ignoring the discovery request as per RFC 7252, Section #8.2
1120 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
1121 // the request should be removed.
1122 // since it never remove and causes a big memory waste.
1123 FindAndDeleteServerRequest(request);
1125 discoveryResult = OC_STACK_CONTINUE;
1132 OICFree(interfaceQuery);
1135 if (resourceTypeQuery)
1137 OICFree(resourceTypeQuery);
1139 OCPayloadDestroy(payload);
1141 // To ignore the message, OC_STACK_CONTINUE is sent
1142 return discoveryResult;
1145 static OCStackResult
1146 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
1150 return OC_STACK_INVALID_PARAM;
1153 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1154 OCEntityHandlerRequest ehRequest = {0};
1155 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
1156 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
1157 VERIFY_SUCCESS(result);
1159 // At this point we know for sure that defaultDeviceHandler exists
1160 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
1161 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
1162 if(ehResult == OC_EH_SLOW)
1164 OIC_LOG(INFO, TAG, "This is a slow resource");
1165 request->slowFlag = 1;
1168 result = EntityHandlerCodeToOCStackCode(ehResult);
1170 // Clear server request on error case
1171 if (!OCResultToSuccess(result))
1173 FindAndDeleteServerRequest(request);
1177 OCPayloadDestroy(ehRequest.payload);
1181 static OCStackResult
1182 HandleResourceWithEntityHandler(OCServerRequest *request,
1183 OCResource *resource)
1185 if(!request || ! resource)
1187 return OC_STACK_INVALID_PARAM;
1190 OCStackResult result = OC_STACK_ERROR;
1191 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1192 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
1193 ResourceObserver *resObs = NULL;
1195 OCEntityHandlerRequest ehRequest = {0};
1197 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
1198 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
1199 // check the security resource
1200 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
1202 type = PAYLOAD_TYPE_SECURITY;
1205 result = EHRequest(&ehRequest, type, request, resource);
1206 VERIFY_SUCCESS(result);
1208 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
1210 OIC_LOG(INFO, TAG, "No observation requested");
1211 ehFlag = OC_REQUEST_FLAG;
1213 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
1215 OIC_LOG(INFO, TAG, "Observation registration requested");
1217 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
1218 request->tokenLength);
1222 OIC_LOG (INFO, TAG, "Observer with this token already present");
1223 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
1224 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
1225 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
1227 // server requests are usually free'd when the response is sent out
1228 // for the request in ocserverrequest.c : HandleSingleResponse()
1229 // Since we are making an early return and not responding, the server request
1230 // needs to be deleted.
1231 FindAndDeleteServerRequest (request);
1236 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
1237 VERIFY_SUCCESS(result);
1239 result = AddObserver ((const char*)(request->resourceUrl),
1240 (const char *)(request->query),
1241 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
1242 resource, request->qos, request->acceptFormat,
1245 if(result == OC_STACK_OK)
1247 OIC_LOG(INFO, TAG, "Added observer successfully");
1248 request->observeResult = OC_STACK_OK;
1249 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
1251 else if (result == OC_STACK_RESOURCE_ERROR)
1253 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
1254 request->observeResult = OC_STACK_ERROR;
1255 ehFlag = OC_REQUEST_FLAG;
1259 // The error in observeResult for the request will be used when responding to this
1260 // request by omitting the observation option/sequence number.
1261 request->observeResult = OC_STACK_ERROR;
1262 OIC_LOG(ERROR, TAG, "Observer Addition failed");
1263 ehFlag = OC_REQUEST_FLAG;
1264 FindAndDeleteServerRequest(request);
1269 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
1271 OIC_LOG(INFO, TAG, "Deregistering observation requested");
1273 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
1277 // Stack does not contain this observation request
1278 // Either token is incorrect or observation list is corrupted
1279 result = OC_STACK_NO_RESOURCE;
1282 ehRequest.obsInfo.obsId = resObs->observeId;
1283 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
1285 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
1287 if(result == OC_STACK_OK)
1289 OIC_LOG(INFO, TAG, "Removed observer successfully");
1290 request->observeResult = OC_STACK_OK;
1291 // There should be no observe option header for de-registration response.
1292 // Set as an invalid value here so we can detect it later and remove the field in response.
1293 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
1297 request->observeResult = OC_STACK_ERROR;
1298 OIC_LOG(ERROR, TAG, "Observer Removal failed");
1299 FindAndDeleteServerRequest(request);
1305 result = OC_STACK_ERROR;
1309 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
1310 if(ehResult == OC_EH_SLOW)
1312 OIC_LOG(INFO, TAG, "This is a slow resource");
1313 request->slowFlag = 1;
1316 result = EntityHandlerCodeToOCStackCode(ehResult);
1318 // Clear server request on error case
1319 if (!OCResultToSuccess(result))
1321 FindAndDeleteServerRequest(request);
1325 OCPayloadDestroy(ehRequest.payload);
1326 FreeObserver(resObs);
1330 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
1331 OCResource *resource)
1333 if (!request || !resource)
1335 return OC_STACK_INVALID_PARAM;
1338 OCEntityHandlerRequest ehRequest = {0};
1339 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
1340 if(result == OC_STACK_OK)
1342 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
1345 OCPayloadDestroy(ehRequest.payload);
1350 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
1352 OCStackResult ret = OC_STACK_OK;
1354 switch (resHandling)
1356 case OC_RESOURCE_VIRTUAL:
1358 ret = HandleVirtualResource (request, resource);
1361 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
1363 ret = HandleDefaultDeviceEntityHandler(request);
1366 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
1368 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
1369 return OC_STACK_ERROR;
1371 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
1373 ret = HandleResourceWithEntityHandler (request, resource);
1376 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
1378 ret = HandleResourceWithEntityHandler (request, resource);
1381 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
1383 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
1386 case OC_RESOURCE_NOT_SPECIFIED:
1388 ret = OC_STACK_NO_RESOURCE;
1393 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
1394 return OC_STACK_ERROR;
1400 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
1402 OCResource *resource = NULL;
1403 if (!info.platformID || !info.manufacturerName)
1405 OIC_LOG(ERROR, TAG, "No value specified.");
1408 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
1410 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
1413 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
1414 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
1415 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
1416 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1417 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1418 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1419 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
1420 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
1422 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
1426 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1429 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
1432 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
1433 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
1434 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
1435 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
1436 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
1437 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
1438 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
1439 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
1440 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
1441 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
1442 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
1443 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
1444 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
1448 return OC_STACK_INVALID_PARAM;
1451 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
1453 OCStringLL *dataModelVersion = NULL;
1454 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1457 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
1460 if (!info.deviceName || info.deviceName[0] == '\0')
1462 OIC_LOG(ERROR, TAG, "Null or empty device name.");
1463 return OC_STACK_INVALID_PARAM;
1466 if (OCGetServerInstanceIDString() == NULL)
1468 OIC_LOG(INFO, TAG, "Device ID generation failed");
1472 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
1473 for (OCStringLL *temp = info.types; temp; temp = temp->next)
1477 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
1480 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
1481 info.specVersion: OC_SPEC_VERSION));
1482 if (info.dataModelVersions)
1484 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, info.dataModelVersions));
1488 dataModelVersion = OCCreateOCStringLL(OC_DATA_MODEL_VERSION);
1489 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dataModelVersion));
1491 OCFreeOCStringLL(dataModelVersion);
1494 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
1498 if (dataModelVersion)
1500 OCFreeOCStringLL(dataModelVersion);
1502 return OC_STACK_ERROR;
1505 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
1507 if (!resource || !attribute)
1509 return OC_STACK_INVALID_PARAM;
1511 if (0 == strlen(attribute))
1513 return OC_STACK_INVALID_PARAM;
1515 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
1517 if (0 == strcmp(attribute, temp->attrName))
1519 // A special case as this type return OCStringLL
1520 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1522 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
1527 *value = OICStrdup((char *)temp->attrValue);
1532 return OC_STACK_NO_RESOURCE;
1535 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
1537 if (!prop || *value)
1539 return OC_STACK_INVALID_PARAM;
1541 if (strlen(prop) == 0)
1543 return OC_STACK_INVALID_PARAM;
1545 OCStackResult res = OC_STACK_NO_RESOURCE;
1546 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1548 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1549 OCResource *resource = FindResourceByUri(pathType);
1552 return OC_STACK_NO_RESOURCE;
1555 res = OCGetAttribute(resource, prop, value);
1560 OCStackResult OCSetAttribute(OCResource* resource, const char* attribute, const void* value)
1562 // See if the attribute already exists in the list.
1563 OCAttribute *resAttrib;
1564 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
1566 if (0 == strcmp(attribute, resAttrib->attrName))
1568 // Found, free the old value.
1569 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
1571 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
1575 OICFree((char *)resAttrib->attrValue);
1581 // If not already in the list, add it.
1582 if (NULL == resAttrib)
1584 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
1585 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
1586 resAttrib->attrName = OICStrdup(attribute);
1587 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
1588 resAttrib->next = resource->rsrcAttributes;
1589 resource->rsrcAttributes = resAttrib;
1592 // Fill in the new value.
1593 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
1595 resAttrib->attrValue = CloneOCStringLL((OCStringLL *)value);
1599 resAttrib->attrValue = OICStrdup((char *)value);
1601 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
1606 OCDeleteResourceAttributes(resAttrib);
1607 return OC_STACK_NO_MEMORY;
1611 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
1613 if (!prop || !value)
1615 return OC_STACK_INVALID_PARAM;
1617 if (strlen(prop) == 0)
1619 return OC_STACK_INVALID_PARAM;
1622 OCStackResult res = OC_STACK_ERROR;
1623 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
1625 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
1626 OCResource *resource = FindResourceByUri(pathType);
1629 OIC_LOG(ERROR, TAG, "Resource does not exist.");
1633 res = OCSetAttribute(resource, prop, value);