2 * Copyright (c) 2018 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>Represents the 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="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
381 public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
383 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
384 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
385 Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
387 if (ipv6Address != IntPtr.Zero)
389 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
390 if (ipv6.Length == 0)
391 ipList.Add(System.Net.IPAddress.Parse("::"));
393 ipList.Add(System.Net.IPAddress.Parse(ipv6));
399 int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
400 if (ret != (int)WiFiError.None)
402 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
403 if (ret == (int)WiFiError.InvalidParameterError)
405 throw new InvalidOperationException("Invalid handle");
407 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
414 /// Gets the Bssid list.
416 /// <since_tizen> 5 </since_tizen>
417 /// <returns>A list of BSSIDs of access points with the same SSID as that of this access point.</returns>
418 /// <feature>http://tizen.org/feature/network.wifi</feature>
419 /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
420 /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
421 public IEnumerable<string> GetBssids()
423 Log.Debug(Globals.LogTag, "GetBssids");
424 List<string> bssidList = new List<string>();
425 Interop.WiFi.AP.FoundBssidCallback callback = (string bssid, int rssi, int freq, IntPtr userData) =>
427 if (string.IsNullOrEmpty(bssid))
429 bssidList.Add(bssid);
435 int ret = Interop.WiFi.AP.GetBssids(_apHandle, callback, IntPtr.Zero);
436 if (ret != (int)WiFiError.None)
438 Log.Error(Globals.LogTag, "Failed to get BSSIDs, Error - " + (WiFiError)ret);
439 if (ret == (int)WiFiError.InvalidParameterError)
441 throw new InvalidOperationException("Invalid handle");
443 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
449 internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
451 _apHandle = apHandle;
452 _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
453 _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
456 int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
457 if (ret != (int)WiFiError.None)
459 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
464 _essid = Marshal.PtrToStringAnsi(strPtr);
467 } //WiFiNetworkInformation