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>
206 /// <feature>http://tizen.org/feature/network.wifi</feature>
207 /// <privilege>http://tizen.org/privilege/network.set</privilege>
208 /// <privilege>http://tizen.org/privilege/network.get</privilege>
209 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
210 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
211 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
212 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
213 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
214 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
215 public Task ConnectAsync()
217 Log.Info(Globals.LogTag, "ConnectAsync");
220 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
222 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
226 id = (IntPtr)_requestId++;
227 _callback_map[id] = (error, key) =>
229 Log.Info(Globals.LogTag, "ConnectAsync done " + (WiFiError)error);
230 if (error != (int)WiFiError.None)
232 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
233 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
237 task.SetResult(true);
241 _callback_map.Remove(key);
248 Log.Info(Globals.LogTag, "Interop.WiFi.Connect");
249 int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
250 if (ret != (int)WiFiError.None)
252 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
253 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
261 /// Connects the access point with the WPS asynchronously.
263 /// <since_tizen> 3 </since_tizen>
264 /// <param name="info">A WpsInfo instance which is type of WpsPbcInfo or WpsPinInfo.</param>
265 /// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
266 /// <feature>http://tizen.org/feature/network.wifi</feature>
267 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
268 /// <privilege>http://tizen.org/privilege/network.get</privilege>
269 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
270 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
271 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
272 /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
273 /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is an empty string or more than 7 characters.</exception>
274 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
275 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
276 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
277 public Task ConnectWpsAsync(WpsInfo info)
279 Log.Info(Globals.LogTag, "ConnectWpsAsync");
282 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
284 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
288 id = (IntPtr)_requestId++;
289 _callback_map[id] = (error, key) =>
291 Log.Info(Globals.LogTag, "ConnectWpsAsync done");
292 if (error != (int)WiFiError.None)
294 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
295 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
299 task.SetResult(true);
303 _callback_map.Remove(key);
311 if (info.GetType() == typeof(WpsPbcInfo))
313 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPb");
314 ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
316 else if (info.GetType() == typeof(WpsPinInfo))
318 WpsPinInfo pinInfo = (WpsPinInfo)info;
319 if (pinInfo.GetWpsPin() == null)
321 throw new ArgumentNullException("Wps pin should not be null");
324 if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
326 throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
329 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPin");
330 ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
333 if (ret != (int)WiFiError.None)
335 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
336 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
344 /// Connects the access point with WPS without SSID asynchronously.
346 /// <since_tizen> 3 </since_tizen>
347 /// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
348 /// <returns>A task which contains Connected access point information.</returns>
350 /// If WpsPinInfo is used, its object has to be constructed with a pin which must be 4 or 8 characters long.
352 /// <feature>http://tizen.org/feature/network.wifi</feature>
353 /// <privilege>http://tizen.org/privilege/network.set</privilege>
354 /// <privilege>http://tizen.org/privilege/network.get</privilege>
355 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
356 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
357 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
358 /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
359 /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is not of 4 or 8 characters long.</exception>
360 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
361 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
362 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
363 public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
365 Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync");
366 TaskCompletionSource<WiFiAP> task = new TaskCompletionSource<WiFiAP>();
370 id = (IntPtr)s_requestId++;
371 s_callbackMap[id] = (error, key) =>
373 Log.Info(Globals.LogTag, "ConnectWpsWithoutSsidAsync done");
374 if (error != (int)WiFiError.None)
376 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
377 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
381 WiFiAP ap = WiFiManagerImpl.Instance.GetConnectedAP();
386 s_callbackMap.Remove(key);
391 s_context.Post((x) =>
394 if (info.GetType() == typeof(WpsPbcInfo))
396 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPbcWithoutSsid");
397 ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);
399 else if (info.GetType() == typeof(WpsPinInfo))
401 WpsPinInfo pinInfo = (WpsPinInfo)info;
402 if (pinInfo.GetWpsPin() == null)
404 throw new ArgumentNullException("Wps pin should not be null");
407 if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
409 throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long");
412 Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPinWithoutSsid");
413 ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id);
416 if (ret != (int)WiFiError.None)
418 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
419 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
427 /// Disconnects the access point asynchronously.
429 /// <since_tizen> 3 </since_tizen>
430 /// <returns> A task indicating whether the disconnect method is done or not.</returns>
431 /// <feature>http://tizen.org/feature/network.wifi</feature>
432 /// <privilege>http://tizen.org/privilege/network.set</privilege>
433 /// <privilege>http://tizen.org/privilege/network.get</privilege>
434 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
435 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
436 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
437 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
438 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
439 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
440 public Task DisconnectAsync()
442 Log.Debug(Globals.LogTag, "DisconnectAsync");
445 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
447 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
451 id = (IntPtr)_requestId++;
452 _callback_map[id] = (error, key) =>
454 Log.Info(Globals.LogTag, "DisconnectAsync done");
455 if (error != (int)WiFiError.None)
457 Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
458 task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
462 task.SetResult(true);
466 _callback_map.Remove(key);
473 Log.Info(Globals.LogTag, "Interop.WiFi.Disconnect");
474 int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
475 if (ret != (int)WiFiError.None)
477 Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
478 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
486 /// Deletes the information of a stored access point and disconnects it when the AP is connected.
487 /// 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.
489 /// <since_tizen> 3 </since_tizen>
490 /// <feature>http://tizen.org/feature/network.wifi</feature>
491 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
492 /// <privilege>http://tizen.org/privilege/network.get</privilege>
493 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
494 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
495 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
496 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
497 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
498 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
499 public void ForgetAP()
501 Log.Debug(Globals.LogTag, "ForgetAP");
504 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
506 int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
507 if (ret != (int)WiFiError.None)
509 Log.Error(Globals.LogTag, "Failed to forget AP, Error - " + (WiFiError)ret);
510 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
515 /// Update the information of a stored access point.
516 /// When a AP information is changed, the change will not be applied until this method is called.
518 /// <since_tizen> 5 </since_tizen>
519 /// <feature>http://tizen.org/feature/network.wifi</feature>
520 /// <privilege>http://tizen.org/privilege/network.profile</privilege>
521 /// <privilege>http://tizen.org/privilege/network.get</privilege>
522 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
523 /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
524 /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
525 /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
526 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
529 Log.Debug(Globals.LogTag, "Update");
532 throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
534 int ret = Interop.WiFi.UpdateAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
535 if (ret != (int)WiFiError.None)
537 Log.Error(Globals.LogTag, "Failed to update AP, Error - " + (WiFiError)ret);
538 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
544 /// An abstract class which is used to represent the WPS information of the access point.
546 /// <since_tizen> 3 </since_tizen>
547 public abstract class WpsInfo
552 /// A class which is used to represent WPS PBC information of the access point.
554 /// <since_tizen> 3 </since_tizen>
555 public class WpsPbcInfo : WpsInfo
560 /// A class which is used to represent WPS PIN information of the access point.
562 /// <since_tizen> 3 </since_tizen>
563 public class WpsPinInfo : WpsInfo
572 /// A public constructor which instantiates WpsPinInfo class with the given pin.
574 /// <since_tizen> 3 </since_tizen>
575 /// <param name="pin">WPS Pin of the access point.</param>
577 /// Pin should not be null or empty. It should be of less than 8 characters.
579 public WpsPinInfo(string pin)
584 internal string GetWpsPin()