Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocstack.c
index b2aec91..5ca4deb 100644 (file)
 //-----------------------------------------------------------------------------
 // Includes
 //-----------------------------------------------------------------------------
+
+// Defining _POSIX_C_SOURCE macro with 200112L (or greater) as value
+// causes header files to expose definitions
+// corresponding to the POSIX.1-2001 base
+// specification (excluding the XSI extension).
+// For POSIX.1-2001 base specification,
+// Refer http://pubs.opengroup.org/onlinepubs/009695399/
 #define _POSIX_C_SOURCE 200112L
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS
+#endif
+#ifndef __STDC_LIMIT_MACROS
+#define __STDC_LIMIT_MACROS
+#endif
+#include <inttypes.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "ocstack.h"
 #include "ocstackinternal.h"
 #include "occlientcb.h"
 #include "ocobserve.h"
 #include "ocrandom.h"
-#include "debug.h"
-#include "occoap.h"
-#include "ocmalloc.h"
+#include "oic_malloc.h"
+#include "oic_string.h"
+#include "logger.h"
 #include "ocserverrequest.h"
+#include "secureresourcemanager.h"
+#include "doxmresource.h"
+#include "cacommon.h"
+#include "cainterface.h"
+#include "ocpayload.h"
+#include "ocpayloadcbor.h"
+
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+#include "routingutility.h"
+#ifdef ROUTING_GATEWAY
+#include "routingmanager.h"
+#endif
+#endif
+
+#ifdef TCP_ADAPTER
+#include "oickeepalive.h"
+#endif
+
+//#ifdef DIRECT_PAIRING
+#include "directpairing.h"
+//#endif
+
+#ifdef WITH_ARDUINO
+#include "Time.h"
+#else
+#include <sys/time.h>
+#endif
+#include "coap_time.h"
+#include "utlist.h"
+#include "pdu.h"
+
+#ifndef ARDUINO
+#include <arpa/inet.h>
+#endif
+
+#ifndef UINT32_MAX
+#define UINT32_MAX   (0xFFFFFFFFUL)
+#endif
 
 //-----------------------------------------------------------------------------
 // Typedefs
 //-----------------------------------------------------------------------------
-typedef enum {
-    OC_STACK_UNINITIALIZED = 0, OC_STACK_INITIALIZED, OC_STACK_UNINIT_IN_PROGRESS
+typedef enum
+{
+    OC_STACK_UNINITIALIZED = 0,
+    OC_STACK_INITIALIZED,
+    OC_STACK_UNINIT_IN_PROGRESS
 } OCStackState;
 
 #ifdef WITH_PRESENCE
-typedef enum {
-    OC_PRESENCE_UNINITIALIZED = 0, OC_PRESENCE_INITIALIZED
+typedef enum
+{
+    OC_PRESENCE_UNINITIALIZED = 0,
+    OC_PRESENCE_INITIALIZED
 } OCPresenceState;
 #endif
 
@@ -55,37 +113,396 @@ typedef enum {
 static OCStackState stackState = OC_STACK_UNINITIALIZED;
 
 OCResource *headResource = NULL;
+static OCResource *tailResource = NULL;
+static OCResourceHandle platformResource = {0};
+static OCResourceHandle deviceResource = {0};
 #ifdef WITH_PRESENCE
 static OCPresenceState presenceState = OC_PRESENCE_UNINITIALIZED;
 static PresenceResource presenceResource;
-uint8_t PresenceTimeOutSize = 0;
-uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
+static uint8_t PresenceTimeOutSize = 0;
+static uint32_t PresenceTimeOut[] = {50, 75, 85, 95, 100};
 #endif
 
-OCMode myStackMode;
+static OCMode myStackMode;
+#ifdef RA_ADAPTER
+//TODO: revisit this design
+static bool gRASetInfo = false;
+#endif
 OCDeviceEntityHandler defaultDeviceHandler;
+void* defaultDeviceHandlerCallbackParameter = NULL;
+static const char COAP_TCP[] = "coap+tcp:";
+
+//#ifdef DIRECT_PAIRING
+OCDirectPairingCB gDirectpairingCallback = NULL;
+//#endif
 
 //-----------------------------------------------------------------------------
 // Macros
 //-----------------------------------------------------------------------------
-#define TAG  PCF("OCStack")
-#define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OC_LOG((logLevel), \
-             TAG, PCF(#arg " is NULL")); return (retVal); } }
+#define TAG  "OIC_RI_STACK"
+#define VERIFY_SUCCESS(op, successCode) { if ((op) != (successCode)) \
+            {OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); goto exit;} }
+#define VERIFY_NON_NULL(arg, logLevel, retVal) { if (!(arg)) { OIC_LOG((logLevel), \
+             TAG, #arg " is NULL"); return (retVal); } }
+#define VERIFY_NON_NULL_NR(arg, logLevel) { if (!(arg)) { OIC_LOG((logLevel), \
+             TAG, #arg " is NULL"); return; } }
+#define VERIFY_NON_NULL_V(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL");\
+    goto exit;} }
 
 //TODO: we should allow the server to define this
 #define MAX_OBSERVE_AGE (0x2FFFFUL)
+
+#define MILLISECONDS_PER_SECOND   (1000)
+
+//-----------------------------------------------------------------------------
+// Private internal function prototypes
+//-----------------------------------------------------------------------------
+
+/**
+ * Generate handle of OCDoResource invocation for callback management.
+ *
+ * @return Generated OCDoResource handle.
+ */
+static OCDoHandle GenerateInvocationHandle();
+
+/**
+ * Initialize resource data structures, variables, etc.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult initResources();
+
+/**
+ * Add a resource to the end of the linked list of resources.
+ *
+ * @param resource Resource to be added
+ */
+static void insertResource(OCResource *resource);
+
+/**
+ * Find a resource in the linked list of resources.
+ *
+ * @param resource Resource to be found.
+ * @return Pointer to resource that was found in the linked list or NULL if the resource was not
+ *         found.
+ */
+static OCResource *findResource(OCResource *resource);
+
+/**
+ * Insert a resource type into a resource's resource type linked list.
+ * If resource type already exists, it will not be inserted and the
+ * resourceType will be free'd.
+ * resourceType->next should be null to avoid memory leaks.
+ * Function returns silently for null args.
+ *
+ * @param resource Resource where resource type is to be inserted.
+ * @param resourceType Resource type to be inserted.
+ */
+static void insertResourceType(OCResource *resource,
+        OCResourceType *resourceType);
+
+/**
+ * Get a resource type at the specified index within a resource.
+ *
+ * @param handle Handle of resource.
+ * @param index Index of resource type.
+ *
+ * @return Pointer to resource type if found, NULL otherwise.
+ */
+static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
+        uint8_t index);
+
+/**
+ * Insert a resource interface into a resource's resource interface linked list.
+ * If resource interface already exists, it will not be inserted and the
+ * resourceInterface will be free'd.
+ * resourceInterface->next should be null to avoid memory leaks.
+ *
+ * @param resource Resource where resource interface is to be inserted.
+ * @param resourceInterface Resource interface to be inserted.
+ */
+static void insertResourceInterface(OCResource *resource,
+        OCResourceInterface *resourceInterface);
+
+/**
+ * Get a resource interface at the specified index within a resource.
+ *
+ * @param handle Handle of resource.
+ * @param index Index of resource interface.
+ *
+ * @return Pointer to resource interface if found, NULL otherwise.
+ */
+static OCResourceInterface *findResourceInterfaceAtIndex(
+        OCResourceHandle handle, uint8_t index);
+
+/**
+ * Delete all of the dynamically allocated elements that were created for the resource type.
+ *
+ * @param resourceType Specified resource type.
+ */
+static void deleteResourceType(OCResourceType *resourceType);
+
+/**
+ * Delete all of the dynamically allocated elements that were created for the resource interface.
+ *
+ * @param resourceInterface Specified resource interface.
+ */
+static void deleteResourceInterface(OCResourceInterface *resourceInterface);
+
+/**
+ * Delete all of the dynamically allocated elements that were created for the resource.
+ *
+ * @param resource Specified resource.
+ */
+static void deleteResourceElements(OCResource *resource);
+
+/**
+ * Delete resource specified by handle.  Deletes resource and all resourcetype and resourceinterface
+ * linked lists.
+ *
+ * @param handle Handle of resource to be deleted.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult deleteResource(OCResource *resource);
+
+/**
+ * Delete all of the resources in the resource list.
+ */
+static void deleteAllResources();
+
+/**
+ * Increment resource sequence number.  Handles rollover.
+ *
+ * @param resPtr Pointer to resource.
+ */
+static void incrementSequenceNumber(OCResource * resPtr);
+
+/**
+ * Verify the lengths of the URI and the query separately.
+ *
+ * @param inputUri Input URI and query.
+ * @param uriLen The length of the initial URI with query.
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult verifyUriQueryLength(const char * inputUri,
+        uint16_t uriLen);
+
+/*
+ * Attempts to initialize every network interface that the CA Layer might have compiled in.
+ *
+ * Note: At least one interface must succeed to initialize. If all calls to @ref CASelectNetwork
+ * return something other than @ref CA_STATUS_OK, then this function fails.
+ *
+ * @return ::CA_STATUS_OK on success, some other value upon failure.
+ */
+static CAResult_t OCSelectNetwork();
+
+/**
+ * Get the CoAP ticks after the specified number of milli-seconds.
+ *
+ * @param afterMilliSeconds Milli-seconds.
+ * @return
+ *     CoAP ticks
+ */
+static uint32_t GetTicks(uint32_t afterMilliSeconds);
+
+/**
+ * Convert CAResponseResult_t to OCStackResult.
+ *
+ * @param caCode CAResponseResult_t code.
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult CAToOCStackResult(CAResponseResult_t caCode);
+
+/**
+ * Convert OCStackResult to CAResponseResult_t.
+ *
+ * @param caCode OCStackResult code.
+ * @param method OCMethod method the return code replies to.
+ * @return ::CA_CONTENT on OK, some other value upon failure.
+ */
+static CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method);
+
+/**
+ * Convert OCTransportFlags_t to CATransportModifiers_t.
+ *
+ * @param ocConType OCTransportFlags_t input.
+ * @return CATransportFlags
+ */
+static CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocConType);
+
+/**
+ * Convert CATransportFlags_t to OCTransportModifiers_t.
+ *
+ * @param caConType CATransportFlags_t input.
+ * @return OCTransportFlags
+ */
+static OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caConType);
+
+/**
+ * Handle response from presence request.
+ *
+ * @param endPoint CA remote endpoint.
+ * @param responseInfo CA response info.
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult HandlePresenceResponse(const CAEndpoint_t *endPoint,
+        const CAResponseInfo_t *responseInfo);
+
+/**
+ * This function will be called back by CA layer when a response is received.
+ *
+ * @param endPoint CA remote endpoint.
+ * @param responseInfo CA response info.
+ */
+static void HandleCAResponses(const CAEndpoint_t* endPoint,
+        const CAResponseInfo_t* responseInfo);
+
+/**
+ * This function will be called back by CA layer when a request is received.
+ *
+ * @param endPoint CA remote endpoint.
+ * @param requestInfo CA request info.
+ */
+static void HandleCARequests(const CAEndpoint_t* endPoint,
+        const CARequestInfo_t* requestInfo);
+
+/**
+ * Extract query from a URI.
+ *
+ * @param uri Full URI with query.
+ * @param query Pointer to string that will contain query.
+ * @param newURI Pointer to string that will contain URI.
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult getQueryFromUri(const char * uri, char** resourceType, char ** newURI);
+
+/**
+ * Finds a resource type in an OCResourceType link-list.
+ *
+ * @param resourceTypeList The link-list to be searched through.
+ * @param resourceTypeName The key to search for.
+ *
+ * @return Resource type that matches the key (ie. resourceTypeName) or
+ *      NULL if there is either an invalid parameter or this function was unable to find the key.
+ */
+static OCResourceType *findResourceType(OCResourceType * resourceTypeList,
+        const char * resourceTypeName);
+
+/**
+ * Reset presence TTL for a ClientCB struct. ttlLevel will be set to 0.
+ * TTL will be set to maxAge.
+ *
+ * @param cbNode Callback Node for which presence ttl is to be reset.
+ * @param maxAge New value of ttl in seconds.
+
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds);
+
+/**
+ * Ensure the accept header option is set appropriatly before sending the requests and routing
+ * header option is updated with destination.
+ *
+ * @param object CA remote endpoint.
+ * @param requestInfo CA request info.
+ *
+ * @return ::OC_STACK_OK on success, some other value upon failure.
+ */
+static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo);
+
 //-----------------------------------------------------------------------------
-// Externs
+// Internal functions
 //-----------------------------------------------------------------------------
-extern void DeinitOCSecurityInfo();
 
+uint32_t GetTicks(uint32_t afterMilliSeconds)
+{
+    coap_tick_t now;
+    coap_ticks(&now);
+
+    // Guard against overflow of uint32_t
+    if (afterMilliSeconds <= ((UINT32_MAX - (uint32_t)now) * MILLISECONDS_PER_SECOND) /
+                             COAP_TICKS_PER_SECOND)
+    {
+        return now + (afterMilliSeconds * COAP_TICKS_PER_SECOND)/MILLISECONDS_PER_SECOND;
+    }
+    else
+    {
+        return UINT32_MAX;
+    }
+}
+
+void CopyEndpointToDevAddr(const CAEndpoint_t *in, OCDevAddr *out)
+{
+    VERIFY_NON_NULL_NR(in, FATAL);
+    VERIFY_NON_NULL_NR(out, FATAL);
+
+    out->adapter = (OCTransportAdapter)in->adapter;
+    out->flags = CAToOCTransportFlags(in->flags);
+    OICStrcpy(out->addr, sizeof(out->addr), in->addr);
+    out->port = in->port;
+    out->interface = in->interface;
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+    memcpy(out->routeData, in->routeData, sizeof(out->routeData));
+#endif
+}
+
+void CopyDevAddrToEndpoint(const OCDevAddr *in, CAEndpoint_t *out)
+{
+    VERIFY_NON_NULL_NR(in, FATAL);
+    VERIFY_NON_NULL_NR(out, FATAL);
+
+    out->adapter = (CATransportAdapter_t)in->adapter;
+    out->flags = OCToCATransportFlags(in->flags);
+    OICStrcpy(out->addr, sizeof(out->addr), in->addr);
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+    memcpy(out->routeData, in->routeData, sizeof(out->routeData));
+#endif
+    out->port = in->port;
+    out->interface = in->interface;
+}
+
+void FixUpClientResponse(OCClientResponse *cr)
+{
+    VERIFY_NON_NULL_NR(cr, FATAL);
+
+    cr->addr = &cr->devAddr;
+    cr->connType = (OCConnectivityType)
+        ((cr->devAddr.adapter << CT_ADAPTER_SHIFT) | (cr->devAddr.flags & CT_MASK_FLAGS));
+}
+
+static OCStackResult OCSendRequest(const CAEndpoint_t *object, CARequestInfo_t *requestInfo)
+{
+    VERIFY_NON_NULL(object, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(requestInfo, FATAL, OC_STACK_INVALID_PARAM);
+
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+    OCStackResult rmResult = RMAddInfo(object->routeData, requestInfo, true, NULL);
+    if (OC_STACK_OK != rmResult)
+    {
+        OIC_LOG(ERROR, TAG, "Add destination option failed");
+        return rmResult;
+    }
+#endif
+
+    // OC stack prefer CBOR encoded payloads.
+    requestInfo->info.acceptFormat = CA_FORMAT_APPLICATION_CBOR;
+    CAResult_t result = CASendRequest(object, requestInfo);
+    if(CA_STATUS_OK != result)
+    {
+        OIC_LOG_V(ERROR, TAG, "CASendRequest failed with CA error %u", result);
+        return CAResultToOCResult(result);
+    }
+    return OC_STACK_OK;
+}
 //-----------------------------------------------------------------------------
 // Internal API function
 //-----------------------------------------------------------------------------
 
 // This internal function is called to update the stack with the status of
 // observers and communication failures
-OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
+OCStackResult OCStackFeedBack(CAToken_t token, uint8_t tokenLength, uint8_t status)
 {
     OCStackResult result = OC_STACK_ERROR;
     ResourceObserver * observer = NULL;
@@ -94,36 +511,42 @@ OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
     switch(status)
     {
     case OC_OBSERVER_NOT_INTERESTED:
-        OC_LOG(DEBUG, TAG, PCF("observer is not interested in our notifications anymore"));
-        observer = GetObserverUsingToken (token);
+        OIC_LOG(DEBUG, TAG, "observer not interested in our notifications");
+        observer = GetObserverUsingToken (token, tokenLength);
         if(observer)
         {
-            result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
-                    OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
-                    NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
+            result = FormOCEntityHandlerRequest(&ehRequest,
+                                                (OCRequestHandle)NULL,
+                                                OC_REST_NOMETHOD,
+                                                &observer->devAddr,
+                                                (OCResourceHandle)NULL,
+                                                NULL, PAYLOAD_TYPE_REPRESENTATION,
+                                                NULL, 0, 0, NULL,
+                                                OC_OBSERVE_DEREGISTER,
+                                                observer->observeId);
             if(result != OC_STACK_OK)
             {
                 return result;
             }
-            observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
+            observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
+                            observer->resource->entityHandlerCallbackParam);
         }
-        //observer is not observing anymore
-        result = DeleteObserverUsingToken (token);
+
+        result = DeleteObserverUsingToken (token, tokenLength);
         if(result == OC_STACK_OK)
         {
-            OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
+            OIC_LOG(DEBUG, TAG, "Removed observer successfully");
         }
         else
         {
             result = OC_STACK_OK;
-            OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
+            OIC_LOG(DEBUG, TAG, "Observer Removal failed");
         }
         break;
+
     case OC_OBSERVER_STILL_INTERESTED:
-        //observer is still interested
-        OC_LOG(DEBUG, TAG, PCF("observer is interested in our \
-                notifications, reset the failedCount"));
-        observer = GetObserverUsingToken(token);
+        OIC_LOG(DEBUG, TAG, "observer still interested, reset the failedCount");
+        observer = GetObserverUsingToken (token, tokenLength);
         if(observer)
         {
             observer->forceHighQos = 0;
@@ -135,32 +558,39 @@ OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
             result = OC_STACK_OBSERVER_NOT_FOUND;
         }
         break;
+
     case OC_OBSERVER_FAILED_COMM:
-        //observer is not reachable
-        OC_LOG(DEBUG, TAG, PCF("observer is unreachable"));
-        observer = GetObserverUsingToken(token);
+        OIC_LOG(DEBUG, TAG, "observer is unreachable");
+        observer = GetObserverUsingToken (token, tokenLength);
         if(observer)
         {
             if(observer->failedCommCount >= MAX_OBSERVER_FAILED_COMM)
             {
-                result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) NULL,
-                        OC_REST_NOMETHOD, (OCResourceHandle) NULL, NULL, NULL, 0,
-                        NULL, OC_OBSERVE_DEREGISTER, observer->observeId);
+                result = FormOCEntityHandlerRequest(&ehRequest,
+                                                    (OCRequestHandle)NULL,
+                                                    OC_REST_NOMETHOD,
+                                                    &observer->devAddr,
+                                                    (OCResourceHandle)NULL,
+                                                    NULL, PAYLOAD_TYPE_REPRESENTATION,
+                                                    NULL, 0, 0, NULL,
+                                                    OC_OBSERVE_DEREGISTER,
+                                                    observer->observeId);
                 if(result != OC_STACK_OK)
                 {
                     return OC_STACK_ERROR;
                 }
-                observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest);
-                //observer is unreachable
-                result = DeleteObserverUsingToken (token);
+                observer->resource->entityHandler(OC_OBSERVE_FLAG, &ehRequest,
+                                    observer->resource->entityHandlerCallbackParam);
+
+                result = DeleteObserverUsingToken (token, tokenLength);
                 if(result == OC_STACK_OK)
                 {
-                    OC_LOG(DEBUG, TAG, PCF("Removed observer successfully"));
+                    OIC_LOG(DEBUG, TAG, "Removed observer successfully");
                 }
                 else
                 {
                     result = OC_STACK_OK;
-                    OC_LOG(DEBUG, TAG, PCF("Observer Removal failed"));
+                    OIC_LOG(DEBUG, TAG, "Observer Removal failed");
                 }
             }
             else
@@ -169,329 +599,1590 @@ OCStackResult OCStackFeedBack(OCCoAPToken * token, uint8_t status)
                 result = OC_STACK_CONTINUE;
             }
             observer->forceHighQos = 1;
-            OC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
+            OIC_LOG_V(DEBUG, TAG, "Failed count for this observer is %d",observer->failedCommCount);
         }
         break;
     default:
-        OC_LOG(ERROR, TAG, PCF("Unknown status"));
+        OIC_LOG(ERROR, TAG, "Unknown status");
         result = OC_STACK_ERROR;
         break;
         }
     return result;
 }
-
-//This function will be called back by occoap layer when a request is received
-OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
+OCStackResult CAToOCStackResult(CAResponseResult_t caCode)
 {
-    OC_LOG(INFO, TAG, PCF("Entering HandleStackRequests (OCStack Layer)"));
+    OCStackResult ret = OC_STACK_ERROR;
 
-    OCStackResult result = OC_STACK_ERROR;
-    ResourceHandling resHandling;
-    OCResource *resource;
-
-    OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken);
-    if(!request)
+    switch(caCode)
     {
-        OC_LOG(INFO, TAG, PCF("This is a new Server Request"));
-        result = AddServerRequest(&request, protocolRequest->coapID,
-                protocolRequest->delayedResNeeded, protocolRequest->secured, 0,
-                protocolRequest->method, protocolRequest->numRcvdVendorSpecificHeaderOptions,
-                protocolRequest->observationOption, protocolRequest->qos,
-                protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
-                protocolRequest->reqJSONPayload, &protocolRequest->requestToken,
-                &protocolRequest->requesterAddr, protocolRequest->resourceUrl,
-                protocolRequest->reqTotalSize);
-        if (OC_STACK_OK != result)
-        {
-            OC_LOG(ERROR, TAG, PCF("Error adding server request"));
-            return result;
-        }
-        VERIFY_NON_NULL(request, ERROR, OC_STACK_NO_MEMORY);
-
-        if(!protocolRequest->reqMorePacket)
-        {
-            request->requestComplete = 1;
-        }
+        case CA_CREATED:
+            ret = OC_STACK_RESOURCE_CREATED;
+            break;
+        case CA_DELETED:
+            ret = OC_STACK_RESOURCE_DELETED;
+            break;
+        case CA_CHANGED:
+        case CA_CONTENT:
+        case CA_VALID:
+            ret = OC_STACK_OK;
+            break;
+        case CA_BAD_REQ:
+            ret = OC_STACK_INVALID_QUERY;
+            break;
+        case CA_UNAUTHORIZED_REQ:
+            ret = OC_STACK_UNAUTHORIZED_REQ;
+            break;
+        case CA_BAD_OPT:
+            ret = OC_STACK_INVALID_OPTION;
+            break;
+        case CA_NOT_FOUND:
+            ret = OC_STACK_NO_RESOURCE;
+            break;
+        case CA_RETRANSMIT_TIMEOUT:
+            ret = OC_STACK_COMM_ERROR;
+            break;
+        default:
+            break;
     }
