Merge Resource Directory into the master 49/2749/5
authorHabib Virji <habib.virji@samsung.com>
Wed, 30 Sep 2015 09:58:37 +0000 (10:58 +0100)
committerHabib Virji <habib.virji@samsung.com>
Wed, 30 Sep 2015 14:46:40 +0000 (15:46 +0100)
Resource Directory Server:
• Hosting of RD (Server side)
• Respond to discovery request with the bias factor.
• Response after receiving publish resource and payload handling (Server side)
• Storage of resources at RD (Server side)
• /oic/res querying support with  a response in collection format.
Resource Directory Clients:
• API to initiate discovery of RD
• Publish resources to RD using the collection format.
• Stop Multicast API suport
Sample Applications:
• Server side application to start resource directory
• Thin-client side application to discover and publish resources to resource directory.
• Client to query resource published at the resource directory.

Change-Id: Iecd03cb26b231dc58ae1f5c30353c5a2514260bf
Signed-off-by: Habib Virji <habib.virji@samsung.com>
21 files changed:
1  2 
auto_build.sh
build_common/SConscript
resource/csdk/SConscript
resource/csdk/connectivity/api/cainterface.h
resource/csdk/connectivity/inc/cainterfacecontroller.h
resource/csdk/connectivity/inc/caraadapter.h
resource/csdk/connectivity/src/caconnectivitymanager.c
resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_eth.cpp
resource/csdk/connectivity/src/ip_adapter/arduino/caipserver_wifi.cpp
resource/csdk/connectivity/src/ip_adapter/caipadapter.c
resource/csdk/connectivity/src/ip_adapter/caipserver.c
resource/csdk/connectivity/src/ra_adapter/caraadapter.c
resource/csdk/stack/include/ocpayload.h
resource/csdk/stack/include/octypes.h
resource/csdk/stack/samples/tizen/build/packaging/com.oic.ri.spec
resource/csdk/stack/src/ocpayload.c
resource/csdk/stack/src/ocpayloadconvert.c
resource/csdk/stack/src/ocpayloadparse.c
resource/csdk/stack/src/ocstack.c
service/SConscript
service/resource-directory/src/internal/rd_storage.c

