/
WiFiAP.cs
779 lines (727 loc) · 36.5 KB
/
WiFiAP.cs
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Tizen.Applications;
namespace Tizen.Network.WiFi
{
/// <summary>
/// A class for managing the network information of the access point (AP).
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class WiFiAP : IDisposable
{
private IntPtr _apHandle = IntPtr.Zero;
private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
private static Dictionary<IntPtr, Interop.WiFi.VoidCallback> s_callbackMap = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
private int _requestId = 0;
private static int s_requestId = 0;
private WiFiNetwork _network;
private WiFiSecurity _security;
private bool _disposed = false;
private static TaskCompletionSource<WiFiAP> wpsWithoutSsidTask = null;
private static Dictionary<IntPtr, TaskCompletionSource<bool>> _wpsTaskMap = new Dictionary<IntPtr, TaskCompletionSource<bool>>();
private TizenSynchronizationContext context = new TizenSynchronizationContext();
private static TizenSynchronizationContext s_context = new TizenSynchronizationContext();
/// <summary>
/// The network information of the access point (AP).
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>The WiFiNetwork instance containing the network information of the AP.</value>
public WiFiNetwork NetworkInformation
{
get
{
return _network;
}
}
/// <summary>
/// The security information of the access point (AP).
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <value>The WiFiSecurity instance containing security information of the AP.</value>
public WiFiSecurity SecurityInformation
{
get
{
return _security;
}
}
internal WiFiAP(IntPtr handle)
{
Log.Debug(Globals.LogTag, "New WiFiAP. Handle: " + handle);
_apHandle = handle;
Initialize();
}
/// <summary>
/// Creates an object for the access point.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="essid">The Extended Service Set Identifier of the access point.</param>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ArgumentNullException">Thrown when the ESSID is passed as null.</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public WiFiAP(string essid)
{
Log.Debug(Globals.LogTag, "New WiFiAP. Essid: " + essid);
createHandle(essid, true);
Initialize();
}
/// <summary>
/// Creates an object for the hidden access point.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="essid">The Extended Service Set Identifier of the access point.</param>
/// <param name="hidden">The value to set a hidden AP.</param>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ArgumentNullException">Thrown when the ESSID is passed as null.</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public WiFiAP(string essid, bool hidden)
{
createHandle(essid, hidden);
Initialize();
}
/// <summary>
/// Destroy the WiFiAP object
/// </summary>
~WiFiAP()
{
Dispose(false);
}
/// <summary>
/// A method to destroy the managed WiFiAP objects.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (_disposed)
return;
Log.Info(Globals.LogTag, "WiFiAP Handle HashCode: " + _apHandle.GetHashCode());
int ret = Interop.WiFi.AP.Destroy(_apHandle);
if (ret == (int)WiFiError.None)
{
_apHandle = IntPtr.Zero;
}
_disposed = true;
}
private void createHandle(string id, bool hidden)
{
int ret;
if (id == null)
{
throw new ArgumentNullException("Essid is null");
}
if (hidden)
{
ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
}
else
{
ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
}
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
}
}
private void Initialize()
{
Interop.WiFi.SafeWiFiAPHandle apHandle = new Interop.WiFi.SafeWiFiAPHandle(_apHandle);
_network = new WiFiNetwork(apHandle);
_security = new WiFiSecurity(apHandle);
}
/// <summary>
/// Refreshes the access point information.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public void Refresh()
{
Log.Debug(Globals.LogTag, "Refresh");
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
int ret = Interop.WiFi.AP.Refresh(_apHandle);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle, "http://tizen.org/privilege/network.get");
}
}
/// <summary>
/// Connects the access point asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns> A task indicating whether the connect method is done or not.</returns>
/// <remarks>
/// This method must be called from MainThread.
/// </remarks>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="NowInProgressException">Thrown when the Wi-Fi connection is now in progress.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
/// <exception cref="InvalidKeyException">Thrown when the key is wrong.</exception>
public Task ConnectAsync()
{
Log.Info(Globals.LogTag, "ConnectAsync HashCode: " + _apHandle.GetHashCode());
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
IntPtr id;
lock (_callback_map)
{
id = (IntPtr)_requestId++;
_callback_map[id] = (error, key) =>
{
Log.Info(Globals.LogTag, "ConnectAsync done " + (WiFiError)error);
if (error != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
task.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi connecting"));
}
else
{
task.SetResult(true);
}
lock (_callback_map)
{
_callback_map.Remove(key);
}
};
}
context.Post((x) =>
{
Log.Info(Globals.LogTag, "Interop.WiFi.Connect");
try
{
int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e);
task.SetException(e);
}
}, null);
return task.Task;
}
/// <summary>
/// Connects the access point with the WPS asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="info">A WpsInfo instance which is type of WpsPbcInfo or WpsPinInfo.</param>
/// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
/// <remarks>
/// This method must be called from MainThread.
/// </remarks>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is an empty string or more than 7 characters.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
/// <exception cref="NowInProgressException">Thrown when the Wi-Fi connection is now in progress.</exception>
/// <exception cref="TimeoutException">Thrown when the timeout of WPS connection is expired.</exception>
public Task ConnectWpsAsync(WpsInfo info)
{
Log.Info(Globals.LogTag, "ConnectWpsAsync");
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
TaskCompletionSource<bool> wpsTask = new TaskCompletionSource<bool>();
_wpsTaskMap[_apHandle] = wpsTask;
IntPtr id;
lock (_callback_map)
{
id = (IntPtr)_requestId++;
_callback_map[id] = (error, key) =>
{
Log.Info(Globals.LogTag, "ConnectWpsAsync done");
if (error != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
wpsTask.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi connecting"));
Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
_wpsTaskMap.Remove(_apHandle);
}
else
{
wpsTask.SetResult(true);
Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
_wpsTaskMap.Remove(_apHandle);
}
lock (_callback_map)
{
_callback_map.Remove(key);
}
};
}
context.Post((x) =>
{
try
{
int ret = -1;
if (info.GetType() == typeof(WpsPbcInfo))
{
Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPb");
ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
}
else if (info.GetType() == typeof(WpsPinInfo))
{
WpsPinInfo pinInfo = (WpsPinInfo)info;
if (pinInfo.GetWpsPin() == null)
{
throw new ArgumentNullException("Wps pin should not be null");
}
if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
{
throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
}
Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPin");
ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
}
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e);
wpsTask.SetException(e);
Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
_wpsTaskMap.Remove(_apHandle);
}
}, null);
return wpsTask.Task;
}
/// <summary>
/// Connects the access point with WPS without SSID asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
/// <returns>A task which contains Connected access point information.</returns>
/// <remarks>
/// If WpsPinInfo is used, its object has to be constructed with a pin which must be 4 or 8 characters long. \n
/// This method must be called from MainThread.
/// </remarks>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is not of 4 or 8 characters long.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
/// <exception cref="NowInProgressException">Thrown when the Wi-Fi connection is now in progress.</exception>
/// <exception cref="TimeoutException">Thrown when the timeout of WPS connection is expired.</exception>
public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
{
Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync");
wpsWithoutSsidTask = new TaskCompletionSource<WiFiAP>();
IntPtr id;
lock (s_callbackMap)
{
id = (IntPtr)s_requestId++;
s_callbackMap[id] = (error, key) =>
{
Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync done");
if (error != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
wpsWithoutSsidTask.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi connecting"));
wpsWithoutSsidTask = null;
Log.Info(Globals.LogTag, "task is null");
}
else
{
WiFiAP ap = WiFiManagerImpl.Instance.GetConnectedAP();
wpsWithoutSsidTask.SetResult(ap);
wpsWithoutSsidTask = null;
Log.Info(Globals.LogTag, "task is null");
}
lock (s_callbackMap)
{
s_callbackMap.Remove(key);
}
};
}
s_context.Post((x) =>
{
try
{
int ret = -1;
if (info.GetType() == typeof(WpsPbcInfo))
{
Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPbcWithoutSsid");
ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);
}
else if (info.GetType() == typeof(WpsPinInfo))
{
WpsPinInfo pinInfo = (WpsPinInfo)info;
if (pinInfo.GetWpsPin() == null)
{
throw new ArgumentNullException("Wps pin should not be null");
}
if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
{
throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long");
}
Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPinWithoutSsid");
ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id);
}
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
}
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e);
wpsWithoutSsidTask.SetException(e);
wpsWithoutSsidTask = null;
Log.Info(Globals.LogTag, "task is null");
}
}, null);
return wpsWithoutSsidTask.Task;
}
/// <summary>
/// Stops ongoing WPS provisioning
/// </summary>
/// <since_tizen> 5 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public static void CancelWps()
{
Log.Debug(Globals.LogTag, "CancelWps");
int ret = Interop.WiFi.CancelWps(WiFiManagerImpl.Instance.GetSafeHandle());
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to cancel Wps, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
}
// Cancel awaiting tasks
if (wpsWithoutSsidTask != null)
{
Log.Info(Globals.LogTag, "Cancel ConnectWpsWithoutSsidAsync()");
wpsWithoutSsidTask.SetCanceled();
}
foreach (var item in _wpsTaskMap)
{
Log.Info(Globals.LogTag, "Cancel ConnectWpsAsync() by " + item.Key.GetHashCode());
item.Value.SetCanceled();
}
_wpsTaskMap.Clear();
}
/// <summary>
/// Disconnects the access point asynchronously.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <returns> A task indicating whether the disconnect method is done or not.</returns>
/// <remarks>
/// This method must be called from MainThread.
/// </remarks>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.set</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public Task DisconnectAsync()
{
Log.Debug(Globals.LogTag, "DisconnectAsync");
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
IntPtr id;
lock (_callback_map)
{
id = (IntPtr)_requestId++;
_callback_map[id] = (error, key) =>
{
Log.Info(Globals.LogTag, "DisconnectAsync done");
if (error != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
}
else
{
task.SetResult(true);
}
lock (_callback_map)
{
_callback_map.Remove(key);
}
};
}
context.Post((x) =>
{
Log.Info(Globals.LogTag, "Interop.WiFi.Disconnect");
try
{
int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e);
task.SetException(e);
}
}, null);
return task.Task;
}
/// <summary>
/// Deletes the information of a stored access point and disconnects it when the AP is connected.
/// If an AP is connected, then the connection information will be stored. This information is used when a connection to that AP is established automatically.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public void ForgetAP()
{
Log.Debug(Globals.LogTag, "ForgetAP");
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to forget AP, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}
/// <summary>
/// Deletes the information of a stored access point and disconnects it when the AP is connected asyncronously.
/// If an AP is connected, then the connection information will be stored. This information is used when a connection to that AP is established automatically.
/// </summary>
/// <returns> A task indicating whether the disconnect method is done or not.</returns>
/// <remarks>
/// This method must be called from MainThread.
/// </remarks>
/// <since_tizen> 5 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public Task ForgetAPAsync()
{
Log.Debug(Globals.LogTag, "ForgetAPAsync");
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
IntPtr id;
lock (_callback_map)
{
id = (IntPtr)_requestId++;
_callback_map[id] = (error, key) =>
{
Log.Info(Globals.LogTag, "ForgetAPAsync done");
if (error != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
}
else
{
task.SetResult(true);
}
lock (_callback_map)
{
_callback_map.Remove(key);
}
};
}
context.Post((x) =>
{
Log.Info(Globals.LogTag, "Interop.WiFi.ForgetAP");
try
{
int ret = Interop.WiFi.ForgetAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to forget wifi, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}
catch (Exception e)
{
Log.Error(Globals.LogTag, "Exception on ForgetAPAsync\n" + e);
task.SetException(e);
}
}, null);
return task.Task;
}
/// <summary>
/// Update the information of a stored access point.
/// When a AP information is changed, the change will not be applied until this method is called.
/// </summary>
/// <since_tizen> 5 </since_tizen>
/// <feature>http://tizen.org/feature/network.wifi</feature>
/// <privilege>http://tizen.org/privilege/network.profile</privilege>
/// <privilege>http://tizen.org/privilege/network.get</privilege>
/// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
/// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
/// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
/// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
/// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
public void Update()
{
Log.Debug(Globals.LogTag, "Update");
if (_disposed)
{
throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
}
int ret = Interop.WiFi.UpdateAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to update AP, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
}
}
}
/// <summary>
/// An abstract class which is used to represent the WPS information of the access point.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public abstract class WpsInfo
{
}
/// <summary>
/// A class which is used to represent WPS PBC information of the access point.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class WpsPbcInfo : WpsInfo
{
}
/// <summary>
/// A class which is used to represent WPS PIN information of the access point.
/// </summary>
/// <since_tizen> 3 </since_tizen>
public class WpsPinInfo : WpsInfo
{
private string _pin;
private WpsPinInfo()
{
}
/// <summary>
/// A public constructor which instantiates WpsPinInfo class with the given pin.
/// </summary>
/// <since_tizen> 3 </since_tizen>
/// <param name="pin">WPS Pin of the access point.</param>
/// <remarks>
/// Pin should not be null or empty. It should be of less than 8 characters.
/// </remarks>
public WpsPinInfo(string pin)
{
_pin = pin;
}
internal string GetWpsPin()
{
return _pin;
}
}
}