diff --git a/WifiScan/Form1.cs b/WifiScan/Form1.cs index f95cf0d..1aeba93 100644 --- a/WifiScan/Form1.cs +++ b/WifiScan/Form1.cs @@ -1,5 +1,4 @@ -using NativeWifi; -using System; +using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; @@ -14,7 +13,6 @@ { public partial class Form1 : Form { - private WlanClient client = new WlanClient(); public Form1() { @@ -26,51 +24,46 @@ #region Tree Management - private TreeNode GetNodeFor(WlanClient.WlanInterface wlanIface) + private TreeNode GetNodeFor(WifiDevice dev) { foreach (TreeNode node in tvTree.Nodes) - { - Guid? nguid = node.Tag as Guid?; - if (nguid == wlanIface.InterfaceGuid) + if (dev == node.Tag) return node; - } - TreeNode nnode = tvTree.Nodes.Add(wlanIface.InterfaceDescription); - nnode.Tag = wlanIface.InterfaceGuid; + TreeNode nnode = tvTree.Nodes.Add(dev.Name); + nnode.Tag = dev; return nnode; } - private TreeNode GetNodeFor(Wlan.WlanBssEntry network, TreeNodeCollection nodes) + private TreeNode GetNodeFor(Wifi wifi, TreeNodeCollection nodes) { - string nbssid = network.GetBSSIDHex(); foreach (TreeNode node in nodes) { - String bssid = node.Tag as String; - if (bssid == nbssid) + if (wifi == node.Tag) return node; } - TreeNode nnode = nodes.Add(network.GetSSID()); - nnode.Tag = nbssid; + TreeNode nnode = nodes.Add(wifi.SSID); + nnode.Tag = wifi; nnode.ImageIndex = 1; nnode.SelectedImageIndex = 1; + nnode.ForeColor = wifi.Color; return nnode; } - + private void PopulateTree() { ArrayList nodes = new ArrayList(tvTree.Nodes); - foreach (WlanClient.WlanInterface wlanIface in client.Interfaces) + foreach (WifiDevice dev in WifiDevice.List()) { - TreeNode node = GetNodeFor(wlanIface); + TreeNode node = GetNodeFor(dev); nodes.Remove(node); - Wlan.WlanBssEntry[] wlanBssEntries = wlanIface.GetNetworkBssList(); - ArrayList netnodes = new ArrayList(wlanBssEntries); - foreach (Wlan.WlanBssEntry network in wlanBssEntries) + Wifi[] wifis = dev.Networks; + ArrayList netnodes = new ArrayList(node.Nodes); + foreach (Wifi wifi in wifis) { - TreeNode nwnode = GetNodeFor(network, node.Nodes); + TreeNode nwnode = GetNodeFor(wifi, node.Nodes); netnodes.Remove(nwnode); - network.Register(); } - foreach (TreeNode nwdel in nodes) + foreach (TreeNode nwdel in netnodes) node.Nodes.Remove(nwdel); } // remove old @@ -78,20 +71,20 @@ tvTree.Nodes.Remove(ndel); tvTree.ExpandAll(); // Take snapshoot of current wifi status and deletes registrations - Wifi.Snapshoot(); } #endregion private void WifiTimer_Tick(object sender, EventArgs e) { + WifiApi.Snapshoot(); PopulateTree(); scMain.Panel2.Invalidate(); } private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e) { - Wifi.Paint(e.Graphics, scMain.Panel2.ClientSize); + WifiApi.Paint(e.Graphics, scMain.Panel2.ClientSize); } } } diff --git a/WifiScan/Wifi.cs b/WifiScan/Wifi.cs index eec5078..f507fe1 100644 --- a/WifiScan/Wifi.cs +++ b/WifiScan/Wifi.cs @@ -3,113 +3,44 @@ using System.Collections; using System.Collections.Generic; using System.Drawing; +using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WifiScan { - static class Wifi + class Wifi { - public static Random rnd = new Random(); - private static Pen pLine = new Pen(Color.Silver); - private static Pen pLineDark = new Pen(Color.Gray); - private static Font font = new Font(FontFamily.GenericSansSerif, 8); - private static Brush bText = new SolidBrush(Color.Silver); + private Wlan.WlanBssEntry network; + private string ssid; + private string bssid; + private DateTime lastSeen; + private Color color = Color.FromArgb(WifiApi.rnd.Next(200) + 56, WifiApi.rnd.Next(200) + 56, WifiApi.rnd.Next(200) + 56); - static Dictionary networks = new Dictionary(); - static List> snap = new List>(); - static Dictionary colors = new Dictionary(); - - public static string GetBSSIDHex(this Wlan.WlanBssEntry network) + public Wifi(Wlan.WlanBssEntry network) { - byte[] macAddr = network.dot11Bssid; - StringBuilder tMac = new StringBuilder(); - for (int i = 0; i < macAddr.Length; i++) - tMac.Append(macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper()); - return tMac.ToString(); + this.network = network; + bssid = network.GetBSSIDHex(); + ssid = network.GetSSID(); + Notify(); } - public static string GetSSID(this Wlan.WlanBssEntry network) + public void Notify() { - return ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString(); + lastSeen = DateTime.Now; } - public static void Register(this Wlan.WlanBssEntry network) + public WifiInfo GetInfo() { - string bssid = network.GetBSSIDHex(); - Wlan.WlanBssEntry net; - if (networks.TryGetValue(bssid, out net) && net.linkQuality > network.linkQuality) - return; - networks[bssid] = network; + return new WifiInfo(this); } - public static void Snapshoot() - { - List step = new List(); - step.AddRange(networks.Values); - snap.Add(step); - networks.Clear(); - } - - private static List GetLastScan() - { - if (snap.Count < 1) - return new List(); - return snap[snap.Count - 1]; - } - - private static List GetNetworksInChannel(int ch) - { - return null; - //List scan = GetLastScan(); - //foreach(Wlan.WlanBssEntry wifi in scan) - //{ - // if (wifi.ch) - //} - } - - #region Charting - const int CHART_FOOT = 20; - - private static Color GetNetworkColor(string bssid) - { - Color cl; - if (!colors.TryGetValue(bssid, out cl)) - colors[bssid] = cl = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)); - return cl; - } - - public static void Paint(Graphics g, Size sz) - { - g.Clear(Color.Black); - //Draw Guidelines - float scale = g.DpiX / 96; - float hh2 = sz.Height / 2f; - g.DrawLine(pLine, new PointF(0, hh2), new PointF(sz.Width, hh2)); - for (int i = 1; i < 4; i++) - { - float hl = (hh2 * i) / 4; - float hl2 = hl + hh2; - g.DrawLine(pLineDark, new PointF(0, hl), new PointF(sz.Width, hl)); - g.DrawLine(pLineDark, new PointF(0, hl2), new PointF(sz.Width, hl2)); - } - //Draw Channels - List scan = GetLastScan(); - float chw6 = (sz.Width * 4) / 16f; - foreach (Wlan.WlanBssEntry wifi in scan) - { - float he = (sz.Height * wifi.linkQuality) / 100f; - Pen pw = new Pen(GetNetworkColor(wifi.GetBSSIDHex()), 2f); - g.DrawEllipse(pw, new RectangleF(0, sz.Height - (he / 2f), chw6, he)); - //wifi.chCenterFrequency - } - for (int i = 1; i <= 14; i++) - { - float x = (sz.Width * (i + 1)) / 16f; - g.DrawString("" + i, font, bText, new PointF(x, sz.Height - (CHART_FOOT * scale))); - } - } - #endregion + public string BSSID => bssid; + public string SSID => ssid; + public bool Active => (DateTime.Now - lastSeen).Seconds < 10; + public Color Color => color; + public uint Signal => network.linkQuality; + public uint Channel => network.GetChannel(); } } diff --git a/WifiScan/WifiDevice.cs b/WifiScan/WifiDevice.cs new file mode 100644 index 0000000..80d3392 --- /dev/null +++ b/WifiScan/WifiDevice.cs @@ -0,0 +1,104 @@ +using NativeWifi; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WifiScan +{ + class WifiDevice + { + + #region Alive Control + + private static WlanClient client = new WlanClient(); + private static List devices = new List(); + + private static WifiDevice GetFor(WlanClient.WlanInterface iface) + { + foreach (WifiDevice d in devices) + { + if (d.iface.InterfaceGuid == iface.InterfaceGuid) + return d; + } + WifiDevice ndev = new WifiDevice(iface); + devices.Add(ndev); + return ndev; + } + + public static List List() + { + List del = new List(devices); + foreach (WlanClient.WlanInterface wlanIface in client.Interfaces) + { + WifiDevice dev = GetFor(wlanIface); + del.Remove(dev); + } + return new List(devices); + } + + #endregion + + public static Wifi[] FetchAllNetworks() + { + List wifis = new List(); + foreach (WifiDevice dev in devices) + foreach (Wifi wifi in dev.Networks) + if (!wifis.Contains(wifi)) + wifis.Add(wifi); + return wifis.ToArray(); + } + + private WlanClient.WlanInterface iface; + private DateTime lastScan; + private List networks = new List(); + + private WifiDevice(WlanClient.WlanInterface iface) + { + this.iface = iface; + Scan(); + } + + private Wifi GetFor(Wlan.WlanBssEntry network) + { + string bssid = network.GetBSSIDHex(); + foreach (Wifi wifi in networks) + { + if (wifi.BSSID == bssid) + return wifi; + } + Wifi nwifi = new Wifi(network); + networks.Add(nwifi); + return nwifi; + } + + private Wifi[] GetNetworks() + { + Scan(); + Wlan.WlanBssEntry[] wlanBssEntries = iface.GetNetworkBssList(); + foreach (Wlan.WlanBssEntry network in wlanBssEntries) + { + Wifi wifi = GetFor(network); + wifi.Notify(); + } + Wifi[] all = networks.ToArray(); + foreach (Wifi wifi in all) + if (!wifi.Active) + networks.Remove(wifi); + return networks.ToArray(); + } + + public void Scan() + { + if (DateTime.Now.Subtract(lastScan).Seconds < 3) + return; + lastScan = DateTime.Now; + iface.Scan(); + } + + public Wifi[] Networks => GetNetworks(); + + public string Name => iface.InterfaceDescription; + } +} diff --git a/WifiScan/WifiInfo.cs b/WifiScan/WifiInfo.cs new file mode 100644 index 0000000..74f49e1 --- /dev/null +++ b/WifiScan/WifiInfo.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WifiScan +{ + class WifiInfo + { + public WifiInfo(Wifi wifi) + { + SSID = wifi.SSID; + Signal = wifi.Signal; + Channel = wifi.Channel; + Color = wifi.Color; + } + + public string SSID { get; } + public uint Signal { get; } + public uint Channel { get; } + public Color Color { get; } + } +} diff --git a/WifiScan/WifiScan.csproj b/WifiScan/WifiScan.csproj index e71411c..e77ac9e 100644 --- a/WifiScan/WifiScan.csproj +++ b/WifiScan/WifiScan.csproj @@ -57,7 +57,10 @@ + + + Form1.cs diff --git a/WifiScan/WifiaPI.cs b/WifiScan/WifiaPI.cs new file mode 100644 index 0000000..a788660 --- /dev/null +++ b/WifiScan/WifiaPI.cs @@ -0,0 +1,115 @@ +using NativeWifi; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WifiScan +{ + static class WifiApi + { + public static Random rnd = new Random(); + private static Pen pLineLight = new Pen(Color.WhiteSmoke, 3); + private static Pen pLine = new Pen(Color.Silver); + private static Pen pLineDark = new Pen(Color.Gray); + private static Pen pLineDark2 = new Pen(Color.FromArgb(30, 30, 30)); + private static Font font = new Font(FontFamily.GenericSansSerif, 8); + private static Brush bText = new SolidBrush(Color.Silver); + + static List> snap = new List>(); + + #region Network Extensions + public static string GetBSSIDHex(this Wlan.WlanBssEntry network) + { + byte[] macAddr = network.dot11Bssid; + StringBuilder tMac = new StringBuilder(); + for (int i = 0; i < macAddr.Length; i++) + tMac.Append(macAddr[i].ToString("x2").PadLeft(2, '0').ToUpper()); + return tMac.ToString(); + } + + public static string GetSSID(this Wlan.WlanBssEntry network) + { + string ssid = ASCIIEncoding.ASCII.GetString(network.dot11Ssid.SSID).ToString(); + ssid = ssid.Replace('\0', ' '); + ssid = ssid.Trim(); + return ssid; + } + + public static uint GetChannel(this Wlan.WlanBssEntry wifi) + { + uint ch = (wifi.chCenterFrequency - 2400000) / 5000; + return ch - 1; + } + + #endregion + + public static void Snapshoot() + { + List step = new List(); + foreach(Wifi wifi in WifiDevice.FetchAllNetworks()) + { + step.Add(wifi.GetInfo()); + } + snap.Add(step); + } + + private static List GetLastScan() + { + if (snap.Count < 1) + return new List(); + return snap[snap.Count - 1]; + } + + private static List GetNetworksInChannel(int ch) + { + List result = new List(); + List scan = GetLastScan(); + foreach (WifiInfo wii in scan) + if (wii.Channel == ch) + result.Add(wii); + return result; + } + + #region Charting + const int CHART_FOOT = 20; + + public static void Paint(Graphics g, Size sz) + { + g.Clear(Color.Black); + g.SmoothingMode = SmoothingMode.AntiAlias; + //Draw Guidelines + float scale = g.DpiX / 96; + float hh2 = sz.Height / 2f; + g.DrawLine(pLineLight, new Point(0, (int)hh2), new Point(sz.Width, (int)hh2)); + for (int i = 1; i < 4; i++) + { + float hl = (hh2 * i) / 4; + float hl2 = hl + hh2; + g.DrawLine(pLineDark, new PointF(0, hl), new PointF(sz.Width, hl)); + g.DrawLine(pLineDark, new PointF(0, hl2), new PointF(sz.Width, hl2)); + } + //Draw Channels + float chw6 = (sz.Width * 4) / 16f; + + for (int i = 1; i <= 14; i++) + { + int x = (int)((sz.Width * (i + 1)) / 16f); + g.DrawString("" + i, font, bText, new PointF(x-10, sz.Height - (CHART_FOOT * scale))); + g.DrawLine(pLineDark2, new PointF(x, sz.Height), new PointF(x, hh2)); + List chw = GetNetworksInChannel(i); + foreach (WifiInfo wii in chw) + { + float he = (sz.Height * wii.Signal) / 100f; + Pen pw = new Pen(wii.Color, 2f); + g.DrawEllipse(pw, new RectangleF(x - (chw6 / 2), sz.Height - (he / 2f), chw6, he)); + } + } + } + #endregion + } +}