diff --cc auto_build.sh
@@@ -12,7 -12,8 +12,9 @@@ function build_all(
                build_linux_secured $1 $2
                build_linux_unsecured_with_ra $1 $2
                build_linux_secured_with_ra $1 $2
 +              build_linux_unsecured_with_rm $1 $2
+               build_linux_unsecured_with_rd $1 $2
+               build_linux_secured_with_rd $1 $2
        fi
  
        build_android $1 $2
@@@ -62,9 -57,21 +64,21 @@@ function build_linux_unsecured_with_ra(
  function build_linux_secured_with_ra()
  {
        echo "*********** Build for linux With Remote Access & Security ************"
 -      scons RELEASE=$1 WITH_RA=1 SECURED=1 $2
 +      scons RELEASE=$1 WITH_RA=1 WITH_RA_IBB=1 SECURED=1 $2
  }
  
+ function build_linux_unsecured_with_rd()
+ {
+       echo "*********** Build for linux With Resource Directory *************"
+       scons RELEASE=$1 WITH_RD=1 $2
+ }
+ function build_linux_secured_with_rd()
+ {
+       echo "*********** Build for linux With Resource Directory & Security ************"
+       scons RELEASE=$1 WITH_RD=1 SECURED=1 $2
+ }
  function build_android()
  {
        # Note: for android, as oic-resource uses C++11 feature stoi and to_string,
@@@ -79,12 -79,8 +79,13 @@@ help_vars.Add(EnumVariable('TARGET_OS'
  
  
  help_vars.Add(BoolVariable('WITH_RA', 'Build with Remote Access module', False))
+ help_vars.Add(EnumVariable('WITH_RD', 'Build including Resource Directory', '0', allowed_values=('0', '1')))
  
 +help_vars.Add(BoolVariable('SIMULATOR', 'Build with simulator module', False))
 +
 +help_vars.Add(BoolVariable('WITH_RA_IBB', 'Build with Remote Access module(workssys)', False))
 +
 +
  if target_os in targets_disallow_multitransport:
        help_vars.Add(ListVariable('TARGET_TRANSPORT', 'Target transport', 'IP', ['BT', 'BLE', 'IP']))
  else:
Simple merge
Simple merge
@@@ -65,6 -65,6 +65,8 @@@ cp resource/csdk/stack/include/ocpresen
  cp resource/csdk/stack/include/ocpayload.h %{DEST_INC_DIR}
  cp resource/c_common/platform_features.h %{DEST_INC_DIR}
  cp resource/csdk/stack/include/payload_logging.h %{DEST_INC_DIR}
++cp resource/csdk/stack/include/rdpayload.h %{DEST_INC_DIR}
++cp extlibs/tinycbor/tinycbor/src/cbor.h %{DEST_INC_DIR}
  cp extlibs/cjson/cJSON.h %{DEST_INC_DIR}
  cp -rf %{ROOTDIR}/com.oic.ri.pc %{DEST_LIB_DIR}/pkgconfig/
  
Simple merge
@@@ -230,110 -206,158 +236,157 @@@ static int64_t OCConvertDiscoveryPayloa
  
      cbor_encoder_init(&encoder, outPayload, *size, 0);
  
-     CborEncoder rootArray;
-     err = err | cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
-     for(size_t i = 0; i < resourceCount; ++i)
+     if (payload->collectionResources)
      {
-         CborEncoder map;
-         OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
+         CborError cborEncoderResult;
+         cborEncoderResult = cbor_encoder_create_array(&encoder, &rootArray, CBOR_ROOT_ARRAY_LENGTH);
+         if (CborNoError != cborEncoderResult)
+         {
+             OC_LOG(ERROR, TAG, "Failed creating root array.");
+             goto cbor_error;
+         }
  
-         if(!resource)
+         CborEncoder colArray;
+         cborEncoderResult = cbor_encoder_create_array(&rootArray, &colArray, CborIndefiniteLength);
+         if (CborNoError != cborEncoderResult)
+         {
+             OC_LOG(ERROR, TAG, "Failed creating collection array.");
+             goto cbor_error;
+         }
 -
+         OCResourceCollectionPayload *colResources = payload->collectionResources;
+         while (colResources)
+         {
+             if (OC_STACK_OK != OCTagsPayloadToCbor(colResources->tags, &colArray))
+             {
+                 goto cbor_error;
+             }
+             if (OC_STACK_OK != OCLinksPayloadToCbor(colResources->setLinks, &colArray))
+             {
+                 goto cbor_error;
+             }
+             colResources = colResources->next;
+         }
+         cborEncoderResult = cbor_encoder_close_container(&rootArray, &colArray);
+         if (CborNoError != cborEncoderResult)
+         {
+             OC_LOG(ERROR, TAG, "Failed closing collection array.");
+             goto cbor_error;
+         }
+         cborEncoderResult = cbor_encoder_close_container(&encoder, &rootArray);
+         if (CborNoError != cborEncoderResult)
          {
-             return OC_STACK_INVALID_PARAM;
+             OC_LOG(ERROR, TAG, "Failed closing root array.");
+             goto cbor_error;
          }
+     }
+     else if (payload->resources)
+     {
+         size_t resourceCount =  OCDiscoveryPayloadGetResourceCount(payload);
+         err = err || cbor_encoder_create_array(&encoder, &rootArray, resourceCount);
  
-         err = err | cbor_encoder_create_map(&rootArray, &map, 3);
-         // Uri
-         err = err | AddTextStringToMap(&map, OC_RSRVD_HREF,
-                 sizeof(OC_RSRVD_HREF) - 1,
-                 resource->uri);
-         // Server ID
-         err = err | cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID,
-                 sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1);
-         err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
-         // Prop Tag
+         for(size_t i = 0; i < resourceCount; ++i)
          {
-             CborEncoder propMap;
-             err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY,
-                     sizeof(OC_RSRVD_PROPERTY) -1 );
-             err = err | cbor_encoder_create_map(&map, &propMap, 3);
+             CborEncoder map;
+             OCResourcePayload* resource = OCDiscoveryPayloadGetResource(payload, i);
  
-             // Resource Type
-             if (resource->types)
+             if(!resource)
              {
-                 char* joinedTypes = OCStringLLJoin(resource->types);
-                 if (joinedTypes)
-                 {
-                     err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
-                             sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
-                     err = err | cbor_encode_text_string(&propMap, joinedTypes,
-                             strlen(joinedTypes));
-                     OICFree(joinedTypes);
-                 }
-                 else
-                 {
-                     return OC_STACK_NO_MEMORY;
-                 }
+                 OICFree(outPayload);
+                 return OC_STACK_INVALID_PARAM;
              }
-             // Interface Types
-             if (resource->interfaces)
+             err = err | cbor_encoder_create_map(&rootArray, &map, 3);
+             // Uri
+             err = err | AddTextStringToMap(&map, OC_RSRVD_HREF,
+                     sizeof(OC_RSRVD_HREF) - 1,
+                     resource->uri);
+             // Server ID
+             err = err | cbor_encode_text_string(&map, OC_RSRVD_SERVER_INSTANCE_ID,
+                     sizeof(OC_RSRVD_SERVER_INSTANCE_ID) - 1);
+             err = err | cbor_encode_byte_string(&map, resource->sid, UUID_SIZE);
+             // Prop Tag
              {
-                 char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
-                 if (joinedInterfaces)
+                 CborEncoder propMap;
+                 err = err | cbor_encode_text_string(&map, OC_RSRVD_PROPERTY,
+                         sizeof(OC_RSRVD_PROPERTY) -1 );
+                 err = err | cbor_encoder_create_map(&map, &propMap, 3);
+                 // Resource Type
++                if (resource->types)
                  {
-                     err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
-                             sizeof(OC_RSRVD_INTERFACE) - 1);
-                     err = err | cbor_encode_text_string(&propMap, joinedInterfaces,
-                             strlen(joinedInterfaces));
-                     OICFree(joinedInterfaces);
 -                    CborEncoder rtArray;
 -                    err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
 -                        sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
 -                    err = err | cbor_encoder_create_array(&propMap, &rtArray, CborIndefiniteLength);
 -
 -                    OCStringLL* rtPtr = resource->types;
 -                    while(rtPtr)
++                    char* joinedTypes = OCStringLLJoin(resource->types);
++                    if (joinedTypes)
+                     {
 -                        err = err | cbor_encode_text_string(&rtArray, rtPtr->value,
 -                                strlen(rtPtr->value));
 -                        rtPtr = rtPtr->next;
++                        err = err | cbor_encode_text_string(&propMap, OC_RSRVD_RESOURCE_TYPE,
++                                sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
++                        err = err | cbor_encode_text_string(&propMap, joinedTypes,
++                                strlen(joinedTypes));
++                        OICFree(joinedTypes);
++                    }
++                    else
++                    {
++                        return OC_STACK_NO_MEMORY;
+                     }
 -
 -                    err = err | cbor_encoder_close_container(&propMap, &rtArray);
                  }
-                 else
+                 // Interface Types
++                if (resource->interfaces)
                  {
-                     return OC_STACK_NO_MEMORY;
 -                    CborEncoder ifArray;
 -                    err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
 -                            sizeof(OC_RSRVD_INTERFACE) - 1);
 -                    err = err | cbor_encoder_create_array(&propMap, &ifArray, CborIndefiniteLength);
 -                    OCStringLL* ifPtr = resource->interfaces;
 -                    while (ifPtr)
++                    char* joinedInterfaces = OCStringLLJoin(resource->interfaces);
++                    if (joinedInterfaces)
+                     {
 -                        err = err | cbor_encode_text_string(&ifArray, ifPtr->value,
 -                            strlen(ifPtr->value));
 -                        ifPtr= ifPtr->next;
++                        err = err | cbor_encode_text_string(&propMap, OC_RSRVD_INTERFACE,
++                                sizeof(OC_RSRVD_INTERFACE) - 1);
++                        err = err | cbor_encode_text_string(&propMap, joinedInterfaces,
++                                strlen(joinedInterfaces));
++                        OICFree(joinedInterfaces);
++                    }
++                    else
++                    {
++                        return OC_STACK_NO_MEMORY;
+                     }
 -
 -                    err = err || cbor_encoder_close_container(&propMap, &ifArray);
                  }
-             }
-             // Policy
-             {
-                 CborEncoder policyMap;
-                 err = err | cbor_encode_text_string(&propMap, OC_RSRVD_POLICY,
-                         sizeof(OC_RSRVD_POLICY) - 1);
-                 err = err | cbor_encoder_create_map(&propMap, &policyMap, CborIndefiniteLength);
-                 // Bitmap
-                 err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
-                         sizeof(OC_RSRVD_BITMAP) - 1);
-                 err = err | cbor_encode_uint(&policyMap, resource->bitmap);
--
-                 if(resource->secure)
+                 // Policy
                  {
-                     err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
-                             sizeof(OC_RSRVD_SECURE) - 1);
-                     err = err | cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
+                     CborEncoder policyMap;
+                     err = err || cbor_encode_text_string(&propMap, OC_RSRVD_POLICY,
+                             sizeof(OC_RSRVD_POLICY) - 1);
+                     err = err || cbor_encoder_create_map(&propMap, &policyMap, CborIndefiniteLength);
+                     // Bitmap
+                     err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_BITMAP,
+                             sizeof(OC_RSRVD_BITMAP) - 1);
+                     err = err || cbor_encode_uint(&policyMap, resource->bitmap);
  
-                     if(resource->port != 0)
+                     if(resource->secure)
                      {
-                         err = err | cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
-                                 sizeof(OC_RSRVD_HOSTING_PORT) - 1);
-                         err = err | cbor_encode_uint(&policyMap, resource->port);
+                         err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_SECURE,
+                                 sizeof(OC_RSRVD_SECURE) - 1);
+                         err = err || cbor_encode_boolean(&policyMap, OC_RESOURCE_SECURE);
+                         if(resource->port != 0)
+                         {
+                             err = err || cbor_encode_text_string(&policyMap, OC_RSRVD_HOSTING_PORT,
+                                     sizeof(OC_RSRVD_HOSTING_PORT) - 1);
+                             err = err || cbor_encode_uint(&policyMap, resource->port);
+                         }
                      }
+                     err = err || cbor_encoder_close_container(&propMap, &policyMap);
                  }
