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 public class WiFiNetwork
30 private Interop.WiFi.SafeWiFiAPHandle _apHandle;
31 private IAddressInformation _ipv4;
32 private IAddressInformation _ipv6;
33 private string _essid;
36 /// The Extended Service Set Identifier(ESSID).
38 /// <value>Essid of the WiFi.</value>
43 if (string.IsNullOrEmpty(_essid))
46 int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
47 if (ret != (int)WiFiError.None)
49 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
54 _essid = Marshal.PtrToStringAnsi(strPtr);
62 /// The Basic Service Set Identifier(BSSID).
64 /// <value>Bssid of the WiFi.</value>
70 int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
71 if (ret != (int)WiFiError.None)
73 Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
76 return Marshal.PtrToStringAnsi(strPtr);
81 /// The address informaiton for IPv4.
83 /// <value>IP address information for IPv4 type.</value>
84 public IAddressInformation IPv4Setting
93 /// The address ainformation for IPv6.
95 /// <value>IP address information for IPv6 type.</value>
96 public IAddressInformation IPv6Setting
105 /// The proxy address.
107 /// <value>Represents proxy address of WiFi.</value>
108 /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
109 /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
110 /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
111 public string ProxyAddress
116 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
117 if (ret != (int)WiFiError.None)
119 Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
122 return Marshal.PtrToStringAnsi(strPtr);
126 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
127 if (ret != (int)WiFiError.None)
129 Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
130 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
136 /// The proxy type(IPv6).
138 /// <value>Represents proxy type of WiFi.</value>
139 /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
140 /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
141 /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
142 public WiFiProxyType ProxyType
147 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
148 if (ret != (int)WiFiError.None)
150 Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
152 return (WiFiProxyType)type;
156 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
157 if (ret != (int)WiFiError.None)
159 Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
160 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
166 /// The frequency band(MHz).
168 /// <value>Represents the frequency band value.</value>
174 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
175 if (ret != (int)WiFiError.None)
177 Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
184 /// The Received signal strength indication(RSSI).
186 /// <value>Represents Rssi level of WiFi.</value>
187 public WiFiRssiLevel Rssi
192 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
193 if (ret != (int)WiFiError.None)
195 Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
197 return (WiFiRssiLevel)rssi;
202 /// The max speed (Mbps).
204 /// <value>Represents max speed value.</value>
210 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
211 if (ret != (int)WiFiError.None)
213 Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
220 /// A property to check whether the access point is favorite or not.
222 /// <value>Boolean value to check if the access point is favorite or not.</value>
223 public bool IsFavorite
228 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
229 if (ret != (int)WiFiError.None)
231 Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
238 /// A property to check whether the access point is passpoint or not.
240 /// <value>Boolean value to check if the access point is passpoint or not.</value>
241 public bool IsPasspoint
246 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
247 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
248 if (ret != (int)WiFiError.None)
250 Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
257 /// The connection state.
259 /// <value>Represents the connection state of WiFi.</value>
260 public WiFiConnectionState ConnectionState
265 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
266 if (ret != (int)WiFiError.None)
268 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
270 return (WiFiConnectionState)state;
275 /// Gets the all IPv6 addresses of the access point
277 /// <returns>A list of IPv6 addresses of the access point.</returns>
278 /// <feature>http://tizen.org/feature/network.wifi</feature>
279 /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
280 /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
281 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
282 public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
284 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
285 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
286 Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
288 if (ipv6Address != IntPtr.Zero)
290 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
291 ipList.Add(System.Net.IPAddress.Parse(ipv6));
297 int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
298 if (ret != (int)WiFiError.None)
300 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
301 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
307 internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
309 _apHandle = apHandle;
310 _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
311 _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
314 int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
315 if (ret != (int)WiFiError.None)
317 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
319 _essid = Marshal.PtrToStringAnsi(strPtr);
321 } //WiFiNetworkInformation