Fix for server request with duplicate token issue
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocstack.c
index f5575b1..240920f 100644 (file)
@@ -61,6 +61,7 @@
 #include "ocpayload.h"
 #include "ocpayloadcbor.h"
 #include "cautilinterface.h"
+#include "camessagehandler.h"
 #include "oicgroup.h"
 
 #if defined (ROUTING_GATEWAY) || defined (ROUTING_EP)
@@ -151,13 +152,14 @@ static const char COAP_TCP_SCHEME[] = "coap+tcp:";
 static const char COAPS_TCP_SCHEME[] = "coaps+tcp:";
 static const char CORESPEC[] = "core";
 
-CAAdapterStateChangedCB g_adapterHandler = NULL;
-CAConnectionStateChangedCB g_connectionHandler = NULL;
-
 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
 static OCOtmEventHandler_t g_otmEventHandler = {NULL, NULL};
 #endif
 
+#ifdef WITH_PROCESS_EVENT
+static oc_event g_ocProcessEvent = NULL;
+#endif // WITH_PROCESS_EVENT
+
 //-----------------------------------------------------------------------------
 // Macros
 //-----------------------------------------------------------------------------
@@ -722,6 +724,9 @@ OCStackResult CAResponseToOCStackResult(CAResponseResult_t caCode)
         case CA_FORBIDDEN_REQ:
             ret = OC_STACK_FORBIDDEN_REQ;
             break;
+        case CA_TOO_MANY_REQUESTS:
+            ret = OC_STACK_TOO_MANY_REQUESTS;
+            break;
         case CA_INTERNAL_SERVER_ERROR:
             ret = OC_STACK_INTERNAL_SERVER_ERROR;
             break;
@@ -807,6 +812,9 @@ CAResponseResult_t OCToCAStackResult(OCStackResult ocCode, OCMethod method)
         case OC_STACK_FORBIDDEN_REQ:
             ret = CA_FORBIDDEN_REQ;
             break;
+        case OC_STACK_TOO_MANY_REQUESTS:
+            ret = CA_TOO_MANY_REQUESTS;
+            break;
         case OC_STACK_INTERNAL_SERVER_ERROR:
             ret = CA_INTERNAL_SERVER_ERROR;
             break;
@@ -1236,6 +1244,7 @@ OCStackResult HandlePresenceResponse(const CAEndpoint_t *endpoint,
                     OIC_LOG(ERROR, TAG,
                                   "Could not allocate memory for cbNode->presence->timeOut");
                     OICFree(cbNode->presence);
+                    cbNode->presence = NULL;
                     result = OC_STACK_NO_MEMORY;
                     goto exit;
                 }
@@ -2006,10 +2015,13 @@ OCStackResult HandleStackRequests(OCServerProtocolRequest * protocolRequest)
         {
             request->requestComplete = 1;
         }
+
+        OIC_LOG_V(WARNING, TAG, "Server request ID = [%u]", request->requestId);
     }
     else
     {
         OIC_LOG(INFO, TAG, "This is either a repeated or blocked Server Request");
+        return OC_STACK_DUPLICATE_REQUEST;
     }
 
     if(request->requestComplete)
@@ -3126,6 +3138,7 @@ OCStackResult OCDoRequest(OCDoHandle *handle,
         goto exit;
     }
 
+    cbData = NULL;        // Client CB list entry now owns it
     token = NULL;         // Client CB list entry now owns it
     devAddr = NULL;       // Client CB list entry now owns it
     resourceUri = NULL;   // Client CB list entry now owns it
@@ -3140,6 +3153,9 @@ OCStackResult OCDoRequest(OCDoHandle *handle,
         {
             *handle = resHandle;
         }
+#ifdef WITH_PROCESS_EVENT
+        OCSendProcessEventSignal();
+#endif // WITH_PROCESS_EVENT
 
         goto exit;
     }
