summaryrefslogtreecommitdiff
path: root/wpd/WpdServiceSampleDriver/WpdService.cpp
blob: 4a255b0b3bc088b6c98ba2d623e16ce9dee264f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "stdafx.h"

#include "WpdService.tmh"

WpdService::WpdService() : m_pContactsService(NULL)
{
}

WpdService::~WpdService()
{

}

HRESULT WpdService::Initialize(_In_ FakeDevice* pDevice)
{
    if (pDevice == NULL)
    {
        return E_POINTER;
    }
    m_pContactsService = pDevice->GetContactsService();
    m_ServiceMethods.Initialize(m_pContactsService);
    m_ServiceCapabilities.Initialize(m_pContactsService);
    return S_OK;
}

HRESULT WpdService::DispatchWpdMessage(
    _In_    REFPROPERTYKEY         Command,
    _In_    IPortableDeviceValues* pParams,
    _In_    IPortableDeviceValues* pResults)
{
    HRESULT     hr                  = S_OK;
    LPWSTR      pszRequestFilename  = NULL;

    // Get the request filename to process the service message
    hr = pParams->GetStringValue(PRIVATE_SAMPLE_DRIVER_REQUEST_FILENAME, &pszRequestFilename);
    if (FAILED(hr))
    {
        hr = E_INVALIDARG;
        CHECK_HR(hr, "Failed to get the required requested filename");
    }

    if (hr == S_OK)
    {    
        hr = CheckRequestFilename(pszRequestFilename);
        CHECK_HR(hr, "Unknown request filename %ws received", pszRequestFilename);
    }

    if (hr == S_OK)
    {
        if (Command.fmtid == WPD_CATEGORY_SERVICE_CAPABILITIES)
        {
            hr = m_ServiceCapabilities.DispatchWpdMessage(Command, pParams, pResults);            
        }
        else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_METHODS_START_INVOKE))
        {
            hr = m_ServiceMethods.OnStartInvoke(pParams, pResults);
        }
        else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_METHODS_END_INVOKE))
        {
            hr = m_ServiceMethods.OnEndInvoke(pParams, pResults);
        }
        else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_METHODS_CANCEL_INVOKE))
        {
            hr = m_ServiceMethods.OnCancelInvoke(pParams, pResults);
        }
        else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_COMMON_GET_SERVICE_OBJECT_ID))
        {
            hr = OnGetServiceObjectID(pszRequestFilename, pParams, pResults);
        }
        else
        {
            hr = E_NOTIMPL;
            CHECK_HR(hr, "Unknown command %ws.%d received",CComBSTR(Command.fmtid), Command.pid);
        }
    }

    CoTaskMemFree(pszRequestFilename);

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_COMMON_GET_SERVICE_OBJECT_ID 
 *  command.
 *
 *  The parameters sent to us are:
 *  None
 *
 *  The driver should:
 *  - Return the objectID associated with the filename. 
 *
 */
HRESULT WpdService::OnGetServiceObjectID(
    _In_    LPCWSTR                pszRequestFilename,
    _In_    IPortableDeviceValues* pParams,
    _In_    IPortableDeviceValues* pResults)
{
    HRESULT hr = S_OK;

    if((pParams    == NULL) ||
       (pResults   == NULL))
    {
        hr = E_POINTER;
        CHECK_HR(hr, "Cannot have NULL parameter");
        return hr;
    }

    // For simplicity, the request filename is the same as the service object ID
    hr = pResults->SetStringValue(WPD_PROPERTY_SERVICE_OBJECT_ID, pszRequestFilename);
    CHECK_HR(hr, "Failed to set WPD_PROPERTY_COMMON_OBJECT_IDS");

    return hr;
}

HRESULT WpdService::CheckRequestFilename(
    _In_    LPCWSTR pszRequestFilename)
{
    HRESULT     hr                 = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
    CAtlStringW strRequestFilename = pszRequestFilename;

    // For simplicity, the request filename happens to be the same as the service object ID
    if (strRequestFilename.CompareNoCase(m_pContactsService->GetRequestFilename()) == 0)
    {
        hr = S_OK;
    }
    else
    {
        CHECK_HR(hr, "Unknown request filename %ws received", pszRequestFilename);
    }

    return hr;
}