size_t tokenLength,
OCDevAddr *addr,
OCResource *resHandle);
+OCStackResult DeleteObserver (uint8_t *token, size_t tokenLength);
OCStackResult ProcessObserveRequest (OCResource *resource, OCRequest *request)
{
OCObserveReq *obs = request->observe;
OC_LOG(INFO, TAG, "Entering ProcessObserveRequest");
- printf ("Observer option: %s\n", obs->option);
if (strcmp ((char *)obs->option, OC_RESOURCE_OBSERVE_REGISTER) == 0) {
+ if (NULL == resource)
+ {
+ return OC_STACK_ERROR;
+ }
// Register new observation
- printf ("Register new observation\n\n");
+ request->entityHandlerRequest->resource = (OCResourceHandle)resource;
result = resource->entityHandler(OC_OBSERVE_FLAG, request->entityHandlerRequest);
if (OC_STACK_OK == result)
{
// Add subscriber to the server observation list
result = AddObserver ((const char*)(request->resourceUrl), (const char *)(ehReq->query),
- obs->coapToken, obs->coapTokenLen, obs->subAddr, resource);
+ obs->token->token, obs->token->tokenLength, obs->subAddr, resource);
}
return result;
- } else if (strcmp ((char *)request->observe, OC_RESOURCE_OBSERVE_DEREGISTER) == 0) {
+ } else if (strcmp ((char *)obs->option, OC_RESOURCE_OBSERVE_DEREGISTER) == 0) {
// Deregister observation
- return OC_STACK_NOTIMPL;
+ result = DeleteObserver (obs->token->token, obs->token->tokenLength);
+ return result;
} else {
// Invalid option
+ OC_LOG(ERROR, TAG, "Invalid CoAP observe option");
return OC_STACK_INVALID_OBSERVE_PARAM;
}
}
result = resPtr->entityHandler (OC_REQUEST_FLAG, &entityHandlerReq);
if (OC_STACK_OK == result)
{
- printf ("Calling OCCoAPSendMessage: Payload: %s\n", entityHandlerReq.resJSONPayload);
- OCCoAPSendMessage (obsRes->addr, result, qos, obsRes->coapTok,
+ OCCoAPSendMessage (obsRes->addr, result, qos, obsRes->token,
(const char *)entityHandlerReq.resJSONPayload,
resPtr->sequenceNum);
}
obsNode->query = (unsigned char *)OCMalloc(sizeof(strlen(query)+1));
VERIFY_NON_NULL (obsNode->query);
memcpy (obsNode->query, query, sizeof(strlen(query)+1));
- obsNode->coapTok = (OCCoAPToken *)OCMalloc(sizeof(OCCoAPToken));
- VERIFY_NON_NULL (obsNode->coapTok);
- tokPtr = obsNode->coapTok;
+ obsNode->token = (OCCoAPToken *)OCMalloc(sizeof(OCCoAPToken));
+ VERIFY_NON_NULL (obsNode->token);
+ tokPtr = obsNode->token;
memcpy (tokPtr->token, token, sizeof(OCCoAPToken));
tokPtr->tokenLength = tokenLength;
obsNode->addr = (OCDevAddr *)OCMalloc(sizeof(OCDevAddr));
exit:
OCFree(obsNode->resUri);
OCFree(obsNode->query);
- OCFree(obsNode->coapTok);
+ OCFree(obsNode->token);
OCFree(obsNode->addr);
OCFree(obsNode);
return OC_STACK_NO_MEMORY;
}
+
+ObserveResourceServer* GetObserver (const uint8_t *token, const size_t tokenLength)
+{
+ ObserveResourceServer *out = NULL;
+
+ if(token)
+ {
+ LL_FOREACH (serverObsList, out)
+ {
+ if((out->token->tokenLength == tokenLength) &&
+ (memcmp(out->token->token, token, tokenLength) == 0)) {
+ return out;
+ }
+ }
+ }
+ OC_LOG(INFO, MOD_NAME, PCF("Observer node not found!!"));
+ return NULL;
+}
+
+OCStackResult DeleteObserver (uint8_t *token, size_t tokenLength)
+{
+ ObserveResourceServer *obsNode = NULL;
+
+ obsNode = GetObserver (token, tokenLength);
+ if (obsNode) {
+ LL_DELETE (serverObsList, obsNode);
+ OCFree(obsNode->resUri);
+ OCFree(obsNode->query);
+ OCFree(obsNode->token);
+ OCFree(obsNode->addr);
+ OCFree(obsNode);
+ return OC_STACK_OK;
+ }
+ return OC_STACK_ERROR;
+}
+
+void DeleteObserverList()
+{
+ ObserveResourceServer *out;
+ ObserveResourceServer *tmp;
+ LL_FOREACH_SAFE (serverObsList, out, tmp)
+ {
+ DeleteObserver (out->token->token, out->token->tokenLength);
+ }
+ serverObsList = NULL;
+}