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
|
/*++
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:
Main.cpp
Abstract:
A simple test for bthecho sample.
Environment:
user mode only
--*/
#define INITGUID
#include <windows.h>
#include <strsafe.h>
#include <stdio.h>
#include <stdlib.h>
#include <cfgmgr32.h>
#include "public.h"
char testData[] = "WDF Bluetooth Sample Echo";
char replyData[sizeof(testData)];
DWORD
GetDevicePath(
_In_ LPGUID InterfaceGuid,
_Out_ LPWSTR * DevicePath
);
BOOL
DoEcho(
HANDLE hDevice
);
VOID
__cdecl
wmain()
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
BOOL echo = TRUE;
LPWSTR devicePath = NULL;
DWORD err = GetDevicePath((LPGUID)&BTHECHOSAMPLE_DEVICE_INTERFACE, &devicePath);
if (ERROR_SUCCESS != err) {
printf("Failed to find the BTHECHO device\n");
exit(1);
}
hDevice = CreateFile(devicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL );
if (hDevice == INVALID_HANDLE_VALUE) {
printf("Failed to open device. Error %d\n",GetLastError());
LocalFree(devicePath);
exit(1);
}
printf("Opened device successfully\n");
while (echo)
{
echo = DoEcho(hDevice);
}
if (INVALID_HANDLE_VALUE != hDevice) {
CloseHandle(hDevice);
printf("Closed device\n");
}
if (NULL != devicePath) {
LocalFree(devicePath);
}
}
BOOL
DoEcho(
HANDLE hDevice
)
{
BOOL retval = FALSE;
DWORD cbWritten = 0;
DWORD cbRead = 0;
BOOL bRet = WriteFile(
hDevice,
testData,
sizeof(testData),
&cbWritten,
NULL
);
if (!bRet)
{
printf("Write failed. Error: %d\n", GetLastError());
goto exit;
}
else
{
printf("Written \t\t%d bytes: %s\n", cbWritten, testData);
}
bRet = ReadFile(
hDevice,
replyData,
cbWritten,
&cbRead,
NULL
);
if (!bRet)
{
printf("Read failed. Error: %d\n", GetLastError());
goto exit;
}
else
{
printf("Reply from server \t%d bytes: %s\n", cbRead, replyData);
}
if (cbRead == cbWritten)
{
retval = memcmp(replyData, testData, cbRead) ? FALSE : TRUE;
}
exit:
return retval;
}
DWORD
GetDevicePath(
_In_ LPGUID InterfaceGuid,
_Out_ LPWSTR * DevicePath
)
{
CONFIGRET cmRet = CR_SUCCESS;
ULONG interfaceListSize = 0;
*DevicePath = NULL;
cmRet = CM_Get_Device_Interface_List_Size_Ex(&interfaceListSize, InterfaceGuid, 0, CM_GET_DEVICE_INTERFACE_LIST_PRESENT, NULL);
if (cmRet != CR_SUCCESS) {
goto exit;
}
*DevicePath = (LPWSTR)LocalAlloc(LMEM_ZEROINIT, interfaceListSize * sizeof((*DevicePath)[0]));
if (NULL == *DevicePath) {
cmRet = CR_OUT_OF_MEMORY;
goto exit;
}
cmRet = CM_Get_Device_Interface_List_Ex(InterfaceGuid, 0, *DevicePath, interfaceListSize, CM_GET_DEVICE_INTERFACE_LIST_PRESENT, NULL);
exit:
if (cmRet != CR_SUCCESS) {
if (NULL != *DevicePath) {
LocalFree(*DevicePath);
*DevicePath = NULL;
}
}
return CM_MapCrToWin32Err(cmRet, ERROR_UNIDENTIFIED_ERROR);
}
|