-                 err = err | cbor_encoder_close_container(&propMap, &policyMap);
+                 // Close
+                 err = err || cbor_encoder_close_container(&map, &propMap);
              }
-             // Close
-             err = err | cbor_encoder_close_container(&map, &propMap);
+             // Close Item
+             err = err || cbor_encoder_close_container(&rootArray, &map);
          }
-         // Close Item
-         err = err | cbor_encoder_close_container(&rootArray, &map);
+         // Close main array
+         err = err | cbor_encoder_close_container(&encoder, &rootArray);
      }
-     // Close main array
-     err = err | cbor_encoder_close_container(&encoder, &rootArray);
  
      return checkError(err, &encoder, outPayload, size);
+ cbor_error:
+     OICFree(outPayload);
+     return OC_STACK_ERROR;
  }
  
  static int64_t OCConvertDevicePayload(OCDevicePayload* payload, uint8_t* outPayload,
@@@ -34,7 -26,8 +35,9 @@@
  #include "ocstackinternal.h"
  #include "ocpayload.h"
  #include "cbor.h"
 +#include "oic_string.h"
+ #include "payload_logging.h"
+ #include "rdpayload.h"
  
  #define TAG "OCPayloadParse"
  
@@@ -198,125 -176,215 +204,173 @@@ static OCStackResult OCParseDiscoveryPa
          return OC_STACK_NO_MEMORY;
      }
  
