Beispiel #1
0
OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
        OCQualityOfService qos)
#endif
{
    OC_LOG(INFO, TAG, PCF("Entering SendObserverNotification"));
    OCStackResult result = OC_STACK_ERROR;
    ResourceObserver * resourceObserver = serverObsList;
    uint8_t numObs = 0;
    OCServerRequest * request = NULL;
    OCEntityHandlerRequest ehRequest = {0};
    OCEntityHandlerResult ehResult = OC_EH_ERROR;

    // Find clients that are observing this resource
    while (resourceObserver)
    {
        if (resourceObserver->resource == resPtr)
        {
            numObs++;
            #ifdef WITH_PRESENCE
            if(method != OC_REST_PRESENCE)
            {
            #endif
                qos = DetermineObserverQoS(method, resourceObserver, qos);
                result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
                        0, resPtr->sequenceNum, qos, resourceObserver->query,
                        NULL, NULL,
                        &resourceObserver->token, resourceObserver->addr,
                        resourceObserver->resUri, 0);
                request->observeResult = OC_STACK_OK;
                if(request && result == OC_STACK_OK)
                {
                    result = FormOCEntityHandlerRequest(&ehRequest, (OCRequestHandle) request,
                                request->method, (OCResourceHandle) resPtr, request->query,
                                request->reqJSONPayload, request->numRcvdVendorSpecificHeaderOptions,
                                request->rcvdVendorSpecificHeaderOptions, OC_OBSERVE_NO_OPTION, 0);
                    if(result == OC_STACK_OK)
                    {
                        ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest);
                        if(ehResult == OC_EH_ERROR)
                        {
                            FindAndDeleteServerRequest(request);
                        }
                    }
                }
            #ifdef WITH_PRESENCE
            }
            else
            {
                OCEntityHandlerResponse ehResponse = {0};
                unsigned char presenceResBuf[MAX_RESPONSE_LENGTH] = {0};
                //This is effectively the implementation for the presence entity handler.
                OC_LOG(DEBUG, TAG, PCF("This notification is for Presence"));
                result = AddServerRequest(&request, 0, 0, 0, 1, OC_REST_GET,
                        0, OC_OBSERVE_NO_OPTION, OC_LOW_QOS,
                        NULL, NULL, NULL, &resourceObserver->token,
                        resourceObserver->addr, resourceObserver->resUri, 0);
                if(result == OC_STACK_OK)
                {
                    // we create the payload here
                    if(resourceType && resourceType->resourcetypename)
                    {
                        snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u:%s",
                                resPtr->sequenceNum, maxAge, resourceType->resourcetypename);
                    }
                    else
                    {
                        snprintf((char *)presenceResBuf, sizeof(presenceResBuf), "%u:%u",
                                resPtr->sequenceNum, maxAge);
                    }
                    ehResponse.ehResult = OC_EH_OK;
                    ehResponse.payload = presenceResBuf;
                    ehResponse.payloadSize = strlen((const char *)presenceResBuf) + 1;
                    ehResponse.persistentBufferFlag = 0;
                    ehResponse.requestHandle = (OCRequestHandle) request;
                    ehResponse.resourceHandle = (OCResourceHandle) resPtr;
                    strcpy((char *)ehResponse.resourceUri, (const char *)resourceObserver->resUri);
                    result = OCDoResponse(&ehResponse);
                }
            }
            #endif
        }
        resourceObserver = resourceObserver->next;
    }
    if (numObs == 0)
    {
        OC_LOG(INFO, TAG, PCF("Resource has no observers"));
        result = OC_STACK_NO_OBSERVERS;
    }
    return result;
}
OCStackResult SendAllObserverNotification (OCMethod method, OCResource *resPtr, uint32_t maxAge,
        OCQualityOfService qos)
