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