Windows operatsioon süsteemi WiFi API

From ICO wiki
Revision as of 10:08, 6 May 2010 by Ajastrem (talk | contribs)
Jump to navigationJump to search


Kui teie oskate programmerida ja teil on Windows Vista või kõrgem operatsiooni süsteem, siis saate ligipääs “varjatud” infole. Selleks teil tuleb vahele toppida (Interop) üks windows library “wlanapi.dll”. Kogu vajalik info saab pärida siit:

[DllImport("wlanapi.dll")]
public static extern int WlanGetNetworkBssList(
	[In] IntPtr clientHandle,
	[In, MarshalAs(UnmanagedType.LPStruct)] Guid interfaceGuid,
	[In] IntPtr dot11SsidInt,
	[In] Dot11BssType dot11BssType,
	[In] bool securityEnabled,
	IntPtr reservedPtr,
	[Out] out IntPtr wlanBssList
);

ja näide algorütm millega saab arvestada välja millist wifi kanalit võiks antud võrgus kasutada Access Pointi seadistamiseks. Algorütm iga kanali jaoks võttab signali tugevust ja liidab juurde kahtee ees pool olevad kanali tugevused ja kahte taga pool olevad kanali tugevused. Siis võrdleb neid ja tagastab neid kanalid millel on kõige vähem "müra". Algorütm on kirjutatud .NET c# keeles.

List<WiFiNetwork> NETWORKS = new List<WiFiNetwork>();

public List<int> AnalyzeResult() {
	string routerSSID = tbRouterSSID.Text.Trim();
	if (routerSSID == string.Empty) routerSSID = null;
	int[] strengthes = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	int[] counts = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	foreach (WiFiNetwork wifiNetwork in NETWORKS) {
		if (wifiNetwork.Name != routerSSID) {
			strengthes[wifiNetwork.Channel - 1] += wifiNetwork.SignalStrength;
			counts[wifiNetwork.Channel - 1] += 1;
		}
	}

	int[] results = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	int[] count_results = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	for (int i = 0; i < 11; i++) {
		for (int j = -2; j <= 2; j++) {
			int o = i + j;
			if (o > -1 && o < 11) {
				int l = strengthes[o];
				int c = counts[o];
				if (l > 0) {
					results[i] += l;
					count_results[i] += c;
				}
			}
		}
	}

	for (int i = 0; i < 11; i++) {
		results[i] = results[i] * count_results[i];
	}

	List<int> result = new List<int>();
	int min = results.Min();
	for (int i = 0; i < 11; i++) {
		if (results[i] == min) result.Add(i + 1);
	}

	return result;
}

        public class WiFiNetwork {
            public string Name { get; set; }
            public int Frequency { get; set; }
            public int SignalStrength { get; set; }

            public WiFiNetwork(string name, int frequency, int signalStrength) {
                this.Name = name;
                this.Frequency = frequency;
                this.SignalStrength = signalStrength;
            }

            public int Channel {
                get {
                    switch (Frequency) {
                        case 2412000:
                            return 1;
                        case 2417000:
                            return 2;
                        case 2422000:
                            return 3;
                        case 2427000:
                            return 4;
                        case 2432000:
                            return 5;
                        case 2437000:
                            return 6;
                        case 2442000:
                            return 7;
                        case 2447000:
                            return 8;
                        case 2452000:
                            return 9;
                        case 2457000:
                            return 10;
                        case 2462000:
                            return 11;
                        default:
                            return 1;
                    }
                }
            }
        }

Edukat wifi seadistamist!!!