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