2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 using System.Threading.Tasks;
20 using System.Runtime.InteropServices;
21 using Tizen.Network.Connection;
22 using System.ComponentModel;
24 namespace Tizen.Network.WiFi
27 /// A class for managing the configuration of Wi-Fi.
29 /// <since_tizen> 3 </since_tizen>
30 public class WiFiConfiguration : IDisposable
32 private IntPtr _configHandle = IntPtr.Zero;
33 private bool _disposed = false;
34 private WiFiEapConfiguration _eapConfig;
37 /// The name of the access point (AP).
39 /// <since_tizen> 3 </since_tizen>
40 /// <value>Name assigned to AP in the Wi-Fi configuration.</value>
46 int ret = Interop.WiFi.Config.GetName(_configHandle, out strPtr);
47 if (ret != (int)WiFiError.None)
49 Log.Error(Globals.LogTag, "Failed to get name, Error - " + (WiFiError)ret);
52 return Marshal.PtrToStringAnsi(strPtr);
57 /// The security type of the access point (AP).
59 /// <since_tizen> 3 </since_tizen>
60 /// <value>Security type of AP in the Wi-Fi configuration.</value>
61 public WiFiSecurityType SecurityType
66 int ret = Interop.WiFi.Config.GetSecurityType(_configHandle, out type);
67 if (ret != (int)WiFiError.None)
69 Log.Error(Globals.LogTag, "Failed to get security type, Error - " + (WiFiError)ret);
71 return (WiFiSecurityType)type;
76 /// The proxy address.
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
89 int ret = Interop.WiFi.Config.GetProxyAddress(_configHandle, out addressFamily, out strPtr);
90 if (ret != (int)WiFiError.None)
92 Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
95 return Marshal.PtrToStringAnsi(strPtr);
101 throw new ObjectDisposedException("Invalid WiFiConfiguration instance (Object may have been disposed or released)");
103 int ret = Interop.WiFi.Config.SetProxyAddress(_configHandle, (int)AddressFamily.IPv4, value);
104 if (ret != (int)WiFiError.None)
106 Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
107 WiFiErrorFactory.ThrowWiFiException(ret, _configHandle);
113 /// A property check whether the access point (AP) is hidden.
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>
125 int ret = Interop.WiFi.Config.GetHiddenAPProperty(_configHandle, out hidden);
126 if (ret != (int)WiFiError.None)
128 Log.Error(Globals.LogTag, "Failed to get isHidden, Error - " + (WiFiError)ret);
136 throw new ObjectDisposedException("Invalid WiFiConfiguration instance (Object may have been disposed or released)");
138 int ret = Interop.WiFi.Config.SetHiddenAPProperty(_configHandle, value);
139 if (ret != (int)WiFiError.None)
141 Log.Error(Globals.LogTag, "Failed to set IsHidden, Error - " + (WiFiError)ret);
142 WiFiErrorFactory.ThrowWiFiException(ret, _configHandle);
148 /// EAP configuration.
150 /// <since_tizen> 3 </since_tizen>
151 /// <value>EAP configuration assigned to the Wi-Fi.</value>
152 public WiFiEapConfiguration EapConfiguration
161 /// The Frequency of the access point (AP).
163 /// <since_tizen> 9 </since_tizen>
164 /// <value>Frequency assigned to AP in the Wi-Fi configuration.</value>
165 [EditorBrowsable(EditorBrowsableState.Never)]
171 Log.Debug(Globals.LogTag, "Frequency");
173 int ret = Interop.WiFi.Config.GetSavedConfigFrequency(_configHandle, out freq);
174 if (ret != (int)WiFiError.None)
176 Log.Error(Globals.LogTag, "Failed to get Freq, Error - " + (WiFiError)ret);
179 Log.Debug(Globals.LogTag, "Frequency is " + freq);
184 internal WiFiConfiguration(IntPtr handle)
186 _configHandle = handle;
187 Interop.WiFi.SafeWiFiConfigHandle configHandle = new Interop.WiFi.SafeWiFiConfigHandle(handle);
188 _eapConfig = new WiFiEapConfiguration(configHandle);
192 /// Creates a WiFiConfiguration object with the given name, passphrase, and securetype.
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)
210 throw new ArgumentNullException("Name of the WiFi is null");
213 int ret = Interop.WiFi.Config.Create(WiFiManagerImpl.Instance.GetSafeHandle(), name, passPhrase, (int)type, out _configHandle);
214 if (ret != (int)WiFiError.None)
216 Log.Error(Globals.LogTag, "Failed to create config handle, Error - " + (WiFiError)ret);
217 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
220 Interop.WiFi.SafeWiFiConfigHandle configHandle = new Interop.WiFi.SafeWiFiConfigHandle(_configHandle);
221 _eapConfig = new WiFiEapConfiguration(configHandle);
225 /// Destroy of the WiFiConfiguration object
233 /// A method to destroy the managed objects in the WiFiConfiguration.
235 /// <since_tizen> 3 </since_tizen>
236 public void Dispose()
239 GC.SuppressFinalize(this);
242 private void Dispose(bool disposing)
247 Interop.WiFi.Config.Destroy(_configHandle);
248 _configHandle = IntPtr.Zero;
252 internal IntPtr GetHandle()
254 return _configHandle;
256 } //WiFiNetworkConfiguratin