[Tizen.Network.WiFi] Add APIs for multi-scanning (#5870)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiConfiguration.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.Net;
19 using System.Threading.Tasks;
20 using System.Runtime.InteropServices;
21 using Tizen.Network.Connection;
22 using System.ComponentModel;
23
24 namespace Tizen.Network.WiFi
25 {
26     /// <summary>
27     /// A class for managing the configuration of Wi-Fi.
28     /// </summary>
29     /// <since_tizen> 3 </since_tizen>
30     public class WiFiConfiguration : IDisposable
31     {
32         private IntPtr _configHandle = IntPtr.Zero;
33         private bool _disposed = false;
34         private WiFiEapConfiguration _eapConfig;
35
36         /// <summary>
37         /// The name of the access point (AP).
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         /// <value>Name assigned to AP in the Wi-Fi configuration.</value>
41         public string Name
42         {
43             get
44             {
45                 IntPtr strPtr;
46                 int ret = Interop.WiFi.Config.GetName(_configHandle, out strPtr);
47                 if (ret != (int)WiFiError.None)
48                 {
49                     Log.Error(Globals.LogTag, "Failed to get name, Error - " + (WiFiError)ret);
50                     return "";
51                 }
52                 return Marshal.PtrToStringAnsi(strPtr);
53             }
54         }
55
56         /// <summary>
57         /// The security type of the access point (AP).
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         /// <value>Security type of AP in the Wi-Fi configuration.</value>
61         public WiFiSecurityType SecurityType
62         {
63             get
64             {
65                 int type;
66                 int ret = Interop.WiFi.Config.GetSecurityType(_configHandle, out type);
67                 if (ret != (int)WiFiError.None)
68                 {
69                     Log.Error(Globals.LogTag, "Failed to get security type, Error - " + (WiFiError)ret);
70                 }
71                 return (WiFiSecurityType)type;
72             }
73         }
74
75         /// <summary>
76         /// The proxy address.
77         /// </summary>
78         /// <since_tizen> 3 </since_tizen>
79         /// <value>Proxy address of the access point.</value>
80         /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
81         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
82         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
83         public string ProxyAddress
84         {
85             get
86             {
87                 IntPtr strPtr;
88                 int addressFamily;
89                 int ret = Interop.WiFi.Config.GetProxyAddress(_configHandle, out addressFamily, out strPtr);
90                 if (ret != (int)WiFiError.None)
91                 {
92                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
93                     return "";
94                 }
95                 return Marshal.PtrToStringAnsi(strPtr);
96             }
97             set
98             {
99                 if (_disposed)
100                 {
101                     throw new ObjectDisposedException("Invalid WiFiConfiguration instance (Object may have been disposed or released)");
102                 }
103                 int ret = Interop.WiFi.Config.SetProxyAddress(_configHandle, (int)AddressFamily.IPv4, value);
104                 if (ret != (int)WiFiError.None)
105                 {
106                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
107                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle);
108                 }
109             }
110         }
111
112         /// <summary>
113         /// A property check whether the access point (AP) is hidden.
114         /// </summary>
115         /// <since_tizen> 3 </since_tizen>
116         /// <value>Boolean value indicating whether the AP is hidden.</value>
117         /// <exception cref="NotSupportedException">Thrown while setting this property when the Wi-Fi is not supported.</exception>
118         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
119         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
120         public bool IsHidden
121         {
122             get
123             {
124                 bool hidden;
125                 int ret = Interop.WiFi.Config.GetHiddenAPProperty(_configHandle, out hidden);
126                 if (ret != (int)WiFiError.None)
127                 {
128                     Log.Error(Globals.LogTag, "Failed to get isHidden, Error - " + (WiFiError)ret);
129                 }
130                 return hidden;
131             }
132             set
133             {
134                 if (_disposed)
135                 {
136                     throw new ObjectDisposedException("Invalid WiFiConfiguration instance (Object may have been disposed or released)");
137                 }
138                 int ret = Interop.WiFi.Config.SetHiddenAPProperty(_configHandle, value);
139                 if (ret != (int)WiFiError.None)
140                 {
141                     Log.Error(Globals.LogTag, "Failed to set IsHidden, Error - " + (WiFiError)ret);
142                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle);
143                 }
144             }
145         }
146
147         /// <summary>
148         /// EAP configuration.
149         /// </summary>
150         /// <since_tizen> 3 </since_tizen>
151         /// <value>EAP configuration assigned to the Wi-Fi.</value>
152         public WiFiEapConfiguration EapConfiguration
153         {
154             get
155             {
156                 return _eapConfig;
157             }
158         }
159
160         /// <summary>
161         /// The Frequency of the access point (AP).
162         /// </summary>
163         /// <since_tizen> 9 </since_tizen>
164         /// <value>Frequency assigned to AP in the Wi-Fi configuration.</value>
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public int Frequency
167         {
168
169             get
170             {
171                 Log.Debug(Globals.LogTag, "Frequency");
172                 int freq;
173                 int ret = Interop.WiFi.Config.GetSavedConfigFrequency(_configHandle, out freq);
174                 if (ret != (int)WiFiError.None)
175                 {
176                     Log.Error(Globals.LogTag, "Failed to get Freq, Error - " + (WiFiError)ret);
177                     return 0;
178                 }
179                 Log.Debug(Globals.LogTag, "Frequency is " + freq);
180                 return freq;
181             }
182         }
183
184         internal WiFiConfiguration(IntPtr handle)
185         {
186             _configHandle = handle;
187             Interop.WiFi.SafeWiFiConfigHandle configHandle = new Interop.WiFi.SafeWiFiConfigHandle(handle);
188             _eapConfig = new WiFiEapConfiguration(configHandle);
189         }
190
191         /// <summary>
192         /// Creates a WiFiConfiguration object with the given name, passphrase, and securetype.
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         /// <param name="name">Name of the Wi-Fi.</param>
196         /// <param name="passPhrase">Password to access the Wi-Fi.</param>
197         /// <param name="type">Security type of the Wi-Fi.</param>
198         /// <feature>http://tizen.org/feature/network.wifi</feature>
199         /// <privilege>http://tizen.org/privilege/network.get</privilege>
200         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
201         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
202         /// <exception cref="ArgumentNullException">Thrown when the object is constructed with name as null.</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         public WiFiConfiguration(string name, string passPhrase, WiFiSecurityType type)
207         {
208             if (name == null)
209             {
210                 throw new ArgumentNullException("Name of the WiFi is null");
211             }
212
213             int ret = Interop.WiFi.Config.Create(WiFiManagerImpl.Instance.GetSafeHandle(), name, passPhrase, (int)type, out _configHandle);
214             if (ret != (int)WiFiError.None)
215             {
216                 Log.Error(Globals.LogTag, "Failed to create config handle, Error - " + (WiFiError)ret);
217                 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
218             }
219
220             Interop.WiFi.SafeWiFiConfigHandle configHandle = new Interop.WiFi.SafeWiFiConfigHandle(_configHandle);
221             _eapConfig = new WiFiEapConfiguration(configHandle);
222         }
223
224         /// <summary>
225         /// Destroy of the WiFiConfiguration object
226         /// </summary>
227         ~WiFiConfiguration()
228         {
229             Dispose(false);
230         }
231
232         /// <summary>
233         /// A method to destroy the managed objects in the WiFiConfiguration.
234         /// </summary>
235         /// <since_tizen> 3 </since_tizen>
236         public void Dispose()
237         {
238             Dispose(true);
239             GC.SuppressFinalize(this);
240         }
241
242         private void Dispose(bool disposing)
243         {
244             if (_disposed)
245                 return;
246
247             Interop.WiFi.Config.Destroy(_configHandle);
248             _configHandle = IntPtr.Zero;
249             _disposed = true;
250         }
251
252         internal IntPtr GetHandle()
253         {
254             return _configHandle;
255         }
256     } //WiFiNetworkConfiguratin
257 }