Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions Source/NETworkManager.Converters/LvlChartsHeaderConverter.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
<ProjectReference Include="..\NETworkManager.Utilities\NETworkManager.Utilities.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="LiveCharts.Wpf" Version="0.9.7" />
<PackageReference Include="MahApps.Metro" Version="2.4.11" />

</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System;
using System.Globalization;
using System.Windows.Data;

namespace NETworkManager.Converters;

public sealed class WiFiDBMReverseConverter : IValueConverter
public sealed class WiFiChannelBandwidthToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !(value is double) ? "-/-" : $"-{100 - (double)value} dBm";
return value is not int bandwidth ? "-/-" : $"{bandwidth} MHz";
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down
22 changes: 20 additions & 2 deletions Source/NETworkManager.Localization/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Source/NETworkManager.Localization/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2274,12 +2274,18 @@ $$hostname$$ --&gt; Hostname</value>
<data name="Channel" xml:space="preserve">
<value>Channel</value>
</data>
<data name="ChannelWidth" xml:space="preserve">
<value>Channel width</value>
</data>
<data name="GHz2dot4" xml:space="preserve">
<value>2.4 GHz</value>
</data>
<data name="GHz5" xml:space="preserve">
<value>5 GHz</value>
</data>
<data name="GHz2dot4And5" xml:space="preserve">
<value>2.4 &amp; 5 GHz</value>
</data>
<data name="SignalStrength" xml:space="preserve">
<value>Signal strength</value>
</data>
Expand Down
115 changes: 65 additions & 50 deletions Source/NETworkManager.Models/Network/WiFi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,25 @@ public static async Task<WiFiNetworkScanInfo> GetNetworksAsync(WiFiAdapter adapt
// Try to get the current connected Wi-Fi network of this network adapter
var (_, bssid) = TryGetConnectedNetworkFromWiFiAdapter(adapter.NetworkAdapter.NetworkAdapterId.ToString());

// The WinRT API does not expose the channel bandwidth, so it is read from the native
// BSS list (wlanapi.dll) and matched by BSSID below. Failures yield an empty map and we
// fall back to a heuristic per network.
var channelWidths = WlanApi.GetBssChannelWidths(adapter.NetworkAdapter.NetworkAdapterId);

var wifiNetworkInfos = new List<WiFiNetworkInfo>();

foreach (var availableNetwork in adapter.NetworkReport.AvailableNetworks)
{
var channelFrequencyInGigahertz = ConvertChannelFrequencyToGigahertz(availableNetwork.ChannelCenterFrequencyInKilohertz);
var radio = GetWiFiRadioFromChannelFrequency(channelFrequencyInGigahertz);

var wifiNetworkInfo = new WiFiNetworkInfo
{
AvailableNetwork = availableNetwork,
Radio = GetWiFiRadioFromChannelFrequency(channelFrequencyInGigahertz),
Radio = radio,
ChannelCenterFrequencyInGigahertz = channelFrequencyInGigahertz,
Channel = GetChannelFromChannelFrequency(channelFrequencyInGigahertz),
ChannelBandwidth = GetChannelBandwidth(channelWidths, availableNetwork.Bssid, radio, availableNetwork.PhyKind),
IsHidden = string.IsNullOrEmpty(availableNetwork.Ssid),
IsConnected = availableNetwork.Bssid.Equals(bssid, StringComparison.OrdinalIgnoreCase),
NetworkAuthenticationType = GetHumanReadableNetworkAuthenticationType(availableNetwork.SecuritySettings.NetworkAuthenticationType),
Expand Down Expand Up @@ -258,62 +265,36 @@ public static async Task<bool> IsWpsAvailable(WiFiAdapter adapter, WiFiAvailable
/// Get the Wi-Fi channel from channel frequency.
/// </summary>
/// <param name="gigahertz">Input like 2.422 or 5.240.</param>
/// <returns>WiFi channel like 3 or 48.</returns>
public static int GetChannelFromChannelFrequency(double gigahertz)
/// <returns>Wi-Fi channel like 3 or 48.</returns>
private static int GetChannelFromChannelFrequency(double gigahertz)
{
return gigahertz switch
{
// 2.4 GHz
2.412 => 1,
2.417 => 2,
2.422 => 3,
2.427 => 4,
2.432 => 5,
2.437 => 6,
2.442 => 7,
2.447 => 8,
2.452 => 9,
2.457 => 10,
2.462 => 11,
2.467 => 12,
2.472 => 13,
2.484 => 14, // Most countries do not allow this channel
// 5 GHz
5.180 => 36, // UNII-1
5.200 => 40, // UNII-1
5.220 => 44, // UNII-1
5.240 => 48, // UNII-1
5.260 => 52, // UNII-2, DFS
5.280 => 56, // UNII-2, DFS
5.300 => 60, // UNII-2, DFS
5.320 => 64, // UNII-2, DFS
5.500 => 100, // UNII-2 Extended, DFS
5.520 => 104, // UNII-2 Extended, DFS
5.540 => 108, // UNII-2 Extended, DFS
5.560 => 112, // UNII-2 Extended, DFS
5.580 => 116, // UNII-2 Extended, DFS
5.600 => 120, // UNII-2 Extended, DFS
5.620 => 124, // UNII-2 Extended, DFS
5.640 => 128, // UNII-2 Extended, DFS
5.660 => 132, // UNII-2 Extended, DFS
5.680 => 136, // UNII-2 Extended, DFS
5.700 => 140, // UNII-2 Extended, DFS
5.720 => 144, // UNII-2 Extended, DFS
5.745 => 149, // UNII-3
5.765 => 153, // UNII-3
5.785 => 157, // UNII-3
5.805 => 161, // UNII-3
5.825 => 165, // UNII-3
_ => -1
};
// Convert to integer MHz to avoid floating-point precision issues from the
// kilohertz / 1_000_000.0 conversion in ConvertChannelFrequencyToGigahertz.
var mhz = (int)Math.Round(gigahertz * 1000);

// 2.4 GHz: channels 1-13 follow f = 2407 + ch×5 MHz; channel 14 is at 2484 MHz
if (mhz is >= 2412 and <= 2472)
return (mhz - 2407) / 5;
if (mhz == 2484)
return 14;

// 5 GHz: channels follow f = 5000 + ch×5 MHz
if (mhz is >= 5180 and <= 5825)
return (mhz - 5000) / 5;

// 6 GHz: channels 1-233 follow f = 5950 + ch×5 MHz
if (mhz is >= 5955 and <= 7115)
return (mhz - 5950) / 5;

return -1;
}

/// <summary>
/// Convert the channel frequency to gigahertz.
/// </summary>
/// <param name="kilohertz">Frequency in kilohertz like 2422000 or 5240000.</param>
/// <returns>Frequency in gigahertz like 2.422 or 5.240.</returns>
public static double ConvertChannelFrequencyToGigahertz(int kilohertz)
private static double ConvertChannelFrequencyToGigahertz(int kilohertz)
{
return Convert.ToDouble(kilohertz) / 1000 / 1000;
}
Expand All @@ -323,7 +304,7 @@ public static double ConvertChannelFrequencyToGigahertz(int kilohertz)
/// </summary>
/// <param name="gigahertz">Frequency in gigahertz like 2.412 or 5.180.</param>
/// <returns>Radio like 2.4 GHz, 5 GHz, etc. as <see cref="WiFiRadio" />.</returns>
public static WiFiRadio GetWiFiRadioFromChannelFrequency(double gigahertz)
private static WiFiRadio GetWiFiRadioFromChannelFrequency(double gigahertz)
{
return gigahertz switch
{
Expand All @@ -334,6 +315,40 @@ public static WiFiRadio GetWiFiRadioFromChannelFrequency(double gigahertz)
};
}

/// <summary>
/// Determines the channel bandwidth (MHz) for a network. Prefers the value parsed from the
/// native BSS list (<see cref="WlanApi" />); if unavailable, falls back to a heuristic based
/// on the radio band and PHY kind, and finally to 20 MHz.
/// </summary>
private static int GetChannelBandwidth(IReadOnlyDictionary<string, int> channelWidths, string networkBssid,
WiFiRadio radio, WiFiPhyKind phyKind)
{
if (!string.IsNullOrEmpty(networkBssid) && channelWidths.TryGetValue(networkBssid, out var width) && width > 0)
return width;

return GetHeuristicChannelBandwidth(radio, phyKind);
}

/// <summary>
/// Estimates the channel bandwidth (MHz) from the radio band and PHY kind when the exact
/// value could not be read from the BSS list.
/// </summary>
private static int GetHeuristicChannelBandwidth(WiFiRadio radio, WiFiPhyKind phyKind)
{
return radio switch
{
WiFiRadio.GHz2dot4 => phyKind == WiFiPhyKind.HT ? 40 : 20,
WiFiRadio.GHz5 => phyKind switch
{
WiFiPhyKind.Vht or WiFiPhyKind.HE or WiFiPhyKind.Eht => 80,
WiFiPhyKind.HT => 40,
_ => 20
},
WiFiRadio.GHz6 => phyKind is WiFiPhyKind.HE or WiFiPhyKind.Eht ? 160 : 20,
_ => 20
};
}

/// <summary>
/// Get the human-readable network authentication type.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Source/NETworkManager.Models/Network/WiFiNetworkInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public WiFiNetworkInfo()
/// </summary>
public int Channel { get; set; }

/// <summary>
/// The channel bandwidth in MHz (e.g. 20, 40, 80, 160). Always &gt;= 20: native value from
/// the BSS list when available, otherwise estimated from radio band and PHY kind.
/// </summary>
public int ChannelBandwidth { get; set; }
Comment on lines +40 to +44

/// <summary>
/// Indicates if the Wi-Fi network Ssid is hidden.
/// </summary>
Expand Down
Loading
Loading