-    else
+    return ret;
+}
+
+CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method)
+{
+    CAResponseResult_t ret = CA_INTERNAL_SERVER_ERROR;
+
+    switch(ocCode)
     {
-        OC_LOG(INFO, TAG, PCF("This is either a repeated Server Request or blocked Server Request"));
+        case OC_STACK_OK:
+           switch (method)
+           {
+               case OC_REST_PUT:
+               case OC_REST_POST:
+                   // This Response Code is like HTTP 204 "No Content" but only used in
+                   // response to POST and PUT requests.
+                   ret = CA_CHANGED;
+                   break;
+               case OC_REST_GET:
+                   // This Response Code is like HTTP 200 "OK" but only used in response to
+                   // GET requests.
+                   ret = CA_CONTENT;
+                   break;
+               default:
+                   // This should not happen but,
+                   // give it a value just in case but output an error
+                   ret = CA_CONTENT;
+                   OIC_LOG_V(ERROR, TAG, "Unexpected OC_STACK_OK return code for method [%d].", method);
+            }
+            break;
+        case OC_STACK_RESOURCE_CREATED:
+            ret = CA_CREATED;
+            break;
+        case OC_STACK_RESOURCE_DELETED:
+            ret = CA_DELETED;
+            break;
+        case OC_STACK_INVALID_QUERY:
+            ret = CA_BAD_REQ;
+            break;
+        case OC_STACK_INVALID_OPTION:
+            ret = CA_BAD_OPT;
+            break;
+        case OC_STACK_NO_RESOURCE:
+            ret = CA_NOT_FOUND;
+            break;
+        case OC_STACK_COMM_ERROR:
+            ret = CA_RETRANSMIT_TIMEOUT;
+            break;
+        case OC_STACK_UNAUTHORIZED_REQ:
+            ret = CA_UNAUTHORIZED_REQ;
+            break;
+        default:
+            break;
+    }
+    return ret;
+}
+
+CATransportFlags_t OCToCATransportFlags(OCTransportFlags ocFlags)
+{
+    CATransportFlags_t caFlags = (CATransportFlags_t)ocFlags;
+
+    // supply default behavior.
+    if ((caFlags & (CA_IPV6|CA_IPV4)) == 0)
+    {
+        caFlags = (CATransportFlags_t)(caFlags|CA_IPV6|CA_IPV4);
+    }
+    if ((caFlags & OC_MASK_SCOPE) == 0)
+    {
+        caFlags = (CATransportFlags_t)(caFlags|OC_SCOPE_LINK);
+    }
+    return caFlags;
+}
+
+OCTransportFlags CAToOCTransportFlags(CATransportFlags_t caFlags)
+{
+    return (OCTransportFlags)caFlags;
+}
+
+static OCStackResult ResetPresenceTTL(ClientCB *cbNode, uint32_t maxAgeSeconds)
+{
+    uint32_t lowerBound  = 0;
+    uint32_t higherBound = 0;
+
+    if (!cbNode || !cbNode->presence || !cbNode->presence->timeOut)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    OIC_LOG_V(INFO, TAG, "Update presence TTL, time is %u", GetTicks(0));
+
+    cbNode->presence->TTL = maxAgeSeconds;
+
+    for (int index = 0; index < PresenceTimeOutSize; index++)
+    {
+        // Guard against overflow
+        if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index]))
+                                     * 100)
+        {
+            lowerBound = GetTicks((PresenceTimeOut[index] *
+                                  cbNode->presence->TTL *
+                                  MILLISECONDS_PER_SECOND)/100);
+        }
+        else
+        {
+            lowerBound = GetTicks(UINT32_MAX);
+        }
+
+        if (cbNode->presence->TTL < (UINT32_MAX/(MILLISECONDS_PER_SECOND*PresenceTimeOut[index+1]))
+                                     * 100)
+        {
+            higherBound = GetTicks((PresenceTimeOut[index + 1] *
+                                   cbNode->presence->TTL *
+                                   MILLISECONDS_PER_SECOND)/100);
+        }
+        else
+        {
+            higherBound = GetTicks(UINT32_MAX);
+        }
+
+        cbNode->presence->timeOut[index] = OCGetRandomRange(lowerBound, higherBound);
+
+        OIC_LOG_V(DEBUG, TAG, "lowerBound timeout  %d", lowerBound);
+        OIC_LOG_V(DEBUG, TAG, "higherBound timeout %d", higherBound);
+        OIC_LOG_V(DEBUG, TAG, "timeOut entry  %d", cbNode->presence->timeOut[index]);
+    }
+
+    cbNode->presence->TTLlevel = 0;
+
+    OIC_LOG_V(DEBUG, TAG, "this TTL level %d", cbNode->presence->TTLlevel);
+    return OC_STACK_OK;
+}
+
+const char *convertTriggerEnumToString(OCPresenceTrigger trigger)
+{
+    if (trigger == OC_PRESENCE_TRIGGER_CREATE)
+    {
+        return OC_RSRVD_TRIGGER_CREATE;
+    }
+    else if (trigger == OC_PRESENCE_TRIGGER_CHANGE)
+    {
+        return OC_RSRVD_TRIGGER_CHANGE;
+    }
+    else
+    {
+        return OC_RSRVD_TRIGGER_DELETE;
+    }
+}
+
+OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr)
+{
+    if(!triggerStr)
+    {
+        return OC_PRESENCE_TRIGGER_CREATE;
+    }
+    else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CREATE) == 0)
+    {
+        return OC_PRESENCE_TRIGGER_CREATE;
+    }
+    else if(strcmp(triggerStr, OC_RSRVD_TRIGGER_CHANGE) == 0)
+    {
+        return OC_PRESENCE_TRIGGER_CHANGE;
+    }
+    else
+    {
+        return OC_PRESENCE_TRIGGER_DELETE;
+    }
+}
+
+/**
+ * The cononical presence allows constructed URIs to be string compared.
+ *
+ * requestUri must be a char array of size CA_MAX_URI_LENGTH
+ */
+static int FormCanonicalPresenceUri(const CAEndpoint_t *endpoint, char *resourceUri,
+        char *presenceUri)
+{
+    VERIFY_NON_NULL(endpoint   , FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(resourceUri, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(presenceUri, FATAL, OC_STACK_INVALID_PARAM);
+
+    CAEndpoint_t *ep = (CAEndpoint_t *)endpoint;
+
+    if (ep->adapter == CA_ADAPTER_IP)
+    {
+        if ((ep->flags & CA_IPV6) && !(ep->flags & CA_IPV4))
+        {
+            if ('\0' == ep->addr[0])  // multicast
+            {
+                return snprintf(presenceUri, CA_MAX_URI_LENGTH, OC_RSRVD_PRESENCE_URI);
+            }
+            else
+            {
+                return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://[%s]:%u%s",
+                        ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
+            }
+        }
+        else
+        {
+            if ('\0' == ep->addr[0])  // multicast
+            {
+                OICStrcpy(ep->addr, sizeof(ep->addr), OC_MULTICAST_IP);
+                ep->port = OC_MULTICAST_PORT;
+            }
+            return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s:%u%s",
+                    ep->addr, ep->port, OC_RSRVD_PRESENCE_URI);
+        }
+    }
+
+    // might work for other adapters (untested, but better than nothing)
+    return snprintf(presenceUri, CA_MAX_URI_LENGTH, "coap://%s%s", ep->addr,
+                    OC_RSRVD_PRESENCE_URI);
+}
+
+
+OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
+                            const CAResponseInfo_t *responseInfo)
+{
+    VERIFY_NON_NULL(endpoint, FATAL, OC_STACK_INVALID_PARAM);
+    VERIFY_NON_NULL(responseInfo, FATAL, OC_STACK_INVALID_PARAM);
+
+    OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
+    ClientCB * cbNode = NULL;
+    char *resourceTypeName = NULL;
+    OCClientResponse response = {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
+    OCStackResult result = OC_STACK_ERROR;
+    uint32_t maxAge = 0;
+    int uriLen;
+    char presenceUri[CA_MAX_URI_LENGTH];
+
+    int presenceSubscribe = 0;
+    int multicastPresenceSubscribe = 0;
+
+    if (responseInfo->result != CA_CONTENT)
+    {
+        OIC_LOG_V(ERROR, TAG, "HandlePresenceResponse failed %d", responseInfo->result);
+        return OC_STACK_ERROR;
+    }
+
+    // check for unicast presence
+    uriLen = FormCanonicalPresenceUri(endpoint, OC_RSRVD_PRESENCE_URI, presenceUri);
+    if (uriLen < 0 || (size_t)uriLen >= sizeof (presenceUri))
+    {
+        return OC_STACK_INVALID_URI;
+    }
+
+    cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
+    if (cbNode)
+    {
+        presenceSubscribe = 1;
+    }
+    else
+    {
+        // check for multiicast presence
+        CAEndpoint_t ep = { .adapter = endpoint->adapter,
+                            .flags = endpoint->flags };
+
+        uriLen = FormCanonicalPresenceUri(&ep, OC_RSRVD_PRESENCE_URI, presenceUri);
+
+        cbNode = GetClientCB(NULL, 0, NULL, presenceUri);
+        if (cbNode)
+        {
+            multicastPresenceSubscribe = 1;
+        }
+    }
+
+    if (!presenceSubscribe && !multicastPresenceSubscribe)
+    {
+        OIC_LOG(ERROR, TAG, "Received a presence notification, but no callback, ignoring");
+        goto exit;
+    }
+
+    response.payload = NULL;
+    response.result = OC_STACK_OK;
+
+    CopyEndpointToDevAddr(endpoint, &response.devAddr);
+    FixUpClientResponse(&response);
+
+    if (responseInfo->info.payload)
+    {
+        result = OCParsePayload(&response.payload,
+                PAYLOAD_TYPE_PRESENCE,
+                responseInfo->info.payload,
+                responseInfo->info.payloadSize);
+
+        if(result != OC_STACK_OK)
+        {
+            OIC_LOG(ERROR, TAG, "Presence parse failed");
+            goto exit;
+        }
+        if(!response.payload || response.payload->type != PAYLOAD_TYPE_PRESENCE)
+        {
+            OIC_LOG(ERROR, TAG, "Presence payload was wrong type");
+            result = OC_STACK_ERROR;
+            goto exit;
+        }
+        response.sequenceNumber = ((OCPresencePayload*)response.payload)->sequenceNumber;
+        resourceTypeName = ((OCPresencePayload*)response.payload)->resourceType;
+        maxAge = ((OCPresencePayload*)response.payload)->maxAge;
+    }
+
+    if (presenceSubscribe)
+    {
+        if(cbNode->sequenceNumber == response.sequenceNumber)
+        {
+            OIC_LOG(INFO, TAG, "No presence change");
+            ResetPresenceTTL(cbNode, maxAge);
+            OIC_LOG_V(INFO, TAG, "ResetPresenceTTL - TTLlevel:%d\n", cbNode->presence->TTLlevel);
+            goto exit;
+        }
+
+        if(maxAge == 0)
+        {
+            OIC_LOG(INFO, TAG, "Stopping presence");
+            response.result = OC_STACK_PRESENCE_STOPPED;
+            if(cbNode->presence)
+            {
+                OICFree(cbNode->presence->timeOut);
+                OICFree(cbNode->presence);
+                cbNode->presence = NULL;
+            }
+        }
+        else
+        {
+            if(!cbNode->presence)
+            {
+                cbNode->presence = (OCPresence *)OICMalloc(sizeof (OCPresence));
+
+                if(!(cbNode->presence))
+                {
+                    OIC_LOG(ERROR, TAG, "Could not allocate memory for cbNode->presence");
+                    result = OC_STACK_NO_MEMORY;
+                    goto exit;
+                }
+
+                VERIFY_NON_NULL_V(cbNode->presence);
+                cbNode->presence->timeOut = NULL;
+                cbNode->presence->timeOut = (uint32_t *)
+                        OICMalloc(PresenceTimeOutSize * sizeof(uint32_t));
+                if(!(cbNode->presence->timeOut)){
+                    OIC_LOG(ERROR, TAG,
+                                  "Could not allocate memory for cbNode->presence->timeOut");
+                    OICFree(cbNode->presence);
+                    result = OC_STACK_NO_MEMORY;
+                    goto exit;
+                }
+            }
+
+            ResetPresenceTTL(cbNode, maxAge);
+
+            cbNode->sequenceNumber = response.sequenceNumber;
+
+            // Ensure that a filter is actually applied.
+            if( resourceTypeName && cbNode->filterResourceType)
+            {
+                if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
+                {
+                    goto exit;
+                }
+            }
+        }
+    }
+    else
+    {
+        // This is the multicast case
+        OCMulticastNode* mcNode = NULL;
+        mcNode = GetMCPresenceNode(presenceUri);
+
+        if(mcNode != NULL)
+        {
+            if(mcNode->nonce == response.sequenceNumber)
+            {
+                OIC_LOG(INFO, TAG, "No presence change (Multicast)");
+                goto exit;
+            }
+            mcNode->nonce = response.sequenceNumber;
+
+            if(maxAge == 0)
+            {
+                OIC_LOG(INFO, TAG, "Stopping presence");
+                response.result = OC_STACK_PRESENCE_STOPPED;
+            }
+        }
+        else
+        {
+            char* uri = OICStrdup(presenceUri);
+            if (!uri)
+            {
+                OIC_LOG(INFO, TAG,
+                    "No Memory for URI to store in the presence node");
+                result = OC_STACK_NO_MEMORY;
+                goto exit;
+            }
+
+            result = AddMCPresenceNode(&mcNode, uri, response.sequenceNumber);
+            if(result == OC_STACK_NO_MEMORY)
+            {
+                OIC_LOG(INFO, TAG,
+                    "No Memory for Multicast Presence Node");
+                OICFree(uri);
+                goto exit;
+            }
+            // presence node now owns uri
+        }
+
+        // Ensure that a filter is actually applied.
+        if(resourceTypeName && cbNode->filterResourceType)
+        {
+            if(!findResourceType(cbNode->filterResourceType, resourceTypeName))
+            {
+                goto exit;
+            }
+        }
+    }
+
+    cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &response);
+
+    if (cbResult == OC_STACK_DELETE_TRANSACTION)
+    {
+        FindAndDeleteClientCB(cbNode);
+    }
+
+exit:
+    OCPayloadDestroy(response.payload);
+    return result;
+}
+
+void OCHandleResponse(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
+{
+    OIC_LOG(DEBUG, TAG, "Enter OCHandleResponse");
+
+    if(responseInfo->info.resourceUri &&
+        strcmp(responseInfo->info.resourceUri, OC_RSRVD_PRESENCE_URI) == 0)
+    {
+        HandlePresenceResponse(endPoint, responseInfo);
+        return;
+    }
+
+    ClientCB *cbNode = GetClientCB(responseInfo->info.token,
+            responseInfo->info.tokenLength, NULL, NULL);
+
+    ResourceObserver * observer = GetObserverUsingToken (responseInfo->info.token,
+            responseInfo->info.tokenLength);
+
+    if(cbNode)
+    {
+        OIC_LOG(INFO, TAG, "There is a cbNode associated with the response token");
+        if(responseInfo->result == CA_EMPTY)
+        {
+            OIC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
+            // We do not have a case for the client to receive a RESET
+            if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
+            {
+                //This is the case of receiving an ACK on a request to a slow resource!
+                OIC_LOG(INFO, TAG, "This is a pure ACK");
+                //TODO: should we inform the client
+                //      app that at least the request was received at the server?
+            }
+        }
+        else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
+        {
+            OIC_LOG(INFO, TAG, "Receiving A Timeout for this token");
+            OIC_LOG(INFO, TAG, "Calling into application address space");
+
+            OCClientResponse response =
+                {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
+            CopyEndpointToDevAddr(endPoint, &response.devAddr);
+            FixUpClientResponse(&response);
+            response.resourceUri = responseInfo->info.resourceUri;
+            memcpy(response.identity.id, responseInfo->info.identity.id,
+                                                sizeof (response.identity.id));
+            response.identity.id_length = responseInfo->info.identity.id_length;
+
+            response.result = CAToOCStackResult(responseInfo->result);
+            cbNode->callBack(cbNode->context,
+                    cbNode->handle, &response);
+            FindAndDeleteClientCB(cbNode);
+        }
+        else
+        {
+            OIC_LOG(INFO, TAG, "This is a regular response, A client call back is found");
+            OIC_LOG(INFO, TAG, "Calling into application address space");
+
+            OCClientResponse response =
+                {.devAddr = {.adapter = OC_DEFAULT_ADAPTER}};
+            response.sequenceNumber = OC_OBSERVE_NO_OPTION;
+            CopyEndpointToDevAddr(endPoint, &response.devAddr);
+            FixUpClientResponse(&response);
+            response.resourceUri = responseInfo->info.resourceUri;
+            memcpy(response.identity.id, responseInfo->info.identity.id,
+                                                sizeof (response.identity.id));
+            response.identity.id_length = responseInfo->info.identity.id_length;
+
+            response.result = CAToOCStackResult(responseInfo->result);
+
+            if(responseInfo->info.payload &&
+               responseInfo->info.payloadSize)
+            {
+                OCPayloadType type = PAYLOAD_TYPE_INVALID;
+                // check the security resource
+                if (SRMIsSecurityResourceURI(cbNode->requestUri))
+                {
+                    type = PAYLOAD_TYPE_SECURITY;
+                }
+                else if (cbNode->method == OC_REST_DISCOVER)
+                {
+                    if (strncmp(OC_RSRVD_WELL_KNOWN_URI,cbNode->requestUri,
+                                sizeof(OC_RSRVD_WELL_KNOWN_URI) - 1) == 0)
+                    {
+                        type = PAYLOAD_TYPE_DISCOVERY;
+                    }
+                    else if (strcmp(cbNode->requestUri, OC_RSRVD_DEVICE_URI) == 0)
+                    {
+                        type = PAYLOAD_TYPE_DEVICE;
+                    }
+                    else if (strcmp(cbNode->requestUri, OC_RSRVD_PLATFORM_URI) == 0)
+                    {
+                        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;
+                    }
+#ifdef TCP_ADAPTER
+                    else if (strcmp(cbNode->requestUri, KEEPALIVE_RESOURCE_URI) == 0)
+                    {
+                        type = PAYLOAD_TYPE_REPRESENTATION;
+                    }
+#endif
+                    else
+                    {
+                        OIC_LOG_V(ERROR, TAG, "Unknown Payload type in Discovery: %d %s",
+                                cbNode->method, cbNode->requestUri);
+                        return;
+                    }
+                }
+                else if (cbNode->method == OC_REST_GET ||
+                         cbNode->method == OC_REST_PUT ||
+                         cbNode->method == OC_REST_POST ||
+                         cbNode->method == OC_REST_OBSERVE ||
+                         cbNode->method == OC_REST_OBSERVE_ALL ||
+                         cbNode->method == OC_REST_DELETE)
+                {
+                    char targetUri[MAX_URI_LENGTH];
+                    snprintf(targetUri, MAX_URI_LENGTH, "%s?rt=%s", OC_RSRVD_RD_URI,
+                            OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
+                    if (strcmp(targetUri, cbNode->requestUri) == 0)
+                    {
+                        type = PAYLOAD_TYPE_RD;
+                    }
+                    else if (strcmp(OC_RSRVD_PLATFORM_URI, cbNode->requestUri) == 0)
+                    {
+                        type = PAYLOAD_TYPE_PLATFORM;
+                    }
+                    else if (strcmp(OC_RSRVD_DEVICE_URI, cbNode->requestUri) == 0)
+                    {
+                        type = PAYLOAD_TYPE_DEVICE;
+                    }
+                    if (type == PAYLOAD_TYPE_INVALID)
+                    {
+                        OIC_LOG_V(INFO, TAG, "Assuming PAYLOAD_TYPE_REPRESENTATION: %d %s",
+                                cbNode->method, cbNode->requestUri);
+                        type = PAYLOAD_TYPE_REPRESENTATION;
+                    }
+                }
+                else
+                {
+                    OIC_LOG_V(ERROR, TAG, "Unknown Payload type: %d %s",
+                            cbNode->method, cbNode->requestUri);
+                    return;
+                }
+
+                if(OC_STACK_OK != OCParsePayload(&response.payload,
+                            type,
+                            responseInfo->info.payload,
+                            responseInfo->info.payloadSize))
+                {
+                    OIC_LOG(ERROR, TAG, "Error converting payload");
+                    OCPayloadDestroy(response.payload);
+                    return;
+                }
+            }
+
+            response.numRcvdVendorSpecificHeaderOptions = 0;
+            if(responseInfo->info.numOptions > 0)
+            {
+                int start = 0;
+                //First option always with option ID is COAP_OPTION_OBSERVE if it is available.
+                if(responseInfo->info.options[0].optionID == COAP_OPTION_OBSERVE)
+                {
+                    size_t i;
+                    uint32_t observationOption;
+                    uint8_t* optionData = (uint8_t*)responseInfo->info.options[0].optionData;
+                    for (observationOption=0, i=0;
+                            i<sizeof(uint32_t) && i<responseInfo->info.options[0].optionLength;
+                            i++)
+                    {
+                        observationOption =
+                            (observationOption << 8) | optionData[i];
+                    }
+                    response.sequenceNumber = observationOption;
+
+                    response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions - 1;
+                    start = 1;
+                }
+                else
+                {
+                    response.numRcvdVendorSpecificHeaderOptions = responseInfo->info.numOptions;
+                }
+
+                if(response.numRcvdVendorSpecificHeaderOptions > MAX_HEADER_OPTIONS)
+                {
+                    OIC_LOG(ERROR, TAG, "#header options are more than MAX_HEADER_OPTIONS");
+                    OCPayloadDestroy(response.payload);
+                    return;
+                }
+
+                for (uint8_t i = start; i < responseInfo->info.numOptions; i++)
+                {
+                    memcpy (&(response.rcvdVendorSpecificHeaderOptions[i-start]),
+                            &(responseInfo->info.options[i]), sizeof(OCHeaderOption));
+                }
+            }
+
+            if (cbNode->method == OC_REST_OBSERVE &&
+                response.sequenceNumber > OC_OFFSET_SEQUENCE_NUMBER &&
+                response.sequenceNumber <= cbNode->sequenceNumber)
+            {
+                OIC_LOG_V(INFO, TAG, "Received stale notification. Number :%d",
+                                                 response.sequenceNumber);
+            }
+            else
+            {
+                OCStackApplicationResult appFeedback = cbNode->callBack(cbNode->context,
+                                                                        cbNode->handle,
+                                                                        &response);
+                cbNode->sequenceNumber = response.sequenceNumber;
+
+                if (appFeedback == OC_STACK_DELETE_TRANSACTION)
+                {
+                    FindAndDeleteClientCB(cbNode);
+                }
+                else
+                {
+                    // To keep discovery callbacks active.
+                    cbNode->TTL = GetTicks(MAX_CB_TIMEOUT_SECONDS *
+                                            MILLISECONDS_PER_SECOND);
+                }
+            }
+
+            //Need to send ACK when the response is CON
+            if(responseInfo->info.type == CA_MSG_CONFIRM)
+            {
+                SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
+                        CA_MSG_ACKNOWLEDGE, 0, NULL, NULL, 0, NULL);
+            }
+
+            OCPayloadDestroy(response.payload);
+        }
+        return;
+    }
+
+    if(observer)
+    {
+        OIC_LOG(INFO, TAG, "There is an observer associated with the response token");
+        if(responseInfo->result == CA_EMPTY)
+        {
+            OIC_LOG(INFO, TAG, "Receiving A ACK/RESET for this token");
+            if(responseInfo->info.type == CA_MSG_RESET)
+            {
+                OIC_LOG(INFO, TAG, "This is a RESET");
+                OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
+                        OC_OBSERVER_NOT_INTERESTED);
+            }
+            else if(responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
+            {
+                OIC_LOG(INFO, TAG, "This is a pure ACK");
+                OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
+                        OC_OBSERVER_STILL_INTERESTED);
+            }
+        }
+        else if(responseInfo->result == CA_RETRANSMIT_TIMEOUT)
+        {
+            OIC_LOG(INFO, TAG, "Receiving Time Out for an observer");
+            OCStackFeedBack(responseInfo->info.token, responseInfo->info.tokenLength,
+                    OC_OBSERVER_FAILED_COMM);
+        }
+        return;
+    }
+
+    if(!cbNode && !observer)
+    {
+        if(myStackMode == OC_CLIENT || myStackMode == OC_CLIENT_SERVER
+           || myStackMode == OC_GATEWAY)
+        {
+            OIC_LOG(INFO, TAG, "This is a client, but no cbNode was found for token");
+            if(responseInfo->result == CA_EMPTY)
+            {
+                OIC_LOG(INFO, TAG, "Receiving CA_EMPTY in the ocstack");
+            }
+            else
+            {
+                OIC_LOG(INFO, TAG, "Received a message without callbacks. Sending RESET");
+                SendDirectStackResponse(endPoint, responseInfo->info.messageId, CA_EMPTY,
+                        CA_MSG_RESET, 0, NULL, NULL, 0, NULL);
+            }
+        }
+
+        if(myStackMode == OC_SERVER || myStackMode == OC_CLIENT_SERVER
+           || myStackMode == OC_GATEWAY)
+        {
+            OIC_LOG(INFO, TAG, "This is a server, but no observer was found for token");
+            if (responseInfo->info.type == CA_MSG_ACKNOWLEDGE)
+            {
+                OIC_LOG_V(INFO, TAG, "Received ACK at server for messageId : %d",
+                                            responseInfo->info.messageId);
+            }
+            if (responseInfo->info.type == CA_MSG_RESET)
+            {
+                OIC_LOG_V(INFO, TAG, "Received RESET at server for messageId : %d",
+                                            responseInfo->info.messageId);
+            }
+        }
+
+        return;
+    }
+
+    OIC_LOG(INFO, TAG, "Exit HandleCAResponses");
+}
+
+void HandleCAResponses(const CAEndpoint_t* endPoint, const CAResponseInfo_t* responseInfo)
+{
+    VERIFY_NON_NULL_NR(endPoint, FATAL);
+    VERIFY_NON_NULL_NR(responseInfo, FATAL);
+
+    OIC_LOG(INFO, TAG, "Enter HandleCAResponses");
+
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+#ifdef ROUTING_GATEWAY
+    bool needRIHandling = false;
+    /*
+     * Routing manager is going to update either of endpoint or response or both.
+     * This typecasting is done to avoid unnecessary duplication of Endpoint and responseInfo
+     * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
+     * destination.
+     */
+    OCStackResult ret = RMHandleResponse((CAResponseInfo_t *)responseInfo, (CAEndpoint_t *)endPoint,
+                                         &needRIHandling);
+    if(ret != OC_STACK_OK || !needRIHandling)
+    {
+        OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
+        return;
+    }
+#endif
+
+    /*
+     * Put source in sender endpoint so that the next packet from application can be routed to
+     * proper destination and remove "RM" coap header option before passing request / response to
+     * RI as this option will make no sense to either RI or application.
+     */
+    RMUpdateInfo((CAHeaderOption_t **) &(responseInfo->info.options),
+                 (uint8_t *) &(responseInfo->info.numOptions),
+                 (CAEndpoint_t *) endPoint);
+#endif
+
+    OCHandleResponse(endPoint, responseInfo);
+
+    OIC_LOG(INFO, TAG, "Exit HandleCAResponses");
+}
+
+/*
+ * This function handles error response from CA
+ * code shall be added to handle the errors
+ */
+void HandleCAErrorResponse(const CAEndpoint_t *endPoint, const CAErrorInfo_t *errrorInfo)
+{
+    OIC_LOG(INFO, TAG, "Enter HandleCAErrorResponse");
+
+    if(NULL == endPoint)
+    {
+        OIC_LOG(ERROR, TAG, "endPoint is NULL");
+        return;
+    }
+
+    if(NULL == errrorInfo)
+    {
+        OIC_LOG(ERROR, TAG, "errrorInfo is NULL");
+        return;
+    }
+    OIC_LOG(INFO, TAG, "Exit HandleCAErrorResponse");
+}
+
+/*
+ * This function sends out Direct Stack Responses. These are responses that are not coming
+ * from the application entity handler. These responses have no payload and are usually ACKs,
+ * RESETs or some error conditions that were caught by the stack.
+ */
+OCStackResult SendDirectStackResponse(const CAEndpoint_t* endPoint, const uint16_t coapID,
+        const CAResponseResult_t responseResult, const CAMessageType_t type,
+        const uint8_t numOptions, const CAHeaderOption_t *options,
+        CAToken_t token, uint8_t tokenLength, const char *resourceUri)
+{
+    OIC_LOG(DEBUG, TAG, "Entering SendDirectStackResponse");
+    CAResponseInfo_t respInfo = {
+        .result = responseResult
+    };
+    respInfo.info.messageId = coapID;
+    respInfo.info.numOptions = numOptions;
+
+    if (respInfo.info.numOptions)
+    {
+        respInfo.info.options =
+            (CAHeaderOption_t *)OICCalloc(respInfo.info.numOptions, sizeof(CAHeaderOption_t));
+        memcpy (respInfo.info.options, options,
+                sizeof(CAHeaderOption_t) * respInfo.info.numOptions);
+
+    }
+
+    respInfo.info.payload = NULL;
+    respInfo.info.token = token;
+    respInfo.info.tokenLength = tokenLength;
+    respInfo.info.type = type;
+    respInfo.info.resourceUri = OICStrdup (resourceUri);
+    respInfo.info.acceptFormat = CA_FORMAT_UNDEFINED;
+
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+    // Add the destination to route option from the endpoint->routeData.
+    bool doPost = false;
+    OCStackResult result = RMAddInfo(endPoint->routeData, &respInfo, false, &doPost);
+    if(OC_STACK_OK != result)
+    {
+        OIC_LOG_V(ERROR, TAG, "Add routing option failed [%d]", result);
+        return result;
+    }
+    if (doPost)
+    {
+        OIC_LOG(DEBUG, TAG, "Sending a POST message for EMPTY ACK in Client Mode");
+        CARequestInfo_t reqInfo = {.method = CA_POST };
+        /* The following initialization is not done in a single initializer block as in
+         * arduino, .c file is compiled as .cpp and moves it from C99 to C++11.  The latter
+         * does not have designated initalizers. This is a work-around for now.
+         */
+        reqInfo.info.type = CA_MSG_NONCONFIRM;
+        reqInfo.info.messageId = coapID;
+        reqInfo.info.tokenLength = tokenLength;
+        reqInfo.info.token = token;
+        reqInfo.info.numOptions = respInfo.info.numOptions;
+        reqInfo.info.payload = NULL;
+        reqInfo.info.resourceUri = OICStrdup (OC_RSRVD_GATEWAY_URI);
+        if (reqInfo.info.numOptions)
+        {
+            reqInfo.info.options =
+                (CAHeaderOption_t *)OICCalloc(reqInfo.info.numOptions, sizeof(CAHeaderOption_t));
+            if (NULL == reqInfo.info.options)
+            {
+                OIC_LOG(ERROR, TAG, "Calloc failed");
+                return OC_STACK_NO_MEMORY;
+            }
+            memcpy (reqInfo.info.options, respInfo.info.options,
+                    sizeof(CAHeaderOption_t) * reqInfo.info.numOptions);
+
+        }
+        CAResult_t caResult = CASendRequest(endPoint, &reqInfo);
+        OICFree (reqInfo.info.resourceUri);
+        OICFree (reqInfo.info.options);
+        OICFree (respInfo.info.resourceUri);
+        OICFree (respInfo.info.options);
+        if (CA_STATUS_OK != caResult)
+        {
+            OIC_LOG(ERROR, TAG, "CASendRequest error");
+            return CAResultToOCResult(caResult);
+        }
+    }
+    else
+#endif
+    {
+        CAResult_t caResult = CASendResponse(endPoint, &respInfo);
+
+        // resourceUri in the info field is cloned in the CA layer and
+        // thus ownership is still here.
+        OICFree (respInfo.info.resourceUri);
+        OICFree (respInfo.info.options);
+        if(CA_STATUS_OK != caResult)
+        {
+            OIC_LOG(ERROR, TAG, "CASendResponse error");
+            return CAResultToOCResult(caResult);
+        }
+    }
+    OIC_LOG(DEBUG, TAG, "Exit SendDirectStackResponse");
+    return OC_STACK_OK;
+}
+
+OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
+{
+    OIC_LOG(INFO, TAG, "Entering HandleStackRequests (OCStack Layer)");
+    OCStackResult result = OC_STACK_ERROR;
+    if(!protocolRequest)
+    {
+        OIC_LOG(ERROR, TAG, "protocolRequest is NULL");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    OCServerRequest * request = GetServerRequestUsingToken(protocolRequest->requestToken,
+            protocolRequest->tokenLength);
+    if(!request)
+    {
+        OIC_LOG(INFO, TAG, "This is a new Server Request");
+        result = AddServerRequest(&request, protocolRequest->coapID,
+                protocolRequest->delayedResNeeded, 0, protocolRequest->method,
+                protocolRequest->numRcvdVendorSpecificHeaderOptions,
+                protocolRequest->observationOption, protocolRequest->qos,
+                protocolRequest->query, protocolRequest->rcvdVendorSpecificHeaderOptions,
+                protocolRequest->payload, protocolRequest->requestToken,
+                protocolRequest->tokenLength, protocolRequest->resourceUrl,
+                protocolRequest->reqTotalSize, protocolRequest->acceptFormat,
+                &protocolRequest->devAddr);
+        if (OC_STACK_OK != result)
+        {
+            OIC_LOG(ERROR, TAG, "Error adding server request");
+            return result;
+        }
+
+        if(!request)
+        {
+            OIC_LOG(ERROR, TAG, "Out of Memory");
+            return OC_STACK_NO_MEMORY;
+        }
+
+        if(!protocolRequest->reqMorePacket)
+        {
+            request->requestComplete = 1;
+        }
+    }
+    else
+    {
+        OIC_LOG(INFO, TAG, "This is either a repeated or blocked Server Request");
     }
 
     if(request->requestComplete)
     {
-        OC_LOG(INFO, TAG, PCF("This Server Request is complete"));
+        OIC_LOG(INFO, TAG, "This Server Request is complete");
+        ResourceHandling resHandling = OC_RESOURCE_VIRTUAL;
+        OCResource *resource = NULL;
         result = DetermineResourceHandling (request, &resHandling, &resource);
         if (result == OC_STACK_OK)
         {
             result = ProcessRequest(resHandling, resource, request);
         }
-        else
-        {
-            result = OC_STACK_ERROR;
-        }
     }
     else
     {
-        OC_LOG(INFO, TAG, PCF("This Server Request is incomplete"));
+        OIC_LOG(INFO, TAG, "This Server Request is incomplete");
         result = OC_STACK_CONTINUE;
     }
     return result;
 }
 
-//This function will be called back by occoap layer when a response is received
-void HandleStackResponses(OCResponse * response)
+void OCHandleRequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
 {
-    OCStackApplicationResult result = OC_STACK_DELETE_TRANSACTION;
-    OC_LOG(INFO, TAG, PCF("Entering HandleStackResponses (OCStack Layer)"));
+    OIC_LOG(DEBUG, TAG, "Enter OCHandleRequests");
 
-    if (response->cbNode)
+#ifdef TCP_ADAPTER
+    if (requestInfo->info.resourceUri &&
+            strcmp(requestInfo->info.resourceUri, KEEPALIVE_RESOURCE_URI) == 0)
     {
-        OC_LOG(INFO, TAG, PCF("Calling into application address space"));
-        result = response->cbNode->callBack(response->cbNode->context,
-                response->cbNode->handle, response->clientResponse);
-        if (result == OC_STACK_DELETE_TRANSACTION ||
-                response->clientResponse->result == OC_STACK_COMM_ERROR ||
-                response->clientResponse->result == OC_STACK_RESOURCE_DELETED)
-        {
-            FindAndDeleteClientCB(response->cbNode);
-        }
+        HandleKeepAliveRequest(endPoint, requestInfo);
+        return;
     }
-}
+#endif
 
-int ParseIPv4Address(unsigned char * ipAddrStr, uint8_t * ipAddr, uint16_t * port)
-{
-    size_t index = 0;
-    unsigned char *itr, *coap;
-    uint8_t dotCount = 0;
+    OCStackResult requestResult = OC_STACK_ERROR;
 
-    ipAddr[index] = 0;
-    *port = 0;
-    /* search for scheme */
-    itr = ipAddrStr;
-    if (!isdigit((unsigned char) *ipAddrStr))
+    if(myStackMode == OC_CLIENT)
     {
-        coap = (unsigned char *) OC_COAP_SCHEME;
-        while (*coap && tolower(*itr) == *coap)
-        {
-            coap++;
-            itr++;
-        }
+        //TODO: should the client be responding to requests?
+        return;
     }
-    ipAddrStr = itr;
 
-    while (*ipAddrStr) {
-        if (isdigit((unsigned char) *ipAddrStr))
-        {
-            ipAddr[index] *= 10;
-            ipAddr[index] += *ipAddrStr - '0';
-        }
-        else if ((unsigned char) *ipAddrStr == '.')
+    OCServerProtocolRequest serverRequest = {0};
+
+    OIC_LOG_V(INFO, TAG, "Endpoint URI : %s", requestInfo->info.resourceUri);
+
+    char * uriWithoutQuery = NULL;
+    char * query  = NULL;
+
+    requestResult = getQueryFromUri(requestInfo->info.resourceUri, &query, &uriWithoutQuery);
+
+    if (requestResult != OC_STACK_OK || !uriWithoutQuery)
+    {
+        OIC_LOG_V(ERROR, TAG, "getQueryFromUri() failed with OC error code %d\n", requestResult);
+        return;
+    }
+    OIC_LOG_V(INFO, TAG, "URI without query: %s", uriWithoutQuery);
+    OIC_LOG_V(INFO, TAG, "Query : %s", query);
+
+    if(strlen(uriWithoutQuery) < MAX_URI_LENGTH)
+    {
+        OICStrcpy(serverRequest.resourceUrl, sizeof(serverRequest.resourceUrl), uriWithoutQuery);
+        OICFree(uriWithoutQuery);
+    }
+    else
+    {
+        OIC_LOG(ERROR, TAG, "URI length exceeds MAX_URI_LENGTH.");
+        OICFree(uriWithoutQuery);
+        OICFree(query);
+        return;
+    }
+
+    if(query)
+    {
+        if(strlen(query) < MAX_QUERY_LENGTH)
         {
-            index++;
-            dotCount++;
-            ipAddr[index] = 0;
+            OICStrcpy(serverRequest.query, sizeof(serverRequest.query), query);
+            OICFree(query);
         }
         else
         {
+            OIC_LOG(ERROR, TAG, "Query length exceeds MAX_QUERY_LENGTH.");
+            OICFree(query);
+            return;
+        }
+    }
+
+    if ((requestInfo->info.payload) && (0 < requestInfo->info.payloadSize))
+    {
+        serverRequest.reqTotalSize = requestInfo->info.payloadSize;
+        serverRequest.payload = (uint8_t *) OICMalloc(requestInfo->info.payloadSize);
+        memcpy (serverRequest.payload, requestInfo->info.payload,
+                requestInfo->info.payloadSize);
+    }
+    else
+    {
+        serverRequest.reqTotalSize = 0;
+    }
+
+    switch (requestInfo->method)
+    {
+        case CA_GET:
+            serverRequest.method = OC_REST_GET;
+            break;
+        case CA_PUT:
+            serverRequest.method = OC_REST_PUT;
             break;
+        case CA_POST:
+            serverRequest.method = OC_REST_POST;
+            break;
+        case CA_DELETE:
+            serverRequest.method = OC_REST_DELETE;
+            break;
+        default:
+            OIC_LOG_V(ERROR, TAG, "Received CA method %d not supported", requestInfo->method);
+            SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_REQ,
+                        requestInfo->info.type, requestInfo->info.numOptions,
+                        requestInfo->info.options, requestInfo->info.token,
+                        requestInfo->info.tokenLength, requestInfo->info.resourceUri);
+            OICFree(serverRequest.payload);
+            return;
+    }
+
+    OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)requestInfo->info.token,
+            requestInfo->info.tokenLength);
+
+    serverRequest.tokenLength = requestInfo->info.tokenLength;
+    if (serverRequest.tokenLength) {
+        // Non empty token
+        serverRequest.requestToken = (CAToken_t)OICMalloc(requestInfo->info.tokenLength);
+
+        if (!serverRequest.requestToken)
+        {
+            OIC_LOG(FATAL, TAG, "Allocation for token failed.");
+            SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_INTERNAL_SERVER_ERROR,
+                    requestInfo->info.type, requestInfo->info.numOptions,
+                    requestInfo->info.options, requestInfo->info.token,
+                    requestInfo->info.tokenLength, requestInfo->info.resourceUri);
+            OICFree(serverRequest.payload);
+            return;
         }
-        ipAddrStr++;
+        memcpy(serverRequest.requestToken, requestInfo->info.token, requestInfo->info.tokenLength);
     }
