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 Wi-Fi.</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);
57 Interop.Libc.Free(strPtr);
65 /// The raw Service Set Identifier (SSID).
67 /// <since_tizen> 5 </since_tizen>
68 /// <value>Raw SSID of the Wi-Fi.</value>
76 int ret = Interop.WiFi.AP.GetRawSsid(_apHandle, out ptr, out length);
77 if (ret != (int)WiFiError.None || length == 0)
79 Log.Error(Globals.LogTag, "Failed to get raw essid, Error - " + (WiFiError)ret);
84 rawSsid = new byte[length];
85 Marshal.Copy(ptr, rawSsid, 0, length);
86 Interop.Libc.Free(ptr);
93 /// The Basic Service Set Identifier (BSSID).
95 /// <since_tizen> 3 </since_tizen>
96 /// <value>BSSID of the Wi-Fi.</value>
103 int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
104 if (ret != (int)WiFiError.None)
106 Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
111 bssid = Marshal.PtrToStringAnsi(strPtr);
118 /// The address information for IPv4.
120 /// <since_tizen> 3 </since_tizen>
121 /// <value>IP address information for IPv4 type.</value>
122 public IAddressInformation IPv4Setting
131 /// The address information for IPv6.
133 /// <since_tizen> 3 </since_tizen>
134 /// <value>IP address information for IPv6 type.</value>
135 public IAddressInformation IPv6Setting
144 /// The proxy address.
146 /// <since_tizen> 3 </since_tizen>
147 /// <value>Represents the proxy address of the Wi-Fi.</value>
148 /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
149 /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
150 /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
151 public string ProxyAddress
157 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
158 if (ret != (int)WiFiError.None)
160 Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
165 proxy = Marshal.PtrToStringAnsi(strPtr);
171 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
172 if (ret != (int)WiFiError.None)
174 Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
175 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
181 /// The proxy type (IPv6).
183 /// <since_tizen> 3 </since_tizen>
184 /// <value>Represents the proxy type of the Wi-Fi.</value>
185 /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
186 /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
187 /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
188 public WiFiProxyType ProxyType
193 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
194 if (ret != (int)WiFiError.None)
196 Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
198 return (WiFiProxyType)type;
202 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
203 if (ret != (int)WiFiError.None)
205 Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
206 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
212 /// The frequency band (MHz).
214 /// <since_tizen> 3 </since_tizen>
215 /// <value>Represents the frequency band value.</value>
221 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
222 if (ret != (int)WiFiError.None)
224 Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
231 /// The received signal strength indicator (RSSI).
233 /// <since_tizen> 3 </since_tizen>
234 /// <value>Represents RSSI of Wi-Fi (dbm).</value>
240 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
241 if (ret != (int)WiFiError.None)
243 Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
250 /// The Received signal strength indication(RSSI).
252 /// <since_tizen> 4 </since_tizen>
253 /// <value>Represents Rssi level of WiFi.</value>
254 /// <feature>http://tizen.org/feature/network.wifi</feature>
255 /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
256 public WiFiRssiLevel RssiLevel
261 int ret = Interop.WiFi.AP.GetRssiLevel(_apHandle, out rssi);
262 if (ret != (int)WiFiError.None)
264 Log.Error(Globals.LogTag, "Failed to get rssi level, Error - " + (WiFiError)ret);
266 return (WiFiRssiLevel)rssi;
271 /// The max speed (Mbps).
273 /// <since_tizen> 3 </since_tizen>
274 /// <value>Represents the max speed value.</value>
280 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
281 if (ret != (int)WiFiError.None)
283 Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
290 /// A property to check whether the access point is a favorite or not.
292 /// <since_tizen> 3 </since_tizen>
293 /// <value>Boolean value to check if the access point is a favorite or not.</value>
294 public bool IsFavorite
299 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
300 if (ret != (int)WiFiError.None)
302 Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
309 /// A property to check whether the access point is a passpoint or not.
311 /// <since_tizen> 3 </since_tizen>
312 /// <value>Boolean value to check if the access point is a passpoint or not.</value>
313 public bool IsPasspoint
318 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
319 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
320 if (ret != (int)WiFiError.None)
322 Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
329 /// The connection state.
331 /// <since_tizen> 3 </since_tizen>
332 /// <value>Represents the connection state of the Wi-Fi.</value>
333 public WiFiConnectionState ConnectionState
338 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
339 if (ret != (int)WiFiError.None)
341 Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
343 return (WiFiConnectionState)state;
348 /// The raw country code
350 /// <since_tizen> 5 </since_tizen>
351 /// <value>Represents the raw country code of the Wi-Fi.</value>
352 public string CountryCode
358 int ret = Interop.WiFi.AP.GetCountryCode(_apHandle, out strPtr);
359 if (ret != (int)WiFiError.None)
361 Log.Error(Globals.LogTag, "Failed to get country code, Error - " + (WiFiError)ret);
366 code = Marshal.PtrToStringAnsi(strPtr);
367 Interop.Libc.Free(strPtr);
374 /// Gets all IPv6 addresses of the access point.
376 /// <since_tizen> 3 </since_tizen>
377 /// <returns>A list of IPv6 addresses of the access point.</returns>
378 /// <feature>http://tizen.org/feature/network.wifi</feature>
379 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
380 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
381 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
382 public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
384 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
385 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
386 Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
388 if (ipv6Address != IntPtr.Zero)
390 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
391 if (ipv6.Length == 0)
392 ipList.Add(System.Net.IPAddress.Parse("::"));
394 ipList.Add(System.Net.IPAddress.Parse(ipv6));
400 int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
401 if (ret != (int)WiFiError.None)
403 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
404 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
411 /// Gets the Bssid list
413 /// <since_tizen> 5 </since_tizen>
414 /// <returns>A list of BSSIDs of access points with the same SSID as that of this access point.</returns>
415 /// <feature>http://tizen.org/feature/network.wifi</feature>
416 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
417 /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
418 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
419 public IEnumerable<string> GetBssids()
421 Log.Debug(Globals.LogTag, "GetBssids");
422 List<string> bssidList = new List<string>();
423 Interop.WiFi.AP.FoundBssidCallback callback = (string bssid, int rssi, int freq, IntPtr userData) =>
425 if (string.IsNullOrEmpty(bssid))
427 bssidList.Add(bssid);
433 int ret = Interop.WiFi.AP.GetBssids(_apHandle, callback, IntPtr.Zero);
434 if (ret != (int)WiFiError.None)
436 Log.Error(Globals.LogTag, "Failed to get BSSIDs, Error - " + (WiFiError)ret);
437 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
443 internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
445 _apHandle = apHandle;
446 _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
447 _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
450 int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
451 if (ret != (int)WiFiError.None)
453 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
458 _essid = Marshal.PtrToStringAnsi(strPtr);
461 } //WiFiNetworkInformation