/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Runtime.InteropServices;
using Tizen.Network.Connection;
namespace Tizen.Network.WiFi
{
///
/// A class for managing the Wi-Fi network information.
///
/// 3
public class WiFiNetwork
{
private Interop.WiFi.SafeWiFiAPHandle _apHandle;
private IAddressInformation _ipv4;
private IAddressInformation _ipv6;
private string _essid;
///
/// The Extended Service Set Identifier (ESSID).
///
/// 3
/// ESSID of the Wi-Fi.
public string Essid
{
get
{
if (string.IsNullOrEmpty(_essid))
{
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
_essid = "";
}
else
{
_essid = Marshal.PtrToStringAnsi(strPtr);
}
}
return _essid;
}
}
///
/// The Basic Service Set Identifier (BSSID).
///
/// 3
/// BSSID of the Wi-Fi.
public string Bssid
{
get
{
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
return "";
}
return Marshal.PtrToStringAnsi(strPtr);
}
}
///
/// The address information for IPv4.
///
/// 3
/// IP address information for IPv4 type.
public IAddressInformation IPv4Setting
{
get
{
return _ipv4;
}
}
///
/// The address information for IPv6.
///
/// 3
/// IP address information for IPv6 type.
public IAddressInformation IPv6Setting
{
get
{
return _ipv6;
}
}
///
/// The proxy address.
///
/// 3
/// Represents the proxy address of the Wi-Fi.
/// Thrown while setting this property when Wi-Fi is not supported.
/// Thrown while setting this property due to an invalid parameter.
/// Thrown while setting this value due to an invalid operation.
public string ProxyAddress
{
get
{
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
return "";
}
return Marshal.PtrToStringAnsi(strPtr);
}
set
{
int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
}
///
/// The proxy type (IPv6).
///
/// 3
/// Represents the proxy type of the Wi-Fi.
/// Thrown while setting this property when Wi-Fi is not supported.
/// Thrown while setting this property due to an invalid parameter.
/// Thrown while setting this value due to an invalid operation.
public WiFiProxyType ProxyType
{
get
{
int type;
int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
}
return (WiFiProxyType)type;
}
set
{
int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
}
///
/// The frequency band (MHz).
///
/// 3
/// Represents the frequency band value.
public int Frequency
{
get
{
int freq;
int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
}
return freq;
}
}
///
/// The received signal strength indicator (RSSI).
///
/// 3
/// Represents RSSI of Wi-Fi (dbm).
public int Rssi
{
get
{
int rssi;
int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
}
return rssi;
}
}
///
/// The Received signal strength indication(RSSI).
///
/// 4
/// Represents Rssi level of WiFi.
/// http://tizen.org/feature/network.wifi
/// Thrown while setting this property when WiFi is not supported.
public WiFiRssiLevel RssiLevel
{
get
{
int rssi;
int ret = Interop.WiFi.AP.GetRssiLevel(_apHandle, out rssi);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get rssi level, Error - " + (WiFiError)ret);
}
return (WiFiRssiLevel)rssi;
}
}
///
/// The max speed (Mbps).
///
/// 3
/// Represents the max speed value.
public int MaxSpeed
{
get
{
int maxSpeed;
int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
}
return maxSpeed;
}
}
///
/// A property to check whether the access point is a favorite or not.
///
/// 3
/// Boolean value to check if the access point is a favorite or not.
public bool IsFavorite
{
get
{
bool isFavorite;
int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
}
return isFavorite;
}
}
///
/// A property to check whether the access point is a passpoint or not.
///
/// 3
/// Boolean value to check if the access point is a passpoint or not.
public bool IsPasspoint
{
get
{
bool isPasspoint;
Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
}
return isPasspoint;
}
}
///
/// The connection state.
///
/// 3
/// Represents the connection state of the Wi-Fi.
public WiFiConnectionState ConnectionState
{
get
{
int state;
int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
}
return (WiFiConnectionState)state;
}
}
///
/// Gets all IPv6 addresses of the access point.
///
/// 3
/// A list of IPv6 addresses of the access point.
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the method failed due to an invalid parameter.
/// Thrown when the method failed due to an invalid operation.
public IEnumerable GetAllIPv6Addresses()
{
Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
List ipList = new List();
Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
{
if (ipv6Address != IntPtr.Zero)
{
string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
if (ipv6.Length == 0)
ipList.Add(System.Net.IPAddress.Parse("::"));
else
ipList.Add(System.Net.IPAddress.Parse(ipv6));
return true;
}
return false;
};
int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
return ipList;
}
internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
{
_apHandle = apHandle;
_ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
_ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
}
_essid = Marshal.PtrToStringAnsi(strPtr);
}
} //WiFiNetworkInformation
}