[WiFi] TCSACR-149 (#292)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiManager.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
21 using System.ComponentModel;
22
23 namespace Tizen.Network.WiFi
24 {
25     /// <summary>
26     /// A class for managing the WiFiManager handle.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public sealed class SafeWiFiManagerHandle : SafeHandle
31     {
32         private int _tid;
33
34         internal SafeWiFiManagerHandle() : base(IntPtr.Zero, true)
35         {
36         }
37
38         /// <summary>
39         /// Checks the validity of the handle.
40         /// </summary>
41         /// <value>Represents the validity of the handle.</value>
42         /// <since_tizen> 3 </since_tizen>
43         public override bool IsInvalid
44         {
45             get
46             {
47                 return this.handle == IntPtr.Zero;
48             }
49         }
50
51         /// <summary>
52         /// Release the handle
53         /// </summary>
54         protected override bool ReleaseHandle()
55         {
56             Interop.WiFi.Deinitialize(_tid, this.handle);
57             this.SetHandle(IntPtr.Zero);
58             return true;
59         }
60
61         internal void SetTID(int id)
62         {
63             _tid = id;
64             Log.Info(Globals.LogTag, "New Handle for Thread " + _tid);
65         }
66     }
67
68     /// <summary>
69     /// A manager class which allows applications to connect to a Wireless Local Area Network (WLAN) and transfer data over the network.
70     /// The Wi-Fi Manager enables your application to activate and deactivate a local Wi-Fi device, and to connect to a WLAN network in the infrastructure mode.
71     /// </summary>
72     /// <since_tizen> 3 </since_tizen>
73     static public class WiFiManager
74     {
75         /// <summary>
76         /// The local MAC address.
77         /// </summary>
78         /// <since_tizen> 3 </since_tizen>
79         /// <value>Represents the MAC address of the Wi-Fi.</value>
80         /// <privilege>http://tizen.org/privilege/network.get</privilege>
81         static public string MacAddress
82         {
83             get
84             {
85                 return WiFiManagerImpl.Instance.MacAddress;
86             }
87         }
88
89         /// <summary>
90         /// The name of the network interface.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <value>Interface name of the Wi-Fi.</value>
94         /// <privilege>http://tizen.org/privilege/network.get</privilege>
95         static public string InterfaceName
96         {
97             get
98             {
99                 return WiFiManagerImpl.Instance.InterfaceName;
100             }
101         }
102
103         /// <summary>
104         /// The network connection state.
105         /// </summary>
106         /// <since_tizen> 3 </since_tizen>
107         /// <value>Represents the connection state of the Wi-Fi.</value>
108         /// <privilege>http://tizen.org/privilege/network.get</privilege>
109         static public WiFiConnectionState ConnectionState
110         {
111             get
112             {
113                 return WiFiManagerImpl.Instance.ConnectionState;
114             }
115         }
116
117         /// <summary>
118         /// A property to check whether Wi-Fi is activated.
119         /// </summary>
120         /// <since_tizen> 3 </since_tizen>
121         /// <value>Boolean value to check whether Wi-Fi is activated or not.</value>
122         /// <privilege>http://tizen.org/privilege/network.get</privilege>
123         static public bool IsActive
124         {
125             get
126             {
127                 return WiFiManagerImpl.Instance.IsActive;
128             }
129         }
130
131         /// <summary>
132         /// DeviceStateChanged is raised when the device state is changed.
133         /// </summary>
134         /// <since_tizen> 3 </since_tizen>
135         /// <privilege>http://tizen.org/privilege/network.get</privilege>
136         /// <feature>http://tizen.org/feature/network.wifi</feature>
137         static public event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
138         {
139             add
140             {
141                 WiFiManagerImpl.Instance.DeviceStateChanged += value;
142             }
143             remove
144             {
145                 WiFiManagerImpl.Instance.DeviceStateChanged -= value;
146             }
147         }
148
149         /// <summary>
150         /// ConnectionStateChanged is raised when the connection state is changed.
151         /// </summary>
152         /// <since_tizen> 3 </since_tizen>
153         /// <privilege>http://tizen.org/privilege/network.get</privilege>
154         /// <feature>http://tizen.org/feature/network.wifi</feature>
155         static public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
156         {
157             add
158             {
159                 WiFiManagerImpl.Instance.ConnectionStateChanged += value;
160             }
161             remove
162             {
163                 WiFiManagerImpl.Instance.ConnectionStateChanged -= value;
164             }
165         }
166
167         /// <summary>
168         /// RssiLevelChanged is raised when the RSSI of the connected Wi-Fi is changed.
169         /// </summary>
170         /// <since_tizen> 3 </since_tizen>
171         /// <privilege>http://tizen.org/privilege/network.get</privilege>
172         /// <feature>http://tizen.org/feature/network.wifi</feature>
173         static public event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
174         {
175             add
176             {
177                 WiFiManagerImpl.Instance.RssiLevelChanged += value;
178             }
179             remove
180             {
181                 WiFiManagerImpl.Instance.RssiLevelChanged -= value;
182             }
183         }
184
185         /// <summary>
186         /// BackgroundScanFinished is raised when the background scan is finished.
187         /// The background scan starts automatically when Wi-Fi is activated. The callback will be invoked periodically.
188         /// </summary>
189         /// <since_tizen> 3 </since_tizen>
190         /// <privilege>http://tizen.org/privilege/network.get</privilege>
191         /// <feature>http://tizen.org/feature/network.wifi</feature>
192         static public event EventHandler BackgroundScanFinished
193         {
194             add
195             {
196                 WiFiManagerImpl.Instance.BackgroundScanFinished += value;
197             }
198             remove
199             {
200                 WiFiManagerImpl.Instance.BackgroundScanFinished -= value;
201             }
202         }
203
204         /// <summary>
205         /// Gets the Wi-Fi safe handle.
206         /// </summary>
207         /// <since_tizen> 3 </since_tizen>
208         /// <returns>The instance of the SafeWiFiManagerHandle.</returns>
209         /// <feature>http://tizen.org/feature/network.wifi</feature>
210         /// <privilege>http://tizen.org/privilege/network.get</privilege>
211         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
212         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
213         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
214         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
215         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
216         [EditorBrowsable(EditorBrowsableState.Never)]
217         public static SafeWiFiManagerHandle GetWiFiHandle()
218         {
219             return WiFiManagerImpl.Instance.GetSafeHandle();
220         }
221
222         /// <summary>
223         /// Gets the result of the scan.
224         /// </summary>
225         /// <since_tizen> 3 </since_tizen>
226         /// <returns>A list of the WiFiAP objects.</returns>
227         /// <feature>http://tizen.org/feature/network.wifi</feature>
228         /// <privilege>http://tizen.org/privilege/network.get</privilege>
229         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
230         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
231         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
232         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
233         static public IEnumerable<WiFiAP> GetFoundAPs()
234         {
235             return WiFiManagerImpl.Instance.GetFoundAPs();
236         }
237
238         /// <summary>
239         /// Gets the result of ScanSpecificAPAsync(string essid) API.
240         /// </summary>
241         /// <since_tizen> 3 </since_tizen>
242         /// <returns>A list containing the WiFiAP objects.</returns>
243         /// <feature>http://tizen.org/feature/network.wifi</feature>
244         /// <privilege>http://tizen.org/privilege/network.get</privilege>
245         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
246         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
247         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
248         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
249         static public IEnumerable<WiFiAP> GetFoundSpecificAPs()
250         {
251             return WiFiManagerImpl.Instance.GetFoundSpecificAPs();
252         }
253
254         /// <summary>
255         /// Gets the result of the BssidScanAsync() API.
256         /// </summary>
257         /// <since_tizen> 5 </since_tizen>
258         /// <returns>A list of the WiFiAP objects.</returns>
259         /// <feature>http://tizen.org/feature/network.wifi</feature>
260         /// <privilege>http://tizen.org/privilege/network.get</privilege>
261         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
262         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
263         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
264         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
265         static public IEnumerable<WiFiAP> GetFoundBssids()
266         {
267             return WiFiManagerImpl.Instance.GetFoundBssids();
268         }
269
270         /// <summary>
271         /// Gets the list of Wi-Fi configurations.
272         /// </summary>
273         /// <since_tizen> 3 </since_tizen>
274         /// <returns>A list containing the WiFiConfiguration objects.</returns>
275         /// <feature>http://tizen.org/feature/network.wifi</feature>
276         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
277         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
278         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
279         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
280         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
281         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
282         static public IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
283         {
284             return WiFiManagerImpl.Instance.GetWiFiConfigurations();
285         }
286
287         /// <summary>
288         /// Saves the Wi-Fi configuration of the access point.
289         /// </summary>
290         /// <since_tizen> 3 </since_tizen>
291         /// <param name="configuration">The configuration to be stored.</param>
292         /// <feature>http://tizen.org/feature/network.wifi</feature>
293         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
294         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
295         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
296         /// <exception cref="ArgumentNullException">Thrown when WiFiConfiguration is passed as null.</exception>
297         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
298         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
299         static public void SaveWiFiConfiguration(WiFiConfiguration configuration)
300         {
301             WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration);
302         }
303
304         /// <summary>
305         /// Gets the object of the connected WiFiAP.
306         /// </summary>
307         /// <since_tizen> 3 </since_tizen>
308         /// <returns>The connected Wi-Fi access point (AP) information.</returns>
309         /// <feature>http://tizen.org/feature/network.wifi</feature>
310         /// <privilege>http://tizen.org/privilege/network.get</privilege>
311         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
312         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
313         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
314         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
315         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
316         static public WiFiAP GetConnectedAP()
317         {
318             return WiFiManagerImpl.Instance.GetConnectedAP();
319         }
320
321         /// <summary>
322         /// Activates the Wi-Fi asynchronously.
323         /// </summary>
324         /// <since_tizen> 3 </since_tizen>
325         /// <returns> A task indicating whether the activate method is done or not.</returns>
326         /// <feature>http://tizen.org/feature/network.wifi</feature>
327         /// <privilege>http://tizen.org/privilege/network.set</privilege>
328         /// <privilege>http://tizen.org/privilege/network.get</privilege>
329         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
330         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
331         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
332         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
333         static public Task ActivateAsync()
334         {
335             return WiFiManagerImpl.Instance.ActivateAsync();
336         }
337
338         /// <summary>
339         /// Activates the Wi-Fi asynchronously and displays the Wi-Fi picker (popup) when the Wi-Fi is not automatically connected.
340         /// </summary>
341         /// <since_tizen> 3 </since_tizen>
342         /// <returns>A task indicating whether the ActivateWithPicker method is done or not.</returns>
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         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
347         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
348         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
349         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
350         static public Task ActivateWithPickerAsync()
351         {
352             return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync();
353         }
354
355         /// <summary>
356         /// Deactivates the Wi-Fi asynchronously.
357         /// </summary>
358         /// <since_tizen> 3 </since_tizen>
359         /// <returns>A task indicating whether the deactivate method is done or not.</returns>
360         /// <feature>http://tizen.org/feature/network.wifi</feature>
361         /// <privilege>http://tizen.org/privilege/network.set</privilege>
362         /// <privilege>http://tizen.org/privilege/network.get</privilege>
363         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
364         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
365         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
366         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
367         static public Task DeactivateAsync()
368         {
369             return WiFiManagerImpl.Instance.DeactivateAsync();
370         }
371
372         /// <summary>
373         /// Starts the scan asynchronously.
374         /// </summary>
375         /// <since_tizen> 3 </since_tizen>
376         /// <returns>A task indicating whether the scan method is done or not.</returns>
377         /// <feature>http://tizen.org/feature/network.wifi</feature>
378         /// <privilege>http://tizen.org/privilege/network.set</privilege>
379         /// <privilege>http://tizen.org/privilege/network.get</privilege>
380         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
381         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
382         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
383         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
384         static public Task ScanAsync()
385         {
386             return WiFiManagerImpl.Instance.ScanAsync();
387         }
388
389         /// <summary>
390         /// Starts a specific access point scan asynchronously.
391         /// </summary>
392         /// <since_tizen> 3 </since_tizen>
393         /// <returns>A task indicating whether the ScanSpecificAP method is done or not.</returns>
394         /// <param name="essid">The ESSID of the hidden AP.</param>
395         /// <feature>http://tizen.org/feature/network.wifi</feature>
396         /// <privilege>http://tizen.org/privilege/network.set</privilege>
397         /// <privilege>http://tizen.org/privilege/network.get</privilege>
398         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
399         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
400         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
401         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
402         static public Task ScanSpecificAPAsync(string essid)
403         {
404             return WiFiManagerImpl.Instance.ScanSpecificAPAsync(essid);
405         }
406
407         /// <summary>
408         /// Starts BSSID scan asynchronously.
409         /// </summary>
410         /// <remarks>
411         /// This method must be called from MainThread.
412         /// </remarks>
413         /// <since_tizen> 5 </since_tizen>
414         /// <returns>A task indicating whether the BssidScanAsync method is done or not.</returns>
415         /// <feature>http://tizen.org/feature/network.wifi</feature>
416         /// <privilege>http://tizen.org/privilege/network.set</privilege>
417         /// <privilege>http://tizen.org/privilege/network.get</privilege>
418         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
419         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
420         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
421         static public Task BssidScanAsync()
422         {
423             return WiFiManagerImpl.Instance.BssidScanAsync();
424         }
425     }
426 }