-    if(*ipAddrStr == ':')
+
+    switch (requestInfo->info.acceptFormat)
     {
-        ipAddrStr++;
-        while (*ipAddrStr){
-            if (isdigit((unsigned char) *ipAddrStr))
-            {
-                *port *= 10;
-                *port += *ipAddrStr - '0';
-            }
-            else
-            {
-                break;
-            }
-            ipAddrStr++;
+        case CA_FORMAT_APPLICATION_CBOR:
+            serverRequest.acceptFormat = OC_FORMAT_CBOR;
+            break;
+        case CA_FORMAT_UNDEFINED:
+            serverRequest.acceptFormat = OC_FORMAT_UNDEFINED;
+            break;
+        default:
+            serverRequest.acceptFormat = OC_FORMAT_UNSUPPORTED;
+    }
+
+    if (requestInfo->info.type == CA_MSG_CONFIRM)
+    {
+        serverRequest.qos = OC_HIGH_QOS;
+    }
+    else
+    {
+        serverRequest.qos = OC_LOW_QOS;
+    }
+    // CA does not need the following field
+    // Are we sure CA does not need them? how is it responding to multicast
+    serverRequest.delayedResNeeded = 0;
+
+    serverRequest.coapID = requestInfo->info.messageId;
+
+    CopyEndpointToDevAddr(endPoint, &serverRequest.devAddr);
+
+    // copy vendor specific header options
+    uint8_t tempNum = (requestInfo->info.numOptions);
+
+    // Assume no observation requested and it is a pure GET.
+    // If obs registration/de-registration requested it'll be fetched from the
+    // options in GetObserveHeaderOption()
+    serverRequest.observationOption = OC_OBSERVE_NO_OPTION;
+
+    GetObserveHeaderOption(&serverRequest.observationOption, requestInfo->info.options, &tempNum);
+    if (requestInfo->info.numOptions > MAX_HEADER_OPTIONS)
+    {
+        OIC_LOG(ERROR, TAG,
+                "The request info numOptions is greater than MAX_HEADER_OPTIONS");
+        SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_BAD_OPT,
+                requestInfo->info.type, requestInfo->info.numOptions,
+                requestInfo->info.options, requestInfo->info.token,
+                requestInfo->info.tokenLength, requestInfo->info.resourceUri);
+        OICFree(serverRequest.payload);
+        OICFree(serverRequest.requestToken);
+        return;
+    }
+    serverRequest.numRcvdVendorSpecificHeaderOptions = tempNum;
+    if (serverRequest.numRcvdVendorSpecificHeaderOptions)
+    {
+        memcpy (&(serverRequest.rcvdVendorSpecificHeaderOptions), requestInfo->info.options,
+            sizeof(CAHeaderOption_t)*tempNum);
+    }
+
+    requestResult = HandleStackRequests (&serverRequest);
+
+    // Send ACK to client as precursor to slow response
+    if (requestResult == OC_STACK_SLOW_RESOURCE)
+    {
+        if (requestInfo->info.type == CA_MSG_CONFIRM)
+        {
+            SendDirectStackResponse(endPoint, requestInfo->info.messageId, CA_EMPTY,
+                                    CA_MSG_ACKNOWLEDGE,0, NULL, NULL, 0, NULL);
         }
     }
+    else if(requestResult != OC_STACK_OK)
+    {
+        OIC_LOG_V(ERROR, TAG, "HandleStackRequests failed. error: %d", requestResult);
+
+        CAResponseResult_t stackResponse =
+            OCToCAStackResult(requestResult, serverRequest.method);
+
+        SendDirectStackResponse(endPoint, requestInfo->info.messageId, stackResponse,
+                requestInfo->info.type, requestInfo->info.numOptions,
+                requestInfo->info.options, requestInfo->info.token,
+                requestInfo->info.tokenLength, requestInfo->info.resourceUri);
+    }
+    // requestToken is fed to HandleStackRequests, which then goes to AddServerRequest.
+    // The token is copied in there, and is thus still owned by this function.
+    OICFree(serverRequest.payload);
+    OICFree(serverRequest.requestToken);
+    OIC_LOG(INFO, TAG, "Exit OCHandleRequests");
+}
+
+//This function will be called back by CA layer when a request is received
+void HandleCARequests(const CAEndpoint_t* endPoint, const CARequestInfo_t* requestInfo)
+{
+    OIC_LOG(INFO, TAG, "Enter HandleCARequests");
+    if(!endPoint)
+    {
+        OIC_LOG(ERROR, TAG, "endPoint is NULL");
+        return;
+    }
+
+    if(!requestInfo)
+    {
+        OIC_LOG(ERROR, TAG, "requestInfo is NULL");
+        return;
+    }
+
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+#ifdef ROUTING_GATEWAY
+    bool needRIHandling = false;
+    bool isEmptyMsg = false;
+    /*
+     * Routing manager is going to update either of endpoint or request or both.
+     * This typecasting is done to avoid unnecessary duplication of Endpoint and requestInfo
+     * RM can update "routeData" option in endPoint so that future RI requests can be sent to proper
+     * destination. It can also remove "RM" coap header option before passing request / response to
+     * RI as this option will make no sense to either RI or application.
+     */
+    OCStackResult ret = RMHandleRequest((CARequestInfo_t *)requestInfo, (CAEndpoint_t *)endPoint,
+                                        &needRIHandling, &isEmptyMsg);
+    if(OC_STACK_OK != ret || !needRIHandling)
+    {
+        OIC_LOG_V(INFO, TAG, "Routing status![%d]. Not forwarding to RI", ret);
+        return;
+    }
+#endif
 
+    /*
+     * Put source in sender endpoint so that the next packet from application can be routed to
+     * proper destination and remove RM header option.
+     */
+    RMUpdateInfo((CAHeaderOption_t **) &(requestInfo->info.options),
+                 (uint8_t *) &(requestInfo->info.numOptions),
+                 (CAEndpoint_t *) endPoint);
 
-    if (ipAddr[0] < 255 && ipAddr[1] < 255 && ipAddr[2] < 255 && ipAddr[3] < 255
-            && dotCount == 3)
+#ifdef ROUTING_GATEWAY
+    if (isEmptyMsg)
     {
-        return 1;
+        /*
+         * In Gateways, the MSGType in route option is used to check if the actual
+         * response is EMPTY message(4 bytes CoAP Header).  In case of Client, the
+         * EMPTY response is sent in the form of POST request which need to be changed
+         * to a EMPTY response by RM.  This translation is done in this part of the code.
+         */
+        OIC_LOG(INFO, TAG, "This is a Empty response from the Client");
+        CAResponseInfo_t respInfo = {.result = CA_EMPTY,
+                                     .info.messageId = requestInfo->info.messageId,
+                                     .info.type = CA_MSG_ACKNOWLEDGE};
+        OCHandleResponse(endPoint, &respInfo);
     }
     else
+#endif
+#endif
     {
-        return 0;
+        // Normal handling of the packet
+        OCHandleRequests(endPoint, requestInfo);
     }
+    OIC_LOG(INFO, TAG, "Exit HandleCARequests");
 }
 
