aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.WiFi/Win32/Helpers/WlanHelpers.cs
blob: d4a9173d1b8508af725e48b752d96c9ef586ac2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Tango.WiFi.Win32.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace Tango.WiFi.Win32.Helpers
{
	internal static class WlanHelpers
	{
		internal static WlanConnectionNotificationData? ParseWlanConnectionNotification(ref WlanNotificationData notifyData)
		{
			int expectedSize = Marshal.SizeOf(typeof(WlanConnectionNotificationData));
			if (notifyData.dataSize < expectedSize)
				return null;

			WlanConnectionNotificationData connNotifyData = (WlanConnectionNotificationData)Marshal.PtrToStructure(notifyData.dataPtr, typeof(WlanConnectionNotificationData));

			if (connNotifyData.wlanReasonCode == WlanReasonCode.Success)
			{
				long profileXmlPtrValue = notifyData.dataPtr.ToInt64() + Marshal.OffsetOf(typeof(WlanConnectionNotificationData), "profileXml").ToInt64();
				connNotifyData.profileXml = Marshal.PtrToStringUni(new IntPtr(profileXmlPtrValue));
			}

			return connNotifyData;
		}

		/// <summary>
		/// Gets a string that describes a specified reason code. NOTE: Not used!
		/// </summary>
		/// <param name="reasonCode">The reason code.</param>
		/// <returns>The string.</returns>
		internal static string GetStringForReasonCode(WlanReasonCode reasonCode)
		{
			StringBuilder sb = new StringBuilder(1024); // the 1024 size here is arbitrary; the WlanReasonCodeToString docs fail to specify a recommended size

			if (WlanInterop.WlanReasonCodeToString(reasonCode, sb.Capacity, sb, IntPtr.Zero) != 0)
			{
				sb.Clear(); // Not sure if we get junk in the stringbuilder buffer from WlanReasonCodeToString, clearing it to be sure. 
				sb.Append("Failed to retrieve reason code, probably too small buffer.");
			}

			return sb.ToString();
		}
	}
}