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;
    }
    OCStackResult InProcClientWrapper::CancelObserveResource(
            OCDoHandle handle,
            const std::string& host, // unused
            const std::string& uri,  // unused
            const HeaderOptions& headerOptions,
            QualityOfService QoS)
    {
        OCStackResult result;
        auto cLock = m_csdkLock.lock();

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

            result = OCCancel(handle,
                    static_cast<OCQualityOfService>(QoS),
                    assembleHeaderOptions(options, headerOptions),
                    headerOptions.size());
        }
        else
        {
            result = OC_STACK_ERROR;
        }

        return result;
    }
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;
}
    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;
    }
    OCStackResult InProcClientWrapper::DeleteResource(
        const OCDevAddr& devAddr,
        const std::string& uri,
        const HeaderOptions& headerOptions,
        DeleteCallback& callback,
        QualityOfService /*QoS*/)
    {
        if(!callback)
        {
            return OC_STACK_INVALID_PARAM;
        }
        OCStackResult result;
        ClientCallbackContext::DeleteContext* ctx =
            new ClientCallbackContext::DeleteContext(callback);
        OCCallbackData cbdata(
                static_cast<void*>(ctx),
                deleteResourceCallback,
                [](void* c){delete static_cast<ClientCallbackContext::DeleteContext*>(c);}
                );

        auto cLock = m_csdkLock.lock();

        if(cLock)
        {
            OCHeaderOption options[MAX_HEADER_OPTIONS];

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

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

        return result;
    }
    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;
    }
    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;
    }
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;
}
OCStackResult InProcClientWrapper::DeleteResource(const std::string& host,
        const std::string& uri, const HeaderOptions& headerOptions,
        DeleteCallback& callback, QualityOfService QoS)
{
    OCStackResult result;
    OCCallbackData cbdata = {0};

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

    ostringstream os;
    os << host << uri;

    auto cLock = m_csdkLock.lock();

    if(cLock)
    {
        OCHeaderOption options[MAX_HEADER_OPTIONS];
        OCDoHandle handle;

        assembleHeaderOptions(options, headerOptions);

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

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

    return result;
}
    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;
        }
    }