Esempio n. 1
0
    OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle,
        const OCDevAddr& devAddr,
        const std::string& uri,
        const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
        ObserveCallback& callback, QualityOfService QoS)
    {
        if(!callback)
        {
            return OC_STACK_INVALID_PARAM;
        }
        OCStackResult result;

        ClientCallbackContext::ObserveContext* ctx =
            new ClientCallbackContext::ObserveContext(callback);
        OCCallbackData cbdata(
                static_cast<void*>(ctx),
                observeResourceCallback,
                [](void* c){delete static_cast<ClientCallbackContext::ObserveContext*>(c);}
                );

        OCMethod method;
        if (observeType == ObserveType::Observe)
        {
            method = OC_REST_OBSERVE;
        }
        else if (observeType == ObserveType::ObserveAll)
        {
            method = OC_REST_OBSERVE_ALL;
        }
        else
        {
            method = OC_REST_OBSERVE_ALL;
        }

        std::string url = assembleSetResourceUri(uri, queryParams).c_str();

        auto cLock = m_csdkLock.lock();

        if(cLock)
        {
            std::lock_guard<std::recursive_mutex> lock(*cLock);
            OCHeaderOption options[MAX_HEADER_OPTIONS];

            result = OCDoResource(handle, method,
                                  url.c_str(), &devAddr,
                                  nullptr,
                                  CT_DEFAULT,
                                  static_cast<OCQualityOfService>(QoS),
                                  &cbdata,
                                  assembleHeaderOptions(options, headerOptions),
                                  headerOptions.size());
        }
        else
        {
            delete ctx;
            return OC_STACK_ERROR;
        }

        return result;
    }
Esempio n. 2
0
OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle,
        const std::string& host, const std::string& uri, const QueryParamsMap& queryParams,
        const HeaderOptions& headerOptions, ObserveCallback& callback, QualityOfService QoS)
{
    OCStackResult result;
    OCCallbackData cbdata = {0};

    ClientCallbackContext::ObserveContext* ctx = new ClientCallbackContext::ObserveContext();
    ctx->callback = callback;
    cbdata.context = static_cast<void*>(ctx);
    cbdata.cb = &observeResourceCallback;
    cbdata.cd = [](void* c) {
        delete static_cast<ClientCallbackContext::ObserveContext*>(c);
    };

    OCMethod method;
    if (observeType == ObserveType::Observe)
    {
        method = OC_REST_OBSERVE;
    }
    else if (observeType == ObserveType::ObserveAll)
    {
        method = OC_REST_OBSERVE_ALL;
    }
    else
    {
        method = OC_REST_OBSERVE_ALL;
    }

    auto cLock = m_csdkLock.lock();

    if(cLock)
    {
        std::ostringstream os;
        os << host << assembleSetResourceUri(uri, queryParams).c_str();

        std::lock_guard<std::recursive_mutex> lock(*cLock);
        OCHeaderOption options[MAX_HEADER_OPTIONS];

        assembleHeaderOptions(options, headerOptions);
        result = OCDoResource(handle, method,
                              os.str().c_str(), nullptr,
                              nullptr,
                              static_cast<OCQualityOfService>(QoS),
                              &cbdata,
                              options, headerOptions.size());
    }
    else
    {
        delete ctx;
        return OC_STACK_ERROR;
    }

    return result;
}
Esempio n. 3
0
    OCStackResult InProcClientWrapper::PutResourceRepresentation(
        const OCDevAddr& devAddr,
        const std::string& uri,
        const OCRepresentation& rep,
        const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
        PutCallback& callback, QualityOfService QoS)
    {
        if(!callback)
        {
            return OC_STACK_INVALID_PARAM;
        }
        OCStackResult result;
        ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
        OCCallbackData cbdata(
                static_cast<void*>(ctx),
                setResourceCallback,
                [](void* c){delete static_cast<ClientCallbackContext::SetContext*>(c);}
                );

        std::string url = assembleSetResourceUri(uri, queryParams).c_str();

        auto cLock = m_csdkLock.lock();

        if(cLock)
        {
            std::lock_guard<std::recursive_mutex> lock(*cLock);
            OCDoHandle handle;
            OCHeaderOption options[MAX_HEADER_OPTIONS];

            result = OCDoResource(&handle, OC_REST_PUT,
                                  url.c_str(), &devAddr,
                                  assembleSetResourcePayload(rep),
                                  CT_DEFAULT,
                                  static_cast<OCQualityOfService>(QoS),
                                  &cbdata,
                                  assembleHeaderOptions(options, headerOptions),
                                  headerOptions.size());
        }
        else
        {
            delete ctx;
            result = OC_STACK_ERROR;
        }

        return result;
    }