-//-----------------------------------------------------------------------------
-// Private internal function prototypes
-//-----------------------------------------------------------------------------
+bool validatePlatformInfo(OCPlatformInfo info)
+{
 
-static OCDoHandle GenerateInvocationHandle();
-static OCStackResult initResources();
-static void insertResource(OCResource *resource);
-static OCResource *findResource(OCResource *resource);
-static void insertResourceType(OCResource *resource,
-        OCResourceType *resourceType);
-static OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle,
-        uint8_t index);
-static void insertResourceInterface(OCResource *resource,
-        OCResourceInterface *resourceInterface);
-static OCResourceInterface *findResourceInterfaceAtIndex(
-        OCResourceHandle handle, uint8_t index);
-static void deleteResourceType(OCResourceType *resourceType);
-static void deleteResourceInterface(OCResourceInterface *resourceInterface);
-static void deleteResourceElements(OCResource *resource);
-static int deleteResource(OCResource *resource);
-static void deleteAllResources();
-static void incrementSequenceNumber(OCResource * resPtr);
-static OCStackResult verifyUriQueryLength(const char * inputUri,
-        uint16_t uriLen);
-static uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size);
-OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI);
+    if (!info.platformID)
+    {
+        OIC_LOG(ERROR, TAG, "No platform ID found.");
+        return false;
+    }
+
+    if (info.manufacturerName)
+    {
+        size_t lenManufacturerName = strlen(info.manufacturerName);
+
+        if(lenManufacturerName == 0 || lenManufacturerName > MAX_MANUFACTURER_NAME_LENGTH)
+        {
+            OIC_LOG(ERROR, TAG, "Manufacturer name fails length requirements.");
+            return false;
+        }
+    }
+    else
+    {
+        OIC_LOG(ERROR, TAG, "No manufacturer name present");
+        return false;
+    }
+
+    if (info.manufacturerUrl)
+    {
+        if(strlen(info.manufacturerUrl) > MAX_MANUFACTURER_URL_LENGTH)
+        {
+            OIC_LOG(ERROR, TAG, "Manufacturer url fails length requirements.");
+            return false;
+        }
+    }
+    return true;
+}
 
 //-----------------------------------------------------------------------------
 // Public APIs
 //-----------------------------------------------------------------------------
+#ifdef RA_ADAPTER
+OCStackResult OCSetRAInfo(const OCRAInfo_t *raInfo)
+{
+    if (!raInfo           ||
+        !raInfo->username ||
+        !raInfo->hostname ||
+        !raInfo->xmpp_domain)
+    {
+
+        return OC_STACK_INVALID_PARAM;
+    }
+    OCStackResult result = CAResultToOCResult(CASetRAInfo((const CARAInfo_t *) raInfo));
+    gRASetInfo = (result == OC_STACK_OK)? true : false;
+
+    return result;
+}
+#endif
 
-/**
- * Initialize the OC Stack.  Must be called prior to starting the stack.
- *
- * @param ipAddr
- *     IP Address of host device
- * @param port
- *     Port of host device
- * @param mode
- *     Host device is client, server, or client-server
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack init error
- */
 OCStackResult OCInit(const char *ipAddr, uint16_t port, OCMode mode)
 {
-    OCStackResult result = OC_STACK_ERROR;
-    OC_LOG(INFO, TAG, PCF("Entering OCInit"));
+    (void) ipAddr;
+    (void) port;
+    return OCInit1(mode, OC_DEFAULT_FLAGS, OC_DEFAULT_FLAGS);
+}
 
-    if (ipAddr)
+OCStackResult OCInit1(OCMode mode, OCTransportFlags serverFlags, OCTransportFlags clientFlags)
+{
+    if(stackState == OC_STACK_INITIALIZED)
     {
-        OC_LOG_V(INFO, TAG, "IP Address = %s", ipAddr);
+        OIC_LOG(INFO, TAG, "Subsequent calls to OCInit() without calling \
+                OCStop() between them are ignored.");
+        return OC_STACK_OK;
     }
 
-    switch (mode)
+#ifndef ROUTING_GATEWAY
+    if (OC_GATEWAY == mode)
     {
-    case OC_CLIENT:
-        OC_LOG(INFO, TAG, PCF("Client mode"));
-        break;
-    case OC_SERVER:
-        OC_LOG(INFO, TAG, PCF("Server mode"));
-        break;
-    case OC_CLIENT_SERVER:
-        OC_LOG(INFO, TAG, PCF("Client-server mode"));
-        break;
-    default:
-        OC_LOG(ERROR, TAG, PCF("Invalid mode"));
+        OIC_LOG(ERROR, TAG, "Routing Manager not supported");
+        return OC_STACK_INVALID_PARAM;
+    }
+#endif
+
+#ifdef RA_ADAPTER
+    if(!gRASetInfo)
+    {
+        OIC_LOG(ERROR, TAG, "Need to call OCSetRAInfo before calling OCInit");
+        return OC_STACK_ERROR;
+    }
+#endif
+
+    OCStackResult result = OC_STACK_ERROR;
+    OIC_LOG(INFO, TAG, "Entering OCInit");
+
+    // Validate mode
+    if (!((mode == OC_CLIENT) || (mode == OC_SERVER) || (mode == OC_CLIENT_SERVER)
+        || (mode == OC_GATEWAY)))
+    {
+        OIC_LOG(ERROR, TAG, "Invalid mode");
         return OC_STACK_ERROR;
-        break;
     }
     myStackMode = mode;
 
+    if (mode == OC_CLIENT || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
+    {
+        caglobals.client = true;
+    }
+    if (mode == OC_SERVER || mode == OC_CLIENT_SERVER || mode == OC_GATEWAY)
+    {
+        caglobals.server = true;
+    }
+
+    caglobals.serverFlags = (CATransportFlags_t)serverFlags;
+    if (!(caglobals.serverFlags & CA_IPFAMILY_MASK))
+    {
+        caglobals.serverFlags = (CATransportFlags_t)(caglobals.serverFlags|CA_IPV4|CA_IPV6);
+    }
+    caglobals.clientFlags = (CATransportFlags_t)clientFlags;
+    if (!(caglobals.clientFlags & CA_IPFAMILY_MASK))
+    {
+        caglobals.clientFlags = (CATransportFlags_t)(caglobals.clientFlags|CA_IPV4|CA_IPV6);
+    }
+
     defaultDeviceHandler = NULL;
+    defaultDeviceHandlerCallbackParameter = NULL;
 
-#ifdef WITH_PRESENCE
-    PresenceTimeOutSize = sizeof(PresenceTimeOut)/sizeof(PresenceTimeOut[0]) - 1;
-#endif // WITH_PRESENCE
+    result = CAResultToOCResult(CAInitialize());
+    VERIFY_SUCCESS(result, OC_STACK_OK);
 
-    // Make call to OCCoAP layer
-    result = OCInitCoAP(ipAddr, (uint16_t) port, myStackMode);
-    if (result == OC_STACK_OK)
+    result = CAResultToOCResult(OCSelectNetwork());
+    VERIFY_SUCCESS(result, OC_STACK_OK);
+
+    switch (myStackMode)
     {
-        stackState = OC_STACK_INITIALIZED;
+        case OC_CLIENT:
+            CARegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
+            result = CAResultToOCResult(CAStartDiscoveryServer());
+            OIC_LOG(INFO, TAG, "Client mode: CAStartDiscoveryServer");
+            break;
+        case OC_SERVER:
+            SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
+            result = CAResultToOCResult(CAStartListeningServer());
+            OIC_LOG(INFO, TAG, "Server mode: CAStartListeningServer");
+            break;
+        case OC_CLIENT_SERVER:
+        case OC_GATEWAY:
+            SRMRegisterHandler(HandleCARequests, HandleCAResponses, HandleCAErrorResponse);
+            result = CAResultToOCResult(CAStartListeningServer());
+            if(result == OC_STACK_OK)
+            {
+                result = CAResultToOCResult(CAStartDiscoveryServer());
+            }
+            break;
     }
+    VERIFY_SUCCESS(result, OC_STACK_OK);
+
+#ifdef TCP_ADAPTER
+    CARegisterKeepAliveHandler(HandleKeepAliveConnCB, HandleKeepAliveDisconnCB);
+#endif
+
+#ifdef WITH_PRESENCE
+    PresenceTimeOutSize = sizeof (PresenceTimeOut) / sizeof (PresenceTimeOut[0]) - 1;
+#endif // WITH_PRESENCE
+
+    //Update Stack state to initialized
+    stackState = OC_STACK_INITIALIZED;
+
     // Initialize resource
-    if(result == OC_STACK_OK && myStackMode != OC_CLIENT)
+    if(myStackMode != OC_CLIENT)
     {
         result = initResources();
     }
+
+    // Initialize the SRM Policy Engine
+    if(result == OC_STACK_OK)
+    {
+        result = SRMInitPolicyEngine();
+        // TODO after BeachHead delivery: consolidate into single SRMInit()
+    }
+#if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
+    RMSetStackMode(mode);
+#ifdef ROUTING_GATEWAY
+    if (OC_GATEWAY == myStackMode)
+    {
+        result = RMInitialize();
+    }
+#endif
+#endif
+
+#ifdef TCP_ADAPTER
+    if(result == OC_STACK_OK)
+    {
+        result = InitializeKeepAlive(myStackMode);
+    }
+#endif
+
+exit:
     if(result != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, PCF("Stack initialization error"));
+        OIC_LOG(ERROR, TAG, "Stack initialization error");
+        deleteAllResources();
+        CATerminate();
+        stackState = OC_STACK_UNINITIALIZED;
     }
     return result;
 }
 
-/**
- * Stop the OC stack.  Use for a controlled shutdown.
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack not initialized
- */
 OCStackResult OCStop()
 {
-    OCStackResult result = OC_STACK_ERROR;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCStop"));
+    OIC_LOG(INFO, TAG, "Entering OCStop");
 
     if (stackState == OC_STACK_UNINIT_IN_PROGRESS)
     {
-        OC_LOG(DEBUG, TAG, PCF("Stack already stopping, exiting"));
+        OIC_LOG(DEBUG, TAG, "Stack already stopping, exiting");
         return OC_STACK_OK;
     }
     else if (stackState != OC_STACK_INITIALIZED)
     {
-        OC_LOG(ERROR, TAG, PCF("Stack not initialized"));
+        OIC_LOG(ERROR, TAG, "Stack not initialized");
         return OC_STACK_ERROR;
     }
 
     stackState = OC_STACK_UNINIT_IN_PROGRESS;
 
-    #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
     // Ensure that the TTL associated with ANY and ALL presence notifications originating from
     // here send with the code "OC_STACK_PRESENCE_STOPPED" result.
     presenceResource.presenceTTL = 0;
-    #endif // WITH_PRESENCE
+#endif // WITH_PRESENCE
+
+#ifdef ROUTING_GATEWAY
+    if (OC_GATEWAY == myStackMode)
+    {
+        RMTerminate();
+    }
+#endif
+
+#ifdef TCP_ADAPTER
+    TerminateKeepAlive(myStackMode);
+#endif
 
     // Free memory dynamically allocated for resources
     deleteAllResources();
     DeleteDeviceInfo();
+    DeletePlatformInfo();
+    CATerminate();
+    // Remove all observers
+    DeleteObserverList();
+    // Remove all the client callbacks
+    DeleteClientCBList();
+
+    // De-init the SRM Policy Engine
+    // TODO after BeachHead delivery: consolidate into single SRMDeInit()
+    SRMDeInitPolicyEngine();
+
+
+    stackState = OC_STACK_UNINITIALIZED;
+    return OC_STACK_OK;
+}
 
-    // Make call to OCCoAP layer
-    if (OCStopCoAP() == OC_STACK_OK)
+OCStackResult OCStartMulticastServer()
+{
+    if(stackState != OC_STACK_INITIALIZED)
     {
-        // Remove all observers
-        DeleteObserverList();
-        // Remove all the client callbacks
-        DeleteClientCBList();
-        stackState = OC_STACK_UNINITIALIZED;
-        result = OC_STACK_OK;
-    } else {
-        stackState = OC_STACK_INITIALIZED;
-        result = OC_STACK_ERROR;
+        OIC_LOG(ERROR, TAG, "OCStack is not initalized. Cannot start multicast server.");
+        return OC_STACK_ERROR;
     }
+    CAResult_t ret = CAStartListeningServer();
+    if (CA_STATUS_OK != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "Failed starting listening server: %d", ret);
+        return OC_STACK_ERROR;
+    }
+    return OC_STACK_OK;
+}
 
-    // Deinit security blob
-    DeinitOCSecurityInfo();
-
-    if (result != OC_STACK_OK) {
-        OC_LOG(ERROR, TAG, PCF("Stack stop error"));
+OCStackResult OCStopMulticastServer()
+{
+    CAResult_t ret = CAStopListeningServer();
+    if (CA_STATUS_OK != ret)
+    {
+        OIC_LOG_V(ERROR, TAG, "Failed stopping listening server: %d", ret);
+        return OC_STACK_ERROR;
     }
+    return OC_STACK_OK;
+}
 
-    return result;
+CAMessageType_t qualityOfServiceToMessageType(OCQualityOfService qos)
+{
+    switch (qos)
+    {
+        case OC_HIGH_QOS:
+            return CA_MSG_CONFIRM;
+        case OC_LOW_QOS:
+        case OC_MEDIUM_QOS:
+        case OC_NA_QOS:
+        default:
+            return CA_MSG_NONCONFIRM;
+    }
 }
 
-/**
- * Verify the lengths of the URI and the query separately
- *
- * @param inputUri       - Input URI and query.
- * @param uriLen         - The length of the initial URI with query.
- *
- * Note: The '?' that appears after the URI is not considered as
- * a part of the query.
- */
 OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
 {
     char *query;
@@ -518,160 +2209,483 @@ OCStackResult verifyUriQueryLength(const char *inputUri, uint16_t uriLen)
 }
 
 /**
- * Discover or Perform requests on a specified resource (specified by that Resource's respective URI).
- *
- * @param handle             - @ref OCDoHandle to refer to the request sent out on behalf of calling this API.
- * @param method             - @ref OCMethod to perform on the resource
- * @param requiredUri        - URI of the resource to interact with
- * @param referenceUri       - URI of the reference resource
- * @param request            - JSON encoded request
- * @param qos                - quality of service
- * @param cbData             - struct that contains asynchronous callback function that is invoked
- *                             by the stack when discovery or resource interaction is complete
- * @param options            - The address of an array containing the vendor specific header
- *                             header options to be sent with the request
- * @param numOptions         - Number of vendor specific header options to be included
+ *  A request uri consists of the following components in order:
+ *                              example
+ *  optionally one of
+ *      CoAP over UDP prefix    "coap://"
+ *      CoAP over TCP prefix    "coap+tcp://"
+ *  optionally one of
+ *      IPv6 address            "[1234::5678]"
+ *      IPv4 address            "192.168.1.1"
+ *  optional port               ":5683"
+ *  resource uri                "/oc/core..."
  *
- * @return
- *     OC_STACK_OK               - no errors
- *     OC_STACK_INVALID_CALLBACK - invalid callback function pointer
- *     OC_STACK_INVALID_METHOD   - invalid resource method
- *     OC_STACK_INVALID_URI      - invalid required or reference URI
+ *  for PRESENCE requests, extract resource type.
  */
+static OCStackResult ParseRequestUri(const char *fullUri,
+                                        OCTransportAdapter adapter,
+                                        OCTransportFlags flags,
+                                        OCDevAddr **devAddr,
+                                        char **resourceUri,
+                                        char **resourceType)
+{
+    VERIFY_NON_NULL(fullUri, FATAL, OC_STACK_INVALID_CALLBACK);
+
+    OCStackResult result = OC_STACK_OK;
+    OCDevAddr *da = NULL;
+    char *colon = NULL;
+    char *end;
+
+    // provide defaults for all returned values
+    if (devAddr)
+    {
+        *devAddr = NULL;
+    }
+    if (resourceUri)
+    {
+        *resourceUri = NULL;
+    }
+    if (resourceType)
+    {
+        *resourceType = NULL;
+    }
+
+    // delimit url prefix, if any
+    const char *start = fullUri;
+    char *slash2 = strstr(start, "//");
+    if (slash2)
+    {
+        start = slash2 + 2;
+    }
+    char *slash = strchr(start, '/');
+    if (!slash)
+    {
+        return OC_STACK_INVALID_URI;
+    }
+
+    // process url scheme
+    size_t prefixLen = slash2 - fullUri;
+    bool istcp = false;
+    if (prefixLen)
+    {
+        if ((prefixLen == sizeof(COAP_TCP) - 1) && (!strncmp(fullUri, COAP_TCP, prefixLen)))
+        {
+            istcp = true;
+        }
+    }
+
+    // TODO: this logic should come in with unit tests exercising the various strings
+    // processs url prefix, if any
+    size_t urlLen = slash - start;
+    // port
+    uint16_t port = 0;
+    size_t len = 0;
+    if (urlLen && devAddr)
+    {   // construct OCDevAddr
+        if (start[0] == '[')
+        {   // ipv6 address
+            char *close = strchr(++start, ']');
+            if (!close || close > slash)
+            {
+                return OC_STACK_INVALID_URI;
+            }
+            end = close;
+            if (close[1] == ':')
+            {
+                colon = close + 1;
+            }
+            adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
+            flags = (OCTransportFlags)(flags | OC_IP_USE_V6);
+        }
+        else
+        {
+            char *dot = strchr(start, '.');
+            if (dot && dot < slash)
+            {   // ipv4 address
+                colon = strchr(start, ':');
+                end = (colon && colon < slash) ? colon : slash;
+
+                if (istcp)
+                {   // coap over tcp
+                    adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_TCP);
+                }
+                else
+                {
+                    adapter = (OCTransportAdapter)(adapter | OC_ADAPTER_IP);
+                    flags = (OCTransportFlags)(flags | OC_IP_USE_V4);
+                }
+            }
+            else
+            {   // MAC address
+                end = slash;
+            }
+        }
+        len = end - start;
+        if (len >= sizeof(da->addr))
+        {
+            return OC_STACK_INVALID_URI;
+        }
+        // collect port, if any
+        if (colon && colon < slash)
+        {
+            for (colon++; colon < slash; colon++)
+            {
+                char c = colon[0];
+                if (c < '0' || c > '9')
+                {
+                    return OC_STACK_INVALID_URI;
+                }
+                port = 10 * port + c - '0';
+            }
+        }
+
+        len = end - start;
+        if (len >= sizeof(da->addr))
+        {
+            return OC_STACK_INVALID_URI;
+        }
+
+        da = (OCDevAddr *)OICCalloc(sizeof (OCDevAddr), 1);
+        if (!da)
+        {
+            return OC_STACK_NO_MEMORY;
+        }
+        OICStrcpyPartial(da->addr, sizeof(da->addr), start, len);
+        da->port = port;
+        da->adapter = adapter;
+        da->flags = flags;
+        if (!strncmp(fullUri, "coaps:", 6))
+        {
+            da->flags = (OCTransportFlags)(da->flags|CA_SECURE);
+        }
+        *devAddr = da;
+    }
+
+    // process resource uri, if any
+    if (slash)
+    {   // request uri and query
+        size_t ulen = strlen(slash); // resource uri length
+        size_t tlen = 0;      // resource type length
+        char *type = NULL;
+
+        static const char strPresence[] = "/oic/ad?rt=";
+        static const size_t lenPresence = sizeof(strPresence) - 1;
+        if (!strncmp(slash, strPresence, lenPresence))
+        {
+            type = slash + lenPresence;
+            tlen = ulen - lenPresence;
+        }
+        // resource uri
+        if (resourceUri)
+        {
+            *resourceUri = (char *)OICMalloc(ulen + 1);
+            if (!*resourceUri)
+            {
+                result = OC_STACK_NO_MEMORY;
+                goto error;
+            }
+            strcpy(*resourceUri, slash);
+        }
+        // resource type
+        if (type && resourceType)
+        {
+            *resourceType = (char *)OICMalloc(tlen + 1);
+            if (!*resourceType)
+            {
+                result = OC_STACK_NO_MEMORY;
+                goto error;
+            }
+
+            OICStrcpy(*resourceType, (tlen+1), type);
+        }
+    }
+
+    return OC_STACK_OK;
+
+error:
+    // free all returned values
+    if (devAddr)
+    {
+        OICFree(*devAddr);
+    }
+    if (resourceUri)
+    {
+        OICFree(*resourceUri);
+    }
+    if (resourceType)
+    {
+        OICFree(*resourceType);
+    }
+    return result;
+}
 
