예제 #1
0
    void parseServerHeaderOptions(OCClientResponse* clientResponse,
                    HeaderOptions& serverHeaderOptions)
    {
        if(clientResponse)
        {
            // Parse header options from server
            uint16_t optionID;
            std::string optionData;

            for(int i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
            {
                optionID = clientResponse->rcvdVendorSpecificHeaderOptions[i].optionID;
                optionData = reinterpret_cast<const char*>
                                (clientResponse->rcvdVendorSpecificHeaderOptions[i].optionData);
                HeaderOption::OCHeaderOption headerOption(optionID, optionData);
                serverHeaderOptions.push_back(headerOption);
            }
        }
        else
        {
            // clientResponse is invalid
            // TODO check proper logging
            std::cout << " Invalid response " << std::endl;
        }
    }
예제 #2
0
void formResourceRequest(OCEntityHandlerFlag flag,
                         OCEntityHandlerRequest * entityHandlerRequest,
                         std::shared_ptr<OCResourceRequest> pRequest)
{
    if(pRequest && entityHandlerRequest)
    {
        pRequest->setRequestHandle(entityHandlerRequest->requestHandle);
        pRequest->setResourceHandle(entityHandlerRequest->resource);
        pRequest->setMessageID(entityHandlerRequest->messageID);
    }

    if(flag & OC_REQUEST_FLAG)
    {
        pRequest->setRequestHandlerFlag(OC::RequestHandlerFlag::RequestFlag);

        if(entityHandlerRequest)
        {
            if(entityHandlerRequest->query)
            {
                OC::Utilities::QueryParamsKeyVal qp = OC::Utilities::getQueryParams(
                        entityHandlerRequest->query);

                if(qp.size() > 0)
                {
                    pRequest->setQueryParams(qp);
                }
            }
            if(entityHandlerRequest->numRcvdVendorSpecificHeaderOptions != 0)
            {
                //Set the header options here.
                uint16_t optionID;
                std::string optionData;
                HeaderOptions headerOptions;

                for(int i = 0;
                    i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions;
                    i++)
                {
                    optionID = entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionID;
                    optionData = reinterpret_cast<const char*>
                             (entityHandlerRequest->rcvdVendorSpecificHeaderOptions[i].optionData);
                    HeaderOption::OCHeaderOption headerOption(optionID, optionData);
                    headerOptions.push_back(headerOption);
                }
                pRequest->setHeaderOptions(headerOptions);
            }

            if(OC_REST_GET == entityHandlerRequest->method)
            {
                pRequest->setRequestType(OC::PlatformCommands::GET);
            }
            else if(OC_REST_PUT == entityHandlerRequest->method)
            {
                pRequest->setRequestType(OC::PlatformCommands::PUT);
                pRequest->setPayload(entityHandlerRequest->payload);
            }
            else if(OC_REST_POST == entityHandlerRequest->method)
            {
                pRequest->setRequestType(OC::PlatformCommands::POST);
                pRequest->setPayload(entityHandlerRequest->payload);
            }
            else if(OC_REST_DELETE == entityHandlerRequest->method)
            {
                pRequest->setRequestType(OC::PlatformCommands::DELETE);
            }
        }
    }

    if(flag & OC_OBSERVE_FLAG)
    {
        pRequest->setRequestHandlerFlag(
                   OC::RequestHandlerFlag::RequestFlag | OC::RequestHandlerFlag::ObserverFlag);

        if(entityHandlerRequest)
        {
            OC::ObservationInfo observationInfo;
            observationInfo.action = (OC::ObserveAction) entityHandlerRequest->obsInfo.action;
            observationInfo.obsId = entityHandlerRequest->obsInfo.obsId;

            observationInfo.connectivityType = static_cast<OCConnectivityType>(
                    (entityHandlerRequest->devAddr.adapter << CT_ADAPTER_SHIFT) |
                    (entityHandlerRequest->devAddr.flags & CT_MASK_FLAGS));
            observationInfo.address = entityHandlerRequest->devAddr.addr;
            observationInfo.port = entityHandlerRequest->devAddr.port;
            pRequest->setObservationInfo(observationInfo);
        }
    }
}
예제 #3
0
    void foundDevice(std::shared_ptr<OCResource> resource)
    {
        using namespace OC::OCPlatform;
        if(resource && resource->uri() == "/device")
        {
            std::cout << "Discovered a device object"<<std::endl;
            std::cout << "\tHost: "<<resource->host()<<std::endl;
            std::cout << "\tURI:  "<<resource->uri() <<std::endl;
        }

        // we have now found a resource, so lets create a few resource objects
        // for the other resources that we KNOW are associated with the intel.fridge
        // server, and query them.
        std::vector<std::string> lightTypes = {"intel.fridge.light"};
        std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
        OCResource::Ptr light = constructResourceObject(resource->host(),
                                "/light", m_connectivityType, false, lightTypes, ifaces);

        if(!light)
        {
            std::cout << "Error: Light Resource Object construction returned null\n";
            return;
        }

        std::vector<std::string> doorTypes = {"intel.fridge.door"};
        OCResource::Ptr leftdoor = constructResourceObject(resource->host(),
                                "/door/left", m_connectivityType, false, doorTypes, ifaces);

        if(!leftdoor)
        {
            std::cout << "Error: Left Door Resource Object construction returned null\n";
            return;
        }

        OCResource::Ptr rightdoor = constructResourceObject(resource->host(),
                                "/door/right", m_connectivityType, false, doorTypes, ifaces);

        if(!rightdoor)
        {
            std::cout << "Error: Right Door Resource Object construction returned null\n";
            return;
        }

        OCResource::Ptr randomdoor = constructResourceObject(resource->host(),
                                "/door/random", m_connectivityType, false, doorTypes, ifaces);
        if(!randomdoor)
        {
            std::cout << "Error: Random Door Resource Object construction returned null\n";
            return;
        }

        // Set header options with API version and token
        HeaderOptions headerOptions;
        try
        {
            // Set API version and client token
            HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0");
            HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf");
            headerOptions.push_back(apiVersion);
            headerOptions.push_back(clientToken);
        }
        catch(OCException& e)
        {
            std::cout << "Error creating HeaderOption: " << e.what() << std::endl;
        }


        // Setting header options will send above options in all requests
        // Header options are set per resource.
        // Below, header options are set only for device resource
        resource->setHeaderOptions(headerOptions);

        ++m_callsMade;
        resource->get(QueryParamsMap(), GetCallback(
                std::bind(&ClientFridge::getResponse, this, "Device", PH::_1,
                    PH::_2, PH::_3, resource, 0)
                ));
        ++m_callsMade;
        light->get(QueryParamsMap(), GetCallback(
                std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1,
                    PH::_2, PH::_3, light, 1)
                ));
        ++m_callsMade;
        leftdoor->get(QueryParamsMap(), GetCallback(
                std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1,
                    PH::_2, PH::_3, leftdoor, 2)
                ));
        ++m_callsMade;
        rightdoor->get(QueryParamsMap(), GetCallback(
                std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1,
                    PH::_2, PH::_3, rightdoor, 3)
                ));
        ++m_callsMade;
        randomdoor->get(QueryParamsMap(), GetCallback(
                std::bind(&ClientFridge::getResponse, this, "Random Door", PH::_1,
                    PH::_2, PH::_3, randomdoor, 4)
                ));
        ++m_callsMade;
        resource->deleteResource(DeleteCallback(
                std::bind(&ClientFridge::deleteResponse, this, "Device", PH::_1,
                    PH::_2, resource, 0)
                ));
    }