@@ -3161,6 +3177,10 @@ exit:
     if (result != OC_STACK_OK)
     {
         OIC_LOG(ERROR, TAG, "OCDoResource error");
+        if (NULL != cbData && NULL != cbData->cd)
+        {
+            cbData->cd(cbData->context);
+        }
         FindAndDeleteClientCB(clientCB);
         CADestroyToken(token);
         if (handle)
@@ -3224,8 +3244,10 @@ OCStackResult OCCancel(OCDoHandle handle, OCQualityOfService qos, OCHeaderOption
 
             CopyDevAddrToEndpoint(clientCB->devAddr, &endpoint);
 
-            if ((endpoint.adapter & CA_ADAPTER_IP) && qos != OC_HIGH_QOS)
+            if (((endpoint.adapter & CA_ADAPTER_IP) && qos != OC_HIGH_QOS) ||
+                    ((endpoint.adapter & CA_ADAPTER_TCP) && OC_LOW_QOS_WITH_TCP == qos))
             {
+                OIC_LOG_V(INFO, TAG, "the %s observe callback is removed", clientCB->requestUri);
                 FindAndDeleteClientCB(clientCB);
                 break;
             }
@@ -3318,7 +3340,11 @@ OCStackResult OCRegisterPersistentStorageHandler(OCPersistentStorage* persistent
 
 #ifdef WITH_PRESENCE
 
-OCStackResult OCProcessPresence()
+#ifdef WITH_PROCESS_EVENT
+OCStackResult OCProcessPresence(uint32_t *nextEventTime)
+#else   // WITH_PROCESS_EVENT
+OCStackResult OCProcessPresence(void)
+#endif  // !WITH_PROCESS_EVENT
 {
     OCStackResult result = OC_STACK_OK;
 
@@ -3326,10 +3352,11 @@ OCStackResult OCProcessPresence()
     // to most purposes.  Uncomment as needed.
     //OIC_LOG(INFO, TAG, "Entering RequestPresence");
     ClientCB* cbNode = NULL;
+    ClientCB* tempcbNode = NULL;
     OCClientResponse clientResponse;
     OCStackApplicationResult cbResult = OC_STACK_DELETE_TRANSACTION;
 
-    LL_FOREACH(cbList, cbNode)
+    LL_FOREACH_SAFE(cbList, cbNode, tempcbNode)
     {
         if (OC_REST_PRESENCE != cbNode->method || !cbNode->presence)
         {
@@ -3375,10 +3402,18 @@ OCStackResult OCProcessPresence()
             {
                 FindAndDeleteClientCB(cbNode);
             }
+            continue;
         }
 
-        if (now < cbNode->presence->timeOut[cbNode->presence->TTLlevel])
+        uint32_t timeout = cbNode->presence->timeOut[cbNode->presence->TTLlevel];
+        if (now < timeout)
         {
+#ifdef WITH_PROCESS_EVENT
+            if (nextEventTime && (timeout - now) < *nextEventTime)
+            {
+                *nextEventTime = timeout - now;
+            }
+#endif // WITH_PROCESS_EVENT
             continue;
         }
 
@@ -3416,6 +3451,56 @@ exit:
 }
 #endif // WITH_PRESENCE
 
+#ifdef WITH_PROCESS_EVENT
+OCStackResult OCProcess(void)
+{
+    uint32_t nextEventTime;
+    return OCProcessEvent(&nextEventTime);
+}
+
+OCStackResult OCProcessEvent(uint32_t *nextEventTime)
+{
+    if (stackState == OC_STACK_UNINITIALIZED)
+    {
+        OIC_LOG(ERROR, TAG, "OCProcess has failed. ocstack is not initialized");
+        return OC_STACK_ERROR;
+    }
+
+    *nextEventTime = UINT32_MAX;
+
+#ifdef WITH_PRESENCE
+    OCProcessPresence(nextEventTime);
+    OIC_LOG_V(INFO, TAG, "OCProcessPresence next event time : %u", *nextEventTime);
+#endif
+    CAHandleRequestResponse();
+
+// TODO
+#ifdef ROUTING_GATEWAY
+    RMProcess(nextEventTime);
+#endif
+
+#ifdef TCP_ADAPTER
+    OCProcessKeepAlive(nextEventTime);
+    OIC_LOG_V(INFO, TAG, "OCProcessKeepAlive next event time : %u", *nextEventTime);
+#endif
+    return OC_STACK_OK;
+}
+
+void OCRegisterProcessEvent(oc_event event)
+{
+    g_ocProcessEvent = event;
+    CARegisterProcessEvent(event);
+}
+
+void OCSendProcessEventSignal(void)
+{
+    if (g_ocProcessEvent)
+    {
+        oc_event_signal(g_ocProcessEvent);
+    }
+}
+#else // WITH_PROCESS_EVENT
+
 OCStackResult OCProcess()
 {
     if (stackState == OC_STACK_UNINITIALIZED)
@@ -3437,6 +3522,7 @@ OCStackResult OCProcess()
 #endif
     return OC_STACK_OK;
 }
+#endif // !WITH_PROCESS_EVENT
 
 #ifdef WITH_PRESENCE
 OCStackResult OCStartPresence(const uint32_t ttl)
@@ -3995,6 +4081,37 @@ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
     return result;
 }
 
+OCStackResult OCResetResourceTypes(OCResourceHandle handle,
+                                   const char *newResourceType)
+{
+    OCStackResult result = OC_STACK_ERROR;
+    OCResource *resource = NULL;
+
+    resource = findResource((OCResource *) handle);
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
+        return OC_STACK_ERROR;
+    }
+
+    // Clear all bound resource types
+    deleteResourceType(resource->rsrcType);
+    resource->rsrcType = NULL;
+
+    // Bind new resource type to resource
+    result = BindResourceTypeToResource(resource, newResourceType);
+
+#ifdef WITH_PRESENCE
+    if(presenceResource.handle)
+    {
+        ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
+    }
+#endif
+
+    return result;
+}
+
 OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
         const char *resourceInterfaceName)
 {
@@ -4022,6 +4139,37 @@ OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle,
     return result;
 }
 
+OCStackResult OCResetResourceInterfaces(OCResourceHandle handle,
+                                        const char *newResourceInterface)
+{
+    OCStackResult result = OC_STACK_ERROR;
+    OCResource *resource = NULL;
+
+    resource = findResource((OCResource *) handle);
+    if (!resource)
+    {
+        OIC_LOG(ERROR, TAG, "Resource not found");
+        return OC_STACK_ERROR;
+    }
+
+    // Clear all bound interface
+    deleteResourceInterface(resource->rsrcInterface);
+    resource->rsrcInterface = NULL;
+
+    // Bind new interface to resource
+    result = BindResourceInterfaceToResource(resource, newResourceInterface);
+
+#ifdef WITH_PRESENCE
+    if (presenceResource.handle)
+    {
+        ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom();
+        SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE);
+    }
+#endif
+
+    return result;
+}
+
 OCStackResult OCGetNumberOfResources(uint8_t *numResources)
 {
     OCResource *pointer = headResource;
@@ -4635,6 +4783,9 @@ OCStackResult deleteResource(OCResource *resource)
                 SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_DELETE);
             }
 #endif
