#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)
{
ehResult = observer->resource->entityHandler(OC_REQUEST_FLAG, &ehRequest,
observer->resource->entityHandlerCallbackParam);
- if (ehResult == OC_EH_ERROR)
+
+ // Clear server request on error case
+ if (!OCResultToSuccess(EntityHandlerCodeToOCStackCode(ehResult)))
{
FindAndDeleteServerRequest(request);
}
OCStackResult result = OC_STACK_ERROR;
ResourceObserver * resourceObserver = NULL;
- uint8_t numObs = 0;
+ uint16_t numObs = 0;
OCServerRequest * request = NULL;
bool observeErrorFlag = false;
OICStrcpy(ehResponse.resourceUri, sizeof(ehResponse.resourceUri),
resourceObserver->resUri);
result = OCDoResponse(&ehResponse);
+ if (result != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to send presence notification!");
+ FindAndDeleteServerRequest(request);
+ }
}
OCPresencePayloadDestroy(presenceResBuf);
}
OCStackResult SendListObserverNotification (OCResource * resource,
- OCObservationId *obsIdList, uint8_t numberOfIds,
+ OCObservationId *obsIdList, uint16_t numberOfIds,
const OCRepPayload *payload,
uint32_t maxAge,
OCQualityOfService qos)
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;
// Increment only if OCDoResponse is successful
numSentNotification++;
-
- OICFree(ehResponse.payload);
- FindAndDeleteServerRequest(request);
}
else
{
OIC_LOG_V(INFO, TAG, "Error notifying observer id %d.", *obsIdList);
+ FindAndDeleteServerRequest(request);
}
+
// Reset Observer TTL.
observer->TTL =
GetTicks(MAX_OBSERVER_TTL_SECONDS * MILLISECONDS_PER_SECOND);
+
+ OICFree(ehResponse.payload);
}
else
{
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;
}
oc_mutex_lock(g_serverObsListMutex);
LL_APPEND (g_serverObsList, obsNode);
+ observer_count++;
oc_mutex_unlock(g_serverObsListMutex);
return OC_STACK_OK;
{
if (out->observeId == observeId)
{
+ OIC_LOG(INFO, TAG, "Found in observer list");
+ ResourceObserver *observer = CloneObserverNode(out);
oc_mutex_unlock(g_serverObsListMutex);
- return CloneObserverNode(out);
+ return observer;
}
CheckTimedOutObserver(out);
}
if ((memcmp(out->token, token, tokenLength) == 0))
{
OIC_LOG(INFO, TAG, "Found in observer list");
+ ResourceObserver *observer = CloneObserverNode(out);
oc_mutex_unlock(g_serverObsListMutex);
- return CloneObserverNode(out);
+ return observer;
}
CheckTimedOutObserver(out);
}
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);
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);
return OC_STACK_OK;
}
-OCStackResult InitializeObseverList()
+OCStackResult InitializeObserverList()
{
- OIC_LOG(DEBUG, TAG, "InitializeObseverList IN");
+ OIC_LOG(DEBUG, TAG, "InitializeObserverList IN");
if (NULL == g_serverObsListMutex)
{
g_serverObsListMutex = oc_mutex_new();
}
- OIC_LOG(DEBUG, TAG, "InitializeObseverList OUT");
+ OIC_LOG(DEBUG, TAG, "InitializeObserverList OUT");
return OC_STACK_OK;
}
{
OIC_LOG(DEBUG, TAG, "TerminateObserverList IN");
- DeleteObserverList();
-
if (NULL != g_serverObsListMutex)
{
oc_mutex_free(g_serverObsListMutex);