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)
107 Interop.WiFi.AP.Destroy(_apHandle);
108 _apHandle = IntPtr.Zero;
113 private void createHandle(string id, bool hidden)
118 ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
123 ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetSafeHandle(), id, out _apHandle);
126 if (ret != (int)WiFiError.None)
128 Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
129 WiFiErrorFactory.ThrowWiFiException(ret);
133 private void Initialize()
135 Interop.WiFi.SafeWiFiAPHandle apHandle = new Interop.WiFi.SafeWiFiAPHandle(_apHandle);
136 _network = new WiFiNetwork(apHandle);
137 _security = new WiFiSecurity(apHandle);
141 /// Refreshes the access point information.
143 public void Refresh()
145 Log.Debug(Globals.LogTag, "Refresh");
146 int ret = Interop.WiFi.AP.Refresh(_apHandle);
147 if (ret != (int)WiFiError.None)
149 Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
150 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle);
155 /// Connects the access point asynchronously.
157 /// <returns> A task indicating whether the Connect method is done or not.</returns>
158 public Task ConnectAsync()
160 Log.Debug(Globals.LogTag, "ConnectAsync");
161 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
165 id = (IntPtr)_requestId++;
166 _callback_map[id] = (error, key) =>
168 Log.Debug(Globals.LogTag, "Connecting finished : " + (WiFiError)error);
169 if (error != (int)WiFiError.None)
171 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
172 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
174 task.SetResult(true);
177 _callback_map.Remove(key);
181 int ret = Interop.WiFi.Connect(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
182 if (ret != (int)WiFiError.None)
184 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
185 WiFiErrorFactory.ThrowWiFiException(ret);
191 /// Connects the access point with WPS PBC asynchronously.
193 /// <returns> A task indicating whether the ConnectByWpsPbs method is done or not.</returns>
194 public Task ConnectByWpsPbcAsync()
196 Log.Debug(Globals.LogTag, "ConnectByWpsPbcAsync");
197 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
201 id = (IntPtr)_requestId++;
202 _callback_map[id] = (error, key) =>
204 Log.Debug(Globals.LogTag, "Connecting by WPS PBC finished");
205 if (error != (int)WiFiError.None)
207 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
208 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
210 task.SetResult(true);
213 _callback_map.Remove(key);
217 int ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
218 if (ret != (int)WiFiError.None)
220 Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
221 WiFiErrorFactory.ThrowWiFiException(ret);
227 /// Connects the access point with WPS PIN asynchronously.
229 /// <returns> A task indicating whether the ConnectByWpsPin method is done or not.</returns>
230 /// <param name="pin">The WPS PIN is a non-null string with length greater than 0 and less than or equal to 8.</param>
231 public Task ConnectByWpsPinAsync(string pin)
233 Log.Debug(Globals.LogTag, "ConnectByWpsPinAsync");
234 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
238 id = (IntPtr)_requestId++;
239 _callback_map[id] = (error, key) =>
241 Log.Debug(Globals.LogTag, "Connecting by WPS PIN finished");
242 if (error != (int)WiFiError.None)
244 Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
245 task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
247 task.SetResult(true);
250 _callback_map.Remove(key);
254 int ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pin, _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);
264 /// Disconnects the access point asynchronously.
266 /// <returns> A task indicating whether the Disconnect method is done or not.</returns>
267 public Task DisconnectAsync()
269 Log.Debug(Globals.LogTag, "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.GetSafeHandle(), _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 Log.Debug(Globals.LogTag, "RemoveAP");
306 int ret = Interop.WiFi.RemoveAP(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle);
307 if (ret != (int)WiFiError.None)
309 Log.Error(Globals.LogTag, "Failed to remove with AP, Error - " + (WiFiError)ret);
310 WiFiErrorFactory.ThrowWiFiException(ret);