-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleConsole.cpp
365 lines (314 loc) · 10.8 KB
/
SimpleConsole.cpp
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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "stdafx.h"
#include "SimpleConsole.h"
#include "WlanHostedNetworkWinRT.h"
SimpleConsole::SimpleConsole()
: _apEvent(CreateEventEx(nullptr, nullptr, 0, WRITE_OWNER | EVENT_ALL_ACCESS))
{
HRESULT hr = _apEvent.IsValid() ? S_OK : HRESULT_FROM_WIN32(GetLastError());
if (FAILED(hr))
{
std::wcout << "Failed to create AP event: " << hr << std::endl;
throw WlanHostedNetworkException("Create event failed", hr);
}
_hostedNetwork.RegisterListener(this);
_hostedNetwork.RegisterPrompt(this);
_hostedNetwork.RegisterPairRequest(this);
m_WFDHelper.Init();
}
SimpleConsole::~SimpleConsole()
{
_hostedNetwork.RegisterListener(nullptr);
_hostedNetwork.RegisterPrompt(nullptr);
_hostedNetwork.RegisterPairRequest(nullptr);
m_WFDHelper.Close();
}
void SimpleConsole::RunConsole()
{
std::wstring command;
bool running = true;
while (running)
{
// Show prompt and get input
ShowPrompt();
getline(std::wcin, command);
if (command.length() > 0)
{
// Run the command, return false if the command was to quit
try
{
running = ExecuteCommand(command);
}
catch (WlanHostedNetworkException& e)
{
std::wcout << "Caught Exception: " << e.what() << std::endl;
}
}
}
}
void SimpleConsole::OnDeviceConnected(std::wstring remoteHostName)
{
std::wcout << std::endl << "Peer connected: " << remoteHostName << std::endl;
}
void SimpleConsole::OnDeviceDisconnected(std::wstring deviceId)
{
std::wcout << std::endl << "Peer disconnected: " << deviceId << std::endl;
}
void SimpleConsole::OnAdvertisementStarted()
{
std::wcout << "Soft AP started!" << std::endl
<< "Peers can connect to: " << _hostedNetwork.GetSSID() << std::endl
<< "Passphrase: " << _hostedNetwork.GetPassphrase() << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnAdvertisementStopped(std::wstring message)
{
std::wcout << "Soft AP stopped." << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnAdvertisementAborted(std::wstring message)
{
std::wcout << "Soft AP aborted: " << message << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnEnumerationCompleted(std::wstring message)
{
std::wcout << "Soft AP enumeration Completed: " << message << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnEnumerationStopped(std::wstring message)
{
std::wcout << "Soft AP enumeration Stopped: " << message << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnDeviceAdded(std::wstring id, std::wstring name)
{
std::wcout << "OnDeviceAdded: " << name << " " << id << std::endl;
}
void SimpleConsole::OnDeviceRemoved(std::wstring message)
{
std::wcout << "OnDeviceRemoved: " << message << std::endl;
}
void SimpleConsole::OnDeviceUnpaired(std::wstring message)
{
std::wcout << "OnDeviceUnpaired: " << message << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnDevicePaired(std::wstring message)
{
std::wcout << "OnDevicePaired: " << message << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnDevicePairedError(std::wstring message, int errorCode)
{
std::wcout << "OnDevicePaired: " << message << " " <<errorCode << std::endl;
SetEvent(_apEvent.Get());
}
void SimpleConsole::OnAsyncException(std::wstring message)
{
std::wcout << std::endl << "Caught exception in asynchronous method: " << message << std::endl;
}
void SimpleConsole::LogMessage(std::wstring message)
{
std::wcout << std::endl << message << std::endl;
}
bool SimpleConsole::AcceptIncommingConnection()
{
std::wcout << std::endl << "Accept peer connection? (y/n)" << std::endl;
std::wstring response;
getline(std::wcin, response);
if (response.length() > 0 &&
(response[0] == 'y' || response[0] == 'Y'))
{
return true;
}
return false;
}
bool SimpleConsole::PairRequest(ABI::Windows::Devices::Enumeration::DevicePairingKinds kinds, std::wstring& strPin)
{
if (kinds & ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_ConfirmOnly)
{
std::wcout << std::endl << "Accept peer connection? (y/n)" << std::endl;
std::wstring response;
getline(std::wcin, response);
if (response.length() > 0 &&
(response[0] == 'y' || response[0] == 'Y'))
{
return true;
}
}
else if (kinds & ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_DisplayPin)
{
std::wcout << std::endl << "Pin: " << strPin << " Accept peer connection? (y/n)" << std::endl;
std::wstring response;
getline(std::wcin, response);
if (response.length() > 0 &&
(response[0] == 'y' || response[0] == 'Y'))
{
return true;
}
}
else if (kinds & ABI::Windows::Devices::Enumeration::DevicePairingKinds::DevicePairingKinds_ProvidePin)
{
std::wcout << std::endl << "Please input Pin:" << std::endl;
std::wstring response;
getline(std::wcin, response);
if (response.length() >= 6)
{
return true;
}
}
return false;
}
void SimpleConsole::ShowPrompt()
{
std::wcout << std::endl << ">";
}
void SimpleConsole::ShowHelp()
{
std::wcout << std::endl
<< "Wi-Fi Direct Legacy AP Demo Usage:" << std::endl
<< "----------------------------------" << std::endl
<< "scan : scan wifi direct device" << std::endl
<< "start : Start the legacy AP to accept connections" << std::endl
<< "stop : Stop the legacy AP" << std::endl
<< "ssid <ssid> : Configure the SSID before starting the legacy AP" << std::endl
<< "pass <passphrase> : Configure the passphrase before starting the legacy AP" << std::endl
<< "autoaccept <0|1> : Configure the legacy AP to accept connections (default) or prompt the user" << std::endl
<< "quit|exit : Exit" << std::endl
<< std::endl;
}
bool SimpleConsole::ExecuteCommand(std::wstring command)
{
// Simple command parsing logic
if (command == L"quit" ||
command == L"exit")
{
std::wcout << std::endl << "Exiting" << std::endl;
return false;
}
else if (command == L"scan")
{
std::wcout << std::endl << "Scanning soft AP..." << std::endl;
_hostedNetwork.Scan();
WaitForSingleObjectEx(_apEvent.Get(), INFINITE, FALSE);
}
else if (command == L"start")
{
std::wcout << std::endl << "Starting soft AP..." << std::endl;
_hostedNetwork.Start();
WaitForSingleObjectEx(_apEvent.Get(), INFINITE, FALSE);
}
else if (command == L"stop")
{
std::wcout << std::endl << "Stopping soft AP..." << std::endl;
_hostedNetwork.Stop();
WaitForSingleObjectEx(_apEvent.Get(), INFINITE, FALSE);
}
else if (0 == command.compare(0, 7, L"connect"))
{
std::wstring::size_type found = command.find_first_of(' ', 0);
if (found != std::wstring::npos && found < command.length())
{
std::wstring id = command.substr(found + 1);
_hostedNetwork.ConnectDevice(id.c_str());
//m_WFDHelper.Connect(id.c_str());
}
}
else if (0 == command.compare(0, 10, L"disconnect"))
{
std::wstring::size_type found = command.find_first_of(' ', 0);
if (found != std::wstring::npos && found < command.length())
{
std::wstring id = command.substr(found + 1);
_hostedNetwork.Disconnect(id.c_str());
//WaitForSingleObjectEx(_apEvent.Get(), INFINITE, FALSE);
}
}
else if (0 == command.compare(0, 4, L"pair"))
{
std::wstring::size_type found = command.find_first_of(' ', 0);
if (found != std::wstring::npos && found < command.length())
{
std::wstring id = command.substr(found + 1);
_hostedNetwork.Pair(id.c_str());
WaitForSingleObjectEx(_apEvent.Get(), INFINITE, FALSE);
}
}
else if (0 == command.compare(0, 6, L"unpair"))
{
std::wstring::size_type found = command.find_first_of(' ', 0);
if (found != std::wstring::npos && found < command.length())
{
std::wstring id = command.substr(found + 1);
_hostedNetwork.Unpair(id.c_str());
WaitForSingleObjectEx(_apEvent.Get(), INFINITE, FALSE);
}
}
else if (0 == command.compare(0, 4, L"ssid"))
{
// Parse the SSID as the first non-space character after ssid
std::wstring ssid;
std::wstring::size_type found = command.find_first_not_of(' ', 4);
if (found != std::wstring::npos && found < command.length())
{
ssid = command.substr(found);
std::wcout << std::endl << "Setting SSID to " << ssid << std::endl;
_hostedNetwork.SetSSID(ssid);
}
else
{
std::wcout << std::endl << "Setting SSID FAILED, bad input" << std::endl;
}
}
else if (0 == command.compare(0, 4, L"pass"))
{
// Parse the Passphrase as the first non-space character after pass
std::wstring passphrase;
std::wstring::size_type found = command.find_first_not_of(' ', 4);
if (found != std::wstring::npos && found < command.length())
{
passphrase = command.substr(found);
std::wcout << std::endl << "Setting Passphrase to " << passphrase << std::endl;
_hostedNetwork.SetPassphrase(passphrase);
}
else
{
std::wcout << std::endl << "Setting Passphrase FAILED, bad input" << std::endl;
}
}
else if (0 == command.compare(0, 10, L"autoaccept"))
{
std::wstring value;
std::wstring::size_type found = command.find_first_not_of(' ', 10);
if (found != std::wstring::npos && found < command.length())
{
value = command.substr(found);
bool autoAccept = true;
if (value == L"0")
{
autoAccept = false;
}
std::wcout << std::endl << "Setting AutoAccept to " << autoAccept << " (input was " << value << ")" << std::endl;
_hostedNetwork.SetAutoAccept(autoAccept);
}
else
{
std::wcout << std::endl << "Setting AutoAccpet FAILED, bad input" << std::endl;
}
}
else
{
ShowHelp();
}
return true;
}