[CONPRO-1429] 100% CPU usage by iotivity stack
[platform/upstream/iotivity.git] / resource / csdk / stack / src / ocobserve.c
index 18de8c6..7495f2b 100644 (file)
 
 #define VERIFY_NON_NULL(arg) { if (!arg) {OIC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
 
+#define MAX_OBSERVERS 3840
+
 static struct ResourceObserver * g_serverObsList = NULL;
 static oc_mutex g_serverObsListMutex = NULL;
 
+static int observer_count = 0;
+
 static ResourceObserver* GetObserverUsingIdAsOwner (const OCObservationId observeId);
 
 static ResourceObserver* CloneObserverNode (ResourceObserver* observer)
@@ -255,7 +259,7 @@ OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr,
 
     OCStackResult result = OC_STACK_ERROR;
     ResourceObserver * resourceObserver = NULL;
-    uint8_t numObs = 0;
+    uint16_t numObs = 0;
     OCServerRequest * request = NULL;
     bool observeErrorFlag = false;
 
@@ -347,7 +351,7 @@ OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr,
 }
 
 OCStackResult SendListObserverNotification (OCResource * resource,
-        OCObservationId  *obsIdList, uint8_t numberOfIds,
+        OCObservationId  *obsIdList, uint16_t numberOfIds,
         const OCRepPayload *payload,
         uint32_t maxAge,
         OCQualityOfService qos)
@@ -358,9 +362,9 @@ OCStackResult SendListObserverNotification (OCResource * resource,
         return OC_STACK_INVALID_PARAM;
     }
 
-    uint8_t numIds = numberOfIds;
+    uint16_t numIds = numberOfIds;
     ResourceObserver *observer = NULL;
-    uint8_t numSentNotification = 0;
+    uint16_t numSentNotification = 0;
     OCServerRequest * request = NULL;
     OCStackResult result = OC_STACK_ERROR;
     bool observeErrorFlag = false;
@@ -464,19 +468,32 @@ OCStackResult GenerateObserverId (OCObservationId *observationId)
     OIC_LOG(INFO, TAG, "Entering GenerateObserverId");
     VERIFY_NON_NULL (observationId);
 
-    do
+    oc_mutex_lock(g_serverObsListMutex);
+
+    if (observer_count < MAX_OBSERVERS)
     {
+        oc_mutex_unlock(g_serverObsListMutex);
         do
         {
-            *observationId = OCGetRandomByte();
-        } while (0 == *observationId); //Make sure *observationId is not 0
-        // Check if observation Id already exists
-        found = IsObserverAvailable (*observationId);
-    } while (found);
-
+            do
+            {
+                *observationId = OCGetRandomTwoByte();
+            } while (0 == *observationId); //Make sure *observationId is not 0
+            // Check if observation Id already exists
+            found = IsObserverAvailable (*observationId);
+        } while (found);
     OIC_LOG_V(INFO, TAG, "GeneratedObservation ID is %u", *observationId);
+    //oc_mutex_unlock(g_serverObsListMutex);
 
     return OC_STACK_OK;
+    }
+    else
+    {
+        OIC_LOG_V(ERROR, TAG, "No more observers can be added");
+        oc_mutex_unlock(g_serverObsListMutex);
+
+        return OC_STACK_ERROR;
+    }
 exit:
     return OC_STACK_ERROR;
 }
@@ -547,6 +564,7 @@ OCStackResult AddObserver (const char         *resUri,
 
         oc_mutex_lock(g_serverObsListMutex);
         LL_APPEND (g_serverObsList, obsNode);
+        observer_count++;
         oc_mutex_unlock(g_serverObsListMutex);
 
         return OC_STACK_OK;
@@ -728,6 +746,7 @@ OCStackResult DeleteObserverUsingToken (CAToken_t token, uint8_t tokenLength)
         OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)obsNode->token, tokenLength);
 
         LL_DELETE (g_serverObsList, obsNode);
+        observer_count--;
         FreeObserver(obsNode);
     }
     oc_mutex_unlock(g_serverObsListMutex);