-
Notifications
You must be signed in to change notification settings - Fork 10
/
WlanHostedNetworkWinRT.cpp
553 lines (480 loc) · 21.5 KB
/
WlanHostedNetworkWinRT.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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//*********************************************************
//
// 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 "WlanHostedNetworkWinRT.h"
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Foundation::Collections;
using namespace ABI::Windows::Devices::Enumeration;
using namespace ABI::Windows::Devices::WiFiDirect;
using namespace ABI::Windows::Security::Credentials;
using namespace ABI::Windows::Networking;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
typedef __FITypedEventHandler_2_Windows__CDevices__CWiFiDirect__CWiFiDirectConnectionListener_Windows__CDevices__CWiFiDirect__CWiFiDirectConnectionRequestedEventArgs ConnectionRequestedHandler;
typedef __FITypedEventHandler_2_Windows__CDevices__CWiFiDirect__CWiFiDirectAdvertisementPublisher_Windows__CDevices__CWiFiDirect__CWiFiDirectAdvertisementPublisherStatusChangedEventArgs StatusChangedHandler;
typedef __FITypedEventHandler_2_Windows__CDevices__CWiFiDirect__CWiFiDirectDevice_IInspectable ConnectionStatusChangedHandler;
typedef __FIAsyncOperationCompletedHandler_1_Windows__CDevices__CWiFiDirect__CWiFiDirectDevice FromIdAsyncHandler;
typedef __FIVectorView_1_Windows__CNetworking__CEndpointPair EndpointPairCollection;
WlanHostedNetworkHelper::WlanHostedNetworkHelper()
: _ssidProvided(false),
_passphraseProvided(false),
_listener(nullptr),
_autoAccept(true)
{
}
WlanHostedNetworkHelper::~WlanHostedNetworkHelper()
{
if (_publisher.Get() != nullptr)
{
_publisher->Stop();
}
Reset();
}
void WlanHostedNetworkHelper::Start()
{
HRESULT hr = S_OK;
// Clean up old state
Reset();
// Create WiFiDirectAdvertisementPublisher
hr = Windows::Foundation::ActivateInstance(HStringReference(RuntimeClass_Windows_Devices_WiFiDirect_WiFiDirectAdvertisementPublisher).Get(), &_publisher);
if (FAILED(hr))
{
throw WlanHostedNetworkException("ActivateInstance for WiFiDirectAdvertisementPublisher failed", hr);
}
// Add event handler for advertisement StatusChanged
hr = _publisher->add_StatusChanged(
Callback<StatusChangedHandler>([this](IWiFiDirectAdvertisementPublisher* sender, IWiFiDirectAdvertisementPublisherStatusChangedEventArgs* args) -> HRESULT
{
HRESULT hr = S_OK;
WiFiDirectAdvertisementPublisherStatus status;
WiFiDirectError error;
try
{
hr = args->get_Status(&status);
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Status for AdvertisementPubliserStatusChangedEventArgs failed", hr);
}
switch (status)
{
case WiFiDirectAdvertisementPublisherStatus_Started:
{
// Begin listening for connections and notify listener that the advertisement started
StartListener();
if (_listener != nullptr)
{
_listener->OnAdvertisementStarted();
}
break;
}
case WiFiDirectAdvertisementPublisherStatus_Aborted:
{
// Check error and notify listener that the advertisement stopped
hr = args->get_Error(&error);
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Error for AdvertisementPubliserStatusChangedEventArgs failed", hr);
}
if (_listener != nullptr)
{
std::wstring message;
switch (error)
{
case WiFiDirectError_RadioNotAvailable:
message = L"Advertisement aborted, Wi-Fi radio is turned off";
break;
case WiFiDirectError_ResourceInUse:
message = L"Advertisement aborted, Resource In Use";
break;
default:
message = L"Advertisement aborted, unknown reason";
break;
}
_listener->OnAdvertisementAborted(message);
}
break;
}
case WiFiDirectAdvertisementPublisherStatus_Stopped:
{
// Notify listener that the advertisement is stopped
if (_listener != nullptr)
{
_listener->OnAdvertisementStopped(L"Advertisement stopped");
}
break;
}
}
}
catch (WlanHostedNetworkException& e)
{
if (_listener != nullptr)
{
std::wostringstream ss;
ss << e.what() << ": " << e.GetErrorCode();
_listener->OnAsyncException(ss.str());
}
return e.GetErrorCode();
}
return hr;
}).Get(), &_statusChangedToken);
// Set Advertisement required settings
hr = _publisher->get_Advertisement(_advertisement.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get advertisement for WiFiDirectAdvertisementPublisher failed", hr);
}
// Must set the autonomous group owner (GO) enabled flag
// Legacy Wi-Fi Direct advertisement uses a Wi-Fi Direct GO to act as an access point to legacy settings
hr = _advertisement->put_IsAutonomousGroupOwnerEnabled(true);
if (FAILED(hr))
{
throw WlanHostedNetworkException("Set is autonomous group owner for WiFiDirectAdvertisement failed", hr);
}
hr = _advertisement->get_LegacySettings(_legacySettings.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get legacy settings for WiFiDirectAdvertisement failed", hr);
}
// Must enable legacy settings so that non-Wi-Fi Direct peers can connect in legacy mode
hr = _legacySettings->put_IsEnabled(true);
if (FAILED(hr))
{
throw WlanHostedNetworkException("Set is enabled for WiFiDirectLegacySettings failed", hr);
}
HString hstrSSID;
HString hstrPassphrase;
// Either specify an SSID, or read the randomly generated one
if (_ssidProvided)
{
hr = hstrSSID.Set(_ssid.c_str());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Failed to create HSTRING representation for SSID", hr);
}
hr = _legacySettings->put_Ssid(hstrSSID.Get());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Set SSID for WiFiDirectLegacySettings failed", hr);
}
}
else
{
hr = _legacySettings->get_Ssid(hstrSSID.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get SSID for WiFiDirectLegacySettings failed", hr);
}
_ssid = hstrSSID.GetRawBuffer(nullptr);
}
// Either specify a passphrase, or read the randomly generated one
ComPtr<IPasswordCredential> passwordCredential;
hr = _legacySettings->get_Passphrase(passwordCredential.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
}
if (_passphraseProvided)
{
hr = hstrPassphrase.Set(_passphrase.c_str());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Failed to create HSTRING representation for Passphrase", hr);
}
hr = passwordCredential->put_Password(hstrPassphrase.Get());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Set Passphrase for WiFiDirectLegacySettings failed", hr);
}
}
else
{
hr = passwordCredential->get_Password(hstrPassphrase.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Passphrase for WiFiDirectLegacySettings failed", hr);
}
_passphrase = hstrPassphrase.GetRawBuffer(nullptr);
}
// Start the advertisement, which will create an access point that other peers can connect to
hr = _publisher->Start();
if (FAILED(hr))
{
throw WlanHostedNetworkException("Start WiFiDirectAdvertisementPublisher failed", hr);
}
}
void WlanHostedNetworkHelper::Stop()
{
HRESULT hr = S_OK;
// Call stop on the publisher and expect the status changed callback
if (_publisher.Get() != nullptr)
{
hr = _publisher->Stop();
if (FAILED(hr))
{
throw WlanHostedNetworkException("Stop WiFiDirectAdvertisementPublisher failed", hr);
}
}
else
{
throw WlanHostedNetworkException("WiFiDirectAdvertisementPublisher is not running", hr);
}
}
void WlanHostedNetworkHelper::StartListener()
{
HRESULT hr = S_OK;
// Create WiFiDirectConnectionListener
hr = Windows::Foundation::ActivateInstance(HStringReference(RuntimeClass_Windows_Devices_WiFiDirect_WiFiDirectConnectionListener).Get(), &_connectionListener);
if (FAILED(hr))
{
throw WlanHostedNetworkException("ActivateInstance for WiFiDirectConnectionListener failed", hr);
}
hr = _connectionListener->add_ConnectionRequested(
Callback<ConnectionRequestedHandler>([this](IWiFiDirectConnectionListener* sender, IWiFiDirectConnectionRequestedEventArgs* args) -> HRESULT
{
HRESULT hr = S_OK;
ComPtr<IWiFiDirectConnectionRequest> request;
ComPtr<IDeviceInformation> deviceInformation;
HString deviceId;
ComPtr<IWiFiDirectDeviceStatics> wfdStatics;
if (_listener != nullptr)
{
_listener->LogMessage(L"Connection Requested...");
}
bool acceptConnection = true;
if (!_autoAccept && _prompt != nullptr)
{
acceptConnection = _prompt->AcceptIncommingConnection();
}
try
{
hr = args->GetConnectionRequest(request.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get connection request for ConnectionRequestedEventArgs failed", hr);
}
if (acceptConnection)
{
hr = request->get_DeviceInformation(deviceInformation.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get device information for ConnectionRequest failed", hr);
}
hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_WiFiDirect_WiFiDirectDevice).Get(), &wfdStatics);
if (FAILED(hr))
{
throw WlanHostedNetworkException("GetActivationFactory for WiFiDirectDevice failed", hr);
}
hr = deviceInformation->get_Id(deviceId.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get ID for DeviceInformation failed", hr);
}
ComPtr<IAsyncOperation<WiFiDirectDevice*>> asyncAction;
hr = wfdStatics->FromIdAsync(deviceId.Get(), &asyncAction);
if (FAILED(hr))
{
throw WlanHostedNetworkException("From ID Async for WiFiDirectDevice failed", hr);
}
hr = asyncAction->put_Completed(Callback<FromIdAsyncHandler>([this](IAsyncOperation<WiFiDirectDevice*>* pHandler, AsyncStatus status) -> HRESULT
{
HRESULT hr = S_OK;
ComPtr<IWiFiDirectDevice> wfdDevice;
ComPtr<EndpointPairCollection> endpointPairs;
ComPtr<IEndpointPair> endpointPair;
ComPtr<IHostName> remoteHostName;
HString remoteHostNameDisplay;
HString deviceId;
try
{
if (status == AsyncStatus::Completed)
{
// Get the WiFiDirectDevice object
hr = pHandler->GetResults(wfdDevice.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Put Completed for FromIDAsync operation failed", hr);
}
// Now retrieve the endpoint pairs, which includes the IP address assigned to the peer
hr = wfdDevice->GetConnectionEndpointPairs(endpointPairs.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get EndpointPairs for WiFiDirectDevice failed", hr);
}
hr = endpointPairs->GetAt(0, endpointPair.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get first EndpointPair in collection failed", hr);
}
hr = endpointPair->get_RemoteHostName(remoteHostName.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Remote HostName for EndpointPair failed", hr);
}
hr = remoteHostName->get_DisplayName(remoteHostNameDisplay.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Display Name for Remote HostName failed", hr);
}
// Add handler for connection status changed
EventRegistrationToken statusChangedToken;
hr = wfdDevice->add_ConnectionStatusChanged(Callback<ConnectionStatusChangedHandler>([this](IWiFiDirectDevice* sender, IInspectable*) -> HRESULT
{
WiFiDirectConnectionStatus status;
HString deviceId;
HRESULT hr = S_OK;
try
{
hr = sender->get_ConnectionStatus(&status);
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get connection status for peer failed", hr);
}
switch (status)
{
case WiFiDirectConnectionStatus_Connected:
// NO-OP
break;
case WiFiDirectConnectionStatus_Disconnected:
// Clean-up state
hr = sender->get_DeviceId(deviceId.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Device ID failed", hr);
}
auto itDevice = _connectedDevices.find(deviceId.GetRawBuffer(nullptr));
auto itToken = _connectedDeviceStatusChangedTokens.find(deviceId.GetRawBuffer(nullptr));
if (itToken != _connectedDeviceStatusChangedTokens.end())
{
if (itDevice != _connectedDevices.end())
{
itDevice->second->remove_ConnectionStatusChanged(itToken->second);
}
_connectedDeviceStatusChangedTokens.erase(itToken);
}
if (itDevice != _connectedDevices.end())
{
_connectedDevices.erase(itDevice);
}
// Notify listener of disconnect
if (_listener != nullptr)
{
_listener->OnDeviceDisconnected(deviceId.GetRawBuffer(nullptr));
}
break;
}
}
catch (WlanHostedNetworkException& e)
{
if (_listener != nullptr)
{
std::wostringstream ss;
ss << e.what() << ": " << e.GetErrorCode();
_listener->OnAsyncException(ss.str());
}
return e.GetErrorCode();
}
return hr;
}).Get(), &statusChangedToken);
// Store the connected peer
hr = wfdDevice->get_DeviceId(deviceId.GetAddressOf());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Get Device ID failed", hr);
}
_connectedDevices.insert(std::make_pair(deviceId.GetRawBuffer(nullptr), wfdDevice));
_connectedDeviceStatusChangedTokens.insert(std::make_pair(deviceId.GetRawBuffer(nullptr), statusChangedToken));
// Notify Listener
if (_listener != nullptr)
{
_listener->OnDeviceConnected(remoteHostNameDisplay.GetRawBuffer(nullptr));
}
}
else
{
if (_listener != nullptr)
{
switch (status)
{
case AsyncStatus::Started:
_listener->LogMessage(L"Device connected, status=Started");
break;
case AsyncStatus::Canceled:
_listener->LogMessage(L"Device connected, status=Canceled");
break;
case AsyncStatus::Error:
_listener->LogMessage(L"Device connected, status=Error");
break;
}
}
}
}
catch (WlanHostedNetworkException& e)
{
if (_listener != nullptr)
{
std::wostringstream ss;
ss << e.what() << ": " << e.GetErrorCode();
_listener->OnAsyncException(ss.str());
}
return e.GetErrorCode();
}
return hr;
}).Get());
if (FAILED(hr))
{
throw WlanHostedNetworkException("Put Completed for FromIDAsync operation failed", hr);
}
}
else
{
if (_listener != nullptr)
{
_listener->LogMessage(L"Declined");
}
}
}
catch (WlanHostedNetworkException& e)
{
if (_listener != nullptr)
{
std::wostringstream ss;
ss << e.what() << ": " << e.GetErrorCode();
_listener->OnAsyncException(ss.str());
}
return e.GetErrorCode();
}
return hr;
}).Get(), &_connectionRequestedToken);
if (FAILED(hr))
{
throw WlanHostedNetworkException("Add ConnectionRequested handler for WiFiDirectConnectionListener failed", hr);
}
if (_listener != nullptr)
{
_listener->LogMessage(L"Connection Listener is ready");
}
}
void WlanHostedNetworkHelper::Reset()
{
if (_connectionListener.Get() != nullptr)
{
_connectionListener->remove_ConnectionRequested(_connectionRequestedToken);
}
if (_publisher.Get() != nullptr)
{
_publisher->remove_StatusChanged(_statusChangedToken);
}
_legacySettings.Reset();
_advertisement.Reset();
_publisher.Reset();
_connectionListener.Reset();
_connectedDevices.clear();
}