-     size_t resourceCount = 0;
-     while(!err &&
-             cbor_value_is_map(arrayVal))
+     if (cbor_value_is_array(arrayVal))
      {
-         OCResourcePayload* resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
-         if(!resource)
+         OCLinksPayload *linksPayload = NULL;
+         OCTagsPayload *tagsPayload = NULL;
+         while (cbor_value_is_container(arrayVal))
          {
-             OC_LOG(ERROR, TAG, "Memory allocation failed");
-             OCDiscoveryPayloadDestroy(out);
-             return OC_STACK_NO_MEMORY;
-         }
-         CborValue curVal;
+             linksPayload = NULL;
+             tagsPayload = NULL;
+             CborValue colResources;
+             CborError cborFindResult = cbor_value_enter_container(arrayVal, &colResources);
+             if (CborNoError != cborFindResult)
+             {
+                 goto cbor_error;
+             }
  
-         // Uri
-         err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
-         size_t len;
-         err = err || cbor_value_dup_text_string(&curVal, &(resource->uri), &len, NULL);
+             if (OC_STACK_OK != OCTagsCborToPayload(&colResources, &tagsPayload))
+             {
+                 OC_LOG(ERROR, TAG, "Tags cbor parsing failed.");
+                 OCFreeTagsResource(tagsPayload);
+                 goto cbor_error;
+             }
  
-         // SID
-         err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_SERVER_INSTANCE_ID, &curVal);
-         err = err || cbor_value_dup_byte_string(&curVal, &(resource->sid), &len, NULL);
+             if (OC_STACK_OK != OCLinksCborToPayload(&colResources, &linksPayload))
+             {
+                 OC_LOG(ERROR, TAG, "Links cbor parsing failed.");
+                 OCFreeTagsResource(tagsPayload);
+                 OCFreeLinksResource(linksPayload);
+                 goto cbor_error;
+             }
  
-         // Prop Tag
+             if (OC_STACK_OK != OCDiscoveryCollectionPayloadAddResource(out, tagsPayload, linksPayload))
+             {
+                 OC_LOG(ERROR, TAG, "Memory allocation failed");
+                 OCFreeLinksResource(linksPayload);
+                 OCFreeTagsResource(tagsPayload);
+                 OCDiscoveryPayloadDestroy(out);
+                 return OC_STACK_NO_MEMORY;
+             }
+             if (CborNoError != cbor_value_advance(arrayVal))
+             {
+                 OC_LOG(ERROR, TAG, "Cbor value advanced failed.");
+                 goto cbor_error;
+             }
+         }
+     }
+     if (cbor_value_is_map(arrayVal))
+     {
+         size_t resourceCount = 0;
+         while(!err &&
+                 cbor_value_is_map(arrayVal))
          {
-             err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_PROPERTY, &curVal);
-             // ResourceTypes
-             CborValue rtVal;
-             err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE, &rtVal);
+             OCResourcePayload* resource = (OCResourcePayload*)OICCalloc(1, sizeof(OCResourcePayload));
+             if(!resource)
+             {
+                 OC_LOG(ERROR, TAG, "Memory allocation failed");
+                 OCDiscoveryPayloadDestroy(out);
+                 return OC_STACK_NO_MEMORY;
+             }
+             CborValue curVal;
+             // Uri
+             err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_HREF, &curVal);
+             size_t len;
+             err = err || cbor_value_dup_text_string(&curVal, &(resource->uri), &len, NULL);
  
-             if (!err && cbor_value_is_text_string(&rtVal))
+             // SID
+             err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_SERVER_INSTANCE_ID, &curVal);
+             err = err || cbor_value_dup_byte_string(&curVal, &(resource->sid), &len, NULL);
+             // Prop Tag
              {
-                 char* input = NULL;
-                 char* savePtr;
-                 err = err || cbor_value_dup_text_string(&rtVal, &input, &len, NULL);
 -                 err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_PROPERTY, &curVal);
++                err = err || cbor_value_map_find_value(arrayVal, OC_RSRVD_PROPERTY, &curVal);
+                 // ResourceTypes
 -                CborValue rtArray;
 -                 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE, &rtArray);
 -
+                 CborValue rtVal;
 -                 err = err || cbor_value_enter_container(&rtArray, &rtVal);
++                err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_RESOURCE_TYPE, &rtVal);
  
-                 if (input)
 -                OCStringLL* llPtr = NULL;
 -                while(!err && cbor_value_is_text_string(&rtVal))