-OCStackResult OCDoResource(OCDoHandle *handle, OCMethod method, const char *requiredUri,
-                           const char *referenceUri, const char *request,
-                           OCQualityOfService qos, OCCallbackData *cbData,
-                           OCHeaderOption * options, uint8_t numOptions)
+static OCStackResult OCPreparePresence(CAEndpoint_t *endpoint,
+                                        char *resourceUri, char **requestUri)
 {
-    OCStackResult result = OC_STACK_ERROR;
-    OCCoAPToken token;
-    ClientCB *clientCB = NULL;
-    unsigned char * requestUri = NULL;
-    unsigned char * resourceType = NULL;
-    char * newUri = (char *)requiredUri;
-    (void) referenceUri;
+    char uri[CA_MAX_URI_LENGTH];
+
+    FormCanonicalPresenceUri(endpoint, resourceUri, uri);
 
-    OC_LOG(INFO, TAG, PCF("Entering OCDoResource"));
+    *requestUri = OICStrdup(uri);
+    if (!*requestUri)
+    {
+        return OC_STACK_NO_MEMORY;
+    }
+
+    return OC_STACK_OK;
+}
+
+/**
+ * Discover or Perform requests on a specified resource
+ */
+OCStackResult OCDoResource(OCDoHandle *handle,
+                            OCMethod method,
+                            const char *requestUri,
+                            const OCDevAddr *destination,
+                            OCPayload* payload,
+                            OCConnectivityType connectivityType,
+                            OCQualityOfService qos,
+                            OCCallbackData *cbData,
+                            OCHeaderOption *options,
+                            uint8_t numOptions)
+{
+    OIC_LOG(INFO, TAG, "Entering OCDoResource");
 
     // Validate input parameters
     VERIFY_NON_NULL(cbData, FATAL, OC_STACK_INVALID_CALLBACK);
     VERIFY_NON_NULL(cbData->cb, FATAL, OC_STACK_INVALID_CALLBACK);
+    VERIFY_NON_NULL(requestUri , FATAL, OC_STACK_INVALID_URI);
+
+    OCStackResult result = OC_STACK_ERROR;
+    CAResult_t caResult;
+    CAToken_t token = NULL;
+    uint8_t tokenLength = CA_MAX_TOKEN_LEN;
+    ClientCB *clientCB = NULL;
+    OCDoHandle resHandle = NULL;
+    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
+    OCDevAddr tmpDevAddr = { OC_DEFAULT_ADAPTER };
+    uint32_t ttl = 0;
+    OCTransportAdapter adapter;
+    OCTransportFlags flags;
+    // the request contents are put here
+    CARequestInfo_t requestInfo = {.method = CA_GET};
+    // requestUri  will be parsed into the following three variables
+    OCDevAddr *devAddr = NULL;
+    char *resourceUri = NULL;
+    char *resourceType = NULL;
 
-    TODO ("Need to form the final query by concatenating require and reference URI's");
-    VERIFY_NON_NULL(requiredUri, FATAL, OC_STACK_INVALID_URI);
+    /*
+     * Support original behavior with address on resourceUri argument.
+     */
+    adapter = (OCTransportAdapter)(connectivityType >> CT_ADAPTER_SHIFT);
+    flags = (OCTransportFlags)(connectivityType & CT_MASK_FLAGS);
 
-    uint16_t uriLen = strlen(requiredUri);
+    result = ParseRequestUri(requestUri, adapter, flags, &devAddr, &resourceUri, &resourceType);
 
-    // ToDo: We should also check if the requiredUri has a mutlicast address, then qos has to be OC_Low_QOS
-    switch (method)
+    if (result != OC_STACK_OK)
     {
-        case OC_REST_GET:
-        case OC_REST_PUT:
-        case OC_REST_POST:
-        case OC_REST_DELETE:
-        case OC_REST_OBSERVE:
-        case OC_REST_OBSERVE_ALL:
-        case OC_REST_CANCEL_OBSERVE:
-            break;
-        #ifdef WITH_PRESENCE
-        case OC_REST_PRESENCE:
-            break;
-        #endif
-        default:
-            result = OC_STACK_INVALID_METHOD;
-            goto exit;
+        OIC_LOG_V(DEBUG, TAG, "Unable to parse uri: %s", requestUri);
+        goto exit;
     }
 
-    if((result = verifyUriQueryLength(requiredUri, uriLen)) != OC_STACK_OK)
+    switch (method)
     {
+    case OC_REST_GET:
+    case OC_REST_OBSERVE:
+    case OC_REST_OBSERVE_ALL:
+    case OC_REST_CANCEL_OBSERVE:
+        requestInfo.method = CA_GET;
+        break;
+    case OC_REST_PUT:
+        requestInfo.method = CA_PUT;
+        break;
+    case OC_REST_POST:
+        requestInfo.method = CA_POST;
+        break;
+    case OC_REST_DELETE:
+        requestInfo.method = CA_DELETE;
+        break;
+    case OC_REST_DISCOVER:
+        qos = OC_LOW_QOS;
+        if (destination || devAddr)
+        {
+            requestInfo.isMulticast = false;
+        }
+        else
+        {
+            tmpDevAddr.adapter = adapter;
+            tmpDevAddr.flags = flags;
+            destination = &tmpDevAddr;
+            requestInfo.isMulticast = true;
+        }
+        // CA_DISCOVER will become GET and isMulticast
+        requestInfo.method = CA_GET;
+        break;
+#ifdef WITH_PRESENCE
+    case OC_REST_PRESENCE:
+        // Replacing method type with GET because "presence"
+        // is a stack layer only implementation.
+        requestInfo.method = CA_GET;
+        break;
+#endif
+    default:
+        result = OC_STACK_INVALID_METHOD;
         goto exit;
     }
 
-    if((request) && (strlen(request) > MAX_REQUEST_LENGTH))
+    if (!devAddr && !destination)
     {
+        OIC_LOG(DEBUG, TAG, "no devAddr and no destination");
         result = OC_STACK_INVALID_PARAM;
         goto exit;
     }
 
-#ifdef WITH_PRESENCE
-    if(method == OC_REST_PRESENCE)
+    /* If not original behavior, use destination argument */
+    if (destination && !devAddr)
     {
-        result = getResourceType(requiredUri, &resourceType, &newUri);
-        if(resourceType) {
-            OC_LOG_V(DEBUG, TAG, "Got Resource Type: %s", resourceType);
-        }
-        else
+        devAddr = (OCDevAddr *)OICMalloc(sizeof (OCDevAddr));
+        if (!devAddr)
         {
-            OC_LOG(DEBUG, TAG, "Got Resource Type is NULL.");
+            result = OC_STACK_NO_MEMORY;
+            goto exit;
         }
-        if(result != OC_STACK_OK)
+        *devAddr = *destination;
+    }
+
+    resHandle = GenerateInvocationHandle();
+    if (!resHandle)
+    {
+        result = OC_STACK_NO_MEMORY;
+        goto exit;
+    }
+
+    caResult = CAGenerateToken(&token, tokenLength);
+    if (caResult != CA_STATUS_OK)
+    {
+        OIC_LOG(ERROR, TAG, "CAGenerateToken error");
+        result= OC_STACK_ERROR;
+        goto exit;
+    }
+
+    // fill in request data
+    requestInfo.info.type = qualityOfServiceToMessageType(qos);
+    requestInfo.info.token = token;
+    requestInfo.info.tokenLength = tokenLength;
+    requestInfo.info.resourceUri = resourceUri;
+
+    if ((method == OC_REST_OBSERVE) || (method == OC_REST_OBSERVE_ALL))
+    {
+        result = CreateObserveHeaderOption (&(requestInfo.info.options),
+                                    options, numOptions, OC_OBSERVE_REGISTER);
+        if (result != OC_STACK_OK)
         {
             goto exit;
         }
+        requestInfo.info.numOptions = numOptions + 1;
+    }
+    else
+    {
+        requestInfo.info.numOptions = numOptions;
+        requestInfo.info.options =
+            (CAHeaderOption_t*) OICCalloc(numOptions, sizeof(CAHeaderOption_t));
+        memcpy(requestInfo.info.options, (CAHeaderOption_t*)options,
+               numOptions * sizeof(CAHeaderOption_t));
     }
-#endif // WITH_PRESENCE
 
-    requestUri = (unsigned char *) OCMalloc(uriLen + 1);
-    if(requestUri)
+    CopyDevAddrToEndpoint(devAddr, &endpoint);
+
+    if(payload)
     {
-        memcpy(requestUri, newUri, (uriLen + 1));
+        if((result =
+            OCConvertPayload(payload, &requestInfo.info.payload, &requestInfo.info.payloadSize))
+                != OC_STACK_OK)
+        {
+            OIC_LOG(ERROR, TAG, "Failed to create CBOR Payload");
+            goto exit;
+        }
+        requestInfo.info.payloadFormat = CA_FORMAT_APPLICATION_CBOR;
     }
     else
     {
-        result = OC_STACK_NO_MEMORY;
-        goto exit;
+        requestInfo.info.payload = NULL;
+        requestInfo.info.payloadSize = 0;
+        requestInfo.info.payloadFormat = CA_FORMAT_UNDEFINED;
     }
 
-    *handle = GenerateInvocationHandle();
-    if(!*handle)
+    if (result != OC_STACK_OK)
     {
-        result = OC_STACK_NO_MEMORY;
+        OIC_LOG(ERROR, TAG, "CACreateEndpoint error");
         goto exit;
     }
 
-    // Generate token which will be used by OCStack to match responses received
-    // with the request
-    OCGenerateCoAPToken(&token);
+    // prepare for response
+#ifdef WITH_PRESENCE
+    if (method == OC_REST_PRESENCE)
+    {
+        char *presenceUri = NULL;
+        result = OCPreparePresence(&endpoint, resourceUri, &presenceUri);
+        if (OC_STACK_OK != result)
+        {
+            goto exit;
+        }
+
+        // Assign full presence uri as coap://ip:port/oic/ad to add to callback list.
+        // Presence notification will form a canonical uri to
+        // look for callbacks into the application.
+        resourceUri = presenceUri;
+    }
+#endif
 
-    if((result = AddClientCB(&clientCB, cbData, &token, handle, method, requestUri, resourceType))
-            != OC_STACK_OK)
+    ttl = GetTicks(MAX_CB_TIMEOUT_SECONDS * MILLISECONDS_PER_SECOND);
+    result = AddClientCB(&clientCB, cbData, token, tokenLength, &resHandle,
+                            method, devAddr, resourceUri, resourceType, ttl);
+    if (OC_STACK_OK != result)
     {
-        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
 
-    // Make call to OCCoAP layer
-    result = OCDoCoAPResource(method, qos, &token, newUri, request, options, numOptions);
+    devAddr = NULL;       // Client CB list entry now owns it
+    resourceUri = NULL;   // Client CB list entry now owns it
+    resourceType = NULL;  // Client CB list entry now owns it
 
-exit:
-    if(newUri != requiredUri)
+    // send request
+    result = OCSendRequest(&endpoint, &requestInfo);
+    if (OC_STACK_OK != result)
+    {
+        goto exit;
+    }
+
+    if (handle)
     {
-        OCFree(newUri);
+        *handle = resHandle;
     }
+
+exit:
     if (result != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, PCF("OCDoResource error"));
+        OIC_LOG(ERROR, TAG, "OCDoResource error");
         FindAndDeleteClientCB(clientCB);
+        CADestroyToken(token);
+        if (handle)
+        {
+            *handle = NULL;
+        }
+        OICFree(resHandle);
     }
+
+    // This is the owner of the payload object, so we free it
+    OCPayloadDestroy(payload);
+    OICFree(requestInfo.info.payload);
+    OICFree(devAddr);
+    OICFree(resourceUri);
+    OICFree(resourceType);
+    OICFree(requestInfo.info.options);
     return result;
 }
 
-/**
- * Cancel a request associated with a specific @ref OCDoResource invocation.
- *
- * @param handle - Used to identify a specific OCDoResource invocation.
- * @param qos    - used to specify Quality of Service (read below for more info)
- * @param options- used to specify vendor specific header options when sending
- *                 explicit observe cancellation
- * @param numOptions- Number of header options to be included
- *
- * @return
- *     OC_STACK_OK               - No errors; Success
- *     OC_STACK_INVALID_PARAM    - The handle provided is invalid.
- */
 OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption * options,
-        uint8_t numOptions) {
+        uint8_t numOptions)
+{
     /*
      * This ftn is implemented one of two ways in the case of observation:
      *
@@ -680,7 +2694,7 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption
      *      When the next notification comes in from server,
      *      reply with RESET message to server.
      *      Keep in mind that the server will react to RESET only
-     *      if the last notification was sent ans CON
+     *      if the last notification was sent as CON
      *
      * 2. qos == OC_CONFIRMABLE. When OCCancel is called,
      *      and it is associated with an observe request
@@ -690,313 +2704,429 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption
      *      Remove the callback associated on client side.
      */
     OCStackResult ret = OC_STACK_OK;
+    CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
+    CARequestInfo_t requestInfo = {.method = CA_GET};
 
-    if(!handle) {
+    if(!handle)
+    {
         return OC_STACK_INVALID_PARAM;
     }
 
-    OC_LOG(INFO, TAG, PCF("Entering OCCancel"));
+    ClientCB *clientCB = GetClientCB(NULL, 0, handle, NULL);
+    if (!clientCB)
+    {
+        OIC_LOG(ERROR, TAG, "Callback not found. Called OCCancel on same resource twice?");
+        return OC_STACK_ERROR;
+    }
+
+    switch (clientCB->method)
+    {
+        case OC_REST_OBSERVE:
+        case OC_REST_OBSERVE_ALL:
+
+            OIC_LOG_V(INFO, TAG, "Canceling observation for resource %s",
+                                        clientCB->requestUri);
+            if (qos != OC_HIGH_QOS)
+            {
+                FindAndDeleteClientCB(clientCB);
+                break;
+            }
+
+            OIC_LOG(INFO, TAG, "Cancelling observation as CONFIRMABLE");
+
+            requestInfo.info.type = qualityOfServiceToMessageType(qos);
+            requestInfo.info.token = clientCB->token;
+            requestInfo.info.tokenLength = clientCB->tokenLength;
 
-    ClientCB *clientCB = GetClientCB(NULL, handle, NULL);
+            if (CreateObserveHeaderOption (&(requestInfo.info.options),
+                    options, numOptions, OC_OBSERVE_DEREGISTER) != OC_STACK_OK)
+            {
+                return OC_STACK_ERROR;
+            }
+            requestInfo.info.numOptions = numOptions + 1;
+            requestInfo.info.resourceUri = OICStrdup (clientCB->requestUri);
+
+            CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
+
+            ret = OCSendRequest(&endpoint, &requestInfo);
+
+            if (requestInfo.info.options)
+            {
+                OICFree (requestInfo.info.options);
+            }
+            if (requestInfo.info.resourceUri)
+            {
+                OICFree (requestInfo.info.resourceUri);
+            }
+
+            break;
+
+        case OC_REST_DISCOVER:
+            OIC_LOG_V(INFO, TAG, "Cancelling discovery callback for resource %s",
+                                           clientCB->requestUri);
+            FindAndDeleteClientCB(clientCB);
+            break;
+
+#ifdef WITH_PRESENCE
+        case OC_REST_PRESENCE:
+            FindAndDeleteClientCB(clientCB);
+            break;
+#endif
+
+        default:
+            ret = OC_STACK_INVALID_METHOD;
+            break;
+    }
+
+    return ret;
+}
 
-    if(clientCB) {
-        switch (clientCB->method)
+/**
+ * @brief   Register Persistent storage callback.
+ * @param   persistentStorageHandler [IN] Pointers to open, read, write, close & unlink handlers.
+ * @return
+ *     OC_STACK_OK    - No errors; Success
+ *     OC_STACK_INVALID_PARAM - Invalid parameter
+ */
+OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistentStorageHandler)
+{
+    OIC_LOG(INFO, TAG, "RegisterPersistentStorageHandler !!");
+    if(!persistentStorageHandler)
+    {
+        OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
+        return OC_STACK_INVALID_PARAM;
+    }
+    else
+    {
+        if( !persistentStorageHandler->open ||
+                !persistentStorageHandler->close ||
+                !persistentStorageHandler->read ||
+                !persistentStorageHandler->unlink ||
+                !persistentStorageHandler->write)
         {
-            case OC_REST_OBSERVE:
-            case OC_REST_OBSERVE_ALL:
-                if(qos == OC_HIGH_QOS)
-                {
-                    ret = OCDoCoAPResource(OC_REST_CANCEL_OBSERVE, qos,
-                            &(clientCB->token), (const char *) clientCB->requestUri, NULL, options,
-                            numOptions);
-                }
-                else
-                {
-                    FindAndDeleteClientCB(clientCB);
-                }
-                break;
-            #ifdef WITH_PRESENCE
-            case OC_REST_PRESENCE:
-                FindAndDeleteClientCB(clientCB);
-                break;
-            #endif
-            default:
-                return OC_STACK_INVALID_METHOD;
+            OIC_LOG(ERROR, TAG, "The persistent storage handler is invalid");
+            return OC_STACK_INVALID_PARAM;
         }
     }
-    return ret;
+    return SRMRegisterPersistentStorageHandler(persistentStorageHandler);
 }
+
 #ifdef WITH_PRESENCE
+
 OCStackResult OCProcessPresence()
 {
     OCStackResult result = OC_STACK_OK;
-    uint8_t ipAddr[4] = { 0 };
-    uint16_t port = 0;
 
-    OC_LOG(INFO, TAG, PCF("Entering RequestPresence"));
+    // the following line floods the log with messages that are irrelevant
+    // to most purposes.  Uncomment as needed.
+    //OIC_LOG(INFO, TAG, "Entering RequestPresence");
     ClientCB* cbNode = NULL;
-    OCDevAddr dst;
     OCClientResponse clientResponse;
-    OCResponse * response = NULL;
+    OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
 
-    LL_FOREACH(cbList, cbNode) {
-        if(OC_REST_PRESENCE == cbNode->method)
+    LL_FOREACH(cbList, cbNode)
+    {
+        if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
         {
-            if(cbNode->presence)
+            continue;
+        }
+
+        uint32_t now = GetTicks(0);
+        OIC_LOG_V(DEBUG, TAG, "this TTL level %d",
+                                                cbNode->presence->TTLlevel);
+        OIC_LOG_V(DEBUG, TAG, "current ticks %d", now);
+
+        if (cbNode->presence->TTLlevel > PresenceTimeOutSize)
+        {
+            goto exit;
+        }
+
+        if (cbNode->presence->TTLlevel < PresenceTimeOutSize)
+        {
+            OIC_LOG_V(DEBUG, TAG, "timeout ticks %d",
+                    cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
+        }
+        if (cbNode->presence->TTLlevel >= PresenceTimeOutSize)
+        {
+            OIC_LOG(DEBUG, TAG, "No more timeout ticks");
+
+            clientResponse.sequenceNumber = 0;
+            clientResponse.result = OC_STACK_PRESENCE_TIMEOUT;
+            clientResponse.devAddr = *cbNode->devAddr;
+            FixUpClientResponse(&clientResponse);
+            clientResponse.payload = NULL;
+
+            // Increment the TTLLevel (going to a next state), so we don't keep
+            // sending presence notification to client.
+            cbNode->presence->TTLlevel++;
+            OIC_LOG_V(DEBUG, TAG, "moving to TTL level %d",
+                                        cbNode->presence->TTLlevel);
+
+            cbResult = cbNode->callBack(cbNode->context, cbNode->handle, &clientResponse);
+            if (cbResult == OC_STACK_DELETE_TRANSACTION)
             {
-                uint32_t now = GetTime(0);
-                OC_LOG_V(DEBUG, TAG, "----------------this TTL level %d", cbNode->presence->TTLlevel);
-                OC_LOG_V(DEBUG, TAG, "----------------current ticks %d", now);
+                FindAndDeleteClientCB(cbNode);
+            }
+        }
 
+        if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
+        {
+            continue;
+        }
 
-                if(cbNode->presence->TTLlevel >= (PresenceTimeOutSize + 1))
-                {
-                    goto exit;
-                }
+        CAEndpoint_t endpoint = {.adapter = CA_DEFAULT_ADAPTER};
+        CAInfo_t requestData = {.type = CA_MSG_CONFIRM};
+        CARequestInfo_t requestInfo = {.method = CA_GET};
 
-                if(cbNode->presence->TTLlevel < PresenceTimeOutSize){
-                    OC_LOG_V(DEBUG, TAG, "----------------timeout ticks %d",
-                            cbNode->presence->timeOut[cbNode->presence->TTLlevel]);
-                }
+        OIC_LOG(DEBUG, TAG, "time to test server presence");
 
-                if(cbNode->presence->TTLlevel >= PresenceTimeOutSize)
-                {
-                    OC_LOG(DEBUG, TAG, PCF("----------------No more timeout ticks"));
-                    if (ParseIPv4Address( cbNode->requestUri, ipAddr, &port))
-                    {
-                        OCBuildIPv4Address(ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3], port,
-                                &dst);
-                        result = FormOCClientResponse(&clientResponse, OC_STACK_PRESENCE_TIMEOUT,
-                                (OCDevAddr *) &dst, 0, NULL);
-                        if(result != OC_STACK_OK)
-                        {
-                            goto exit;
-                        }
-                        result = FormOCResponse(&response, cbNode, 0, &clientResponse);
-                        if(result != OC_STACK_OK)
-                        {
-                            goto exit;
-                        }
-
-                        // Increment the TTLLevel (going to a next state), so we don't keep
-                        // sending presence notification to client.
-                        cbNode->presence->TTLlevel++;
-                        OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d",
-                                                cbNode->presence->TTLlevel);
-                    }
-                    else
-                    {
-                        result = OC_STACK_INVALID_IP;
-                        goto exit;
-                    }
-                    HandleStackResponses(response);
-                }
-                if(now >= cbNode->presence->timeOut[cbNode->presence->TTLlevel])
-                {
-                    OC_LOG(DEBUG, TAG, PCF("time to test server presence =========="));
-                    OCCoAPToken token;
-                    OCGenerateCoAPToken(&token);
-                    result = OCDoCoAPResource(OC_REST_GET, OC_LOW_QOS,
-                            &token, (const char *)cbNode->requestUri, NULL, NULL, 0);
-                    if(result != OC_STACK_OK)
-                    {
-                        goto exit;
-                    }
-                    cbNode->presence->TTLlevel++;
-                    OC_LOG_V(DEBUG, TAG, "----------------moving to TTL level %d", cbNode->presence->TTLlevel);
-                }
-            }
+        CopyDevAddrToEndpoint(cbNode->devAddr, &endpoint);
+
+        requestData.type = CA_MSG_NONCONFIRM;
+        requestData.token = cbNode->token;
+        requestData.tokenLength = cbNode->tokenLength;
+        requestData.resourceUri = OC_RSRVD_PRESENCE_URI;
+        requestInfo.method = CA_GET;
+        requestInfo.info = requestData;
+
+        result = OCSendRequest(&endpoint, &requestInfo);
+        if (OC_STACK_OK != result)
+        {
+            goto exit;
         }
+
+        cbNode->presence->TTLlevel++;
+        OIC_LOG_V(DEBUG, TAG, "moving to TTL level %d", cbNode->presence->TTLlevel);
     }
 exit:
     if (result != OC_STACK_OK)
     {
-        OC_LOG(ERROR, TAG, PCF("OCProcessPresence error"));
+        OIC_LOG(ERROR, TAG, "OCProcessPresence error");
     }
+
     return result;
 }
-#endif
-
-/**
- * Called in main loop of OC client or server.  Allows low-level processing of
- * stack services.
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
-OCStackResult OCProcess() {
+#endif // WITH_PRESENCE
 
-    OC_LOG(INFO, TAG, PCF("Entering OCProcess"));
-    #ifdef WITH_PRESENCE
+OCStackResult OCProcess()
+{
+#ifdef WITH_PRESENCE
     OCProcessPresence();
-    #endif
-    OCProcessCoAP();
+#endif
+    CAHandleRequestResponse();
+
+#ifdef ROUTING_GATEWAY
+    RMProcess();
+#endif
 
+#ifdef TCP_ADAPTER
+    ProcessKeepAlive();
+#endif
     return OC_STACK_OK;
 }
 
 #ifdef WITH_PRESENCE
-/**
- * When operating in @ref OCServer or @ref OCClientServer mode, this API will start sending out
- * presence notifications to clients via multicast. Once this API has been called with a success,
- * clients may query for this server's presence and this server's stack will respond via multicast.
- *
- * Server can call this function when it comes online for the first time, or when it comes back
- * online from offline mode, or when it re enters network.
- *
- * @param ttl - Time To Live in seconds
- * Note: If ttl is '0', then the default stack value will be used (60 Seconds).
- *
- * @return
- *     OC_STACK_OK      - No errors; Success
- */
 OCStackResult OCStartPresence(const uint32_t ttl)
 {
+    uint8_t tokenLength = CA_MAX_TOKEN_LEN;
     OCChangeResourceProperty(
             &(((OCResource *)presenceResource.handle)->resourceProperties),
             OC_ACTIVE, 1);
 
-    if(ttl > 0)
+    if (OC_MAX_PRESENCE_TTL_SECONDS < ttl)
+    {
+        presenceResource.presenceTTL = OC_MAX_PRESENCE_TTL_SECONDS;
+        OIC_LOG(INFO, TAG, "Setting Presence TTL to max value");
+    }
+    else if (0 == ttl)
+    {
+        presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
+        OIC_LOG(INFO, TAG, "Setting Presence TTL to default value");
+    }
+    else
     {
         presenceResource.presenceTTL = ttl;
     }
+    OIC_LOG_V(DEBUG, TAG, "Presence TTL is %" PRIu32 " seconds", presenceResource.presenceTTL);
 
-    if(OC_PRESENCE_UNINITIALIZED == presenceState)
+    if (OC_PRESENCE_UNINITIALIZED == presenceState)
     {
-        OCDevAddr multiCastAddr;
-        OCCoAPToken token;
-
         presenceState = OC_PRESENCE_INITIALIZED;
-        OCGenerateCoAPToken(&token);
-        OCBuildIPv4Address(224, 0, 1, 187, 5683, &multiCastAddr);
-        //add the presence observer
-        AddObserver(OC_PRESENCE_URI, NULL, 0, &token, &multiCastAddr,
-            (OCResource *)presenceResource.handle, OC_LOW_QOS);
+
+        OCDevAddr devAddr = { OC_DEFAULT_ADAPTER };
+
+        CAToken_t caToken = NULL;
+        CAResult_t caResult = CAGenerateToken(&caToken, tokenLength);
+        if (caResult != CA_STATUS_OK)
+        {
+            OIC_LOG(ERROR, TAG, "CAGenerateToken error");
+            CADestroyToken(caToken);
+            return OC_STACK_ERROR;
+        }
+
+        AddObserver(OC_RSRVD_PRESENCE_URI, NULL, 0, caToken, tokenLength,
+                (OCResource *)presenceResource.handle, OC_LOW_QOS, OC_FORMAT_UNDEFINED, &devAddr);
+        CADestroyToken(caToken);
     }
 
     // Each time OCStartPresence is called
     // a different random 32-bit integer number is used
     ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
 
-    return SendPresenceNotification(NULL);
+    return SendPresenceNotification(((OCResource *)presenceResource.handle)->rsrcType,
+            OC_PRESENCE_TRIGGER_CREATE);
 }
 
