////////////////////////////////////////////////////////////////
//
// Copyright (c) 2007-2010 MetaGeek, LLC
//
// 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.Text;
using System.Net.NetworkInformation;
using Inssider.Properties;
namespace Inssider
{
public abstract class WirelessInterface
{
///
/// Gets the name of the interface
///
public abstract string Name
{
get;
}
///
/// Gets the description of the WirelessInterface
///
public abstract string Description
{
get;
}
///
/// Instructs the network driver to scan for available
/// networks.
///
public abstract void ScanNetworks();
///
/// Gets detailed information about the available wireless
/// networks.
///
/// an array of WirelessDetails objects, which
/// contain information about the available wirless networks, such
/// as MAC address and RSSI.
///
public abstract List GetWirelessDetails();
///
/// Creates an instance of the appropritate WirelessInterface based
/// on the current operating environment.
///
/// a WirelessInterface instance
public static WirelessInterface Create(NetworkInterface networkInterface) {
WirelessInterface wlanInterface = null;
try
{
if ((IsXP() && Settings.Default.driverMode == 0) || Settings.Default.driverMode == 2)
{
wlanInterface = new NdisWirelessInterface(networkInterface);
}
else
{
wlanInterface = new NativeWirelessInterface(networkInterface);
}
}
catch (ApplicationException ex)
{
if (Settings.Default.driverMode != 0)
{
Settings.Default.driverMode = 0;
wlanInterface = Create(networkInterface);
}
else
{
throw (ex);
}
}
return (wlanInterface);
} // End Create()
///
/// Determines if the current operating environment is Windows
/// XP
///
/// true of the underlying operating environment is Windows XP, false otherwise
protected static bool IsXP() {
return (System.Environment.OSVersion.Version.Major == 5) &&
(System.Environment.OSVersion.Version.Minor >= 1);
} // End IsXP()
///
/// Converts a list of raw rate values into a list of rates
/// in units of Mbits/second.
///
/// array of raw rate values
/// output list of rates in Mbs
protected void ConvertToMbs(ushort[] rates, List rateList)
{
ConvertToMbs(rates, rateList, false);
}
///
/// Converts a list of raw rate values into a list of rates
/// in units of Mbits/second.
///
/// array of raw rate values
/// utput list of rates in Mbs
/// indicates whether this is a type N network
protected void ConvertToMbs(ushort[] rates, List rateList, bool isTypeN) {
for (int i = 0; i < rates.Length; ++i) {
if (rates[i] > 0) {
rateList.Add((double)(rates[i] & 0x7FFF) * 0.5);
}
}
if (isTypeN)
{
rateList.Add(65.0f);
}
rateList.Sort(new Comparison(Compare));
}// End ConvertToMbs()
///
/// Converts a list of raw rate values into a list of rates
/// in units of Mbits/second.
///
/// array of raw rate values
/// output list of rates in Mbs
protected void ConvertToMbs(byte[] rates, List rateList)
{
ConvertToMbs(rates, rateList, false);
}
///
/// Converts a list of raw rate values into a list of rates
/// in units of Mbits/second.
///
/// array of raw rate values
/// utput list of rates in Mbs
/// indicates whether this is a type N network
/// indicates whether this is a type N network
protected void ConvertToMbs(byte[] rates, List rateList, bool isTypeN)
{
for (int i = 0; i < rates.Length; ++i)
{
if (rates[i] > 0)
{
rateList.Add((double)(rates[i] & 0x7F) * 0.5);
}
}
if (isTypeN)
{
rateList.Add(65.0f);
}
rateList.Sort(new Comparison(Compare));
}// End ConvertToMbs()
///
/// Compares two doubles for equality.
///
///
///
///
private int Compare(double d1, double d2) {
if (d1 > d2) {
return 1;
}
else if (d1 < d2) {
return -1;
}
else {
return 0;
}
}// End Compare()
///
/// Converts a frequency value to a designated WIFI channel
///
/// frequency to convert to a channel
/// the WIFI channel at the given frequency
protected uint ConvertToChannel(uint frequency) {
// 2.4 GHz
if ((frequency > 2400000) && (frequency < 2484000))
{
return (frequency - 2407000) / 5000;
}
else if ((frequency >= 2484000) && (frequency <= 2495000))
{
return 14;
}
// 5 GHz
else if ((frequency > 5000000) && (frequency < 5900000))
{
return (frequency - 5000000) / 5000;
}
// Unknown
else
{
return 0;
}
} // ConvertToChannel()
}
}