[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiManager.cs
1 /*
2  * Copyright (c) 2018 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         /// The Wi-Fi scan state.
133         /// </summary>
134         /// <since_tizen> 6 </since_tizen>
135         /// <value>Represents the scan state of the Wi-Fi.</value>
136         static public WiFiScanState ScanState
137         {
138             get
139             {
140                 return WiFiManagerImpl.Instance.ScanState;
141             }
142         }
143
144         /// <summary>
145         /// DeviceStateChanged is raised when the device state is changed.
146         /// </summary>
147         /// <since_tizen> 3 </since_tizen>
148         /// <privilege>http://tizen.org/privilege/network.get</privilege>
149         /// <feature>http://tizen.org/feature/network.wifi</feature>
150         static public event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
151         {
152             add
153             {
154                 WiFiManagerImpl.Instance.DeviceStateChanged += value;
155             }
156             remove
157             {
158                 WiFiManagerImpl.Instance.DeviceStateChanged -= value;
159             }
160         }
161
162         /// <summary>
163         /// ConnectionStateChanged is raised when the connection state is changed.
164         /// </summary>
165         /// <since_tizen> 3 </since_tizen>
166         /// <privilege>http://tizen.org/privilege/network.get</privilege>
167         /// <feature>http://tizen.org/feature/network.wifi</feature>
168         static public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
169         {
170             add
171             {
172                 WiFiManagerImpl.Instance.ConnectionStateChanged += value;
173             }
174             remove
175             {
176                 WiFiManagerImpl.Instance.ConnectionStateChanged -= value;
177             }
178         }
179
180         /// <summary>
181         /// RssiLevelChanged is raised when the RSSI of the connected Wi-Fi is changed.
182         /// </summary>
183         /// <since_tizen> 3 </since_tizen>
184         /// <privilege>http://tizen.org/privilege/network.get</privilege>
185         /// <feature>http://tizen.org/feature/network.wifi</feature>
186         static public event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
187         {
188             add
189             {
190                 WiFiManagerImpl.Instance.RssiLevelChanged += value;
191             }
192             remove
193             {
194                 WiFiManagerImpl.Instance.RssiLevelChanged -= value;
195             }
196         }
197
198         /// <summary>
199         /// BackgroundScanFinished is raised when the background scan is finished.
200         /// The background scan starts automatically when Wi-Fi is activated. The callback will be invoked periodically.
201         /// </summary>
202         /// <since_tizen> 3 </since_tizen>
203         /// <privilege>http://tizen.org/privilege/network.get</privilege>
204         /// <feature>http://tizen.org/feature/network.wifi</feature>
205         static public event EventHandler BackgroundScanFinished
206         {
207             add
208             {
209                 WiFiManagerImpl.Instance.BackgroundScanFinished += value;
210             }
211             remove
212             {
213                 WiFiManagerImpl.Instance.BackgroundScanFinished -= value;
214             }
215         }
216
217         /// <summary>
218         /// ScanStateChanged is raised when the scan state is changed.
219         /// </summary>
220         /// <since_tizen> 6 </since_tizen>
221         /// <feature>http://tizen.org/feature/network.wifi</feature>
222         static public event EventHandler<ScanStateChangedEventArgs> ScanStateChanged
223         {
224             add
225             {
226                 WiFiManagerImpl.Instance.ScanStateChanged += value;
227             }
228             remove
229             {
230                 WiFiManagerImpl.Instance.ScanStateChanged -= value;
231             }
232         }
233
234         /// <summary>
235         /// Gets the Wi-Fi safe handle.
236         /// </summary>
237         /// <since_tizen> 3 </since_tizen>
238         /// <returns>The instance of the SafeWiFiManagerHandle.</returns>
239         /// <feature>http://tizen.org/feature/network.wifi</feature>
240         /// <privilege>http://tizen.org/privilege/network.get</privilege>
241         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
242         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
243         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
244         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public static SafeWiFiManagerHandle GetWiFiHandle()
247         {
248             return WiFiManagerImpl.Instance.GetSafeHandle();
249         }
250
251         /// <summary>
252         /// Gets the result of the scan.
253         /// </summary>
254         /// <since_tizen> 3 </since_tizen>
255         /// <returns>A list of the WiFiAP objects.</returns>
256         /// <feature>http://tizen.org/feature/network.wifi</feature>
257         /// <privilege>http://tizen.org/privilege/network.get</privilege>
258         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
259         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
260         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
261         static public IEnumerable<WiFiAP> GetFoundAPs()
262         {
263             return WiFiManagerImpl.Instance.GetFoundAPs();
264         }
265
266         /// <summary>
267         /// Gets the result of ScanSpecificAPAsync(string essid) API.
268         /// </summary>
269         /// <since_tizen> 3 </since_tizen>
270         /// <returns>A list containing the WiFiAP objects.</returns>
271         /// <feature>http://tizen.org/feature/network.wifi</feature>
272         /// <privilege>http://tizen.org/privilege/network.get</privilege>
273         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
274         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
275         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
276         static public IEnumerable<WiFiAP> GetFoundSpecificAPs()
277         {
278             return WiFiManagerImpl.Instance.GetFoundSpecificAPs();
279         }
280
281         /// <summary>
282         /// Gets the result of the BssidScanAsync() API.
283         /// </summary>
284         /// <since_tizen> 5 </since_tizen>
285         /// <returns>A list of the WiFiAP objects.</returns>
286         /// <feature>http://tizen.org/feature/network.wifi</feature>
287         /// <privilege>http://tizen.org/privilege/network.get</privilege>
288         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
289         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
290         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
291         static public IEnumerable<WiFiAP> GetFoundBssids()
292         {
293             return WiFiManagerImpl.Instance.GetFoundBssids();
294         }
295
296         /// <summary>
297         /// Gets the list of Wi-Fi configurations.
298         /// </summary>
299         /// <since_tizen> 3 </since_tizen>
300         /// <returns>A list containing the WiFiConfiguration objects.</returns>
301         /// <feature>http://tizen.org/feature/network.wifi</feature>
302         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
303         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
304         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
305         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
306         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
307         static public IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
308         {
309             return WiFiManagerImpl.Instance.GetWiFiConfigurations();
310         }
311
312         /// <summary>
313         /// Saves the Wi-Fi configuration of the access point.
314         /// </summary>
315         /// <since_tizen> 3 </since_tizen>
316         /// <param name="configuration">The configuration to be stored.</param>
317         /// <feature>http://tizen.org/feature/network.wifi</feature>
318         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
319         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
320         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
321         /// <exception cref="ArgumentNullException">Thrown when WiFiConfiguration is passed as null.</exception>
322         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
323         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
324         static public void SaveWiFiConfiguration(WiFiConfiguration configuration)
325         {
326             WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration);
327         }
328
329         /// <summary>
330         /// Gets the object of the connected WiFiAP.
331         /// </summary>
332         /// <since_tizen> 3 </since_tizen>
333         /// <returns>The connected Wi-Fi access point (AP) information.</returns>
334         /// <feature>http://tizen.org/feature/network.wifi</feature>
335         /// <privilege>http://tizen.org/privilege/network.get</privilege>
336         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
337         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
338         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
339         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
340         static public WiFiAP GetConnectedAP()
341         {
342             return WiFiManagerImpl.Instance.GetConnectedAP();
343         }
344
345         /// <summary>
346         /// Activates the Wi-Fi asynchronously.
347         /// </summary>
348         /// <since_tizen> 3 </since_tizen>
349         /// <returns> A task indicating whether the activate method is done or not.</returns>
350         /// <feature>http://tizen.org/feature/network.wifi</feature>
351         /// <privilege>http://tizen.org/privilege/network.set</privilege>
352         /// <privilege>http://tizen.org/privilege/network.get</privilege>
353         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
354         /// <exception cref="NowInProgressException">Thrown when the Wi-Fi activation is now in progress.</exception>
355         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
356         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
357         static public Task ActivateAsync()
358         {
359             return WiFiManagerImpl.Instance.ActivateAsync();
360         }
361
362         /// <summary>
363         /// Activates the Wi-Fi asynchronously and displays the Wi-Fi picker (popup) when the Wi-Fi is not automatically connected.
364         /// </summary>
365         /// <since_tizen> 3 </since_tizen>
366         /// <returns>A task indicating whether the ActivateWithPicker method is done or not.</returns>
367         /// <feature>http://tizen.org/feature/network.wifi</feature>
368         /// <privilege>http://tizen.org/privilege/network.set</privilege>
369         /// <privilege>http://tizen.org/privilege/network.get</privilege>
370         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
371         /// <exception cref="NowInProgressException">Thrown when the Wi-Fi activation is now in progress.</exception>
372         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
373         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
374         static public Task ActivateWithPickerAsync()
375         {
376             return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync();
377         }
378
379         /// <summary>
380         /// Deactivates the Wi-Fi asynchronously.
381         /// </summary>
382         /// <since_tizen> 3 </since_tizen>
383         /// <returns>A task indicating whether the deactivate method is done or not.</returns>
384         /// <feature>http://tizen.org/feature/network.wifi</feature>
385         /// <privilege>http://tizen.org/privilege/network.set</privilege>
386         /// <privilege>http://tizen.org/privilege/network.get</privilege>
387         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
388         /// <exception cref="NowInProgressException">Thrown when the Wi-Fi deactivation is now in progress.</exception>
389         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
390         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
391         static public Task DeactivateAsync()
392         {
393             return WiFiManagerImpl.Instance.DeactivateAsync();
394         }
395
396         /// <summary>
397         /// Starts the scan asynchronously.
398         /// </summary>
399         /// <since_tizen> 3 </since_tizen>
400         /// <returns>A task indicating whether the scan method is done or not.</returns>
401         /// <feature>http://tizen.org/feature/network.wifi</feature>
402         /// <privilege>http://tizen.org/privilege/network.set</privilege>
403         /// <privilege>http://tizen.org/privilege/network.get</privilege>
404         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
405         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
406         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
407         static public Task ScanAsync()
408         {
409             return WiFiManagerImpl.Instance.ScanAsync();
410         }
411
412         /// <summary>
413         /// Starts a specific access point scan asynchronously.
414         /// </summary>
415         /// <since_tizen> 3 </since_tizen>
416         /// <returns>A task indicating whether the ScanSpecificAP method is done or not.</returns>
417         /// <param name="essid">The ESSID of the hidden AP.</param>
418         /// <feature>http://tizen.org/feature/network.wifi</feature>
419         /// <privilege>http://tizen.org/privilege/network.set</privilege>
420         /// <privilege>http://tizen.org/privilege/network.get</privilege>
421         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
422         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
423         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
424         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
425         static public Task ScanSpecificAPAsync(string essid)
426         {
427             return WiFiManagerImpl.Instance.ScanSpecificAPAsync(essid);
428         }
429
430         /// <summary>
431         /// Starts BSSID scan asynchronously.
432         /// </summary>
433         /// <remarks>
434         /// This method must be called from MainThread.
435         /// </remarks>
436         /// <since_tizen> 5 </since_tizen>
437         /// <returns>A task indicating whether the BssidScanAsync method is done or not.</returns>
438         /// <feature>http://tizen.org/feature/network.wifi</feature>
439         /// <privilege>http://tizen.org/privilege/network.set</privilege>
440         /// <privilege>http://tizen.org/privilege/network.get</privilege>
441         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
442         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
443         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
444         static public Task BssidScanAsync()
445         {
446             return WiFiManagerImpl.Instance.BssidScanAsync();
447         }
448     }
449 }