2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Threading.Tasks;
19 using System.Collections.Generic;
20 using Tizen.Applications;
22 namespace Tizen.Network.WiFi
25 /// A class for managing the network information of the access point (AP).
27 /// <since_tizen> 3 </since_tizen>
28 public class WiFiAP : IDisposable
30 private IntPtr _apHandle = IntPtr.Zero;
31 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
32 private static Dictionary<IntPtr, Interop.WiFi.VoidCallback> s_callbackMap = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
33 private int _requestId = 0;
34 private static int s_requestId = 0;
35 private WiFiNetwork _network;
36 private WiFiSecurity _security;
37 private bool _disposed = false;
39 private TizenSynchronizationContext context = new TizenSynchronizationContext();
40 private static TizenSynchronizationContext s_context = new TizenSynchronizationContext();
43 /// The network information of the access point (AP).
45 /// <since_tizen> 3 </since_tizen>
46 /// <value>The WiFiNetwork instance containing the network information of the AP.</value>
47 public WiFiNetwork NetworkInformation
56 /// The security information of the access point (AP).
58 /// <since_tizen> 3 </since_tizen>
59 /// <value>The WiFiSecurity instance containing security information of the AP.</value>
60 public WiFiSecurity SecurityInformation
68 internal WiFiAP(IntPtr handle)
70 Log.Debug(Globals.LogTag, "New WiFiAP. Handle: " + handle);
76 /// Creates an object for the access point.
78 /// <since_tizen> 3 </since_tizen>
79 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
80 /// <feature>http://tizen.org/feature/network.wifi</feature>
81 /// <privilege>http://tizen.org/privilege/network.get</privilege>
82 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
83 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
84 /// <exception cref="ArgumentNullException">Thrown when the ESSID is passed as null.</exception>
85 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
86 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
87 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
88 public WiFiAP(string essid)
90 Log.Debug(Globals.LogTag, "New WiFiAP. Essid: " + essid);
91 createHandle(essid, true);
96 /// Creates an object for the hidden access point.
98 /// <since_tizen> 3 </since_tizen>
99 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
100 /// <param name="hidden">The value to set a hidden AP.</param>
101 /// <feature>http://tizen.org/feature/network.wifi</feature>
102 /// <privilege>http://tizen.org/privilege/network.get</privilege>
103 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
104 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
105 /// <exception cref="ArgumentNullException">Thrown when the ESSID is passed as null.</exception>
106 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
107 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
108 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
109 public WiFiAP(string essid, bool hidden)
111 createHandle(essid, hidden);
116 /// Destroy the WiFiAP object
124 /// A method to destroy the managed WiFiAP objects.
126 /// <since_tizen> 3 </since_tizen>
127 public void Dispose()
130 GC.SuppressFinalize(this);
133 private void Dispose(bool disposing)
138 Interop.WiFi.AP.Destroy(_apHandle);
139 _apHandle = IntPtr.Zero;
143 private void createHandle(string id, bool hidden)
148 throw new ArgumentNullException("Essid is null");
153 ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
158 ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
161 if (ret != (int)WiFiError.None)
163 Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
164 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
168 private void Initialize()
170 Interop.WiFi.SafeWiFiAPHandle apHandle = new Interop.WiFi.SafeWiFiAPHandle(_apHandle);
171 _network = new WiFiNetwork(apHandle);
172 _security = new WiFiSecurity(apHandle);
176 /// Refreshes the access point information.
178 /// <since_tizen> 3 </since_tizen>
179 /// <feature>http://tizen.org/feature/network.wifi</feature>
180 /// <privilege>http://tizen.org/privilege/network.get</privilege>
181 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
182 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
183 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
184 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
185 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
186 public void Refresh()
188 Log.Debug(Globals.LogTag, "Refresh");
191 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
193 int ret = Interop.WiFi.AP.Refresh(_apHandle);
194 if (ret != (int)WiFiError.None)
196 Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
197 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle, "http://tizen.org/privilege/network.get");
202 /// Connects the access point asynchronously.
204 /// <since_tizen> 3 </since_tizen>
205 /// <returns> A task indicating whether the connect method is done or not.</returns>
207 /// This method must be called from MainThread.
209 /// <feature>http://tizen.org/feature/network.wifi</feature>
210 /// <privilege>http://tizen.org/privilege/network.set</privilege>
211 /// <privilege>http://tizen.org/privilege/network.get</privilege>
212 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
213 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
214 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
215 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
216 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
217 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
218 public Task ConnectAsync()
220 Log.Info(Globals.LogTag, "ConnectAsync");
223 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
225 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
229 id = (IntPtr)_requestId++;
230 _callback_map[id] = (error, key) =>
232 Log.Info(Globals.LogTag, "ConnectAsync done " + (WiFiError)error);
233 if (error != (int)WiFiError.None)
235 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
236 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
240 task.SetResult(true);
244 _callback_map.Remove(key);
251 Log.Info(Globals.LogTag, "Interop.WiFi.Connect");
254 int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
255 if (ret != (int)WiFiError.None)
257 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
258 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
263 Log.Error(Globals.LogTag, "Exception on ConnectAsync\n" + e.ToString());
264 task.SetException(e);
272 /// Connects the access point with the WPS asynchronously.
274 /// <since_tizen> 3 </since_tizen>
275 /// <param name="info">A WpsInfo instance which is type of WpsPbcInfo or WpsPinInfo.</param>
276 /// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
278 /// This method must be called from MainThread.
280 /// <feature>http://tizen.org/feature/network.wifi</feature>
281 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
282 /// <privilege>http://tizen.org/privilege/network.get</privilege>
283 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
284 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
285 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
286 /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
287 /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is an empty string or more than 7 characters.</exception>
288 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
289 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
290 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
291 public Task ConnectWpsAsync(WpsInfo info)
293 Log.Info(Globals.LogTag, "ConnectWpsAsync");
296 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
298 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
302 id = (IntPtr)_requestId++;
303 _callback_map[id] = (error, key) =>
305 Log.Info(Globals.LogTag, "ConnectWpsAsync done");
306 if (error != (int)WiFiError.None)
308 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
309 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
313 task.SetResult(true);
317 _callback_map.Remove(key);
327 if (info.GetType() == typeof(WpsPbcInfo))
329 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPb");
330 ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
332 else if (info.GetType() == typeof(WpsPinInfo))
334 WpsPinInfo pinInfo = (WpsPinInfo)info;
335 if (pinInfo.GetWpsPin() == null)
337 throw new ArgumentNullException("Wps pin should not be null");
340 if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
342 throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
345 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPin");
346 ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
349 if (ret != (int)WiFiError.None)
351 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
352 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
357 Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e.ToString());
358 task.SetException(e);
366 /// Connects the access point with WPS without SSID asynchronously.
368 /// <since_tizen> 3 </since_tizen>
369 /// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
370 /// <returns>A task which contains Connected access point information.</returns>
372 /// If WpsPinInfo is used, its object has to be constructed with a pin which must be 4 or 8 characters long. \n
373 /// This method must be called from MainThread.
375 /// <feature>http://tizen.org/feature/network.wifi</feature>
376 /// <privilege>http://tizen.org/privilege/network.set</privilege>
377 /// <privilege>http://tizen.org/privilege/network.get</privilege>
378 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
379 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
380 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
381 /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
382 /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is not of 4 or 8 characters long.</exception>
383 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
384 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
385 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
386 public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
388 Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync");
389 TaskCompletionSource<WiFiAP> task = new TaskCompletionSource<WiFiAP>();
393 id = (IntPtr)s_requestId++;
394 s_callbackMap[id] = (error, key) =>
396 Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync done");
397 if (error != (int)WiFiError.None)
399 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
400 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
404 WiFiAP ap = WiFiManagerImpl.Instance.GetConnectedAP();
409 s_callbackMap.Remove(key);
414 s_context.Post((x) =>
419 if (info.GetType() == typeof(WpsPbcInfo))
421 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPbcWithoutSsid");
422 ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);
424 else if (info.GetType() == typeof(WpsPinInfo))
426 WpsPinInfo pinInfo = (WpsPinInfo)info;
427 if (pinInfo.GetWpsPin() == null)
429 throw new ArgumentNullException("Wps pin should not be null");
432 if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
434 throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long");
437 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPinWithoutSsid");
438 ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id);
441 if (ret != (int)WiFiError.None)
443 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
444 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
449 Log.Error(Globals.LogTag, "Exception on ConnectWpsWithoutSsidAsync\n" + e.ToString());
450 task.SetException(e);
458 /// Disconnects the access point asynchronously.
460 /// <since_tizen> 3 </since_tizen>
461 /// <returns> A task indicating whether the disconnect method is done or not.</returns>
463 /// This method must be called from MainThread.
465 /// <feature>http://tizen.org/feature/network.wifi</feature>
466 /// <privilege>http://tizen.org/privilege/network.set</privilege>
467 /// <privilege>http://tizen.org/privilege/network.get</privilege>
468 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
469 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
470 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
471 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
472 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
473 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
474 public Task DisconnectAsync()
476 Log.Debug(Globals.LogTag, "DisconnectAsync");
479 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
481 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
485 id = (IntPtr)_requestId++;
486 _callback_map[id] = (error, key) =>
488 Log.Info(Globals.LogTag, "DisconnectAsync done");
489 if (error != (int)WiFiError.None)
491 Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
492 task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
496 task.SetResult(true);
500 _callback_map.Remove(key);
507 Log.Info(Globals.LogTag, "Interop.WiFi.Disconnect");
510 int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
511 if (ret != (int)WiFiError.None)
513 Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
514 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
519 Log.Error(Globals.LogTag, "Exception on Disconnect\n" + e.ToString());
520 task.SetException(e);
528 /// Deletes the information of a stored access point and disconnects it when the AP is connected.
529 /// 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.
531 /// <since_tizen> 3 </since_tizen>
532 /// <feature>http://tizen.org/feature/network.wifi</feature>
533 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
534 /// <privilege>http://tizen.org/privilege/network.get</privilege>
535 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
536 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
537 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
538 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
539 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
540 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
541 public void ForgetAP()
543 Log.Debug(Globals.LogTag, "ForgetAP");
546 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
548 int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
549 if (ret != (int)WiFiError.None)
551 Log.Error(Globals.LogTag, "Failed to forget AP, Error - " + (WiFiError)ret);
552 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
558 /// An abstract class which is used to represent the WPS information of the access point.
560 /// <since_tizen> 3 </since_tizen>
561 public abstract class WpsInfo
566 /// A class which is used to represent WPS PBC information of the access point.
568 /// <since_tizen> 3 </since_tizen>
569 public class WpsPbcInfo : WpsInfo
574 /// A class which is used to represent WPS PIN information of the access point.
576 /// <since_tizen> 3 </since_tizen>
577 public class WpsPinInfo : WpsInfo
586 /// A public constructor which instantiates WpsPinInfo class with the given pin.
588 /// <since_tizen> 3 </since_tizen>
589 /// <param name="pin">WPS Pin of the access point.</param>
591 /// Pin should not be null or empty. It should be of less than 8 characters.
593 public WpsPinInfo(string pin)
598 internal string GetWpsPin()