/*
* Copyright (c) 2018 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.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace Tizen.Network.WiFi
{
///
/// A class for managing the EAP information of the access point (AP).
///
/// 3
public class WiFiEap : IWiFiEap
{
private Interop.WiFi.SafeWiFiAPHandle _apHandle;
///
/// The file path of CA Certificate of EAP.
///
/// 3
/// CA certification file of EAP.
/// Thrown while setting this value when Wi-Fi is not supported.
/// Thrown while setting this value when the file value is null.
/// Thrown while setting this property due to an invalid parameter.
/// Thrown while setting this value due to an invalid operation.
public string CaCertificationFile
{
get
{
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetEapCaCertFile(_apHandle, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get caCertFile, Error - " + (WiFiError)ret);
return "";
}
return Marshal.PtrToStringAnsi(strPtr);
}
set
{
if (value == null)
{
throw new ArgumentNullException("File value is null");
}
int ret = Interop.WiFi.AP.SetEapCaCertFile(_apHandle, value);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set caCertFile, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
}
///
/// The EAP type of Wi-Fi.
///
/// 3
/// Type of EAP.
/// Thrown while setting this value 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 WiFiEapType EapType
{
get
{
int type;
int ret = Interop.WiFi.AP.GetEapType(_apHandle, out type);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get eap type, Error - " + (WiFiError)ret);
}
return (WiFiEapType)type;
}
set
{
int ret = Interop.WiFi.AP.SetEapType(_apHandle, (int)value);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set eap type, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
}
///
/// The type of EAP phase 2 authentication of the Wi-Fi.
///
/// 3
/// Authentication type of the Wi-Fi.
/// Thrown while setting this value 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 WiFiAuthenticationType AuthenticationType
{
get
{
int type;
int ret = Interop.WiFi.AP.GetEapAuthType(_apHandle, out type);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get auth type, Error - " + (WiFiError)ret);
}
return (WiFiAuthenticationType)type;
}
set
{
int ret = Interop.WiFi.AP.SetEapAuthType(_apHandle, (int)value);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set eap auth type, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
}
internal WiFiEap(Interop.WiFi.SafeWiFiAPHandle apHandle)
{
_apHandle = apHandle;
}
///
/// Gets the private key file of EAP.
///
/// 3
/// The file path of private key.
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the system is out of memory.
/// Thrown when the method fails due to an invalid parameter.
/// Thrown when the method fails due to an invalid operation.
public string GetPrivateKeyFile()
{
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetEapPrivateKeyFile(_apHandle, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get private key file, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
return Marshal.PtrToStringAnsi(strPtr);
}
///
/// Sets the private key information of EAP.
///
/// 3
/// The file path of private key.
/// The password.
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the file path of private key is null.
/// Thrown when the method failed due to an invalid parameter.
/// Thrown when the method failed due an to invalid operation.
public void SetPrivateKeyFile(string privateKeyFile, string password)
{
if (privateKeyFile == null)
{
throw new ArgumentNullException("File path of private key is null");
}
int ret = Interop.WiFi.AP.SetEapPrivateKeyFile(_apHandle, privateKeyFile, password);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set private key file, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
///
/// Gets the client certificate of EAP.
///
/// 3
/// The file path of client certificate.
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the system is out of memory.
/// Thrown when the method failed due an to invalid operation.
public string GetClientCertFile()
{
IntPtr strPtr;
int ret = Interop.WiFi.AP.GetEapClientCertFile(_apHandle, out strPtr);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get client cert file, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
return Marshal.PtrToStringAnsi(strPtr);
}
///
/// Sets the CA certificate of EAP.
///
/// 3
/// The file path of client certificate.
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the file path of client certificate is null.
/// Thrown when the method failed due to an invalid parameter.
/// Thrown when the method failed due to an invalid operation.
public void SetClientCertFile(string clientCertFile)
{
if (clientCertFile == null)
{
throw new ArgumentNullException("File path of Client certificate is null");
}
int ret = Interop.WiFi.AP.SetEapClientCertFile(_apHandle, clientCertFile);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set client cert file, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
///
/// Gets the username of EAP passphrase.
///
/// 3
/// The user name
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the system is out of memory.
/// Thrown when the method failed due to an invalid operation.
public string GetUserName()
{
IntPtr strptr;
bool passwordSet;
int ret = Interop.WiFi.AP.GetEapPassphrase(_apHandle, out strptr, out passwordSet);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get user name in eap passphrase, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
return Marshal.PtrToStringAnsi(strptr);
}
///
/// Returns whether the password is set or not.
///
/// 3
/// True if password is set, false if password is not set.
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the system is out of memory.
/// Thrown when the method failed due to an invalid operation.
public bool IsPasswordSet()
{
IntPtr strptr;
bool passwordSet;
int ret = Interop.WiFi.AP.GetEapPassphrase(_apHandle, out strptr, out passwordSet);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to get IsPasswordSet in passphrase, Error - " + (WiFiError)ret);
if (ret == (int)WiFiError.InvalidParameterError)
{
throw new InvalidOperationException("Invalid handle");
}
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
return passwordSet;
}
///
/// Sets the user name of EAP.
///
/// 3
/// The user name
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the user name is passed as null.
/// Thrown when the method failed due to an invalid parameter.
/// Thrown when the method failed due to an invalid operation.
public void SetUserName(string userName)
{
if (userName == null)
{
throw new ArgumentNullException("User name is null");
}
int ret = Interop.WiFi.AP.SetEapPassphrase(_apHandle, userName, null);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set username, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
///
/// Sets the password of EAP.
///
/// 3
/// The password
/// http://tizen.org/feature/network.wifi
/// Thrown when the Wi-Fi is not supported.
/// Thrown when the password is passed as null.
/// Thrown when the method failed due to an invalid parameter.
/// Thrown when the method failed due to an invalid operation.
public void SetPassword(string password)
{
if (password == null)
{
throw new ArgumentNullException("Password is null");
}
int ret = Interop.WiFi.AP.SetEapPassphrase(_apHandle, null, password);
if (ret != (int)WiFiError.None)
{
Log.Error(Globals.LogTag, "Failed to set password, Error - " + (WiFiError)ret);
WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
}
}
} //WiFiEapInformation
}