// <Include>
///////////////////////////////////////////////////////////////////////////////
//
// THIS FILE IS AUTOMATICALLY GENERATED.  DO NOT MODIFY IT BY HAND.
//
///////////////////////////////////////////////////////////////////////////////
// </Include>

// <LiteralInclude>
#include <wsdapi.h>
// </LiteralInclude>

// <LiteralInclude>
#include "SimpleThermostat_WSD.h"
// </LiteralInclude>

// <LiteralInclude>
#include "SimpleThermostat_WSDTypes.h"
// </LiteralInclude>

// <MessageTypeDeclarations>
//
// Port type http://www.example.com/ncd/Thermostat/SimpleThermostat_WSD
// Message type declarations
//
extern WSDXML_TYPE REQUESTTYPE_GetCurrentTemp;
extern WSDXML_TYPE RESPONSETYPE_GetCurrentTemp;
extern WSDXML_TYPE REQUESTTYPE_GetDesiredTemp;
extern WSDXML_TYPE RESPONSETYPE_GetDesiredTemp;
extern WSDXML_TYPE REQUESTTYPE_SetDesiredTemp;

// </MessageTypeDeclarations>

// <HostBuilderImplementation>
HRESULT CreateSimpleThermostat_WSDHost(
                LPCWSTR pszDeviceAddress,
                const WSD_THIS_DEVICE_METADATA* pThisDeviceMetadata,
                ISimpleThermostat_WSD* pISimpleThermostat_WSD,
                IWSDDeviceHost** ppHostOut,
                IWSDXMLContext** ppContextOut)
{
    HRESULT hr = S_OK;
    IWSDXMLContext* pContext = NULL;
    IWSDDeviceHost* pHost = NULL;

    // 
    // Validate parameters
    // 
    if( NULL == pszDeviceAddress )
    {
        return E_INVALIDARG;
    }

    if( NULL == pThisDeviceMetadata )
    {
        return E_INVALIDARG;
    }

    // pISimpleThermostat_WSD is optional
    if( NULL == ppHostOut )
    {
        return E_POINTER;
    }

    // ppContextOut is optional

    *ppHostOut = NULL;
    if( NULL != ppContextOut )
    {
        *ppContextOut = NULL;
    }

    // 
    // Create XML context for namespace and type registration
    // 
    hr = WSDXMLCreateContext(&pContext);

    // 
    // Register types used by the service
    // 
    if( S_OK == hr )
    {
        hr = SimpleThermostat_WSDRegisterTypes(pContext);
    }

    // 
    // Register namespaces used by the service
    // 
    if( S_OK == hr )
    {
        hr = SimpleThermostat_WSDRegisterNamespaces(pContext);
    }

    // 
    // Create device host
    // 
    if( S_OK == hr )
    {
        hr = WSDCreateDeviceHost(pszDeviceAddress, pContext, &pHost);
    }

    // 
    // Register port types
    // 
    if( S_OK == hr )
    {
        hr = pHost->RegisterPortType(&PortType_SimpleThermostat_WSD);
    }

    // 
    // Set metadata
    // 
    if( S_OK == hr )
    {
        hr = pHost->SetMetadata(&thisModelMetadata, pThisDeviceMetadata, &hostMetadata, NULL);
    }

    // 
    // Register services and set discoverability
    // 
    if( S_OK == hr && pISimpleThermostat_WSD != NULL )
    {
        hr = pHost->RegisterService(L"uri:MySimpleThermostat_WSDService", pISimpleThermostat_WSD);
    }

    // 
    // Cleanup
    // 
    if( S_OK == hr && ppContextOut )
    {
        *ppContextOut = pContext;
    }
    else
    {
        if( NULL != pContext )
        {
            pContext->Release();
        }
    }

    if( S_OK == hr )
    {
        *ppHostOut = pHost;
    }
    else
    {
        if( NULL != pHost )
        {
            pHost->Release();
        }
    }

    return hr;
}

// </HostBuilderImplementation>

// <StubDefinitions>
HRESULT
Stub_GetCurrentTemp
(   IUnknown* server
,   IWSDServiceMessaging* service
,   WSD_EVENT* event
)
{
    HRESULT hr = S_OK;
    RESPONSEBODY_SimpleThermostat_WSD_GetCurrentTemp result;
    ::ZeroMemory(&result,sizeof(result));
    hr =
        reinterpret_cast<ISimpleThermostat_WSD*>(server)->GetCurrentTemp
        (   &result.temp
        );
    if (SUCCEEDED(hr))
    {
        hr = service->SendResponse(&result,event->Operation,event->MessageParameters);
    }
    if (FAILED(hr))
    {
        hr = service->FaultRequest(&event->Soap->Header,event->MessageParameters, NULL);
    }

    // Clean up memory from outparams

    return hr;
}

HRESULT
Stub_GetDesiredTemp
(   IUnknown* server
,   IWSDServiceMessaging* service
,   WSD_EVENT* event
)
{
    HRESULT hr = S_OK;
    RESPONSEBODY_SimpleThermostat_WSD_GetDesiredTemp result;
    ::ZeroMemory(&result,sizeof(result));
    hr =
        reinterpret_cast<ISimpleThermostat_WSD*>(server)->GetDesiredTemp
        (   &result.temp
        );
    if (SUCCEEDED(hr))
    {
        hr = service->SendResponse(&result,event->Operation,event->MessageParameters);
    }
    if (FAILED(hr))
    {
        hr = service->FaultRequest(&event->Soap->Header,event->MessageParameters, NULL);
    }

    // Clean up memory from outparams

    return hr;
}

HRESULT
Stub_SetDesiredTemp
(   IUnknown* server
,   IWSDServiceMessaging* service
,   WSD_EVENT* event
)
{
    HRESULT hr = S_OK;
    REQUESTBODY_SimpleThermostat_WSD_SetDesiredTemp* p = reinterpret_cast<REQUESTBODY_SimpleThermostat_WSD_SetDesiredTemp*>(event->Soap->Body);
        reinterpret_cast<ISimpleThermostat_WSD*>(server)->SetDesiredTemp
        (   p->temp
        );
    return hr;
}

// </StubDefinitions>