#endif
{
    OC_LOG(INFO, TAG, "Entering SendObserverNotification");
    if(!resPtr)
    {
        return OC_STACK_INVALID_PARAM;
    }

    OCStackResult result = OC_STACK_ERROR;
    ResourceObserver * resourceObserver = serverObsList;
    uint8_t numObs = 0;
    OCServerRequest * request = NULL;
    OCEntityHandlerRequest ehRequest = {0};
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    bool observeErrorFlag = false;

    // Find clients that are observing this resource
    while (resourceObserver)
    {
        if (resourceObserver->resource == resPtr)
        {
            numObs++;
#ifdef WITH_PRESENCE
            if(method != OC_REST_PRESENCE)
            {
#endif
                qos = DetermineObserverQoS(method, resourceObserver, qos);

                result = AddServerRequest(&request, 0, 0, 1, OC_REST_GET,
                        0, resPtr->sequenceNum, qos, resourceObserver->query,
                        NULL, NULL,
                        resourceObserver->token, resourceObserver->tokenLength,
                        resourceObserver->resUri, 0, resourceObserver->acceptFormat,
                        &resourceObserver->devAddr);

                if(request)
                {
                    request->observeResult = OC_STACK_OK;
                    if(result == OC_STACK_OK)
                    {
                        result = FormOCEntityHandlerRequest(
                                    &ehRequest,
                                    (OCRequestHandle) request,
                                    request->method,
                                    &request->devAddr,
                                    (OCResourceHandle) resPtr,
                                    request->query,
                                    PAYLOAD_TYPE_REPRESENTATION,
                                    request->payload,
                                    request->payloadSize,
                                    request->numRcvdVendorSpecificHeaderOptions,
                                    request->rcvdVendorSpecificHeaderOptions,
                                    OC_OBSERVE_NO_OPTION,
                                    0);
                        if(result == OC_STACK_OK)
                        {
                            ehResult = resPtr->entityHandler(OC_REQUEST_FLAG, &ehRequest,
                                                resPtr->entityHandlerCallbackParam);
                            if(ehResult == OC_EH_ERROR)
                            {
                                FindAndDeleteServerRequest(request);
                            }
                        }
                        OCPayloadDestroy(ehRequest.payload);
                    }
                }
#ifdef WITH_PRESENCE
            }
            else
            {
                OCEntityHandlerResponse ehResponse = {0};

                //This is effectively the implementation for the presence entity handler.
                OC_LOG(DEBUG, TAG, "This notification is for Presence");
                result = AddServerRequest(&request, 0, 0, 1, OC_REST_GET,
                        0, resPtr->sequenceNum, qos, resourceObserver->query,
                        NULL, NULL,
                        resourceObserver->token, resourceObserver->tokenLength,
                        resourceObserver->resUri, 0, resourceObserver->acceptFormat,
                        &resourceObserver->devAddr);

                if(result == OC_STACK_OK)
                {
                    OCPresencePayload* presenceResBuf = OCPresencePayloadCreate(
                            resPtr->sequenceNum, maxAge, trigger,
                            resourceType ? resourceType->resourcetypename : NULL);

                    if(!presenceResBuf)
                    {
                        return OC_STACK_NO_MEMORY;
                    }

                    if(result == OC_STACK_OK)
                    {
                        ehResponse.ehResult = OC_EH_OK;
                        ehResponse.payload = (OCPayload*)presenceResBuf;
                        ehResponse.persistentBufferFlag = 0;
                        ehResponse.requestHandle = (OCRequestHandle) request;
                        ehResponse.resourceHandle = (OCResourceHandle) resPtr;
                        OICStrcpy(ehResponse.resourceUri, sizeof(ehResponse.resourceUri),
                                resourceObserver->resUri);
                        result = OCDoResponse(&ehResponse);
                    }

                    OCPresencePayloadDestroy(presenceResBuf);
                }
            }
#endif

            // Since we are in a loop, set an error flag to indicate at least one error occurred.
            if (result != OC_STACK_OK)
            {
                observeErrorFlag = true;
            }
        }
        resourceObserver = resourceObserver->next;
    }

    if (numObs == 0)
    {
        OC_LOG(INFO, TAG, "Resource has no observers");
        result = OC_STACK_NO_OBSERVERS;
    }
    else if (observeErrorFlag)
    {
        OC_LOG(ERROR, TAG, "Observer notification error");
        result = OC_STACK_ERROR;
    }
    return result;
}