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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
/*++
Copyright (c) Microsoft Corporation. All rights reserved.
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
PURPOSE.
Module Name:
clisrv.c
Abstract:
Implementation of functions common to bth echo server and client
Environment:
Kernel mode
--*/
#include "clisrv.h"
#if defined(EVENT_TRACING)
#include "clisrv.tmh"
#endif
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
BthEchoSharedDeviceContextHeaderInit(
PBTHECHOSAMPLE_DEVICE_CONTEXT_HEADER Header,
WDFDEVICE Device
)
/*++
Description:
Initializes the common context header between server and client
Arguments:
Header - Contex header
Device - Framework device object
Return Value:
NTSTATUS Status code.
--*/
{
NTSTATUS status;
WDF_OBJECT_ATTRIBUTES attributes;
Header->Device = Device;
Header->IoTarget = WdfDeviceGetIoTarget(Device);
//
// Initialize request object
//
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = Device;
status = WdfRequestCreate(
&attributes,
Header->IoTarget,
&Header->Request
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"Failed to pre-allocate request in device context, Status code %!STATUS!\n", status);
goto exit;
}
exit:
return status;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
BthEchoSharedRetrieveLocalInfo(
_In_ PBTHECHOSAMPLE_DEVICE_CONTEXT_HEADER DevCtxHdr
)
/*++
Description:
Retrieves the local bth address.
This address is burnt into device hence doesn't change.
It also retrieves the local host supported features if available.
Arguments:
Header - Contex header
Return Value:
NTSTATUS Status code.
--*/
{
NTSTATUS status = STATUS_SUCCESS;
struct _BRB_GET_LOCAL_BD_ADDR * brb = NULL;
brb = (struct _BRB_GET_LOCAL_BD_ADDR *)
DevCtxHdr->ProfileDrvInterface.BthAllocateBrb(
BRB_HCI_GET_LOCAL_BD_ADDR,
POOLTAG_BTHECHOSAMPLE
);
if(brb == NULL)
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"Failed to allocate brb BRB_HCI_GET_LOCAL_BD_ADDR, returning status code %!STATUS!\n", status);
goto exit;
}
status = BthEchoSharedSendBrbSynchronously(
DevCtxHdr->IoTarget,
DevCtxHdr->Request,
(PBRB) brb,
sizeof(*brb)
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"Retrieving local bth address failed, Status code %!STATUS!\n", status);
goto exit1;
}
DevCtxHdr->LocalBthAddr = brb->BtAddress;
#if (NTDDI_VERSION >= NTDDI_WIN8)
//
// Now retreive local host supported features
//
status = BthEchoSharedGetHostSupportedFeatures(DevCtxHdr);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_PNP,
"Sending IOCTL for reading supported features failed, Status code %!STATUS!\n", status);
goto exit1;
}
#endif
exit1:
DevCtxHdr->ProfileDrvInterface.BthFreeBrb((PBRB)brb);
exit:
return status;
}
#if (NTDDI_VERSION >= NTDDI_WIN8)
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
BthEchoSharedGetHostSupportedFeatures(
_In_ PBTHECHOSAMPLE_DEVICE_CONTEXT_HEADER DevCtxHdr
)
/*++
Routine Description:
This routine synchronously checks the local stack's supported features
Arguments:
DevCtxHdr - Information about the local device
Return Value:
NTSTATUS Status code.
--*/
{
WDF_MEMORY_DESCRIPTOR outMemDesc = {0};
BTH_HOST_FEATURE_MASK localFeatures = {0};
NTSTATUS status = STATUS_SUCCESS;
DevCtxHdr->LocalFeatures.Mask = 0;
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(
&outMemDesc,
&localFeatures,
sizeof(localFeatures)
);
status = WdfIoTargetSendIoctlSynchronously(
DevCtxHdr->IoTarget,
NULL,
IOCTL_BTH_GET_HOST_SUPPORTED_FEATURES,
NULL,
&outMemDesc,
NULL,
NULL
);
if (!NT_SUCCESS(status)) {
return status;
}
DevCtxHdr->LocalFeatures = localFeatures;
return status;
}
#endif
_IRQL_requires_max_(DISPATCH_LEVEL)
NTSTATUS
BthEchoSharedSendBrbAsync(
_In_ WDFIOTARGET IoTarget,
_In_ WDFREQUEST Request,
_In_ PBRB Brb,
_In_ size_t BrbSize,
_In_ PFN_WDF_REQUEST_COMPLETION_ROUTINE ComplRoutine,
_In_opt_ WDFCONTEXT Context
)
/*++
Routine Description:
This routine formats a request with brb and sends it asynchronously
Arguments:
IoTarget - Target to send the brb to
Request - request object to be formatted with brb
Brb - Brb to be sent
BrbSize - size of the Brb data structure
ComplRoutine - WDF completion routine for the request
This must be specified because we are formatting the request
and hence not using SEND_AND_FORGET flag
Context - (optional) context to be passed in to the completion routine
Return Value:
Success implies that request was sent correctly and completion routine will be called
for it,
failure implies it was not sent and caller should complete the request
Notes:
This routine does not call WdfRequestReuse on the Request passed in.
Caller must do so before passing in the request, if it is reusing the request.
This routine does not complete the request in case of failure.
Caller must complete the request in case of failure.
--*/
{
NTSTATUS status = BTH_ERROR_SUCCESS;
WDF_OBJECT_ATTRIBUTES attributes;
WDFMEMORY memoryArg1 = NULL;
if (BrbSize <= 0)
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_CONT_READER,
"BrbSize has invalid value: %I64d\n",
BrbSize
);
status = STATUS_INVALID_PARAMETER;
goto exit;
}
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = Request;
status = WdfMemoryCreatePreallocated(
&attributes,
Brb,
BrbSize,
&memoryArg1
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_UTIL,
"Creating preallocted memory for Brb 0x%p failed, Request to be formatted 0x%p, "
"Status code %!STATUS!\n",
Brb,
Request,
status
);
goto exit;
}
status = WdfIoTargetFormatRequestForInternalIoctlOthers(
IoTarget,
Request,
IOCTL_INTERNAL_BTH_SUBMIT_BRB,
memoryArg1,
NULL, //OtherArg1Offset
NULL, //OtherArg2
NULL, //OtherArg2Offset
NULL, //OtherArg4
NULL //OtherArg4Offset
);
if (!NT_SUCCESS(status))
{
TraceEvents(TRACE_LEVEL_ERROR, DBG_UTIL,
"Formatting request 0x%p with Brb 0x%p failed, Status code %!STATUS!\n",
Request,
Brb,
status
);
goto exit;
}
//
// Set a CompletionRoutine callback function.
//
WdfRequestSetCompletionRoutine(
Request,
ComplRoutine,
Context
);
if (FALSE == WdfRequestSend(
Request,
IoTarget,
NULL
))
{
status = WdfRequestGetStatus(Request);
TraceEvents(TRACE_LEVEL_ERROR, DBG_UTIL,
"Request send failed for request 0x%p, Brb 0x%p, Status code %!STATUS!\n",
Request,
Brb,
status
);
goto exit;
}
exit:
return status;
}
_IRQL_requires_max_(PASSIVE_LEVEL)
NTSTATUS
BthEchoSharedSendBrbSynchronously(
_In_ WDFIOTARGET IoTarget,
_In_ WDFREQUEST Request,
_In_ PBRB Brb,
_In_ ULONG BrbSize
)
/*++
Routine Description:
This routine formats a request with brb and sends it synchronously
Arguments:
IoTarget - Target to send the brb to
Request - request object to be formatted with brb
Brb - Brb to be sent
BrbSize - size of the Brb data structure
Return Value:
NTSTATUS Status code.
Notes:
This routine does calls WdfRequestReuse on the Request passed in.
Caller need not do so before passing in the request.
This routine does not complete the request in case of failure.
Caller must complete the request in case of failure.
--*/
{
NTSTATUS status;
WDF_REQUEST_REUSE_PARAMS reuseParams;
WDF_MEMORY_DESCRIPTOR OtherArg1Desc;
WDF_REQUEST_REUSE_PARAMS_INIT(
&reuseParams,
WDF_REQUEST_REUSE_NO_FLAGS,
STATUS_NOT_SUPPORTED
);
status = WdfRequestReuse(Request, &reuseParams);
if (!NT_SUCCESS(status))
{
goto exit;
}
WDF_MEMORY_DESCRIPTOR_INIT_BUFFER(
&OtherArg1Desc,
Brb,
BrbSize
);
status = WdfIoTargetSendInternalIoctlOthersSynchronously(
IoTarget,
Request,
IOCTL_INTERNAL_BTH_SUBMIT_BRB,
&OtherArg1Desc,
NULL, //OtherArg2
NULL, //OtherArg4
NULL, //RequestOptions
NULL //BytesReturned
);
exit:
return status;
}
|