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.
18 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
21 using Tizen.Network.Connection;
23 namespace Tizen.Network.WiFi
26 /// A class for managing the Wi-Fi network information.
28 /// <since_tizen> 3 </since_tizen>
29 public class WiFiNetwork
31 private Interop.WiFi.SafeWiFiAPHandle _apHandle;
32 private IAddressInformation _ipv4;
33 private IAddressInformation _ipv6;
34 private string _essid;
37 /// The Extended Service Set Identifier(ESSID).
39 /// <since_tizen> 3 </since_tizen>
40 /// <value>Essid of the WiFi.</value>
45 if (string.IsNullOrEmpty(_essid))
48 int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
49 if (ret != (int)WiFiError.None)
51 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
56 _essid = Marshal.PtrToStringAnsi(strPtr);
64 /// The Basic Service Set Identifier(BSSID).
66 /// <since_tizen> 3 </since_tizen>
67 /// <value>Bssid of the WiFi.</value>
73 int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
74 if (ret != (int)WiFiError.None)
76 Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
79 return Marshal.PtrToStringAnsi(strPtr);
84 /// The address informaiton for IPv4.
86 /// <since_tizen> 3 </since_tizen>
87 /// <value>IP address information for IPv4 type.</value>
88 public IAddressInformation IPv4Setting
97 /// The address information for IPv6.
99 /// <since_tizen> 3 </since_tizen>
100 /// <value>IP address information for IPv6 type.</value>
101 public IAddressInformation IPv6Setting
110 /// The proxy address.
112 /// <since_tizen> 3 </since_tizen>
113 /// <value>Represents proxy address of WiFi.</value>
114 /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
115 /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
116 /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
117 public string ProxyAddress
122 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
123 if (ret != (int)WiFiError.None)
125 Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
128 return Marshal.PtrToStringAnsi(strPtr);
132 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
133 if (ret != (int)WiFiError.None)
135 Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
136 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
142 /// The proxy type(IPv6).
144 /// <since_tizen> 3 </since_tizen>
145 /// <value>Represents proxy type of WiFi.</value>
146 /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
147 /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
148 /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
149 public WiFiProxyType ProxyType
154 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
155 if (ret != (int)WiFiError.None)
157 Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
159 return (WiFiProxyType)type;
163 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
164 if (ret != (int)WiFiError.None)
166 Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
167 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
173 /// The frequency band(MHz).
175 /// <since_tizen> 3 </since_tizen>
176 /// <value>Represents the frequency band value.</value>
182 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
183 if (ret != (int)WiFiError.None)
185 Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
192 /// The Received signal strength indication(RSSI).
194 /// <since_tizen> 3 </since_tizen>
195 /// <value>Represents Rssi of WiFi(dbm).</value>
201 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
202 if (ret != (int)WiFiError.None)
204 Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
211 /// The max speed (Mbps).
213 /// <since_tizen> 3 </since_tizen>
214 /// <value>Represents max speed value.</value>
220 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
221 if (ret != (int)WiFiError.None)
223 Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
230 /// A property to check whether the access point is favorite or not.
232 /// <since_tizen> 3 </since_tizen>
233 /// <value>Boolean value to check if the access point is favorite or not.</value>
234 public bool IsFavorite
239 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
240 if (ret != (int)WiFiError.None)
242 Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
249 /// A property to check whether the access point is passpoint or not.
251 /// <since_tizen> 3 </since_tizen>
252 /// <value>Boolean value to check if the access point is passpoint or not.</value>
253 public bool IsPasspoint
258 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
259 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
260 if (ret != (int)WiFiError.None)
262 Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
269 /// The connection state.
271 /// <since_tizen> 3 </since_tizen>
272 /// <value>Represents the connection state of WiFi.</value>
273 public WiFiConnectionState ConnectionState
278 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
279 if (ret != (int)WiFiError.None)
281 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
283 return (WiFiConnectionState)state;
288 /// Gets the all IPv6 addresses of the access point
290 /// <since_tizen> 3 </since_tizen>
291 /// <returns>A list of IPv6 addresses of the access point.</returns>
292 /// <feature>http://tizen.org/feature/network.wifi</feature>
293 /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
294 /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
295 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
296 public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
298 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
299 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
300 Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
302 if (ipv6Address != IntPtr.Zero)
304 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
305 ipList.Add(System.Net.IPAddress.Parse(ipv6));
311 int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
312 if (ret != (int)WiFiError.None)
314 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
315 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
321 internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
323 _apHandle = apHandle;
324 _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
325 _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
328 int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
329 if (ret != (int)WiFiError.None)
331 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
333 _essid = Marshal.PtrToStringAnsi(strPtr);
335 } //WiFiNetworkInformation