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 int _requestId = 0;
31 private WiFiNetwork _network;
32 private WiFiSecurity _security;
33 private bool disposed = false;
36 /// The network information of the access point(AP).
38 public WiFiNetwork NetworkInformation
47 /// The security information of the access point(AP).
49 public WiFiSecurity SecurityInformation
57 internal WiFiAP(IntPtr handle)
59 Log.Debug(Globals.LogTag, "New WiFiAP. Handle: " + handle);
65 /// Creates an object for the access point.
67 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
68 public WiFiAP(string essid)
70 Log.Debug(Globals.LogTag, "New WiFiAP. Essid: " + essid);
71 createHandle(essid, true);
76 /// Creates an object for the hidden access point.
78 /// <param name="essid">The Extended Service Set Identifier of the access point.</param>
79 /// <param name="hidden">The value to set hidden AP</param>
80 public WiFiAP(string essid, bool hidden)
82 createHandle(essid, hidden);
92 /// A method to destroy the managed WiFiAP objects.
97 GC.SuppressFinalize(this);
100 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.GetHandle(), id, out _apHandle);
124 ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetHandle(), id, out _apHandle);
127 if (ret != (int)WiFiError.None)
129 Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
130 WiFiErrorFactory.ThrowWiFiException(ret);
134 private void Initialize()
136 _network = new WiFiNetwork(_apHandle);
137 _security = new WiFiSecurity(_apHandle);
141 /// Refreshes the access point information.
143 public void Refresh()
145 int ret = Interop.WiFi.AP.Refresh(_apHandle);
146 if (ret != (int)WiFiError.None)
148 Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
149 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle);
153 internal IntPtr GetHandle()
159 /// Connects the access point asynchronously.
161 /// <returns> A task indicating whether the Connect method is done or not.</returns>
162 public Task ConnectAsync()
164 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
168 id = (IntPtr)_requestId++;
169 _callback_map[id] = (error, key) =>
171 Log.Debug(Globals.LogTag, "Connecting finished : " + (WiFiError)error);
172 if (error != (int)WiFiError.None)
174 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
175 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
177 task.SetResult(true);
180 _callback_map.Remove(key);
184 int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetHandle(), _apHandle, _callback_map[id], id);
185 if (ret != (int)WiFiError.None)
187 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
188 WiFiErrorFactory.ThrowWiFiException(ret);
194 /// Connects the access point with WPS PBC asynchronously.
196 /// <returns> A task indicating whether the ConnectByWpsPbs method is done or not.</returns>
197 public Task ConnectByWpsPbcAsync()
199 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
203 id = (IntPtr)_requestId++;
204 _callback_map[id] = (error, key) =>
206 Log.Debug(Globals.LogTag, "Connecting by WPS PBC finished");
207 if (error != (int)WiFiError.None)
209 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
210 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
212 task.SetResult(true);
215 _callback_map.Remove(key);
219 int ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetHandle(), _apHandle, _callback_map[id], id);
220 if (ret != (int)WiFiError.None)
222 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
223 WiFiErrorFactory.ThrowWiFiException(ret);
229 /// Connects the access point with WPS PIN asynchronously.
231 /// <returns> A task indicating whether the ConnectByWpsPin method is done or not.</returns>
232 /// <param name="pin">The WPS PIN is a non-null string with length greater than 0 and less than or equal to 8.</param>
233 public Task ConnectByWpsPinAsync(string pin)
235 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
239 id = (IntPtr)_requestId++;
240 _callback_map[id] = (error, key) =>
242 Log.Debug(Globals.LogTag, "Connecting by WPS PIN finished");
243 if (error != (int)WiFiError.None)
245 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
246 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
248 task.SetResult(true);
251 _callback_map.Remove(key);
255 int ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetHandle(), _apHandle, pin, _callback_map[id], id);
256 if (ret != (int)WiFiError.None)
258 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
259 WiFiErrorFactory.ThrowWiFiException(ret);
265 /// Disconnects the access point asynchronously.
267 /// <returns> A task indicating whether the Disconnect method is done or not.</returns>
268 public Task DisconnectAsync()
270 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
274 id = (IntPtr)_requestId++;
275 _callback_map[id] = (error, key) =>
277 Log.Debug(Globals.LogTag, "Disconnecting finished");
278 if (error != (int)WiFiError.None)
280 Log.Error(Globals.LogTag, "Error occurs during WiFi disconnecting, " + (WiFiError)error);
281 task.SetException(new InvalidOperationException("Error occurs during WiFi disconnecting, " + (WiFiError)error));
283 task.SetResult(true);
286 _callback_map.Remove(key);
290 int ret = Interop.WiFi.Disconnect(WiFiManagerImpl.Instance.GetHandle(), _apHandle, _callback_map[id], id);
291 if (ret != (int)WiFiError.None)
293 Log.Error(Globals.LogTag, "Failed to disconnect wifi, Error - " + (WiFiError)ret);
294 WiFiErrorFactory.ThrowWiFiException(ret);
300 /// Deletes the information of stored access point and disconnects it when it is connected.<br>
301 /// If an AP is connected, then connection information will be stored. This information is used when a connection to that AP is established automatically.
303 public void RemoveAP()
305 int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetHandle(), _apHandle);
306 if (ret != (int)WiFiError.None)
308 Log.Error(Globals.LogTag, "Failed to remove with AP, Error - " + (WiFiError)ret);
309 WiFiErrorFactory.ThrowWiFiException(ret);