1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1-2001 base
24 // specification (excluding the XSI extension).
25 // For POSIX.1-2001 base specification,
26 // Refer http://pubs.opengroup.org/onlinepubs/009695399/
27 #define _POSIX_C_SOURCE 200112L
32 #include "iotivity_config.h"
40 #include "ocresource.h"
41 #include "ocresourcehandler.h"
42 #include "ocobserve.h"
43 #include "occollection.h"
44 #include "oic_malloc.h"
45 #include "oic_string.h"
47 #include "ocpayload.h"
48 #include "secureresourcemanager.h"
50 #include "cainterface.h"
51 #include "ocpayload.h"
52 #include "oickeepalive.h"
53 #include "platform_features.h"
54 #include "payload_logging.h"
55 #include "ocendpoint.h"
56 #include "ocstackinternal.h"
57 #include "oickeepalive.h"
58 #include "ocpayloadcbor.h"
59 #include "psinterface.h"
61 #ifdef ROUTING_GATEWAY
62 #include "routingmanager.h"
66 #define TAG "OIC_RI_RESOURCE"
68 // Using 1k as block size since most persistent storage implementations use a power of 2.
69 #define INTROSPECTION_FILE_SIZE_BLOCK 1024
71 #define VERIFY_SUCCESS(op) { if (op != (OC_STACK_OK)) \
72 {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
75 * Default cbor payload size. This value is increased in case of CborErrorOutOfMemory.
76 * The value of payload size is increased up to CBOR_MAX_SIZE.
78 static const uint16_t CBOR_SIZE = 512;
81 * Max cbor size payload.
83 static const uint16_t CBOR_MAX_SIZE = 4400;
85 extern OCResource *headResource;
86 extern bool g_multicastServerStopped;
89 * Prepares a Payload for response.
91 static OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
92 OCDiscoveryPayload *payload,
94 CAEndpoint_t *networkInfo,
98 * Sets the value of an attribute on a resource.
100 static OCStackResult SetAttributeInternal(OCResource *resource,
101 const char *attribute,
103 bool updateDatabase);
105 //-----------------------------------------------------------------------------
106 // Default resource entity handler function
107 //-----------------------------------------------------------------------------
108 OCEntityHandlerResult defaultResourceEHandler(OCEntityHandlerFlag flag,
109 OCEntityHandlerRequest * request, void* callbackParam)
111 //TODO ("Implement me!!!!");
112 // TODO: remove silence unused param warnings
115 (void) callbackParam;
116 return OC_EH_OK; // Making sure that the Default EH and the Vendor EH have matching signatures
119 /* This method will retrieve the port at which the secure resource is hosted */
120 static OCStackResult GetSecurePortInfo(OCDevAddr *endpoint, uint16_t *port)
124 if (endpoint->adapter == OC_ADAPTER_IP)
126 if (endpoint->flags & OC_IP_USE_V6)
128 p = caglobals.ip.u6s.port;
130 else if (endpoint->flags & OC_IP_USE_V4)
132 p = caglobals.ip.u4s.port;
141 /* This method will retrieve the tcp port */
142 static OCStackResult GetTCPPortInfo(OCDevAddr *endpoint, uint16_t *port, bool secured)
146 if (endpoint->adapter == OC_ADAPTER_IP)
148 if (endpoint->flags & OC_IP_USE_V4)
150 p = secured ? caglobals.tcp.ipv4s.port : caglobals.tcp.ipv4.port;
152 else if (endpoint->flags & OC_IP_USE_V6)
154 p = secured ? caglobals.tcp.ipv6s.port : caglobals.tcp.ipv6.port;
164 * Function will extract 0, 1 or 2 filters from query.
165 * More than 2 filters or unsupported filters will result in error.
166 * If both filters are of the same supported type, the 2nd one will be picked.
167 * Resource and device filters in the SAME query are NOT validated
168 * and resources will likely not clear filters.
170 OCStackResult ExtractFiltersFromQuery(const char *query, char **filterOne, char **filterTwo)
174 OIC_LOG(ERROR, TAG, "Query is empty!");
175 return OC_STACK_INVALID_QUERY;
179 char *queryDup = NULL;
180 char *restOfQuery = NULL;
181 char *keyValuePair = NULL;
182 int numKeyValuePairsParsed = 0;
187 queryDup = OICStrdup(query);
188 if (NULL == queryDup)
190 OIC_LOG(ERROR, TAG, "Creating duplicate string failed!");
191 return OC_STACK_NO_MEMORY;
194 OIC_LOG_V(INFO, TAG, "Extracting params from %s", queryDup);
196 OCStackResult eCode = OC_STACK_INVALID_QUERY;
197 if (strnlen(queryDup, MAX_QUERY_LENGTH) >= MAX_QUERY_LENGTH)
199 OIC_LOG(ERROR, TAG, "Query exceeds maximum length.");
203 keyValuePair = strtok_r (queryDup, OC_QUERY_SEPARATOR, &restOfQuery);
207 if (numKeyValuePairsParsed >= 2)
209 OIC_LOG(ERROR, TAG, "More than 2 queries params in URI.");
213 key = strtok_r(keyValuePair, OC_KEY_VALUE_DELIMITER, &value);
219 else if (strncasecmp(key, OC_RSRVD_INTERFACE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
221 *filterOne = value; // if
223 else if (strncasecmp(key, OC_RSRVD_RESOURCE_TYPE, sizeof(OC_RSRVD_INTERFACE) - 1) == 0)
225 *filterTwo = value; // rt
229 OIC_LOG_V(ERROR, TAG, "Unsupported query key: %s", key);
232 ++numKeyValuePairsParsed;
234 keyValuePair = strtok_r(NULL, OC_QUERY_SEPARATOR, &restOfQuery);
239 *filterOne = OICStrdup(*filterOne);
240 if (NULL == *filterOne)
242 OIC_LOG(ERROR, TAG, "Creating duplicate string failed!");
243 eCode = OC_STACK_NO_MEMORY;
250 *filterTwo = OICStrdup(*filterTwo);
251 if (NULL == *filterTwo)
253 OIC_LOG(ERROR, TAG, "Creating duplicate string failed!");
255 eCode = OC_STACK_NO_MEMORY;
261 OIC_LOG_V(INFO, TAG, "Extracted params if: %s and rt: %s.", *filterOne, *filterTwo);
271 OCVirtualResources GetTypeOfVirtualURI(const char *uriInRequest)
273 if (strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_URI) == 0)
275 return OC_WELL_KNOWN_URI;
277 else if (strcmp(uriInRequest, OC_RSRVD_DEVICE_URI) == 0)
279 return OC_DEVICE_URI;
281 else if (strcmp(uriInRequest, OC_RSRVD_PLATFORM_URI) == 0)
283 return OC_PLATFORM_URI;
285 else if (strcmp(uriInRequest, OC_RSRVD_RESOURCE_TYPES_URI) == 0)
287 return OC_RESOURCE_TYPES_URI;
289 else if (strcmp(uriInRequest, OC_RSRVD_INTROSPECTION_URI) == 0)
291 return OC_INTROSPECTION_URI;
293 else if (strcmp(uriInRequest, OC_RSRVD_INTROSPECTION_PAYLOAD_URI) == 0)
295 return OC_INTROSPECTION_PAYLOAD_URI;
297 #ifdef ROUTING_GATEWAY
298 else if (0 == strcmp(uriInRequest, OC_RSRVD_GATEWAY_URI))
300 return OC_GATEWAY_URI;
304 else if (strcmp(uriInRequest, OC_RSRVD_PRESENCE_URI) == 0)
308 #endif //WITH_PRESENCE
311 else if (0 == strcmp(uriInRequest, OC_RSRVD_WELL_KNOWN_MQ_URI))
313 return OC_MQ_BROKER_URI;
318 else if (strcmp(uriInRequest, OC_RSRVD_KEEPALIVE_URI) == 0)
320 return OC_KEEPALIVE_RESOURCE_URI;
324 return OC_UNKNOWN_URI;
327 static OCStackResult getQueryParamsForFiltering (OCVirtualResources uri, char *query,
328 char **filterOne, char **filterTwo)
330 if(!filterOne || !filterTwo)
332 return OC_STACK_INVALID_PARAM;
339 if (uri == OC_PRESENCE)
341 //Nothing needs to be done, except for pass a OC_PRESENCE query through as OC_STACK_OK.
342 OIC_LOG(INFO, TAG, "OC_PRESENCE Request for virtual resource.");
347 OCStackResult result = OC_STACK_OK;
351 result = ExtractFiltersFromQuery(query, filterOne, filterTwo);
357 static OCStackResult BuildDevicePlatformPayload(const OCResource *resourcePtr, OCRepPayload** payload,
360 OCRepPayload *tempPayload = OCRepPayloadCreate();
364 OCRepPayloadDestroy(tempPayload);
365 return OC_STACK_INVALID_PARAM;
370 return OC_STACK_NO_MEMORY;
375 const char *deviceId = OCGetServerInstanceIDString();
378 OIC_LOG(ERROR, TAG, "Failed retrieving device id.");
379 return OC_STACK_ERROR;
381 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_DEVICE_ID, deviceId);
384 for (OCResourceType *resType = resourcePtr->rsrcType; resType; resType = resType->next)
386 OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename);
389 for (OCResourceInterface *resInterface = resourcePtr->rsrcInterface; resInterface;
390 resInterface = resInterface->next)
392 OCRepPayloadAddInterface(tempPayload, resInterface->name);
395 for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
397 if (resAttrib->attrName && resAttrib->attrValue)
399 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
401 char *dmv = OCCreateString((OCStringLL *)resAttrib->attrValue);
404 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, dmv);
410 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
417 *payload = tempPayload;
421 OCRepPayloadAppend(*payload, tempPayload);
427 OCStackResult BuildResponseRepresentation(const OCResource *resourcePtr,
428 OCRepPayload** payload, OCDevAddr *devAddr)
430 OCRepPayload *tempPayload = OCRepPayloadCreate();
434 OCRepPayloadDestroy(tempPayload);
435 return OC_STACK_INVALID_PARAM;
440 return OC_STACK_NO_MEMORY;
443 OCRepPayloadSetPropString(tempPayload, OC_RSRVD_HREF, resourcePtr->uri);
445 uint8_t numElement = 0;
446 if (OC_STACK_OK == OCGetNumberOfResourceTypes((OCResource *)resourcePtr, &numElement))
448 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
449 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
450 for (uint8_t i = 0; i < numElement; ++i)
452 const char *value = OCGetResourceTypeName((OCResource *)resourcePtr, i);
453 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
454 rt[i] = OICStrdup(value);
456 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_RESOURCE_TYPE, rt, rtDim);
460 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces((OCResource *)resourcePtr, &numElement))
462 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
463 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
464 for (uint8_t i = 0; i < numElement; ++i)
466 const char *value = OCGetResourceInterfaceName((OCResource *)resourcePtr, i);
467 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
468 itf[i] = OICStrdup(value);
470 OCRepPayloadSetStringArrayAsOwner(tempPayload, OC_RSRVD_INTERFACE, itf, ifDim);
473 for (OCAttribute *resAttrib = resourcePtr->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
475 if (resAttrib->attrName && resAttrib->attrValue)
477 OCRepPayloadSetPropString(tempPayload, resAttrib->attrName, (char *)resAttrib->attrValue);
481 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)resourcePtr);
482 OCRepPayload *policy = OCRepPayloadCreate();
485 OCPayloadDestroy((OCPayload *)tempPayload);
486 return OC_STACK_NO_MEMORY;
488 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
491 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
492 uint16_t securePort = 0;
493 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
497 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
499 OCRepPayloadSetPropObjectAsOwner(tempPayload, OC_RSRVD_POLICY, policy);
503 *payload = tempPayload;
507 OCRepPayloadAppend(*payload, tempPayload);
513 void CleanUpDeviceProperties(OCDeviceProperties **deviceProperties)
515 if (!deviceProperties || !(*deviceProperties))
520 OICFreeAndSetToNull((void**)(deviceProperties));
523 static OCStackResult CreateDeviceProperties(const char *piid, OCDeviceProperties **deviceProperties)
525 OIC_LOG(DEBUG, TAG, "CreateDeviceProperties IN");
527 OCStackResult result = OC_STACK_OK;
529 if (!piid || !deviceProperties)
531 return OC_STACK_INVALID_PARAM;
534 *deviceProperties = (OCDeviceProperties*)OICCalloc(1, sizeof(OCDeviceProperties));
535 if (*deviceProperties)
537 OICStrcpy((*deviceProperties)->protocolIndependentId, UUID_STRING_SIZE, piid);
541 result = OC_STACK_NO_MEMORY;
544 OIC_LOG(DEBUG, TAG, "CreateDeviceProperties OUT");
549 static OCStackResult GenerateDeviceProperties(OCDeviceProperties **deviceProperties)
551 OCStackResult result = OC_STACK_OK;
552 OicUuid_t generatedProtocolIndependentId = {.id = {0}};
553 char* protocolIndependentId = NULL;
555 if (!deviceProperties)
557 return OC_STACK_INVALID_PARAM;
560 *deviceProperties = NULL;
562 // Generate a UUID for the Protocol Independent ID
563 if (OCGenerateUuid(generatedProtocolIndependentId.id))
565 protocolIndependentId = (char*)OICCalloc(UUID_STRING_SIZE, sizeof(char));
566 if (protocolIndependentId)
568 if (!OCConvertUuidToString(generatedProtocolIndependentId.id, protocolIndependentId))
570 OIC_LOG(ERROR, TAG, "ConvertUuidToStr failed");
571 result = OC_STACK_ERROR;
576 result = OC_STACK_NO_MEMORY;
581 OIC_LOG(FATAL, TAG, "Generate UUID for Device Properties Protocol Independent ID failed!");
582 result = OC_STACK_ERROR;
585 if (OC_STACK_OK == result)
587 result = CreateDeviceProperties(protocolIndependentId, deviceProperties);
588 if (OC_STACK_OK != result)
590 OIC_LOG(ERROR, TAG, "CreateDeviceProperties failed");
595 OICFreeAndSetToNull((void**)&protocolIndependentId);
600 OCStackResult CBORPayloadToDeviceProperties(const uint8_t *payload, size_t size, OCDeviceProperties **deviceProperties)
602 OCStackResult result = OC_STACK_OK;
603 CborError cborResult = CborNoError;
604 char* protocolIndependentId = NULL;
609 if (!payload || (size <= 0) || !deviceProperties)
611 return OC_STACK_INVALID_PARAM;
614 *deviceProperties = NULL;
616 cbor_parser_init(payload, size, 0, &parser, &dpCbor);
618 // Protocol Independent ID - Mandatory
619 cborResult = cbor_value_map_find_value(&dpCbor, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, &dpMap);
620 if ((CborNoError == cborResult) && cbor_value_is_text_string(&dpMap))
624 cborResult = cbor_value_dup_text_string(&dpMap, &protocolIndependentId, &len, NULL);
625 if (CborNoError != cborResult)
627 OIC_LOG(ERROR, TAG, "Failed to get Protocol Independent Id!");
628 result = OC_STACK_ERROR;
633 OIC_LOG(ERROR, TAG, "Protocol Independent Id is not present or invalid!");
634 result = OC_STACK_ERROR;
637 if (OC_STACK_OK == result)
639 result = CreateDeviceProperties(protocolIndependentId, deviceProperties);
640 if (OC_STACK_OK != result)
642 OIC_LOG(ERROR, TAG, "CreateDeviceProperties failed");
647 OICFreeAndSetToNull((void**)&protocolIndependentId);
652 OCStackResult DevicePropertiesToCBORPayload(const OCDeviceProperties *deviceProperties, uint8_t **payload, size_t *size)
654 OCStackResult result = OC_STACK_OK;
655 CborError cborResult = CborNoError;
656 uint8_t *cborPayload = NULL;
657 size_t cborLen = CBOR_SIZE;
661 if (!deviceProperties || !payload || !size || (*size > CBOR_MAX_SIZE))
663 return OC_STACK_INVALID_PARAM;
666 // Reset the CBOR length if we need to
675 cborPayload = (uint8_t*)OICCalloc(1, cborLen);
676 if (NULL != cborPayload)
678 cbor_encoder_init(&encoder, cborPayload, cborLen, 0);
680 // Create the Device Properties encoder map
681 cborResult = cbor_encoder_create_map(&encoder, &dpMap, CborIndefiniteLength);
682 if (CborNoError != cborResult)
684 OIC_LOG(ERROR, TAG, "Failed to create encoder map!");
685 result = OC_STACK_ERROR;
690 return OC_STACK_NO_MEMORY;
693 // Protocol Independent ID - Mandatory
694 if (OC_STACK_OK == result)
696 cborResult = cbor_encode_text_string(&dpMap, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, strlen(OC_RSRVD_PROTOCOL_INDEPENDENT_ID));
697 if (CborNoError == cborResult)
699 cborResult = cbor_encode_text_string(&dpMap,
700 deviceProperties->protocolIndependentId,
701 strlen(deviceProperties->protocolIndependentId));
702 if (CborNoError != cborResult)
704 OIC_LOG(ERROR, TAG, "Failed to encode protocolIndependentId!");
705 result = OC_STACK_ERROR;
710 OIC_LOG(ERROR, TAG, "Failed to encode OC_RSRVD_PROTOCOL_INDEPENDENT_ID!");
711 result = OC_STACK_ERROR;
715 // Encoding is finished
716 if (OC_STACK_OK == result)
718 cborResult = cbor_encoder_close_container(&encoder, &dpMap);
719 if (CborNoError != cborResult)
721 OIC_LOG(ERROR, TAG, "Failed to close dpMap container!");
722 result = OC_STACK_ERROR;
726 if (OC_STACK_OK == result)
728 *size = cbor_encoder_get_buffer_size(&encoder, cborPayload);
729 *payload = cborPayload;
732 else if ((CborErrorOutOfMemory == cborResult) && (cborLen < CBOR_MAX_SIZE))
734 OICFreeAndSetToNull((void**)&cborPayload);
736 // Realloc and try again
737 cborLen += cbor_encoder_get_buffer_size(&encoder, encoder.end);
738 result = DevicePropertiesToCBORPayload(deviceProperties, payload, &cborLen);
739 if (OC_STACK_OK == result)
746 OICFreeAndSetToNull((void**)&cborPayload);
752 static OCStackResult UpdateDeviceInfoResourceWithDeviceProperties(const OCDeviceProperties *deviceProperties, bool updateDatabase)
754 OCStackResult result = OC_STACK_OK;
755 OCResource *resource = NULL;
757 if (!deviceProperties)
759 return OC_STACK_INVALID_PARAM;
762 resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
765 result = SetAttributeInternal(resource, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, deviceProperties->protocolIndependentId, updateDatabase);
766 if (OC_STACK_OK != result)
768 OIC_LOG(ERROR, TAG, "OCSetPropertyValue failed to set Protocol Independent ID");
773 OIC_LOG(ERROR, TAG, "Resource does not exist.");
774 result = OC_STACK_NO_RESOURCE;
780 static OCStackResult ReadDevicePropertiesFromDatabase(OCDeviceProperties **deviceProperties)
782 uint8_t *data = NULL;
785 OCStackResult result = ReadDatabaseFromPS(OC_DEVICE_PROPS_FILE_NAME, OC_JSON_DEVICE_PROPS_NAME, &data, &size);
786 if (OC_STACK_OK == result)
788 // Read device properties from PS
789 result = CBORPayloadToDeviceProperties(data, size, deviceProperties);
790 if (OC_STACK_OK != result)
792 OIC_LOG(WARNING, TAG, "CBORPayloadToDeviceProperties failed");
797 OIC_LOG(ERROR, TAG, "ReadDatabaseFromPS failed");
801 OICFreeAndSetToNull((void**)&data);
806 static OCStackResult UpdateDevicePropertiesDatabase(const OCDeviceProperties *deviceProperties)
808 OCStackResult result = OC_STACK_OK;
809 uint8_t *payload = NULL;
812 if (!deviceProperties)
814 return OC_STACK_INVALID_PARAM;
817 // Check to see if persistent storage exists. If it doesn't then
818 // we just allow the device properties to exist in memory and
819 // it is the application's job to manage them.
820 if (!OCGetPersistentStorageHandler())
822 OIC_LOG(DEBUG, TAG, "Persistent Storage handler is NULL.");
826 // Convert OCDeviceProperties into CBOR to use for updating Persistent Storage
827 result = DevicePropertiesToCBORPayload(deviceProperties, &payload, &size);
828 if ((OC_STACK_OK == result) && payload)
830 result = UpdateResourceInPS(OC_DEVICE_PROPS_FILE_NAME, OC_JSON_DEVICE_PROPS_NAME, payload, size);
831 if (OC_STACK_OK != result)
833 OIC_LOG_V(ERROR, TAG, "UpdateResourceInPS failed with %d!", result);
838 OIC_LOG_V(ERROR, TAG, "DevicePropertiesToCBORPayload failed with %d!", result);
842 OICFreeAndSetToNull((void**)&payload);
847 OCStackResult InitializeDeviceProperties()
849 OIC_LOG(DEBUG, TAG, "InitializeDeviceProperties IN");
851 OCStackResult result = OC_STACK_OK;
852 OCDeviceProperties *deviceProperties = NULL;
853 bool updateDatabase = false;
855 // Populate OCDeviceProperties from persistent storage
856 result = ReadDevicePropertiesFromDatabase(&deviceProperties);
857 if (OC_STACK_OK != result)
859 OIC_LOG(ERROR, TAG, "ReadDevicePropertiesFromDatabase failed");
862 // If the device properties in persistent storage are corrupted or
863 // not available for some reason, a default OCDeviceProperties is created.
864 if ((OC_STACK_OK != result) || !deviceProperties)
866 result = GenerateDeviceProperties(&deviceProperties);
867 if (OC_STACK_OK == result)
869 updateDatabase = true;
873 OIC_LOG(ERROR, TAG, "GenerateDeviceProperties failed");
877 // Set the device properties information on the device info resource. This will
878 // also persist OCDeviceProperties so they can be used in the future if they are
879 // not already in the database.
880 if (OC_STACK_OK == result)
882 result = UpdateDeviceInfoResourceWithDeviceProperties(deviceProperties, updateDatabase);
883 if (OC_STACK_OK != result)
885 OIC_LOG(ERROR, TAG, "UpdateDeviceInfoResourceWithDeviceProperties failed");
890 CleanUpDeviceProperties(&deviceProperties);
892 OIC_LOG(DEBUG, TAG, "InitializeDeviceProperties OUT");
897 static size_t GetIntrospectionDataSize(const OCPersistentStorage *ps)
900 char buffer[INTROSPECTION_FILE_SIZE_BLOCK];
908 fp = ps->open(OC_INTROSPECTION_FILE_NAME, "rb");
911 size_t bytesRead = 0;
914 bytesRead = ps->read(buffer, 1, INTROSPECTION_FILE_SIZE_BLOCK, fp);
922 OCStackResult GetIntrospectionDataFromPS(char **data, size_t *size)
924 OIC_LOG(DEBUG, TAG, "GetIntrospectionDataFromPS IN");
927 uint8_t *fsData = NULL;
929 OCStackResult ret = OC_STACK_ERROR;
930 OCPersistentStorage *ps = NULL;
932 if (!data || *data || !size)
934 return OC_STACK_INVALID_PARAM;
937 ps = OCGetPersistentStorageHandler();
940 OIC_LOG(ERROR, TAG, "Persistent Storage handler is NULL");
944 fileSize = GetIntrospectionDataSize(ps);
945 OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
948 // allocate one more byte to accomodate null terminator for string we are reading.
949 fsData = (uint8_t *)OICCalloc(1, fileSize + 1);
952 OIC_LOG(ERROR, TAG, "Could not allocate memory for introspection data");
956 fp = ps->open(OC_INTROSPECTION_FILE_NAME, "rb");
959 OIC_LOG(ERROR, TAG, "Could not open persistent storage file for introspection data");
962 if (ps->read(fsData, 1, fileSize, fp) == fileSize)
965 fsData[fileSize] = '\0';
966 *data = (char *)fsData;
971 OIC_LOG(DEBUG, TAG, "GetIntrospectionDataFromPS OUT");
985 OCStackResult BuildIntrospectionPayloadResponse(const OCResource *resourcePtr,
986 OCRepPayload** payload, OCDevAddr *devAddr)
988 OC_UNUSED(resourcePtr);
991 char *introspectionData = NULL;
993 OCStackResult ret = GetIntrospectionDataFromPS(&introspectionData, &size);
994 if (OC_STACK_OK == ret)
996 OCRepPayload *tempPayload = OCRepPayloadCreate();
999 if (OCRepPayloadSetPropStringAsOwner(tempPayload, OC_RSRVD_INTROSPECTION_DATA_NAME, introspectionData))
1001 *payload = tempPayload;
1005 OCRepPayloadDestroy(tempPayload);
1006 ret = OC_STACK_ERROR;
1011 ret = OC_STACK_NO_MEMORY;
1014 if (ret != OC_STACK_OK)
1016 OICFree(introspectionData);
1022 OCRepPayload *BuildUrlInfoWithProtocol(const char *protocol)
1024 OCStackResult result = OC_STACK_OK;
1025 OCRepPayload *urlInfoPayload = OCRepPayloadCreate();
1026 if (!urlInfoPayload)
1028 OIC_LOG(ERROR, TAG, "Failed to create a new RepPayload");
1029 result = OC_STACK_NO_MEMORY;
1033 if (!OCRepPayloadSetPropString(urlInfoPayload, OC_RSRVD_INTROSPECTION_URL, OC_RSRVD_INTROSPECTION_PAYLOAD_URI))
1035 OIC_LOG(ERROR, TAG, "Failed to add url");
1036 result = OC_STACK_ERROR;
1039 if (!OCRepPayloadSetPropString(urlInfoPayload, OC_RSRVD_INTROSPECTION_PROTOCOL, protocol))
1041 OIC_LOG(ERROR, TAG, "Failed to add protocol");
1042 result = OC_STACK_ERROR;
1045 if (!OCRepPayloadSetPropString(urlInfoPayload, OC_RSRVD_INTROSPECTION_CONTENT_TYPE, OC_RSRVD_INTROSPECTION_CONTENT_TYPE_VALUE))
1047 OIC_LOG(ERROR, TAG, "Failed to add content type");
1048 result = OC_STACK_ERROR;
1051 if (!OCRepPayloadSetPropInt(urlInfoPayload, OC_RSRVD_INTROSPECTION_VERSION, OC_RSRVD_INTROSPECTION_VERSION_VALUE))
1053 OIC_LOG(ERROR, TAG, "Failed to add version");
1054 result = OC_STACK_ERROR;
1059 if (result != OC_STACK_OK)
1061 OCRepPayloadDestroy(urlInfoPayload);
1062 urlInfoPayload = NULL;
1064 return urlInfoPayload;
1067 OCStackResult AddProtocolToLL(OCStringLL **protoLL, const char *protocol)
1069 OCStringLL* cur = *protoLL;
1070 // Check if protocol is already in list
1073 if (strcmp(cur->value, protocol) == 0)
1081 // The intent of the protocol list is to collect all unique protocols available on this
1082 // endpoint. Set an error that can be used to skip processing this protocol further as
1083 // it already exists in the list.
1084 return OC_STACK_INVALID_PARAM;
1088 cur = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1091 return OC_STACK_NO_MEMORY;
1095 cur->value = OICStrdup(protocol);
1099 return OC_STACK_NO_MEMORY;
1102 cur->next = *protoLL;
1107 void FreeProtocolLL(OCStringLL *protoLL)
1109 OCStringLL* cur = protoLL;
1112 OCStringLL *temp = cur;
1114 OICFree(temp->value);
1119 OCStackResult BuildIntrospectionResponseRepresentation(const OCResource *resourcePtr,
1120 OCRepPayload** payload, OCDevAddr *devAddr)
1124 size_t dimensions[3] = { 0, 0, 0 };
1125 OCRepPayload *tempPayload = NULL;
1126 OCRepPayload **urlInfoPayload = NULL;
1127 OCStringLL *protoLL = NULL;
1128 OCStackResult ret = OC_STACK_OK;
1129 OCResourceType *resType = NULL;
1130 OCResourceInterface *resInterface = NULL;
1134 ret = OC_STACK_INVALID_PARAM;
1138 tempPayload = OCRepPayloadCreate();
1141 ret = OC_STACK_NO_MEMORY;
1145 if (!OCRepPayloadSetUri(tempPayload, resourcePtr->uri))
1147 OIC_LOG(ERROR, TAG, "Failed to set payload URI");
1148 ret = OC_STACK_ERROR;
1152 resType = resourcePtr->rsrcType;
1155 if (!OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename))
1157 OIC_LOG(ERROR, TAG, "Failed at add resource type");
1158 ret = OC_STACK_ERROR;
1161 resType = resType->next;
1164 resInterface = resourcePtr->rsrcInterface;
1165 while (resInterface)
1167 if (!OCRepPayloadAddInterface(tempPayload, resInterface->name))
1169 OIC_LOG(ERROR, TAG, "Failed to add interface");
1170 ret = OC_STACK_ERROR;
1173 resInterface = resInterface->next;
1175 if (!OCRepPayloadSetPropString(tempPayload, OC_RSRVD_INTROSPECTION_NAME, OC_RSRVD_INTROSPECTION_NAME_VALUE))
1177 OIC_LOG(ERROR, TAG, "Failed to set Name property.");
1178 ret = OC_STACK_ERROR;
1182 // Figure out which protocols this endpoint supports
1183 if (resourcePtr->endpointType & OC_COAP)
1185 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAP_STR))
1190 if (resourcePtr->endpointType & OC_COAPS)
1192 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAPS_STR))
1198 if (resourcePtr->endpointType & OC_COAP_TCP)
1200 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAP_STR))
1205 if (resourcePtr->endpointType & OC_COAPS_TCP)
1207 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAPS_STR))
1214 if (resourcePtr->endpointType & OC_HTTP)
1216 if (OC_STACK_OK == AddProtocolToLL(&protoLL, HTTP_STR))
1221 if (resourcePtr->endpointType & OC_HTTPS)
1223 if (OC_STACK_OK == AddProtocolToLL(&protoLL, HTTPS_STR))
1230 if (resourcePtr->endpointType & OC_COAP_RFCOMM)
1232 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAP_STR))
1238 // Add a urlInfo object for each protocol supported
1239 urlInfoPayload = (OCRepPayload **)OICMalloc(dimensions[0] * sizeof(OCRepPayload));
1242 OCStringLL *proto = protoLL;
1246 urlInfoPayload[i] = BuildUrlInfoWithProtocol(proto->value);
1247 if (!urlInfoPayload[i])
1249 OIC_LOG(ERROR, TAG, "Unable to build urlInfo object for protocol");
1250 ret = OC_STACK_ERROR;
1253 proto = proto->next;
1256 if (!OCRepPayloadSetPropObjectArrayAsOwner(tempPayload,
1257 OC_RSRVD_INTROSPECTION_URL_INFO,
1261 OIC_LOG(ERROR, TAG, "Unable to add urlInfo object to introspection payload ");
1262 ret = OC_STACK_ERROR;
1268 OIC_LOG(ERROR, TAG, "Unable to allocate memory for urlInfo ");
1269 ret = OC_STACK_NO_MEMORY;
1275 *payload = tempPayload;
1279 OCRepPayloadAppend(*payload, tempPayload);
1282 if (ret != OC_STACK_OK)
1284 OCRepPayloadDestroy(tempPayload);
1287 OICFree(urlInfoPayload);
1290 FreeProtocolLL(protoLL);
1295 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
1296 OCDiscoveryPayload *payload,
1298 CAEndpoint_t *networkInfo,
1301 if (!resourcePtr || !payload)
1303 return OC_STACK_INVALID_PARAM;
1305 uint16_t securePort = 0;
1306 if (resourcePtr->resourceProperties & OC_SECURE)
1308 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
1314 bool isVirtual = false;
1315 if (GetTypeOfVirtualURI(resourcePtr->uri) != OC_UNKNOWN_URI)
1320 uint16_t tcpPort = 0;
1321 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
1323 OCDiscoveryPayloadAddResourceWithEps(payload, resourcePtr, securePort,
1324 isVirtual, networkInfo, infoSize, devAddr, tcpPort);
1326 OCDiscoveryPayloadAddResourceWithEps(payload, resourcePtr, securePort,
1327 isVirtual, networkInfo, infoSize, devAddr);
1333 OCResource *FindResourceByUri(const char* resourceUri)
1340 OCResource * pointer = headResource;
1343 if (strcmp(resourceUri, pointer->uri) == 0)
1347 pointer = pointer->next;
1349 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
1353 OCStackResult CheckRequestsEndpoint(const OCDevAddr *reqDevAddr,
1354 OCTpsSchemeFlags resTpsFlags)
1358 OIC_LOG(ERROR, TAG, "OCDevAddr* is NULL!!!");
1359 return OC_STACK_INVALID_PARAM;
1362 OCTpsSchemeFlags reqTpsFlags = OC_NO_TPS;
1363 OCStackResult result = OCGetMatchedTpsFlags((CATransportAdapter_t)reqDevAddr->adapter,
1364 (CATransportFlags_t)reqDevAddr->flags, &reqTpsFlags);
1366 if (result != OC_STACK_OK)
1368 OIC_LOG_V(ERROR, TAG, "Failed at get TPS flags. errcode is %d", result);
1372 // bit compare between request tps flags and resource tps flags
1373 if (reqTpsFlags & resTpsFlags)
1375 OIC_LOG(INFO, TAG, "Request come from registered TPS");
1380 OIC_LOG(ERROR, TAG, "Request come from unregistered TPS!!!");
1381 return OC_STACK_BAD_ENDPOINT;
1385 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
1386 ResourceHandling *handling,
1387 OCResource **resource)
1389 if(!request || !handling || !resource)
1391 return OC_STACK_INVALID_PARAM;
1394 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
1396 // Check if virtual resource
1397 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
1399 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
1400 *handling = OC_RESOURCE_VIRTUAL;
1401 *resource = headResource;
1404 if (strlen((const char*)(request->resourceUrl)) == 0)
1406 // Resource URL not specified
1407 *handling = OC_RESOURCE_NOT_SPECIFIED;
1408 return OC_STACK_NO_RESOURCE;
1412 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
1413 *resource = resourcePtr;
1415 // Checking resource TPS flags if resource exist in stack.
1418 OCStackResult result = CheckRequestsEndpoint(&(request->devAddr), resourcePtr->endpointType);
1420 if (result != OC_STACK_OK)
1422 if (result == OC_STACK_BAD_ENDPOINT)
1424 OIC_LOG(ERROR, TAG, "Request come from bad endpoint. ignore request!!!");
1425 return OC_STACK_BAD_ENDPOINT;
1429 OIC_LOG_V(ERROR, TAG, "Failed at get tps flag errcode: %d", result);
1437 if(defaultDeviceHandler)
1439 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
1443 // Resource does not exist
1444 // and default device handler does not exist
1445 *handling = OC_RESOURCE_NOT_SPECIFIED;
1446 return OC_STACK_NO_RESOURCE;
1449 if (resourcePtr && resourcePtr->rsrcChildResourcesHead != NULL)
1451 // Collection resource
1452 if (resourcePtr->entityHandler != defaultResourceEHandler)
1454 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
1459 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
1465 // Resource not a collection
1466 if (resourcePtr->entityHandler != defaultResourceEHandler)
1468 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
1473 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
1480 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
1482 OCStackResult result;
1489 result = OC_STACK_OK;
1492 result = OC_STACK_SLOW_RESOURCE;
1495 result = OC_STACK_ERROR;
1497 case OC_EH_FORBIDDEN:
1498 result = OC_STACK_FORBIDDEN_REQ;
1500 case OC_EH_INTERNAL_SERVER_ERROR:
1501 result = OC_STACK_INTERNAL_SERVER_ERROR;
1503 case OC_EH_RESOURCE_CREATED:
1504 result = OC_STACK_RESOURCE_CREATED;
1506 case OC_EH_RESOURCE_DELETED:
1507 result = OC_STACK_RESOURCE_DELETED;
1510 result = OC_STACK_RESOURCE_CHANGED;
1512 case OC_EH_RESOURCE_NOT_FOUND:
1513 result = OC_STACK_NO_RESOURCE;
1516 result = OC_STACK_ERROR;
1522 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
1529 // Null is analogous to no filter.i.e. query is of form /oic/res?if=oic.if.baseline or /oic/res,
1530 // without rt query.
1531 if (NULL == resourceTypeFilter)
1536 // Empty resourceType filter is analogous to error query
1537 // It is an error as query is of form /oic/res?rt=
1538 if (0 == strlen(resourceTypeFilter))
1543 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
1545 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
1551 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
1555 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
1562 // Null is analogous to no filter i.e. query is of form /oic/res?rt=core.light or /oic/res,
1563 // without if query.
1564 if (NULL == interfaceFilter)
1569 // Empty interface filter is analogous to error query
1570 // It is an error as query is of form /oic/res?if=
1571 if (0 == strlen(interfaceFilter))
1576 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
1578 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
1579 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
1580 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
1586 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
1591 * If the filters are null, they will be assumed to NOT be present
1592 * and the resource will not be matched against them.
1593 * Function will return true if all non null AND non empty filters passed in find a match.
1595 static bool includeThisResourceInResponse(OCResource *resource,
1596 char *interfaceFilter,
1597 char *resourceTypeFilter)
1601 OIC_LOG(ERROR, TAG, "Invalid resource");
1605 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
1608 * At least one valid filter should be available to
1609 * include the resource in discovery response
1611 if (!(resourceTypeFilter && *resourceTypeFilter))
1613 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
1614 resource", resource->uri);
1618 else if (!(resource->resourceProperties & OC_ACTIVE) ||
1619 !(resource->resourceProperties & OC_DISCOVERABLE))
1621 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
1625 return resourceMatchesIFFilter(resource, interfaceFilter) &&
1626 resourceMatchesRTFilter(resource, resourceTypeFilter);
1629 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
1630 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
1632 OCEntityHandlerResponse *response = NULL;
1633 OCStackResult result = OC_STACK_ERROR;
1635 response = (OCEntityHandlerResponse *)OICCalloc(1, sizeof(*response));
1636 VERIFY_PARAM_NON_NULL(TAG, response, "Failed allocating OCEntityHandlerResponse");
1638 response->ehResult = ehResult;
1639 response->payload = discoveryPayload;
1640 response->persistentBufferFlag = 0;
1641 response->requestHandle = (OCRequestHandle) request;
1642 response->resourceHandle = (OCResourceHandle) resource;
1644 result = OCDoResponse(response);
1650 return OC_STACK_NO_MEMORY;
1653 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
1654 OCServerRequest *request, OCResource *resource)
1656 return FormOCEntityHandlerRequest(ehRequest,
1657 (OCRequestHandle)request,
1660 (OCResourceHandle)resource,
1664 request->payloadSize,
1665 request->numRcvdVendorSpecificHeaderOptions,
1666 request->rcvdVendorSpecificHeaderOptions,
1667 (OCObserveAction)(request->notificationFlag ? OC_OBSERVE_NO_OPTION :
1668 request->observationOption),
1675 * Find resources at the resource directory server. These resources are not local resources but
1678 * @param interfaceQuery The interface query parameter.
1679 * @param resourceTypeQuery The resourceType query parameter.
1680 * @param discPayload The payload that will be added with the resource information if found at RD.
1682 * @return ::OC_STACK_OK if any resources are found else ::OC_STACK_NO_RESOURCE.
1683 * In case if RD server is not started, it returns ::OC_STACK_NO_RESOURCE.
1685 static OCStackResult findResourcesAtRD(const char *interfaceQuery,
1686 const char *resourceTypeQuery, OCDiscoveryPayload **discPayload)
1688 OCStackResult result = OC_STACK_NO_RESOURCE;
1689 if (OCGetResourceHandleAtUri(OC_RSRVD_RD_URI) != NULL)
1691 result = OCRDDatabaseDiscoveryPayloadCreate(interfaceQuery, resourceTypeQuery,
1692 (*discPayload) ? &(*discPayload)->next : discPayload);
1694 if ((*discPayload) && (*discPayload)->resources)
1696 result = OC_STACK_OK;
1703 * Creates a discovery payload and add device id information. This information is included in all
1704 * /oic/res response.
1706 * @param payload payload that will have memory alllocated and device id information added.
1708 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
1709 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
1711 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
1715 OIC_LOG(DEBUG, TAG, "Payload is already allocated");
1719 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
1720 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
1723 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
1724 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
1725 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
1727 const char* uid = OCGetServerInstanceIDString();
1730 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
1736 OCPayloadDestroy(*payload);
1737 return OC_STACK_NO_MEMORY;
1741 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
1743 * @param discPayload payload that will have the baseline information included.
1745 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
1746 * allocating the memory for the baseline information.
1748 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
1752 OIC_LOG(ERROR, TAG, "Payload is not allocated");
1753 return OC_STACK_INVALID_PARAM;
1756 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **)&discPayload->name);
1758 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1759 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
1760 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
1761 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
1763 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
1764 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
1765 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
1770 return OC_STACK_NO_MEMORY;
1773 static bool isUnicast(OCServerRequest *request)
1775 bool isMulticast = request->devAddr.flags & OC_MULTICAST;
1776 return (isMulticast == false &&
1777 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
1778 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE));
1781 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
1783 if (!request || !resource)
1785 return OC_STACK_INVALID_PARAM;
1788 OCPayload* payload = NULL;
1789 char *interfaceQuery = NULL;
1790 char *resourceTypeQuery = NULL;
1792 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
1794 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
1797 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
1799 // Received request for a keepalive
1800 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
1801 return HandleKeepAliveRequest(request, resource);
1805 OCStackResult discoveryResult = OC_STACK_ERROR;
1806 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
1807 request->method == OC_REST_DELETE)
1809 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
1810 request->resourceUrl, request->method);
1811 return OC_STACK_UNAUTHORIZED_REQ;
1814 discoveryResult = HandleVirtualObserveRequest(request);
1815 if (discoveryResult == OC_STACK_DUPLICATE_REQUEST)
1817 // server requests are usually free'd when the response is sent out
1818 // for the request in ocserverrequest.c : HandleSingleResponse()
1819 // Since we are making an early return and not responding, the server request
1820 // needs to be deleted.
1821 FindAndDeleteServerRequest (request);
1822 discoveryResult = OC_STACK_OK;
1825 else if (discoveryResult != OC_STACK_OK)
1830 // Step 1: Generate the response to discovery request
1831 if (virtualUriInRequest == OC_WELL_KNOWN_URI
1833 || virtualUriInRequest == OC_MQ_BROKER_URI
1837 if (g_multicastServerStopped && !isUnicast(request))
1839 // Ignore the discovery request
1840 FindAndDeleteServerRequest(request);
1841 discoveryResult = OC_STACK_CONTINUE;
1845 CAEndpoint_t *networkInfo = NULL;
1846 size_t infoSize = 0;
1848 CAResult_t caResult = CAGetNetworkInformation(&networkInfo, &infoSize);
1849 if (CA_STATUS_FAILED == caResult)
1851 OIC_LOG(ERROR, TAG, "CAGetNetworkInformation has error on parsing network infomation");
1852 return OC_STACK_ERROR;
1855 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
1856 &interfaceQuery, &resourceTypeQuery);
1857 VERIFY_SUCCESS(discoveryResult);
1859 if (!interfaceQuery && !resourceTypeQuery)
1861 // If no query is sent, default interface is used i.e. oic.if.ll.
1862 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
1865 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
1866 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
1867 VERIFY_SUCCESS(discoveryResult);
1869 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
1870 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
1872 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
1873 VERIFY_SUCCESS(discoveryResult);
1875 OCResourceProperty prop = OC_DISCOVERABLE;
1877 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
1879 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
1881 // This case will handle when no resource type and it is oic.if.ll.
1882 // Do not assume check if the query is ll
1883 if (!resourceTypeQuery &&
1884 (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
1886 // Only include discoverable type
1887 if (resource->resourceProperties & prop)
1889 discoveryResult = BuildVirtualResourceResponse(resource,
1896 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
1898 discoveryResult = BuildVirtualResourceResponse(resource,
1906 discoveryResult = OC_STACK_OK;
1909 if (discPayload->resources == NULL)
1911 discoveryResult = OC_STACK_NO_RESOURCE;
1912 OCPayloadDestroy(payload);
1918 OICFree(networkInfo);
1921 discoveryResult = findResourcesAtRD(interfaceQuery, resourceTypeQuery, (OCDiscoveryPayload **)&payload);
1924 else if (virtualUriInRequest == OC_DEVICE_URI)
1926 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1927 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1928 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1930 else if (virtualUriInRequest == OC_PLATFORM_URI)
1932 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1933 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1934 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1936 #ifdef ROUTING_GATEWAY
1937 else if (OC_GATEWAY_URI == virtualUriInRequest)
1939 // Received request for a gateway
1940 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1941 discoveryResult = RMHandleGatewayRequest(request, resource);
1944 else if (OC_INTROSPECTION_URI == virtualUriInRequest)
1946 // Received request for introspection
1947 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_INTROSPECTION_URI);
1948 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Introspection URI not found.");
1949 discoveryResult = BuildIntrospectionResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
1950 OIC_LOG(INFO, TAG, "Request is for Introspection");
1952 else if (OC_INTROSPECTION_PAYLOAD_URI == virtualUriInRequest)
1954 // Received request for introspection payload
1955 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_INTROSPECTION_PAYLOAD_URI);
1956 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Introspection Payload URI not found.");
1957 discoveryResult = BuildIntrospectionPayloadResponse(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
1958 OIC_LOG(INFO, TAG, "Request is for Introspection Payload");
1961 * Step 2: Send the discovery response
1963 * Iotivity should respond to discovery requests in below manner:
1964 * 1)If query filter matching fails and discovery request is multicast,
1965 * it should NOT send any response.
1966 * 2)If query filter matching fails and discovery request is unicast,
1967 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1968 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1969 * request is multicast, it should NOT send any response.
1970 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1971 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1974 #ifdef WITH_PRESENCE
1975 if ((virtualUriInRequest == OC_PRESENCE) &&
1976 (resource->resourceProperties & OC_ACTIVE))
1978 // Need to send ACK when the request is CON.
1979 if (request->qos == OC_HIGH_QOS)
1981 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1982 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1983 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1984 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1986 FindAndDeleteServerRequest(request);
1988 // Presence uses observer notification api to respond via SendPresenceNotification.
1989 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1994 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1995 if (OC_GATEWAY_URI != virtualUriInRequest)
1998 OIC_LOG_PAYLOAD(DEBUG, payload);
1999 if(discoveryResult == OC_STACK_OK)
2001 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
2003 else // Error handling
2005 if (isUnicast(request))
2007 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
2008 discoveryResult, virtualUriInRequest);
2009 SendNonPersistantDiscoveryResponse(request, resource, NULL,
2010 (discoveryResult == OC_STACK_NO_RESOURCE) ?
2011 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
2015 // Ignoring the discovery request as per RFC 7252, Section #8.2
2016 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
2017 // the request should be removed.
2018 // since it never remove and causes a big memory waste.
2019 FindAndDeleteServerRequest(request);
2021 discoveryResult = OC_STACK_CONTINUE;
2028 OICFree(interfaceQuery);
2031 if (resourceTypeQuery)
2033 OICFree(resourceTypeQuery);
2035 OCPayloadDestroy(payload);
2037 // To ignore the message, OC_STACK_CONTINUE is sent
2038 return discoveryResult;
2041 static OCStackResult
2042 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
2046 return OC_STACK_INVALID_PARAM;
2049 OCEntityHandlerResult ehResult = OC_EH_ERROR;
2050 OCEntityHandlerRequest ehRequest = {0};
2051 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
2052 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
2053 VERIFY_SUCCESS(result);
2055 // At this point we know for sure that defaultDeviceHandler exists
2056 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
2057 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
2058 if(ehResult == OC_EH_SLOW)
2060 OIC_LOG(INFO, TAG, "This is a slow resource");
2061 request->slowFlag = 1;
2063 else if(ehResult == OC_EH_ERROR)
2065 FindAndDeleteServerRequest(request);
2067 result = EntityHandlerCodeToOCStackCode(ehResult);
2069 OCPayloadDestroy(ehRequest.payload);
2073 static OCStackResult
2074 HandleResourceWithEntityHandler(OCServerRequest *request,
2075 OCResource *resource)
2077 if(!request || ! resource)
2079 return OC_STACK_INVALID_PARAM;
2082 OCStackResult result = OC_STACK_ERROR;
2083 OCEntityHandlerResult ehResult = OC_EH_ERROR;
2084 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
2085 ResourceObserver *resObs = NULL;
2087 OCEntityHandlerRequest ehRequest = {0};
2089 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
2090 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
2091 // check the security resource
2092 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
2094 type = PAYLOAD_TYPE_SECURITY;
2097 result = EHRequest(&ehRequest, type, request, resource);
2098 VERIFY_SUCCESS(result);
2100 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
2102 OIC_LOG(INFO, TAG, "No observation requested");
2103 ehFlag = OC_REQUEST_FLAG;
2105 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
2107 OIC_LOG(INFO, TAG, "Observation registration requested");
2109 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
2110 request->tokenLength);
2114 OIC_LOG (INFO, TAG, "Observer with this token already present");
2115 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
2116 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
2117 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
2119 // server requests are usually free'd when the response is sent out
2120 // for the request in ocserverrequest.c : HandleSingleResponse()
2121 // Since we are making an early return and not responding, the server request
2122 // needs to be deleted.
2123 FindAndDeleteServerRequest (request);
2127 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
2128 VERIFY_SUCCESS(result);
2130 result = AddObserver ((const char*)(request->resourceUrl),
2131 (const char *)(request->query),
2132 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
2133 resource, request->qos, request->acceptFormat,
2134 request->acceptVersion, &request->devAddr);
2136 if(result == OC_STACK_OK)
2138 OIC_LOG(INFO, TAG, "Added observer successfully");
2139 request->observeResult = OC_STACK_OK;
2140 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
2142 else if (result == OC_STACK_RESOURCE_ERROR)
2144 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
2145 request->observeResult = OC_STACK_ERROR;
2146 ehFlag = OC_REQUEST_FLAG;
2150 // The error in observeResult for the request will be used when responding to this
2151 // request by omitting the observation option/sequence number.
2152 request->observeResult = OC_STACK_ERROR;
2153 OIC_LOG(ERROR, TAG, "Observer Addition failed");
2154 ehFlag = OC_REQUEST_FLAG;
2155 FindAndDeleteServerRequest(request);
2160 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
2162 OIC_LOG(INFO, TAG, "Deregistering observation requested");
2164 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
2168 // Stack does not contain this observation request
2169 // Either token is incorrect or observation list is corrupted
2170 result = OC_STACK_ERROR;
2173 ehRequest.obsInfo.obsId = resObs->observeId;
2174 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
2176 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
2178 if(result == OC_STACK_OK)
2180 OIC_LOG(INFO, TAG, "Removed observer successfully");
2181 request->observeResult = OC_STACK_OK;
2182 // There should be no observe option header for de-registration response.
2183 // Set as an invalid value here so we can detect it later and remove the field in response.
2184 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
2188 request->observeResult = OC_STACK_ERROR;
2189 OIC_LOG(ERROR, TAG, "Observer Removal failed");
2190 FindAndDeleteServerRequest(request);
2196 result = OC_STACK_ERROR;
2200 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
2201 if(ehResult == OC_EH_SLOW)
2203 OIC_LOG(INFO, TAG, "This is a slow resource");
2204 request->slowFlag = 1;
2206 else if(ehResult == OC_EH_ERROR)
2208 FindAndDeleteServerRequest(request);
2210 result = EntityHandlerCodeToOCStackCode(ehResult);
2212 OCPayloadDestroy(ehRequest.payload);
2216 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
2217 OCResource *resource)
2219 if (!request || !resource)
2221 return OC_STACK_INVALID_PARAM;
2224 OCEntityHandlerRequest ehRequest = {0};
2225 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
2226 if(result == OC_STACK_OK)
2228 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
2231 OCPayloadDestroy(ehRequest.payload);
2236 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
2238 OCStackResult ret = OC_STACK_OK;
2240 switch (resHandling)
2242 case OC_RESOURCE_VIRTUAL:
2244 ret = HandleVirtualResource (request, resource);
2247 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
2249 ret = HandleDefaultDeviceEntityHandler(request);
2252 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
2254 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
2255 return OC_STACK_ERROR;
2257 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
2259 ret = HandleResourceWithEntityHandler (request, resource);
2262 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
2264 ret = HandleResourceWithEntityHandler (request, resource);
2267 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
2269 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
2272 case OC_RESOURCE_NOT_SPECIFIED:
2274 ret = OC_STACK_NO_RESOURCE;
2279 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
2280 return OC_STACK_ERROR;
2286 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
2288 OCResource *resource = NULL;
2289 if (!info.platformID || !info.manufacturerName)
2291 OIC_LOG(ERROR, TAG, "No value specified.");
2294 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
2296 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
2299 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
2300 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
2301 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
2302 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2303 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2304 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2305 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2306 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
2308 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
2313 * @todo (IOT-1541) There are several versions of a UUID structure and conversion
2314 * methods scattered around the IoTivity code base. They need to be combined
2317 uint8_t uuid[UUID_SIZE];
2318 if (!OCConvertStringToUuid(info.platformID, uuid))
2320 OIC_LOG(ERROR, TAG, "Platform ID is not a UUID.");
2324 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
2327 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
2330 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
2331 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
2332 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
2333 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
2334 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
2335 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
2336 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
2337 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
2338 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
2339 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
2340 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
2341 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
2342 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
2346 return OC_STACK_INVALID_PARAM;
2349 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
2351 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
2354 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
2357 if (!info.deviceName || info.deviceName[0] == '\0')
2359 OIC_LOG(ERROR, TAG, "Null or empty device name.");
2360 return OC_STACK_INVALID_PARAM;
2363 if (OCGetServerInstanceIDString() == NULL)
2365 OIC_LOG(INFO, TAG, "Device ID generation failed");
2369 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
2370 for (OCStringLL *temp = info.types; temp; temp = temp->next)
2374 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
2377 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
2378 info.specVersion: OC_SPEC_VERSION));
2380 if (info.dataModelVersions)
2382 char *dmv = OCCreateString(info.dataModelVersions);
2383 VERIFY_PARAM_NON_NULL(TAG, dmv, "Failed allocating dataModelVersions");
2384 OCStackResult r = OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dmv);
2390 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
2391 OC_DATA_MODEL_VERSION));
2393 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
2397 return OC_STACK_ERROR;
2400 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
2402 if (!resource || !attribute)
2404 return OC_STACK_INVALID_PARAM;
2406 if (0 == strlen(attribute))
2408 return OC_STACK_INVALID_PARAM;
2410 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
2412 if (0 == strcmp(attribute, temp->attrName))
2414 // A special case as this type return OCStringLL
2415 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
2417 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
2422 *value = OICStrdup((char *)temp->attrValue);
2427 return OC_STACK_NO_RESOURCE;
2430 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
2434 return OC_STACK_INVALID_PARAM;
2436 if (strlen(prop) == 0)
2438 return OC_STACK_INVALID_PARAM;
2444 OCStackResult res = OC_STACK_NO_RESOURCE;
2445 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
2447 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
2448 OCResource *resource = FindResourceByUri(pathType);
2451 return OC_STACK_NO_RESOURCE;
2454 res = OCGetAttribute(resource, prop, value);
2459 static OCStackResult SetAttributeInternal(OCResource *resource,
2460 const char *attribute,
2462 bool updateDatabase)
2464 OCAttribute *resAttrib = NULL;
2466 // See if the attribute already exists in the list.
2467 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
2469 if (0 == strcmp(attribute, resAttrib->attrName))
2471 // Found, free the old value.
2472 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
2474 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
2478 OICFree((char *)resAttrib->attrValue);
2484 // If not already in the list, add it.
2485 if (NULL == resAttrib)
2487 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
2488 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
2489 resAttrib->attrName = OICStrdup(attribute);
2490 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
2491 resAttrib->next = resource->rsrcAttributes;
2492 resource->rsrcAttributes = resAttrib;
2495 // Fill in the new value.
2496 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
2498 resAttrib->attrValue = OCCreateOCStringLL((char *)value);
2502 resAttrib->attrValue = OICStrdup((char *)value);
2504 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
2506 // The resource has changed from what is stored in the database. Update the database to
2507 // reflect the new value.
2510 OCDeviceProperties *deviceProperties = NULL;
2512 OCStackResult result = CreateDeviceProperties((const char*)value, &deviceProperties);
2513 if (OC_STACK_OK == result)
2515 result = UpdateDevicePropertiesDatabase(deviceProperties);
2516 if (OC_STACK_OK != result)
2518 OIC_LOG(ERROR, TAG, "UpdateDevicePropertiesDatabase failed!");
2521 CleanUpDeviceProperties(&deviceProperties);
2525 OIC_LOG(ERROR, TAG, "CreateDeviceProperties failed!");
2532 OCDeleteResourceAttributes(resAttrib);
2533 return OC_STACK_NO_MEMORY;
2536 static OCStackResult IsDatabaseUpdateNeeded(const char *attribute, const void *value, bool *update)
2538 OCStackResult result = OC_STACK_OK;
2539 void *currentPIID = NULL;
2541 if (!attribute || !value || !update)
2543 return OC_STACK_INVALID_PARAM;
2548 // Protocol Independent ID
2549 if (0 == strcmp(OC_RSRVD_PROTOCOL_INDEPENDENT_ID, attribute))
2551 result = OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, ¤tPIID);
2552 if (OC_STACK_OK == result)
2554 // PIID has already been set on the resource and stored in the database. Check to see
2555 // if the value is changing so the database can be updated accordingly.
2556 if (0 != strcmp((char *)currentPIID, (char*)value))
2561 else if (OC_STACK_NO_RESOURCE == result)
2563 // PIID has not been set yet so we should always update the database.
2565 result = OC_STACK_OK;
2569 OIC_LOG_V(ERROR, TAG,
2570 "Call to OCGetPropertyValue for the current PIID failed with error: %d", result);
2575 OICFreeAndSetToNull(¤tPIID);
2580 OCStackResult OCSetAttribute(OCResource *resource, const char *attribute, const void *value)
2582 bool updateDatabase = false;
2584 // Check to see if we also need to update the database for this attribute. If the current
2585 // value matches what is stored in the database we can skip the update and an unnecessary
2587 if (OC_STACK_OK != IsDatabaseUpdateNeeded(attribute, value, &updateDatabase))
2589 OIC_LOG_V(WARNING, TAG,
2590 "Could not determine if a database update was needed for %s. Proceeding without updating the database.",
2592 updateDatabase = false;
2595 return SetAttributeInternal(resource, attribute, value, updateDatabase);
2598 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
2600 if (!prop || !value)
2602 return OC_STACK_INVALID_PARAM;
2604 if (strlen(prop) == 0)
2606 return OC_STACK_INVALID_PARAM;
2609 OCStackResult res = OC_STACK_ERROR;
2610 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
2612 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
2613 OCResource *resource = FindResourceByUri(pathType);
2616 OIC_LOG(ERROR, TAG, "Resource does not exist.");
2620 res = OCSetAttribute(resource, prop, value);