Esempio n. 4
0
    OCStackResult InProcClientWrapper::GetResourceRepresentation(
        const OCDevAddr& devAddr,
        const std::string& resourceUri,
        const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
        GetCallback& callback, QualityOfService QoS)
    {
        if (!callback)
        {
            return OC_STACK_INVALID_PARAM;
        }
        OCStackResult result;
        ClientCallbackContext::GetContext* ctx =
            new ClientCallbackContext::GetContext(callback);
        OCCallbackData cbdata;
        cbdata.context = static_cast<void*>(ctx),
        cbdata.cb      = getResourceCallback;
        cbdata.cd      = [](void* c){delete (ClientCallbackContext::GetContext*)c;};


        std::string uri = assembleSetResourceUri(resourceUri, queryParams);

        auto cLock = m_csdkLock.lock();

        if (cLock)
        {
            std::lock_guard<std::recursive_mutex> lock(*cLock);
            OCHeaderOption options[MAX_HEADER_OPTIONS];

            result = OCDoResource(
                                  nullptr, OC_REST_GET,
                                  uri.c_str(),
                                  &devAddr, nullptr,
                                  CT_DEFAULT,
                                  static_cast<OCQualityOfService>(QoS),
                                  &cbdata,
                                  assembleHeaderOptions(options, headerOptions),
                                  headerOptions.size());
        }
        else
        {
            delete ctx;
            result = OC_STACK_ERROR;
        }
        return result;
    }
Esempio n. 5
0
    OCStackResult InProcClientWrapper::ListenForMQTopic(
            const OCDevAddr& devAddr,
            const std::string& resourceUri,
            const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
            FindCallback& callback, QualityOfService QoS)
    {
        oclog() << "ListenForMQTopic()" << std::flush;

        if (!callback)
        {
            return OC_STACK_INVALID_PARAM;
        }

        ClientCallbackContext::ListenContext* context =
            new ClientCallbackContext::ListenContext(callback, shared_from_this());
        OCCallbackData cbdata;
        cbdata.context = static_cast<void*>(context),
        cbdata.cb      = listenMQCallback;
        cbdata.cd      = [](void* c){delete (ClientCallbackContext::ListenContext*)c;};

        std::string uri = assembleSetResourceUri(resourceUri, queryParams);

        OCStackResult result = OC_STACK_ERROR;
        auto cLock = m_csdkLock.lock();
        if (cLock)
        {
            std::lock_guard<std::recursive_mutex> lock(*cLock);
            OCHeaderOption options[MAX_HEADER_OPTIONS];
            result = OCDoResource(
                                  nullptr, OC_REST_GET,
                                  uri.c_str(),
                                  &devAddr, nullptr,
                                  CT_DEFAULT,
                                  static_cast<OCQualityOfService>(QoS),
                                  &cbdata,
                                  assembleHeaderOptions(options, headerOptions),
                                  headerOptions.size());
        }
        else
        {
            delete context;
        }

        return result;
    }
Esempio n. 6
0
OCStackResult InProcClientWrapper::PutResourceRepresentation(const std::string& host,
        const std::string& uri, const OCRepresentation& rep,
        const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
        PutCallback& callback, QualityOfService QoS)
{
    OCStackResult result;
    OCCallbackData cbdata = {0};

    ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext();
    ctx->callback = callback;
    cbdata.cb = &setResourceCallback;
    cbdata.cd = [](void* c) {
        delete static_cast<ClientCallbackContext::SetContext*>(c);
    };
    cbdata.context = static_cast<void*>(ctx);

    // TODO: in the future the cstack should be combining these two strings!
    ostringstream os;
    os << host << assembleSetResourceUri(uri, queryParams).c_str();
    // TODO: end of above

    auto cLock = m_csdkLock.lock();

    if(cLock)
    {
        std::lock_guard<std::recursive_mutex> lock(*cLock);
        OCDoHandle handle;
        OCHeaderOption options[MAX_HEADER_OPTIONS];

        assembleHeaderOptions(options, headerOptions);
        result = OCDoResource(&handle, OC_REST_PUT,
                              os.str().c_str(), nullptr,
                              assembleSetResourcePayload(rep).c_str(),
                              static_cast<OCQualityOfService>(QoS),
                              &cbdata,
                              options, headerOptions.size());
    }
    else
    {
        delete ctx;
        result = OC_STACK_ERROR;
    }

    return result;
}
Esempio n. 7
0
    OCStackResult InProcClientWrapper::SubscribeDevicePresence(OCDoHandle* handle,
                                                               const std::string& host,
                                                               const QueryParamsList& queryParams,
                                                               OCConnectivityType connectivityType,
                                                               ObserveCallback& callback)
    {
        if (!callback)
        {
            return OC_STACK_INVALID_PARAM;
        }
        OCStackResult result;

        ClientCallbackContext::ObserveContext* ctx =
            new ClientCallbackContext::ObserveContext(callback);
        OCCallbackData cbdata;
        cbdata.context = static_cast<void*>(ctx),
        cbdata.cb      = observeResourceCallback;
        cbdata.cd      = [](void* c){delete (ClientCallbackContext::ObserveContext*)c;};

        auto cLock = m_csdkLock.lock();

        if (cLock)
        {
            std::lock_guard<std::recursive_mutex> lock(*cLock);

            std::ostringstream os;
            os << host << OCF_RSRVD_DEVICE_PRESENCE_URI;
            std::string url = assembleSetResourceUri(os.str(), queryParams);

            result = OCDoResource(handle, OC_REST_OBSERVE,
                                  url.c_str(), nullptr,
                                  nullptr, connectivityType,
                                  OC_LOW_QOS, &cbdata,
                                  nullptr, 0);
        }
        else
        {
            delete ctx;
            result = OC_STACK_ERROR;
        }

        return result;
    }