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;
21 namespace Tizen.Network.WiFi
24 /// A class for managing the network information of the access point (AP).
26 /// <since_tizen> 3 </since_tizen>
27 public class WiFiAP : IDisposable
29 private IntPtr _apHandle = IntPtr.Zero;
30 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
31 private static Dictionary<IntPtr, Interop.WiFi.VoidCallback> s_callbackMap = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
32 private int _requestId = 0;
33 private static int s_requestId = 0;
34 private WiFiNetwork _network;
35 private WiFiSecurity _security;
36 private bool _disposed = false;
39 /// The network information of the access point (AP).
41 /// <since_tizen> 3 </since_tizen>
42 /// <value>The WiFiNetwork instance containing the network information of the AP.</value>
43 public WiFiNetwork NetworkInformation
52 /// The security information of the access point (AP).
54 /// <since_tizen> 3 </since_tizen>
55 /// <value>The WiFiSecurity instance containing security information of the AP.</value>
56 public WiFiSecurity SecurityInformation
64 internal WiFiAP(IntPtr handle)
66 Log.Debug(Globals.LogTag, "New WiFiAP. Handle: " + handle);
72 /// Creates an object for the access point.
74 /// <since_tizen> 3 </since_tizen>
75 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
76 /// <feature>http://tizen.org/feature/network.wifi</feature>
77 /// <privilege>http://tizen.org/privilege/network.get</privilege>
78 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
79 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
80 /// <exception cref="ArgumentNullException">Thrown when the ESSID is passed as null.</exception>
81 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
82 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
83 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
84 public WiFiAP(string essid)
86 Log.Debug(Globals.LogTag, "New WiFiAP. Essid: " + essid);
87 createHandle(essid, true);
92 /// Creates an object for the hidden access point.
94 /// <since_tizen> 3 </since_tizen>
95 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
96 /// <param name="hidden">The value to set a hidden AP.</param>
97 /// <feature>http://tizen.org/feature/network.wifi</feature>
98 /// <privilege>http://tizen.org/privilege/network.get</privilege>
99 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
100 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
101 /// <exception cref="ArgumentNullException">Thrown when the ESSID is passed as null.</exception>
102 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
103 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
104 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
105 public WiFiAP(string essid, bool hidden)
107 createHandle(essid, hidden);
112 /// Destroy the WiFiAP object
120 /// A method to destroy the managed WiFiAP objects.
122 /// <since_tizen> 3 </since_tizen>
123 public void Dispose()
126 GC.SuppressFinalize(this);
129 private void Dispose(bool disposing)
136 Interop.WiFi.AP.Destroy(_apHandle);
137 _apHandle = IntPtr.Zero;
142 private void createHandle(string id, bool hidden)
147 throw new ArgumentNullException("Essid is null");
152 ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
157 ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
160 if (ret != (int)WiFiError.None)
162 Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
163 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
167 private void Initialize()
169 Interop.WiFi.SafeWiFiAPHandle apHandle = new Interop.WiFi.SafeWiFiAPHandle(_apHandle);
170 _network = new WiFiNetwork(apHandle);
171 _security = new WiFiSecurity(apHandle);
175 /// Refreshes the access point information.
177 /// <since_tizen> 3 </since_tizen>
178 /// <feature>http://tizen.org/feature/network.wifi</feature>
179 /// <privilege>http://tizen.org/privilege/network.get</privilege>
180 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
181 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
182 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
183 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
184 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
185 public void Refresh()
187 Log.Debug(Globals.LogTag, "Refresh");
190 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
192 int ret = Interop.WiFi.AP.Refresh(_apHandle);
193 if (ret != (int)WiFiError.None)
195 Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
196 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle, "http://tizen.org/privilege/network.get");
201 /// Connects the access point asynchronously.
203 /// <since_tizen> 3 </since_tizen>
204 /// <returns> A task indicating whether the connect method is done or not.</returns>
205 /// <feature>http://tizen.org/feature/network.wifi</feature>
206 /// <privilege>http://tizen.org/privilege/network.set</privilege>
207 /// <privilege>http://tizen.org/privilege/network.get</privilege>
208 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
209 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
210 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
211 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
212 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
213 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
214 public Task ConnectAsync()
216 Log.Debug(Globals.LogTag, "ConnectAsync");
219 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
221 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
225 id = (IntPtr)_requestId++;
226 _callback_map[id] = (error, key) =>
228 Log.Debug(Globals.LogTag, "Connecting finished : " + (WiFiError)error);
229 if (error != (int)WiFiError.None)
231 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
232 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
236 task.SetResult(true);
240 _callback_map.Remove(key);
245 int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
246 if (ret != (int)WiFiError.None)
248 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
249 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
256 /// Connects the access point with the WPS asynchronously.
258 /// <since_tizen> 3 </since_tizen>
259 /// <param name="info">A WpsInfo instance which is type of WpsPbcInfo or WpsPinInfo.</param>
260 /// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
261 /// <feature>http://tizen.org/feature/network.wifi</feature>
262 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
263 /// <privilege>http://tizen.org/privilege/network.get</privilege>
264 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
265 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
266 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
267 /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
268 /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is an empty string or more than 7 characters.</exception>
269 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
270 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
271 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
272 public Task ConnectWpsAsync(WpsInfo info)
274 Log.Debug(Globals.LogTag, "ConnectWpsAsync");
277 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
279 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
283 id = (IntPtr)_requestId++;
284 _callback_map[id] = (error, key) =>
286 Log.Debug(Globals.LogTag, "Connecting by WPS finished");
287 if (error != (int)WiFiError.None)
289 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
290 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
294 task.SetResult(true);
298 _callback_map.Remove(key);
304 if (info.GetType() == typeof(WpsPbcInfo))
306 ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
309 else if (info.GetType() == typeof(WpsPinInfo))
311 WpsPinInfo pinInfo = (WpsPinInfo)info;
312 if (pinInfo.GetWpsPin() == null)
314 throw new ArgumentNullException("Wps pin should not be null");
317 if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
319 throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
322 ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
325 if (ret != (int)WiFiError.None)
327 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
328 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
335 /// Connects the access point with WPS without SSID asynchronously.
337 /// <since_tizen> 3 </since_tizen>
338 /// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
339 /// <returns>A task which contains Connected access point information.</returns>
341 /// If WpsPinInfo is used, its object has to be constructed with a pin which must be 4 or 8 characters long.
343 /// <feature>http://tizen.org/feature/network.wifi</feature>
344 /// <privilege>http://tizen.org/privilege/network.set</privilege>
345 /// <privilege>http://tizen.org/privilege/network.get</privilege>
346 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
347 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
348 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
349 /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
350 /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is not of 4 or 8 characters long.</exception>
351 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
352 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
353 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
354 public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
356 TaskCompletionSource<WiFiAP> task = new TaskCompletionSource<WiFiAP>();
360 id = (IntPtr)s_requestId++;
361 s_callbackMap[id] = (error, key) =>
363 Log.Debug(Globals.LogTag, "Connecting by WPS finished");
364 if (error != (int)WiFiError.None)
366 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
367 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
371 WiFiAP ap = WiFiManagerImpl.Instance.GetConnectedAP();
376 s_callbackMap.Remove(key);
382 if (info.GetType() == typeof(WpsPbcInfo))
384 ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);
387 else if (info.GetType() == typeof(WpsPinInfo))
389 WpsPinInfo pinInfo = (WpsPinInfo)info;
390 if (pinInfo.GetWpsPin() == null)
392 throw new ArgumentNullException("Wps pin should not be null");
395 if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
397 throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long");
400 ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id);
403 if (ret != (int)WiFiError.None)
405 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
406 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
413 /// Disconnects the access point asynchronously.
415 /// <since_tizen> 3 </since_tizen>
416 /// <returns> A task indicating whether the disconnect method is done or not.</returns>
417 /// <feature>http://tizen.org/feature/network.wifi</feature>
418 /// <privilege>http://tizen.org/privilege/network.set</privilege>
419 /// <privilege>http://tizen.org/privilege/network.get</privilege>
420 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
421 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
422 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
423 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
424 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
425 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
426 public Task DisconnectAsync()
428 Log.Debug(Globals.LogTag, "DisconnectAsync");
431 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
433 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
437 id = (IntPtr)_requestId++;
438 _callback_map[id] = (error, key) =>
440 Log.Debug(Globals.LogTag, "Disconnecting finished");
441 if (error != (int)WiFiError.None)
443 Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
444 task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
448 task.SetResult(true);
452 _callback_map.Remove(key);
456 int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
457 if (ret != (int)WiFiError.None)
459 Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
460 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
466 /// Deletes the information of a stored access point and disconnects it when the AP is connected.
467 /// 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.
469 /// <since_tizen> 3 </since_tizen>
470 /// <feature>http://tizen.org/feature/network.wifi</feature>
471 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
472 /// <privilege>http://tizen.org/privilege/network.get</privilege>
473 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
474 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
475 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
476 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
477 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
478 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
479 public void ForgetAP()
481 Log.Debug(Globals.LogTag, "ForgetAP");
484 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
486 int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
487 if (ret != (int)WiFiError.None)
489 Log.Error(Globals.LogTag, "Failed to forget AP, Error - " + (WiFiError)ret);
490 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
495 /// Update the information of a stored access point.
496 /// When a AP information is changed, the change will not be applied until this method is called.
498 /// <since_tizen> 5 </since_tizen>
499 /// <feature>http://tizen.org/feature/network.wifi</feature>
500 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
501 /// <privilege>http://tizen.org/privilege/network.get</privilege>
502 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
503 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
504 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
505 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
506 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
509 Log.Debug(Globals.LogTag, "Update");
512 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
514 int ret = Interop.WiFi.UpdateAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
515 if (ret != (int)WiFiError.None)
517 Log.Error(Globals.LogTag, "Failed to update AP, Error - " + (WiFiError)ret);
518 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
524 /// An abstract class which is used to represent the WPS information of the access point.
526 /// <since_tizen> 3 </since_tizen>
527 public abstract class WpsInfo
532 /// A class which is used to represent WPS PBC information of the access point.
534 /// <since_tizen> 3 </since_tizen>
535 public class WpsPbcInfo : WpsInfo
540 /// A class which is used to represent WPS PIN information of the access point.
542 /// <since_tizen> 3 </since_tizen>
543 public class WpsPinInfo : WpsInfo
552 /// A public constructor which instantiates WpsPinInfo class with the given pin.
554 /// <since_tizen> 3 </since_tizen>
555 /// <param name="pin">WPS Pin of the access point.</param>
557 /// Pin should not be null or empty. It should be of less than 8 characters.
559 public WpsPinInfo(string pin)
564 internal string GetWpsPin()