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 result = 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);
990 OCRepPayload *tempPayload = NULL;
992 char *introspectionData = NULL;
994 ret = GetIntrospectionDataFromPS(&introspectionData, &size);
995 if (OC_STACK_OK == ret)
997 OCRepPayload *tempPayload = OCRepPayloadCreate();
1000 if (OCRepPayloadSetPropStringAsOwner(tempPayload, OC_RSRVD_INTROSPECTION_DATA_NAME, introspectionData))
1002 *payload = tempPayload;
1007 ret = OC_STACK_NO_MEMORY;
1010 if (ret != OC_STACK_OK)
1012 OICFree(introspectionData);
1013 OCRepPayloadDestroy(tempPayload);
1019 OCRepPayload *BuildUrlInfoWithProtocol(const char *protocol)
1021 OCStackResult result = OC_STACK_OK;
1022 OCRepPayload *urlInfoPayload = OCRepPayloadCreate();
1023 if (!urlInfoPayload)
1025 OIC_LOG(ERROR, TAG, "Failed to create a new RepPayload");
1026 result = OC_STACK_NO_MEMORY;
1030 if (!OCRepPayloadSetPropString(urlInfoPayload, OC_RSRVD_INTROSPECTION_URL, OC_RSRVD_INTROSPECTION_PAYLOAD_URI))
1032 OIC_LOG(ERROR, TAG, "Failed to add url");
1033 result = OC_STACK_ERROR;
1036 if (!OCRepPayloadSetPropString(urlInfoPayload, OC_RSRVD_INTROSPECTION_PROTOCOL, protocol))
1038 OIC_LOG(ERROR, TAG, "Failed to add protocol");
1039 result = OC_STACK_ERROR;
1042 if (!OCRepPayloadSetPropString(urlInfoPayload, OC_RSRVD_INTROSPECTION_CONTENT_TYPE, OC_RSRVD_INTROSPECTION_CONTENT_TYPE_VALUE))
1044 OIC_LOG(ERROR, TAG, "Failed to add content type");
1045 result = OC_STACK_ERROR;
1048 if (!OCRepPayloadSetPropInt(urlInfoPayload, OC_RSRVD_INTROSPECTION_VERSION, OC_RSRVD_INTROSPECTION_VERSION_VALUE))
1050 OIC_LOG(ERROR, TAG, "Failed to add version");
1051 result = OC_STACK_ERROR;
1056 if (result != OC_STACK_OK)
1058 OCRepPayloadDestroy(urlInfoPayload);
1059 urlInfoPayload = NULL;
1061 return urlInfoPayload;
1064 OCStackResult AddProtocolToLL(OCStringLL **protoLL, const char *protocol)
1066 OCStringLL* cur = *protoLL;
1067 // Check if protocol is already in list
1070 if (strcmp(cur->value, protocol) == 0)
1078 // The intent of the protocol list is to collect all unique protocols available on this
1079 // endpoint. Set an error that can be used to skip processing this protocol further as
1080 // it already exists in the list.
1081 return OC_STACK_INVALID_PARAM;
1085 cur = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1088 return OC_STACK_NO_MEMORY;
1092 cur->value = OICStrdup(protocol);
1096 return OC_STACK_NO_MEMORY;
1099 cur->next = *protoLL;
1104 void FreeProtocolLL(OCStringLL *protoLL)
1106 OCStringLL* cur = protoLL;
1109 OCStringLL *temp = cur;
1111 OICFree(temp->value);
1116 OCStackResult BuildIntrospectionResponseRepresentation(const OCResource *resourcePtr,
1117 OCRepPayload** payload, OCDevAddr *devAddr)
1120 size_t dimensions[3] = { 0, 0, 0 };
1121 OCRepPayload *tempPayload = NULL;
1122 OCRepPayload **urlInfoPayload = NULL;
1123 OCStringLL *protoLL = NULL;
1124 OCStackResult ret = OC_STACK_OK;
1125 OCResourceType *resType = NULL;
1126 OCResourceInterface *resInterface = NULL;
1130 ret = OC_STACK_INVALID_PARAM;
1134 tempPayload = OCRepPayloadCreate();
1137 ret = OC_STACK_NO_MEMORY;
1141 if (!OCRepPayloadSetUri(tempPayload, resourcePtr->uri))
1143 OIC_LOG(ERROR, TAG, "Failed to set payload URI");
1144 ret = OC_STACK_ERROR;
1148 resType = resourcePtr->rsrcType;
1151 if (!OCRepPayloadAddResourceType(tempPayload, resType->resourcetypename))
1153 OIC_LOG(ERROR, TAG, "Failed at add resource type");
1154 ret = OC_STACK_ERROR;
1157 resType = resType->next;
1160 resInterface = resourcePtr->rsrcInterface;
1161 while (resInterface)
1163 if (!OCRepPayloadAddInterface(tempPayload, resInterface->name))
1165 OIC_LOG(ERROR, TAG, "Failed to add interface");
1166 ret = OC_STACK_ERROR;
1169 resInterface = resInterface->next;
1171 if (!OCRepPayloadSetPropString(tempPayload, OC_RSRVD_INTROSPECTION_NAME, OC_RSRVD_INTROSPECTION_NAME_VALUE))
1173 OIC_LOG(ERROR, TAG, "Failed to set Name property.");
1174 ret = OC_STACK_ERROR;
1178 // Figure out which protocols this endpoint supports
1179 if (resourcePtr->endpointType & OC_COAP)
1181 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAP_STR))
1186 if (resourcePtr->endpointType & OC_COAPS)
1188 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAPS_STR))
1194 if (resourcePtr->endpointType & OC_COAP_TCP)
1196 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAP_STR))
1201 if (resourcePtr->endpointType & OC_COAPS_TCP)
1203 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAPS_STR))
1210 if (resourcePtr->endpointType & OC_HTTP)
1212 if (OC_STACK_OK == AddProtocolToLL(&protoLL, HTTP_STR))
1217 if (resourcePtr->endpointType & OC_HTTPS)
1219 if (OC_STACK_OK == AddProtocolToLL(&protoLL, HTTPS_STR))
1226 if (resourcePtr->endpointType & OC_COAP_RFCOMM)
1228 if (OC_STACK_OK == AddProtocolToLL(&protoLL, COAP_STR))
1234 // Add a urlInfo object for each protocol supported
1235 urlInfoPayload = (OCRepPayload **)OICMalloc(dimensions[0] * sizeof(OCRepPayload));
1238 OCStringLL *proto = protoLL;
1242 urlInfoPayload[i] = BuildUrlInfoWithProtocol(proto->value);
1243 if (!urlInfoPayload[i])
1245 OIC_LOG(ERROR, TAG, "Unable to build urlInfo object for protocol");
1246 ret = OC_STACK_ERROR;
1249 proto = proto->next;
1252 if (!OCRepPayloadSetPropObjectArrayAsOwner(tempPayload,
1253 OC_RSRVD_INTROSPECTION_URL_INFO,
1257 OIC_LOG(ERROR, TAG, "Unable to add urlInfo object to introspection payload ");
1258 ret = OC_STACK_ERROR;
1264 OIC_LOG(ERROR, TAG, "Unable to allocate memory for urlInfo ");
1265 ret = OC_STACK_NO_MEMORY;
1271 *payload = tempPayload;
1275 OCRepPayloadAppend(*payload, tempPayload);
1278 if (ret != OC_STACK_OK)
1280 OCRepPayloadDestroy(tempPayload);
1283 OICFree(urlInfoPayload);
1286 FreeProtocolLL(protoLL);
1291 OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr,
1292 OCDiscoveryPayload *payload,
1294 CAEndpoint_t *networkInfo,
1297 if (!resourcePtr || !payload)
1299 return OC_STACK_INVALID_PARAM;
1301 uint16_t securePort = 0;
1302 if (resourcePtr->resourceProperties & OC_SECURE)
1304 if (GetSecurePortInfo(devAddr, &securePort) != OC_STACK_OK)
1310 bool isVirtual = false;
1311 if (GetTypeOfVirtualURI(resourcePtr->uri) != OC_UNKNOWN_URI)
1316 uint16_t tcpPort = 0;
1317 GetTCPPortInfo(devAddr, &tcpPort, (resourcePtr->resourceProperties & OC_SECURE));
1319 OCDiscoveryPayloadAddResourceWithEps(payload, resourcePtr, securePort,
1320 isVirtual, networkInfo, infoSize, devAddr, tcpPort);
1322 OCDiscoveryPayloadAddResourceWithEps(payload, resourcePtr, securePort,
1323 isVirtual, networkInfo, infoSize, devAddr);
1329 OCResource *FindResourceByUri(const char* resourceUri)
1336 OCResource * pointer = headResource;
1339 if (strcmp(resourceUri, pointer->uri) == 0)
1343 pointer = pointer->next;
1345 OIC_LOG_V(INFO, TAG, "Resource %s not found", resourceUri);
1349 OCStackResult CheckRequestsEndpoint(const OCDevAddr *reqDevAddr,
1350 OCTpsSchemeFlags resTpsFlags)
1354 OIC_LOG(ERROR, TAG, "OCDevAddr* is NULL!!!");
1355 return OC_STACK_INVALID_PARAM;
1358 OCTpsSchemeFlags reqTpsFlags = OC_NO_TPS;
1359 OCStackResult result = OCGetMatchedTpsFlags((CATransportAdapter_t)reqDevAddr->adapter,
1360 (CATransportFlags_t)reqDevAddr->flags, &reqTpsFlags);
1362 if (result != OC_STACK_OK)
1364 OIC_LOG_V(ERROR, TAG, "Failed at get TPS flags. errcode is %d", result);
1368 // bit compare between request tps flags and resource tps flags
1369 if (reqTpsFlags & resTpsFlags)
1371 OIC_LOG(INFO, TAG, "Request come from registered TPS");
1376 OIC_LOG(ERROR, TAG, "Request come from unregistered TPS!!!");
1377 return OC_STACK_BAD_ENDPOINT;
1381 OCStackResult DetermineResourceHandling (const OCServerRequest *request,
1382 ResourceHandling *handling,
1383 OCResource **resource)
1385 if(!request || !handling || !resource)
1387 return OC_STACK_INVALID_PARAM;
1390 OIC_LOG_V(INFO, TAG, "DetermineResourceHandling for %s", request->resourceUrl);
1392 // Check if virtual resource
1393 if (GetTypeOfVirtualURI(request->resourceUrl) != OC_UNKNOWN_URI)
1395 OIC_LOG_V (INFO, TAG, "%s is virtual", request->resourceUrl);
1396 *handling = OC_RESOURCE_VIRTUAL;
1397 *resource = headResource;
1400 if (strlen((const char*)(request->resourceUrl)) == 0)
1402 // Resource URL not specified
1403 *handling = OC_RESOURCE_NOT_SPECIFIED;
1404 return OC_STACK_NO_RESOURCE;
1408 OCResource *resourcePtr = FindResourceByUri((const char*)request->resourceUrl);
1409 *resource = resourcePtr;
1411 // Checking resource TPS flags if resource exist in stack.
1414 OCStackResult result = CheckRequestsEndpoint(&(request->devAddr), resourcePtr->endpointType);
1416 if (result != OC_STACK_OK)
1418 if (result == OC_STACK_BAD_ENDPOINT)
1420 OIC_LOG(ERROR, TAG, "Request come from bad endpoint. ignore request!!!");
1421 return OC_STACK_BAD_ENDPOINT;
1425 OIC_LOG_V(ERROR, TAG, "Failed at get tps flag errcode: %d", result);
1433 if(defaultDeviceHandler)
1435 *handling = OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER;
1439 // Resource does not exist
1440 // and default device handler does not exist
1441 *handling = OC_RESOURCE_NOT_SPECIFIED;
1442 return OC_STACK_NO_RESOURCE;
1445 if (resourcePtr && resourcePtr->rsrcChildResourcesHead != NULL)
1447 // Collection resource
1448 if (resourcePtr->entityHandler != defaultResourceEHandler)
1450 *handling = OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER;
1455 *handling = OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER;
1461 // Resource not a collection
1462 if (resourcePtr->entityHandler != defaultResourceEHandler)
1464 *handling = OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER;
1469 *handling = OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER;
1476 OCStackResult EntityHandlerCodeToOCStackCode(OCEntityHandlerResult ehResult)
1478 OCStackResult result;
1485 result = OC_STACK_OK;
1488 result = OC_STACK_SLOW_RESOURCE;
1491 result = OC_STACK_ERROR;
1493 case OC_EH_FORBIDDEN:
1494 result = OC_STACK_FORBIDDEN_REQ;
1496 case OC_EH_INTERNAL_SERVER_ERROR:
1497 result = OC_STACK_INTERNAL_SERVER_ERROR;
1499 case OC_EH_RESOURCE_CREATED:
1500 result = OC_STACK_RESOURCE_CREATED;
1502 case OC_EH_RESOURCE_DELETED:
1503 result = OC_STACK_RESOURCE_DELETED;
1506 result = OC_STACK_RESOURCE_CHANGED;
1508 case OC_EH_RESOURCE_NOT_FOUND:
1509 result = OC_STACK_NO_RESOURCE;
1512 result = OC_STACK_ERROR;
1518 static bool resourceMatchesRTFilter(OCResource *resource, char *resourceTypeFilter)
1525 // Null is analogous to no filter.i.e. query is of form /oic/res?if=oic.if.baseline or /oic/res,
1526 // without rt query.
1527 if (NULL == resourceTypeFilter)
1532 // Empty resourceType filter is analogous to error query
1533 // It is an error as query is of form /oic/res?rt=
1534 if (0 == strlen(resourceTypeFilter))
1539 for (OCResourceType *rtPtr = resource->rsrcType; rtPtr; rtPtr = rtPtr->next)
1541 if (0 == strcmp(rtPtr->resourcetypename, resourceTypeFilter))
1547 OIC_LOG_V(INFO, TAG, "%s does not contain rt=%s.", resource->uri, resourceTypeFilter);
1551 static bool resourceMatchesIFFilter(OCResource *resource, char *interfaceFilter)
1558 // Null is analogous to no filter i.e. query is of form /oic/res?rt=core.light or /oic/res,
1559 // without if query.
1560 if (NULL == interfaceFilter)
1565 // Empty interface filter is analogous to error query
1566 // It is an error as query is of form /oic/res?if=
1567 if (0 == strlen(interfaceFilter))
1572 for (OCResourceInterface *ifPtr = resource->rsrcInterface; ifPtr; ifPtr = ifPtr->next)
1574 if (0 == strcmp(ifPtr->name, interfaceFilter) ||
1575 0 == strcmp(OC_RSRVD_INTERFACE_LL, interfaceFilter) ||
1576 0 == strcmp(OC_RSRVD_INTERFACE_DEFAULT, interfaceFilter))
1582 OIC_LOG_V(INFO, TAG, "%s does not contain if=%s.", resource->uri, interfaceFilter);
1587 * If the filters are null, they will be assumed to NOT be present
1588 * and the resource will not be matched against them.
1589 * Function will return true if all non null AND non empty filters passed in find a match.
1591 static bool includeThisResourceInResponse(OCResource *resource,
1592 char *interfaceFilter,
1593 char *resourceTypeFilter)
1597 OIC_LOG(ERROR, TAG, "Invalid resource");
1601 if (resource->resourceProperties & OC_EXPLICIT_DISCOVERABLE)
1604 * At least one valid filter should be available to
1605 * include the resource in discovery response
1607 if (!(resourceTypeFilter && *resourceTypeFilter))
1609 OIC_LOG_V(INFO, TAG, "%s no query string for EXPLICIT_DISCOVERABLE\
1610 resource", resource->uri);
1614 else if (!(resource->resourceProperties & OC_ACTIVE) ||
1615 !(resource->resourceProperties & OC_DISCOVERABLE))
1617 OIC_LOG_V(INFO, TAG, "%s not ACTIVE or DISCOVERABLE", resource->uri);
1621 return resourceMatchesIFFilter(resource, interfaceFilter) &&
1622 resourceMatchesRTFilter(resource, resourceTypeFilter);
1625 OCStackResult SendNonPersistantDiscoveryResponse(OCServerRequest *request, OCResource *resource,
1626 OCPayload *discoveryPayload, OCEntityHandlerResult ehResult)
1628 OCEntityHandlerResponse *response = NULL;
1629 OCStackResult result = OC_STACK_ERROR;
1631 response = (OCEntityHandlerResponse *)OICCalloc(1, sizeof(*response));
1632 VERIFY_PARAM_NON_NULL(TAG, response, "Failed allocating OCEntityHandlerResponse");
1634 response->ehResult = ehResult;
1635 response->payload = discoveryPayload;
1636 response->persistentBufferFlag = 0;
1637 response->requestHandle = (OCRequestHandle) request;
1638 response->resourceHandle = (OCResourceHandle) resource;
1640 result = OCDoResponse(response);
1646 return OC_STACK_NO_MEMORY;
1649 static OCStackResult EHRequest(OCEntityHandlerRequest *ehRequest, OCPayloadType type,
1650 OCServerRequest *request, OCResource *resource)
1652 return FormOCEntityHandlerRequest(ehRequest,
1653 (OCRequestHandle)request,
1656 (OCResourceHandle)resource,
1660 request->payloadSize,
1661 request->numRcvdVendorSpecificHeaderOptions,
1662 request->rcvdVendorSpecificHeaderOptions,
1663 (OCObserveAction)(request->notificationFlag ? OC_OBSERVE_NO_OPTION :
1664 request->observationOption),
1671 * Find resources at the resource directory server. These resources are not local resources but
1674 * @param interfaceQuery The interface query parameter.
1675 * @param resourceTypeQuery The resourceType query parameter.
1676 * @param discPayload The payload that will be added with the resource information if found at RD.
1678 * @return ::OC_STACK_OK if any resources are found else ::OC_STACK_NO_RESOURCE.
1679 * In case if RD server is not started, it returns ::OC_STACK_NO_RESOURCE.
1681 static OCStackResult findResourcesAtRD(const char *interfaceQuery,
1682 const char *resourceTypeQuery, OCDiscoveryPayload **discPayload)
1684 OCStackResult result = OC_STACK_NO_RESOURCE;
1685 if (OCGetResourceHandleAtUri(OC_RSRVD_RD_URI) != NULL)
1687 result = OCRDDatabaseDiscoveryPayloadCreate(interfaceQuery, resourceTypeQuery,
1688 (*discPayload) ? &(*discPayload)->next : discPayload);
1690 if ((*discPayload) && (*discPayload)->resources)
1692 result = OC_STACK_OK;
1699 * Creates a discovery payload and add device id information. This information is included in all
1700 * /oic/res response.
1702 * @param payload payload that will have memory alllocated and device id information added.
1704 * @return ::OC_STACK_OK if successful in allocating memory and adding ID information.
1705 * ::OC_STACK_NO_MEMORY if failed allocating the memory.
1707 static OCStackResult discoveryPayloadCreateAndAddDeviceId(OCPayload **payload)
1711 OIC_LOG(DEBUG, TAG, "Payload is already allocated");
1715 *payload = (OCPayload *) OCDiscoveryPayloadCreate();
1716 VERIFY_PARAM_NON_NULL(TAG, *payload, "Failed adding device id to discovery payload.");
1719 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)*payload;
1720 discPayload->sid = (char *)OICCalloc(1, UUID_STRING_SIZE);
1721 VERIFY_PARAM_NON_NULL(TAG, discPayload->sid, "Failed adding device id to discovery payload.");
1723 const char* uid = OCGetServerInstanceIDString();
1726 memcpy(discPayload->sid, uid, UUID_STRING_SIZE);
1732 OCPayloadDestroy(*payload);
1733 return OC_STACK_NO_MEMORY;
1737 * Add the common properties to the payload, they are only included in case of oic.if.baseline response.
1739 * @param discPayload payload that will have the baseline information included.
1741 * @return ::OC_STACK_OK if successful in adding all the information. ::OC_STACK_NO_MEMORY if failed
1742 * allocating the memory for the baseline information.
1744 static OCStackResult addDiscoveryBaselineCommonProperties(OCDiscoveryPayload *discPayload)
1748 OIC_LOG(ERROR, TAG, "Payload is not allocated");
1749 return OC_STACK_INVALID_PARAM;
1752 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **)&discPayload->name);
1754 discPayload->type = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
1755 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt to discovery payload.");
1756 discPayload->type->value = OICStrdup(OC_RSRVD_RESOURCE_TYPE_RES);
1757 VERIFY_PARAM_NON_NULL(TAG, discPayload->type, "Failed adding rt value to discovery payload.");
1759 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_LL);
1760 OCResourcePayloadAddStringLL(&discPayload->iface, OC_RSRVD_INTERFACE_DEFAULT);
1761 VERIFY_PARAM_NON_NULL(TAG, discPayload->iface, "Failed adding if to discovery payload.");
1766 return OC_STACK_NO_MEMORY;
1769 static bool isUnicast(OCServerRequest *request)
1771 bool isMulticast = request->devAddr.flags & OC_MULTICAST;
1772 return (isMulticast == false &&
1773 (request->devAddr.adapter != OC_ADAPTER_RFCOMM_BTEDR) &&
1774 (request->devAddr.adapter != OC_ADAPTER_GATT_BTLE));
1777 static OCStackResult HandleVirtualResource (OCServerRequest *request, OCResource* resource)
1779 if (!request || !resource)
1781 return OC_STACK_INVALID_PARAM;
1784 OCPayload* payload = NULL;
1785 char *interfaceQuery = NULL;
1786 char *resourceTypeQuery = NULL;
1788 OIC_LOG(INFO, TAG, "Entering HandleVirtualResource");
1790 OCVirtualResources virtualUriInRequest = GetTypeOfVirtualURI (request->resourceUrl);
1793 if (OC_KEEPALIVE_RESOURCE_URI == virtualUriInRequest)
1795 // Received request for a keepalive
1796 OIC_LOG(INFO, TAG, "Request is for KeepAlive Request");
1797 return HandleKeepAliveRequest(request, resource);
1801 OCStackResult discoveryResult = OC_STACK_ERROR;
1802 if (request->method == OC_REST_PUT || request->method == OC_REST_POST ||
1803 request->method == OC_REST_DELETE)
1805 OIC_LOG_V(ERROR, TAG, "Resource : %s not permitted for method: %d",
1806 request->resourceUrl, request->method);
1807 return OC_STACK_UNAUTHORIZED_REQ;
1810 discoveryResult = HandleVirtualObserveRequest(request);
1811 if (discoveryResult == OC_STACK_DUPLICATE_REQUEST)
1813 // server requests are usually free'd when the response is sent out
1814 // for the request in ocserverrequest.c : HandleSingleResponse()
1815 // Since we are making an early return and not responding, the server request
1816 // needs to be deleted.
1817 FindAndDeleteServerRequest (request);
1818 discoveryResult = OC_STACK_OK;
1821 else if (discoveryResult != OC_STACK_OK)
1826 // Step 1: Generate the response to discovery request
1827 if (virtualUriInRequest == OC_WELL_KNOWN_URI
1829 || virtualUriInRequest == OC_MQ_BROKER_URI
1833 if (g_multicastServerStopped && !isUnicast(request))
1835 // Ignore the discovery request
1836 FindAndDeleteServerRequest(request);
1837 discoveryResult = OC_STACK_CONTINUE;
1841 char *interfaceQuery = NULL;
1842 char *resourceTypeQuery = NULL;
1844 CAEndpoint_t *networkInfo = NULL;
1845 size_t infoSize = 0;
1847 CAResult_t caResult = CAGetNetworkInformation(&networkInfo, &infoSize);
1848 if (CA_STATUS_FAILED == caResult)
1850 OIC_LOG(ERROR, TAG, "CAGetNetworkInformation has error on parsing network infomation");
1851 return OC_STACK_ERROR;
1854 discoveryResult = getQueryParamsForFiltering (virtualUriInRequest, request->query,
1855 &interfaceQuery, &resourceTypeQuery);
1856 VERIFY_SUCCESS(discoveryResult);
1858 if (!interfaceQuery && !resourceTypeQuery)
1860 // If no query is sent, default interface is used i.e. oic.if.ll.
1861 interfaceQuery = OICStrdup(OC_RSRVD_INTERFACE_LL);
1864 discoveryResult = discoveryPayloadCreateAndAddDeviceId(&payload);
1865 VERIFY_PARAM_NON_NULL(TAG, payload, "Failed creating Discovery Payload.");
1866 VERIFY_SUCCESS(discoveryResult);
1868 OCDiscoveryPayload *discPayload = (OCDiscoveryPayload *)payload;
1869 if (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_DEFAULT))
1871 discoveryResult = addDiscoveryBaselineCommonProperties(discPayload);
1872 VERIFY_SUCCESS(discoveryResult);
1874 OCResourceProperty prop = OC_DISCOVERABLE;
1876 prop = (OC_MQ_BROKER_URI == virtualUriInRequest) ? OC_MQ_BROKER : prop;
1878 for (; resource && discoveryResult == OC_STACK_OK; resource = resource->next)
1880 // This case will handle when no resource type and it is oic.if.ll.
1881 // Do not assume check if the query is ll
1882 if (!resourceTypeQuery &&
1883 (interfaceQuery && 0 == strcmp(interfaceQuery, OC_RSRVD_INTERFACE_LL)))
1885 // Only include discoverable type
1886 if (resource->resourceProperties & prop)
1888 discoveryResult = BuildVirtualResourceResponse(resource,
1895 else if (includeThisResourceInResponse(resource, interfaceQuery, resourceTypeQuery))
1897 discoveryResult = BuildVirtualResourceResponse(resource,
1905 discoveryResult = OC_STACK_OK;
1908 if (discPayload->resources == NULL)
1910 discoveryResult = OC_STACK_NO_RESOURCE;
1911 OCPayloadDestroy(payload);
1917 OICFree(networkInfo);
1920 discoveryResult = findResourcesAtRD(interfaceQuery, resourceTypeQuery, (OCDiscoveryPayload **)&payload);
1923 else if (virtualUriInRequest == OC_DEVICE_URI)
1925 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_DEVICE_URI);
1926 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Device URI not found.");
1927 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, true);
1929 else if (virtualUriInRequest == OC_PLATFORM_URI)
1931 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
1932 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Platform URI not found.");
1933 discoveryResult = BuildDevicePlatformPayload(resourcePtr, (OCRepPayload **)&payload, false);
1935 #ifdef ROUTING_GATEWAY
1936 else if (OC_GATEWAY_URI == virtualUriInRequest)
1938 // Received request for a gateway
1939 OIC_LOG(INFO, TAG, "Request is for Gateway Virtual Request");
1940 discoveryResult = RMHandleGatewayRequest(request, resource);
1943 else if (OC_INTROSPECTION_URI == virtualUriInRequest)
1945 // Received request for introspection
1946 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_INTROSPECTION_URI);
1947 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Introspection URI not found.");
1948 discoveryResult = BuildIntrospectionResponseRepresentation(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
1949 OIC_LOG(INFO, TAG, "Request is for Introspection");
1951 else if (OC_INTROSPECTION_PAYLOAD_URI == virtualUriInRequest)
1953 // Received request for introspection payload
1954 OCResource *resourcePtr = FindResourceByUri(OC_RSRVD_INTROSPECTION_PAYLOAD_URI);
1955 VERIFY_PARAM_NON_NULL(TAG, resourcePtr, "Introspection Payload URI not found.");
1956 discoveryResult = BuildIntrospectionPayloadResponse(resourcePtr, (OCRepPayload **)&payload, &request->devAddr);
1957 OIC_LOG(INFO, TAG, "Request is for Introspection Payload");
1960 * Step 2: Send the discovery response
1962 * Iotivity should respond to discovery requests in below manner:
1963 * 1)If query filter matching fails and discovery request is multicast,
1964 * it should NOT send any response.
1965 * 2)If query filter matching fails and discovery request is unicast,
1966 * it should send an error(RESOURCE_NOT_FOUND - 404) response.
1967 * 3)If Server does not have any 'DISCOVERABLE' resources and discovery
1968 * request is multicast, it should NOT send any response.
1969 * 4)If Server does not have any 'DISCOVERABLE' resources and discovery
1970 * request is unicast, it should send an error(RESOURCE_NOT_FOUND - 404) response.
1973 #ifdef WITH_PRESENCE
1974 if ((virtualUriInRequest == OC_PRESENCE) &&
1975 (resource->resourceProperties & OC_ACTIVE))
1977 // Need to send ACK when the request is CON.
1978 if (request->qos == OC_HIGH_QOS)
1980 CAEndpoint_t endpoint = { .adapter = CA_DEFAULT_ADAPTER };
1981 CopyDevAddrToEndpoint(&request->devAddr, &endpoint);
1982 SendDirectStackResponse(&endpoint, request->coapID, CA_EMPTY, CA_MSG_ACKNOWLEDGE,
1983 0, NULL, NULL, 0, NULL, CA_RESPONSE_FOR_RES);
1985 FindAndDeleteServerRequest(request);
1987 // Presence uses observer notification api to respond via SendPresenceNotification.
1988 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
1993 // Gateway uses the RMHandleGatewayRequest to respond to the request.
1994 if (OC_GATEWAY_URI != virtualUriInRequest)
1997 OIC_LOG_PAYLOAD(DEBUG, payload);
1998 if(discoveryResult == OC_STACK_OK)
2000 SendNonPersistantDiscoveryResponse(request, resource, payload, OC_EH_OK);
2002 else // Error handling
2004 if (isUnicast(request))
2006 OIC_LOG_V(ERROR, TAG, "Sending a (%d) error to (%d) discovery request",
2007 discoveryResult, virtualUriInRequest);
2008 SendNonPersistantDiscoveryResponse(request, resource, NULL,
2009 (discoveryResult == OC_STACK_NO_RESOURCE) ?
2010 OC_EH_RESOURCE_NOT_FOUND : OC_EH_ERROR);
2014 // Ignoring the discovery request as per RFC 7252, Section #8.2
2015 OIC_LOG(INFO, TAG, "Silently ignoring the request since no useful data to send.");
2016 // the request should be removed.
2017 // since it never remove and causes a big memory waste.
2018 FindAndDeleteServerRequest(request);
2020 discoveryResult = OC_STACK_CONTINUE;
2027 OICFree(interfaceQuery);
2030 if (resourceTypeQuery)
2032 OICFree(resourceTypeQuery);
2034 OCPayloadDestroy(payload);
2036 // To ignore the message, OC_STACK_CONTINUE is sent
2037 return discoveryResult;
2040 static OCStackResult
2041 HandleDefaultDeviceEntityHandler(OCServerRequest *request)
2045 return OC_STACK_INVALID_PARAM;
2048 OCEntityHandlerResult ehResult = OC_EH_ERROR;
2049 OCEntityHandlerRequest ehRequest = {0};
2050 OIC_LOG(INFO, TAG, "Entering HandleResourceWithDefaultDeviceEntityHandler");
2051 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, NULL);
2052 VERIFY_SUCCESS(result);
2054 // At this point we know for sure that defaultDeviceHandler exists
2055 ehResult = defaultDeviceHandler(OC_REQUEST_FLAG, &ehRequest,
2056 (char*) request->resourceUrl, defaultDeviceHandlerCallbackParameter);
2057 if(ehResult == OC_EH_SLOW)
2059 OIC_LOG(INFO, TAG, "This is a slow resource");
2060 request->slowFlag = 1;
2062 else if(ehResult == OC_EH_ERROR)
2064 FindAndDeleteServerRequest(request);
2066 result = EntityHandlerCodeToOCStackCode(ehResult);
2068 OCPayloadDestroy(ehRequest.payload);
2072 static OCStackResult
2073 HandleResourceWithEntityHandler(OCServerRequest *request,
2074 OCResource *resource)
2076 if(!request || ! resource)
2078 return OC_STACK_INVALID_PARAM;
2081 OCStackResult result = OC_STACK_ERROR;
2082 OCEntityHandlerResult ehResult = OC_EH_ERROR;
2083 OCEntityHandlerFlag ehFlag = OC_REQUEST_FLAG;
2084 ResourceObserver *resObs = NULL;
2086 OCEntityHandlerRequest ehRequest = {0};
2088 OIC_LOG(INFO, TAG, "Entering HandleResourceWithEntityHandler");
2089 OCPayloadType type = PAYLOAD_TYPE_REPRESENTATION;
2090 // check the security resource
2091 if (request && request->resourceUrl && SRMIsSecurityResourceURI(request->resourceUrl))
2093 type = PAYLOAD_TYPE_SECURITY;
2096 result = EHRequest(&ehRequest, type, request, resource);
2097 VERIFY_SUCCESS(result);
2099 if(ehRequest.obsInfo.action == OC_OBSERVE_NO_OPTION)
2101 OIC_LOG(INFO, TAG, "No observation requested");
2102 ehFlag = OC_REQUEST_FLAG;
2104 else if(ehRequest.obsInfo.action == OC_OBSERVE_REGISTER)
2106 OIC_LOG(INFO, TAG, "Observation registration requested");
2108 ResourceObserver *obs = GetObserverUsingToken (request->requestToken,
2109 request->tokenLength);
2113 OIC_LOG (INFO, TAG, "Observer with this token already present");
2114 OIC_LOG (INFO, TAG, "Possibly re-transmitted CON OBS request");
2115 OIC_LOG (INFO, TAG, "Not adding observer. Not responding to client");
2116 OIC_LOG (INFO, TAG, "The first request for this token is already ACKED.");
2118 // server requests are usually free'd when the response is sent out
2119 // for the request in ocserverrequest.c : HandleSingleResponse()
2120 // Since we are making an early return and not responding, the server request
2121 // needs to be deleted.
2122 FindAndDeleteServerRequest (request);
2126 result = GenerateObserverId(&ehRequest.obsInfo.obsId);
2127 VERIFY_SUCCESS(result);
2129 result = AddObserver ((const char*)(request->resourceUrl),
2130 (const char *)(request->query),
2131 ehRequest.obsInfo.obsId, request->requestToken, request->tokenLength,
2132 resource, request->qos, request->acceptFormat,
2133 request->acceptVersion, &request->devAddr);
2135 if(result == OC_STACK_OK)
2137 OIC_LOG(INFO, TAG, "Added observer successfully");
2138 request->observeResult = OC_STACK_OK;
2139 ehFlag = (OCEntityHandlerFlag)(OC_REQUEST_FLAG | OC_OBSERVE_FLAG);
2141 else if (result == OC_STACK_RESOURCE_ERROR)
2143 OIC_LOG(INFO, TAG, "The Resource is not active, discoverable or observable");
2144 request->observeResult = OC_STACK_ERROR;
2145 ehFlag = OC_REQUEST_FLAG;
2149 // The error in observeResult for the request will be used when responding to this
2150 // request by omitting the observation option/sequence number.
2151 request->observeResult = OC_STACK_ERROR;
2152 OIC_LOG(ERROR, TAG, "Observer Addition failed");
2153 ehFlag = OC_REQUEST_FLAG;
2154 FindAndDeleteServerRequest(request);
2159 else if(ehRequest.obsInfo.action == OC_OBSERVE_DEREGISTER)
2161 OIC_LOG(INFO, TAG, "Deregistering observation requested");
2163 resObs = GetObserverUsingToken (request->requestToken, request->tokenLength);
2167 // Stack does not contain this observation request
2168 // Either token is incorrect or observation list is corrupted
2169 result = OC_STACK_ERROR;
2172 ehRequest.obsInfo.obsId = resObs->observeId;
2173 ehFlag = (OCEntityHandlerFlag)(ehFlag | OC_OBSERVE_FLAG);
2175 result = DeleteObserverUsingToken (request->requestToken, request->tokenLength);
2177 if(result == OC_STACK_OK)
2179 OIC_LOG(INFO, TAG, "Removed observer successfully");
2180 request->observeResult = OC_STACK_OK;
2181 // There should be no observe option header for de-registration response.
2182 // Set as an invalid value here so we can detect it later and remove the field in response.
2183 request->observationOption = MAX_SEQUENCE_NUMBER + 1;
2187 request->observeResult = OC_STACK_ERROR;
2188 OIC_LOG(ERROR, TAG, "Observer Removal failed");
2189 FindAndDeleteServerRequest(request);
2195 result = OC_STACK_ERROR;
2199 ehResult = resource->entityHandler(ehFlag, &ehRequest, resource->entityHandlerCallbackParam);
2200 if(ehResult == OC_EH_SLOW)
2202 OIC_LOG(INFO, TAG, "This is a slow resource");
2203 request->slowFlag = 1;
2205 else if(ehResult == OC_EH_ERROR)
2207 FindAndDeleteServerRequest(request);
2209 result = EntityHandlerCodeToOCStackCode(ehResult);
2211 OCPayloadDestroy(ehRequest.payload);
2215 static OCStackResult HandleCollectionResourceDefaultEntityHandler(OCServerRequest *request,
2216 OCResource *resource)
2218 if (!request || !resource)
2220 return OC_STACK_INVALID_PARAM;
2223 OCEntityHandlerRequest ehRequest = {0};
2224 OCStackResult result = EHRequest(&ehRequest, PAYLOAD_TYPE_REPRESENTATION, request, resource);
2225 if(result == OC_STACK_OK)
2227 result = DefaultCollectionEntityHandler (OC_REQUEST_FLAG, &ehRequest);
2230 OCPayloadDestroy(ehRequest.payload);
2235 ProcessRequest(ResourceHandling resHandling, OCResource *resource, OCServerRequest *request)
2237 OCStackResult ret = OC_STACK_OK;
2239 switch (resHandling)
2241 case OC_RESOURCE_VIRTUAL:
2243 ret = HandleVirtualResource (request, resource);
2246 case OC_RESOURCE_DEFAULT_DEVICE_ENTITYHANDLER:
2248 ret = HandleDefaultDeviceEntityHandler(request);
2251 case OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER:
2253 OIC_LOG(INFO, TAG, "OC_RESOURCE_NOT_COLLECTION_DEFAULT_ENTITYHANDLER");
2254 return OC_STACK_ERROR;
2256 case OC_RESOURCE_NOT_COLLECTION_WITH_ENTITYHANDLER:
2258 ret = HandleResourceWithEntityHandler (request, resource);
2261 case OC_RESOURCE_COLLECTION_WITH_ENTITYHANDLER:
2263 ret = HandleResourceWithEntityHandler (request, resource);
2266 case OC_RESOURCE_COLLECTION_DEFAULT_ENTITYHANDLER:
2268 ret = HandleCollectionResourceDefaultEntityHandler (request, resource);
2271 case OC_RESOURCE_NOT_SPECIFIED:
2273 ret = OC_STACK_NO_RESOURCE;
2278 OIC_LOG(INFO, TAG, "Invalid Resource Determination");
2279 return OC_STACK_ERROR;
2285 OCStackResult OCSetPlatformInfo(OCPlatformInfo info)
2287 OCResource *resource = NULL;
2288 if (!info.platformID || !info.manufacturerName)
2290 OIC_LOG(ERROR, TAG, "No value specified.");
2293 if (0 == strlen(info.platformID) || 0 == strlen(info.manufacturerName))
2295 OIC_LOG(ERROR, TAG, "The passed value cannot be empty");
2298 if ((info.manufacturerName && strlen(info.manufacturerName) > MAX_PLATFORM_NAME_LENGTH) ||
2299 (info.manufacturerUrl && strlen(info.manufacturerUrl) > MAX_PLATFORM_URL_LENGTH) ||
2300 (info.modelNumber && strlen(info.modelNumber) > MAX_PLATFORM_NAME_LENGTH) ||
2301 (info.platformVersion && strlen(info.platformVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2302 (info.operatingSystemVersion && strlen(info.operatingSystemVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2303 (info.hardwareVersion && strlen(info.hardwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2304 (info.firmwareVersion && strlen(info.firmwareVersion) > MAX_PLATFORM_NAME_LENGTH) ||
2305 (info.supportUrl && strlen(info.supportUrl) > MAX_PLATFORM_URL_LENGTH))
2307 OIC_LOG(ERROR, TAG, "The passed value is bigger than permitted.");
2312 * @todo (IOT-1541) There are several versions of a UUID structure and conversion
2313 * methods scattered around the IoTivity code base. They need to be combined
2316 uint8_t uuid[UUID_SIZE];
2317 if (!OCConvertStringToUuid(info.platformID, uuid))
2319 OIC_LOG(ERROR, TAG, "Platform ID is not a UUID.");
2323 resource = FindResourceByUri(OC_RSRVD_PLATFORM_URI);
2326 OIC_LOG(ERROR, TAG, "Platform Resource does not exist.");
2329 OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
2330 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_ID, info.platformID));
2331 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_NAME, info.manufacturerName));
2332 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_URL, info.manufacturerUrl);
2333 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, info.modelNumber);
2334 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MFG_DATE, info.dateOfManufacture);
2335 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_PLATFORM_VERSION, info.platformVersion);
2336 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_OS_VERSION, info.operatingSystemVersion);
2337 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_HARDWARE_VERSION, info.hardwareVersion);
2338 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_FIRMWARE_VERSION, info.firmwareVersion);
2339 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SUPPORT_URL, info.supportUrl);
2340 OCSetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_SYSTEM_TIME, info.systemTime);
2341 OIC_LOG(INFO, TAG, "Platform parameter initialized successfully.");
2345 return OC_STACK_INVALID_PARAM;
2348 OCStackResult OCSetDeviceInfo(OCDeviceInfo info)
2350 OCResource *resource = FindResourceByUri(OC_RSRVD_DEVICE_URI);
2353 OIC_LOG(ERROR, TAG, "Device Resource does not exist.");
2356 if (!info.deviceName || info.deviceName[0] == '\0')
2358 OIC_LOG(ERROR, TAG, "Null or empty device name.");
2359 return OC_STACK_INVALID_PARAM;
2362 if (OCGetServerInstanceIDString() == NULL)
2364 OIC_LOG(INFO, TAG, "Device ID generation failed");
2368 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, info.deviceName));
2369 for (OCStringLL *temp = info.types; temp; temp = temp->next)
2373 VERIFY_SUCCESS(OCBindResourceTypeToResource(resource, temp->value));
2376 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, info.specVersion ?
2377 info.specVersion: OC_SPEC_VERSION));
2379 if (info.dataModelVersions)
2381 char *dmv = OCCreateString(info.dataModelVersions);
2382 VERIFY_PARAM_NON_NULL(TAG, dmv, "Failed allocating dataModelVersions");
2383 OCStackResult r = OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, dmv);
2389 VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION,
2390 OC_DATA_MODEL_VERSION));
2392 OIC_LOG(INFO, TAG, "Device parameter initialized successfully.");
2396 return OC_STACK_ERROR;
2399 OCStackResult OCGetAttribute(const OCResource *resource, const char *attribute, void **value)
2401 if (!resource || !attribute)
2403 return OC_STACK_INVALID_PARAM;
2405 if (0 == strlen(attribute))
2407 return OC_STACK_INVALID_PARAM;
2409 for (OCAttribute *temp = resource->rsrcAttributes; temp; temp = temp->next)
2411 if (0 == strcmp(attribute, temp->attrName))
2413 // A special case as this type return OCStringLL
2414 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
2416 *value = CloneOCStringLL((OCStringLL *)temp->attrValue);
2421 *value = OICStrdup((char *)temp->attrValue);
2426 return OC_STACK_NO_RESOURCE;
2429 OCStackResult OCGetPropertyValue(OCPayloadType type, const char *prop, void **value)
2433 return OC_STACK_INVALID_PARAM;
2435 if (strlen(prop) == 0)
2437 return OC_STACK_INVALID_PARAM;
2443 OCStackResult res = OC_STACK_NO_RESOURCE;
2444 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
2446 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
2447 OCResource *resource = FindResourceByUri(pathType);
2450 return OC_STACK_NO_RESOURCE;
2453 res = OCGetAttribute(resource, prop, value);
2458 static OCStackResult SetAttributeInternal(OCResource *resource,
2459 const char *attribute,
2461 bool updateDatabase)
2463 OCAttribute *resAttrib = NULL;
2465 // See if the attribute already exists in the list.
2466 for (resAttrib = resource->rsrcAttributes; resAttrib; resAttrib = resAttrib->next)
2468 if (0 == strcmp(attribute, resAttrib->attrName))
2470 // Found, free the old value.
2471 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, resAttrib->attrName))
2473 OCFreeOCStringLL((OCStringLL *)resAttrib->attrValue);
2477 OICFree((char *)resAttrib->attrValue);
2483 // If not already in the list, add it.
2484 if (NULL == resAttrib)
2486 resAttrib = (OCAttribute *)OICCalloc(1, sizeof(OCAttribute));
2487 VERIFY_PARAM_NON_NULL(TAG, resAttrib, "Failed allocating OCAttribute");
2488 resAttrib->attrName = OICStrdup(attribute);
2489 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrName, "Failed allocating attribute name");
2490 resAttrib->next = resource->rsrcAttributes;
2491 resource->rsrcAttributes = resAttrib;
2494 // Fill in the new value.
2495 if (0 == strcmp(OC_RSRVD_DATA_MODEL_VERSION, attribute))
2497 resAttrib->attrValue = OCCreateOCStringLL((char *)value);
2501 resAttrib->attrValue = OICStrdup((char *)value);
2503 VERIFY_PARAM_NON_NULL(TAG, resAttrib->attrValue, "Failed allocating attribute value");
2505 // The resource has changed from what is stored in the database. Update the database to
2506 // reflect the new value.
2509 OCDeviceProperties *deviceProperties = NULL;
2511 OCStackResult result = CreateDeviceProperties((const char*)value, &deviceProperties);
2512 if (OC_STACK_OK == result)
2514 result = UpdateDevicePropertiesDatabase(deviceProperties);
2515 if (OC_STACK_OK != result)
2517 OIC_LOG(ERROR, TAG, "UpdateDevicePropertiesDatabase failed!");
2520 CleanUpDeviceProperties(&deviceProperties);
2524 OIC_LOG(ERROR, TAG, "CreateDeviceProperties failed!");
2531 OCDeleteResourceAttributes(resAttrib);
2532 return OC_STACK_NO_MEMORY;
2535 static OCStackResult IsDatabaseUpdateNeeded(const char *attribute, const void *value, bool *update)
2537 OCStackResult result = OC_STACK_OK;
2538 void *currentPIID = NULL;
2540 if (!attribute || !value || !update)
2542 return OC_STACK_INVALID_PARAM;
2547 // Protocol Independent ID
2548 if (0 == strcmp(OC_RSRVD_PROTOCOL_INDEPENDENT_ID, attribute))
2550 result = OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_PROTOCOL_INDEPENDENT_ID, ¤tPIID);
2551 if (OC_STACK_OK == result)
2553 // PIID has already been set on the resource and stored in the database. Check to see
2554 // if the value is changing so the database can be updated accordingly.
2555 if (0 != strcmp((char *)currentPIID, (char*)value))
2560 else if (OC_STACK_NO_RESOURCE == result)
2562 // PIID has not been set yet so we should always update the database.
2564 result = OC_STACK_OK;
2568 OIC_LOG_V(ERROR, TAG,
2569 "Call to OCGetPropertyValue for the current PIID failed with error: %d", result);
2574 OICFreeAndSetToNull(¤tPIID);
2579 OCStackResult OCSetAttribute(OCResource *resource, const char *attribute, const void *value)
2581 bool updateDatabase = false;
2583 // Check to see if we also need to update the database for this attribute. If the current
2584 // value matches what is stored in the database we can skip the update and an unnecessary
2586 if (OC_STACK_OK != IsDatabaseUpdateNeeded(attribute, value, &updateDatabase))
2588 OIC_LOG_V(WARNING, TAG,
2589 "Could not determine if a database update was needed for %s. Proceeding without updating the database.",
2591 updateDatabase = false;
2594 return SetAttributeInternal(resource, attribute, value, updateDatabase);
2597 OCStackResult OCSetPropertyValue(OCPayloadType type, const char *prop, const void *value)
2599 if (!prop || !value)
2601 return OC_STACK_INVALID_PARAM;
2603 if (strlen(prop) == 0)
2605 return OC_STACK_INVALID_PARAM;
2608 OCStackResult res = OC_STACK_ERROR;
2609 if (PAYLOAD_TYPE_DEVICE == type || PAYLOAD_TYPE_PLATFORM == type)
2611 const char *pathType = (type == PAYLOAD_TYPE_DEVICE) ? OC_RSRVD_DEVICE_URI : OC_RSRVD_PLATFORM_URI;
2612 OCResource *resource = FindResourceByUri(pathType);
2615 OIC_LOG(ERROR, TAG, "Resource does not exist.");
2619 res = OCSetAttribute(resource, prop, value);