/*
* 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.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Tizen.Network.Connection
{
///
/// This class manages the connection handle resources.
///
/// 3
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class SafeConnectionHandle : SafeHandle
{
internal SafeConnectionHandle(IntPtr handle) : base(handle, true)
{
}
///
/// Checks whether the handle value is valid or not.
///
/// True if the handle is invalid, otherwise false.
/// 3
public override bool IsInvalid
{
get
{
return this.handle == IntPtr.Zero;
}
}
///
/// Frees the handle.
///
/// True if the handle is released successfully, otherwise false.
protected override bool ReleaseHandle()
{
this.SetHandle(IntPtr.Zero);
return true;
}
}
///
/// This is the ConnectionManager class. It provides functions to manage data connections.
///
/// 3
public static class ConnectionManager
{
private static ConnectionItem _currentConnection = null;
///
/// An event is called when the type of the current connection is changed.
///
/// 3
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static event EventHandler ConnectionTypeChanged
{
add
{
ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
}
remove
{
ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
}
}
///
/// An event for the ethernet cable is plugged [in/out] event.
///
/// 3
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static event EventHandler EthernetCableStateChanged
{
add
{
ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
}
remove
{
ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
}
}
///
/// An event is called when the IP address is changed.
///
/// 3
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static event EventHandler IPAddressChanged
{
add
{
ConnectionInternalManager.Instance.IPAddressChanged += value;
}
remove
{
ConnectionInternalManager.Instance.IPAddressChanged -= value;
}
}
///
/// An event is called when the proxy address is changed.
///
/// 3
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static event EventHandler ProxyAddressChanged
{
add
{
ConnectionInternalManager.Instance.ProxyAddressChanged += value;
}
remove
{
ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
}
}
///
/// Gets the connection handle.
///
/// 3
/// Instance of SafeConnectionHandle.
[EditorBrowsable(EditorBrowsableState.Never)]
public static SafeConnectionHandle GetConnectionHandle()
{
IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
return new SafeConnectionHandle(handle);
}
///
/// Gets the IP address of the current connection.
///
/// 3
/// The address family.
/// IP address of the connection (global address in case of IPv6).
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static System.Net.IPAddress GetIPAddress(AddressFamily family)
{
return ConnectionInternalManager.Instance.GetIPAddress(family);
}
///
/// Gets all the IPv6 addresses of the current connection.
///
/// 3
/// The type of current network connection.
/// A list of IPv6 addresses of the connection.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static IEnumerable GetAllIPv6Addresses(ConnectionType type)
{
return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
}
///
/// Gets the proxy address of the current connection.
///
/// 3
/// The address family.
/// Proxy address of the connection.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static string GetProxy(AddressFamily family)
{
return ConnectionInternalManager.Instance.GetProxy(family);
}
///
/// Gets the MAC address of the Wi-Fi or ethernet.
///
/// 3
/// The type of current network connection.
/// MAC address of the Wi-Fi or ethernet.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when value is an invalid parameter.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static string GetMacAddress(ConnectionType type)
{
return ConnectionInternalManager.Instance.GetMacAddress(type);
}
///
/// Gets the statistics information.
///
/// 3
/// The type of connection (only WiFi and Cellular are supported).
/// The type of statistics.
/// The statistics information associated with statisticsType.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when value is an invalid parameter.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to invalid operation.
public static long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
{
return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
}
///
/// Resets the statistics information.
///
/// 3
/// The type of connection (only WiFi and Cellular are supported).
/// The type of statistics.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/privilege/network.set
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
{
ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
}
///
/// Adds a route to the routing table.
///
/// 4
/// The address family.
/// The name of the network interface.
/// The IP address of the host.
/// The gateway address.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/privilege/network.route
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.wifi
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.route
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when an interfaceName or a hostAddress or a gateway is null.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
{
ConnectionInternalManager.Instance.AddRoute(family, interfaceName, hostAddress, gateway);
}
///
/// Removes a route from the routing table.
///
/// 4
/// The address family.
/// The name of network interface.
/// The IP address of the host.
/// The gateway address.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/privilege/network.route
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.wifi
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.route
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when an interfaceName or a hostAddress or a gateway is null.
/// Thrown when memory is not enough to continue execution.
/// Thrown when a connection instance is invalid or when a method fails due to an invalid operation.
public static void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
{
ConnectionInternalManager.Instance.RemoveRoute(family, interfaceName, hostAddress, gateway);
}
///
/// The type and state of the current profile for data connection.
///
/// 3
/// Instance of ConnectionItem.
public static ConnectionItem CurrentConnection
{
get
{
if (_currentConnection == null)
{
_currentConnection = new ConnectionItem();
}
return _currentConnection;
}
}
///
/// Creates a cellular profile handle.
///
/// 3
/// The type of profile. Cellular profile type is supported.
/// The keyword included in profile name.
/// CellularProfile object.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
/// Thrown when a value is an invalid parameter.
/// Thrown when a keyword value is null.
/// Thrown when a method fails due to invalid operation.
public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
{
IntPtr profileHandle = IntPtr.Zero;
if (type == ConnectionProfileType.Cellular)
{
profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
}
else
{
Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
}
return new CellularProfile(profileHandle);
}
///
/// The state of the cellular connection.
///
/// 3
/// Cellular network state.
/// http://tizen.org/feature/network.telephony
/// Thrown when a feature is not supported.
public static CellularState CellularState
{
get
{
return ConnectionInternalManager.Instance.CellularState;
}
}
///
/// The state of the Wi-Fi connection.
///
/// 3
/// WiFi connection state.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static ConnectionState WiFiState
{
get
{
return ConnectionInternalManager.Instance.WiFiState;
}
}
///
/// The state of the Bluetooth connection.
///
/// 3
/// Bluetooth connection state.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.tethering.bluetooth
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static ConnectionState BluetoothState
{
get
{
return ConnectionInternalManager.Instance.BluetoothState;
}
}
///
/// The Ethernet connection state.
///
/// 3
/// Ethernet connection state.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static ConnectionState EthernetState
{
get
{
return ConnectionInternalManager.Instance.EthernetState;
}
}
///
/// Checks if the ethernet cable is attached or not.
///
/// 3
/// Ethernet cable state.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public static EthernetCableState EthernetCableState
{
get
{
return ConnectionInternalManager.Instance.EthernetCableState;
}
}
} // class ConnectionManager
///
/// This class contains connection information, such as connection type and state.
///
/// 3
public class ConnectionItem
{
internal ConnectionItem()
{
}
///
/// The type of the current profile for data connection.
///
/// 3
/// Data connection current profile.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public ConnectionType Type
{
get
{
return ConnectionInternalManager.Instance.ConnectionType;
}
}
///
/// The state of the current profile for data connection.
///
/// 3
/// Connection state of the current connection type.
/// http://tizen.org/privilege/network.get
/// http://tizen.org/feature/network.ethernet
/// http://tizen.org/feature/network.telephony
/// http://tizen.org/feature/network.tethering.bluetooth
/// http://tizen.org/feature/network.wifi
/// Thrown when a feature is not supported.
/// Thrown when a permission is denied.
public ConnectionState State
{
get
{
if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
{
if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
{
return ConnectionState.Connected;
}
else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
{
return ConnectionState.Disconnected;
}
else {
return ConnectionState.Deactivated;
}
}
else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
{
return ConnectionInternalManager.Instance.BluetoothState;
}
else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
{
return ConnectionInternalManager.Instance.WiFiState;
}
else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
{
return ConnectionInternalManager.Instance.EthernetState;
}
else { // TO DO : Add Net Proxy
return ConnectionState.Disconnected;
}
}
}
} // class ConnectionItem
///
/// An extended EventArgs class, which contains changed connection type.
///
/// 3
public class ConnectionTypeEventArgs : EventArgs
{
private ConnectionType Type = ConnectionType.Disconnected;
internal ConnectionTypeEventArgs(ConnectionType type)
{
Type = type;
}
///
/// The connection type.
///
/// 3
/// Type of the connection.
public ConnectionType ConnectionType
{
get
{
return Type;
}
}
}
///
/// An extended EventArgs class, which contains changed ethernet cable state.
///
/// 3
public class EthernetCableStateEventArgs : EventArgs
{
private EthernetCableState State;
internal EthernetCableStateEventArgs(EthernetCableState state)
{
State = state;
}
///
/// The ethernet cable state.
///
/// 3
/// Attached or detached state of the ethernet cable.
public EthernetCableState EthernetCableState
{
get
{
return State;
}
}
}
///
/// An extended EventArgs class, which contains changed address.
///
/// 3
public class AddressEventArgs : EventArgs
{
private string IPv4 = "";
private string IPv6 = "";
internal AddressEventArgs(string ipv4, string ipv6)
{
IPv4 = ipv4;
IPv6 = ipv6;
}
///
/// The IPV4 address.
///
/// 3
/// IP address in the format of the IPV4 syntax.
public string IPv4Address
{
get
{
return IPv4;
}
}
///
/// The IPV6 address.
///
/// 3
/// IP address in the format of the IPV6 syntax.
public string IPv6Address
{
get
{
return IPv6;
}
}
}
}