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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
#pragma once
#include "resource.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define STRSAFE_NO_DEPRECATE
#include <stdio.h>
#include <tchar.h>
#include <atlbase.h>
#include <atlcom.h>
#include <atlcoll.h>
#include <atlstr.h>
#include <strsafe.h>
// This driver is entirely user-mode
_Analysis_mode_(_Analysis_code_type_user_code_);
//
// Driver specific tracing #defines
//
// TODO: Change these values to be appropriate for your driver.
//
#define MYDRIVER_TRACING_ID L"Microsoft\\WPD\\BasicHardwareDriver"
//
// TODO: Choose a different trace control GUID
//
#define WPP_CONTROL_GUIDS \
WPP_DEFINE_CONTROL_GUID(BasicHardwareDriverCtlGuid,(80068de5,197d,4db6,b77c,29ee3cca4537), \
WPP_DEFINE_BIT(TRACE_FLAG_ALL) \
WPP_DEFINE_BIT(TRACE_FLAG_DEVICE) \
WPP_DEFINE_BIT(TRACE_FLAG_DRIVER) \
WPP_DEFINE_BIT(TRACE_FLAG_QUEUE) \
)
#define WPP_LEVEL_FLAGS_LOGGER(lvl,flags) \
WPP_LEVEL_LOGGER(flags)
#define WPP_LEVEL_FLAGS_ENABLED(lvl, flags) \
(WPP_LEVEL_ENABLED(flags) && WPP_CONTROL(WPP_BIT_ ## flags).Level >= lvl)
//
// This comment block is scanned by the trace preprocessor to define our
// TraceEvents function.
//
// begin_wpp config
// FUNC Trace{FLAG=TRACE_FLAG_ALL}(LEVEL, MSG, ...);
// FUNC TraceEvents(LEVEL, FLAGS, MSG, ...);
// end_wpp
//
// This comment block is scanned by the trace preprocessor to define our
// CHECK_HR function.
//
//
// begin_wpp config
// USEPREFIX (CHECK_HR,"%!STDPREFIX!");
// FUNC CHECK_HR{FLAG=TRACE_FLAG_ALL}(hrCheck, MSG, ...);
// USESUFFIX (CHECK_HR, " hr= %!HRESULT!", hrCheck);
// end_wpp
//
// PRE macro: The name of the macro includes the condition arguments FLAGS and EXP
// define in FUNC above
//
#define WPP_FLAG_hrCheck_PRE(FLAGS, hrCheck) {if(hrCheck != S_OK) {
//
// POST macro
// The name of the macro includes the condition arguments FLAGS and EXP
// define in FUNC above
#define WPP_FLAG_hrCheck_POST(FLAGS, hrCheck) ; } }
//
// The two macros below are for checking if the event should be logged and for
// choosing the logger handle to use when calling the ETW trace API
//
#define WPP_FLAG_hrCheck_ENABLED(FLAGS, hrCheck) WPP_FLAG_ENABLED(FLAGS)
#define WPP_FLAG_hrCheck_LOGGER(FLAGS, hrCheck) WPP_FLAG_LOGGER(FLAGS)
#include <PortableDevice.h>
#include <PortableDeviceTypes.h>
#include <PortableDeviceClassExtension.h>
#include <initguid.h> // Required to access the DEFINE_GUID macro
#include <propkeydef.h> // Required to access the DEFINE_PROPERTYKEY macro
// {CDD18979-A7B0-4D5E-9EB2-0A826805CBBD}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_WUDF_DEVICE_OBJECT, 0xCDD18979, 0xA7B0, 0x4D5E, 0x9E, 0xB2, 0x0A, 0x82, 0x68, 0x05, 0xCB, 0xBD, 2);
// {9BD949E5-59CF-41AE-90A9-BE1D044F578F}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_WPD_SERIALIZER_OBJECT, 0x9BD949E5, 0x59CF, 0x41AE, 0x90, 0xA9, 0xBE, 0x1D, 0x04, 0x4F, 0x57, 0x8F, 2);
// {4DF6C8C7-2CE5-457C-9F53-EFCECAA95C04}
DEFINE_PROPERTYKEY(PRIVATE_SAMPLE_DRIVER_CLIENT_CONTEXT_MAP, 0x4DF6C8C7, 0x2CE5, 0x457C, 0x9F, 0x53, 0xEF, 0xCE, 0xCA, 0xA9, 0x5C, 0x04, 2);
/****************************************************************************
* This section defines the Functional Category associated with the sensor object
****************************************************************************/
//
// FUNCTIONAL_CATEGORY_SENSOR_SAMPLE
// Indicates this object encapsulates sensor functionality on the device
DEFINE_GUID (FUNCTIONAL_CATEGORY_SENSOR_SAMPLE, 0x09b93609, 0xd5c1,0x497b,0xb2, 0x52, 0xb4, 0x92, 0xcb, 0x5e, 0xfe, 0x52);
/****************************************************************************
* This section defines all Commands, Parameters and Options associated with:
* SENSOR_PROPERTIES_V1
*
* This category is for properties common to all sensor objects.
****************************************************************************/
DEFINE_GUID (SENSOR_PROPERTIES_V1, 0xa7ef4367, 0x6550, 0x4055, 0xb6, 0x6f, 0xbe, 0x6f, 0xda, 0xcf, 0x4e, 0x9f);
//
// SENSOR_READING
// [ VT_UI4 ] Indicates the sensor reading in degrees Kelvin.
DEFINE_PROPERTYKEY(SENSOR_READING, 0xa7ef4367, 0x6550, 0x4055, 0xb6, 0x6f, 0xbe, 0x6f, 0xda, 0xcf, 0x4e, 0x9f, 2);
//
// SENSOR_UPDATE_INTERVAL
// [ VT_UI4 ] Indicates the sensor update interval in milliseconds.
DEFINE_PROPERTYKEY(SENSOR_UPDATE_INTERVAL, 0xa7ef4367, 0x6550, 0x4055, 0xb6, 0x6f, 0xbe, 0x6f, 0xda, 0xcf, 0x4e, 0x9f, 3);
/****************************************************************************
* This section defines all Events associated with the sensor object
****************************************************************************/
//
// EVENT_SENSOR_READING_UPDATED
// This event is sent after a new sensor reading is available on the device.
DEFINE_GUID (EVENT_SENSOR_READING_UPDATED, 0xada23b0b, 0xce13, 0x4e11, 0x9d, 0x2f, 0x15, 0xfe, 0x10, 0xd6, 0x63, 0x37);
//
// Macro Definitions
//
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) if( NULL != p ) { ( p )->Release(); p = NULL; }
#endif
class ContextMap : public IUnknown
{
public:
ContextMap() :
m_cRef(1)
{
}
~ContextMap()
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
IUnknown* pUnk = NULL;
POSITION elementPosition = NULL;
elementPosition = m_Map.GetStartPosition();
while(elementPosition != NULL)
{
pUnk = m_Map.GetNextValue(elementPosition);
if(pUnk != NULL)
{
pUnk->Release();
}
}
}
public: // IUnknown
ULONG __stdcall AddRef()
{
InterlockedIncrement((long*) &m_cRef);
return m_cRef;
}
_At_(this, __drv_freesMem(Mem))
ULONG __stdcall Release()
{
ULONG ulRefCount = m_cRef - 1;
if (InterlockedDecrement((long*) &m_cRef) == 0)
{
delete this;
return 0;
}
return ulRefCount;
}
HRESULT __stdcall QueryInterface(
REFIID riid,
void** ppv)
{
HRESULT hr = S_OK;
if(riid == IID_IUnknown)
{
*ppv = static_cast<IUnknown*>(this);
AddRef();
}
else
{
*ppv = NULL;
hr = E_NOINTERFACE;
}
return hr;
}
public: // Context accessor methods
// If successful, this method AddRef's the context and returns
// a context key
HRESULT Add(
_In_ IUnknown* pContext,
_Out_ CAtlStringW& key)
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
HRESULT hr = S_OK;
GUID guidContext = GUID_NULL;
CComBSTR bstrContext;
key = L"";
// Create a unique context key
hr = CoCreateGuid(&guidContext);
if (hr == S_OK)
{
bstrContext = guidContext;
if(bstrContext.Length() > 0)
{
key = bstrContext;
}
else
{
hr = E_OUTOFMEMORY;
}
}
if (hr == S_OK)
{
// Insert this into the map
POSITION elementPosition = m_Map.SetAt(key, pContext);
if(elementPosition != NULL)
{
// AddRef since we are holding onto it
pContext->AddRef();
}
else
{
hr = E_OUTOFMEMORY;
}
}
return hr;
}
void Remove(
_In_ const CAtlStringW& key)
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
// Get the element
IUnknown* pContext = NULL;
if (m_Map.Lookup(key, pContext) == true)
{
// Remove the entry for it
m_Map.RemoveKey(key);
// Release it
pContext->Release();
}
}
// Returns the context pointer. If not found, return value is NULL.
// If non-NULL, caller is responsible for Releasing when it is done,
// since this method will AddRef the context.
IUnknown* GetContext(
_In_ const CAtlStringW& key)
{
CComCritSecLock<CComAutoCriticalSection> Lock(m_CriticalSection);
// Get the element
IUnknown* pContext = NULL;
if (m_Map.Lookup(key, pContext) == true)
{
// AddRef
pContext->AddRef();
}
return pContext;
}
private:
CComAutoCriticalSection m_CriticalSection;
CAtlMap<CAtlStringW, IUnknown*> m_Map;
DWORD m_cRef;
};
HRESULT UpdateDeviceFriendlyName(
_In_ IPortableDeviceClassExtension* pPortableDeviceClassExtension,
_In_ LPCWSTR wszDeviceFriendlyName);
// Forward declaration of WpdBaseDriver as it is accessed by other classes
class WpdBaseDriver;
#include "WpdBasicHardwareDriver.h"
#include "RS232Connection.h"
#include "RS232Target.h"
#include "WpdObjectEnum.h"
#include "WpdObjectProperties.h"
#include "WpdCapabilities.h"
#include "WpdBaseDriver.h"
#include "Device.h"
#include "Driver.h"
#include "Queue.h"
extern HINSTANCE g_hInstance;
|