Make OCProcessEvent method.
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocstack.c
index e3aef64..63eef55 100644 (file)
@@ -155,6 +155,10 @@ static const char CORESPEC[] = "core";
 static OCOtmEventHandler_t g_otmEventHandler = {NULL, NULL};
 #endif
 
+#ifdef WITH_PROCESS_EVENT
+static oc_event g_ocProcessEvent = NULL;
+#endif // WITH_PROCESS_EVENT
+
 //-----------------------------------------------------------------------------
 // Macros
 //-----------------------------------------------------------------------------
@@ -3145,6 +3149,9 @@ OCStackResult OCDoRequest(OCDoHandle *handle,
         {
             *handle = resHandle;
         }
+#ifdef WITH_PROCESS_EVENT
+        OCSendProcessEventSignal();
+#endif // WITH_PROCESS_EVENT
 
         goto exit;
     }
@@ -3329,7 +3336,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;
 
@@ -3390,8 +3401,15 @@ OCStackResult OCProcessPresence()
             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;
         }
 
@@ -3429,6 +3447,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)
@@ -3450,6 +3518,7 @@ OCStackResult OCProcess()
 #endif
     return OC_STACK_OK;
 }
+#endif // !WITH_PROCESS_EVENT
 
 #ifdef WITH_PRESENCE
 OCStackResult OCStartPresence(const uint32_t ttl)
@@ -5499,7 +5568,7 @@ OCStackResult OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t numOptions, uin
 void OCDefaultAdapterStateChangedHandler(CATransportAdapter_t adapter, bool enabled)
 {
     OIC_LOG(DEBUG, TAG, "OCDefaultAdapterStateChangedHandler");
-    
+
     OC_UNUSED(adapter);
     OC_UNUSED(enabled);
 }