예제 #4
0
    virtual OCEntityHandlerResult entityHandler(std::shared_ptr<OCResourceRequest> request)
    {
        OCEntityHandlerResult ehResult = OC_EH_ERROR;
        if(request)
        {
            // Get the header options from the request
            HeaderOptions headerOptions = request->getHeaderOptions();
            std::string clientAPIVersion;
            std::string clientToken;

            // Search the header options map and look for API version and Client token
            for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
            {
                uint16_t optionID = it->getOptionID();
                if(optionID == API_VERSION)
                {
                    clientAPIVersion = it->getOptionData();
                    std::cout << " Client API version: " << clientAPIVersion << std::endl;
                }
                else if(optionID == TOKEN)
                {
                    clientToken = it->getOptionData();
                    std::cout << " Client token: " << clientToken << std::endl;
                }
                else
                {
                    std::cout << " Invalid header option " << std::endl;
                }
            }

            // In this case Server entity handler verifies API version
            // and client token. If they are valid, client requests are handled.
            if(clientAPIVersion == FRIDGE_CLIENT_API_VERSION && clientToken == FRIDGE_CLIENT_TOKEN)
            {
                HeaderOptions serverHeaderOptions;
                try
                {
                    // Set API version from server side
                    HeaderOption::OCHeaderOption apiVersion(API_VERSION, FRIDGE_CLIENT_API_VERSION);
                    serverHeaderOptions.push_back(apiVersion);
                }
                catch(OCException& e)
                {
                    std::cout << "Error creating HeaderOption in server: " << e.what() << std::endl;
                }

                if(request->getRequestHandlerFlag() == RequestHandlerFlag::RequestFlag)
                {
                    auto pResponse = std::make_shared<OC::OCResourceResponse>();
                    pResponse->setRequestHandle(request->getRequestHandle());
                    pResponse->setResourceHandle(request->getResourceHandle());
                    pResponse->setHeaderOptions(serverHeaderOptions);

                    if(request->getRequestType() == "GET")
                    {
                        std::cout<<"DeviceResource Get Request"<<std::endl;
                        pResponse->setErrorCode(200);
                        pResponse->setResponseResult(OC_EH_OK);
                        pResponse->setResourceRepresentation(get(), "");
                        if(OC_STACK_OK == OCPlatform::sendResponse(pResponse))
                        {
                            ehResult = OC_EH_OK;
                        }
                    }
                    else if(request->getRequestType() == "DELETE")
                    {
                        std::cout<<"DeviceResource Delete Request"<<std::endl;
                        if(deleteDeviceResource() == OC_STACK_OK)
                        {
                            pResponse->setErrorCode(200);
                            pResponse->setResponseResult(OC_EH_RESOURCE_DELETED);
                            ehResult = OC_EH_OK;
                        }
                        else
                        {
                            pResponse->setResponseResult(OC_EH_ERROR);
                            ehResult = OC_EH_ERROR;
                        }
                        OCPlatform::sendResponse(pResponse);
                    }
                    else
                    {
                        std::cout <<"DeviceResource unsupported request type "
                        << request->getRequestType() << std::endl;
                        pResponse->setResponseResult(OC_EH_ERROR);
                        OCPlatform::sendResponse(pResponse);
                        ehResult = OC_EH_ERROR;
                    }
                }
                else
                {
                    std::cout << "DeviceResource unsupported request flag" <<std::endl;
                }
            }
            else
            {
                std::cout << "Unsupported/invalid header options/values" << std::endl;
            }
        }

        return ehResult;
    }