Release 4.0.0-preview1-00051
[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 WiFiManager handle.
27     /// </summary>
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public sealed class SafeWiFiManagerHandle : SafeHandle
30     {
31         internal SafeWiFiManagerHandle() : base(IntPtr.Zero, true)
32         {
33         }
34
35         /// <summary>
36         /// Checks the validity of the handle.
37         /// </summary>
38         /// <value>Represents the validity of the handle.</value>
39         public override bool IsInvalid
40         {
41             get
42             {
43                 return this.handle == IntPtr.Zero;
44             }
45         }
46
47         protected override bool ReleaseHandle()
48         {
49             Interop.WiFi.Deinitialize(this.handle);
50             this.SetHandle(IntPtr.Zero);
51             return true;
52         }
53     }
54
55     /// <summary>
56     /// A manager class which allows applications to connect to a Wireless Local Area Network (WLAN) and to transfer data over the network.<br>
57     /// 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.
58     /// </summary>
59     /// <since_tizen> 3 </since_tizen>
60     static public class WiFiManager
61     {
62         /// <summary>
63         /// The local MAC address.
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         /// <value>Represents the mac address of the WiFi.</value>
67         /// <privilege>http://tizen.org/privilege/network.get</privilege>
68         static public string MacAddress
69         {
70             get
71             {
72                 return WiFiManagerImpl.Instance.MacAddress;
73             }
74         }
75
76         /// <summary>
77         /// The name of the network interface.
78         /// </summary>
79         /// <since_tizen> 3 </since_tizen>
80         /// <value>Interface name of WiFi.</value>
81         /// <privilege>http://tizen.org/privilege/network.get</privilege>
82         static public string InterfaceName
83         {
84             get
85             {
86                 return WiFiManagerImpl.Instance.InterfaceName;
87             }
88         }
89
90         /// <summary>
91         /// The network connection state.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         /// <value>Represents the connection state of WiFi.</value>
95         /// <privilege>http://tizen.org/privilege/network.get</privilege>
96         static public WiFiConnectionState ConnectionState
97         {
98             get
99             {
100                 return WiFiManagerImpl.Instance.ConnectionState;
101             }
102         }
103
104         /// <summary>
105         /// A property to Check whether Wi-Fi is activated.
106         /// </summary>
107         /// <since_tizen> 3 </since_tizen>
108         /// <value>Boolean value to check whether WiFi is activated or not.</value>
109         /// <privilege>http://tizen.org/privilege/network.get</privilege>
110         static public bool IsActive
111         {
112             get
113             {
114                 return WiFiManagerImpl.Instance.IsActive;
115             }
116         }
117
118         /// <summary>
119         /// DeviceStateChanged is raised when the device state is changed.
120         /// </summary>
121         /// <since_tizen> 3 </since_tizen>
122         /// <privilege>http://tizen.org/privilege/network.get</privilege>
123         /// <feature>http://tizen.org/feature/network.wifi</feature>
124         static public event EventHandler<DeviceStateChangedEventArgs> DeviceStateChanged
125         {
126             add
127             {
128                 WiFiManagerImpl.Instance.DeviceStateChanged += value;
129             }
130             remove
131             {
132                 WiFiManagerImpl.Instance.DeviceStateChanged -= value;
133             }
134         }
135
136         /// <summary>
137         /// ConnectionStateChanged is raised when the connection state is changed.
138         /// </summary>
139         /// <since_tizen> 3 </since_tizen>
140         /// <privilege>http://tizen.org/privilege/network.get</privilege>
141         /// <feature>http://tizen.org/feature/network.wifi</feature>
142         static public event EventHandler<ConnectionStateChangedEventArgs> ConnectionStateChanged
143         {
144             add
145             {
146                 WiFiManagerImpl.Instance.ConnectionStateChanged += value;
147             }
148             remove
149             {
150                 WiFiManagerImpl.Instance.ConnectionStateChanged -= value;
151             }
152         }
153
154         /// <summary>
155         /// RssiLevelChanged is raised when the RSSI of connected Wi-Fi is changed.
156         /// </summary>
157         /// <since_tizen> 3 </since_tizen>
158         /// <privilege>http://tizen.org/privilege/network.get</privilege>
159         /// <feature>http://tizen.org/feature/network.wifi</feature>
160         static public event EventHandler<RssiLevelChangedEventArgs> RssiLevelChanged
161         {
162             add
163             {
164                 WiFiManagerImpl.Instance.RssiLevelChanged += value;
165             }
166             remove
167             {
168                 WiFiManagerImpl.Instance.RssiLevelChanged -= value;
169             }
170         }
171
172         /// <summary>
173         /// BackgroundScanFinished is raised when the background scan is finished.
174         /// The background scan starts automatically when wifi is activated. The callback will be invoked periodically.
175         /// </summary>
176         /// <since_tizen> 3 </since_tizen>
177         /// <privilege>http://tizen.org/privilege/network.get</privilege>
178         /// <feature>http://tizen.org/feature/network.wifi</feature>
179         static public event EventHandler BackgroundScanFinished
180         {
181             add
182             {
183                 WiFiManagerImpl.Instance.BackgroundScanFinished += value;
184             }
185             remove
186             {
187                 WiFiManagerImpl.Instance.BackgroundScanFinished -= value;
188             }
189         }
190
191         /// <summary>
192         /// Gets the WiFi safe handle.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         /// <returns>The instance of SafeWiFiManagerHandle</returns>
196         /// <feature>http://tizen.org/feature/network.wifi</feature>
197         /// <privilege>http://tizen.org/privilege/network.get</privilege>
198         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
199         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
200         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
201         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
202         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
203         [EditorBrowsable(EditorBrowsableState.Never)]
204         public static SafeWiFiManagerHandle GetWiFiHandle()
205         {
206             return WiFiManagerImpl.Instance.GetSafeHandle();
207         }
208
209         /// <summary>
210         /// Gets the result of the scan.
211         /// </summary>
212         /// <since_tizen> 3 </since_tizen>
213         /// <returns> A list of WiFiAP objects.</returns>
214         /// <feature>http://tizen.org/feature/network.wifi</feature>
215         /// <privilege>http://tizen.org/privilege/network.get</privilege>
216         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
217         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
218         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
219         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
220         static public IEnumerable<WiFiAP> GetFoundAPs()
221         {
222             return WiFiManagerImpl.Instance.GetFoundAPs();
223         }
224
225         /// <summary>
226         /// Gets the result of specific AP scan.
227         /// </summary>
228         /// <since_tizen> 3 </since_tizen>
229         /// <returns> A list contains the WiFiAP objects.</returns>
230         /// <feature>http://tizen.org/feature/network.wifi</feature>
231         /// <privilege>http://tizen.org/privilege/network.get</privilege>
232         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
233         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
234         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
235         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
236         static public IEnumerable<WiFiAP> GetFoundSpecificAPs()
237         {
238             return WiFiManagerImpl.Instance.GetFoundSpecificAPs();
239         }
240
241         /// <summary>
242         /// Gets the list of wifi configurations.
243         /// </summary>
244         /// <since_tizen> 3 </since_tizen>
245         /// <returns>A list contains the WiFiConfiguration objects.</returns>
246         /// <feature>http://tizen.org/feature/network.wifi</feature>
247         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
248         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
249         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
250         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
251         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
252         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
253         static public IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
254         {
255             return WiFiManagerImpl.Instance.GetWiFiConfigurations();
256         }
257
258         /// <summary>
259         /// Saves Wi-Fi configuration of access point.
260         /// </summary>
261         /// <since_tizen> 3 </since_tizen>
262         /// <param name="configuration">The configuration to be stored</param>
263         /// <feature>http://tizen.org/feature/network.wifi</feature>
264         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
265         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
266         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
267         /// <exception cref="ArgumentNullException">Thrown when WiFiConfiguration is passed as null.</exception>
268         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
269         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
270         static public void SaveWiFiConfiguration(WiFiConfiguration configuration)
271         {
272             WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration);
273         }
274
275         /// <summary>
276         /// Gets the object of the connected WiFiAP.
277         /// </summary>
278         /// <since_tizen> 3 </since_tizen>
279         /// <returns> The connected wifi access point(AP) information.</returns>
280         /// <feature>http://tizen.org/feature/network.wifi</feature>
281         /// <privilege>http://tizen.org/privilege/network.get</privilege>
282         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
283         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
284         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
285         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
286         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
287         static public WiFiAP GetConnectedAP()
288         {
289             return WiFiManagerImpl.Instance.GetConnectedAP();
290         }
291
292         /// <summary>
293         /// Activates Wi-Fi asynchronously.
294         /// </summary>
295         /// <since_tizen> 3 </since_tizen>
296         /// <returns> A task indicating whether the Activate method is done or not.</returns>
297         /// <feature>http://tizen.org/feature/network.wifi</feature>
298         /// <privilege>http://tizen.org/privilege/network.set</privilege>
299         /// <privilege>http://tizen.org/privilege/network.get</privilege>
300         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
301         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
302         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
303         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
304         static public Task ActivateAsync()
305         {
306             return WiFiManagerImpl.Instance.ActivateAsync();
307         }
308
309         /// <summary>
310         /// Activates Wi-Fi asynchronously and displays Wi-Fi picker (popup) when Wi-Fi is not automatically connected.
311         /// </summary>
312         /// <since_tizen> 3 </since_tizen>
313         /// <returns> A task indicating whether the ActivateWithPicker method is done or not.</returns>
314         /// <feature>http://tizen.org/feature/network.wifi</feature>
315         /// <privilege>http://tizen.org/privilege/network.set</privilege>
316         /// <privilege>http://tizen.org/privilege/network.get</privilege>
317         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
318         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
319         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
320         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
321         static public Task ActivateWithPickerAsync()
322         {
323             return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync();
324         }
325
326         /// <summary>
327         /// Deactivates Wi-Fi asynchronously.
328         /// </summary>
329         /// <since_tizen> 3 </since_tizen>
330         /// <returns> A task indicating whether the Deactivate method is done or not.</returns>
331         /// <feature>http://tizen.org/feature/network.wifi</feature>
332         /// <privilege>http://tizen.org/privilege/network.set</privilege>
333         /// <privilege>http://tizen.org/privilege/network.get</privilege>
334         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
335         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
336         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
337         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
338         static public Task DeactivateAsync()
339         {
340             return WiFiManagerImpl.Instance.DeactivateAsync();
341         }
342
343         /// <summary>
344         /// Starts scan asynchronously.
345         /// </summary>
346         /// <since_tizen> 3 </since_tizen>
347         /// <returns> A task indicating whether the Scan method is done or not.</returns>
348         /// <feature>http://tizen.org/feature/network.wifi</feature>
349         /// <privilege>http://tizen.org/privilege/network.set</privilege>
350         /// <privilege>http://tizen.org/privilege/network.get</privilege>
351         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
352         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
353         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
354         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
355         static public Task ScanAsync()
356         {
357             return WiFiManagerImpl.Instance.ScanAsync();
358         }
359
360         /// <summary>
361         /// Starts specific access point scan, asynchronously.
362         /// </summary>
363         /// <since_tizen> 3 </since_tizen>
364         /// <returns> A task indicating whether the ScanSpecificAP method is done or not.</returns>
365         /// <param name="essid">The essid of hidden ap</param>
366         /// <feature>http://tizen.org/feature/network.wifi</feature>
367         /// <privilege>http://tizen.org/privilege/network.set</privilege>
368         /// <privilege>http://tizen.org/privilege/network.get</privilege>
369         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
370         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
371         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
372         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
373         static public Task ScanSpecificAPAsync(string essid)
374         {
375             return WiFiManagerImpl.Instance.ScanSpecificAPAsync(essid);
376         }
377     }
378 }