コード例 #1
0
ファイル: fridgeclient.cpp プロジェクト: EmuxEvans/iotivity
 //Function to print the headerOptions received from the server
 void printHeaderOptions(const HeaderOptions& headerOptions)
 {
     for (auto it = headerOptions.begin(); it != headerOptions.end(); ++it)
     {
         if(it->getOptionID() == API_VERSION)
         {
             std::cout << "Server API version in GET response: " <<
                     it->getOptionData() << std::endl;
         }
     }
 }
コード例 #2
0
void InProcClientWrapper::assembleHeaderOptions(OCHeaderOption options[],
        const HeaderOptions& headerOptions)
{
    int i = 0;

    for (auto it=headerOptions.begin(); it != headerOptions.end(); ++it)
    {
        options[i].protocolID = OC_COAP_ID;
        options[i].optionID = static_cast<uint16_t>(it->getOptionID());
        options[i].optionLength = (it->getOptionData()).length() + 1;
        memcpy(options[i].optionData, (it->getOptionData()).c_str(),
               (it->getOptionData()).length() + 1);
        i++;
    }
}
コード例 #3
0
    OCStackResult InProcServerWrapper::sendResponse(
        const std::shared_ptr<OCResourceResponse> pResponse)
    {
        auto cLock = m_csdkLock.lock();
        OCStackResult result = OC_STACK_ERROR;

        if(!pResponse)
        {
            result = OC_STACK_MALFORMED_RESPONSE;
            throw OCException(OC::Exception::STR_NULL_RESPONSE, OC_STACK_MALFORMED_RESPONSE);
        }
        else
        {
            OCEntityHandlerResponse response;
//            OCRepPayload* payLoad = pResponse->getPayload();
            HeaderOptions serverHeaderOptions = pResponse->getHeaderOptions();

            response.requestHandle = pResponse->getRequestHandle();
            response.resourceHandle = pResponse->getResourceHandle();
            response.ehResult = pResponse->getResponseResult();

            response.payload = reinterpret_cast<OCPayload*>(pResponse->getPayload());

            response.persistentBufferFlag = 0;

            response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size();
            int i = 0;
            for (auto it=serverHeaderOptions.begin(); it != serverHeaderOptions.end(); ++it)
            {
                response.sendVendorSpecificHeaderOptions[i].protocolID = OC_COAP_ID;
                response.sendVendorSpecificHeaderOptions[i].optionID =
                    static_cast<uint16_t>(it->getOptionID());
                response.sendVendorSpecificHeaderOptions[i].optionLength =
                    (it->getOptionData()).length() + 1;
                std::string optionData = it->getOptionData();
                std::copy(optionData.begin(),
                         optionData.end(),
                         response.sendVendorSpecificHeaderOptions[i].optionData);
                response.sendVendorSpecificHeaderOptions[i].optionData[it->getOptionData().length()]
                    = '\0';
                i++;
            }

            if(OC_EH_RESOURCE_CREATED == response.ehResult)
            {
                pResponse->getNewResourceUri().copy(response.resourceUri,
                        sizeof (response.resourceUri) - 1);
                response.resourceUri[pResponse->getNewResourceUri().length()] = '\0';
            }

            if(cLock)
            {
                std::lock_guard<std::recursive_mutex> lock(*cLock);
                result = OCDoResponse(&response);
            }
            else
            {
                OICFree(response.payload);
                result = OC_STACK_ERROR;
            }

            if(result != OC_STACK_OK)
            {
                oclog() << "Error sending response\n";
            }
            return result;
        }
    }
コード例 #4
0
ファイル: fridgeserver.cpp プロジェクト: Frank-KunLi/iotivity
    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;
    }