-/**
- * When operating in @ref OCServer or @ref OCClientServer mode, this API will stop sending out
- * presence notifications to clients via multicast. Once this API has been called with a success,
- * this server's stack will not respond to clients querying for this server's presence.
- *
- * Server can call this function when it is terminating, going offline, or when going
- * away from network.
- *
- * @return
- *     OC_STACK_OK      - No errors; Success
- */
 OCStackResult OCStopPresence()
 {
     OCStackResult result = OC_STACK_ERROR;
-    //make resource inactive
+
+    if(presenceResource.handle)
+    {
+        ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
+
+    // make resource inactive
     result = OCChangeResourceProperty(
             &(((OCResource *) presenceResource.handle)->resourceProperties),
             OC_ACTIVE, 0);
-    result = SendPresenceNotification(NULL);
+    }
 
-    return result;
+    if(result != OC_STACK_OK)
+    {
+        OIC_LOG(ERROR, TAG,
+                      "Changing the presence resource properties to ACTIVE not successful");
+        return result;
+    }
+
+    return SendStopNotification();
 }
 #endif
 
-
-OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler)
+OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandler,
+                                            void* callbackParameter)
 {
     defaultDeviceHandler = entityHandler;
+    defaultDeviceHandlerCallbackParameter = callbackParameter;
 
     return OC_STACK_OK;
 }
 
-OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
+OCStackResult OCSetPlatformInfo(OCPlatformInfo platformInfo)
 {
-    OC_LOG(INFO, TAG, PCF("Entering OCSetDeviceInfo"));
+    OIC_LOG(INFO, TAG, "Entering OCSetPlatformInfo");
 
-    if(myStackMode == OC_CLIENT)
+    if(myStackMode ==  OC_SERVER || myStackMode == OC_CLIENT_SERVER || myStackMode == OC_GATEWAY)
+    {
+        if (validatePlatformInfo(platformInfo))
+        {
+            return SavePlatformInfo(platformInfo);
+        }
+        else
+        {
+            return OC_STACK_INVALID_PARAM;
+        }
+    }
+    else
     {
         return OC_STACK_ERROR;
     }
+}
+
+OCStackResult OCSetDeviceInfo(OCDeviceInfo deviceInfo)
+{
+    OIC_LOG(INFO, TAG, "Entering OCSetDeviceInfo");
+
+    if (!deviceInfo.deviceName || deviceInfo.deviceName[0] == '\0')
+    {
+        OIC_LOG(ERROR, TAG, "Null or empty device name.");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    if (deviceInfo.types)
+    {
+        OCStringLL *type =  deviceInfo.types;
+        OCResource *resource = findResource((OCResource *) deviceResource);
+        if (!resource)
+        {
+            return OC_STACK_INVALID_PARAM;
+        }
+        deleteResourceType(resource->rsrcType);
+        resource->rsrcType = NULL;
 
+        while (type)
+        {
+            OCBindResourceTypeToResource(deviceResource, type->value);
+            type = type->next;
+        }
+    }
     return SaveDeviceInfo(deviceInfo);
 }
 
-/**
- * Create a resource
- *
- * @param handle - pointer to handle to newly created resource.  Set by ocstack.  Used to refer to resource
- * @param resourceTypeName - name of resource type.  Example: "core.led"
- * @param resourceInterfaceName - name of resource interface.  Example: "core.rw"
- * @param uri - URI of the resource.  Example:  "/a/led"
- * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
- *                        NULL for default entity handler
- * @param resourceProperties - properties supported by resource.  Example: OC_DISCOVERABLE|OC_OBSERVABLE
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
 OCStackResult OCCreateResource(OCResourceHandle *handle,
         const char *resourceTypeName,
         const char *resourceInterfaceName,
         const char *uri, OCEntityHandler entityHandler,
-        uint8_t resourceProperties) {
+        void* callbackParam,
+        uint8_t resourceProperties)
+{
 
     OCResource *pointer = NULL;
-    char *str = NULL;
-    size_t size;
     OCStackResult result = OC_STACK_ERROR;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCCreateResource"));
+    OIC_LOG(INFO, TAG, "Entering OCCreateResource");
 
     if(myStackMode == OC_CLIENT)
     {
-        return result;
+        return OC_STACK_INVALID_PARAM;
     }
     // Validate parameters
-    if(!uri || (strlen(uri) == 0))
+    if(!uri || uri[0]=='\0' || strlen(uri)>=MAX_URI_LENGTH )
     {
-        OC_LOG(ERROR, TAG, PCF("URI is invalid"));
+        OIC_LOG(ERROR, TAG, "URI is empty or too long");
         return OC_STACK_INVALID_URI;
     }
     // Is it presented during resource discovery?
-    if (!handle || !resourceTypeName) {
-        OC_LOG(ERROR, TAG, PCF("Input parameter is NULL"));
+    if (!handle || !resourceTypeName || resourceTypeName[0] == '\0' )
+    {
+        OIC_LOG(ERROR, TAG, "Input parameter is NULL");
         return OC_STACK_INVALID_PARAM;
     }
 
-    if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0) {
+    if(!resourceInterfaceName || strlen(resourceInterfaceName) == 0)
+    {
         resourceInterfaceName = OC_RSRVD_INTERFACE_DEFAULT;
     }
 
     // Make sure resourceProperties bitmask has allowed properties specified
     if (resourceProperties
-            > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE)) {
-        OC_LOG(ERROR, TAG, PCF("Invalid property"));
+            > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW | OC_SECURE |
+               OC_EXPLICIT_DISCOVERABLE))
+    {
+        OIC_LOG(ERROR, TAG, "Invalid property");
         return OC_STACK_INVALID_PARAM;
     }
 
     // If the headResource is NULL, then no resources have been created...
     pointer = headResource;
-    if (pointer) {
+    if (pointer)
+    {
         // At least one resources is in the resource list, so we need to search for
         // repeated URLs, which are not allowed.  If a repeat is found, exit with an error
-        while (pointer) {
-            if (strcmp(uri, pointer->uri) == 0) {
-                OC_LOG(ERROR, TAG, PCF("URI already in use"));
+        while (pointer)
+        {
+            if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0)
+            {
+                OIC_LOG_V(ERROR, TAG, "Resource %s already exists", uri);
                 return OC_STACK_INVALID_PARAM;
             }
             pointer = pointer->next;
         }
     }
     // Create the pointer and insert it into the resource list
-    pointer = (OCResource *) OCCalloc(1, sizeof(OCResource));
-    if (!pointer) {
+    pointer = (OCResource *) OICCalloc(1, sizeof(OCResource));
+    if (!pointer)
+    {
+        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
     pointer->sequenceNum = OC_OFFSET_SEQUENCE_NUMBER;
@@ -1004,13 +3134,12 @@ OCStackResult OCCreateResource(OCResourceHandle *handle,
     insertResource(pointer);
 
     // Set the uri
-    size = strlen(uri) + 1;
-    str = (char *) OCMalloc(size);
-    if (!str) {
+    pointer->uri = OICStrdup(uri);
+    if (!pointer->uri)
+    {
+        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
-    strncpy(str, uri, size);
-    pointer->uri = str;
 
     // Set properties.  Set OC_ACTIVE
     pointer->resourceProperties = (OCResourceProperty) (resourceProperties
@@ -1018,15 +3147,17 @@ OCStackResult OCCreateResource(OCResourceHandle *handle,
 
     // Add the resourcetype to the resource
     result = BindResourceTypeToResource(pointer, resourceTypeName);
-    if (result != OC_STACK_OK) {
-        OC_LOG(ERROR, TAG, PCF("Error adding resourcetype"));
+    if (result != OC_STACK_OK)
+    {
+        OIC_LOG(ERROR, TAG, "Error adding resourcetype");
         goto exit;
     }
 
     // Add the resourceinterface to the resource
     result = BindResourceInterfaceToResource(pointer, resourceInterfaceName);
-    if (result != OC_STACK_OK) {
-        OC_LOG(ERROR, TAG, PCF("Error adding resourceinterface"));
+    if (result != OC_STACK_OK)
+    {
+        OIC_LOG(ERROR, TAG, "Error adding resourceinterface");
         goto exit;
     }
 
@@ -1035,236 +3166,260 @@ OCStackResult OCCreateResource(OCResourceHandle *handle,
     if (entityHandler)
     {
         pointer->entityHandler = entityHandler;
+        pointer->entityHandlerCallbackParam = callbackParam;
     }
     else
     {
         pointer->entityHandler = defaultResourceEHandler;
+        pointer->entityHandlerCallbackParam = NULL;
     }
 
+    // Initialize a pointer indicating child resources in case of collection
+    pointer->rsrcChildResourcesHead = NULL;
+
     *handle = pointer;
     result = OC_STACK_OK;
 
-    #ifdef WITH_PRESENCE
-    if(presenceResource.handle)
+#ifdef WITH_PRESENCE
+    if (presenceResource.handle)
     {
         ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-        SendPresenceNotification(pointer->rsrcType);
+        SendPresenceNotification(pointer->rsrcType, OC_PRESENCE_TRIGGER_CREATE);
     }
-    #endif
+#endif
 exit:
     if (result != OC_STACK_OK)
     {
         // Deep delete of resource and other dynamic elements that it contains
         deleteResource(pointer);
-        OCFree(str);
-    }
-    return result;
-}
-
-
-
-/**
- * Create a resource. with host ip address for remote resource
- *
- * @param handle - pointer to handle to newly created resource.  Set by ocstack.
- *                 Used to refer to resource
- * @param resourceTypeName - name of resource type.  Example: "core.led"
- * @param resourceInterfaceName - name of resource interface.  Example: "core.rw"
- * @param host - HOST address of the remote resource.  Example:  "coap://xxx.xxx.xxx.xxx:xxxxx"
- * @param uri - URI of the resource.  Example:  "/a/led"
- * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
- *                        NULL for default entity handler
- * @param resourceProperties - properties supported by resource.
- *                             Example: OC_DISCOVERABLE|OC_OBSERVABLE
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
-
-OCStackResult OCCreateResourceWithHost(OCResourceHandle *handle,
-        const char *resourceTypeName,
-        const char *resourceInterfaceName,
-        const char *host,
-        const char *uri,
-        OCEntityHandler entityHandler,
-        uint8_t resourceProperties)
-{
-    char *str = NULL;
-    size_t size;
-    OCStackResult result = OC_STACK_ERROR;
-
-    result = OCCreateResource(handle, resourceTypeName, resourceInterfaceName,
-                                uri, entityHandler, resourceProperties);
-
-    if (result != OC_STACK_ERROR)
-    {
-        // Set the uri
-        size = strlen(host) + 1;
-        str = (char *) OCMalloc(size);
-        if (!str)
-        {
-            return OC_STACK_ERROR;
-        }
-        strncpy(str, host, size);
-        ((OCResource *) *handle)->host = str;
     }
-
     return result;
 }
 
-/**
- * Add a resource to a collection resource.
- *
- * @param collectionHandle - handle to the collection resource
- * @param resourceHandle - handle to resource to be added to the collection resource
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- *     OC_STACK_INVALID_PARAM - invalid collectionhandle
- */
 OCStackResult OCBindResource(
-        OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
-    OCResource *resource;
-    uint8_t i;
+        OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
+{
+    OCResource *resource = NULL;
+    OCChildResource *tempChildResource = NULL;
+    OCChildResource *newChildResource = NULL;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCBindResource"));
+    OIC_LOG(INFO, TAG, "Entering OCBindResource");
 
     // Validate parameters
     VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
     VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
     // Container cannot contain itself
-    if (collectionHandle == resourceHandle) {
-        OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle"));
+    if (collectionHandle == resourceHandle)
+    {
+        OIC_LOG(ERROR, TAG, "Added handle equals collection handle");
         return OC_STACK_INVALID_PARAM;
     }
 
     // Use the handle to find the resource in the resource linked list
     resource = findResource((OCResource *) collectionHandle);
-    if (!resource) {
-        OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Collection handle not found");
         return OC_STACK_INVALID_PARAM;
     }
 
     // Look for an open slot to add add the child resource.
     // If found, add it and return success
-    for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
-        if (!resource->rsrcResources[i]) {
-            resource->rsrcResources[i] = (OCResource *) resourceHandle;
-            OC_LOG(INFO, TAG, PCF("resource bound"));
-            return OC_STACK_OK;
-        }
+
+    tempChildResource = resource->rsrcChildResourcesHead;
+
+    while(resource->rsrcChildResourcesHead && tempChildResource->next)
+    {
+        // TODO: what if one of child resource was deregistered without unbinding?
+        tempChildResource = tempChildResource->next;
     }
 
-    #ifdef WITH_PRESENCE
-    if(presenceResource.handle)
+    // Do memory allocation for child resource
+    newChildResource = (OCChildResource *) OICCalloc(1, sizeof(OCChildResource));
+    if(!newChildResource)
+    {
+        OIC_LOG(ERROR, TAG, "Adding new child resource is failed due to memory allocation failure");
+        return OC_STACK_ERROR;
+    }
+
+    newChildResource->rsrcResource = (OCResource *) resourceHandle;
+    newChildResource->next = NULL;
+
+    if(!resource->rsrcChildResourcesHead)
+    {
+        resource->rsrcChildResourcesHead = newChildResource;
+    }
+    else {
+        tempChildResource->next = newChildResource;
+    }
+
+    OIC_LOG(INFO, TAG, "resource bound");
+
+#ifdef WITH_PRESENCE
+    if (presenceResource.handle)
     {
         ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-        SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
+        SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
+                OC_PRESENCE_TRIGGER_CHANGE);
     }
-    #endif
+#endif
 
-    // Unable to add resourceHandle, so return error
-    return OC_STACK_ERROR;
+    return OC_STACK_OK;
 }
 
-/**
- * Remove a resource from a collection resource.
- *
- * @param collectionHandle - handle to the collection resource
- * @param resourceHandle - handle to resource to be added to the collection resource
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- *     OC_STACK_INVALID_PARAM - invalid collectionHandle
- */
 OCStackResult OCUnBindResource(
-        OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) {
-    OCResource *resource;
-    uint8_t i;
+        OCResourceHandle collectionHandle, OCResourceHandle resourceHandle)
+{
+    OCResource *resource = NULL;
+    OCChildResource *tempChildResource = NULL;
+    OCChildResource *tempLastChildResource = NULL;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource"));
+    OIC_LOG(INFO, TAG, "Entering OCUnBindResource");
 
     // Validate parameters
     VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR);
     VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR);
     // Container cannot contain itself
-    if (collectionHandle == resourceHandle) {
-        OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle"));
+    if (collectionHandle == resourceHandle)
+    {
+        OIC_LOG(ERROR, TAG, "removing handle equals collection handle");
         return OC_STACK_INVALID_PARAM;
     }
 
     // Use the handle to find the resource in the resource linked list
     resource = findResource((OCResource *) collectionHandle);
-    if (!resource) {
-        OC_LOG(ERROR, TAG, PCF("Collection handle not found"));
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Collection handle not found");
         return OC_STACK_INVALID_PARAM;
     }
 
     // Look for an open slot to add add the child resource.
     // If found, add it and return success
-    for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) {
-        if (resourceHandle == resource->rsrcResources[i]) {
-            resource->rsrcResources[i] = (OCResource *) NULL;
-            OC_LOG(INFO, TAG, PCF("resource unbound"));
+    if(!resource->rsrcChildResourcesHead)
+    {
+        OIC_LOG(INFO, TAG, "resource not found in collection");
+
+        // Unable to add resourceHandle, so return error
+        return OC_STACK_ERROR;
+
+    }
+
+    tempChildResource = resource->rsrcChildResourcesHead;
+
+    while (tempChildResource)
+    {
+        if(tempChildResource->rsrcResource == resourceHandle)
+        {
+            // if resource going to be unbinded is the head one.
+            if( tempChildResource == resource->rsrcChildResourcesHead )
+            {
+                OCChildResource *temp = resource->rsrcChildResourcesHead->next;
+                OICFree(resource->rsrcChildResourcesHead);
+                resource->rsrcChildResourcesHead = temp;
+                temp = NULL;
+            }
+            else
+            {
+                OCChildResource *temp = tempChildResource->next;
+                OICFree(tempChildResource);
+                tempLastChildResource->next = temp;
+                temp = NULL;
+            }
+
+            OIC_LOG(INFO, TAG, "resource unbound");
+
+            // Send notification when resource is unbounded successfully.
+#ifdef WITH_PRESENCE
+            if (presenceResource.handle)
+            {
+                ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
+                SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType,
+                        OC_PRESENCE_TRIGGER_CHANGE);
+            }
+#endif
+            tempChildResource = NULL;
+            tempLastChildResource = NULL;
+
             return OC_STACK_OK;
+
         }
+
+        tempLastChildResource = tempChildResource;
+        tempChildResource = tempChildResource->next;
     }
 
-    OC_LOG(INFO, TAG, PCF("resource not found in collection"));
+    OIC_LOG(INFO, TAG, "resource not found in collection");
 
-    #ifdef WITH_PRESENCE
-    if(presenceResource.handle)
+    tempChildResource = NULL;
+    tempLastChildResource = NULL;
+
+    // Unable to add resourceHandle, so return error
+    return OC_STACK_ERROR;
+}
+
+// Precondition is that the parameter has been checked to not equal NULL.
+static bool ValidateResourceTypeInterface(const char *resourceItemName)
+{
+    if (resourceItemName[0] < 'a' || resourceItemName[0] > 'z')
     {
-        ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-        SendPresenceNotification(((OCResource *) resourceHandle)->rsrcType);
+        return false;
+    }
+
+    size_t index = 1;
+    while (resourceItemName[index] != '\0')
+    {
+        if (resourceItemName[index] != '.' &&
+                resourceItemName[index] != '-' &&
+                (resourceItemName[index] < 'a' || resourceItemName[index] > 'z') &&
+                (resourceItemName[index] < '0' || resourceItemName[index] > '9'))
+        {
+            return false;
+        }
+        ++index;
     }
-    #endif
 
-    // Unable to add resourceHandle, so return error
-    return OC_STACK_ERROR;
+    return true;
 }
-
 OCStackResult BindResourceTypeToResource(OCResource* resource,
                                             const char *resourceTypeName)
 {
     OCResourceType *pointer = NULL;
     char *str = NULL;
-    size_t size;
     OCStackResult result = OC_STACK_ERROR;
 
-    OC_LOG(INFO, TAG, PCF("Entering BindResourceTypeToResource"));
-
-    // Validate parameters
     VERIFY_NON_NULL(resourceTypeName, ERROR, OC_STACK_INVALID_PARAM);
-    // TODO:  Does resource attribute resentation really have to be maintained in stack?
-    // Is it presented during resource discovery?
 
-    TODO ("Make sure that the resourcetypename doesn't already exist in the resource");
+    if (!ValidateResourceTypeInterface(resourceTypeName))
+    {
+        OIC_LOG(ERROR, TAG, "resource type illegal (see RFC 6690)");
+        return OC_STACK_INVALID_PARAM;
+    }
 
-    // Create the resourcetype and insert it into the resource list
-    pointer = (OCResourceType *) OCCalloc(1, sizeof(OCResourceType));
-    if (!pointer) {
+    pointer = (OCResourceType *) OICCalloc(1, sizeof(OCResourceType));
+    if (!pointer)
+    {
+        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
 
-    // Set the resourceTypeName
-    size = strlen(resourceTypeName) + 1;
-    str = (char *) OCMalloc(size);
-    if (!str) {
+    str = OICStrdup(resourceTypeName);
+    if (!str)
+    {
+        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
-    strncpy(str, resourceTypeName, size);
     pointer->resourcetypename = str;
+    pointer->next = NULL;
 
     insertResourceType(resource, pointer);
     result = OC_STACK_OK;
 
-    exit: if (result != OC_STACK_OK) {
-        OCFree(pointer);
-        OCFree(str);
+exit:
+    if (result != OC_STACK_OK)
+    {
+        OICFree(pointer);
+        OICFree(str);
     }
 
     return result;
@@ -1275,29 +3430,31 @@ OCStackResult BindResourceInterfaceToResource(OCResource* resource,
 {
     OCResourceInterface *pointer = NULL;
     char *str = NULL;
-    size_t size;
     OCStackResult result = OC_STACK_ERROR;
 
-    OC_LOG(INFO, TAG, PCF("Entering BindResourceInterfaceToResource"));
-
-    // Validate parameters
     VERIFY_NON_NULL(resourceInterfaceName, ERROR, OC_STACK_INVALID_PARAM);
 
-    TODO ("Make sure that the resourceinterface name doesn't already exist in the resource");
+    if (!ValidateResourceTypeInterface(resourceInterfaceName))
+    {
+        OIC_LOG(ERROR, TAG, "resource /interface illegal (see RFC 6690)");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    OIC_LOG_V(INFO, TAG, "Binding %s interface to %s", resourceInterfaceName, resource->uri);
 
-    // Create the resourceinterface and insert it into the resource list
-    pointer = (OCResourceInterface *) OCCalloc(1, sizeof(OCResourceInterface));
-    if (!pointer) {
+    pointer = (OCResourceInterface *) OICCalloc(1, sizeof(OCResourceInterface));
+    if (!pointer)
+    {
+        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
 
-    // Set the resourceinterface name
-    size = strlen(resourceInterfaceName) + 1;
-    str = (char *) OCMalloc(size);
-    if (!str) {
+    str = OICStrdup(resourceInterfaceName);
+    if (!str)
+    {
+        result = OC_STACK_NO_MEMORY;
         goto exit;
     }
-    strncpy(str, resourceInterfaceName, size);
     pointer->name = str;
 
     // Bind the resourceinterface to the resource
@@ -1305,234 +3462,160 @@ OCStackResult BindResourceInterfaceToResource(OCResource* resource,
 
     result = OC_STACK_OK;
 
-    exit: if (result != OC_STACK_OK) {
-        OCFree(pointer);
-        OCFree(str);
+    exit:
+    if (result != OC_STACK_OK)
+    {
+        OICFree(pointer);
+        OICFree(str);
     }
 
     return result;
 }
 
-/**
- * Bind a resourcetype to a resource.
- *
- * @param handle - handle to the resource
- * @param resourceTypeName - name of resource type.  Example: "core.led"
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
 OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
-        const char *resourceTypeName) {
+        const char *resourceTypeName)
+{
 
     OCStackResult result = OC_STACK_ERROR;
-    OCResource *resource;
+    OCResource *resource = NULL;
 
-    // Make sure resource exists
     resource = findResource((OCResource *) handle);
-    if (!resource) {
-        OC_LOG(ERROR, TAG, PCF("Resource not found"));
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
         return OC_STACK_ERROR;
     }
 
-    // call internal function
     result = BindResourceTypeToResource(resource, resourceTypeName);
 
-    #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
     if(presenceResource.handle)
     {
         ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-        SendPresenceNotification(resource->rsrcType);
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
     }
-    #endif
+#endif
 
     return result;
 }
 
-/**
- * Bind a resourceinterface to a resource.
- *
- * @param handle - handle to the resource
- * @param resourceInterfaceName - name of resource interface.  Example: "oc.mi.b"
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
-
 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
-        const char *resourceInterfaceName) {
+        const char *resourceInterfaceName)
+{
 
     OCStackResult result = OC_STACK_ERROR;
-    OCResource *resource;
+    OCResource *resource = NULL;
 
-    // Make sure resource exists
     resource = findResource((OCResource *) handle);
-    if (!resource) {
-        OC_LOG(ERROR, TAG, PCF("Resource not found"));
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
         return OC_STACK_ERROR;
     }
 
-    // call internal function
     result = BindResourceInterfaceToResource(resource, resourceInterfaceName);
 
-    #ifdef WITH_PRESENCE
-    if(presenceResource.handle)
+#ifdef WITH_PRESENCE
+    if (presenceResource.handle)
     {
         ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-        SendPresenceNotification(resource->rsrcType);
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
     }
-    #endif
+#endif
 
     return result;
 }
 
-/**
- * Get the number of resources that have been created in the stack.
- *
- * @param numResources - pointer to count variable
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
-
- */
-OCStackResult OCGetNumberOfResources(uint8_t *numResources) {
+OCStackResult OCGetNumberOfResources(uint8_t *numResources)
+{
     OCResource *pointer = headResource;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResources"));
     VERIFY_NON_NULL(numResources, ERROR, OC_STACK_INVALID_PARAM);
     *numResources = 0;
-    while (pointer) {
+    while (pointer)
+    {
         *numResources = *numResources + 1;
         pointer = pointer->next;
     }
     return OC_STACK_OK;
 }
 
-/**
- * Get a resource handle by index.
- *
- * @param index - index of resource, 0 to Count - 1
- *
- * @return
- *    Resource handle - if found
- *    NULL - if not found
- */
-OCResourceHandle OCGetResourceHandle(uint8_t index) {
+OCResourceHandle OCGetResourceHandle(uint8_t index)
+{
     OCResource *pointer = headResource;
-    uint8_t i = 0;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandle"));
 
-    // Iterate through the list
-    while ((i < index) && pointer) {
-        i++;
+    for( uint8_t i = 0; i < index && pointer; ++i)
+    {
         pointer = pointer->next;
     }
     return (OCResourceHandle) pointer;
 }
 
-/**
- * Delete resource specified by handle.  Deletes resource and all resourcetype and resourceinterface
- * linked lists.
- *
- * @param handle - handle of resource to be deleted
- *
- * @return
- *     OC_STACK_OK              - no errors
- *     OC_STACK_ERROR           - stack process error
- *     OC_STACK_NO_RESOURCE     - resource not found
- *     OC_STACK_INVALID_PARAM   - invalid param
- */
-OCStackResult OCDeleteResource(OCResourceHandle handle) {
-    OC_LOG(INFO, TAG, PCF("Entering OCDeleteResource"));
-
-    if (!handle) {
-        OC_LOG(ERROR, TAG, PCF("Invalid param"));
+OCStackResult OCDeleteResource(OCResourceHandle handle)
+{
+    if (!handle)
+    {
+        OIC_LOG(ERROR, TAG, "Invalid handle for deletion");
         return OC_STACK_INVALID_PARAM;
     }
 
     OCResource *resource = findResource((OCResource *) handle);
-    if (resource == NULL) {
-        OC_LOG(ERROR, TAG, PCF("Resource not found"));
+    if (resource == NULL)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
         return OC_STACK_NO_RESOURCE;
     }
 
-    if (deleteResource((OCResource *) handle) == 0) {
-        OC_LOG(ERROR, TAG, PCF("Error deleting resource"));
+    if (deleteResource((OCResource *) handle) != OC_STACK_OK)
+    {
+        OIC_LOG(ERROR, TAG, "Error deleting resource");
         return OC_STACK_ERROR;
     }
 
     return OC_STACK_OK;
 }
 
-/**
- * Get the URI of the resource specified by handle.
- *
- * @param handle - handle of resource
- * @return
- *    URI string - if resource found
- *    NULL - resource not found
- */
-const char *OCGetResourceUri(OCResourceHandle handle) {
-    OCResource *resource;
-    OC_LOG(INFO, TAG, PCF("Entering OCGetResourceUri"));
+const char *OCGetResourceUri(OCResourceHandle handle)
+{
+    OCResource *resource = NULL;
 
     resource = findResource((OCResource *) handle);
-    if (resource) {
+    if (resource)
+    {
         return resource->uri;
     }
     return (const char *) NULL;
 }
 
-/**
- * Get the properties of the resource specified by handle.
- * NOTE: that after a resource is created, the OC_ACTIVE property is set
- * for the resource by the stack.
- *
- * @param handle - handle of resource
- * @return
- *    property bitmap - if resource found
- *    NULL - resource not found
- */
-uint8_t OCGetResourceProperties(OCResourceHandle handle) {
-    OCResource *resource;
-    OC_LOG(INFO, TAG, PCF("Entering OCGetResourceProperties"));
+OCResourceProperty OCGetResourceProperties(OCResourceHandle handle)
+{
+    OCResource *resource = NULL;
 
     resource = findResource((OCResource *) handle);
-    if (resource) {
+    if (resource)
+    {
         return resource->resourceProperties;
     }
-    return 0;
+    return (OCResourceProperty)-1;
 }
 
-/**
- * Get the number of resource types of the resource.
- *
- * @param handle - handle of resource
- * @param numResourceTypes - pointer to count variable
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
 OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
-        uint8_t *numResourceTypes) {
-    OCResource *resource;
-    OCResourceType *pointer;
+        uint8_t *numResourceTypes)
+{
+    OCResource *resource = NULL;
+    OCResourceType *pointer = NULL;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceTypes"));
     VERIFY_NON_NULL(numResourceTypes, ERROR, OC_STACK_INVALID_PARAM);
     VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
 
     *numResourceTypes = 0;
 
     resource = findResource((OCResource *) handle);
-    if (resource) {
+    if (resource)
+    {
         pointer = resource->rsrcType;
-        while (pointer) {
+        while (pointer)
+        {
             *numResourceTypes = *numResourceTypes + 1;
             pointer = pointer->next;
         }
@@ -1540,55 +3623,34 @@ OCStackResult OCGetNumberOfResourceTypes(OCResourceHandle handle,
     return OC_STACK_OK;
 }
 
-/**
- * Get name of resource type of the resource.
- *
- * @param handle - handle of resource
- * @param index - index of resource, 0 to Count - 1
- *
- * @return
- *    resource type name - if resource found
- *    NULL - resource not found
- */
-const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index) {
-    OCResourceType *resourceType;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCGetResourceTypeName"));
+const char *OCGetResourceTypeName(OCResourceHandle handle, uint8_t index)
+{
+    OCResourceType *resourceType = NULL;
 
     resourceType = findResourceTypeAtIndex(handle, index);
-    if (resourceType) {
+    if (resourceType)
+    {
         return resourceType->resourcetypename;
     }
     return (const char *) NULL;
 }
 
-
-
-/**
- * Get the number of resource interfaces of the resource.
- *
- * @param handle - handle of resource
- * @param numResources - pointer to count variable
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
 OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
-        uint8_t *numResourceInterfaces) {
-    OCResourceInterface *pointer;
-    OCResource *resource;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCGetNumberOfResourceInterfaces"));
+        uint8_t *numResourceInterfaces)
+{
+    OCResourceInterface *pointer = NULL;
+    OCResource *resource = NULL;
 
     VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
     VERIFY_NON_NULL(numResourceInterfaces, ERROR, OC_STACK_INVALID_PARAM);
 
     *numResourceInterfaces = 0;
     resource = findResource((OCResource *) handle);
-    if (resource) {
+    if (resource)
+    {
         pointer = resource->rsrcInterface;
-        while (pointer) {
+        while (pointer)
+        {
             *numResourceInterfaces = *numResourceInterfaces + 1;
             pointer = pointer->next;
         }
@@ -1596,114 +3658,88 @@ OCStackResult OCGetNumberOfResourceInterfaces(OCResourceHandle handle,
     return OC_STACK_OK;
 }
 
-/**
- * Get name of resource interface of the resource.
- *
- * @param handle - handle of resource
- * @param index - index of resource, 0 to Count - 1
- *
- * @return
- *    resource interface name - if resource found
- *    NULL - resource not found
- */
-const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) {
-    OCResourceInterface *resourceInterface;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCGetResourceInterfaceName"));
+const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index)
+{
+    OCResourceInterface *resourceInterface = NULL;
 
     resourceInterface = findResourceInterfaceAtIndex(handle, index);
-    if (resourceInterface) {
+    if (resourceInterface)
+    {
         return resourceInterface->name;
     }
     return (const char *) NULL;
 }
 
-/**
- * Get resource handle from the collection resource by index.
- *
- * @param collectionHandle - handle of collection resource
- * @param index - index of contained resource, 0 to Count - 1
- *
- * @return
- *    handle to resource - if resource found
- *    NULL - resource not found
- */
 OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle,
-        uint8_t index) {
-    OCResource *resource;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCGetContainedResource"));
+        uint8_t index)
+{
+    OCResource *resource = NULL;
+    OCChildResource *tempChildResource = NULL;
+    uint8_t num = 0;
 
-    if (index >= MAX_CONTAINED_RESOURCES) {
+    resource = findResource((OCResource *) collectionHandle);
+    if (!resource)
+    {
         return NULL;
     }
 
-    resource = findResource((OCResource *) collectionHandle);
-    if (!resource) {
-        return NULL;
+    tempChildResource = resource->rsrcChildResourcesHead;
+
+    while(tempChildResource)
+    {
+        if( num == index )
+        {
+            return tempChildResource->rsrcResource;
+        }
+        num++;
+        tempChildResource = tempChildResource->next;
     }
 
-    return resource->rsrcResources[index];
+    // In this case, the number of resource handles in the collection exceeds the index
+    tempChildResource = NULL;
+    return NULL;
 }
 
-/**
- * Bind an entity handler to the resource.
- *
- * @param handle - handle to the resource that the contained resource is to be bound
- * @param entityHandler - entity handler function that is called by ocstack to handle requests, etc
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_ERROR - stack process error
- */
 OCStackResult OCBindResourceHandler(OCResourceHandle handle,
-        OCEntityHandler entityHandler) {
-    OCResource *resource;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCBindResourceHandler"));
+        OCEntityHandler entityHandler,
+        void* callbackParam)
+{
+    OCResource *resource = NULL;
 
     // Validate parameters
     VERIFY_NON_NULL(handle, ERROR, OC_STACK_INVALID_PARAM);
-    //VERIFY_NON_NULL(entityHandler, ERROR, OC_STACK_INVALID_PARAM);
 
     // Use the handle to find the resource in the resource linked list
     resource = findResource((OCResource *)handle);
-    if (!resource) {
-        OC_LOG(ERROR, TAG, PCF("Resource not found"));
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
         return OC_STACK_ERROR;
     }
 
     // Bind the handler
     resource->entityHandler = entityHandler;
+    resource->entityHandlerCallbackParam = callbackParam;
 
-    #ifdef WITH_PRESENCE
-    if(presenceResource.handle)
+#ifdef WITH_PRESENCE
+    if (presenceResource.handle)
     {
         ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-        SendPresenceNotification(resource->rsrcType);
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
     }
-    #endif
+#endif
 
     return OC_STACK_OK;
 }
 
-/**
- * Get the entity handler for a resource.
- *
- * @param handle - handle of resource
- *
- * @return
- *    entity handler - if resource found
- *    NULL - resource not found
- */
-OCEntityHandler OCGetResourceHandler(OCResourceHandle handle) {
-    OCResource *resource;
-
-    OC_LOG(INFO, TAG, PCF("Entering OCGetResourceHandler"));
+OCEntityHandler OCGetResourceHandler(OCResourceHandle handle)
+{
+    OCResource *resource = NULL;
 
-    // Use the handle to find the resource in the resource linked list
     resource = findResource((OCResource *)handle);
-    if (!resource) {
-        OC_LOG(ERROR, TAG, PCF("Resource not found"));
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
         return NULL;
     }
 
@@ -1723,18 +3759,11 @@ void incrementSequenceNumber(OCResource * resPtr)
 }
 
 #ifdef WITH_PRESENCE
-/**
- * Notify Presence subscribers that a resource has been modified
- *
- * @param resourceType - Handle to the resourceType linked list of resource
- *                       that was modified.
- * @param qos          - Quality Of Service
- *
- */
-OCStackResult SendPresenceNotification(OCResourceType *resourceType)
+OCStackResult SendPresenceNotification(OCResourceType *resourceType,
+        OCPresenceTrigger trigger)
 {
     OCResource *resPtr = NULL;
-    OCStackResult result;
+    OCStackResult result = OC_STACK_ERROR;
     OCMethod method = OC_REST_PRESENCE;
     uint32_t maxAge = 0;
     resPtr = findResource((OCResource *) presenceResource.handle);
@@ -1742,46 +3771,51 @@ OCStackResult SendPresenceNotification(OCResourceType *resourceType)
     {
         return OC_STACK_NO_RESOURCE;
     }
+
     if((((OCResource *) presenceResource.handle)->resourceProperties) & OC_ACTIVE)
     {
         maxAge = presenceResource.presenceTTL;
-    }
-    else
-    {
-        maxAge = 0;
+
+        result = SendAllObserverNotification(method, resPtr, maxAge,
+                trigger, resourceType, OC_LOW_QOS);
     }
 
-    result = SendAllObserverNotification(method, resPtr, maxAge, resourceType, OC_LOW_QOS);
     return result;
 }
-#endif
 
-/**
- * Notify observers that an observed value has changed.
- *
- * @param handle - handle of resource
- *
- * @return
- *     OC_STACK_OK    - no errors
- *     OC_STACK_NO_RESOURCE - invalid resource handle
- *     OC_STACK_NO_OBSERVERS - no more observers intrested in resource
- */
-OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos) {
+OCStackResult SendStopNotification()
+{
+    OCResource *resPtr = NULL;
+    OCStackResult result = OC_STACK_ERROR;
+    OCMethod method = OC_REST_PRESENCE;
+    resPtr = findResource((OCResource *) presenceResource.handle);
+    if(NULL == resPtr)
+    {
+        return OC_STACK_NO_RESOURCE;
+    }
 
-    OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
+    // maxAge is 0. ResourceType is NULL.
+    result = SendAllObserverNotification(method, resPtr, 0, OC_PRESENCE_TRIGGER_DELETE,
+            NULL, OC_LOW_QOS);
 
+    return result;
+}
+
+#endif // WITH_PRESENCE
+OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService qos)
+{
     OCResource *resPtr = NULL;
-    OCStackResult result;
+    OCStackResult result = OC_STACK_ERROR;
     OCMethod method = OC_REST_NOMETHOD;
     uint32_t maxAge = 0;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCNotifyAllObservers"));
-    #ifdef WITH_PRESENCE
+    OIC_LOG(INFO, TAG, "Notifying all observers");
+#ifdef WITH_PRESENCE
     if(handle == presenceResource.handle)
     {
         return OC_STACK_OK;
     }
-    #endif // WITH_PRESENCE
+#endif // WITH_PRESENCE
     VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
 
     // Verify that the resource exists
@@ -1796,11 +3830,12 @@ OCStackResult OCNotifyAllObservers(OCResourceHandle handle, OCQualityOfService q
         incrementSequenceNumber(resPtr);
         method = OC_REST_OBSERVE;
         maxAge = MAX_OBSERVE_AGE;
-        #ifdef WITH_PRESENCE
-        result = SendAllObserverNotification (method, resPtr, maxAge, NULL, qos);
-        #else
+#ifdef WITH_PRESENCE
+        result = SendAllObserverNotification (method, resPtr, maxAge,
+                OC_PRESENCE_TRIGGER_DELETE, NULL, qos);
+#else
         result = SendAllObserverNotification (method, resPtr, maxAge, qos);
-        #endif
+#endif
         return result;
     }
 }
@@ -1809,10 +3844,10 @@ OCStackResult
 OCNotifyListOfObservers (OCResourceHandle handle,
                          OCObservationId  *obsIdList,
                          uint8_t          numberOfIds,
-                         unsigned char    *notificationJSONPayload,
+                         const OCRepPayload       *payload,
                          OCQualityOfService qos)
 {
-    OC_LOG(INFO, TAG, PCF("Entering OCNotifyListOfObservers"));
+    OIC_LOG(INFO, TAG, "Entering OCNotifyListOfObservers");
 
     OCResource *resPtr = NULL;
     //TODO: we should allow the server to define this
@@ -1820,9 +3855,8 @@ OCNotifyListOfObservers (OCResourceHandle handle,
 
     VERIFY_NON_NULL(handle, ERROR, OC_STACK_ERROR);
     VERIFY_NON_NULL(obsIdList, ERROR, OC_STACK_ERROR);
-    VERIFY_NON_NULL(notificationJSONPayload, ERROR, OC_STACK_ERROR);
+    VERIFY_NON_NULL(payload, ERROR, OC_STACK_ERROR);
 
-    // Verify that the resource exists
     resPtr = findResource ((OCResource *) handle);
     if (NULL == resPtr || myStackMode == OC_CLIENT)
     {
@@ -1833,113 +3867,108 @@ OCNotifyListOfObservers (OCResourceHandle handle,
         incrementSequenceNumber(resPtr);
     }
     return (SendListObserverNotification(resPtr, obsIdList, numberOfIds,
-            notificationJSONPayload, maxAge, qos));
+            payload, maxAge, qos));
 }
 
-/**
- * Send a response to a request.
- * The response can be a regular, slow, or block (i.e. a response that
- * is too large to be sent in a single PDU and must span multiple transmissions)
- *
- * @param response - pointer to structure that contains response parameters
- *
- * @return
- *     OC_STACK_OK                         - No errors; Success
- *     OC_STACK_INVALID_PARAM              - Invalid pointer to OCServerResponse
- *     OC_STACK_INVALID_REQUEST_HANDLE     - Request handle not found
- *     OC_STACK_PERSISTENT_BUFFER_REQUIRED - Block transfer needed for response, so a
- *                                           persistent response buffer is necessary
- */
 OCStackResult OCDoResponse(OCEntityHandlerResponse *ehResponse)
 {
     OCStackResult result = OC_STACK_ERROR;
     OCServerRequest *serverRequest = NULL;
 
-    OC_LOG(INFO, TAG, PCF("Entering OCDoResponse"));
+    OIC_LOG(INFO, TAG, "Entering OCDoResponse");
 
     // Validate input parameters
     VERIFY_NON_NULL(ehResponse, ERROR, OC_STACK_INVALID_PARAM);
     VERIFY_NON_NULL(ehResponse->requestHandle, ERROR, OC_STACK_INVALID_PARAM);
 
-    // TODO: Placeholder for creating a response entry when implementing
-    // block transfer feature
-
-    // If a response payload is present, check if block transfer is required
-    if (ehResponse->payload && OCIsPacketTransferRequired(NULL,
-            (const char *)ehResponse->payload, ehResponse->payloadSize))
+    // Normal response
+    // Get pointer to request info
+    serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
+    if(serverRequest)
     {
-        OC_LOG(INFO, TAG, PCF("Block transfer required"));
-
-        // Persistent response buffer is needed for block transfer
-        if (!ehResponse->persistentBufferFlag)
-        {
-            OC_LOG(WARNING, TAG, PCF("Persistent response buffer required"));
-            return OC_STACK_PERSISTENT_BUFFER_REQUIRED;
-        }
-        // TODO: Placeholder for block transfer handling
-        // TODO: Placeholder for setting the the response handle in the OCServerResponse struct
-            // when implementing the block transfer feature
+        // response handler in ocserverrequest.c. Usually HandleSingleResponse.
+        result = serverRequest->ehResponseHandler(ehResponse);
     }
-    else
-    {
-        // Normal response
 
-        // Get pointer to request info
-        serverRequest = GetServerRequestUsingHandle((OCServerRequest *)ehResponse->requestHandle);
-        if(serverRequest)
-        {
-            result = serverRequest->ehResponseHandler(ehResponse);
-        }
-    }
     return result;
 }
 
-/**
- * Cancel a response.  Applies to a block response
- *
- * @param responseHandle - response handle set by stack in OCServerResponse after
- *                         OCDoResponse is called
- *
- * @return
- *     OC_STACK_OK               - No errors; Success
- *     OC_STACK_INVALID_PARAM    - The handle provided is invalid.
- */
-OCStackResult OCCancelResponse(OCResponseHandle responseHandle)
+//#ifdef DIRECT_PAIRING
+const OCDPDev_t* OCDiscoverDirectPairingDevices(unsigned short waittime)
+{
+    OIC_LOG(INFO, TAG, "Start OCDiscoverDirectPairingDevices");
+    if(OC_STACK_OK != DPDeviceDiscovery(waittime))
+    {
+        OIC_LOG(ERROR, TAG, "Fail to discover Direct-Pairing device");
+        return NULL;
+    }
+
+    return (const OCDPDev_t*)DPGetDiscoveredDevices();
+}
+
+const OCDPDev_t* OCGetDirectPairedDevices()
 {
-    OCStackResult result = OC_STACK_NOTIMPL;
+    return (const OCDPDev_t*)DPGetPairedDevices();
+}
 
-    OC_LOG(INFO, TAG, PCF("Entering OCCancelResponse"));
+void DirectPairingCB (OCDirectPairingDev_t * peer, OCStackResult result)
+{
+    if (gDirectpairingCallback)
+    {
+        gDirectpairingCallback((OCDPDev_t*)peer, result);
+        gDirectpairingCallback = NULL;
+    }
+}
 
-    // TODO: validate response handle
+OCStackResult OCDoDirectPairing(OCDPDev_t* peer, OCPrm_t pmSel, char *pinNumber,
+                                                     OCDirectPairingCB resultCallback)
+{
+    OIC_LOG(INFO, TAG, "Start OCDoDirectPairing");
+    if(NULL ==  peer || NULL == pinNumber)
+    {
+        OIC_LOG(ERROR, TAG, "Invalid parameters");
+        return OC_STACK_INVALID_PARAM;
+    }
+    if(NULL == resultCallback)
+    {
+        OIC_LOG(ERROR, TAG, "Invalid callback");
+        return OC_STACK_INVALID_CALLBACK;
+    }
 
-    return result;
+    gDirectpairingCallback = resultCallback;
+    return DPDirectPairing((OCDirectPairingDev_t*)peer, (OicSecPrm_t)pmSel,
+                                           pinNumber, DirectPairingCB);
 }
+//#endif // DIRECT_PAIRING
 
 //-----------------------------------------------------------------------------
 // Private internal function definitions
 //-----------------------------------------------------------------------------
-/**
- * Generate handle of OCDoResource invocation for callback management.
- */
 static OCDoHandle GenerateInvocationHandle()
 {
     OCDoHandle handle = NULL;
     // Generate token here, it will be deleted when the transaction is deleted
-    handle = (OCDoHandle) OCMalloc(sizeof(uint8_t[MAX_TOKEN_LENGTH]));
+    handle = (OCDoHandle) OICMalloc(sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
     if (handle)
     {
-        OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[MAX_TOKEN_LENGTH]));
+        OCFillRandomMem((uint8_t*)handle, sizeof(uint8_t[CA_MAX_TOKEN_LEN]));
     }
 
     return handle;
 }
+
 #ifdef WITH_PRESENCE
 OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
         OCResourceProperty resourceProperties, uint8_t enable)
 {
+    if (!inputProperty)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
     if (resourceProperties
-            > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW)) {
-        OC_LOG(ERROR, TAG, PCF("Invalid property"));
+            > (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE | OC_SLOW))
+    {
+        OIC_LOG(ERROR, TAG, "Invalid property");
         return OC_STACK_INVALID_PARAM;
     }
     if(!enable)
@@ -1954,65 +3983,93 @@ OCStackResult OCChangeResourceProperty(OCResourceProperty * inputProperty,
 }
 #endif
 
-/**
- * Initialize resource data structures, variables, etc.
- */
-OCStackResult initResources() {
+OCStackResult initResources()
+{
     OCStackResult result = OC_STACK_OK;
-    // Init application resource vars
+
     headResource = NULL;
+    tailResource = NULL;
     // Init Virtual Resources
-    #ifdef WITH_PRESENCE
-    presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL;
-    //presenceResource.token = OCGenerateCoAPToken();
+#ifdef WITH_PRESENCE
+    presenceResource.presenceTTL = OC_DEFAULT_PRESENCE_TTL_SECONDS;
+
     result = OCCreateResource(&presenceResource.handle,
             OC_RSRVD_RESOURCE_TYPE_PRESENCE,
             "core.r",
-            OC_PRESENCE_URI,
+            OC_RSRVD_PRESENCE_URI,
+            NULL,
             NULL,
             OC_OBSERVABLE);
     //make resource inactive
     result = OCChangeResourceProperty(
             &(((OCResource *) presenceResource.handle)->resourceProperties),
             OC_ACTIVE, 0);
-    #endif
+#endif
+#ifndef WITH_ARDUINO
+    if (result == OC_STACK_OK)
+    {
+        result = SRMInitSecureResources();
+    }
+#endif
+
+    if(result == OC_STACK_OK)
+    {
+        result = OCCreateResource(&deviceResource,
+                                  OC_RSRVD_RESOURCE_TYPE_DEVICE,
+                                  OC_RSRVD_INTERFACE_DEFAULT,
+                                  OC_RSRVD_DEVICE_URI,
+                                  NULL,
+                                  NULL,
+                                  OC_DISCOVERABLE);
+        if(result == OC_STACK_OK)
+        {
+            result = BindResourceInterfaceToResource((OCResource *)deviceResource,
+                                                     OC_RSRVD_INTERFACE_READ);
+        }
+    }
+
+    if(result == OC_STACK_OK)
+    {
+        result = OCCreateResource(&platformResource,
+                                  OC_RSRVD_RESOURCE_TYPE_PLATFORM,
+                                  OC_RSRVD_INTERFACE_DEFAULT,
+                                  OC_RSRVD_PLATFORM_URI,
+                                  NULL,
+                                  NULL,
+                                  OC_DISCOVERABLE);
+        if(result == OC_STACK_OK)
+        {
+            result = BindResourceInterfaceToResource((OCResource *)platformResource,
+                                                     OC_RSRVD_INTERFACE_READ);
+        }
+    }
+
     return result;
 }
 
-/**
- * Add a resource to the end of the linked list of resources.
- *
- * @param resource - resource to be added
- */
-void insertResource(OCResource *resource) {
-    OCResource *pointer;
-
-    if (!headResource) {
+void insertResource(OCResource *resource)
+{
+    if (!headResource)
+    {
         headResource = resource;
-    } else {
-        pointer = headResource;
-
-        while (pointer->next) {
-            pointer = pointer->next;
-        }
-        pointer->next = resource;
+        tailResource = resource;
+    }
+    else
+    {
+        tailResource->next = resource;
+        tailResource = resource;
     }
     resource->next = NULL;
 }
 
-/**
- * Find a resource in the linked list of resources.
- *
- * @param resource - resource to be found
- * @return
- *     NULL                - resource not found
- *     pointer to resource - pointer to resource that was found in the linked list
- */
-OCResource *findResource(OCResource *resource) {
+OCResource *findResource(OCResource *resource)
+{
     OCResource *pointer = headResource;
 
-    while (pointer) {
-        if (pointer == resource) {
+    while (pointer)
+    {
+        if (pointer == resource)
+        {
             return resource;
         }
         pointer = pointer->next;
@@ -2023,214 +4080,203 @@ OCResource *findResource(OCResource *resource) {
 void deleteAllResources()
 {
     OCResource *pointer = headResource;
-    OCResource *temp;
+    OCResource *temp = NULL;
 
     while (pointer)
     {
         temp = pointer->next;
-        #ifdef WITH_PRESENCE
-        if(pointer != (OCResource *) presenceResource.handle)
+#ifdef WITH_PRESENCE
+        if (pointer != (OCResource *) presenceResource.handle)
         {
-            #endif // WITH_PRESENCE
+#endif // WITH_PRESENCE
             deleteResource(pointer);
-            #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
         }
-        #endif // WITH_PRESENCE
+#endif // WITH_PRESENCE
         pointer = temp;
     }
 
-    #ifdef WITH_PRESENCE
+    SRMDeInitSecureResources();
+
+#ifdef WITH_PRESENCE
     // Ensure that the last resource to be deleted is the presence resource. This allows for all
     // presence notification attributed to their deletion to be processed.
     deleteResource((OCResource *) presenceResource.handle);
-    #endif // WITH_PRESENCE
+#endif // WITH_PRESENCE
 }
 
-/**
- * Delete the resource from the linked list.
- *
- * @param resource - resource to be deleted
- * @return
- *    0 - error
- *    1 - success
- */
-int deleteResource(OCResource *resource) {
+OCStackResult deleteResource(OCResource *resource)
+{
     OCResource *prev = NULL;
-    OCResource *temp;
+    OCResource *temp = NULL;
+    if(!resource)
+    {
+        OIC_LOG(DEBUG,TAG,"resource is NULL");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    OIC_LOG_V (INFO, TAG, "Deleting resource %s", resource->uri);
 
     temp = headResource;
-    while (temp) {
-        if (temp == resource) {
+    while (temp)
+    {
+        if (temp == resource)
+        {
             // Invalidate all Resource Properties.
             resource->resourceProperties = (OCResourceProperty) 0;
-            #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
             if(resource != (OCResource *) presenceResource.handle)
             {
-            #endif // WITH_PRESENCE
+#endif // WITH_PRESENCE
                 OCNotifyAllObservers((OCResourceHandle)resource, OC_HIGH_QOS);
-            #ifdef WITH_PRESENCE
+#ifdef WITH_PRESENCE
             }
 
             if(presenceResource.handle)
             {
                 ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
-                if(resource != (OCResource *) presenceResource.handle)
-                {
-                    SendPresenceNotification(resource->rsrcType);
-                }
-                else
-                {
-                    SendPresenceNotification(NULL);
-                }
+                SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
             }
-        #endif
-
-            if (temp == headResource) {
+#endif
+            // Only resource in list.
+            if (temp == headResource && temp == tailResource)
+            {
+                headResource = NULL;
+                tailResource = NULL;
+            }
+            // Deleting head.
+            else if (temp == headResource)
+            {
                 headResource = temp->next;
-            } else {
+            }
+            // Deleting tail.
+            else if (temp == tailResource)
+            {
+                tailResource = prev;
+                tailResource->next = NULL;
+            }
+            else
+            {
                 prev->next = temp->next;
             }
 
             deleteResourceElements(temp);
-            OCFree(temp);
-            return 1;
-        } else {
+            OICFree(temp);
+            return OC_STACK_OK;
+        }
+        else
+        {
             prev = temp;
             temp = temp->next;
         }
     }
 
-    return 0;
+    return OC_STACK_ERROR;
 }
 
-/**
- * Delete all of the dynamically allocated elements that were created for the resource.
- *
- * @param resource - specified resource
- */
-void deleteResourceElements(OCResource *resource) {
-    if (!resource) {
+void deleteResourceElements(OCResource *resource)
+{
+    if (!resource)
+    {
         return;
     }
 
-    // remove URI
-    OCFree(resource->uri);
-
-    // Delete resourcetype linked list
+    OICFree(resource->uri);
     deleteResourceType(resource->rsrcType);
-
-    // Delete resourceinterface linked list
     deleteResourceInterface(resource->rsrcInterface);
 }
 
-/**
- * Delete all of the dynamically allocated elements that were created for the resource type.
- *
- * @param resourceType - specified resource type
- */
-void deleteResourceType(OCResourceType *resourceType) {
+void deleteResourceType(OCResourceType *resourceType)
+{
     OCResourceType *pointer = resourceType;
-    OCResourceType *next;
+    OCResourceType *next = NULL;
 
-    while (pointer) {
+    while (pointer)
+    {
         next = pointer->next;
-        OCFree(pointer->resourcetypename);
-        OCFree(pointer);
+        OICFree(pointer->resourcetypename);
+        OICFree(pointer);
         pointer = next;
     }
 }
 
-/**
- * Delete all of the dynamically allocated elements that were created for the resource interface.
- *
- * @param resourceInterface - specified resource interface
- */
-void deleteResourceInterface(OCResourceInterface *resourceInterface) {
+void deleteResourceInterface(OCResourceInterface *resourceInterface)
+{
     OCResourceInterface *pointer = resourceInterface;
-    OCResourceInterface *next;
+    OCResourceInterface *next = NULL;
 
-    while (pointer) {
+    while (pointer)
+    {
         next = pointer->next;
-        OCFree(pointer->name);
-        OCFree(pointer);
+        OICFree(pointer->name);
+        OICFree(pointer);
         pointer = next;
     }
 }
 
-/**
- * Insert a resource type into a resource's resource type linked list.
- *
- * @param resource - resource where resource type is to be inserted
- * @param resourceType - resource type to be inserted
- */
-void insertResourceType(OCResource *resource, OCResourceType *resourceType) {
-    OCResourceType *pointer;
-
-    if (resource && !resource->rsrcType) {
+void insertResourceType(OCResource *resource, OCResourceType *resourceType)
+{
+    OCResourceType *pointer = NULL;
+    OCResourceType *previous = NULL;
+    if (!resource || !resourceType)
+    {
+        return;
+    }
+    // resource type list is empty.
+    else if (!resource->rsrcType)
+    {
         resource->rsrcType = resourceType;
-    } else {
-        if(resource)
-        {
-            pointer = resource->rsrcType;
-        }
-        else
+    }
+    else
+    {
+        pointer = resource->rsrcType;
+
+        while (pointer)
         {
-            pointer = resourceType;
-        }
-        while (pointer->next) {
+            if (!strcmp(resourceType->resourcetypename, pointer->resourcetypename))
+            {
+                OIC_LOG_V(INFO, TAG, "Type %s already exists", resourceType->resourcetypename);
+                OICFree(resourceType->resourcetypename);
+                OICFree(resourceType);
+                return;
+            }
+            previous = pointer;
             pointer = pointer->next;
         }
-        pointer->next = resourceType;
+        previous->next = resourceType;
     }
     resourceType->next = NULL;
+
+    OIC_LOG_V(INFO, TAG, "Added type %s to %s", resourceType->resourcetypename, resource->uri);
 }
 
-/**
- * Get a resource type at the specified index within a resource.
- *
- * @param handle - handle of resource
- * @param index - index of resource type
- *
- * @return
- *    resourcetype - if found
- *    NULL - not found
- */
-OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index) {
-    OCResource *resource;
-    OCResourceType *pointer;
-    uint8_t i;
+OCResourceType *findResourceTypeAtIndex(OCResourceHandle handle, uint8_t index)
+{
+    OCResource *resource = NULL;
+    OCResourceType *pointer = NULL;
 
     // Find the specified resource
     resource = findResource((OCResource *) handle);
-    if (!resource) {
+    if (!resource)
+    {
         return NULL;
     }
 
     // Make sure a resource has a resourcetype
-    if (!resource->rsrcType) {
+    if (!resource->rsrcType)
+    {
         return NULL;
     }
 
     // Iterate through the list
     pointer = resource->rsrcType;
-    i = 0;
-    while ((i < index) && pointer) {
-        i++;
+    for(uint8_t i = 0; i< index && pointer; ++i)
+    {
         pointer = pointer->next;
     }
     return pointer;
 }
 
-/**
- * Finds a resource type in an OCResourceType link-list.
- *
- * @param resourceTypeList - the link-list to be searched through
- * @param resourceTypeName - the key to search for
- *
- * @return
- *      resourceType that matches the key (ie. resourceTypeName)
- *      NULL - either an invalid parameter or this function was unable to find the key.
- */
 OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char * resourceTypeName)
 {
     if(resourceTypeList && resourceTypeName)
@@ -2250,160 +4296,282 @@ OCResourceType *findResourceType(OCResourceType * resourceTypeList, const char *
     }
     return NULL;
 }
-/**
- * Insert a resource interface into a resource's resource interface linked list.
- *
- * @param resource - resource where resource interface is to be inserted
- * @param resourceInterface - resource interface to be inserted
+
+/*
+ * Insert a new interface into interface linked list only if not already present.
+ * If alredy present, 2nd arg is free'd.
+ * Default interface will always be first if present.
  */
-void insertResourceInterface(OCResource *resource,
-        OCResourceInterface *resourceInterface) {
-    OCResourceInterface *pointer;
+void insertResourceInterface(OCResource *resource, OCResourceInterface *newInterface)
+{
+    OCResourceInterface *pointer = NULL;
+    OCResourceInterface *previous = NULL;
 
-    if (!resource->rsrcInterface) {
-        resource->rsrcInterface = resourceInterface;
-    } else {
-        pointer = resource->rsrcInterface;
-        while (pointer->next) {
+    newInterface->next = NULL;
+
+    OCResourceInterface **firstInterface = &(resource->rsrcInterface);
+
+    if (!*firstInterface)
+    {
+        // If first interface is not oic.if.baseline, by default add it as first interface type.
+        if (0 == strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT))
+        {
+            *firstInterface = newInterface;
+        }
+        else
+        {
+            OCStackResult result = BindResourceInterfaceToResource(resource, OC_RSRVD_INTERFACE_DEFAULT);
+            if (result != OC_STACK_OK)
+            {
+                OICFree(newInterface->name);
+                OICFree(newInterface);
+                return;
+            }
+            if (*firstInterface)
+            {
+                (*firstInterface)->next = newInterface;
+            }
+        }
+    }
+    // If once add oic.if.baseline, later too below code take care of freeing memory.
+    else if (strcmp(newInterface->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
+    {
+        if (strcmp((*firstInterface)->name, OC_RSRVD_INTERFACE_DEFAULT) == 0)
+        {
+            OICFree(newInterface->name);
+            OICFree(newInterface);
+            return;
+        }
+        // This code will not hit anymore, keeping
+        else
+        {
+            newInterface->next = *firstInterface;
+            *firstInterface = newInterface;
+        }
+    }
+    else
+    {
+        pointer = *firstInterface;
+        while (pointer)
+        {
+            if (strcmp(newInterface->name, pointer->name) == 0)
+            {
+                OICFree(newInterface->name);
+                OICFree(newInterface);
+                return;
+            }
+            previous = pointer;
             pointer = pointer->next;
         }
-        pointer->next = resourceInterface;
+        previous->next = newInterface;
     }
-    resourceInterface->next = NULL;
 }
 
-/**
- * Get a resource interface at the specified index within a resource.
- *
- * @param handle - handle of resource
- * @param index - index of resource interface
- *
- * @return
- *    resourceinterface - if found
- *    NULL - not found
- */
 OCResourceInterface *findResourceInterfaceAtIndex(OCResourceHandle handle,
-        uint8_t index) {
-    OCResource *resource;
-    OCResourceInterface *pointer;
-    uint8_t i = 0;
+        uint8_t index)
+{
+    OCResource *resource = NULL;
+    OCResourceInterface *pointer = NULL;
 
     // Find the specified resource
     resource = findResource((OCResource *) handle);
-    if (!resource) {
+    if (!resource)
+    {
         return NULL;
     }
 
     // Make sure a resource has a resourceinterface
-    if (!resource->rsrcInterface) {
+    if (!resource->rsrcInterface)
+    {
         return NULL;
     }
 
     // Iterate through the list
     pointer = resource->rsrcInterface;
 
-    while ((i < index) && pointer) {
-        i++;
+    for (uint8_t i = 0; i < index && pointer; ++i)
+    {
         pointer = pointer->next;
     }
     return pointer;
 }
 
-/**
- * Determine if a request/response must be sent in a block transfer because it is too large to be
- * sent in a single PDU.  This function can be used for either a request or a response
- *
- * @param request  - NULL or pointer to request
- * @param response - NULL or pointer to response
- * @param size     - 0 or size of the request/response.  If 0, strlen is used for determining
- *                   the length of the request/response
- *
- * @return
- *    0 - packet transfer NOT required (i.e. normal request/response)
- *    1 - packet transfer required (i.e. block transfer needed)
+/*
+ * This function splits the uri using the '?' delimiter.
+ * "uriWithoutQuery" is the block of characters between the beginning
+ * till the delimiter or '\0' which ever comes first.
+ * "query" is whatever is to the right of the delimiter if present.
+ * No delimiter sets the query to NULL.
+ * If either are present, they will be malloc'ed into the params 2, 3.
+ * The first param, *uri is left untouched.
+
+ * NOTE: This function does not account for whitespace at the end of the uri NOR
+ *       malformed uri's with '??'. Whitespace at the end will be assumed to be
+ *       part of the query.
  */
-uint8_t OCIsPacketTransferRequired(const char *request, const char *response, uint16_t size)
+OCStackResult getQueryFromUri(const char * uri, char** query, char ** uriWithoutQuery)
 {
-    uint8_t result = 0;
+    if(!uri)
+    {
+        return OC_STACK_INVALID_URI;
+    }
+    if(!query || !uriWithoutQuery)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    *query           = NULL;
+    *uriWithoutQuery = NULL;
+
+    size_t uriWithoutQueryLen = 0;
+    size_t queryLen = 0;
+    size_t uriLen = strlen(uri);
 
-    // Determine if we are checking a request or a response
-    if (request)
+    char *pointerToDelimiter = strstr(uri, "?");
+
+    uriWithoutQueryLen = pointerToDelimiter == NULL ? uriLen : (size_t)(pointerToDelimiter - uri);
+    queryLen = pointerToDelimiter == NULL ? 0 : uriLen - uriWithoutQueryLen - 1;
+
+    if (uriWithoutQueryLen)
     {
-        // If size is greater than 0, use it for the request size value, otherwise
-        // assume request is null terminated and use strlen for size value
-        if ((size > MAX_REQUEST_LENGTH) || (strlen(request) > MAX_REQUEST_LENGTH))
+        *uriWithoutQuery =  (char *) OICCalloc(uriWithoutQueryLen + 1, 1);
+        if (!*uriWithoutQuery)
         {
-            result = 1;
+            goto exit;
         }
+        OICStrcpy(*uriWithoutQuery, uriWithoutQueryLen +1, uri);
     }
-    else if (response)
+    if (queryLen)
     {
-        // If size is greater than 0, use it for the response size value, otherwise
-        // assume response is null terminated and use strlen for size value
-        if ((size > MAX_RESPONSE_LENGTH) || (strlen(response) > MAX_RESPONSE_LENGTH))
+        *query = (char *) OICCalloc(queryLen + 1, 1);
+        if (!*query)
         {
-            result = 1;
+            OICFree(*uriWithoutQuery);
+            *uriWithoutQuery = NULL;
+            goto exit;
         }
+        OICStrcpy(*query, queryLen + 1, pointerToDelimiter + 1);
     }
-    return result;
+
+    return OC_STACK_OK;
+
+    exit:
+        return OC_STACK_NO_MEMORY;
 }
 
-/**
- * Retrieves a resource type based upon a uri string if the uri string contains only just one
- * resource attribute (and that has to be of type "rt").
- *
- * @remark This API malloc's memory for the resource type and newURI. Do not malloc resourceType
- * or newURI before passing in.
- *
- * @param uri - Valid URI for "requiredUri" parameter to OCDoResource API.
- * @param resourceType - The resource type to be populated; pass by reference.
- * @param newURI - Return URI without resourceType appended to the end of it. This is used to
- *                 ensure that the uri parameter is not modified; pass by reference.
- *
- * @return
- *  OC_STACK_INVALID_URI   - Returns this if the URI is invalid/NULL.
- *  OC_STACK_INVALID_PARAM - Returns this if the resourceType parameter is invalid/NULL.
- *  OC_STACK_OK            - Success
- */
-OCStackResult getResourceType(const char * uri, unsigned char** resourceType, char ** newURI)
+static const OicUuid_t* OCGetServerInstanceID(void)
 {
-    if(!uri)
+    static bool generated = false;
+    static OicUuid_t sid;
+    if (generated)
     {
-        return OC_STACK_INVALID_URI;
+        return &sid;
     }
-    if(!resourceType || !newURI)
+
+    if (OC_STACK_OK != GetDoxmDeviceID(&sid))
     {
-        return OC_STACK_INVALID_PARAM;
+        OIC_LOG(FATAL, TAG, "Generate UUID for Server Instance failed!");
+        return NULL;
+    }
+    generated = true;
+    return &sid;
+}
+
+const char* OCGetServerInstanceIDString(void)
+{
+    static bool generated = false;
+    static char sidStr[UUID_STRING_SIZE];
+
+    if(generated)
+    {
+        return sidStr;
     }
-    char * leftToken = NULL;
-    char * tempURI = (char *) OCMalloc(strlen(uri) + 1);
-    if(!tempURI)
+
+    const OicUuid_t *sid = OCGetServerInstanceID();
+    if(OCConvertUuidToString(sid->id, sidStr) != RAND_UUID_OK)
     {
-        goto exit;
+        OIC_LOG(FATAL, TAG, "Generate UUID String for Server Instance failed!");
+        return NULL;
     }
-    strcpy(tempURI, uri);
-    char* strTokPtr;
-    leftToken = strtok_r((char *)tempURI, "?", &strTokPtr);
 
-    while(leftToken != NULL)
+    generated = true;
+    return sidStr;
+}
+
+CAResult_t OCSelectNetwork()
+{
+    CAResult_t retResult = CA_STATUS_FAILED;
+    CAResult_t caResult = CA_STATUS_OK;
+
+    CATransportAdapter_t connTypes[] = {
+            CA_ADAPTER_IP,
+            CA_ADAPTER_RFCOMM_BTEDR,
+            CA_ADAPTER_GATT_BTLE,
+            CA_ADAPTER_NFC
+#ifdef RA_ADAPTER
+            ,CA_ADAPTER_REMOTE_ACCESS
+#endif
+
+#ifdef TCP_ADAPTER
+            ,CA_ADAPTER_TCP
+#endif
+        };
+    int numConnTypes = sizeof(connTypes)/sizeof(connTypes[0]);
+
+    for(int i = 0; i<numConnTypes; i++)
     {
-        if(strncmp(leftToken, "rt=", 3) == 0)
+        // Ignore CA_NOT_SUPPORTED error. The CA Layer may have not compiled in the interface.
+        if(caResult == CA_STATUS_OK || caResult == CA_NOT_SUPPORTED)
         {
-            *resourceType = (unsigned char *) OCMalloc(strlen(leftToken)-3);
-            if(!*resourceType)
-            {
-                goto exit;
-            }
-            strcpy((char *)*resourceType, ((const char *)&leftToken[3]));
-            break;
+           caResult = CASelectNetwork(connTypes[i]);
+           if(caResult == CA_STATUS_OK)
+           {
+               retResult = CA_STATUS_OK;
+           }
         }
-        leftToken = strtok_r(NULL, "?", &strTokPtr);
     }
 
-    *newURI = tempURI;
+    if(retResult != CA_STATUS_OK)
+    {
+        return caResult; // Returns error of appropriate transport that failed fatally.
+    }
 
-    return OC_STACK_OK;
+    return retResult;
+}
 
-    exit:
-        return OC_STACK_NO_MEMORY;
+OCStackResult CAResultToOCResult(CAResult_t caResult)
+{
+    switch (caResult)
+    {
+        case CA_STATUS_OK:
+            return OC_STACK_OK;
+        case CA_STATUS_INVALID_PARAM:
+            return OC_STACK_INVALID_PARAM;
+        case CA_ADAPTER_NOT_ENABLED:
+            return OC_STACK_ADAPTER_NOT_ENABLED;
+        case CA_SERVER_STARTED_ALREADY:
+            return OC_STACK_OK;
+        case CA_SERVER_NOT_STARTED:
+            return OC_STACK_ERROR;
+        case CA_DESTINATION_NOT_REACHABLE:
+            return OC_STACK_COMM_ERROR;
+        case CA_SOCKET_OPERATION_FAILED:
+            return OC_STACK_COMM_ERROR;
+        case CA_SEND_FAILED:
+            return OC_STACK_COMM_ERROR;
+        case CA_RECEIVE_FAILED:
+            return OC_STACK_COMM_ERROR;
+        case CA_MEMORY_ALLOC_FAILED:
+            return OC_STACK_NO_MEMORY;
+        case CA_REQUEST_TIMEOUT:
+            return OC_STACK_TIMEOUT;
+        case CA_DESTINATION_DISCONNECTED:
+            return OC_STACK_COMM_ERROR;
+        case CA_STATUS_FAILED:
+            return OC_STACK_ERROR;
+        case CA_NOT_SUPPORTED:
+            return OC_STACK_NOTIMPL;
+        default:
+            return OC_STACK_ERROR;
+    }
 }