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 public class WiFiAP : IDisposable
28 private IntPtr _apHandle = IntPtr.Zero;
29 private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
30 private static Dictionary<IntPtr, Interop.WiFi.VoidCallback> s_callbackMap = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
31 private int _requestId = 0;
32 private static int s_requestId = 0;
33 private WiFiNetwork _network;
34 private WiFiSecurity _security;
35 private bool _disposed = false;
38 /// The network information of the access point(AP).
40 public WiFiNetwork NetworkInformation
49 /// The security information of the access point(AP).
51 public WiFiSecurity SecurityInformation
59 internal WiFiAP(IntPtr handle)
61 Log.Debug(Globals.LogTag, "New WiFiAP. Handle: " + handle);
67 /// Creates an object for the access point.
69 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
70 public WiFiAP(string essid)
72 Log.Debug(Globals.LogTag, "New WiFiAP. Essid: " + essid);
73 createHandle(essid, true);
78 /// Creates an object for the hidden access point.
80 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
81 /// <param name="hidden">The value to set hidden AP</param>
82 public WiFiAP(string essid, bool hidden)
84 createHandle(essid, hidden);
94 /// A method to destroy the managed WiFiAP objects.
99 GC.SuppressFinalize(this);
102 private void Dispose(bool disposing)
109 Interop.WiFi.AP.Destroy(_apHandle);
110 _apHandle = IntPtr.Zero;
115 private void createHandle(string id, bool hidden)
120 ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
125 ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
128 if (ret != (int)WiFiError.None)
130 Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
131 WiFiErrorFactory.ThrowWiFiException(ret);
135 private void Initialize()
137 Interop.WiFi.SafeWiFiAPHandle apHandle = new Interop.WiFi.SafeWiFiAPHandle(_apHandle);
138 _network = new WiFiNetwork(apHandle);
139 _security = new WiFiSecurity(apHandle);
143 /// Refreshes the access point information.
145 public void Refresh()
147 Log.Debug(Globals.LogTag, "Refresh");
148 int ret = Interop.WiFi.AP.Refresh(_apHandle);
149 if (ret != (int)WiFiError.None)
151 Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
152 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle);
157 /// Connects the access point asynchronously.
159 /// <returns> A task indicating whether the Connect method is done or not.</returns>
160 public Task ConnectAsync()
162 Log.Debug(Globals.LogTag, "ConnectAsync");
163 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
167 id = (IntPtr)_requestId++;
168 _callback_map[id] = (error, key) =>
170 Log.Debug(Globals.LogTag, "Connecting finished : " + (WiFiError)error);
171 if (error != (int)WiFiError.None)
173 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
174 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
176 task.SetResult(true);
179 _callback_map.Remove(key);
183 int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
184 if (ret != (int)WiFiError.None)
186 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
187 WiFiErrorFactory.ThrowWiFiException(ret);
193 /// Connects the access point with WPS asynchronously.
195 /// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
196 /// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
197 public Task ConnectWpsAsync(WpsInfo info)
199 Log.Debug(Globals.LogTag, "ConnectWpsAsync");
200 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
204 id = (IntPtr)_requestId++;
205 _callback_map[id] = (error, key) =>
207 Log.Debug(Globals.LogTag, "Connecting by WPS finished");
208 if (error != (int)WiFiError.None)
210 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
211 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
213 task.SetResult(true);
216 _callback_map.Remove(key);
221 if (info.GetType() == typeof(WpsPbcInfo))
223 int ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
224 if (ret != (int)WiFiError.None)
226 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
227 WiFiErrorFactory.ThrowWiFiException(ret);
231 else if (info.GetType() == typeof(WpsPinInfo))
233 WpsPinInfo pinInfo = (WpsPinInfo)info;
234 int ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
235 if (ret != (int)WiFiError.None)
237 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
238 WiFiErrorFactory.ThrowWiFiException(ret);
246 /// Connects the access point with WPS without ssid asynchronously.
248 /// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
249 /// <returns>A task which contains Connected access point information.</returns>
250 public static Task<WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
252 TaskCompletionSource<WiFiAP> task = new TaskCompletionSource<WiFiAP>();
256 id = (IntPtr)s_requestId++;
257 s_callbackMap[id] = (error, key) =>
259 Log.Debug(Globals.LogTag, "Connecting by WPS finished");
260 if (error != (int)WiFiError.None)
262 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
263 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
265 WiFiAP ap = WiFiManagerImpl.Instance.GetConnectedAP();
269 s_callbackMap.Remove(key);
274 if (info.GetType() == typeof(WpsPbcInfo))
276 int ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);
277 if (ret != (int)WiFiError.None)
279 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
280 WiFiErrorFactory.ThrowWiFiException(ret);
284 else if (info.GetType() == typeof(WpsPinInfo))
286 WpsPinInfo pinInfo = (WpsPinInfo)info;
287 int ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id);
288 if (ret != (int)WiFiError.None)
290 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
291 WiFiErrorFactory.ThrowWiFiException(ret);
299 /// Disconnects the access point asynchronously.
301 /// <returns> A task indicating whether the Disconnect method is done or not.</returns>
302 public Task DisconnectAsync()
304 Log.Debug(Globals.LogTag, "DisconnectAsync");
305 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
309 id = (IntPtr)_requestId++;
310 _callback_map[id] = (error, key) =>
312 Log.Debug(Globals.LogTag, "Disconnecting finished");
313 if (error != (int)WiFiError.None)
315 Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
316 task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
318 task.SetResult(true);
321 _callback_map.Remove(key);
325 int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
326 if (ret != (int)WiFiError.None)
328 Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
329 WiFiErrorFactory.ThrowWiFiException(ret);
335 /// Deletes the information of stored access point and disconnects it when it is connected.<br>
336 /// If an AP is connected, then connection information will be stored. This information is used when a connection to that AP is established automatically.
338 public void ForgetAP()
340 Log.Debug(Globals.LogTag, "ForgetAP");
341 int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
342 if (ret != (int)WiFiError.None)
344 Log.Error(Globals.LogTag, "Failed to forget AP, Error - " + (WiFiError)ret);
345 WiFiErrorFactory.ThrowWiFiException(ret);
351 /// An abstract class which is used to represent WPS information of access point.
353 public abstract class WpsInfo
358 /// A class which is used to represent WPS PBC information of access point.
360 public class WpsPbcInfo : WpsInfo
365 /// A class which is used to represent WPS PIN information of access point.
367 public class WpsPinInfo : WpsInfo
376 /// A public constructor which instantiates WpsPinInfo class with the given pin.
378 /// <param name="pin">WPS Pin of the access point.</param>
379 public WpsPinInfo(string pin)
384 internal string GetWpsPin()