++                if (!err && cbor_value_is_text_string(&rtVal))
                  {
-                     char* curPtr = strtok_r(input, " ", &savePtr);
 -                    if(resource->types == NULL)
 -                    {
 -                        resource->types = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
 -                        llPtr = resource->types;
 -                        if(!llPtr)
 -                        {
 -                            OC_LOG(ERROR, TAG, "Memory allocation failed");
 -                            OICFree(resource->uri);
 -                            OICFree(resource->sid);
 -                            OICFree(resource);
 -                            OCDiscoveryPayloadDestroy(out);
 -                            return OC_STACK_NO_MEMORY;
 -                        }
 -                    }
 -                    else if(llPtr)
++                    char* input = NULL;
++                    char* savePtr;
++                    err = err || cbor_value_dup_text_string(&rtVal, &input, &len, NULL);
 +
-                     while (curPtr)
++                    if (input)
                      {
-                         char* trimmed = InPlaceStringTrim(curPtr);
-                         if (trimmed[0] !='\0')
 -                        llPtr->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
 -                        llPtr = llPtr->next;
 -                        if(!llPtr)
++                        char* curPtr = strtok_r(input, " ", &savePtr);
++
++                        while (curPtr)
                          {
-                             if (!OCResourcePayloadAddResourceType(resource, trimmed))
 -                            OC_LOG(ERROR, TAG, "Memory allocation failed");
 -                            OICFree(resource->uri);
 -                            OICFree(resource->sid);
 -                            OCFreeOCStringLL(resource->types);
 -                            OICFree(resource);
 -                            OCDiscoveryPayloadDestroy(out);
 -                            return OC_STACK_NO_MEMORY;
++                            char* trimmed = InPlaceStringTrim(curPtr);
++                            if (trimmed[0] !='\0')
 +                            {
-                                 OICFree(resource->uri);
-                                 OICFree(resource->sid);
-                                 OCFreeOCStringLL(resource->types);
-                                 OICFree(resource);
-                                 OCDiscoveryPayloadDestroy(out);
-                                 return OC_STACK_NO_MEMORY;
++                                if (!OCResourcePayloadAddResourceType(resource, trimmed))
++                                {
++                                    OICFree(resource->uri);
++                                    OICFree(resource->sid);
++                                    OCFreeOCStringLL(resource->types);
++                                    OICFree(resource);
++                                    OCDiscoveryPayloadDestroy(out);
++                                    return OC_STACK_NO_MEMORY;
++                                }
 +                            }
++                            curPtr = strtok_r(NULL, " ", &savePtr);
                          }
-                         curPtr = strtok_r(NULL, " ", &savePtr);
++                        OICFree(input);
                      }
-                     OICFree(input);
 -                    else
 -                    {
 -                            OC_LOG(ERROR, TAG, "Unknown state in resource type copying");
 -                            OICFree(resource->uri);
 -                            OICFree(resource->sid);
 -                            OCFreeOCStringLL(resource->types);
 -                            OICFree(resource);
 -                            OCDiscoveryPayloadDestroy(out);
 -                            return OC_STACK_NO_MEMORY;
 -                    }
 -
 -                     err = err || cbor_value_dup_text_string(&rtVal, &(llPtr->value), &len, NULL);
 -                     err = err || cbor_value_advance(&rtVal);
                  }
-             }
-             // Interface Types
-             CborValue ifVal;
-             err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &ifVal);
  
-             if (!err && cbor_value_is_text_string(&ifVal))
-             {
-                 char* input = NULL;
-                 char* savePtr;
-                 err = err || cbor_value_dup_text_string(&ifVal, &input, &len, NULL);
 -                 err = err || cbor_value_leave_container(&rtArray, &rtVal);
 -                //
+                 // Interface Types
 -                CborValue ifArray;
 -                 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &ifArray);
+                 CborValue ifVal;
 -                 err = err || cbor_value_enter_container(&ifArray, &ifVal);
++                err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_INTERFACE, &ifVal);
  
-                 if (input)
 -                llPtr = NULL;
 -                while(!err && cbor_value_is_text_string(&ifVal))
