[Connection][WiFi]Apply TizenSynchronizationContext for event handling (#136)
[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 list of Wi-Fi configurations.
256         /// </summary>
257         /// <since_tizen> 3 </since_tizen>
258         /// <returns>A list containing the WiFiConfiguration objects.</returns>
259         /// <feature>http://tizen.org/feature/network.wifi</feature>
260         /// <privilege>http://tizen.org/privilege/network.profile</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="OutOfMemoryException">Thrown when system is out of memory.</exception>
264         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
265         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
266         static public IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
267         {
268             return WiFiManagerImpl.Instance.GetWiFiConfigurations();
269         }
270
271         /// <summary>
272         /// Saves the Wi-Fi configuration of the access point.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         /// <param name="configuration">The configuration to be stored.</param>
276         /// <feature>http://tizen.org/feature/network.wifi</feature>
277         /// <privilege>http://tizen.org/privilege/network.profile</privilege>
278         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
279         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
280         /// <exception cref="ArgumentNullException">Thrown when WiFiConfiguration is passed as null.</exception>
281         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
282         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
283         static public void SaveWiFiConfiguration(WiFiConfiguration configuration)
284         {
285             WiFiManagerImpl.Instance.SaveWiFiNetworkConfiguration(configuration);
286         }
287
288         /// <summary>
289         /// Gets the object of the connected WiFiAP.
290         /// </summary>
291         /// <since_tizen> 3 </since_tizen>
292         /// <returns>The connected Wi-Fi access point (AP) information.</returns>
293         /// <feature>http://tizen.org/feature/network.wifi</feature>
294         /// <privilege>http://tizen.org/privilege/network.get</privilege>
295         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
296         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
297         /// <exception cref="OutOfMemoryException">Thrown when system is out of memory.</exception>
298         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
299         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
300         static public WiFiAP GetConnectedAP()
301         {
302             return WiFiManagerImpl.Instance.GetConnectedAP();
303         }
304
305         /// <summary>
306         /// Activates the Wi-Fi asynchronously.
307         /// </summary>
308         /// <since_tizen> 3 </since_tizen>
309         /// <returns> A task indicating whether the activate method is done or not.</returns>
310         /// <feature>http://tizen.org/feature/network.wifi</feature>
311         /// <privilege>http://tizen.org/privilege/network.set</privilege>
312         /// <privilege>http://tizen.org/privilege/network.get</privilege>
313         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
314         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
315         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
316         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
317         static public Task ActivateAsync()
318         {
319             return WiFiManagerImpl.Instance.ActivateAsync();
320         }
321
322         /// <summary>
323         /// Activates the Wi-Fi asynchronously and displays the Wi-Fi picker (popup) when the Wi-Fi is not automatically connected.
324         /// </summary>
325         /// <since_tizen> 3 </since_tizen>
326         /// <returns>A task indicating whether the ActivateWithPicker method is done or not.</returns>
327         /// <feature>http://tizen.org/feature/network.wifi</feature>
328         /// <privilege>http://tizen.org/privilege/network.set</privilege>
329         /// <privilege>http://tizen.org/privilege/network.get</privilege>
330         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
331         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
332         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
333         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
334         static public Task ActivateWithPickerAsync()
335         {
336             return WiFiManagerImpl.Instance.ActivateWithWiFiPickerTestedAsync();
337         }
338
339         /// <summary>
340         /// Deactivates the Wi-Fi asynchronously.
341         /// </summary>
342         /// <since_tizen> 3 </since_tizen>
343         /// <returns>A task indicating whether the deactivate method is done or not.</returns>
344         /// <feature>http://tizen.org/feature/network.wifi</feature>
345         /// <privilege>http://tizen.org/privilege/network.set</privilege>
346         /// <privilege>http://tizen.org/privilege/network.get</privilege>
347         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
348         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
349         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
350         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
351         static public Task DeactivateAsync()
352         {
353             return WiFiManagerImpl.Instance.DeactivateAsync();
354         }
355
356         /// <summary>
357         /// Starts the scan asynchronously.
358         /// </summary>
359         /// <since_tizen> 3 </since_tizen>
360         /// <returns>A task indicating whether the scan method is done or not.</returns>
361         /// <feature>http://tizen.org/feature/network.wifi</feature>
362         /// <privilege>http://tizen.org/privilege/network.set</privilege>
363         /// <privilege>http://tizen.org/privilege/network.get</privilege>
364         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
365         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
366         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
367         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
368         static public Task ScanAsync()
369         {
370             return WiFiManagerImpl.Instance.ScanAsync();
371         }
372
373         /// <summary>
374         /// Starts a specific access point scan asynchronously.
375         /// </summary>
376         /// <since_tizen> 3 </since_tizen>
377         /// <returns>A task indicating whether the ScanSpecificAP method is done or not.</returns>
378         /// <param name="essid">The ESSID of the hidden AP.</param>
379         /// <feature>http://tizen.org/feature/network.wifi</feature>
380         /// <privilege>http://tizen.org/privilege/network.set</privilege>
381         /// <privilege>http://tizen.org/privilege/network.get</privilege>
382         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
383         /// <exception cref="UnauthorizedAccessException">Thrown when the permission is denied.</exception>
384         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
385         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
386         static public Task ScanSpecificAPAsync(string essid)
387         {
388             return WiFiManagerImpl.Instance.ScanSpecificAPAsync(essid);
389         }
390     }
391 }