CONPRO-1320] Cleanup observers of deleted resource.
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocobserve.c
index 9fde469..18de8c6 100644 (file)
@@ -767,6 +767,30 @@ OCStackResult DeleteObserverUsingDevAddr(const OCDevAddr *devAddr)
     return OC_STACK_OK;
 }
 
+OCStackResult DeleteObserverUsingResource(OCResource *res)
+{
+    if (!res)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    oc_mutex_lock(g_serverObsListMutex);
+    ResourceObserver *obs = NULL;
+    ResourceObserver *next = NULL;
+    LL_FOREACH_SAFE(g_serverObsList, obs, next)
+    {
+        if (obs->resource == res)
+        {
+            OIC_LOG_V(INFO, TAG, "Deleting observer: id-%u, token-", obs->observeId);
+            OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obs->token, obs->tokenLength);
+            LL_DELETE(g_serverObsList, obs);
+            FreeObserver(obs);
+        }
+    }
+    oc_mutex_unlock(g_serverObsListMutex);
+    return OC_STACK_OK;
+}
+
 void DeleteObserverList()
 {
     oc_mutex_lock(g_serverObsListMutex);