+            // Delete resource's all observers
+            DeleteObserverUsingResource(resource);
+
             // Only resource in list.
             if (temp == headResource && temp == tailResource)
             {
@@ -4703,6 +4854,8 @@ void deleteResourceElements(OCResource *resource)
         OCDeleteResourceAttributes(resource->rsrcAttributes);
         resource->rsrcAttributes = NULL;
     }
+
+    resource->entityHandler = NULL;
     resource = NULL;
 }
 
@@ -5419,19 +5572,14 @@ OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions, uin
 void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
 {
     OIC_LOG(DEBUG, TAG, "OCDefaultAdapterStateChangedHandler");
-    if (g_adapterHandler)
-    {
-        g_adapterHandler(adapter, enabled);
-    }
+
+    OC_UNUSED(adapter);
+    OC_UNUSED(enabled);
 }
 
 void OCDefaultConnectionStateChangedHandler(const CAEndpoint_t *info, bool isConnected)
 {
     OIC_LOG(DEBUG, TAG, "OCDefaultConnectionStateChangedHandler");
-    if (g_connectionHandler)
-    {
-       g_connectionHandler(info, isConnected);
-    }
 
     /*
      * If the client observes one or more resources over a reliable connection,
@@ -5497,6 +5645,17 @@ OCStackResult OCGetDeviceOwnedState(bool *isOwned)
     return ret;
 }
 
+OCStackResult OCGetDeviceOperationalState(bool* isOp)
+{
+    if(NULL != isOp)
+    {
+        *isOp = GetPstatIsop();
+        return OC_STACK_OK;
+    }
+
+    return OC_STACK_ERROR;
+}
+
 void OCClearCallBackList()
 {
     DeleteClientCBList();