++                if (!err && cbor_value_is_text_string(&ifVal))
                  {
-                     char* curPtr = strtok_r(input, " ", &savePtr);
 -                    if(resource->interfaces == NULL)
 -                    {
 -                        resource->interfaces = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
 -                        llPtr = resource->interfaces;
 -                        if(!llPtr)
 -                        {
 -                            OC_LOG(ERROR, TAG, "Memory allocation failed");
 -                            OICFree(resource->uri);
 -                            OICFree(resource->sid);
 -                            OCFreeOCStringLL(resource->types);
 -                            OICFree(resource);
 -                            OCDiscoveryPayloadDestroy(out);
 -                            return OC_STACK_NO_MEMORY;
 -                        }
 -                    }
 -                    else if (llPtr)
++                    char* input = NULL;
++                    char* savePtr;
++                    err = err || cbor_value_dup_text_string(&ifVal, &input, &len, NULL);
 +
-                     while (curPtr)
++                    if (input)
                      {
-                         char* trimmed = InPlaceStringTrim(curPtr);
-                         if (trimmed[0] !='\0')
 -                        llPtr->next = (OCStringLL*)OICCalloc(1, sizeof(OCStringLL));
 -                        llPtr = llPtr->next;
 -                        if(!llPtr)
++                        char* curPtr = strtok_r(input, " ", &savePtr);
++
++                        while (curPtr)
                          {
-                             if (!OCResourcePayloadAddInterface(resource, trimmed))
 -                            OC_LOG(ERROR, TAG, "Memory allocation failed");
 -                            OICFree(resource->uri);
 -                            OICFree(resource->sid);
 -                            OCFreeOCStringLL(resource->types);
 -                            OCFreeOCStringLL(resource->interfaces);
 -                            OICFree(resource);
 -                            OCDiscoveryPayloadDestroy(out);
 -                            return OC_STACK_NO_MEMORY;
++                            char* trimmed = InPlaceStringTrim(curPtr);
++                            if (trimmed[0] !='\0')
 +                            {
-                                 OICFree(resource->uri);
-                                 OICFree(resource->sid);
-                                 OCFreeOCStringLL(resource->types);
-                                 OICFree(resource);
-                                 OCDiscoveryPayloadDestroy(out);
-                                 return OC_STACK_NO_MEMORY;
++                                if (!OCResourcePayloadAddInterface(resource, trimmed))
++                                {
++                                    OICFree(resource->uri);
++                                    OICFree(resource->sid);
++                                    OCFreeOCStringLL(resource->types);
++                                    OICFree(resource);
++                                    OCDiscoveryPayloadDestroy(out);
++                                    return OC_STACK_NO_MEMORY;
++                                }
 +                            }
++                            curPtr = strtok_r(NULL, " ", &savePtr);
                          }
-                         curPtr = strtok_r(NULL, " ", &savePtr);
++                        OICFree(input);
                      }
-                     OICFree(input);
 -                    else
 -                    {
 -                            OC_LOG(ERROR, TAG, "Unknown state in resource interfaces copying");
 -                            OICFree(resource->uri);
 -                            OICFree(resource->sid);
 -                            OCFreeOCStringLL(resource->types);
 -                            OICFree(resource);
 -                            OCDiscoveryPayloadDestroy(out);
 -                            return OC_STACK_NO_MEMORY;
 -                    }
 -
 -                     err = err || cbor_value_dup_text_string(&ifVal, &(llPtr->value), &len, NULL);
 -                     err = err || cbor_value_advance(&ifVal);
                  }
-             }
 -                 err = err || cbor_value_leave_container(&ifArray, &ifVal);
  
-             // Policy
-             {
-                 CborValue policyMap;
-                 err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_POLICY, &policyMap);
-                 // Bitmap
-                 CborValue val;
-                 err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_BITMAP, &val);
-                 uint64_t temp = 0;
-                 err = err || cbor_value_get_uint64(&val, &temp);
-                 resource->bitmap = (uint8_t)temp;
-                 // Secure Flag
-                 err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &val);
-                 if(cbor_value_is_valid(&val))
+                 // Policy
                  {
-                     err = err || cbor_value_get_boolean(&val, &(resource->secure));
-                     // Port
-                     CborValue port;
-                     err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT,
-                                     &port);
-                     if(cbor_value_is_valid(&port))
+                     CborValue policyMap;
+                     err = err || cbor_value_map_find_value(&curVal, OC_RSRVD_POLICY, &policyMap);
+                     // Bitmap
+                     CborValue val;
+                     err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_BITMAP, &val);
+                     uint64_t temp = 0;
+                     err = err || cbor_value_get_uint64(&val, &temp);
+                     resource->bitmap = (uint8_t)temp;
+                     // Secure Flag
+                     err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_SECURE, &val);
+                     if(cbor_value_is_valid(&val))
                      {
-                         err = err || cbor_value_get_uint64(&port, &temp);
-                         resource->port = (uint16_t)temp;
+                         err = err || cbor_value_get_boolean(&val, &(resource->secure));
+                         // Port
+                         CborValue port;
+                         err = err || cbor_value_map_find_value(&policyMap, OC_RSRVD_HOSTING_PORT,
+                                         &port);
+                         if(cbor_value_is_valid(&port))
+                         {
+                             err = err || cbor_value_get_uint64(&port, &temp);
+                             resource->port = (uint16_t)temp;
+                         }
                      }
                  }
              }
@@@ -1173,12 -1173,10 +1173,16 @@@ void HandleCAResponses(const CAEndpoint
                      {
                          type = PAYLOAD_TYPE_PLATFORM;
                      }
 +#ifdef ROUTING_GATEWAY
 +                    else if (strcmp(cbNode->requestUri, OC_RSRVD_GATEWAY_URI) == 0)
 +                    {
 +                        type = PAYLOAD_TYPE_REPRESENTATION;
 +                    }
 +#endif
+                     else if (strcmp(cbNode->requestUri, OC_RSRVD_RD_URI) == 0)
+                     {
+                         type = PAYLOAD_TYPE_RD;
+                     }
                      else
                      {
                          OC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
@@@ -27,19 -27,24 +27,25 @@@ Import('env'
  target_os = env.get('TARGET_OS')
  
  if target_os not in ['arduino','darwin']:
 -      # Build things manager project
 -      SConscript('things-manager/SConscript')
 +    # Build things manager project
 +    SConscript('things-manager/SConscript')
  
 -      # Build soft sensor manager project
 -      SConscript('soft-sensor-manager/SConscript')
 +    # Build notification manager project
 +    SConscript('resource-hosting/SConscript')
  
 -      # Build protocol plugin project
 -      # protocol-plugin use 'inotify', this feature isn't support by MAC OSX
 -      if target_os not in ['darwin', 'ios', 'android']:
 -              SConscript('protocol-plugin/SConscript')
 +    # Build resource-encapsulation project
 +    SConscript('resource-encapsulation/SConscript')
  
 -      # Build notification manager project
 -      SConscript('notification-manager/SConscript')
 +    # Build simulator module
 +    if target_os in ['linux'] and env.get('SIMULATOR', False):
 +        SConscript('simulator/SConscript')
  
 -      # Build resource-encapsulation project
 -      if target_os not in ['tizen']:
 -              SConscript('resource-encapsulation/SConscript')
++    # Build resource directory project
++    if env.get('WITH_RD') == '1':
++        SConscript('resource-directory/SConscript')
 -      # Build resource directory project
 -      if env.get('WITH_RD') == '1':
 -              SConscript('resource-directory/SConscript')
 +# Build EasySetup module
 +if target_os in ['arduino','android', 'linux']:
 +    SConscript('easy-setup/SConscript')
++
++      
index 0000000,0210a49..5f80354
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,230 +1,230 @@@
 -            OCTagsLog(DEBUG, TAG, temp->publishedResource->tags);
 -            OCLinksLog(DEBUG, TAG, temp->publishedResource->setLinks);
+ //******************************************************************
+ //
+ // Copyright 2015 Samsung Electronics All Rights Reserved.
+ //
+ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ //
+ // Licensed under the Apache License, Version 2.0 (the "License");
+ // you may not use this file except in compliance with the License.
+ // You may obtain a copy of the License at
+ //
+ //      http://www.apache.org/licenses/LICENSE-2.0
+ //
+ // Unless required by applicable law or agreed to in writing, software
+ // distributed under the License is distributed on an "AS IS" BASIS,
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ // See the License for the specific language governing permissions and
+ // limitations under the License.
+ //
+ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ #include "rd_storage.h"
+ #include <pthread.h>
+ #include <string.h>
+ #include "payload_logging.h"
+ #include "oic_malloc.h"
+ #include "rdpayload.h"
+ #define TAG  PCF("RDStorage")
+ pthread_mutex_t storageMutex = PTHREAD_MUTEX_INITIALIZER;
+ // This variable holds the published resources on the RD.
+ static OCRDStorePublishResources *g_rdStorage = NULL;
+ static void printStoragedResources(OCRDStorePublishResources *payload)
+ {
+     OC_LOG(DEBUG, TAG, "Print Storage Resources ... ");
+     for (OCRDStorePublishResources *temp = payload; temp; temp = temp->next)
+     {
+         if (temp->publishedResource)
+         {
++            OCTagsLog(DEBUG, temp->publishedResource->tags);
++            OCLinksLog(DEBUG, temp->publishedResource->setLinks);
+         }
+     }
+ }
+ OCStackResult OCRDStorePublishedResources(const OCResourceCollectionPayload *payload)
+ {
+     OCResourceCollectionPayload *storeResource = (OCResourceCollectionPayload *)OICCalloc(1, sizeof(OCResourceCollectionPayload));
+     if (!storeResource)
+     {
+         OC_LOG(ERROR, TAG, "Failed allocating memory for OCRDStorePublishResources.");
+         return OC_STACK_NO_MEMORY;
+     }
+     OC_LOG(DEBUG, TAG, "Storing Resources ... ");
+     OCTagsPayload *tags = payload->tags;
+     storeResource->tags = OCCopyTagsResources(tags->n.deviceName, tags->di.id, tags->baseURI,
+         tags->bitmap, tags->port, tags->ins, tags->rts, tags->drel, tags->ttl);
+     if (!storeResource->tags)
+     {
+         OC_LOG(ERROR, TAG, "Failed allocating memory for tags.");
+         OCFreeCollectionResource(storeResource);
+         return OC_STACK_NO_MEMORY;
+     }
+     for (OCLinksPayload *links = payload->setLinks; links; links = links->next)
+     {
+         if (!storeResource->setLinks)
+         {
+             storeResource->setLinks = OCCopyLinksResources(links->href, links->rt, links->itf,
+                 links->rel, links->obs, links->title, links->uri, links->ins, links->mt);
+             if (!storeResource->setLinks)
+             {
+                 OC_LOG(ERROR, TAG, "Failed allocating memory for links.");
+                 OCFreeCollectionResource(storeResource);
+                 return OC_STACK_NO_MEMORY;
+             }
+         }
+         else
+         {
+             OCLinksPayload *temp = storeResource->setLinks;
+             while (temp->next)
+             {
+                 temp = temp->next;
+             }
+             temp->next = OCCopyLinksResources(links->href, links->rt, links->itf, links->rel,
+                 links->obs, links->title, links->uri, links->ins, links->mt);
+             if (!temp->next)
+             {
+                 OC_LOG(ERROR, TAG, "Failed allocating memory for links.");
+                 OCFreeCollectionResource(storeResource);
+                 return OC_STACK_NO_MEMORY;
+             }
+         }
+     }
+     storeResource->next = NULL;
+     OCRDStorePublishResources *resources = (OCRDStorePublishResources *)OICCalloc(1, sizeof(OCRDStorePublishResources));
+     if (!resources)
+     {
+         OCFreeCollectionResource(storeResource);
+         return OC_STACK_NO_MEMORY;
+     }
+     resources->publishedResource = storeResource;
+     pthread_mutex_lock(&storageMutex);
+     if (g_rdStorage)
+     {
+         OCRDStorePublishResources *temp = g_rdStorage;
+         while (temp->next)
+         {
+             temp = temp->next;
+         }
+         temp->next = resources;
+     }
+     else
+     {
+         g_rdStorage = resources;
+     }
+     pthread_mutex_unlock(&storageMutex);
+     printStoragedResources(g_rdStorage);
+     return OC_STACK_OK;
+ }
+ OCStackResult OCRDCheckPublishedResource(const char *interfaceType, const char *resourceType,
+         OCResourceCollectionPayload **payload)
+ {
+     // ResourceType and InterfaceType if both are NULL it will return. If either is
+     // not null it will continue execution.
+     if (!resourceType && !interfaceType)
+     {
+         OC_LOG(DEBUG, TAG, "Missing resource type and interace type.");
+         return OC_STACK_INVALID_PARAM;
+     }
+     OC_LOG(DEBUG, TAG, "Check Resource in RD");
+     if (g_rdStorage && g_rdStorage->publishedResource)
+     {
+         for (OCRDStorePublishResources *pResource = g_rdStorage;
+                 pResource; pResource = pResource->next)
+         {
+             if (pResource->publishedResource->setLinks)
+             {
+                 for (OCLinksPayload *tLinks = pResource->publishedResource->setLinks; tLinks; tLinks = tLinks->next)
+                 {
+                     // If either rt or itf are NULL, it should skip remaining code execution.
+                     if (!tLinks->rt || !tLinks->itf)
+                     {
+                         OC_LOG(DEBUG, TAG, "Either resource type and interface type are missing.");
+                         continue;
+                     }
+                     if (resourceType)
+                     {
+                         OCStringLL *temp = tLinks->rt;
+                         while(temp)
+                         {
+                             OC_LOG_V(DEBUG, TAG, "Resource Type: %s %s", resourceType, temp->value);
+                             if (strcmp(resourceType, temp->value) == 0)
+                             {
+                                 OCTagsPayload *tag = pResource->publishedResource->tags;
+                                 OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
+                                     tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
+                                 if (!tags)
+                                 {
+                                     return OC_STACK_NO_MEMORY;
+                                 }
+                                 OCLinksPayload *links = OCCopyLinksResources(tLinks->href, tLinks->rt, tLinks->itf,
+                                     tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
+                                 if (!links)
+                                 {
+                                     OCFreeTagsResource(tags);
+                                     return OC_STACK_NO_MEMORY;
+                                 }
+                                 *payload = OCCopyCollectionResource(tags, links);
+                                 if (!*payload)
+                                 {
+                                     OCFreeTagsResource(tags);
+                                     OCFreeLinksResource(links);
+                                     return OC_STACK_NO_MEMORY;
+                                 }
+                                 return OC_STACK_OK;
+                             }
+                             temp = temp->next;
+                         }
+                     }
+                     if (interfaceType)
+                     {
+                         OCStringLL *temp = tLinks->itf;
+                         while (temp)
+                         {
+                             OC_LOG_V(DEBUG, TAG, "Interface Type: %s %s", interfaceType, temp->value);
+                             if (strcmp(interfaceType, temp->value) == 0)
+                             {
+                                 OCTagsPayload *tag = pResource->publishedResource->tags;
+                                 OCTagsPayload *tags = OCCopyTagsResources(tag->n.deviceName, tag->di.id, tag->baseURI,
+                                     tag->bitmap, tag->port, tag->ins, tag->rts, tag->drel, tag->ttl);
+                                 if (!tags)
+                                 {
+                                     return OC_STACK_NO_MEMORY;
+                                 }
+                                 OCLinksPayload *links = OCCopyLinksResources(tLinks->uri, tLinks->rt, tLinks->itf,
+                                     tLinks->rel, tLinks->obs, tLinks->title, tLinks->uri, tLinks->ins, tLinks->mt);
+                                 if (!links)
+                                 {
+                                     OCFreeTagsResource(tags);
+                                     return OC_STACK_NO_MEMORY;
+                                 }
+                                 *payload = OCCopyCollectionResource(tags, links);
+                                 if (!*payload)
+                                 {
+                                     OCFreeTagsResource(tags);
+                                     OCFreeLinksResource(links);
+                                     return OC_STACK_NO_MEMORY;
+                                 }
+                                 return OC_STACK_OK;
+                             }
+                             temp = temp->next;
+                         }
+                     }
+                 }
+             }
+         }
+     }
+     return OC_STACK_ERROR;
+ }